def __init__(self, indir="./METSkim_v1", outdir='./output/stacker/', channel='inclusive', whichregion='SR', zpt_cut='100', met_cut= '100', lumi = 2.318278305, sepSig=True, LogY=True, doRatio=True, addSig=True, addData=True, scaleDphi=False, onlyStats=False): self.outdir=outdir self.channel=channel self.whichregion=whichregion self.zpt_cut, self.met_cut = zpt_cut, met_cut self.lumi=lumi CheckDir(self.outdir) # if not, this will create it #channel='inclusive'#raw_input("Please choose a channel (el or mu): \n") self.tag0='ZJstudy' self.plotter_dd = InitializePlotter(indir=indir, addSig=addSig, addData=addData, doRatio=doRatio, scaleDphi=scaleDphi, onlyStats=onlyStats) self.plotter = InitializePlotter(indir=indir, addSig=addSig, addData=addData, doRatio=doRatio, onlyStats=onlyStats) self.plotter.Stack.rmPlotter(self.plotter.ZJets, "ZJets","Z+Jets", "background") setcuts = SetCuts() self.cuts_eld = setcuts.abcdCuts(channel=self.channel, whichRegion=whichregion, zpt_cut=zpt_cut, met_cut=met_cut) zjcuts = setcuts.GetZjetsCuts(whichRegion=whichregion, zpt_cut=zpt_cut, met_cut=met_cut) self.cuts = zjcuts[self.channel] # self.fabsDphi={'A': "fabs(llnunu_deltaPhi)", # 'B': "fabs(llnunu_deltaPhi)+TMath::Pi()/4", # 'C': "fabs(llnunu_deltaPhi)+TMath::Pi()/2", # 'D': "fabs(llnunu_deltaPhi)+TMath::Pi()*3/4"} self.fabsDphi="fabs(llnunu_deltaPhi)" # et1="TMath::Sqrt(llnunu_l1_pt*llnunu_l1_pt+llnunu_l1_mass*llnunu_l1_mass)" # et2="TMath::Sqrt(llnunu_l2_pt*llnunu_l2_pt+llnunu_l1_mass*llnunu_l1_mass)" # newMtB='TMath::Sqrt(2.0*(llnunu_l1_mass*llnunu_l1_mass+'+et1+'*'+et2+'-llnunu_l1_pt*llnunu_l2_pt*cos('+self.fabsDphi['B']+')))' # newMtC='TMath::Sqrt(2.0*(llnunu_l1_mass*llnunu_l1_mass+'+et1+'*'+et2+'-llnunu_l1_pt*llnunu_l2_pt*cos('+self.fabsDphi['C']+')))' # newMtD='TMath::Sqrt(2.0*(llnunu_l1_mass*llnunu_l1_mass+'+et1+'*'+et2+'-llnunu_l1_pt*llnunu_l2_pt*cos('+self.fabsDphi['D']+')))' # self.Mt={'A':'llnunu_mt', # 'B':newMtB, # 'C':newMtC, # 'D':newMtD} self.Mt='llnunu_mt' self.mtxbins=[150,200,250,300,350,400,450,550,850] self.mtnbins, self.mtxmin, self.mtxmax= len(self.mtxbins), min(self.mtxbins), max(self.mtxbins)#14, 150.0, 850.0 ROOT.gROOT.ProcessLine('.x ../src/tdrstyle.C') ROOT.gStyle.SetPadBottomMargin(0.2) ROOT.gStyle.SetPadLeftMargin(0.15)
def __init__(self, indir="./nonResSkim_v3.0", outdir='stack_test', lumi = 2.318278305, sepSig=True, LogY=True, doRatio=True, addSig=True, addData=True): if not os.path.exists(outdir): os.system('mkdir '+outdir) self.logy=LogY self.outdir = outdir self.lumi = lumi self.plotter_eu=InitializePlotter(indir, addSig=False, addData=True, doRatio=doRatio, doElMu=True, LogY=LogY) self.plotter_ll=InitializePlotter(indir, addSig=addSig, addData=True, doRatio=doRatio, LogY=LogY) self.plotter_ll.Stack.rmPlotter(self.plotter_ll.TT, "TT","TT", "background") self.plotter_ll.Stack.rmPlotter(self.plotter_ll.WW, "WW","WW, WZ non-reson.", "background") self.plotter_ll.Stack.rmPlotter(self.plotter_ll.WJets, "WJets","W+Jets", "background") self.setcuts=SetCuts() ROOT.gROOT.ProcessLine('.x ../src/tdrstyle.C') self.zpt_cut, self.met_cut= '100', '0' self.cuts=self.setcuts.GetAlphaCuts(zpt_cut=self.zpt_cut, met_cut=self.met_cut)
whichregion = 'VR' zpt_cut, met_cut = '100', '200' var = "fabs(llnunu_deltaPhi)" doSub = True whichdt = 'dt_sub' if doSub else 'dt' whichbcd = 'zjets' if doSub else 'allmc' tag = tag0 + '_' + 'printer' outTag = outdir + '/' + tag CheckDir(outdir) outtxt = open(outdir + '/num_out.txt', 'a') ### ----- Initialize (samples): plotter = InitializePlotter(indir=indir) setcuts = SetCuts() #cuts=setcuts.abcdCuts(channel=channel, whichRegion=whichregion, zpt_cut=zpt_cut, met_cut=met_cut) #preselect=setcuts.abcdCuts(channel=channel, whichRegion=whichregion, zpt_cut=zpt_cut, met_cut=met_cut,isPreSelect=True) zjcuts = setcuts.GetZjetsCuts(whichRegion=whichregion, zpt_cut=zpt_cut, met_cut=met_cut) cuts = zjcuts[channel] outtxt.write('\n' + '*' * 20 + '\n') outtxt.write('\n' + whichregion + '\n') for reg in cuts: outtxt.write(reg + " " + channel + " : " + cuts[reg] + '\n' + '-' * 20 + '\n')
logy = True zpt_cut, met_cut = '100', '0' if not os.path.exists(outdir): os.system('mkdir ' + outdir) tag = tag0 + '_' + 'test' outTag = outdir + '/' + tag # ll in Z | ll out Z # -------------------- M_out [35,65] U [115,120] # eu in Z | eu out Z # M_in (70,110) ### ----- Initialize (samples): plotter_ll = InitializePlotter(indir, addSig=False, addData=True, doRatio=True, LogY=logy) plotter_eu = InitializePlotter(indir, addSig=False, addData=True, doRatio=True, doElMu=True, scaleElMu=False, LogY=logy) setcuts = SetCuts() cuts = setcuts.GetAlphaCuts(zpt_cut=zpt_cut, met_cut=met_cut) outtxt.write('\n' + '*' * 20 + '\n') for reg in cuts: outtxt.write(reg + " inclusive : " + cuts[reg]['inclusive'] + '\n' +
'var': 'abs(abs(llnunu_deltaPhi)-TMath::Pi()/2)', 'nick': 'udPhi', 'title': '||#Delta#Phi_{Z,MET}| - #pi/2|', 'par': (16, 0, 1.6) }, 2: { 'var': '(llnunu_l2_pt*(abs(llnunu_deltaPhi)-TMath::Pi()/2)/abs(abs(llnunu_deltaPhi)-TMath::Pi()/2)/llnunu_l1_pt)', 'nick': 'ptRatio_signed', 'title': '(+/-)E_{T}^{miss}/p_{T}^{Z}', 'par': (50, -5, 5) } } ### ----- Initialize (cuts and samples): Plotters = InitializePlotter(indir, addSig=True, addData=True) plotters = [(Plotters.ZJets, tags[0]), (Plotters.sigPlotters[1], tags[1])] #ZJets = plotter.ZJets #ZJets=plotter.sigPlotters[1] mycuts = SetCuts() #tex_dic = mycuts.Tex_dic cuts = mycuts.abcdCuts(Channel, Region) preSelect = mycuts.abcdCuts(Channel, Region, isPreSelect=True, zpt_cut=zpt_cut, met_cut=met_cut) preCuts = OrderedDict({ 'preSelect': preSelect,
def ValidateDphiShapeCorr(self, indir, whichvar='fabsDphi', isNormalized=True, yieldCorr=False, whichbcd='ZJets', scaleDphi=True, onlyStats=False, logy=True,suffix=''): """ use shape correction that is derived from MC to apply to all the MC samples to validate the 'dphi_sf' algorithm whichvar=(absDphi, mt, zpt, met) """ binnedPlots=['met', 'zpt', 'metzpt'] if isNormalized: yieldCorr=False lumi_str='1' if isNormalized else str(self.lumi*1000) validatorMC=InitializePlotter(indir=indir, addSig=False, addData=False, doRatio=False, scaleDphi=scaleDphi,onlyStats=onlyStats) zjetsMC =InitializePlotter(indir=indir, addSig=False, addData=False, doRatio=False, scaleDphi=False, onlyStats=onlyStats) nom_suffix='normalized' if isNormalized else 'yield' leg_suffix='corr.' if scaleDphi else '' compareTagseq=[self.tag0, 'closureTest',self.whichregion,self.channel, whichvar, nom_suffix] nameseq=[whichvar, self.tag0, 'closureTest', self.whichregion, self.channel, 'met'+self.met_cut,'zpt'+self.zpt_cut, leg_suffix+'bcd'+whichbcd, nom_suffix, suffix] if '' in nameseq: nameseq.remove('') compareTag='_'.join(nameseq) #compareTag = self.tag0+'_'+'closureTest'+'_'+self.whichregion+'_'+self.channel+'_'+'regTrg'+'_'+whichvar outTag=self.outdir+'/'+compareTag if whichvar=='fabsDphi': var=self.fabsDphi nbins, xmin, xmax, titlex, units =4, 3*ROOT.TMath.Pi()/4, ROOT.TMath.Pi(), "|#Delta#phi_{Z,MET}|", "" elif whichvar=='mt': var=self.Mt if int(self.zpt_cut)*int(self.met_cut) == 0: nbins, xmin, xmax, titlex, units =140, 100.0, 800.0, "M_{T}^{ZZ}", "GeV" else: binnedPlots.append(whichvar) xbins, nbins, xmin, xmax = self.mtxbins, self.mtnbins, self.mtxmin, self.mtxmax titlex, units = "M_{T}^{ZZ}", "GeV" elif whichvar=='zpt': var=copy.deepcopy(self.Mt) for i in var: var[i]='llnunu_l1_pt' titlex, units = "p_{T}^{Z}", "GeV" xbins=[0,100,150,200,300,400,1000] nbins=len(xbins)-1 if int(self.zpt_cut)==0: xmin, xmax = 0.0, 1000.0 else: xmin, xmax = int(self.zpt_cut), 1000.0 elif whichvar=='met': var=copy.deepcopy(self.Mt) for i in var: var[i]='llnunu_l2_pt' #if int(self.met_cut)==0: nbins, xmin, xmax, titlex, units =90, 0.0, 450.0, "E_{T}^{miss}", "GeV" #else: xmin, xmax, titlex, units = int(self.met_cut)-10, 450.0, "E_{T}^{miss}", "GeV"; nbins=int((xmax-xmin)/10) if (xmax-xmin)%10==0 else 40 xmin,xmax, titlex, units = 0, 1000, "E_{T}^{miss}", "GeV" xbins=[0,25,50,80,120,1000] nbins=len(xbins)-1 elif whichvar=='zmass': var=copy.deepcopy(self.Mt) for i in var: var[i]='llnunu_l1_mass' nbins, xmin, xmax, titlex, units =40, 70., 110., "M_{Z}", "GeV" elif whichvar=='metzpt': var=copy.deepcopy(self.Mt) for i in var: var[i]='llnunu_l2_pt/llnunu_l1_pt' xmin, xmax, titlex, units = 0., 3., "E_{T}^{miss}/p_{T}^{Z}", "" xbins=[0,0.1,0.2,0.4,0.6,3] nbins=len(xbins)-1 else: print "[error] not right whichvar = ", whichvar," please check!"; exit(0) if whichbcd=='allBG': bcdPlotter = validatorMC.allBG notes='bcd from all bg' elif whichbcd=='ZJets': bcdPlotter = validatorMC.ZJets notes='bcd from zjets' else: print "[error] Please check the whichbcd = %s, is 'allBG' or 'ZJets'" % (whichbcd); exit(0) ### ----- Execute (plotting): if whichvar in binnedPlots: ha=zjetsMC.ZJets.drawTH1Binned('regTrg', var['A'], self.cuts['regTrg'], lumi_str, xbins, titlex = titlex, unitsx = units) hb=bcdPlotter.drawTH1Binned('regB_shift', var['B'], self.cuts['regB'], lumi_str, xbins,titlex = titlex,unitsx = units) hc=bcdPlotter.drawTH1Binned('regC_shift', var['C'], self.cuts['regC'], lumi_str, xbins,titlex = titlex,unitsx = units) hd=bcdPlotter.drawTH1Binned('regD_shift', var['D'], self.cuts['regD'], lumi_str, xbins,titlex = titlex,unitsx = units) else: ha=self.plotter.ZJets.drawTH1('regTrg', var['A'], self.cuts['regTrg'], lumi_str, nbins, xmin, xmax, titlex = titlex, units = units) hb=bcdPlotter.drawTH1('regB_shift', var['B'], self.cuts['regB'], lumi_str, nbins, xmin, xmax,titlex = titlex,units = units) hc=bcdPlotter.drawTH1('regC_shift', var['C'], self.cuts['regC'], lumi_str, nbins, xmin, xmax,titlex = titlex,units = units) hd=bcdPlotter.drawTH1('regD_shift', var['D'], self.cuts['regD'], lumi_str, nbins, xmin, xmax,titlex = titlex,units = units) iga=ha.GetSumOfWeights() #ha.Integral(0,1+ha.GetNbinsX()) igb=hb.GetSumOfWeights() #hb.Integral(0,1+hb.GetNbinsX()) igc=hc.GetSumOfWeights() #hc.Integral(0,1+hc.GetNbinsX()) igd=hd.GetSumOfWeights() #hd.Integral(0,1+hd.GetNbinsX()) print 'regB: ', hb.GetSumOfWeights(), '; integral:', hb.Integral(0,1+hb.GetNbinsX()),\ '\nregC: ', hc.GetSumOfWeights(), '; integral:', hc.Integral(0,1+hc.GetNbinsX()),\ '\nregD(sum of weight): ', hd.GetSumOfWeights(), '; integral:', hd.Integral(0,1+hd.GetNbinsX()) if isNormalized: ha.Scale(1./iga) hb.Scale(1./igb) hc.Scale(1./igc) hd.Scale(1./igd) ytitle='normalized' else: # Scale yield of bcd regions if yieldCorr: hb.Scale(iga/igb) hc.Scale(iga/igc) hd.Scale(iga/igd) print "[info] yield ratios: A/B=%.2f, A/C=%.2f, A/D=%.2f" % (iga/igb,iga/igc,iga/igd) ytitle='events' drawCompareSimple(hb, ha, "reg.B"+' '+leg_suffix, "reg. A", xmin=xmin, xmax=xmax, outdir=self.outdir, notes=notes,lumi=self.lumi,logy=logy, tag=compareTag+'BA', units='', ytitle=ytitle, setmax=1) drawCompareSimple(hc, ha, "reg.C"+' '+leg_suffix, "reg. A", xmin=xmin, xmax=xmax, outdir=self.outdir, notes=notes,lumi=self.lumi,logy=logy, tag=compareTag+'CA', units='', ytitle=ytitle, setmax=1) drawCompareSimple(hd, ha, "reg.D"+' '+leg_suffix, "reg. A", xmin=xmin, xmax=xmax, outdir=self.outdir, notes=notes,lumi=self.lumi,logy=logy, tag=compareTag+'DA', units='', ytitle=ytitle, setmax=1) ha.SetLineColor(ROOT.kRed) hb.SetLineColor(ROOT.kBlue) hc.SetLineColor(ROOT.kGreen) hd.SetLineColor(ROOT.kYellow) ha.SetMarkerColor(ROOT.kRed) hb.SetMarkerColor(ROOT.kBlue) hc.SetMarkerColor(ROOT.kGreen) hd.SetMarkerColor(ROOT.kYellow) if suffix=='': nameseq.insert(-1, '3v1') else : nameseq.append('3v1') #if isNormalized: if not yieldCorr: c1=ROOT.TCanvas(1) legend=GetLegendv1(0.65,0.75,0.85,0.90, [ha,hb,hc,hd],['reg. A','reg. B','reg. C','reg. D'],opt=['lpe','lpe','lpe','lpe']) nostack=ROOT.THStack("hs","hs") nostack.Add(ha,"e") nostack.Add(hb,"e") nostack.Add(hc,"e") nostack.Add(hd,"e") nostack.Draw("nostack") if units: ytitle+=' /' + str((xmax-xmin)/nbins) + '['+ units +']' nostack.GetXaxis().SetTitle(titlex) nostack.GetYaxis().SetTitle(ytitle) c1.Update() legend.Draw("same") c1.SetLogy() c1.SaveAs(self.outdir+'/'+'_'.join(nameseq)+'.pdf') else: hb.Add(hc);hb.Add(hd);hb.Scale(1.0/3.0) drawCompareSimple(ha, hb, "reg.A(MC)", "reg.A(bcd)", xmin=xmin, xmax=xmax, outdir=self.outdir, notes=notes,lumi=self.lumi, tag='_'.join(nameseq), units=units, ytitle=ytitle, setmax=2) #fout=ROOT.TFile(self.outdir+'/'+'_'.join(nameseq)+'.root','recreate') return
tag = tag0 + '_' + 'test' outTag = outdir + '/' + tag # B | A # ------- |dphi-pi/2| # D | C # +- met/pt tex_dic = mycuts.Tex_dic whichregion = raw_input("Please choose a benchmarck Region (SR or VR): \n") lswhichregion = [] if whichregion == "": lswhichregion = ['SR', 'VR'] else: lswhichregion.append(whichregion) ### ----- Initialize (samples): plotter = InitializePlotter(indir=indir, addData=True, doMetCorr=True) ZJets = plotter.ZJets Data = plotter.Data otherBG = plotter.NonZBG ### ----- Execute (plotting): comb = [lsChannel, lswhichregion] print list(product(*comb)) for Channel, whichregion in list(product(*comb)): cuts = mycuts.abcdCuts(Channel, whichregion, zpt_cut='100', met_cut='50') #cuts_loose=mycuts.abcdCuts(Channel, whichregion, met_cut='50') print cuts ROOT.gROOT.ProcessLine('.x ../src/tdrstyle.C')
#!/usr/bin/env python import os, sys, math from ROOT import * from python.InitializePlotter import InitializePlotter indir='../zjetsSkim' samples=InitializePlotter(indir=indir) outdir='./' fout=TFile(outdir+"cutflow.root","recreate") outtxt = open('cutflow_out.txt', 'a') test = True fchain=TChain("tree") if test: sampledir=["BulkGravToZZToZlepZinv_narrow_1000"] # ZZTo4L else: sampledir=samples.zjetsSamples for sample in samples.zjetsSamples: status = fchain.Add(indir+'/'+sample+'.root/tree', 0) if status<=0: raise RuntimeError, "[cutflow.py] ERROR! Invalid tree added to fchain, please check!" #---- Prepare cutflow plots and initialize cutflow = TH1F("h_cutflow", "cutflow; cut# ; / event ",10 , 0.,10. ) cutflow_w = TH1F("h_cutflow_w", "weighted cutflow; cut# ; / yields ",10 , 0.,10. ) nll = nflags = nsublep = 0 nsiglep = nZwindow = nZpt = nmet = ndjetfakeMet = nlepfakeMet = nvar1 = nvar2 = nlepVeto =0 #--- Loop: print fchain.GetEntriesFast(),' entries to process:'
indir = "../../AnalysisRegion_nonRes" lumi = 2.318278305 zpt_cut, met_cut = '100', '100' if not os.path.exists(outdir): os.system('mkdir ' + outdir) tag = tag0 + '_' + 'test' outTag = outdir + '/' + tag # ll in Z | ll out Z # -------------------- M_out [35,65] U [115,120] # eu in Z | eu out Z # M_in (70,110) ### ----- Initialize (samples): plotter_ll = InitializePlotter(indir, addSig=False, addData=True, doRatio=False) plotter_eu = InitializePlotter(indir, addSig=False, addData=True, doRatio=False, doElMu=True) setcuts = SetCuts() cuts_ll = { 'inclusive': setcuts.alphaCuts(Zmass='inclusive', zpt_cut=zpt_cut, met_cut=met_cut), 'in': setcuts.alphaCuts(isll=True, Zmass='in', zpt_cut=zpt_cut, met_cut=met_cut), 'out': setcuts.alphaCuts(isll=True, Zmass='out', zpt_cut=zpt_cut, met_cut=met_cut) }
from python.SimplePlot import * tag0 = 'sf_vs_met' tag = tag0 + '_' + 'test' outdir = 'plots/sfvsMet/' indir = "./METSkim" lumi = 2.3182783052 if not os.path.exists(outdir): os.system('mkdir ' + outdir) #met_pt=np.arange(30,150,5).tolist() ### ----- Initialize (samples): plotter_ll = InitializePlotter(addSig=False, addData=True, doRatio=False, indir=indir, doMetCorr=True) #plotter_eu=InitializePlotter(addSig=False, addData=True,doRatio=False, doElMu=True) setcuts = SetCuts() # print "I am cuts_ll:" # cuts_ll=setcuts.alphaCuts(Zmass='inclusive') # print "I am cuts_eu:" # cuts_eu=setcuts.alphaCuts(isll=False, Zmass='inclusive') # ROOT.gROOT.ProcessLine('.x tdrstyle.C') channel = raw_input( "[info] 'sf_vs_met.py' -> Please choose el/mu channel (el or mu, default mu): \n" ) if channel == '': channel = 'mu'
var={'A': "fabs(llnunu_deltaPhi)", 'AtoB': "fabs(llnunu_deltaPhi)-TMath::Pi()*3/4", 'AtoD': "fabs(llnunu_deltaPhi)-TMath::Pi()/2", 'AtoC': "fabs(llnunu_deltaPhi)-TMath::Pi()/4"} nbins=8 xmin=[0, ROOT.TMath.Pi()/4, ROOT.TMath.Pi()/2, 3*ROOT.TMath.Pi()/4] xmax=[ROOT.TMath.Pi()/4, ROOT.TMath.Pi()/2, 3*ROOT.TMath.Pi()/4, ROOT.TMath.Pi()] tag = tag0+'_'+'shapeCorrector' outTag=outdir+'/'+tag CheckDir(outdir) outtxt = open(outdir+'num_out.txt', 'a') ### ----- Initialize (samples): plotter=InitializePlotter(indir=indir, addData=True) setcuts = SetCuts() cuts=setcuts.abcdCuts(channel=channel, whichRegion=whichregion, zpt_cut=zpt_cut, met_cut=met_cut) # print cuts outtxt.write( '\n'+ '*'*20+'\n') outtxt.write( '\n'+ whichregion+'\n') for reg in cuts: outtxt.write(reg+" "+channel+" : "+cuts[reg]+'\n'+'-'*20+'\n') ROOT.gROOT.ProcessLine('.x ../src/tdrstyle.C') ROOT.gStyle.SetPadBottomMargin(0.2) ROOT.gStyle.SetPadLeftMargin(0.15) #ROOT.TH1.AddDirectory(ROOT.kFALSE) ### ----- Execute (plotting):
"[Input] which 2D shape reweight: 1) met vs dphi; 2) met/zpt vs dphi. (reply 1 or 2, default 2) \n" ) if which2d == '1': print "[info] met vs dphi" toprint = 'met vs fabs(dphi)' yvar, ytitle = "llnunu_l2_pt", "E_{T}^{miss}" ybins = [0, 25, 50, 80, 120, 1000] else: print "[info] met/zpt vs dphi" toprint = 'met/zpt vs fabs(dphi)' yvar, ytitle = "llnunu_l2_pt/llnunu_l1_pt", "E_{T}^{miss}/p_{T}^{Z}" ybins = [0, 0.1, 0.2, 0.4, 0.6, 3] print yvar, ybins ### ----- Initialize (samples): plotter = InitializePlotter(indir=indir, addData=True, onlyStats=onlyStats) setcuts = SetCuts() cuts = setcuts.abcdCuts(channel=channel, whichRegion=whichregion, zpt_cut=zpt_cut, met_cut=met_cut) # print cuts outtxt.write('\n' + '*' * 20 + '\n') outtxt.write('\n' + whichregion + '\n') for reg in cuts: outtxt.write(reg + " " + channel + " : " + cuts[reg] + '\n' + '-' * 20 + '\n') ### ----- Execute (plotting):