tt = False zz = False wz = False data = False #Now here we have a dictionary, with #a key for each squared background error #From here we can calculate the uncertainty on each background individually #We also can do the uncertainty of all backgrounds together if len(bkgdfs["DYJetsToLL"]) > 0: dy = True uncsqdDYJetsdf = sum(bkgdfs["DYJetsToLL"]) uncDYJetsdf = uncsqdDYJetsdf**(1 / 2) saveDYJetsunc = go.makeOutFile(mcprefix + '.DYJetsToLL', 'unc_' + reg, '.pkl', str(zptcut), str(hptcut), str(metcut), str(btagwp)) uncDYJetsdf.to_pickle(saveDYJetsunc) if len(bkgdfs["TT"]) > 0: tt = True uncsqdTTdf = sum(bkgdfs["TT"]) uncTTdf = uncsqdTTdf**(1 / 2) saveTTunc = go.makeOutFile(mcprefix + '.TT', 'unc_' + reg, '.pkl', str(zptcut), str(hptcut), str(metcut), str(btagwp)) uncTTdf.to_pickle(saveTTunc) if len(bkgdfs["WZTo2L2Q"]) > 0: wz = True uncsqdWZdf = sum(bkgdfs["WZTo2L2Q"])
cfdict[zptstr][bkg["name"]] = round(yieldzpt/origevnts*100,4) cfdict[hptstr][bkg["name"]] = round(yieldhpt/origevnts*100,4) cfdict[btgstr][bkg["name"]] = round(yieldbtag/origevnts*100,4) cfdict[sbstr][bkg["name"]] = round(yieldsb/origevnts*100,4) cfdict[metstr]["totbkg"] = round(totymet/totorig*100,4) cfdict[zptstr]["totbkg"] = round(totyzpt/totorig*100,4) cfdict[hptstr]["totbkg"] = round(totyhpt/totorig*100,4) cfdict[btgstr]["totbkg"] = round(totybtg/totorig*100,4) cfdict[sbstr]["totbkg"] = round(totysb/totorig*100,4) #gets cuts in order of loosest to tightest based on yield cutordered = sorted(cfdict,key= lambda x : -1*cfdict[x]["totbkg"]) cutFlowTableTex = go.makeOutFile("ZpAnomalon_mumu_bkg","cutflow",'.tex',str(int(zptcut)),str(int(hptcut)),str(int(metcut)),str(btagwp).split('.')[-1]+'E-10') cftab = open(cutFlowTableTex,"w") cftab.write(r'\begin{table}[htbp]') cftab.write('\n') cftab.write(r'\begin{center}') cftab.write('\n') cftab.write(r'\begin{tabular}{l | c | c | c | c | c | c }')#need to make dynamic cftab.write('\n') cftab.write("\hline\hline\n") cftab.write(r'cut description & ZZ & WZ & ttbar & DY+jets & Total \\') cftab.write('\n') cftab.write("\hline\n") for key in cutordered: #tabline = r''+key+' & '+str(cfdict[key]['ZZTo2L2Q'])+' & '+str(cfdict[key]['WZTo2L2Q'])+' & '+str(cfdict[key]['TT'])+' & '+str(cfdict[key]['DYJetsToLL'])+' & '+str(cfdict[key]['totbkg'])+' \\' tabline = key+' & '+str(cfdict[key]['ZZTo2L2Q'])+' & '+str(cfdict[key]['WZTo2L2Q'])+' & '+str(cfdict[key]['TT'])+' & '+str(cfdict[key]['DYJetsToLL'])+' & '+str(cfdict[key]['totbkg'])+' \\' cftab.write(tabline)
mg.GetXaxis().SetLimits(binlist[0], binlist[-1] + hsumb.GetBinWidth(1)) #y axis mg.GetYaxis().SetTitle("data/MC") mg.GetYaxis().SetTitleSize(0.07) mg.GetYaxis().SetTitleOffset(.7) mg.GetYaxis().SetLabelSize(0.05) mg.SetMinimum(0.5) mg.SetMaximum(ratio_max) mg.SetMaximum(1.5) mg.Draw("AP") l.Draw() #Go back to previous pad so next kinematic plots draw tc.cd() p1.cd() #Draw the legent with everything added if not "50rig" in hname: leg.SetBorderSize(0) leg.Draw() #Draw the legent with everything added leg.SetBorderSize(0) leg.Draw() #Save the plot pngname = gecorg.makeOutFile(hname, 'ratio_' + regiondescrip, '.png', str(zptcut), str(hptcut), str(metcut), str(btagwp)) tc.SaveAs(pngname)
tf2 = ROOT.TFile(f17dyjetsr[0]) hsr = tf2.Get('h_zp_jigm') hsr.Reset("ICESM") hsr = makeAddedHist(f17dyjetsr, a18dyjetsr, f17dyjetsrerrs, a18dyjetsrerrs, xspairs, hsr) ROOT.gSystem.CompileMacro("cfunctions/alphafits.C", "kfc") ROOT.gSystem.Load("cfunctions/alphafits_C") hsrt = hsr.Clone() hsbt = hsb.Clone() #Draw ROOT.gStyle.SetOptFit(1011) tc.cd(1) sbfit = ROOT.expFit(hsbt, "sbl", "R0+") hsb.Draw() sbfit.Draw("SAME") tc.cd(2) srfit = ROOT.expFit(hsrt, "srl", "R0+") hsr.Draw() srfit.Draw("SAME") tc.cd(3) alpha = ROOT.alphaRatioMakerExp(hsbt, hsrt) #print(alpha) alpha.Draw() figname = go.makeOutFile('Run2_2017_2018', 'alpha_fits', '.png', str(zptcut), str(hptcut), str(metcut), str(btagwp)) tc.SaveAs(figname)
parser = argparse.ArgumentParser() if __name__=='__main__': parser.add_argument("-f","--sample",help = "sample file") parser.add_argument("-o","--output",help = "output file name") parser.add_argument("-b","--btagger",help = "btagger selection, need name part of key") parser.add_argument("-wp","--btagWP",type=float,default=0.6,help = "doulbeB tagger working point (default M1)") parser.add_argument("-zpt","--zPtCut",type=float,default = 100.0,help = "pT cut on Z") parser.add_argument("-hpt","--hPtCut",type=float,default = 250.0,help = "pT cut on h") parser.add_argument("-met","--metPtCut",type=float,default = 50.0,help = "pT cut on met") parser.add_argument("-sdm","--sdmCut",type=float,default = 10.0,help = "lowest soft drop mass cut") parser.add_argument("-date","--date",type=str,help = "where are your topiary plots?") args = parser.parse_args() samp = args.sample sdmcut = args.sdmCut zptcut = args.zPtCut hptcut = args.hPtCut metcut = args.metPtCut btaggr = args.btagger btagwp = args.btagWP #inputfiles = glob.glob('analysis_output_ZpAnomalon/'+args.date+'/*_Zptcut'+str(zptcut)+'_Hptcut'+str(hptcut)+'_metcut'+str(metcut)+'_btagwp'+str(btagwp)+'.png') #inputfiles = glob.glob('../notes/AN-20-120_/*_Zptcut'+str(zptcut)+'_Hptcut'+str(hptcut)+'_metcut'+str(metcut)+'_btagwp'+str(btagwp)+'.png') for f in inputfiles: protoname = f.split("_Zpt")[0] name = protoname.split(args.date+"/")[-1] newfilename = go.makeOutFile(name,'tex','.png',str(int(zptcut)),str(int(hptcut)),str(int(metcut)),'loose') subprocess.run(["mv",f,newfilename])
origevnts = 0 if samptype != 1: inChain = ROOT.TChain("PreSelection") inputs = glob.glob("../dataHandling/"+year+"/"+samp+"*.root") for f in inputs: inChain.Add(f) tf = ROOT.TFile.Open(f) origevnts += tf.Get("hnevents").GetBinContent(1) else: inChain = ROOT.TChain("TreeMaker2/PreSelection") inChain.Add("../dataHandling/"+year+"/"+samp+"*.root") origevnts = inChain.GetEntries() outFile = go.makeOutFile(samp,'topiary','.root','0.0','250.0','0.0','0.0')#Needs to become dynamic with cuts print( "Making topiary of ",samp) print(" Sample type ",samptype) print(" Sample Year ",checkedyear) print(" Events in TChain: ",inChain.GetEntries()) print((" Original data set had {0} events in type.").format(origevnts)) print(" Saving topiary in ",outFile) ROOT.gSystem.CompileMacro("TreeMakerTopiary.C","g0ck") ROOT.gSystem.Load('TreeMakerTopiary_C') topiary = ROOT.TreeMakerTopiary(inChain,samptype,checkedyear) topiary.Loop(outFile,origevnts,samptype,checkedyear)
fdf = srdf region = "signalr" elif comb: fdf = btdf region = "totalr" else: fdf = sbdf region = "sideband" else: fdf = sbdf print(" number of passing events ",len(fdf)) #print("number of btag passing events ",len(btdf)) #lets make some histograms. rootfilename = go.makeOutFile(samp,'upout_'+region+'_'+btaggr,'.root',str(zptcut),str(hptcut),str(metcut),str(btagwp))#need to update for btagger npfilename = go.makeOutFile(samp,'totalevents_'+region+'_'+btaggr,'.npy',str(zptcut),str(hptcut),str(metcut),str(btagwp)) pklfilename = go.makeOutFile(samp,'selected_errors_'+region+'_'+btaggr,'.pkl',str(zptcut),str(hptcut),str(metcut),str(btagwp)) rootOutFile = up3.recreate(rootfilename,compression = None) npOutFile = open(npfilename,'wb') rootOutFile["h_z_pt"] = np.histogram(fdf['ZCandidate_pt'],bins=80,range=(0,800),weights=fdf['event_weight']) #rootOutFile["h_z_phi"] = np.histogram(fdf['ZCandidate_phi'],bins=100,range=(0,3.14159),weights=fdf['event_weight'])#needs to fit range rootOutFile["h_z_eta"] = np.histogram(fdf['ZCandidate_eta'],bins=100,range=(-5,5),weights=fdf['event_weight']) rootOutFile["h_z_m"] = np.histogram(fdf['ZCandidate_m'],bins=100,range=(40,140),weights=fdf['event_weight']) rootOutFile["h_h_pt"] = np.histogram(fdf['hCandidate_pt'],bins=40,range=(200,1200),weights=fdf['event_weight']) #rootOutFile["h_h_phi"] = np.histogram(fdf['hCandidate_phi'],bins=100,range=(0,3.14159))#needs to fit range rootOutFile["h_h_eta"] = np.histogram(fdf['hCandidate_eta'],bins=100,range=(-5,5),weights=fdf['event_weight']) rootOutFile["h_h_m"] = np.histogram(fdf['hCandidate_m'],bins=80,range=(0,400),weights=fdf['event_weight']) rootOutFile["h_h_sd"] = np.histogram(fdf['hCandidate_sd'],bins=80,range=(0,400),weights=fdf['event_weight']) rootOutFile["h_met"] = np.histogram(fdf['METclean'],bins=78,range=(50,2000),weights=fdf['event_weight'])
bkgeffs = np.delete(bkgeffs, 0) sigeffs = np.delete(sigeffs, 0) cutlist = np.delete(cutlist, 0) tg = ROOT.TGraph(hsum.GetNbinsX() - 1, sigeffs, bkgeffs) tg.SetTitle(btag) tg.SetLineWidth(2) tg.SetLineColor(btagcols[i]) mg.Add(tg) gleg.AddEntry(tg, btag) tc.cd() tc.SetLogy() mg.Draw("AL") mg.GetXaxis().SetTitle("signal efficiency") mg.GetXaxis().SetLimits(0, 1.0) mg.GetYaxis().SetTitle("background efficiency") mg.GetYaxis().SetTitleOffset(1.1) mg.GetYaxis().SetLabelSize(0.025) mg.SetMinimum(0.0001) mg.SetMaximum(1.0) gleg.Draw() tc.Update() tc.Draw() #Save the plot pngname = gecorg.makeOutFile(hname, 'btagger_roc_' + sigsamp, '.png', str(zptcut), str(hptcut), str(metcut)) tc.SaveAs(pngname)
temp_max = np.amax(signiflist) if temp_max > max_max: max_max = temp_max mg.SetTitle("") #x axis mg.GetXaxis().SetTitle("cut value") mg.GetXaxis().SetTitleSize(0.07) mg.GetXaxis().SetLabelSize(0.05) mg.GetXaxis().SetLimits(cutlist[0], cutlist[-1] + hsum.GetBinWidth(1)) #y axis mg.GetYaxis().SetTitle("S/#sqrt{B+S} a.u.") mg.GetYaxis().SetTitleSize(0.07) mg.GetYaxis().SetTitleOffset(.7) mg.GetYaxis().SetLabelSize(0.05) mg.SetMinimum(0) mg.SetMaximum(30) #Go back to previous pad so next kinematic plots draw tc.cd() p1.cd() #Draw the legent with everything added leg.SetBorderSize(0) leg.Draw() #Save the plot pngname = gecorg.makeOutFile(hname, 'optimization', '.png', str(zptcut), str(hptcut), str(metcut), str(btagwp)) tc.SaveAs(pngname)
mg.GetXaxis().SetTitleSize(0.07) mg.GetXaxis().SetLabelSize(0.05) mg.GetXaxis().SetLimits(binlist[0],binlist[-1]+hsumb.GetBinWidth(1)) #y axis mg.GetYaxis().SetTitle("data/MC") mg.GetYaxis().SetTitleSize(0.07) mg.GetYaxis().SetTitleOffset(.7) mg.GetYaxis().SetLabelSize(0.05) mg.SetMinimum(0.5) mg.SetMaximum(ratio_max) mg.SetMaximum(1.5) mg.Draw("AP") l.Draw() #Go back to previous pad so next kinematic plots draw tc.cd() p1.cd() #Draw the legent with everything added if not "50rig" in hname: leg.SetBorderSize(0) leg.Draw() #Draw the legent with everything added leg.SetBorderSize(0) leg.Draw() #Save the plot pngname = gecorg.makeOutFile(hname,'ratio_2018','.png',str(zptcut),str(hptcut),str(metcut),str(btagwp)) tc.SaveAs(pngname)