data = DoubleMuon_Run2015D data['isData']=True #get the TChains for each sample for s in backgrounds+[data]: s['chain'] = getChain(s,histname="") #plots plots = {\ 'nVert': {'title':'nVert', 'name':'nVert', 'binning': [50,0,50], 'histo':{}}, } #make plot in each sample: for s in backgrounds: for pk in plots.keys(): plots[pk]['histo'][s['name']] = getPlotFromChain(s['chain'], plots[pk]['name'], plots[pk]['binning'], cutString = preselection) for s in [data]: for pk in plots.keys(): plots[pk]['histo'][s['name']] = getPlotFromChain(s['chain'], plots[pk]['name'], plots[pk]['binning'], cutString = preselection+"&&"+dataCut) for pk in plots.keys(): plots[pk]['sum'] = plots[pk]['histo'][backgrounds[0]['name']].Clone() for b in backgrounds[1:]: plots[pk]['sum'].Add(plots[pk]['histo'][b['name']]) #Some coloring TTJets_50ns["color"]=ROOT.kRed TTJets_25ns["color"]=ROOT.kRed WJetsHTToLNu_25ns["color"]=ROOT.kGreen WJetsToLNu_25ns["color"]=ROOT.kGreen
#get the TChains for each sample for s in backgrounds: s['chain'] = getChain(s, histname="") #plots plots = {\ 'nTrueInt': {'title':'nTrueInt', 'name':'nTrueInt', 'binning': [50,0,50], 'histo':{}}, } #make plot in each sample: for s in backgrounds: for pk in plots.keys(): plots[pk]['histo'][s['name']] = getPlotFromChain( s['chain'], plots[pk]['name'], plots[pk]['binning'], cutString=preselection) for pk in plots.keys(): plots[pk]['sum'] = plots[pk]['histo'][backgrounds[0]['name']].Clone() for b in backgrounds[1:]: plots[pk]['sum'].Add(plots[pk]['histo'][b['name']]) #Some coloring TTJets_Lep["color"] = ROOT.kBlack WJetsToLNu["color"] = ROOT.kGreen #TTVH["color"]=ROOT.kMagenta DY["color"] = ROOT.kBlue diBoson["color"] = ROOT.kRed QCD_Mu5["color"] = ROOT.kCyan
# backgrounds = [TTJets_25ns, DY_25ns, singleTop_25ns, diBoson_25ns, WJetsHTToLNu_25ns]#, QCD] for b in backgrounds: b["isData"] = False # get the TChains for each sample for s in backgrounds: s["chain"] = getChain(s, histname="") # plots plots = {"nTrueInt": {"title": "nTrueInt", "name": "nTrueInt", "binning": [50, 0, 50], "histo": {}}} # make plot in each sample: for s in backgrounds: for pk in plots.keys(): plots[pk]["histo"][s["name"]] = getPlotFromChain( s["chain"], plots[pk]["name"], plots[pk]["binning"], cutString=preselection ) for pk in plots.keys(): plots[pk]["sum"] = plots[pk]["histo"][backgrounds[0]["name"]].Clone() for b in backgrounds[1:]: plots[pk]["sum"].Add(plots[pk]["histo"][b["name"]]) # Some coloring TTJets_Lep["color"] = ROOT.kBlack WJetsToLNu["color"] = ROOT.kGreen # TTVH["color"]=ROOT.kMagenta DY["color"] = ROOT.kBlue diBoson["color"] = ROOT.kRed QCD_Mu5["color"] = ROOT.kCyan
def loopAndFill(stacks, mode="loop"): allSamples=[] allSampleNames=[] allPlots = [] usedBranches = [] for s in stacks: usedBranches = list(set(usedBranches+s.usedBranches)) for l in s.stackLists: for p in l: allPlots.append(p) if p.leaf: if not p.leaf in usedBranches: usedBranches.append(p.leaf) if p.weightString and not p.weightString in usedBranches: usedBranches.append(p.weightString) else: usedBranches = list(set(usedBranches+p.usedBranches)) if not p.sample in allSamples: assert p.sample.has_key('dir') or p.sample.has_key('dirname'), "Missing key dir or dirname in sample %s"%repr(p.sample) allSamples.append(p.sample) if mode=='loop': assert not any ([p.string for p in allPlots]), "Loop mode is %s but specified 'string' for: %s"%(mode, ", ".join([p.name for p in allPlots if p.string])) if mode=='draw': assert all([p.string for p in allPlots]), "Loop mode is %s but specified no 'string' for: %s"%(mode, ", ".join([p.name for p in allPlots if not p.string])) print "Found",len(allSamples),'different samples:',", ".join(s['name'] for s in allSamples) for s in allSamples: cutStringForSample=[] plotsPerCutForSample={} # print s['name'], s.has_key('isData'), s.has_key('isData') and s['isData'], s.has_key('isData') and s['isData'] and s.has_key('dataCut') for p in allPlots: if p.sample==s: cut = p.cut['string'] if not (s.has_key('isData') and s['isData'] and p.cut.has_key('dataCut')) else "("+p.cut['string']+")&&("+p.cut['dataCut']+")" if not cut in cutStringForSample: cutStringForSample.append(cut) plotsPerCutForSample[cut]=[] if not p in plotsPerCutForSample[cut]: plotsPerCutForSample[cut].append(p) s['plotsPerCutForSample'] = plotsPerCutForSample for s in allSamples: sampleScaleFac = 1 if not s.has_key('scale') else s['scale'] if sampleScaleFac!=1: print "Using sampleScaleFac", sampleScaleFac ,"for sample",s["name"] for b in s['bins']: c = ROOT.TChain('Events' if not s.has_key('treeName') else s['treeName']) counter=0 dir = s['dirname'] if s.has_key('dirname') else s['dir'] for f in getFileList(dir+'/'+b, maxN = -1 if not (s.has_key('small') and s['small']) else 1, histname=""):#, minAgeDPM, histname, xrootPrefix, maxN): if not f[-5:]=='.root':continue counter+=1 c.Add(f) ntot = c.GetEntries() print "Added ",counter,'files from sample',s['name'],'dir',dir,'bin',b,'ntot',ntot switchOnBranches(c, usedBranches) if ntot==0: print "Warning! Found zero events in",s['name'],'bin',b," -> do nothing" continue if mode.lower()=='loop': for ics, cutString in enumerate(s['plotsPerCutForSample'].keys()): plotsToFill = s['plotsPerCutForSample'][cutString] elistName = "eList_"+s['name']+'_'+b+'_'+str(ics) elist = ROOT.TEventList(elistName) c.Draw(">>"+elistName,cutString) # print "elist",elist,elist.GetN(),cutString,'plots',plotsToFill number_events = elist.GetN()# if not (s.has_key('small') and s['small']) else min(elist.GetN(), 100) print "Reading: ", s["name"], b, "with",number_events,"events passing cutString", cutString, 'and will fill', len([p.name for p in plotsToFill]),'vars.' for p in plotsToFill: if not (p.cut.has_key('func') and p.cut['func']): p.cut['func']=None if p.TTreeFormula: assert p.TTreeFormula and not (p.TTreeFormula==""), "Problem in TTreeFormula %s" % p.TTreeFormula fString='ROOT.TTreeFormula("'+p.name+'","'+p.TTreeFormula+'",c)' exec('p.ttreeFormula='+fString) print "Created TTreeFormula:",fString for i in range(0,number_events): if (i%10000 == 0) and i>0 : print i c.GetEntry(elist.GetEntry(i)) for p in plotsToFill: # print p.cut['func'], p.cut['func'](c) if (not p.cut['func']) or p.cut['func'](c): weight = c.GetLeaf(p.weightString).GetValue() if p.weightString else 1. reWeight = p.weightFunc(c) if p.weightFunc else 1. # print c, p.weightFunc, p.weightFunc(c), getVarValue(c, "nVert"), c.GetLeaf("nVert").GetValue(), c.nVert if p.leaf: val = getVarValue(c, p.leaf, p.ind) # print "Fill leaf",p.leaf, p.ind, val, weight,sampleScaleFac if p.TTreeFormula: p.ttreeFormula.UpdateFormulaLeaves() val = p.ttreeFormula.EvalInstance() if p.func: val = p.func(c) # if val>170:print val, reWeight, weight, sampleScaleFac, p.leaf, p.ind, i, c.GetEntries(), elist.GetEntry(i),"x",c.GetLeaf('Jet_pt').GetValue(4), c.GetLeaf('met_pt').GetValue(), c.GetLeaf('lumi').GetValue(), c.GetLeaf('evt').GetValue() if val<float('inf'): p.histo.Fill(val, reWeight*weight*sampleScaleFac) # print p.histo.GetName(), b, val, weight*sampleScaleFac, reWeight*weight*sampleScaleFac elif mode.lower()=='draw': for ics, cutString in enumerate(s['plotsPerCutForSample'].keys()): plotsToFill = s['plotsPerCutForSample'][cutString] print "Reading: ", s["name"], b, "with cutString", cutString, 'and will fill', len([p.name for p in plotsToFill]),'vars.' for p in plotsToFill: # print c, p.string, p.binning, cutString, p.weightString, p.binningIsExplicit, sampleScaleFac tmp = getPlotFromChain(c, p.string, p.binning, cutString, p.weightString, binningIsExplicit=p.binningIsExplicit) tmp.Scale(sampleScaleFac) p.histo.Add(tmp) # for ics, cutString in enumerate(s['plotsPerCutForSample'].keys()): # plotsToFill = s['plotsPerCutForSample'][cutString] # for p in plotsToFill: # print c.GetEntries(), p.name,p.histo.Integral() # c.GetListOfFiles().ls() c.Reset() del c #do over-flow bins for p in allPlots: if p.overFlow and p.overFlow in [ "upper", "both"]: nbins = p.histo.GetNbinsX() p.histo.SetBinContent(nbins , p.histo.GetBinContent(nbins) + p.histo.GetBinContent(nbins + 1)) p.histo.SetBinError(nbins , sqrt(p.histo.GetBinError(nbins)**2 + p.histo.GetBinError(nbins + 1)**2)) if p.overFlow and p.overFlow in [ "lower", "both"]: p.histo.SetBinContent(1 , p.histo.GetBinContent(0) + p.histo.GetBinContent(1)) p.histo.SetBinError(1 , sqrt(p.histo.GetBinError(0)**2 + p.histo.GetBinError(1)**2)) for s in stacks: sumStackHistos(s)
def loopAndFill(stacks, mode="loop"): allSamples = [] allSampleNames = [] allPlots = [] usedBranches = [] for s in stacks: usedBranches = list(set(usedBranches + s.usedBranches)) for l in s.plotLists: for p in l: allPlots.append(p) if p.leaf: if not p.leaf in usedBranches: usedBranches.append(p.leaf) if p.weightString and not p.weightString in usedBranches: usedBranches.append(p.weightString) else: usedBranches = list(set(usedBranches + p.usedBranches)) if not p.sample in allSamples: assert p.sample.has_key('dir') or p.sample.has_key( 'dirname' ), "Missing key dir or dirname in sample %s" % repr( p.sample) allSamples.append(p.sample) if mode == 'loop': assert not any([ p.string for p in allPlots ]), "Loop mode is %s but specified 'string' for: %s" % ( mode, ", ".join([p.name for p in allPlots if p.string])) if mode == 'draw': assert all([ p.string for p in allPlots ]), "Loop mode is %s but specified no 'string' for: %s" % ( mode, ", ".join([p.name for p in allPlots if not p.string])) print "Found", len(allSamples), 'different samples:', ", ".join( s['name'] for s in allSamples) for s in allSamples: cutStringForSample = [] plotsPerCutForSample = {} # print s['name'], s.has_key('isData'), s.has_key('isData') and s['isData'], s.has_key('isData') and s['isData'] and s.has_key('dataCut') for p in allPlots: if p.sample == s: cut = p.cut['string'] if not ( s.has_key('isData') and s['isData'] and p.cut.has_key('dataCut') ) else "(" + p.cut['string'] + ")&&(" + p.cut['dataCut'] + ")" if not cut in cutStringForSample: cutStringForSample.append(cut) plotsPerCutForSample[cut] = [] if not p in plotsPerCutForSample[cut]: plotsPerCutForSample[cut].append(p) s['plotsPerCutForSample'] = plotsPerCutForSample for s in allSamples: sampleScaleFac = 1 if not s.has_key('scale') else s['scale'] if sampleScaleFac != 1: print "Using sampleScaleFac", sampleScaleFac, "for sample", s[ "name"] bins = s['bins'] if s.has_key('bins') else ['default'] for b in bins: treeName = 'Events' if not s.has_key('treeName') else s['treeName'] maxN = -1 if not (s.has_key('small') and s['small']) else 1 c = ROOT.TChain(treeName) counter = 0 dir = s['dirname'] if s.has_key('dirname') else s['dir'] fileList = getFileList( dir + '/' + b, maxN=maxN, histname="") if s.has_key('bins') else [s['file']] for f in fileList: if not f[-5:] == '.root': continue # counter+=1 # c.Add(f) if checkRootFile(f, checkForObjects=[treeName]): counter += 1 c.Add(f) else: print "File %s looks broken." % f ntot = c.GetEntries() print "Added ", counter, 'files from sample', s[ 'name'], 'dir', dir, 'bin', b, 'ntot', ntot switchOnBranches(c, usedBranches) if ntot == 0: print "Warning! Found zero events in", s[ 'name'], 'bin', b, " -> do nothing" continue if mode.lower() == 'loop': for ics, cutString in enumerate( s['plotsPerCutForSample'].keys()): plotsToFill = s['plotsPerCutForSample'][cutString] elistName = "eList_" + s['name'] + '_' + b + '_' + str(ics) elist = ROOT.TEventList(elistName) c.Draw(">>" + elistName, cutString) # print "elist",elist,elist.GetN(),cutString,'plots',plotsToFill number_events = elist.GetN( ) # if not (s.has_key('small') and s['small']) else min(elist.GetN(), 100) print "Reading: ", s[ "name"], b, "with", number_events, "events passing cutString", cutString, 'and will fill', len( [p.name for p in plotsToFill]), 'vars.' for p in plotsToFill: if not (p.cut.has_key('func') and p.cut['func']): p.cut['func'] = None if p.TTreeFormula: assert p.TTreeFormula and not ( p.TTreeFormula == "" ), "Problem in TTreeFormula %s" % p.TTreeFormula fString = 'ROOT.TTreeFormula("' + p.name + '","' + p.TTreeFormula + '",c)' exec('p.ttreeFormula=' + fString) print "Created TTreeFormula:", fString for i in range(0, number_events): if (i % 10000 == 0) and i > 0: print i c.GetEntry(elist.GetEntry(i)) for p in plotsToFill: # print p.cut['func'], p.cut['func'](c) if (not p.cut['func']) or p.cut['func'](c): weight = c.GetLeaf(p.weightString).GetValue( ) if p.weightString else 1. reWeight = p.weightFunc( c) if p.weightFunc else 1. # print c, p.weightFunc, p.weightFunc(c), getVarValue(c, "nVert"), c.GetLeaf("nVert").GetValue(), c.nVert if p.leaf: val = getVarValue(c, p.leaf, p.ind) # print "Fill leaf",p.leaf, p.ind, val, weight,sampleScaleFac if p.TTreeFormula: p.ttreeFormula.UpdateFormulaLeaves() val = p.ttreeFormula.EvalInstance() if p.func: val = p.func(c) # if val>170:print val, reWeight, weight, sampleScaleFac, p.leaf, p.ind, i, c.GetEntries(), elist.GetEntry(i),"x",c.GetLeaf('Jet_pt').GetValue(4), c.GetLeaf('met_pt').GetValue(), c.GetLeaf('lumi').GetValue(), c.GetLeaf('evt').GetValue() if val < float('inf'): p.histo.Fill( val, reWeight * weight * sampleScaleFac) # print p.histo.GetName(), b, val, weight*sampleScaleFac, reWeight*weight*sampleScaleFac elif mode.lower() == 'draw': for ics, cutString in enumerate( s['plotsPerCutForSample'].keys()): plotsToFill = s['plotsPerCutForSample'][cutString] print "Reading: ", s[ "name"], b, "with cutString", cutString, 'and will fill', len( [p.name for p in plotsToFill]), 'vars.' for p in plotsToFill: print c, "String", p.string, p.binning, "Cut", cutString, p.weightString, p.binningIsExplicit, sampleScaleFac tmp = getPlotFromChain( c, p.string, p.binning, cutString, p.weightString, binningIsExplicit=p.binningIsExplicit) tmp.Scale(sampleScaleFac) p.histo.Add(tmp) # for ics, cutString in enumerate(s['plotsPerCutForSample'].keys()): # plotsToFill = s['plotsPerCutForSample'][cutString] # for p in plotsToFill: # print c.GetEntries(), p.name,p.histo.Integral() # c.GetListOfFiles().ls() c.Reset() del c #do over-flow bins for p in allPlots: if p.overFlow and p.overFlow in ["upper", "both"]: nbins = p.histo.GetNbinsX() p.histo.SetBinContent( nbins, p.histo.GetBinContent(nbins) + p.histo.GetBinContent(nbins + 1)) p.histo.SetBinError( nbins, sqrt( p.histo.GetBinError(nbins)**2 + p.histo.GetBinError(nbins + 1)**2)) if p.overFlow and p.overFlow in ["lower", "both"]: p.histo.SetBinContent( 1, p.histo.GetBinContent(0) + p.histo.GetBinContent(1)) p.histo.SetBinError( 1, sqrt(p.histo.GetBinError(0)**2 + p.histo.GetBinError(1)**2)) #sum stacks for s in stacks: sumStackHistos(s) #normalize for p in allPlots: if p.normalizeTo: t = p.normalizeTo.histo.Integral() y = p.histo.Integral() r = p.normalizeRef.histo.Integral() if p.normalizeRef else y if r > 0: p.histo.Scale(t / r)
stuff=[] for name, varP, cutP, varC,cutC, binning in [ # ('MET_Inclusive', "MissingET.MET",'(1)', 'met_pt', '(1)', [45,0,450]), # ('MET_GenPhoton80', "MissingET.MET",'(1)', 'met_pt', '(Sum$(genPartAll_pt>80&&genPartAll_pdgId==22)>=1)', [45,0,450]), # ('MET_GenPhoton145', "MissingET.MET",'(1)', 'met_pt', '(Sum$(genPartAll_pt>145&&genPartAll_pdgId==22)>=1)', [45,0,450]), # ('ptGenGamma_compMET', "Max$(Particle.PT*(Particle.Status==1&&abs(Particle.PID)==22))",'Max$(Particle.PT*(Particle.Status==1&&Particle.PID==22))>50', 'Max$(genPartAll_pt*( abs(genPartAll_pdgId)==22&&genPartAll_status==1 ))', 'Max$(genPartAll_pt*(genPartAll_pdgId==22&&genPartAll_status==1))>50', [50,0,300]), # ('ptGenGamma', "Max$(Particle.PT*(Particle.Status==1&&abs(Particle.PID)==22))","(1)", 'Max$(genPartAll_pt*( abs(genPartAll_pdgId)==22&&genPartAll_status==1 ))',"(1)", [50,0,300]), # ('HT', "Sum$(JetMA5.PT*(JetMA5.PT>30))",'Max$(Particle.PT*(Particle.Status==1&&Particle.PID==22))>50', 'Sum$(Jet_pt*(Jet_pt>30))', 'Max$(genPartAll_pt*(genPartAll_pdgId==22&&genPartAll_status==1))>50', [50,0,1000]), # ('MET', "Max$(MissingET.MET)",'Max$(Particle.PT*(Particle.Status==1&&Particle.PID==22))>50', 'met_pt', 'Max$(genPartAll_pt*(genPartAll_pdgId==22&&genPartAll_status==1))>50', [100,0,500]), ('Jet0Pt', "Max$(JetMA5.PT*(JetMA5.PT>30))",'Max$(Particle.PT*(Particle.Status==1&&Particle.PID==22))>50', 'Max$(Jet_pt*(Jet_pt>30))', 'Max$(genPartAll_pt*(genPartAll_pdgId==22&&genPartAll_status==1))>50', [50,0,1000]), # ('SumPt', "Sum$(Particle.PT*(Particle.Status==1))", 'met_sumEt', [100,0,3000]), ]: h_c =getPlotFromChain(gJets, varC, binning, cutC,weight="weight") h_mg=getPlotFromChain(mg_delphes_cms_13TeV, varP, binning,cutP,weight="(1)") # h_p8=getPlotFromChain(p8_delphes_cms, varP, binning,cutP,weight="(1)") # h_mg=getPlotFromChain(mg_hep_13TeV, varP, binning,cutP,weight="(1)") # h_p8=getPlotFromChain(p8_hep, varP, binning,cutP,weight="(1)") # h_p8.SetLineColor(ROOT.kRed) h_c.SetLineColor(ROOT.kBlue) # h_p8.SetMarkerColor(ROOT.kRed) h_mg.SetMarkerColor(ROOT.kBlack) h_c .SetMarkerColor(ROOT.kBlue) # h_p8.SetMarkerSize(0) h_mg.SetMarkerSize(0) h_c .SetMarkerSize(0) # h_p8.SetMarkerStyle(0) h_mg.SetMarkerStyle(0) h_c .SetMarkerStyle(0)
#p8_hep = ROOT.TChain("Delphes") #for f in P8_hep: # p8_hep.Add(f) from StopsDilepton.tools.helpers import getChain, getObjDict, getEList, getVarValue, getPlotFromChain from StopsDilepton.samples.cmgTuples_Spring15_mAODv2_25ns_0l_postProcessed import GJets gJets = getChain(GJets, histname="") stuff=[] for name, varP, varC, binning in [ # ('MEx', "Sum$(Particle.Px*(Particle.Status==1))", 'met_genPt*cos(met_genPhi)', [100,-1,1]), ('ptGamma', "Max$(Particle.PT*(Particle.Status==1&&abs(Particle.PID)==22))", 'Max$(genPartAll_pt*( abs(genPartAll_pdgId)==22 ))', [100,0,500]), # ('SumPt', "Sum$(Particle.PT*(Particle.Status==1))", 'met_sumEt', [100,0,3000]), ]: h_c =getPlotFromChain(gJets, varC, binning,"1",weight="weight") h_mg=getPlotFromChain(mg_hep, varP, binning,"1","1") h_p8=getPlotFromChain(p8_hep, varP, binning,"1","1") h_p8.SetLineColor(ROOT.kRed) h_c.SetLineColor(ROOT.kBlue) h_p8.SetMarkerColor(ROOT.kRed) h_mg.SetMarkerColor(ROOT.kBlack) h_c .SetMarkerColor(ROOT.kBlue) h_p8.SetMarkerSize(0) h_mg.SetMarkerSize(0) h_c .SetMarkerSize(0) h_p8.SetMarkerStyle(0) h_mg.SetMarkerStyle(0) h_c .SetMarkerStyle(0) c1 = ROOT.TCanvas() l=ROOT.TLegend(0.75,0.75,1.0,1.0)