Beispiel #1
0
def makeResponse(trueTree,measTree,bins,accCuts,massReq,plotName,var):
    tTree=trueTree.CopyTree(accCuts+"&&"+massReq)
    mTree=measTree.CopyTree(massReq)

    hist_measured=TH1F("hist_measured","hist_measured",len(bins)-1,array('d',bins))
    hist_truth=TH1F("hist_truth","hist_truth",len(bins)-1,array('d',bins))
    print "about to make the RooUnfoldResponse"
    print hist_truth
    print hist_measured
    response = RooUnfoldResponse(hist_measured, hist_truth);

    recEvents={}
    nr=0
    for i in mTree:
        recEvents[i.EVENT]={"zzMass":i.mass,
                "zzPt":i.pt,
                "gMass":i.gMass,
                "evt":i.EVENT,
                "gz1Mass":i.gz1Mass,
                "gz2Mass":i.gz2Mass,
                "dR_z1_gz1":i.dR_z1_gz1,
                "dR_z2_gz2":i.dR_z2_gz2,
                "dR_z1_gz2":i.dR_z1_gz2,
                "dR_z2_gz1":i.dR_z2_gz1
                }
        if i.z1Pt > i.z2Pt:
            recEvents[i.EVENT]["z1_eta_by_pt"]=i.z1Eta
            recEvents[i.EVENT]["z2_eta_by_pt"]=i.z2Eta
            recEvents[i.EVENT]["z1_pt_by_pt"]=i.z1Pt
            recEvents[i.EVENT]["z2_pt_by_pt"]=i.z2Pt
        else:
            recEvents[i.EVENT]["z1_eta_by_pt"]=i.z2Eta
            recEvents[i.EVENT]["z2_eta_by_pt"]=i.z1Eta
            recEvents[i.EVENT]["z1_pt_by_pt"]=i.z2Pt
            recEvents[i.EVENT]["z2_pt_by_pt"]=i.z1Pt
        recEvents[i.EVENT]["leading_lep_pt"]=max(max(max(i.z1l1Pt,i.z1l2Pt),i.z2l1Pt),i.z2l2Pt)
        recEvents[i.EVENT]["dR_Zs"]=sqrt( (i.z1Eta-i.z2Eta)**2 + (i.z1Phi-i.z2Phi)**2)
        recEvents[i.EVENT]["dPhi_Zs"] = abs(dPhi(i.z1Phi, i.z2Phi))

    recSet=set()
    for i in recEvents:
        recSet.add(i)

    tTrueEvents={}
    for i in tTree: #loop over truth events
        tTrueEvents[i.EVENT]={'zzMass':i.zzMass,
                'zzPt':i.zzPt,
                'evt':i.EVENT}
        if i.z1Pt > i.z2Pt:
            tTrueEvents[i.EVENT]["z1_eta_by_pt"]=i.z1Eta
            tTrueEvents[i.EVENT]["z2_eta_by_pt"]=i.z2Eta
            tTrueEvents[i.EVENT]["z1_pt_by_pt"]=i.z1Pt
            tTrueEvents[i.EVENT]["z2_pt_by_pt"]=i.z2Pt
        else:
            tTrueEvents[i.EVENT]["z1_eta_by_pt"]=i.z2Eta
            tTrueEvents[i.EVENT]["z2_eta_by_pt"]=i.z1Eta
            tTrueEvents[i.EVENT]["z1_pt_by_pt"]=i.z2Pt
            tTrueEvents[i.EVENT]["z2_pt_by_pt"]=i.z1Pt
        tTrueEvents[i.EVENT]["leading_lep_pt"]=max(max(max(i.z1l1Pt,i.z1l2Pt),i.z2l1Pt),i.z2l2Pt)
        tTrueEvents[i.EVENT]["dR_Zs"]=sqrt( (i.z1Eta-i.z2Eta)**2 + (i.z1Phi-i.z2Phi)**2)
        tTrueEvents[i.EVENT]["dPhi_Zs"] = abs(dPhi(i.z1Phi, i.z2Phi))
    trueSet=set()
    for i in tTrueEvents:
        trueSet.add(i)

    hit=0
    miss=0
    agree=0
    # Training
    measEvents={}
    for event in trueSet:
        if event in recSet:
            agree=agree+1
            #hack hack hack to make sure I can still close
            if (recEvents[event]['dR_z1_gz1'] < 0.5 and recEvents[event]['dR_z2_gz2'] < 0.5) or (recEvents[event]['dR_z1_gz2']<0.5 and recEvents[event]['dR_z2_gz1']<0.5):
               measEvents[event]=recEvents[event]
               response.Fill(recEvents[event][var],tTrueEvents[event][var])
               hit=hit+1
            else:
                response.Miss(tTrueEvents[event][var])
                miss=miss+1
        else:
            response.Miss(tTrueEvents[event][var])
            miss=miss+1
    print len(trueSet),"true events used in training"
    print len(recSet),"measured events used for training"
    print len(recSet-trueSet),"measured not in true"
    print "hit:",hit
    print "miss:",miss
    print "in both true and rec:",agree
    c1=TCanvas("c1","c1",600,600)
    c1.cd()
    #  Response matrix as a 2D-histogram: (x,y)=(measured,truth) from http://hepunx.rl.ac.uk/~adye/software/unfold/htmldoc/RooUnfoldResponse.html#RooUnfoldResponse:Hresponse%1
    response.Hresponse().GetXaxis().SetTitle(plotName+" (Measured)")
    response.Hresponse().GetYaxis().SetTitle(plotName+" (Truth)")
    response.Hresponse().Draw("colz")
    c1.SaveAs("diffDists/"+plotName+"_responseMat.png")
    c1.SaveAs("diffDists/"+plotName+"_responseMat.C")
    c1.Delete()
    return response
