def MT2PlotMaker(rootdir, samples, data, dirname, plots, output_dir=".", exts=["pdf"], tag=""): # rootdir contains output of MT2Looper, samples are names of the .root files, # data is the name of the data file, dirname is the directory within the root file # to extract plots from, plots are a list of plot definitions from MT2PlotDefs # exts is a list of file extensions to produce # note that dirname can be a '+' separated string of directories to add together h_bkg_vecs = [[] for x in plots] h_data = [] dirnames = [s.strip() for s in dirname.split("+")] ## deal with suffixes # crdybaseInclLowPtOF means that we should take the plots in crdybaseIncl that end in LowPt # crdybaseInclLowPtSF means that we should take the plots in crdybaseIncl that end in LowPtemu suffix = None if "LowPtOF" in dirnames[0]: dirnames[0] = dirnames[0].replace("LowPtOF", "") suffix = "LowPtemu" if "LowPtSF" in dirnames[0]: dirnames[0] = dirnames[0].replace("LowPtSF", "") suffix = "LowPt" ## get background histograms for isamp in range(len(samples)): # get the root file for the given sample. This assumes that frag/fake photons come from qcd_ht.root if samples[isamp] in ["fragphoton", "fakephoton"]: fn = os.path.join(rootdir, "qcd_ht.root") else: fn = os.path.join(rootdir, samples[isamp] + ".root") fid = ROOT.TFile(fn) for iplot in range(len(plots)): vn = plots[iplot][0] # fix the var name for fake photons if samples[isamp] == "fakephoton": if vn.find("Loose") == -1 and vn.find("AllIso") == -1: vn += "Fake" else: vn = vn.replace("Loose", "FakeLoose") vn = vn.replace("AllIso", "FakeAllIso") if suffix != None: vn += suffix #print (dirnames[0]+"/h_"+vn) h_bkg_vecs[iplot].append(fid.Get(dirnames[0] + "/h_" + vn)) # histogram won't exist if there are no events. Replace it with None, handle later if type(h_bkg_vecs[iplot][-1]) == type(ROOT.TObject()): h_bkg_vecs[iplot][-1] = None else: h_bkg_vecs[iplot][-1].SetDirectory(0) # handle the case with more than one directory for idir in range(1, len(dirnames)): h_bkg_vecs[iplot][-1].Add( fid.Get(dirnames[idir] + "/h_" + vn)) fid.Close() # deal with nonexistent histograms for i in range(len(plots)): firstGood = -1 for isamp in range(len(samples)): if h_bkg_vecs[i][isamp] != None: firstGood = isamp break if firstGood == -1: raise RuntimeError( "all background histograms are empty for {0}/{1}!".format( dirname, plots[i][0])) for isamp in range(len(samples)): if h_bkg_vecs[i][isamp] == None: h_bkg_vecs[i][isamp] = h_bkg_vecs[i][firstGood].Clone() h_bkg_vecs[i][isamp].Reset() ## get data histograms if data == None: h_data = [None for i in plots] else: data_file = os.path.join(rootdir, data + ".root") fid = ROOT.TFile(data_file) for pl in plots: vn = pl[0] if suffix != None: vn += suffix h_data.append(fid.Get(dirnames[0] + "/h_" + vn)) if type(h_data[-1]) == type(ROOT.TObject()): raise Exception("No {0}/h_{1} histogram for {2}!".format( dirname, vn, data)) h_data[-1].SetDirectory(0) # handle the case with more than one directory for idir in range(1, len(dirnames)): h_data[-1].Add(fid.Get(dirnames[idir] + "/h_" + vn)) fid.Close() # make the output directory if it doesn't exist if not os.path.isdir(os.path.join(output_dir, dirname + tag)): os.makedirs(os.path.join(output_dir, dirname + tag)) # make all of the plots for i in range(len(plots)): vn = plots[i][0] userMin, userMax = None, None if plots[i][3] != None: userMin = plots[i][3][0] userMax = plots[i][3][1] if len(plots[i]) >= 5: utils.Rebin(h_bkg_vecs[i], h_data[i], plots[i][4]) doOverflow = True if len(plots[i]) >= 6: doOverflow = plots[i][5] markerSize = 0.8 title = utils.GetCRName(dirname) # title = None xAxisTitle = utils.GetVarName(vn) unit = utils.GetUnit(vn) subtitles = utils.GetSubtitles(dirname) if h_data[i] != None: subLegText = ["MC scaled by {datamcsf}", "# Data events: {ndata}"] else: subLegText = None # subLegText = None sns = [utils.GetSampleName(s) for s in samples] for ext in exts: saveAs = os.path.join(output_dir, dirname + tag, "{0}_{1}.{2}".format(dirname, vn, ext)) ppm.plotDataMC(h_bkg_vecs[i], sns, h_data[i], doPause=False, xAxisTitle=xAxisTitle, lumi=pd.lumi, lumiUnit=pd.lumiUnit, title=title, subtitles=subtitles, xRangeUser=plots[i][2], isLog=plots[i][1], saveAs=saveAs, scaleMCtoData=True, xAxisUnit=unit, userMin=userMin, userMax=userMax, doSort=False, doMT2Colors=True, markerSize=markerSize, titleSize=0.035, subtitleSize=0.033, legCoords=(0.60, 0.70, 0.87, 0.895), subLegText=subLegText, subLegTextSize=0.036, doBkgError=True, doOverflow=doOverflow, cmsTextSize=0.04, convertToPoisson=True, drawZeros=False)
# h_RS.Scale(lumi) h_data.Rebin(rebin[ivar]) h_RS.Rebin(rebin[ivar]) if var=="nJet30" or var=="nBJet20": if var=="nJet30": bins = np.array([0.,2.,4.,7.,10.,15.]) if "H" in ht_regs or "UH" in ht_regs else np.array([0.,2.,4.,7.,15.]) else: bins = np.array([0.,1.,2.,3.,6.]) h_data = h_data.Rebin(bins.size-1, str(random.random()), bins) h_RS = h_RS.Rebin(bins.size-1, str(random.random()), bins) for i,h in enumerate(h_mc_vec): h_mc_vec[i] = h.Rebin(bins.size-1, str(random.random()), bins) tot_ewk = sum(h.Integral(0,-1) for h in h_mc_vec) pctdiff = (h_RS.Integral(0,-1) / (h_data.Integral(0,-1) - tot_ewk) - 1) * 100 # subLegText = "Overpred: {0:.1f}%".format(pctdiff) subLegText = None saveAs = outdir+"/{0}/{1}".format(cr+ht_name,var) exts = [".pdf",".png"] for ext in exts: ppm.plotDataMC([h_RS]+h_mc_vec[::-1], ["RS QCD Pred", "Z(#nu#nu)+Jets", "W+Jets", "Top"], h_data=h_data, saveAs=saveAs+ext, lumi=lumi, extraEnergyText=str(data_year_txt) if not RSfromMC else None, xAxisTitle=titles[ivar], doMT2Colors=True, xRangeUser=xRangeUser[ivar], doOverflow=False, xAxisUnit=units[ivar], subLegText=subLegText, legCoords=(0.60,0.63,0.87,0.89), cmsTextSize=0.045)
def makeNormalizedLostLep(indir, samples, data, outdir='.', exts=['png', 'pdf'], ht_regs=None): ## makes plots of lost lepton CR (crsl) yields after MC is normalized in ## each HT, Njet, Nbjet bin separately. Makes 0b, 1b, inclusive plots ## and puts in directory called "lostlep" fmc = [ ROOT.TFile(os.path.join(indir, s + ".root"), "READ") for s in samples ] fdata = ROOT.TFile(os.path.join(indir, data + ".root"), "READ") regions_0b = ["1", "4", "7"] regions_ge1b = ["2", "3", "5", "6", "8", "10"] regions_incl = regions_0b + regions_ge1b regions = [regions_0b, regions_ge1b, regions_incl] region_names = ["0b", "ge1b", "incl"] if ht_regs == None: ht_regs = ["VL", "L", "M", "H", "UH"] mt2bins = [200, 300, 400, 500, 600, 800, 1000, 1500] mt2bins = np.array(mt2bins, dtype=float) bkg_names = [utils.GetSampleName(s) for s in samples] try: os.makedirs(os.path.join(outdir, "lostlep")) except: pass #loop over sets of regions (0b, >=1b, inclusive) for iregs, regs in enumerate(regions): h_mt2_mc_cr_vec = [None for s in samples] h_mt2_data_cr = None # loop over set of SRs within the given region for isr, sr in enumerate(regs): for ht_reg in ht_regs: # get the data/mc CR yields mc_cr_yield = 0.0 data_cr_yield = 0.0 for i in range(0, len(fmc)): mc_cr_yield += fmc[i].Get("crsl{0}{1}/h_mt2".format( sr, ht_reg)).Integral(0, -1) # sometimes we get 0 data events in a CR bin, so handle appropriately try: data_cr_yield += fdata.Get("crsl{0}{1}/h_mt2".format( sr, ht_reg)).Integral(0, -1) except AttributeError: pass scalefactor = data_cr_yield / mc_cr_yield # form the appropriately scaled histograms for i in range(len(fmc)): if h_mt2_mc_cr_vec[i] == None: h_mt2_mc_cr_vec[i] = fmc[i].Get( "crsl{0}{1}/h_mt2".format( sr, ht_reg)).Clone("h_mt2_mc_cr_" + str(i)) h_mt2_mc_cr_vec[i].Scale(scalefactor) else: h_mt2_mc_cr_vec[i].Add( fmc[i].Get("crsl{0}{1}/h_mt2".format(sr, ht_reg)), scalefactor) # again, somtimes 0 events in data CR try: if h_mt2_data_cr == None: h_mt2_data_cr = fdata.Get("crsl{0}{1}/h_mt2".format( sr, ht_reg)).Clone("h_mt2_data_cr") else: h_mt2_data_cr.Add( fdata.Get("crsl{0}{1}/h_mt2".format(sr, ht_reg))) except (TypeError, AttributeError): pass h_mt2bins_mc_vec = [ h.Rebin(mt2bins.size - 1, "h_mt2bins_mc_" + str(i), mt2bins) for h in h_mt2_mc_cr_vec ] h_mt2bins_data = h_mt2_data_cr.Rebin(mt2bins.size - 1, "h_mt2bins_data", mt2bins) nbins = h_mt2bins_data.GetNbinsX() systs = [0 for i in range(nbins)] ## get systematic in first bin incr = 0 for ibin in range(2, nbins + 1): incr += 0.4 / (nbins - 1) * (ibin - 1) * h_mt2bins_data.GetBinContent(i) systs[0] = incr / h_mt2bins_data.GetBinContent(1) ## get systematics in other bins for ibin in range(2, nbins + 1): systs[ibin - 1] = 0.4 / (nbins - 1) * (ibin - 1) subtitles = [ "#geq 2j, 1 lepton", "M_{T2} > 200 GeV", "H_{T} > 250 GeV" ] if iregs == 0: subtitles[0] = "#geq 2j, 0b, 1 lepton" if iregs == 1: subtitles[0] = "#geq 2j, #geq 1b, 1 lepton" if ht_regs[0] == "L": subtitles[2] = "H_{T} > 450 GeV" for ext in exts: saveAs = os.path.join( outdir, "lostlep", "lostlep_{0}_mt2bins.{1}".format(region_names[iregs], ext)) ppm.plotDataMC(h_mt2bins_mc_vec, bkg_names, h_mt2bins_data, doPause=False, xAxisTitle="M_{T2}", lumi=pd.lumi, lumiUnit=pd.lumiUnit, title=None, subtitles=subtitles, isLog=True, saveAs=saveAs, scaleMCtoData=False, xAxisUnit="GeV", doSort=False, doMT2Colors=True, markerSize=1.0, subtitleSize=0.040, doBkgError=True, doOverflow=False, cmsTextSize=0.040, doPull=False, convertToPoisson=True, drawSystematicBand=True, systematics=systs) for i in range(len(fmc)): fmc[i].Close() fdata.Close()
def makeLostLepHybrid(indir, samples=['lostlepFromCRs'], data='data_Run2016', outdir='.', exts=['png', 'pdf'], ht_regs=None): ## makes plots of lost lepton CR (crsl) hybrid MT2 yields integrated over HT, Njet, Nbjet bins ## Makes 0b, 1b, inclusive plots and puts in directory called "lostlepHybrid" fmc = [ ROOT.TFile(os.path.join(indir, s + ".root"), "READ") for s in samples ] fdata = ROOT.TFile(os.path.join(indir, data + ".root"), "READ") regions_0b = ["1", "4", "7", "12"] regions_ge1b = ["2", "3", "5", "6", "8", "10", "13", "14", "15"] regions_incl = regions_0b + regions_ge1b regions = [regions_0b, regions_ge1b, regions_incl] region_names = ["0b", "ge1b", "incl"] if ht_regs == None: ht_regs = ["VL", "L", "M", "H", "UH"] suffix = '' bkg_names = [utils.GetSampleName(s) for s in samples] try: os.makedirs(os.path.join(outdir, "lostlepHybrid")) except: pass #loop over sets of regions (0b, >=1b, inclusive) for iregs, regs in enumerate(regions): h_mt2binsAll_mc_cr_vec = [None for s in samples] h_mt2binsAll_mc_cr_var_vec = [None for s in samples] h_mt2binsAll_data_cr = None # loop over set of SRs within the given region for isr, sr in enumerate(regs): for ht_reg in ht_regs: if ht_reg == "VL" and sr in ["4", "5", "6", "7", "8", "10"]: continue elif ht_reg in ["L", "M", "H", "UH" ] and sr in ["12", "13", "14", "15"]: continue # this bin appears to be empty in data and MC.. elif ht_reg == "UH" and sr == "3": continue # form the aggregated histograms for i in range(len(fmc)): if h_mt2binsAll_mc_cr_vec[i] == None: h_mt2binsAll_mc_cr_vec[i] = fmc[i].Get( "sr{0}{1}/h_mt2binsAllCRMChybrid".format( sr, ht_reg)).Clone("h_mt2binsAll_mc_cr_" + str(i)) h_mt2binsAll_mc_cr_var_vec[i] = fmc[i].Get( "sr{0}{1}/h_mt2binsAllCRMChybridExtrapErr".format( sr, ht_reg)).Clone("h_mt2binsAll_mc_cr_var_" + str(i)) else: h_mt2binsAll_mc_cr_vec[i].Add(fmc[i].Get( "sr{0}{1}/h_mt2binsAllCRMChybrid".format( sr, ht_reg))) h_mt2binsAll_mc_cr_var_vec[i].Add(fmc[i].Get( "sr{0}{1}/h_mt2binsAllCRMChybridExtrapErr".format( sr, ht_reg))) # somtimes 0 events in data CR try: if h_mt2binsAll_data_cr == None: h_mt2binsAll_data_cr = fdata.Get( "crsl{0}{1}/h_mt2binsAll".format( sr, ht_reg)).Clone("h_mt2binsAll_data_cr") else: h_mt2binsAll_data_cr.Add( fdata.Get("crsl{0}{1}/h_mt2binsAll".format( sr, ht_reg))) except (TypeError, AttributeError): pass nbins = h_mt2binsAll_data_cr.GetNbinsX() systs = [0. for i in range(nbins)] for i in range(nbins): nom_val = 0. var_val = 0. for h_nom, h_var in zip(h_mt2binsAll_mc_cr_vec, h_mt2binsAll_mc_cr_var_vec): nom_val += h_nom.GetBinContent(i + 1) var_val += h_var.GetBinContent(i + 1) if (nom_val > 0.01): systs[i] = abs(1. - var_val / nom_val) # ## systematic just based on number of bins # incr = 0 # for ibin in range(2,nbins+1): # incr += 0.4 / (nbins-1) * (ibin-1) * h_mt2bins_data.GetBinContent(i) # systs[0] = incr/h_mt2bins_data.GetBinContent(1) # ## get systematics in other bins # for ibin in range(2,nbins+1): # systs[ibin-1] = 0.4 / (nbins-1) * (ibin-1) subtitles = [ "#geq 2j, 1 lepton", "M_{T2} > 200 GeV", "H_{T} > 250 GeV" ] if iregs == 0: subtitles[0] = "#geq 2j, 0b, 1 lepton" if iregs == 1: subtitles[0] = "#geq 2j, #geq 1b, 1 lepton" if ht_regs[0] == "VL" and len(ht_regs) == 1: subtitles[2] = "250 < H_{T} < 450 GeV" suffix = "_HT250to450" elif ht_regs[0] == "L" and len(ht_regs) == 1: subtitles[2] = "450 < H_{T} < 575 GeV" suffix = "_HT450to575" elif ht_regs[0] == "L": subtitles[2] = "H_{T} > 450 GeV" suffix = "_HTge450" elif ht_regs[0] == "M" and len(ht_regs) == 1: subtitles[2] = "575 < H_{T} < 1000 GeV" suffix = "_HT575to1000" elif ht_regs[0] == "M": subtitles[2] = "H_{T} > 575 GeV" suffix = "_HTge575" elif ht_regs[0] == "H" and len(ht_regs) == 1: subtitles[2] = "1000 < H_{T} < 1500 GeV" suffix = "_HT1000to1500" elif ht_regs[0] == "H": subtitles[2] = "H_{T} > 1000 GeV" suffix = "_HTge1000" elif ht_regs[0] == "UH": subtitles[2] = "H_{T} > 1500 GeV" suffix = "_HTge1500" for ext in exts: saveAs = os.path.join( outdir, "lostlepHybrid", "lostlepHybrid_{0}{1}_mt2bins.{2}".format( region_names[iregs], suffix, ext)) ppm.plotDataMC(h_mt2binsAll_mc_cr_vec, bkg_names, h_mt2binsAll_data_cr, doPause=False, xAxisTitle="M_{T2}", lumi=pd.lumi, lumiUnit=pd.lumiUnit, title=None, subtitles=subtitles, isLog=True, saveAs=saveAs, scaleMCtoData=False, xAxisUnit="GeV", doSort=False, doMT2Colors=True, markerSize=1.0, subtitleSize=0.040, doBkgError=True, doOverflow=False, cmsTextSize=0.040, doPull=False, convertToPoisson=True, drawSystematicBand=True, systematics=systs) for i in range(len(fmc)): fmc[i].Close() fdata.Close()
hs[p] = r.TH1D( "h_{0}_{1}_{2}_{3}".format(cfg_name, p, zoom, sch), "", *binnings[zoom]) if sum(hs[p].Integral(0, -1) for p in procs) == 0: continue binwidth = int( (binnings[zoom][2] - binnings[zoom][1]) / binnings[zoom][0]) ppm.plotDataMC([hs[p] for p in procs], legnames, h_data=None, xAxisTitle="nPE", xAxisUnit=None, lumi=None if COSMICS else lumi, isLog=True, userMin=(0.5, ), cmsText="milliQan simulation", doSort=False, customColors=colors, normText=str(binwidth) + " PE", legCoords=legCoords, extensions=["pdf", "png"], saveAs=os.path.join(outdir, cfg_name, "nPE", "{0}_{1}".format(zoom, sch))) # timing plots if not COSMICS: cfg_names = [ "slabNoBar", "fourSlab", "reco_slabNoBar", "neighbBars", "nonneighbBars", "reco_neighbBars", "reco_nonneighbBars" ] if "pencil" in fin.GetName() or "largeArea" in fin.GetName():
h_RS.Scale(lumi) else: h_RS = rs_file.Get("srbase/h_{0}".format(var)).Clone() GetTotalHistogram(h_RS, rs_file, var, cr, ht_regs, True) h_data.Rebin(rebin[ivar]) h_RS.Rebin(rebin[ivar]) if var == "nJet30" or var == "nBJet20": bins = np.array([ 0., 2., 4., 7., 15. ]) if var == "nJet30" else np.array([0., 1., 2., 3., 6.]) h_data = h_data.Rebin(4, str(random.random()), bins) h_RS = h_RS.Rebin(4, str(random.random()), bins) for i, h in enumerate(h_mc_vec): h_mc_vec[i] = h.Rebin(4, str(random.random()), bins) saveAs = outdir + "/{0}/{1}".format(cr + ht_name, var) exts = [".pdf", ".png", ".root"] for ext in exts: ppm.plotDataMC([h_RS] + h_mc_vec[::-1], ["RS QCD Pred", "Z+jets", "Top", "W+jets"], h_data=h_data, saveAs=saveAs + ext, lumi=lumi, xAxisTitle=titles[ivar], doMT2Colors=True, xRangeUser=xRangeUser[ivar], doOverflow=False, xAxisUnit=units[ivar])
rebin = [2,2,2,1,1,2,2,1,2] for tdir in ["ht450to1200","ht1200toInf","ht250to1200"]: # for tdir in ["ht450to1200","ht1200toInf"]: os.system("mkdir -p /home/users/{0}/public_html/mt2/RebalanceAndSmear/{1}/{2}/{3}".format(username, tag, inp.replace('.root',''),tdir)) os.system("cp ~/scripts/index.php /home/users/{0}/public_html/mt2/RebalanceAndSmear/{1}/{2}/{3}/".format(username, tag, inp.replace('.root',''),tdir)) for ivar,var in enumerate(vars): hrs = frse.Get("{0}/h_{1}".format(tdir, var)) hnrs = fnrse.Get("{0}/h_{1}".format(tdir, var)) hrs.Rebin(rebin[ivar]) hnrs.Rebin(rebin[ivar]) for ext in [".pdf",".png",".root"]: saveAs = "/home/users/{0}/public_html/mt2/RebalanceAndSmear/{1}/{5}/{2}/{2}_{3}{4}".format(username, tag, tdir,var,ext,inp.replace('.root','')) ppm.plotDataMC([hnrs], ["QCD MC"], h_data=hrs, dataTitle="R&S from MC", saveAs=saveAs, xAxisTitle=names[ivar], xAxisUnit=units[ivar]) if "nJet30" in var or "nBJet20" in var: newbins = np.array([0.,2.,4.,7.,10.,15.]) if var=="nJet30" else np.array([0.,1.,2.,3.,4.,6.]) tmp_hrs = hrs.Rebin(5,"tmp_hrs",newbins) tmp_hnrs = hnrs.Rebin(5,"tmp_hnrs",newbins) for ext in [".pdf",".png",".root"]: saveAs = "/home/users/{0}/public_html/mt2/RebalanceAndSmear/{1}/{5}/{2}/{2}_{3}{4}".format(username, tag, tdir,var+"_rebin",ext,inp.replace('.root','')) ppm.plotDataMC([tmp_hnrs], ["QCD MC"], h_data=tmp_hrs, dataTitle="R&S from MC", saveAs=saveAs, xAxisTitle=names[ivar], xAxisUnit=units[ivar])
def makeLostLepHybrid(indir, samples=['lostlepFromCRs'], data='data_Run2016', outdir='.', exts=['png','pdf'], ht_regs=None): ## makes plots of lost lepton CR (crsl) hybrid MT2 yields integrated over HT, Njet, Nbjet bins ## Makes 0b, 1b, inclusive plots and puts in directory called "lostlepHybrid" fmc = [ROOT.TFile(os.path.join(indir,s+".root"), "READ") for s in samples] fdata = ROOT.TFile(os.path.join(indir,data+".root"), "READ") regions_0b = ["1","4","7","20","25"] regions_ge1b = ["2","3","5","6","8","9","10","11","21","22","23","24","26","27","28","29"] regions_incl = regions_0b + regions_ge1b regions = [regions_0b, regions_ge1b, regions_incl] region_names = ["0b","ge1b","incl"] if ht_regs == None: ht_regs = ["VL","L","M","H","UH"] suffix = '' bkg_names = [utils.GetSampleName(s) for s in samples] try: os.makedirs(os.path.join(outdir, "lostlepHybrid")) except: pass last, lastvar = 0., 0. binning = np.array([200, 300, 400, 500, 600, 800, 1100, 1400, 1800, 2400], dtype=float) #loop over sets of regions (0b, >=1b, inclusive) for iregs,regs in enumerate(regions): h_mt2binsAll_mc_cr_vec = [None for s in samples] h_mt2binsAll_mc_cr_var_vec = [None for s in samples] h_mt2binsAll_data_cr = None # loop over set of SRs within the given region for isr, sr in enumerate(regs): for ht_reg in ht_regs: if ht_reg in ["VL","L"] and sr in ["20","21","22","23","24","25","26","27","28","29"]: continue elif ht_reg in ["M","H","UH"] and sr in ["7","8","9","11"]: continue # # this bin appears to be empty in data and MC.. # elif ht_reg == "UH" and sr == "3": # continue # form the aggregated histograms for i in range(len(fmc)): h_mt2binsAll_mc_cr_temp = fmc[i].Get("sr{0}{1}/h_mt2binsAllCRMChybrid".format(sr,ht_reg)).Clone("h_mt2binsAll_mc_cr_"+str(i)) h_mt2binsAll_mc_cr_var_temp = fmc[i].Get("sr{0}{1}/h_mt2binsAllCRMChybridExtrapErr".format(sr,ht_reg)).Clone("h_mt2binsAll_mc_cr_var_"+str(i)) for j in range(1,h_mt2binsAll_mc_cr_temp.GetNbinsX()+1): c1 = h_mt2binsAll_mc_cr_temp.GetBinContent(j) c2 = h_mt2binsAll_mc_cr_var_temp.GetBinContent(j) if c2 < c1: c2 = c1+(c1-c2) h_mt2binsAll_mc_cr_var_temp.SetBinContent(j, c2) if h_mt2binsAll_mc_cr_vec[i] == None: h_mt2binsAll_mc_cr_vec[i] = h_mt2binsAll_mc_cr_temp h_mt2binsAll_mc_cr_var_vec[i] = h_mt2binsAll_mc_cr_var_temp else: h_mt2binsAll_mc_cr_vec[i].Add(h_mt2binsAll_mc_cr_temp) h_mt2binsAll_mc_cr_var_vec[i].Add(h_mt2binsAll_mc_cr_var_temp) # somtimes 0 events in data CR try: if h_mt2binsAll_data_cr == None: h_mt2binsAll_data_cr = fdata.Get("crsl{0}{1}/h_mt2binsAll".format(sr,ht_reg)).Clone("h_mt2binsAll_data_cr") else: h_mt2binsAll_data_cr.Add(fdata.Get("crsl{0}{1}/h_mt2binsAll".format(sr,ht_reg))) except (TypeError, AttributeError): pass #rebin for i in range(len(h_mt2binsAll_mc_cr_vec)): h_mt2binsAll_mc_cr_vec[i] = h_mt2binsAll_mc_cr_vec[i].Rebin(binning.size-1, h_mt2binsAll_mc_cr_vec[i].GetName()+"rebin", binning) h_mt2binsAll_mc_cr_var_vec[i] = h_mt2binsAll_mc_cr_var_vec[i].Rebin(binning.size-1, h_mt2binsAll_mc_cr_var_vec[i].GetName()+"rebin", binning) h_mt2binsAll_data_cr = h_mt2binsAll_data_cr.Rebin(binning.size-1, h_mt2binsAll_data_cr.GetName()+"rebin", binning) nbins = h_mt2binsAll_data_cr.GetNbinsX() systs = [0. for i in range(nbins)] for i in range(nbins): nom_val = 0. var_val = 0. for h_nom,h_var in zip(h_mt2binsAll_mc_cr_vec,h_mt2binsAll_mc_cr_var_vec): nom_val += h_nom.GetBinContent(i+1) var_val += h_var.GetBinContent(i+1) if (nom_val > 0.00001): systs[i] = abs(1. - var_val / nom_val) if ht_reg=="H" and iregs==1 and i==5: print nom_val, h_mt2binsAll_data_cr.GetBinContent(i+1) # ## systematic just based on number of bins # incr = 0 # for ibin in range(2,nbins+1): # incr += 0.4 / (nbins-1) * (ibin-1) * h_mt2bins_data.GetBinContent(i) # systs[0] = incr/h_mt2bins_data.GetBinContent(1) # ## get systematics in other bins # for ibin in range(2,nbins+1): # systs[ibin-1] = 0.4 / (nbins-1) * (ibin-1) subtitles = ["#geq 2j, 1 lepton", "M_{T2} > 200 GeV","H_{T} > 250 GeV"] if iregs==0: subtitles[0] = "#geq 2j, 0b, 1 lepton" if iregs==1: subtitles[0] = "#geq 2j, #geq 1b, 1 lepton" if ht_regs[0]=="VL" and len(ht_regs) == 1: subtitles[2] = "250 < H_{T} < 450 GeV" suffix = "_HT250to450" elif ht_regs[0]=="L" and len(ht_regs) == 1: subtitles[2] = "450 < H_{T} < 575 GeV" suffix = "_HT450to575" elif ht_regs[0]=="L": subtitles[2] = "H_{T} > 450 GeV" suffix = "_HTge450" elif ht_regs[0]=="M" and len(ht_regs) == 1: subtitles[2] = "575 < H_{T} < 1200 GeV" suffix = "_HT575to1200" elif ht_regs[0]=="M": subtitles[2] = "H_{T} > 575 GeV" suffix = "_HTge575" elif ht_regs[0]=="H" and len(ht_regs) == 1: subtitles[2] = "1200 < H_{T} < 1500 GeV" suffix = "_HT1200to1500" elif ht_regs[0]=="H": subtitles[2] = "H_{T} > 1200 GeV" suffix = "_HTge1200" elif ht_regs[0]=="UH": subtitles[2] = "H_{T} > 1500 GeV" suffix = "_HTge1500" for ext in exts: saveAs = os.path.join(outdir, "lostlepHybrid", "lostlepHybrid_{0}{1}_mt2bins.{2}".format(region_names[iregs],suffix,ext)) ppm.plotDataMC(h_mt2binsAll_mc_cr_vec, bkg_names, h_mt2binsAll_data_cr, doPause=False, xAxisTitle="M_{T2}", lumi=pd.lumi, lumiUnit=pd.lumiUnit, title=None, subtitles=subtitles, isLog=True, saveAs=saveAs, scaleMCtoData=False, xAxisUnit="GeV", yAxisTitleSize=0.04, xAxisTitleSize=0.036, yAxisLabelSize=0.04, xAxisLabelSize=0.038, doSort=False, doMT2Colors=True, markerSize=1.4, subtitleSize=0.040, doBkgError=True, doOverflow=False, legCoords=(0.58,0.70,0.84,0.87), drawLegBox=False, cmsTextSize=0.050, cmsText="CMS", doPull=False, convertToPoisson=True, drawSystematicBand=True, systematics=systs) for i in range(len(fmc)): fmc[i].Close() fdata.Close()
def MT2PlotMaker(rootdir, samples, data, dirname, plots, output_dir=".", exts=["pdf"], tag="", scaleMC=True, suffix = '', datatitle = 'Observed', multisuf=None, systset=None, lumi=None, ratioType=0, signame=None, sig_points=None, gencats=None, ratioRange=None, doComposition=False): ''' rootdir contains output of MT2Looper, samples are names of the .root files, data is the name of the data file, dirname is the directory within the root file to extract plots from, plots are a list of plot definitions from MT2PlotDefs exts is a list of file extensions to produce note that dirname can be a '+' separated string of directories to add together plots is a vector of sets (hname, logy, xragne, yrange, rbin) ''' h_bkg_vecs = [[] for x in plots] h_data = [] dirnames = [s.strip() for s in dirname.split("+")] if type(multisuf) == list: dirnames = [dirname+s for s in multisuf] # dirname = dirname.replace('+','') dogencat = False ncats = len(samples) if type(gencats) == list and ncats == 1: ncats = len(gencats) dogencat = True if lumi==None and data != None: if '16' in data or 'plots16' in output_dir: lumi = 35.9 if '17' in data or 'plots17' in output_dir: lumi = 41.5 if '18' in data or 'plots18' in output_dir: lumi = 59.7 if 'run2' in data or 'plotsn2' in output_dir: lumi = 137.2 else: lumi = 35.9 if '2016' in rootdir: lumi = 35.9 if '2017' in rootdir: lumi = 41.5 if '2018' in rootdir: lumi = 59.7 if 'run2' in rootdir: lumi = 137.2 ## deal with suffixes if suffix != '' and suffix[0] != '_': suffix = '_'+suffix systs = [None for x in plots] drawSystematicBand = False if systset is not None and type(systset)==list: drawSystematicBand = True systs = [[] for x in plots] h_bkg_vecs_syst_up = [[[] for s in systset] for p in plots ] h_bkg_vecs_syst_dn = [[[] for s in systset] for p in plots ] ## get background histograms for icat in range(ncats): # get the root file for the given sample. This assumes that frag/fake photons come from qcd_ht.root if dogencat: fn = os.path.join(rootdir, samples[0]+".root") else: fn = os.path.join(rootdir, samples[icat]+".root") fid = ROOT.TFile(fn) for iplot in range(len(plots)): vn = plots[iplot][0] if dogencat: vn += '_'+gencats[icat] if suffix != None: vn += suffix vni = vn+multisuf[0] if type(multisuf)==list else vn h = fid.Get("{0}/h_{1}".format(dirnames[0],vni)) if not h: print( h, dirnames[0], vni, fn ) h_bkg_vecs[iplot].append( h ) # histogram won't exist if there are no events. Replace it with None, handle later if type(h_bkg_vecs[iplot][-1])==type(ROOT.TObject()): h_bkg_vecs[iplot][-1] = None else: h_bkg_vecs[iplot][-1].SetDirectory(0) # handle the case with more than one directory for idir in range(1, len(dirnames)): vni = vn+multisuf[idir] if type(multisuf)==list else vn h = fid.Get("{0}/h_{1}".format(dirnames[idir],vni)) h_bkg_vecs[iplot][-1].Add(h) if len(plots[iplot]) >= 5: h_bkg_vecs[iplot][-1].Rebin(plots[iplot][4]) if drawSystematicBand: for isys, sys in enumerate(systset): vnup = "{}_{}Up{}".format(plots[iplot][0], sys, suffix) vndn = "{}_{}Dn{}".format(plots[iplot][0], sys, suffix) hsysup = fid.Get("{}/h_{}".format(dirnames[0],vnup)) hsysdn = fid.Get("{}/h_{}".format(dirnames[0],vndn)) if not hsysup or not hsysdn: h_bkg_vecs_syst_up[iplot][isys].append( None ) h_bkg_vecs_syst_dn[iplot][isys].append( None ) continue h_bkg_vecs_syst_up[iplot][isys].append( hsysup ) h_bkg_vecs_syst_dn[iplot][isys].append( hsysdn ) h_bkg_vecs_syst_up[iplot][isys][-1].SetDirectory(0) h_bkg_vecs_syst_dn[iplot][isys][-1].SetDirectory(0) if len(plots[iplot]) >= 5: h_bkg_vecs_syst_up[iplot][isys][-1].Rebin(plots[iplot][4]) h_bkg_vecs_syst_dn[iplot][isys][-1].Rebin(plots[iplot][4]) fid.Close() # deal with nonexistent histograms <-- skipList = [] for i in range(len(plots)): firstGood = -1 for icat in range(ncats): if h_bkg_vecs[i][icat] != None: firstGood = icat break if firstGood==-1: # raise RuntimeError("all background histograms are empty for {0}/h_{1}!".format(dirname,plots[i][0])) print( "Error: all background histograms are empty for {0}/h_{1}! Skipping!".format(dirname,plots[i][0]) ) skipList.append(i) continue for icat in range(ncats): if h_bkg_vecs[i][icat] == None: h_bkg_vecs[i][icat] = h_bkg_vecs[i][firstGood].Clone() h_bkg_vecs[i][icat].Reset() if drawSystematicBand: h_bkg_tot = h_bkg_vecs[i][firstGood].Clone() h_bkg_tot.GetNbinsX() syst_up = [0.0] * h_bkg_tot.GetNbinsX() syst_dn = [0.0] * h_bkg_tot.GetNbinsX() for icat in range(ncats): if icat != firstGood and h_bkg_vecs[i][icat] != None: h_bkg_tot.Add(h_bkg_vecs[i][icat]) for isys, sys in enumerate(systset): h_bkg_tot_syst_up = h_bkg_vecs_syst_up[i][isys][firstGood].Clone() h_bkg_tot_syst_dn = h_bkg_vecs_syst_dn[i][isys][firstGood].Clone() for icat in range(ncats): if icat == firstGood: continue if h_bkg_vecs_syst_up[i][isys][icat] != None: h_bkg_tot_syst_up.Add(h_bkg_vecs_syst_up[i][isys][icat]) if h_bkg_vecs_syst_dn[i][isys][icat] != None: h_bkg_tot_syst_dn.Add(h_bkg_vecs_syst_dn[i][isys][icat]) if h_bkg_tot_syst_up.Integral() <= 0 or h_bkg_tot_syst_dn.Integral() <= 0: print(h_bkg_tot_syst_up) h_bkg_tot_syst_up.Scale(h_bkg_tot.Integral()/h_bkg_tot_syst_up.Integral()) h_bkg_tot_syst_dn.Scale(h_bkg_tot.Integral()/h_bkg_tot_syst_dn.Integral()) h_bkg_tot_syst_up.Divide(h_bkg_tot) h_bkg_tot_syst_dn.Divide(h_bkg_tot) for ibin in range(1, h_bkg_tot.GetNbinsX()+1): sysup = h_bkg_tot_syst_up.GetBinContent(ibin)-1 sysdn = h_bkg_tot_syst_dn.GetBinContent(ibin)-1 h_bkg_tot_syst_up.SetBinError(ibin, abs(sysup)) h_bkg_tot_syst_dn.SetBinError(ibin, abs(sysdn)) h_bkg_tot_syst_up.SetBinContent(ibin, 1) h_bkg_tot_syst_dn.SetBinContent(ibin, 1) # Temporary syst_up[ibin-1] = ((syst_up[ibin-1])**2 + sysup**2)**0.5 syst_dn[ibin-1] = ((syst_dn[ibin-1])**2 + sysdn**2)**0.5 systs[i] = syst_up # Temporary for ibin in range(len(syst_up)): systs[i][ibin] = max(syst_up[ibin], syst_dn[ibin]) ## get data histograms if data==None: h_data = [None for i in plots] else: data_file = os.path.join(rootdir, data+".root") fid = ROOT.TFile(data_file) for i, pl in enumerate(plots): if i in skipList: continue vn = pl[0] if suffix != None: vn += suffix vni = vn+multisuf[0] if type(multisuf)==list else vn h = fid.Get("{0}/h_{1}".format(dirnames[0],vni)) h_data.append( h ) if type(h_data[-1])==type(ROOT.TObject()): raise Exception("No {0}/h_{1} histogram for {2}!".format(dirnames[0], vni, data)) h_data[-1].SetDirectory(0) # handle the case with more than one directory for idir in range(1, len(dirnames)): vni = vn+multisuf[idir] if type(multisuf)==list else vn h_data[-1].Add(fid.Get("{0}/h_{1}".format(dirnames[idir],vni))) if len(plots[i]) >= 5: h_data[-1].Rebin(plots[i][4]) fid.Close() ## get signal histograms h_sig_vec = [[] for i in plots] sig_names = [[] for i in plots] if sig_points is not None: sig_file = os.path.join(rootdir, signame+".root") fid = ROOT.TFile(sig_file) for i, pl in enumerate(plots): if i in skipList: continue for spt in sig_points: vn = pl[0]+suffix+spt h = fid.Get("{0}/h_{1}".format(dirnames[0],vn)) h_sig_vec[i].append( h ) sig_names[i].append( utils.GetSampleName(signame+spt)) if len(plots[i]) >= 5: h_sig_vec[i][-1].Rebin(plots[i][4]) if type(h_sig_vec[i][-1])==type(ROOT.TObject()): raise Exception("No {0}/h_{1} histogram for {2}!".format(dirname, vn, signame)) h_sig_vec[i][-1].SetDirectory(0) # handle the case with more than one directory for idir in range(1, len(dirnames)): vni = vn[idir] if type(multisuf)==list else vn h = fid.Get("{0}/h_{1}".format(dirnames[idir],vni)) h_sig_vec[i][-1].Add( h ) fid.Close() # make the output directory if it doesn't exist if not os.path.isdir(os.path.join(output_dir,dirname+tag)): os.makedirs(os.path.join(output_dir,dirname+tag)) # make all of the plots for i in range(len(plots)): if i in skipList: continue vn = plots[i][0] if suffix != None: vn += suffix userMin,userMax = None,None if plots[i][3]!=None: userMin = plots[i][3][0] userMax = plots[i][3][1] doOverflow = True if len(plots[i]) >= 6: doOverflow = plots[i][5] legcoord = (0.54,0.67,0.87,0.895) if len(plots[i]) >= 7: legpos = plots[i][6] if legpos.lower() == 'tl': legcoord = (0.14, 0.68, 0.5, 0.89) elif legcoord.lower() == 'bl': legcoord = (0.14, 0.18, 0.5, 0.39) elif legcoord.lower() == 'tm': legcoord = (0.34, 0.68, 0.7, 0.89) elif legcoord.lower() == 'bm': legcoord = (0.34, 0.18, 0.7, 0.39) elif legcoord.lower() == 'bl': legcoord = (0.14, 0.18, 0.5, 0.39) elif legcoord.lower() == 'br': legcoord = (0.58, 0.18, 0.92, 0.39) else: # 'tr' legcoord = (0.54, 0.67, 0.87, 0.895) markerSize=0.8 title = utils.GetCRName(dirname+tag, output_dir) xAxisTitle = h_bkg_vecs[i][0].GetXaxis().GetTitle() xAxisTitle = xAxisTitle.replace('E_{T}^{miss}', 'p_{T}^{miss}') unit = None if xAxisTitle == "": xAxisTitle = utils.GetVarName(vn) unit = utils.GetUnit(vn) elif '[GeV]' in xAxisTitle: unit = 'GeV' xAxisTitle = xAxisTitle.replace('[GeV]', '') subtitles = utils.GetSubtitles(dirname, vn+tag) if len(plots[i]) >= 7 and plots[i][6].lower() == 'tl': title = '' subtitles = [] if h_data[i]!=None: if not scaleMC: subLegText = ["# Observed events: {ndata}"] elif type(scaleMC) == float: subLegText = ["MC scaled by {}".format(scaleMC),"# Data events: {ndata}"] else: subLegText = ["MC scaled by {datamcsf}","# Data events: {ndata}"] else: subLegText = None # subLegText = None if dogencat: sns = [utils.GetSampleName(s) for s in gencats] colors = [utils.GetColor(s) for s in gencats] else: sns = [utils.GetSampleName(s) for s in samples] colors = [utils.GetColor(s) for s in samples] pltname = vn if type(multisuf)!=list else vn + tag for ext in exts: # saveAs = os.path.join(output_dir,dirname+tag,"{0}_{1}.{2}".format(dirname,vn,ext)) saveAs = os.path.join(output_dir,dirname+tag,"{1}.{2}".format(dirname,pltname,ext)) ppm.plotDataMC(h_bkg_vecs[i], sns, h_data[i], doPause=False, xAxisTitle=xAxisTitle, lumi=lumi, lumiUnit='fb', title=title, subtitles=subtitles, dataTitle=datatitle, xRangeUser=plots[i][2], isLog=plots[i][1], saveAs=saveAs, scaleMCtoData=scaleMC, xAxisUnit=unit, userMin=userMin, userMax=userMax, doSort=False, customColors=colors, markerSize=markerSize, titleSize=0.035, subtitleSize=0.033, legCoords=legcoord, legNCol=2, subLegText=subLegText, subLegTextSize=0.036, doBkgError=True, doOverflow=doOverflow, cmsTextSize=0.04, convertToPoisson=False, drawZeros=False, drawSystematicBand=drawSystematicBand, systematics=systs[i], h_sig_vec=h_sig_vec[i], sig_names=sig_names[i], ratioType=ratioType, ratioTitle='Obs./Sim.', yRangeUserRatio=ratioRange, doComposition=doComposition)
if chan in bar_chans: for p in procs: tot_bar[p].Add(hs[p]) if chan in slab_chans: for p in procs: tot_slab[p].Add(hs[p]) ppm.plotDataMC([hs[p] for p in procs], legnames, h_data=None, xAxisTitle="nPE", xAxisUnit=None, lumi=None, isLog=True, cmsText=None, doSort=False, userMin=0.5, customColors=colors, legCoords=legCoords, extensions=["pdf", "png"], saveAs=os.path.join( outdir, cfg_name, "{0}_chan{1}".format(bname, chan))) ppm.plotDataMC([tot_bar[p] for p in procs], legnames, h_data=None, xAxisTitle="nPE", xAxisUnit=None, lumi=None, isLog=True,
log = False elif hist.find("h_j1pt") != -1: xtitle = "p_{T}^{Jet 1}" xunit = "GeV" log = True jet = "All-hadronic, 1 Jet" desc = "M_{glu} = 1400 GeV, M_{LSP} = 200 GeV" subtitles = [desc, jet] outputFile = "PNGs/0p001_200_" + hist + ".png" ppm.plotDataMC(h_T1qqqq_vec_200, h_T1qqqq_name, h0p001_200, dataTitle="T1qqqq (ctau = 0.001 cm)", isLog=log, ratioTitle="LL / Std", subtitles=subtitles, xAxisTitle=xtitle, xAxisUnit=xunit, markerSize=1.1, energy=13, saveAs=outputFile) desc = "M_{glu} = 1400 GeV, M_{LSP} = 800 GeV" subtitles = [desc, jet] outputFile = "PNGs/0p001_800_" + hist + ".png" ppm.plotDataMC(h_T1qqqq_vec_800, h_T1qqqq_name, h0p001_800, dataTitle="T1qqqq (ctau = 0.001 cm)", isLog=log, ratioTitle="LL / Std",
os.system("cp ~/scripts/index.php /home/users/{0}/public_html/mt2/RebalanceAndSmear/data_tests2/tmp/{1}/".format(username,tdir)) for ivar,var in enumerate(vars): hrs = frs.Get("{0}/h_{1}".format(tdir, var)) hnrs = fnrs.Get("{0}/h_{1}".format(tdir, var)) hrs.Rebin(rebin[ivar]) hnrs.Rebin(rebin[ivar]) h_mc_vec = [f.Get("{0}/h_{1}".format(tdir,var)) for f in fmc] for h in h_mc_vec: h.Scale(lumi) h.Rebin(rebin[ivar]) for ext in [".pdf",".png",".root"]: saveAs = "/home/users/{0}/public_html/mt2/RebalanceAndSmear/data_tests2/tmp/{1}/{1}_{2}{3}".format(username,tdir,var,ext) ppm.plotDataMC([hrs]+h_mc_vec[::-1], ["RS from data", "Z+jets", "Top", "W+jets"], h_data=hnrs, dataTitle="data", saveAs=saveAs, xAxisTitle=names[ivar], xAxisUnit=units[ivar]) if "nJet30" in var or "nBJet20" in var: newbins = np.array([0.,2.,4.,7.,15.]) if var=="nJet30" else np.array([0.,1.,2.,3.,6.]) tmp_hrs = hrs.Rebin(4,"tmp_hrs",newbins) tmp_hnrs = hnrs.Rebin(4,"tmp_hnrs",newbins) tmp_h_mc_vec = [h.Rebin(4,"tmp_"+h.GetName(),newbins) for h in h_mc_vec] for ext in [".pdf",".png",".root"]: saveAs = "/home/users/{0}/public_html/mt2/RebalanceAndSmear/data_tests2/tmp/{1}/{1}_{2}{3}".format(username,tdir,var+"_rebin",ext) ppm.plotDataMC([tmp_hrs]+tmp_h_mc_vec[::-1], ["RS from data", "Z+jets", "Top", "W+jets"], h_data=tmp_hnrs, dataTitle="data", saveAs=saveAs, xAxisTitle=names[ivar], xAxisUnit=units[ivar])
log = False y = [0.5, 3] jet = "All-hadronic, 2+ Jets" metmht = "|MET-MHT|/MET < 0.5" phimin = "QCD Enriched (#Delta#phi(Jet_{1...4},MET)_{min} < 0.3)" subtitles = [desc, jet, htmet, metmht, phimin] outputFiles = ["PNGs/" + hist + ".png", "PDFs/" + hist + ".pdf"] for outputFile in outputFiles: ppm.plotDataMC(h_16_vec, h_16_names, h_data=h17, dataTitle="Data (2017)", isLog=log, ratioTitle="2017 / 2016", subtitles=subtitles, subtitleSize=0.02, xAxisTitle=xtitle, xAxisUnit=xunit, yRangeUserRatio=y, markerSize=1.1, lumi=11.379, lumiUnit="fb", energy=13, saveAs=outputFile)
rebin = [2,2,2,1,1,2,2,1,2] for tdir in ["ht450to1000","ht1000toInf","ht250to1000"]: # for tdir in ["ht450to1000","ht1000toInf"]: os.system("mkdir -p /home/users/{0}/public_html/mt2/RebalanceAndSmear/MCtests2/tmp/{1}/{2}".format(username,inp.replace('.root',''),tdir)) os.system("cp ~/scripts/index.php /home/users/{0}/public_html/mt2/RebalanceAndSmear/MCtests2/tmp/{1}/{2}/".format(username,inp.replace('.root',''),tdir)) for ivar,var in enumerate(vars): hrs = frse.Get("{0}/h_{1}".format(tdir, var)) hnrs = fnrse.Get("{0}/h_{1}".format(tdir, var)) hrs.Rebin(rebin[ivar]) hnrs.Rebin(rebin[ivar]) for ext in [".pdf",".png",".root"]: saveAs = "/home/users/{0}/public_html/mt2/RebalanceAndSmear/MCtests2/tmp/{4}/{1}/{1}_{2}{3}".format(username,tdir,var,ext,inp.replace('.root','')) ppm.plotDataMC([hnrs], ["QCD MC"], h_data=hrs, dataTitle="R&S from MC", saveAs=saveAs, xAxisTitle=names[ivar], xAxisUnit=units[ivar]) if "nJet30" in var or "nBJet20" in var: newbins = np.array([0.,2.,4.,7.,15.]) if var=="nJet30" else np.array([0.,1.,2.,3.,6.]) tmp_hrs = hrs.Rebin(4,"tmp_hrs",newbins) tmp_hnrs = hnrs.Rebin(4,"tmp_hnrs",newbins) for ext in [".pdf",".png",".root"]: saveAs = "/home/users/{0}/public_html/mt2/RebalanceAndSmear/MCtests2/tmp/{4}/{1}/{1}_{2}{3}".format(username,tdir,var+"_rebin",ext,inp.replace('.root','')) ppm.plotDataMC([tmp_hnrs], ["QCD MC"], h_data=tmp_hrs, dataTitle="R&S from MC", saveAs=saveAs, xAxisTitle=names[ivar], xAxisUnit=units[ivar])
if "HT1500toInf" in ht_name: xRangeUser[0] = (1500,3000) for ivar,var in enumerate(vars): h_data = data_file.Get("srbase/h_{0}".format(var)).Clone() h_data.Reset() GetTotalHistogram(h_data, data_file, var, cr, ht_regs, False) h_data.Scale(lumi) h_RS = rs_file.Get("srbase/h_{0}".format(var)).Clone() GetTotalHistogram(h_RS, rs_file, var, cr, ht_regs, True) h_RS.Scale(lumi) h_data.Rebin(rebin[ivar]) h_RS.Rebin(rebin[ivar]) if var=="nJet30" or var=="nBJet20": print "HI" bins = np.array([0.,2.,4.,7.,15.]) if var=="nJet30" else np.array([0.,1.,2.,3.,6.]) h_data = h_data.Rebin(4, str(random.random()), bins) h_RS = h_RS.Rebin(4, str(random.random()), bins) saveAs = outdir+"/{0}/{1}".format(cr+ht_name,var) exts = [".pdf",".png",".root"] for ext in exts: ppm.plotDataMC([h_RS], ["RS QCD Pred"], h_data=h_data, saveAs=saveAs+ext, lumi=lumi, xAxisTitle=titles[ivar], doMT2Colors=True, xRangeUser=xRangeUser[ivar], doOverflow=False, xAxisUnit=units[ivar])
def makeNormalizedLostLep(indir, samples, data, outdir='.', exts=['png','pdf'], ht_regs=None): ## makes plots of lost lepton CR (crsl) yields after MC is normalized in ## each HT, Njet, Nbjet bin separately. Makes 0b, 1b, inclusive plots ## and puts in directory called "lostlep" fmc = [ROOT.TFile(os.path.join(indir,s+".root"), "READ") for s in samples] fdata = ROOT.TFile(os.path.join(indir,data+".root"), "READ") regions_0b = ["1","4","7"] regions_ge1b = ["2","3","5","6","8","10"] regions_incl = regions_0b + regions_ge1b regions = [regions_0b, regions_ge1b, regions_incl] region_names = ["0b","ge1b","incl"] if ht_regs == None: ht_regs = ["VL","L","M","H","UH"] mt2bins = [200, 300, 400, 500, 600, 800, 1000, 1500] mt2bins = np.array(mt2bins, dtype=float) bkg_names = [utils.GetSampleName(s) for s in samples] try: os.makedirs(os.path.join(outdir, "lostlep")) except: pass #loop over sets of regions (0b, >=1b, inclusive) for iregs,regs in enumerate(regions): h_mt2_mc_cr_vec = [None for s in samples] h_mt2_data_cr = None # loop over set of SRs within the given region for isr, sr in enumerate(regs): for ht_reg in ht_regs: # get the data/mc CR yields mc_cr_yield = 0.0 data_cr_yield = 0.0 for i in range(0,len(fmc)): mc_cr_yield += fmc[i].Get("crsl{0}{1}/h_mt2".format(sr,ht_reg)).Integral(0,-1) # sometimes we get 0 data events in a CR bin, so handle appropriately try: data_cr_yield += fdata.Get("crsl{0}{1}/h_mt2".format(sr,ht_reg)).Integral(0,-1) except AttributeError: pass scalefactor = data_cr_yield/mc_cr_yield # form the appropriately scaled histograms for i in range(len(fmc)): if h_mt2_mc_cr_vec[i] == None: h_mt2_mc_cr_vec[i] = fmc[i].Get("crsl{0}{1}/h_mt2".format(sr,ht_reg)).Clone("h_mt2_mc_cr_"+str(i)) h_mt2_mc_cr_vec[i].Scale(scalefactor) else: h_mt2_mc_cr_vec[i].Add(fmc[i].Get("crsl{0}{1}/h_mt2".format(sr,ht_reg)), scalefactor) # again, somtimes 0 events in data CR try: if h_mt2_data_cr == None: h_mt2_data_cr = fdata.Get("crsl{0}{1}/h_mt2".format(sr,ht_reg)).Clone("h_mt2_data_cr") else: h_mt2_data_cr.Add(fdata.Get("crsl{0}{1}/h_mt2".format(sr,ht_reg))) except (TypeError, AttributeError): pass h_mt2bins_mc_vec = [h.Rebin(mt2bins.size-1, "h_mt2bins_mc_"+str(i), mt2bins) for h in h_mt2_mc_cr_vec] h_mt2bins_data = h_mt2_data_cr.Rebin(mt2bins.size-1, "h_mt2bins_data", mt2bins) nbins = h_mt2bins_data.GetNbinsX() systs = [0 for i in range(nbins)] ## get systematic in first bin incr = 0 for ibin in range(2,nbins+1): incr += 0.4 / (nbins-1) * (ibin-1) * h_mt2bins_data.GetBinContent(i) systs[0] = incr/h_mt2bins_data.GetBinContent(1) ## get systematics in other bins for ibin in range(2,nbins+1): systs[ibin-1] = 0.4 / (nbins-1) * (ibin-1) subtitles = ["#geq 2j, 1 lepton", "M_{T2} > 200 GeV","H_{T} > 250 GeV"] if iregs==0: subtitles[0] = "#geq 2j, 0b, 1 lepton" if iregs==1: subtitles[0] = "#geq 2j, #geq 1b, 1 lepton" if ht_regs[0]=="L": subtitles[2] = "H_{T} > 450 GeV" for ext in exts: saveAs = os.path.join(outdir, "lostlep", "lostlep_{0}_mt2bins.{1}".format(region_names[iregs],ext)) ppm.plotDataMC(h_mt2bins_mc_vec, bkg_names, h_mt2bins_data, doPause=False, xAxisTitle="M_{T2}", lumi=pd.lumi, lumiUnit=pd.lumiUnit, title=None, subtitles=subtitles, isLog=True, saveAs=saveAs, scaleMCtoData=False, xAxisUnit="GeV", doSort=False, doMT2Colors=True, markerSize=1.0, subtitleSize=0.040, doBkgError=True, doOverflow=False, cmsTextSize=0.040, doPull=False, convertToPoisson=True, drawSystematicBand=True, systematics=systs) for i in range(len(fmc)): fmc[i].Close() fdata.Close()
"({0})*({1})".format(str(baseline + extra), weight), "goff") for plot in plots: h_data = hists[data[0]][plot] for p in data[1:]: h_data.Add(hists[p][plot]) toplot, isLog, binning, sigScale = plots[plot] h_bkg_vec = [hists[proc][plot] for proc in bkgs] h_sig_vec = [hists[sig][plot]] subLegText = "MC scaled by {datamcsf} #pm {datamcsferr}" saveAs = "/home/users/bemarsh/public_html/WHphigamma/crplots/v4/{0}".format( plot) for ext in [".png", ".pdf"]: ppm.plotDataMC(h_bkg_vec, bkgs, h_data, h_sig_vec=h_sig_vec, sig_names=[sig], isLog=isLog, scaleSigToMC=sigScale, scaleMCtoData=True, doOverflow=False, doBkgError=True, legCoords=(0.60, 0.72, 0.87, 0.89), legNCol=2, lumi=137, xAxisTitle=plot, subLegText=subLegText, doPause=False, saveAs=saveAs + ext)
def MT2PlotMaker(rootdir, samples, data, dirname, plots, output_dir=".", exts=["pdf"], tag="", signals=[], dataNames=["Data"], dataScales=None, mcScale=None, opts=""): # rootdir contains output of MT2Looper, samples are names of the .root files, # data is the name of the data file, dirname is the directory within the root file # to extract plots from, plots are a list of plot definitions from MT2PlotDefs # exts is a list of file extensions to produce # note that dirname can be a '+' separated string of directories to add together h_bkg_vecs = [[] for x in plots] h_sig_vecs = [[] for x in plots] # handle multiple sets of data histograms # pass a list of strings if data==None: h_data_vec = [None for x in plots] else: if type(data) != type([]): data = [data] h_data_vec = [[] for x in plots] dirnames = [s.strip() for s in dirname.split("+")] ## deal with suffixes # crdybaseInclLowPtOF means that we should take the plots in crdybaseIncl that end in LowPt # crdybaseInclLowPtSF means that we should take the plots in crdybaseIncl that end in LowPtemu suffix = [] for i,dn in enumerate(dirnames): if "LowPtOF" in dn: dirnames[i] = dirnames[i].replace("LowPtOF","") suffix.append("LowPtemu") elif "LowPtSF" in dn: dirnames[i] = dirnames[i].replace("LowPtSF","") suffix.append("LowPt") else: suffix.append("") ## get background histograms for isamp in range(len(samples)): # get the root file for the given sample. This assumes that frag/fake photons come from qcd_ht.root if samples[isamp] in ["fragphoton","fakephoton"]: fn = os.path.join(rootdir,"qcd_ht.root") else: fn = os.path.join(rootdir,samples[isamp]+".root") fid = ROOT.TFile(fn) for iplot in range(len(plots)): vn = plots[iplot][0] # fix the var name for fake photons if samples[isamp]=="fakephoton": if vn.find("Loose")==-1 and vn.find("AllIso")==-1: vn += "Fake" else: vn = vn.replace("Loose","FakeLoose") vn = vn.replace("AllIso","FakeAllIso") #print (dirnames[0]+"/h_"+vn) h_bkg_vecs[iplot].append( fid.Get(dirnames[0]+"/h_"+vn+suffix[0]) ) # histogram won't exist if there are no events. Replace it with None, handle later if type(h_bkg_vecs[iplot][-1])==type(ROOT.TObject()): h_bkg_vecs[iplot][-1] = None else: h_bkg_vecs[iplot][-1].SetDirectory(0) # handle the case with more than one directory for idir in range(1, len(dirnames)): h_bkg_vecs[iplot][-1].Add(fid.Get(dirnames[idir]+"/h_"+vn+suffix[idir])) if mcScale is not None: h_bkg_vecs[iplot][-1].Scale(mcScale) fid.Close() ## get signal histograms for isig in range(len(signals)): # get the root file for the given sample. This assumes that frag/fake photons come from qcd_ht.root fn = os.path.join(rootdir,signals[isig]+".root") fid = ROOT.TFile(fn) for iplot in range(len(plots)): vn = plots[iplot][0] h_sig_vecs[iplot].append( fid.Get(dirnames[0]+"/h_"+vn+suffix[0]) ) # histogram won't exist if there are no events. Replace it with None, handle later if type(h_sig_vecs[iplot][-1])==type(ROOT.TObject()): h_sig_vecs[iplot][-1] = None else: h_sig_vecs[iplot][-1].SetDirectory(0) # handle the case with more than one directory for idir in range(1, len(dirnames)): h_sig_vecs[iplot][-1].Add(fid.Get(dirnames[idir]+"/h_"+vn+suffix[idir])) fid.Close() # deal with nonexistent histograms for i in range(len(plots)): firstGood = -1 for isamp in range(len(samples)): if h_bkg_vecs[i][isamp] != None: firstGood = isamp break if firstGood==-1: raise RuntimeError("all background histograms are empty for {0}/{1}!".format(dirname,plots[i][0])) for isamp in range(len(samples)): if h_bkg_vecs[i][isamp] == None: h_bkg_vecs[i][isamp] = h_bkg_vecs[i][firstGood].Clone() h_bkg_vecs[i][isamp].Reset() for isig in range(len(signals)): if h_sig_vecs[i][isig] == None: h_sig_vecs[i][isig] = h_bkg_vecs[i][firstGood].Clone() h_sig_vecs[i][isig].Reset() ## get data histograms if data != None: for ip,pl in enumerate(plots): for id,dname in enumerate(data): data_file = os.path.join(rootdir, dname+".root") fid = ROOT.TFile(data_file) vn = pl[0] h_data_vec[ip].append( fid.Get(dirnames[0]+"/h_"+vn+suffix[0]) ) if type(h_data_vec[ip][-1])==type(ROOT.TObject()): raise Exception("No {0}/h_{1} histogram for {2}!".format(dirname, vn, dname)) h_data_vec[ip][-1].SetDirectory(0) # handle the case with more than one directory for idir in range(1, len(dirnames)): h_data_vec[ip][-1].Add(fid.Get(dirnames[idir]+"/h_"+vn+suffix[idir])) if dataScales is not None: h_data_vec[ip][id].Scale(dataScales[id]) fid.Close() # make the output directory if it doesn't exist if not os.path.isdir(os.path.join(output_dir,dirname+tag)): os.makedirs(os.path.join(output_dir,dirname+tag)) # make all of the plots for i in range(len(plots)): vn = plots[i][0] userMin,userMax = None,None if plots[i][3]!=None: userMin = plots[i][3][0] userMax = plots[i][3][1] if len(plots[i]) >= 5: utils.Rebin(h_bkg_vecs[i], h_data_vec[i], plots[i][4], h_sig_vec=h_sig_vecs[i]) doOverflow = True if len(plots[i]) >= 6: doOverflow = plots[i][5] markerSize=0.8 title = utils.GetCRName(dirname) # title = None xAxisTitle = utils.GetVarName(vn) unit = utils.GetUnit(vn) subtitles = utils.GetSubtitles(dirname) if "noSubtitles" in opts: subtitles=None if h_data_vec[0]!=None: subLegText = ["MC scaled by {datamcsf}"] for ih in range(len(h_data_vec[0])): subLegText.append("# Data{0} events: {{ndata{0}}}".format("" if len(h_data_vec[0])==1 else ih+1)) else: subLegText = None # subLegText = None drawZeros = False if plots[i][0] in ["mt2bins","htEle","htMu","jetpt1","jetpt2"]: drawZeros = True sns = [utils.GetSampleName(s) for s in samples] yRangeUserRatio = (0,2) scaleMCtoData = True if dirname=="crqcdbase": if "HEMveto" not in output_dir: yRangeUserRatio = (0,10) scaleMCtoData=False for ext in exts: saveAs = os.path.join(output_dir,dirname+tag,"{0}_{1}.{2}".format(dirname,vn,ext)) ppm.plotDataMC(h_bkg_vecs[i], sns, h_data_vec[i], doPause=False, xAxisTitle=xAxisTitle, lumi=pd.lumi, lumiUnit=pd.lumiUnit, dataTitle=dataNames, title=title, subtitles=subtitles, xRangeUser=plots[i][2], isLog=plots[i][1], saveAs=saveAs, scaleMCtoData=scaleMCtoData, xAxisUnit=unit, userMin=userMin, userMax=userMax, doSort=False, doMT2Colors=True, markerSize=markerSize, titleSize=0.035, subtitleSize=0.033, legCoords=(0.60,0.70,0.87,0.895), ratioTitle="Data/MC", subLegText=subLegText, subLegTextSize=0.036, doBkgError=True, doOverflow=doOverflow, cmsTextSize=0.04, convertToPoisson=True, drawZeros=drawZeros, h_sig_vec=h_sig_vecs[i], sig_names=signals, ratioType=0, yRangeUserRatio=yRangeUserRatio)
h_bkg1.FillRandom("b1", 5000) h_bkg2.FillRandom("b2", 5000) h_data.FillRandom("b1", 7500) h_data.FillRandom("b2", 7500) h_bkg_vec = [h_bkg1, h_bkg2] bkg_names = ["Background 1", "Background 2"] subtitles = ["subtitle 1", "subtitle 2"] subLegText = ["MC scaled by: {datamcsf}", "# Data Events: {ndata}"] systematics = [0.8 / (11 - i / 5.) for i in range(50)] ppm.plotDataMC(h_bkg_vec, bkg_names, h_data, doPause=True, isLog=False, title="Test plot", subtitles=subtitles, subLegText=subLegText, scaleMCtoData=True, markerSize=0.9, lumi=3.64, lumiUnit="ab", energy=14, saveAs="test.pdf", doBkgError=True, drawSystematicBand=True, systematics=systematics)
def MT2PlotMaker(rootdir, samples, data, dirname, plots, output_dir=".", exts=["pdf"], tag="", scaleMC=True, suffix=None, datatitle='Observed', systset=None, lumi=None, ratioType=0, signame=None, sig_points=None, gencats=None): ''' rootdir contains output of MT2Looper, samples are names of the .root files, data is the name of the data file, dirname is the directory within the root file to extract plots from, plots are a list of plot definitions from MT2PlotDefs exts is a list of file extensions to produce note that dirname can be a '+' separated string of directories to add together ''' h_bkg_vecs = [[] for x in plots] h_data = [] dirnames = [s.strip() for s in dirname.split("+")] dogencat = False ncats = len(samples) if type(gencats) == list and ncats == 1: ncats = len(gencats) dogencat = True if lumi == None: if 'samp16' in rootdir: lumi = 35.9 if 'samp17' in rootdir: lumi = 41.5 if 'samp18' in rootdir: lumi = 59.7 if 'Run2' in rootdir: lumi = 137.2 if lumi == None: if '16' in data: lumi = 35.9 if '17' in data: lumi = 41.5 if '18' in data: lumi = 59.7 if 'run2' in data: lumi = 137.2 ## deal with suffixes # crdybaseInclLowPtOF means that we should take the plots in crdybaseIncl that end in LowPt # crdybaseInclLowPtSF means that we should take the plots in crdybaseIncl that end in LowPtemu if suffix != None and suffix != '' and suffix[0] != '_': suffix = '_' + suffix systs = [None for x in plots] drawSystematicBand = False if systset is not None and type(systset) == list: drawSystematicBand = True systs = [[] for x in plots] h_bkg_vecs_syst_up = [[[] for s in systset] for p in plots] h_bkg_vecs_syst_dn = [[[] for s in systset] for p in plots] # nbins = h_mt2bins_data.GetNbinsX() # systs = [0 for i in range(nbins)] # ## get systematic in first bin # incr = 0 # for ibin in range(2,nbins+1): # incr += 0.4 / (nbins-1) * (ibin-1) * h_mt2bins_data.GetBinContent(i) # systs[0] = incr/h_mt2bins_data.GetBinContent(1) # ## get systematics in other bins # for ibin in range(2,nbins+1): # systs[ibin-1] = 0.4 / (nbins-1) * (ibin-1) ## get background histograms for icat in range(ncats): # get the root file for the given sample. This assumes that frag/fake photons come from qcd_ht.root if dogencat: fn = os.path.join(rootdir, samples[0] + ".root") else: fn = os.path.join(rootdir, samples[icat] + ".root") fid = ROOT.TFile(fn) for iplot in range(len(plots)): vn = plots[iplot][0] if dogencat: vn += '_' + gencats[icat] if suffix != None: vn += suffix h_bkg_vecs[iplot].append(fid.Get(dirnames[0] + "/h_" + vn)) # histogram won't exist if there are no events. Replace it with None, handle later if type(h_bkg_vecs[iplot][-1]) == type(ROOT.TObject()): h_bkg_vecs[iplot][-1] = None else: h_bkg_vecs[iplot][-1].SetDirectory(0) # handle the case with more than one directory for idir in range(1, len(dirnames)): h_bkg_vecs[iplot][-1].Add( fid.Get(dirnames[idir] + "/h_" + vn)) if drawSystematicBand: for isys, sys in enumerate(systset): h_bkg_vecs_syst_up[iplot][isys].append( fid.Get("{}/h_{}_{}Up".format(dirnames[0], vn, sys))) h_bkg_vecs_syst_dn[iplot][isys].append( fid.Get("{}/h_{}_{}Dn".format(dirnames[0], vn, sys))) h_bkg_vecs_syst_up[iplot][isys][-1].SetDirectory(0) h_bkg_vecs_syst_dn[iplot][isys][-1].SetDirectory(0) fid.Close() # deal with nonexistent histograms <-- skipList = [] for i in range(len(plots)): firstGood = -1 for icat in range(ncats): if h_bkg_vecs[i][icat] != None: firstGood = icat break if firstGood == -1: # raise RuntimeError("all background histograms are empty for {0}/h_{1}!".format(dirname,plots[i][0])) print "Error: all background histograms are empty for {0}/h_{1}! Skipping!".format( dirname, plots[i][0]) skipList.append(i) continue for icat in range(ncats): if h_bkg_vecs[i][icat] == None: h_bkg_vecs[i][icat] = h_bkg_vecs[i][firstGood].Clone() h_bkg_vecs[i][icat].Reset() if drawSystematicBand: h_bkg_tot = h_bkg_vecs[i][firstGood].Clone() syst_up = [0.0] * h_bkg_tot.GetNbinsX() syst_dn = [0.0] * h_bkg_tot.GetNbinsX() for icat in range(ncats): if icat != firstGood and h_bkg_vecs[i][icat] != None: h_bkg_tot.Add(h_bkg_vecs[i][icat]) for isys, sys in enumerate(systset): h_bkg_tot_syst_up = h_bkg_vecs_syst_up[i][isys][ firstGood].Clone() h_bkg_tot_syst_dn = h_bkg_vecs_syst_dn[i][isys][ firstGood].Clone() for icat in range(ncats): if icat == firstGood: continue if h_bkg_vecs_syst_up[i][isys][icat] != None: h_bkg_tot_syst_up.Add( h_bkg_vecs_syst_up[i][isys][icat]) if h_bkg_vecs_syst_dn[i][isys][icat] != None: h_bkg_tot_syst_dn.Add( h_bkg_vecs_syst_dn[i][isys][icat]) h_bkg_tot_syst_up.Scale(h_bkg_tot.Integral() / h_bkg_tot_syst_up.Integral()) h_bkg_tot_syst_dn.Scale(h_bkg_tot.Integral() / h_bkg_tot_syst_dn.Integral()) h_bkg_tot_syst_up.Divide(h_bkg_tot) h_bkg_tot_syst_dn.Divide(h_bkg_tot) for ibin in range(1, h_bkg_tot.GetNbinsX() + 1): sysup = h_bkg_tot_syst_up.GetBinContent(ibin) - 1 sysdn = h_bkg_tot_syst_dn.GetBinContent(ibin) - 1 h_bkg_tot_syst_up.SetBinError(ibin, abs(sysup)) h_bkg_tot_syst_dn.SetBinError(ibin, abs(sysdn)) h_bkg_tot_syst_up.SetBinContent(ibin, 1) h_bkg_tot_syst_dn.SetBinContent(ibin, 1) # Temporary syst_up[ibin - 1] = ((syst_up[ibin - 1])**2 + sysup**2)**0.5 syst_dn[ibin - 1] = ((syst_dn[ibin - 1])**2 + sysdn**2)**0.5 systs[i] = syst_up # Temporary for ibin in range(len(syst_up)): systs[i][ibin] = max(syst_up[ibin], syst_dn[ibin]) ## get data histograms if data == None: h_data = [None for i in plots] else: data_file = os.path.join(rootdir, data + ".root") fid = ROOT.TFile(data_file) for i, pl in enumerate(plots): if i in skipList: continue vn = pl[0] # if suffix != None: # vn += suffix h_data.append(fid.Get(dirnames[0] + "/h_" + vn)) if type(h_data[-1]) == type(ROOT.TObject()): raise Exception("No {0}/h_{1} histogram for {2}!".format( dirname, vn, data)) h_data[-1].SetDirectory(0) # handle the case with more than one directory for idir in range(1, len(dirnames)): h_data[-1].Add(fid.Get(dirnames[idir] + "/h_" + vn)) fid.Close() ## get signal histograms h_sig_vec = [[] for i in plots] sig_names = [[] for i in plots] if sig_points is not None: sig_file = os.path.join(rootdir, signame + ".root") fid = ROOT.TFile(sig_file) for i, pl in enumerate(plots): if i in skipList: continue for spt in sig_points: vn = pl[0] + '_' + spt h_sig_vec[i].append(fid.Get(dirnames[0] + "/h_" + vn)) #hacking if spt == "T2tt_1050_100": sig_names[i].append( "#tilde{t}#rightarrowt#tilde{#chi}^{0}_{1}(1050,100)") elif spt == "T2tt_1000_100": sig_names[i].append( "#tilde{t}#rightarrowt#tilde{#chi}^{0}_{1}(1000,100)") elif spt == "T2tt_900_500": sig_names[i].append( "#tilde{t}#rightarrowt#tilde{#chi}^{0}_{1}(900,500)") elif spt == "T2tt_425_325": sig_names[i].append( "#tilde{t}#rightarrowt#tilde{#chi}^{0}_{1}(425,325)") elif spt == "T2bW_950_100": sig_names[i].append( "#tilde{t}#rightarrowb#tilde{#chi}^{#pm}_{1}(950,100)") elif spt == "T2bW_800_450": sig_names[i].append( "#tilde{t}#rightarrowb#tilde{#chi}^{#pm}_{1}(800,450)") elif spt == "T2bt_950_100": sig_names[i].append( "#tilde{t}#rightarrowt#tilde{#chi}^{0}_{1}/b#tilde{#chi}^{#pm}_{1}(950,100)" ) elif spt == "T2bt_750_400": sig_names[i].append( "#tilde{t}#rightarrowt#tilde{#chi}^{0}_{1}/b#tilde{#chi}^{#pm}_{1}(750,400)" ) else: sig_names[i].append(spt) if type(h_sig_vec[i][-1]) == type(ROOT.TObject()): raise Exception("No {0}/h_{1} histogram for {2}!".format( dirname, vn, signame)) h_sig_vec[i][-1].SetDirectory(0) # handle the case with more than one directory for idir in range(1, len(dirnames)): h_sig_vec[i][-1].Add(fid.Get(dirnames[idir] + "/h_" + vn)) fid.Close() # make the output directory if it doesn't exist if not os.path.isdir(os.path.join(output_dir, dirname + tag)): os.makedirs(os.path.join(output_dir, dirname + tag)) # make all of the plots for i in range(len(plots)): if i in skipList: continue vn = plots[i][0] if suffix != None: vn += suffix userMin, userMax = None, None if plots[i][3] != None: userMin = plots[i][3][0] userMax = plots[i][3][1] if len(plots[i]) >= 5: utils.Rebin(h_bkg_vecs[i], h_sig_vec[i], h_data[i], plots[i][4]) doOverflow = True if len(plots[i]) >= 6: doOverflow = plots[i][5] markerSize = 0.8 title = utils.GetCRName(dirname) xAxisTitle = h_bkg_vecs[i][0].GetXaxis().GetTitle() unit = None if xAxisTitle == "": xAxisTitle = utils.GetVarName(vn) unit = utils.GetUnit(vn) xAxisTitle = utils.GetVarName(vn) #now I'm going stupid unit = utils.GetUnit(vn) #now I'm going stupid subtitles = utils.GetSubtitles(dirname) if h_data[i] != None: if not scaleMC: subLegText = ["# Data events: {ndata}"] elif type(scaleMC) == float: subLegText = [ "MC scaled by {}".format(scaleMC), "# Data events: {ndata}" ] else: subLegText = [ "MC scaled by {datamcsf}", "# Data events: {ndata}" ] else: subLegText = None subLegText = None subtitles = None title = None # subLegText = None if dogencat: sns = [utils.GetSampleName(s) for s in gencats] colors = [utils.GetColor(s) for s in gencats] else: sns = [utils.GetSampleName(s) for s in samples] colors = [utils.GetColor(s) for s in samples] for ext in exts: saveAs = os.path.join(output_dir, dirname + tag, "{0}_{1}.{2}".format(dirname, vn, ext)) ppm.plotDataMC( h_bkg_vecs[i], sns, h_data[i], doPause=False, xAxisTitle=xAxisTitle, lumi=lumi, lumiUnit='fb', title=title, subtitles=subtitles, dataTitle=datatitle, xRangeUser=plots[i][2], isLog=plots[i][1], saveAs=saveAs, scaleMCtoData=scaleMC, xAxisUnit=unit, userMin=userMin, userMax=userMax, doSort=False, customColors=colors, #markerSize=markerSize, titleSize=0.035, subtitleSize=0.033, legCoords=(0.60,0.70,0.87,0.895), markerSize=markerSize, titleSize=0.035, subtitleSize=0.033, legCoords=(0.33, 0.66, 0.87, 0.895), subLegText=subLegText, subLegTextSize=0.036, doBkgError=True, doOverflow=doOverflow, cmsTextSize=0.04, convertToPoisson=False, drawZeros=False, drawSystematicBand=drawSystematicBand, systematics=systs[i], h_sig_vec=h_sig_vec[i], sig_names=sig_names[i], ratioType=ratioType)
h_rs = f_rs.Get("{0}/h_{1}".format(dir,hname)) h_data = f_data.Get("{0}/h_{1}".format(dir,hname)) if not isMC: h_ewk = [f.Get("{0}/h_{1}".format(dir,hname)) for f in f_ewk] if isMC: h_rs.Scale(lumi) h_data.Scale(lumi) else: [h.Scale(lumi) for h in h_ewk] h_bkg_vec = [h_rs] if isMC else [h_rs] + h_ewk bkg_names = ["RS from MC"] if isMC else ["RS from Data"] + ewk_samples if not isMC and "sr" in dir: h_bkg_vec = [h_rs] bkg_names = ["RS from Data"] h_data = None saveAs=outdir+"/{0}_{1}_{2}.".format("mc" if isMC else "data{0}".format(year), dir, hname) for ext in ["pdf","png"]: ppm.plotDataMC(h_bkg_vec, bkg_names, h_data, dataTitle="QCD MC" if isMC else "Data", lumi=lumi, xAxisTitle=xnames[ih], doPause=False, doMT2Colors=True, yRangeUserRatio=(0,3), xRangeUser=xRangeUser[ih], doOverflow=False, isLog=isLog[ih], saveAs=saveAs+ext)