def main(args):
    fin = TFile(args.filein,"update")
    print args.trees
    stuff=[]
    if "all" in args.trees:
        for t in fin.GetListOfKeys():
            if t.ReadObj().Class().InheritsFrom(TTree.Class()) is True:
                stuff.append(t.GetName())
            else:
                continue
    else:
        stuff=[i for i in args.trees]
    for i in stuff:
        if "genEventTree" not in i and "mmee" not in i and "eemm" not in i and "eeee" not in i and "mmmm" not in i and "llll" not in i:
            stuff.remove(i)

    # vars needed as inputs: z1Eta, z2Eta, z1Pt, z2Pt, z1Phi, z2Phi, mass, pt, zilj[Eta, Phi, Pt], z1Mass, z2Mass
    ins=["zzMass", "zzPt", "zzEta", "z1Pt", "z2Pt", "z1Eta", "z2Eta", "z1Phi", "z2Phi"]
    for i in ["z1l1Pt", "z1l1Eta", "z1l1Phi", "z1Mass", "z1l2Pt", "z1l2Eta", "z1l2Phi", "z2Mass"]:
        ins.append(i)
    for i in ["z2l1Pt", "z2l1Eta", "z2l1Phi", "z2l2Pt", "z2l2Eta", "z2l2Phi"]:
        ins.append(i)
    inputVars={}
    fin = TFile(args.filein,"update")

    for t in stuff:
        fin = TFile(args.filein,"update")
        tree = fin.Get(t)
        newtree = TTree(tree.GetName()+"_moreVars",tree.GetName()+"_moreVars")

        n={}
        leplegs=["z1l1","z1l2","z2l1","z2l2"]

        for i in ins:
            inputVars[i] = -137.0


        # ouput vars : z_Eta (leading by Pt), z_Pt (leading by Pt), dR between Z, dPhi between Z, leading lepton Pt
        outs=["z1_eta_by_pt", "z2_eta_by_pt", "z1_pt_by_pt","z2_pt_by_pt", "dR_Zs", "dPhi_Zs", "leading_lep_pt"]
        for outvar in outs:
            # TODO do I need these genlevel for reco branch??
            n[outvar] = N.zeros(1,dtype=float)
            newtree.Branch(outvar, n[outvar],outvar+"/d")

        for i in tree:
            # get ins
            # these will have to be different if it's the truth/measured tree...
            for var in ["Mass" , "Pt", "Eta"]:
                if "gen" in tree.GetName():
                    inputVars["zz"+var] = i.GetLeaf("zz"+var).GetValue()
                else:
                    inputVars["zzMass"] = i.GetLeaf("mass").GetValue()
#                    inputVars["zzEta"] = i.GetLeaf("eta").GetValue()
                    inputVars["zzPt"] = i.GetLeaf("pt").GetValue()
            for  var in ["Mass", "Pt", "Eta", "Phi"]:
                inputVars["z1"+var] = i.GetLeaf("z1"+var).GetValue()
            for  var in ["Mass", "Pt", "Eta", "Phi"]:
                inputVars["z2"+var] = i.GetLeaf("z2"+var).GetValue()
            for l in range(len(leplegs)): # for each lepton, figure out type and get its weight.
                try:
                    inputVars[leplegs[l]+"Pt"] = i.GetLeaf(leplegs[l]+"Pt").GetValue()
                    inputVars[leplegs[l]+"Eta"] = i.GetLeaf(leplegs[l]+"Eta").GetValue()
                    inputVars[leplegs[l]+"Phi"] = i.GetLeaf(leplegs[l]+"Phi").GetValue()
                except ReferenceError: # this tree sucks and doesn't have the right vars
                    print "Trying to get",leplegs[l],"from tree",tree.GetName()
                    continue

            #set outs
            if inputVars["z1Pt"]>inputVars["z2Pt"]:
                n["z1_eta_by_pt"][0]=inputVars["z1Eta"]
                n["z1_pt_by_pt"][0]=inputVars["z1Pt"]
                n["z2_eta_by_pt"][0]=inputVars["z2Eta"]
                n["z2_pt_by_pt"][0]=inputVars["z2Pt"]
            else:
                n["z1_eta_by_pt"][0]=inputVars["z2Eta"]
                n["z1_pt_by_pt"][0]=inputVars["z2Pt"]
                n["z2_eta_by_pt"][0]=inputVars["z1Eta"]
                n["z2_pt_by_pt"][0]=inputVars["z1Pt"]
            n["leading_lep_pt"][0]=max(max(max(inputVars["z1l1Pt"],inputVars["z1l2Pt"]),inputVars["z2l1Pt"]),inputVars["z2l2Pt"])
            n["dR_Zs"][0]=sqrt( (inputVars["z1Eta"]-inputVars["z2Eta"])**2 + (inputVars["z1Phi"]-inputVars["z2Phi"])**2)
            n["dPhi_Zs"][0] = dPhi(inputVars["z1Phi"], inputVars["z2Phi"])
            newtree.Fill()
        print tree.GetName(),"has",tree.GetEntries(),"entries"
        print newtree.GetName(),"has",tree.GetEntries(),"entries"
        newtree.Write()
        tree.AddFriend(newtree.GetName())
        tree.Write("",TObject.kOverwrite)
        fin.Close()