def closure_plot(predict, estimate, nbins=1): # Glob the file lists bkg_list_wjets = [output_dirpath + "/wj_ht.root"] bkg_list_ttbar = [output_dirpath + "/tt_1l.root"] # Get all the histogram objects h_wjets_predict = ru.get_summed_histogram(bkg_list_wjets, predict) h_ttbar_predict = ru.get_summed_histogram(bkg_list_ttbar, predict) h_wjets_estimate = ru.get_summed_histogram(bkg_list_wjets, estimate) h_ttbar_estimate = ru.get_summed_histogram(bkg_list_ttbar, estimate) # Set the names of the histograms h_wjets_predict.SetTitle("W predict") h_ttbar_predict.SetTitle("Top predict") h_wjets_estimate.SetTitle("W estimate") h_ttbar_estimate.SetTitle("Top estimate") # Color settings colors = [ 2005, 2001, ] # Options alloptions = { "ratio_range": [0.0, 2.0], "nbins": nbins, "autobin": False, "legend_scalex": 1.8, "legend_scaley": 1.1, "output_name": "plots/{}/{}/{}/closure/{}_nbins{}.pdf".format( input_ntup_tag, analysis_tag, "ss" if isSS else "3l", predict + "__" + estimate, nbins), "bkg_sort_method": "unsorted", "no_ratio": False, "print_yield": False, "yaxis_log": False, "legend_smart": True, "lumi_value": lumi, "legend_datalabel": "Estimate", "yield_prec": 3, "print_yield": True, } # The bkg histogram list bgs_list = [h_ttbar_predict.Clone(), h_wjets_predict.Clone()] #bgs_list = [ h_ttbar_predict ] h_estimate = h_wjets_estimate.Clone("Estimate") h_estimate.Add(h_ttbar_estimate) ll = ["Top Predict", "W Predict"] # Plot them p.plot_hist(bgs=bgs_list, data=h_estimate, colors=colors, syst=None, legend_labels=ll, options=alloptions)
def write_datacards(ntuple_version, tag): # ntuple_version = args.sample_set_name # tag = args.tag if args.wwz_only: fname_sig = "outputs/{}/{}/wwz.root".format(ntuple_version, tag) else: fname_sig = "outputs/{}/{}/sig.root".format(ntuple_version, tag) fname_wwz = "outputs/{}/{}/wwz.root".format(ntuple_version, tag) fname_wzz = "outputs/{}/{}/wzz.root".format(ntuple_version, tag) fname_zzz = "outputs/{}/{}/zzz.root".format(ntuple_version, tag) fname_ttz = "outputs/{}/{}/ttz.root".format(ntuple_version, tag) fname_zz = "outputs/{}/{}/zz.root".format(ntuple_version, tag) fname_wz = "outputs/{}/{}/wz.root".format(ntuple_version, tag) fname_twz = "outputs/{}/{}/twz.root".format(ntuple_version, tag) fname_rare = "outputs/{}/{}/rare.root".format(ntuple_version, tag) fname_dyttbar = "outputs/{}/{}/dyttbar.root".format(ntuple_version, tag) fname_higgs = "outputs/{}/{}/higgs.root".format(ntuple_version, tag) fname_othernoh = "outputs/{}/{}/othernoh.root".format(ntuple_version, tag) fname_data = "outputs/{}/{}/data.root".format(ntuple_version, tag) year = "2" + ntuple_version.split("_")[0].split("2")[1] if "2016" in ntuple_version and "2017" in ntuple_version: year = "All" prefix = "{}/{}".format(ntuple_version, tag) procs = [ "data_obs", "sig", "ttz", "zz", "wz", "twz", "rare", "dyttbar", "higgs" ] mcprocs = procs[1:] bkgprocs = procs[2:] fnames = [ fname_data, fname_sig, fname_ttz, fname_zz, fname_wz, fname_twz, fname_rare, fname_dyttbar, fname_higgs ] nonzzbkg = [ fname_sig, fname_ttz, fname_wz, fname_twz, fname_rare, fname_dyttbar, fname_higgs ] nonttzbkg = [ fname_sig, fname_zz, fname_wz, fname_twz, fname_rare, fname_dyttbar, fname_higgs ] procs = ["data_obs", "sig", "ttz", "zz", "wz", "twz", "higgs", "other"] mcprocs = procs[1:] bkgprocs = procs[2:] fnames = [ fname_data, fname_sig, fname_ttz, fname_zz, fname_wz, fname_twz, fname_higgs, fname_othernoh ] nonzzbkg = [ fname_sig, fname_ttz, fname_wz, fname_twz, fname_higgs, fname_othernoh ] nonttzbkg = [ fname_sig, fname_zz, fname_wz, fname_twz, fname_higgs, fname_othernoh ] if args.wwz_only: procs = [ "data_obs", "sig", "wzz", "zzz", "zz", "ttz", "twz", "wz", "higgs", "other" ] mcprocs = procs[1:] bkgprocs = procs[2:] fnames = [ fname_data, fname_wwz, fname_wzz, fname_zzz, fname_zz, fname_ttz, fname_twz, fname_wz, fname_higgs, fname_othernoh ] nonzzbkg = [ fname_wwz, fname_wzz, fname_zzz, fname_ttz, fname_twz, fname_wz, fname_higgs, fname_othernoh ] nonttzbkg = [ fname_wwz, fname_wzz, fname_zzz, fname_zz, fname_twz, fname_wz, fname_higgs, fname_othernoh ] systcategs = [ "BTagHF", "BTagLF", "JES", "Pileup", "Qsq", "PDF", "AlphaS", "MET", "JER", "METPileup" ] # Null string is the nominal variation systnames = ["Nominal"] # Nominal always exist for systcateg in systcategs: systnames.append(systcateg + "Up") systnames.append(systcateg + "Down") ############# # Open TFiles ############# tfiles = {} for proc, fname in zip(procs, fnames): tfiles[proc] = r.TFile(fname) ########################### # OnZ Control region yields ########################### onz_zz_h = pr.get_summed_histogram([fname_zz], "ChannelOnZ__Yield") onz_data_h = pr.get_summed_histogram([fname_data], "ChannelOnZ__Yield") onz_nonzz_h = pr.get_summed_histogram(nonzzbkg, "ChannelOnZ__Yield") zz_sf = pr.get_sf(onz_zz_h, onz_data_h, onz_nonzz_h).GetBinContent(1) zz_sferr = pr.get_sf(onz_zz_h, onz_data_h, onz_nonzz_h).GetBinError(1) expected_nevt_zz = onz_data_h.GetBinContent(1) ############################ # BTag Control region yields ############################ bcr_ttz_h = pr.get_summed_histogram([fname_ttz], "ChannelBTagEMu__Yield") bcr_data_h = pr.get_summed_histogram([fname_data], "ChannelBTagEMu__Yield") bcr_nonttz_h = pr.get_summed_histogram(nonttzbkg, "ChannelBTagEMu__Yield") ttz_sf = pr.get_sf(bcr_ttz_h, bcr_data_h, bcr_nonttz_h).GetBinContent(1) ttz_sferr = pr.get_sf(bcr_ttz_h, bcr_data_h, bcr_nonttz_h).GetBinError(1) expected_nevt_ttz = bcr_data_h.GetBinContent(1) if not args.print_yields: print year, "ttz_sf", "{:.2f} +/- {:.2f}".format( ttz_sf, ttz_sferr), expected_nevt_ttz print year, "zz_sf", "{:.2f} +/- {:.2f}".format( zz_sf, zz_sferr), expected_nevt_zz ############################### # EMu channel data card writing ############################### # number of bins fitreg = "EMuHighMT" if args.emu_one_bin: nbins = 1 fitvar = "Yield" else: nbins = 5 fitvar = "MllNom" # nbins = 5 # fitvar = "pt_zeta" # Main data base to hold all the histograms hists_db = {} # Loop over the processes for proc in procs: # Retrieve the tfile tfile = tfiles[proc] # For each processes create another map to hold various histograms hists_db[proc] = {} # Loop over the systematic variations for syst in systnames: if syst == "Nominal": if nbins == 5: h = rebin36( tfile.Get("Channel{}__{}".format(fitreg, fitvar)).Clone()) else: h = tfile.Get("Channel{}__{}".format(fitreg, fitvar)).Clone() else: systhacked = syst if proc == "NONE": systhacked = "" if nbins == 5: h = rebin36( tfile.Get("Channel{}{}__{}".format( fitreg, systhacked, fitvar)).Clone()) else: h = tfile.Get("Channel{}{}__{}".format( fitreg, systhacked, fitvar)).Clone() # if nbins == 5: # h = rebin36(tfile.Get("Channel{}{}__{}".format(fitreg, syst, fitvar)).Clone()) # else: # h = tfile.Get("Channel{}{}__{}".format(fitreg, syst, fitvar)).Clone() h.SetTitle("emu{}_{}".format(year, proc)) if proc == "ttz": before_scale = h.Integral() h.Scale(ttz_sf) after_scale = h.Integral() if syst == "Nominal": print year, "ttz", before_scale, after_scale if proc == "zz": before_scale = h.Integral() h.Scale(zz_sf) after_scale = h.Integral() if syst == "Nominal": print year, "zz", before_scale, after_scale # if proc == "wz": h.Scale(2) hists_db[proc][syst] = h systs = [] # ZZ CR systematic line onz_cr_hist = r.TH1F("onz_cr", "", nbins, 0, nbins) for i in xrange(1, nbins + 1): onz_cr_hist.SetBinContent(i, expected_nevt_zz) alpha = hists_db["zz"]["Nominal"].Clone("alpha") alpha.Divide(onz_cr_hist) thissyst = {} for proc in mcprocs: if proc == "zz": thissyst["emu{}_".format(year) + proc] = [ "{:4f}".format(alpha.GetBinContent(i)) for i in range(1, nbins + 1) ] else: thissyst["emu{}_".format(year) + proc] = 0 systs.append(("CRZZ{}".format(year), "gmN", [onz_cr_hist], thissyst)) # ttZ CR systematic line btag_cr_hist = r.TH1F("btag_cr", "", nbins, 0, nbins) for i in xrange(1, nbins + 1): btag_cr_hist.SetBinContent(i, expected_nevt_ttz) alpha = hists_db["ttz"]["Nominal"].Clone("alpha") alpha.Divide(btag_cr_hist) thissyst = {} for proc in mcprocs: if proc == "ttz": thissyst["emu{}_".format(year) + proc] = [ "{:4f}".format(alpha.GetBinContent(i)) for i in range(1, nbins + 1) ] else: thissyst["emu{}_".format(year) + proc] = 0 systs.append(("CRTTZ{}".format(year), "gmN", [btag_cr_hist], thissyst)) # Experimental systematics for systcateg in systcategs: thissyst = {} for proc in mcprocs: if proc not in ["zz", "ttz"]: thissyst["emu{}_".format(year) + proc] = [ hists_db[proc][systcateg + "Up"], hists_db[proc][systcateg + "Down"] ] else: thissyst["emu{}_".format(year) + proc] = 0 systs.append((systcateg + year, "lnN", [], thissyst)) # # Flat additional systematics # thissyst = {} # for proc in mcprocs: # if proc == "ttz": thissyst["emu{}_".format(year) + proc] = "1.11" # else: thissyst["emu{}_".format(year) + proc] = 0 # systs.append( ("FlatSystTFNbTTZ{}".format(year), "lnN", [], thissyst) ) # # Flat additional systematics # thissyst = {} # for proc in mcprocs: # if proc == "ttz": thissyst["emu{}_".format(year) + proc] = "1.02" # else: thissyst["emu{}_".format(year) + proc] = 0 # systs.append( ("FlatSystMTexpTTZ{}".format(year), "lnN", [], thissyst) ) # Flat additional systematics thissyst = {} for proc in mcprocs: if proc == "ttz": thissyst["emu{}_".format(year) + proc] = "1.105594" else: thissyst["emu{}_".format(year) + proc] = 0 systs.append(("FlatSystTFEMuTTZ{}".format(year), "lnN", [], thissyst)) # Flat additional systematics thissyst = {} for proc in mcprocs: if proc == "zz": thissyst["emu{}_".format(year) + proc] = "1.049173" else: thissyst["emu{}_".format(year) + proc] = 0 systs.append(("FlatSystTFEMuZZ{}".format(year), "lnN", [], thissyst)) # # Flat additional systematics # thissyst = {} # for proc in mcprocs: # if proc == "zz": thissyst["emu{}_".format(year) + proc] = "1.05" # else: thissyst["emu{}_".format(year) + proc] = 0 # systs.append( ("FlatSystMTexpZZ{}".format(year), "lnN", [], thissyst) ) # Flat additional systematics thissyst = {} for proc in mcprocs: if proc == "wz": thissyst["emu{}_".format(year) + proc] = "1.6" # Fake Syst else: thissyst["emu{}_".format(year) + proc] = 0 systs.append(("FlatSystWZ{}".format(year), "lnN", [], thissyst)) # Flat additional systematics thissyst = {} for proc in mcprocs: thissyst["emu{}_".format(year) + proc] = "1.025" systs.append(("FlatSystLumi{}".format(year), "lnN", [], thissyst)) # Flat additional systematics thissyst = {} for proc in mcprocs: thissyst["emu{}_".format(year) + proc] = "1.03" systs.append(("FlatSystsIP3D{}".format(year), "lnN", [], thissyst)) # Flat additional systematics thissyst = {} for proc in mcprocs: thissyst["emu{}_".format(year) + proc] = "1.02" systs.append(("FlatSystsTrigSF{}".format(year), "lnN", [], thissyst)) # Now create data card writer sig = hists_db["sig"]["Nominal"] bgs = [hists_db[proc]["Nominal"] for proc in bkgprocs] data = hists_db["data_obs"]["Nominal"] d = dw.DataCardWriter( sig=sig, bgs=bgs, data=data, systs=systs, no_stat_procs=["emu{}_zz".format(year), "emu{}_ttz".format(year)]) finalyields = [] if nbins == 5: for i in xrange(1, nbins + 1): d.set_bin(i) d.set_region_name("bin{}".format(i)) d.write("stats/{}/emu_datacard_bin{}.txt".format(prefix, i)) if args.print_yields and args.wwz_only: vals = d.print_yields(detail=args.print_detail) if vals: print_yield_table(vals[0], vals[1], "textable/emu{}{}".format(year, i)) finalyields.append(vals) elif nbins == 1: d.set_bin(1) d.set_region_name("bin{}".format(1)) d.write("stats/{}/emu_datacard_singlebin{}.txt".format(prefix, 1)) if args.print_yields and args.wwz_only: vals = d.print_yields(detail=args.print_detail) if vals: print_yield_table(vals[0], vals[1], "textable/emu{}".format(year)) # colors = [2005, 2001, 2003, 2007, 920, 2012, 2011, 2002] # p.plot_hist(data=None, bgs=bgs, sigs=[sig], options={"bkg_sort_method":"ascending", "yaxis_range":[0.,2.5]}, colors=colors, sig_labels=["sig"], legend_labels=bkgprocs) ################################ # OffZ channel data card writing ################################ # number of bins nbins = 1 # Main data base to hold all the histograms hists_db = {} # Loop over the processes for proc in procs: # Retrieve the tfile tfile = tfiles[proc] # For each processes create another map to hold various histograms hists_db[proc] = {} # Loop over the systematic variations for syst in systnames: if syst == "Nominal": h = tfile.Get("ChannelOffZHighMET__Yield").Clone() else: systhacked = syst if proc == "NONE": systhacked = "" h = tfile.Get( "ChannelOffZHighMET{}__Yield".format(systhacked)).Clone() # h = tfile.Get("ChannelOffZHighMET{}__Yield".format(syst)).Clone() h.SetTitle("offz{}_{}".format(year, proc)) if proc == "ttz": before_scale = h.Integral() h.Scale(ttz_sf) after_scale = h.Integral() if syst == "Nominal": print year, "ttz", before_scale, after_scale if proc == "zz": before_scale = h.Integral() h.Scale(zz_sf) after_scale = h.Integral() if syst == "Nominal": print year, "zz", before_scale, after_scale # if proc == "wz": h.Scale(2) hists_db[proc][syst] = h systs = [] # ZZ CR systematic line onz_cr_hist = r.TH1F("onz_cr", "", nbins, 0, nbins) for i in xrange(1, nbins + 1): onz_cr_hist.SetBinContent(i, expected_nevt_zz) alpha = hists_db["zz"]["Nominal"].Clone("alpha") alpha.Divide(onz_cr_hist) thissyst = {} for proc in mcprocs: if proc == "zz": thissyst["offz{}_".format(year) + proc] = ["{:4f}".format(alpha.GetBinContent(1))] else: thissyst["offz{}_".format(year) + proc] = 0 systs.append(("CRZZ{}".format(year), "gmN", [onz_cr_hist], thissyst)) # ttZ CR systematic line btag_cr_hist = r.TH1F("btag_cr", "", nbins, 0, nbins) for i in xrange(1, nbins + 1): btag_cr_hist.SetBinContent(i, expected_nevt_ttz) alpha = hists_db["ttz"]["Nominal"].Clone("alpha") alpha.Divide(btag_cr_hist) thissyst = {} for proc in mcprocs: if proc == "ttz": thissyst["offz{}_".format(year) + proc] = ["{:4f}".format(alpha.GetBinContent(1))] else: thissyst["offz{}_".format(year) + proc] = 0 systs.append(("CRTTZ{}".format(year), "gmN", [btag_cr_hist], thissyst)) # Experimental systematics for systcateg in systcategs: thissyst = {} for proc in mcprocs: if proc not in ["zz", "ttz"]: thissyst["offz{}_".format(year) + proc] = [ hists_db[proc][systcateg + "Up"], hists_db[proc][systcateg + "Down"] ] else: thissyst["offz{}_".format(year) + proc] = 0 systs.append((systcateg + year, "lnN", [], thissyst)) # # Flat additional systematics # thissyst = {} # for proc in mcprocs: # if proc == "ttz": thissyst["offz{}_".format(year) + proc] = "1.10" # else: thissyst["offz{}_".format(year) + proc] = 0 # systs.append( ("FlatSystTFeemmTTZ{}".format(year), "lnN", [], thissyst) ) # # Flat additional systematics # thissyst = {} # for proc in mcprocs: # if proc == "ttz": thissyst["offz{}_".format(year) + proc] = "1.03" # else: thissyst["offz{}_".format(year) + proc] = 0 # systs.append( ("FlatSystMETexpTTZ{}".format(year), "lnN", [], thissyst) ) # Flat additional systematics thissyst = {} for proc in mcprocs: if proc == "ttz": thissyst["offz{}_".format(year) + proc] = "1.111924" else: thissyst["offz{}_".format(year) + proc] = 0 systs.append(("FlatSystTFEEMMTTZ{}".format(year), "lnN", [], thissyst)) # Flat additional systematics thissyst = {} for proc in mcprocs: if proc == "zz": thissyst["offz{}_".format(year) + proc] = "1.167012" else: thissyst["offz{}_".format(year) + proc] = 0 systs.append(("FlatSystTFEEMMZZ{}".format(year), "lnN", [], thissyst)) # # Flat additional systematics # thissyst = {} # for proc in mcprocs: # if proc == "zz": thissyst["offz{}_".format(year) + proc] = "1.23" # else: thissyst["offz{}_".format(year) + proc] = 0 # systs.append( ("FlatSystMETexpZZ{}".format(year), "lnN", [], thissyst) ) # Flat additional systematics thissyst = {} for proc in mcprocs: if proc == "wz": thissyst["offz{}_".format(year) + proc] = "1.6" # Fake Syst else: thissyst["offz{}_".format(year) + proc] = 0 systs.append(("FlatSystWZ{}".format(year), "lnN", [], thissyst)) # Flat additional systematics thissyst = {} for proc in mcprocs: thissyst["offz{}_".format(year) + proc] = "1.025" systs.append(("FlatSystLumi{}".format(year), "lnN", [], thissyst)) # Flat additional systematics thissyst = {} for proc in mcprocs: thissyst["offz{}_".format(year) + proc] = "1.03" systs.append(("FlatSystsIP3D{}".format(year), "lnN", [], thissyst)) # Flat additional systematics thissyst = {} for proc in mcprocs: thissyst["offz{}_".format(year) + proc] = "1.02" systs.append(("FlatSystsTrigSF{}".format(year), "lnN", [], thissyst)) # Now create data card writer sig = hists_db["sig"]["Nominal"] bgs = [hists_db[proc]["Nominal"] for proc in bkgprocs] data = hists_db["data_obs"]["Nominal"] d = dw.DataCardWriter( sig=sig, bgs=bgs, data=data, systs=systs, no_stat_procs=["offz{}_zz".format(year), "offz{}_ttz".format(year)]) for i in xrange(1, nbins + 1): d.set_bin(i) d.set_region_name("bin{}".format(i)) d.write("stats/{}/offz_datacard_bin{}.txt".format(prefix, i)) if args.print_yields and args.wwz_only: vals = d.print_yields(detail=args.print_detail) if vals: print_yield_table(vals[0], vals[1], "textable/offz{}".format(year)) finalyields.append(vals) if len(finalyields) > 0: procs = [ "sig", "wzz", "zzz", "zz", "ttz", "twz", "wz", "higgs", "other" ] histsdict = {} for proc in procs: if proc == "sig": h = r.TH1F("WWZ", "", 6, 0, 6) elif proc == "wzz": h = r.TH1F("WZZ", "", 6, 0, 6) elif proc == "zzz": h = r.TH1F("ZZZ", "", 6, 0, 6) else: h = r.TH1F("Fit{}".format(proc), "", 6, 0, 6) h.GetXaxis().SetBinLabel(1, "e#mu Bin 1") h.GetXaxis().SetBinLabel(2, "e#mu Bin 2") h.GetXaxis().SetBinLabel(3, "e#mu Bin 3") h.GetXaxis().SetBinLabel(4, "e#mu Bin 4") h.GetXaxis().SetBinLabel(5, "e#mu Bin 5") h.GetXaxis().SetBinLabel(6, "ee/#mu#mu") histsdict[proc] = h for index, item in enumerate(finalyields): for procfullname, rate in zip(item[0], item[1]): procname = procfullname.split("_")[1] print index, procname, rate histsdict[procname].SetBinContent(index + 1, rate.val) histsdict[procname].SetBinError(index + 1, rate.err) bkghists = [histsdict[proc].Clone() for proc in procs[3:]] sighists = [histsdict[proc].Clone() for proc in procs[:3]] lumi = 137 if "2016" in year: lumi = 35.9 if "2017" in year: lumi = 41.3 if "2018" in year: lumi = 59.74 p.plot_hist( bgs=bkghists, sigs=sighists, options={ "output_name": "fitplot/fit{}.pdf".format(year), "print_yield": True, "signal_scale": 1, "legend_scalex": 1.8, "legend_scaley": 1.0, "legend_ncolumns": 3, "legend_smart": True, "yaxis_log": False, "ymax_scale": 1.2, "lumi_value": lumi, # "no_overflow": True, "remove_underflow": True, "xaxis_ndivisions": 505, "ratio_range": [0., 2.], "xaxis_label": "Fit regions", "ratio_xaxis_title": "Fit regions", "no_ratio": True, }, colors=[2001, 2005, 2007, 2003, 2011, 920, 2012, 2011, 2002], legend_labels=["ZZ", "t#bar{t}Z", "tWZ", "WZ", "Higgs", "Other"], # sig_labels = ["WWZ","WZZ","ZZZ"] )
def plot(histnames, ps=0, sf=None, sfqcd=None, output_suffix="", dd_qcd=None): # Glob the file lists bkg_list_wjets = [output_dirpath + "/wj_incl.root"] bkg_list_dy = [output_dirpath + "/dy.root"] bkg_list_ttbar = [output_dirpath + "/tt_incl.root"] bkg_list_vv = [output_dirpath + "/ww.root", output_dirpath + "/wz.root"] bkg_list_qcd_mu = [output_dirpath + "/qcd_mu.root"] bkg_list_qcd_el = [output_dirpath + "/qcd_em.root"] bkg_list_qcd_bc = [output_dirpath + "/qcd_bc.root"] bkg_list_all = bkg_list_wjets + bkg_list_dy + bkg_list_ttbar + bkg_list_vv # Glob the data file list depending on the region if "Mu" in histnames: data_list = [output_dirpath + "/data_mu.root"] elif "El" in histnames: data_list = [output_dirpath + "/data_el.root"] else: data_list = [ output_dirpath + "/data_mu.root", output_dirpath + "/data_el.root" ] # Get all the histogram objects h_wjets = ru.get_summed_histogram(bkg_list_wjets, histnames) h_dy = ru.get_summed_histogram(bkg_list_dy, histnames) h_ttbar = ru.get_summed_histogram(bkg_list_ttbar, histnames) h_vv = ru.get_summed_histogram(bkg_list_vv, histnames) h_qcd_mu = ru.get_summed_histogram(bkg_list_qcd_mu, histnames) h_qcd_el = ru.get_summed_histogram(bkg_list_qcd_el, histnames) h_qcd_bc = ru.get_summed_histogram(bkg_list_qcd_bc, histnames) h_data = ru.get_summed_histogram(data_list, histnames) # Set the names of the histograms h_wjets.SetName("W") h_dy.SetName("Z") h_ttbar.SetName("Top") h_vv.SetName("VV") h_qcd_mu.SetName("QCD(#mu)") h_qcd_el.SetName("QCD(e)") h_qcd_bc.SetName("QCD(bc)") h_data.SetName("Data") # print h_wjets.Integral() + h_dy.Integral() + h_ttbar.Integral() + h_vv.Integral() # print h_qcd_el.Integral() + h_qcd_bc.Integral() # Scale the histograms appropriately from SF from the EWKCR if sf: if isinstance(sf, list): hists = [h_wjets, h_dy, h_ttbar, h_vv] for h in hists: for ii, s in enumerate(sf): bc = h.GetBinContent(ii + 1) be = h.GetBinError(ii + 1) h.SetBinContent(ii + 1, bc * s) h.SetBinError(ii + 1, be * s) else: if sf > 0: h_wjets.Scale(sf) h_dy.Scale(sf) h_ttbar.Scale(sf) h_vv.Scale(sf) if sfqcd: if isinstance(sfqcd, list): hists = [h_qcd_mu, h_qcd_el, h_qcd_bc] for h in hists: for ii, s in enumerate(sfqcd): bc = h.GetBinContent(ii + 1) be = h.GetBinError(ii + 1) h.SetBinContent(ii + 1, bc * s) h.SetBinError(ii + 1, be * s) else: if sfqcd > 0: h_qcd_mu.Scale(sfqcd) h_qcd_el.Scale(sfqcd) h_qcd_bc.Scale(sfqcd) # If the data needs some additional correction for the prescale if ps > 0: h_data.Scale(ps) # print h_wjets.Integral() + h_dy.Integral() + h_ttbar.Integral() + h_vv.Integral() # print h_qcd_el.Integral() + h_qcd_bc.Integral() # print h_data.Integral() # Color settings colors = [2007, 2005, 2003, 2001, 920, 921] # Options alloptions = { "ratio_range": [0.0, 2.0], "nbins": 30, "autobin": False, "legend_scalex": 1.8, "legend_scaley": 1.1, "output_name": "plots/{}/{}/{}/plot/{}{}.pdf".format(input_ntup_tag, analysis_tag, "ss" if isSS else "3l", histnames, output_suffix), "bkg_sort_method": "unsorted", "no_ratio": False, "print_yield": True, "yaxis_log": False if "ptcorr" in histnames else False, #"yaxis_log": False, #"yaxis_log": False, "divide_by_bin_width": True, "legend_smart": False if "ptcorr" in histnames else True, "lumi_value": lumi, } # The bkg histogram list h_qcd = h_qcd_mu if "Mu" in histnames else h_qcd_el if dd_qcd: h_qcd = dd_qcd bgs_list = [h_vv, h_ttbar, h_dy, h_wjets, h_qcd] legend_labels = ["VV", "t#bar{t}", "DY", "W", "QCD(#mu)" ] if "Mu" in histnames else [ "VV", "t#bar{t}", "DY", "W", "QCD(e)", "QCD(HF)" ] if "Mu" not in histnames: bgs_list.append(h_qcd_bc) # # For 2018 merge the last two bins in the central # if "ptcorretarolledcoarse" in histnames: # def merge_4_5(h): # bc4 = h.GetBinContent(4) # bc5 = h.GetBinContent(5) # be4 = h.GetBinError(4) # be5 = h.GetBinError(5) # nb = E(bc4, be4) + E(bc5, be5) # nbc = nb.val # nbe = nb.err # h.SetBinContent(4, nbc) # h.SetBinError(4, nbe) # h.SetBinContent(5, nbc) # h.SetBinError(5, nbe) # merge_4_5(h_vv) # merge_4_5(h_ttbar) # merge_4_5(h_dy) # merge_4_5(h_wjets) # merge_4_5(h_qcd_mu) # merge_4_5(h_qcd_el) # merge_4_5(h_qcd_bc) # merge_4_5(h_data) # Plot them p.plot_hist(bgs=bgs_list, data=h_data.Clone("Data"), colors=colors, syst=None, legend_labels=legend_labels, options=alloptions) # print h_wjets.Integral() + h_dy.Integral() + h_ttbar.Integral() + h_vv.Integral() # print h_qcd_el.Integral() + h_qcd_bc.Integral() # print h_data.Integral() # Obtain the histogram again to return the object for further calculations # Data-driven QCD = data - bkg h_ddqcd = ru.get_summed_histogram(data_list, histnames) h_bkg = ru.get_summed_histogram(bkg_list_all, histnames) h_wjets = ru.get_summed_histogram(bkg_list_wjets, histnames) h_dy = ru.get_summed_histogram(bkg_list_dy, histnames) h_ttbar = ru.get_summed_histogram(bkg_list_ttbar, histnames) h_vv = ru.get_summed_histogram(bkg_list_vv, histnames) if ps > 0: h_ddqcd.Scale(ps) # Scale the histograms appropriately from SF from the EWKCR if sf: if isinstance(sf, list): hists = [h_bkg, h_wjets, h_dy, h_ttbar, h_vv] for h in hists: for ii, s in enumerate(sf): bc = h.GetBinContent(ii + 1) be = h.GetBinError(ii + 1) h.SetBinContent(ii + 1, bc * s) h.SetBinError(ii + 1, be * s) else: if sf > 0: h_bkg.Scale(sf) h_wjets.Scale(sf) h_dy.Scale(sf) h_ttbar.Scale(sf) h_vv.Scale(sf) if "ptcorretarolled" in histnames: # print h_ddqcd.GetBinContent(6), h_ddqcd.GetBinContent(7) # d6 = E(h_ddqcd.GetBinContent(6), h_ddqcd.GetBinError(6)) + E(h_ddqcd.GetBinContent(7), h_ddqcd.GetBinError(7)) # d13 = E(h_ddqcd.GetBinContent(13), h_ddqcd.GetBinError(13)) + E(h_ddqcd.GetBinContent(14), h_ddqcd.GetBinError(14)) # b6 = E(h_bkg.GetBinContent(6), h_bkg.GetBinError(6)) + E(h_bkg.GetBinContent(7), h_bkg.GetBinError(7)) # b13 = E(h_bkg.GetBinContent(13), h_bkg.GetBinError(13)) + E(h_bkg.GetBinContent(14), h_bkg.GetBinError(14)) # h_ddqcd.SetBinContent(6, d6.val) # h_ddqcd.SetBinContent(7, d6.val) # h_ddqcd.SetBinError(6, d6.err) # h_ddqcd.SetBinError(7, d6.err) # h_ddqcd.SetBinContent(13, d13.val) # h_ddqcd.SetBinContent(14, d13.val) # h_ddqcd.SetBinError(13, d13.err) # h_ddqcd.SetBinError(14, d13.err) # h_bkg.SetBinContent(6, b6.val) # h_bkg.SetBinContent(7, b6.val) # h_bkg.SetBinError(6, b6.err) # h_bkg.SetBinError(7, b6.err) # h_bkg.SetBinContent(13, b13.val) # h_bkg.SetBinContent(14, b13.val) # h_bkg.SetBinError(13, b13.err) # h_bkg.SetBinError(14, b13.err) for ii in xrange(1, h_ddqcd.GetNbinsX() + 1): data_bc = h_ddqcd.GetBinContent(ii) data_be = h_ddqcd.GetBinError(ii) bkg_bc = h_bkg.GetBinContent(ii) bkg_be = h_bkg.GetBinError(ii) d = E(data_bc, data_be) b = E(bkg_bc, bkg_be) n = d - b if isSS: if d.err > n.val: n.val = d.err h_ddqcd.SetBinContent(ii, n.val) h_ddqcd.SetBinError(ii, n.err) else: h_ddqcd.Add(h_bkg, -1) # MC QCD h_qcd_mu = ru.get_summed_histogram(bkg_list_qcd_mu, histnames).Clone("QCD(#mu)") h_qcd_el = ru.get_summed_histogram(bkg_list_qcd_el, histnames).Clone("QCD(EM)") h_qcd_bc = ru.get_summed_histogram(bkg_list_qcd_bc, histnames).Clone("QCD(HF)") return h_ddqcd, h_data, h_bkg, h_qcd_mu, h_qcd_el, h_qcd_bc, h_wjets, h_dy, h_ttbar, h_vv
def el_iso(binname=""): # Glob the file lists mcs = [ output_dirpath + "/wj_ht.root", output_dirpath + "/tt_1l.root", ] qcds = [ output_dirpath + "/qcd_em.root", output_dirpath + "/qcd_bc.root", ] bkg_hist = "ElClosureLoose{}__eliso".format(binname) qcd_hist = "OneElMR{}__iso".format(binname) h_bkg = ru.get_summed_histogram(mcs, bkg_hist) h_qcd = ru.get_summed_histogram(qcds, qcd_hist) h_qcd_em = ru.get_summed_histogram([output_dirpath + "/qcd_em.root"], qcd_hist) h_qcd_bc = ru.get_summed_histogram([output_dirpath + "/qcd_bc.root"], qcd_hist) h_bkg.Scale(1. / h_bkg.Integral()) h_qcd.Scale(1. / h_qcd.Integral()) h_qcd_em.Scale(1. / h_qcd_em.Integral()) h_qcd_bc.Scale(1. / h_qcd_bc.Integral()) h_qcd.SetName("QCD(e)") h_qcd_em.SetName("QCD(LF)") h_qcd_bc.SetName("QCD(HF)") # Color settings colors = [ 2001, ] # Options alloptions = { "ratio_range": [0.0, 2.0], "nbins": 10, "autobin": False, "legend_scalex": 1.8, "legend_scaley": 1.1, "output_name": "plots/{}/{}/{}/eliso/{}.pdf".format(input_ntup_tag, analysis_tag, "ss" if isSS else "3l", bkg_hist + "__" + qcd_hist), "bkg_sort_method": "unsorted", "no_ratio": False, "print_yield": True, "yield_prec": 3, "draw_points": True, "lumi_value": 41.3, "legend_datalabel": "W+t#bar{t}", } # Plot them p.plot_hist(sigs=[h_qcd_em, h_qcd_bc], bgs=[h_qcd], data=h_bkg, colors=colors, syst=None, options=alloptions)
def el_fakerate(): # Glob the file lists mcs = [ output_dirpath + "/wj_ht.root", output_dirpath + "/tt_1l.root", ] num = "ElClosureTight__elptcorretarolledcoarse" den = "ElClosureLoose__elptcorretarolledcoarse" h_num = ru.get_summed_histogram(mcs, num) h_den = ru.get_summed_histogram(mcs, den) u.move_in_overflows(h_num) u.move_in_overflows(h_den) h_num.Divide(h_den) qcds = [ output_dirpath + "/qcd_em.root", output_dirpath + "/qcd_bc.root", ] qcd_num = "OneElTightMR__elptcorretarolledcoarse" qcd_den = "OneElMR__elptcorretarolledcoarse" h_qcd_num = ru.get_summed_histogram(qcds, qcd_num) h_qcd_den = ru.get_summed_histogram(qcds, qcd_den) u.move_in_overflows(h_qcd_num) u.move_in_overflows(h_qcd_den) h_qcd_num.Divide(h_qcd_den) h_qcd_num.SetName("QCD(e)") # Color settings colors = [ 2005, 2001, ] # Options alloptions = { "ratio_range": [0.0, 2.0], "nbins": 180, "autobin": False, "legend_scalex": 1.8, "legend_scaley": 1.1, "output_name": "plots/{}/{}/{}/fakeratemc/{}.pdf".format(input_ntup_tag, analysis_tag, "ss" if isSS else "3l", num + "__" + den), "bkg_sort_method": "unsorted", "no_ratio": False, "print_yield": True, "yield_prec": 3, "draw_points": True, "lumi_value": 41.3, "legend_datalabel": "W+t#bar{t}" } # Plot them p.plot_hist(bgs=[h_qcd_num], data=h_num, colors=colors, syst=None, options=alloptions)
def plot(histnames, ps=0, sf=0): # Glob the file lists # bkg_list_wjets = glob.glob(output_dirpath+"/WJetsToLNu_Tune*.root") # bkg_list_dy = glob.glob(output_dirpath+"/DY*.root") # bkg_list_ttbar = glob.glob(output_dirpath+"/TTJets_Tune*.root") # bkg_list_vv = glob.glob(output_dirpath+"/WW*.root") + glob.glob(output_dirpath+"/WW*.root") # bkg_list_qcd_mu = glob.glob(output_dirpath+"/QCD*MuEn*.root") # bkg_list_qcd_el = glob.glob(output_dirpath+"/QCD*EMEn*.root") # bkg_list_qcd_bc = glob.glob(output_dirpath+"/QCD*bcToE*.root") bkg_list_wjets = [output_dirpath + "/wj_incl.root"] bkg_list_dy = [output_dirpath + "/dy.root"] bkg_list_ttbar = [output_dirpath + "/tt_incl.root"] bkg_list_vv = [ output_dirpath + "/ww.root", output_dirpath + "/wz.root" ] bkg_list_qcd_mu = [output_dirpath + "/qcd_mu.root"] bkg_list_qcd_el = [output_dirpath + "/qcd_em.root"] bkg_list_qcd_bc = [output_dirpath + "/qcd_bc.root"] bkg_list_all = bkg_list_wjets + bkg_list_dy + bkg_list_ttbar + bkg_list_vv # Glob the data file list depending on the region if "Mu" in histnames: data_list = [output_dirpath + "/data_mu.root"] elif "El" in histnames: data_list = [output_dirpath + "/data_el.root"] else: data_list = [ output_dirpath + "/data_mu.root", output_dirpath + "/data_el.root" ] # Get all the histogram objects h_wjets = ru.get_summed_histogram(bkg_list_wjets, histnames) h_dy = ru.get_summed_histogram(bkg_list_dy, histnames) h_ttbar = ru.get_summed_histogram(bkg_list_ttbar, histnames) h_vv = ru.get_summed_histogram(bkg_list_vv, histnames) h_qcd_mu = ru.get_summed_histogram(bkg_list_qcd_mu, histnames) h_qcd_el = ru.get_summed_histogram(bkg_list_qcd_el, histnames) h_qcd_bc = ru.get_summed_histogram(bkg_list_qcd_bc, histnames) h_data = ru.get_summed_histogram(data_list, histnames) # Set the names of the histograms h_wjets.SetName("W") h_dy.SetName("Z") h_ttbar.SetName("Top") h_vv.SetName("VV") h_qcd_mu.SetName("QCD(#mu)") h_qcd_el.SetName("QCD(e)") h_qcd_bc.SetName("QCD(bc)") h_data.SetName("Data") # Scale the histograms appropriately from SF from the EWKCR if sf > 0: h_wjets.Scale(sf) h_dy.Scale(sf) h_ttbar.Scale(sf) h_vv.Scale(sf) # If the data needs some additional correction for the prescale if ps > 0: h_data.Scale(ps) # Color settings colors = [2007, 2005, 2003, 2001, 920, 2] # Options alloptions = { "ratio_range": [0.0, 2.0], "nbins": 30, "autobin": False, "legend_scalex": 1.8, "legend_scaley": 1.1, "output_name": "plots/{}/{}/{}/plot/{}.pdf".format(input_ntup_tag, analysis_tag, "ss" if isSS else "3l", histnames), "bkg_sort_method": "unsorted", "no_ratio": False, "print_yield": False, "yaxis_log": True if "ptcorr" in histnames else False, #"yaxis_log": False, "divide_by_bin_width": True, "legend_smart": False if "ptcorr" in histnames else True, "lumi_value": 41.3, } # The bkg histogram list bgs_list = [h_vv, h_ttbar, h_dy, h_wjets, h_qcd_mu], bgs_list = [ h_vv, h_ttbar, h_dy, h_wjets, h_qcd_mu ] if "Mu" in histnames else [h_vv, h_ttbar, h_dy, h_wjets, h_qcd_el] legend_labels = ["VV", "t#bar{t}", "DY", "W", "QCD"] # Plot them p.plot_hist(bgs=bgs_list, data=h_data.Clone("Data"), colors=colors, syst=None, legend_labels=legend_labels, options=alloptions) # Obtain the histogram again to return the object for further calculations # Data-driven QCD = data - bkg h_ddqcd = ru.get_summed_histogram(data_list, histnames) h_bkg = ru.get_summed_histogram(bkg_list_all, histnames) if ps > 0: h_ddqcd.Scale(ps) if sf > 0: h_bkg.Scale(sf) h_ddqcd.Add(h_bkg, -1) # MC QCD h_qcd_mu = ru.get_summed_histogram(bkg_list_qcd_mu, histnames).Clone("QCD(#mu)") h_qcd_el = ru.get_summed_histogram(bkg_list_qcd_el, histnames).Clone("QCD(EM)") h_qcd_bc = ru.get_summed_histogram(bkg_list_qcd_bc, histnames).Clone("QCD(HF)") return h_ddqcd, h_data, h_bkg, h_qcd_mu, h_qcd_el, h_qcd_bc
def get_alpha_uncertainty(ntuple_version, tag, numerator, denominator, num_proc, valopt): if "2016" in ntuple_version: lumi = 35.9 if "2017" in ntuple_version: lumi = 41.3 if "2018" in ntuple_version: lumi = 59.74 plots_basedir = "plots/{}/{}/exp/".format(ntuple_version, tag) fname_sig = "outputs/{}/{}/sig.root".format(ntuple_version, tag) # fname_sig = "outputs/{}/{}/wwz.root".format(ntuple_version, tag) fname_ttz = "outputs/{}/{}/ttz.root".format(ntuple_version, tag) fname_zz = "outputs/{}/{}/zz.root".format(ntuple_version, tag) fname_wz = "outputs/{}/{}/wz.root".format(ntuple_version, tag) fname_twz = "outputs/{}/{}/twz.root".format(ntuple_version, tag) fname_rare = "outputs/{}/{}/rare.root".format(ntuple_version, tag) # fname_rare = "outputs/{}/{}/rarevvv.root".format(ntuple_version, tag) fname_dyttbar = "outputs/{}/{}/dyttbar.root".format(ntuple_version, tag) fname_higgs = "outputs/{}/{}/higgs.root".format(ntuple_version, tag) fname_data = "outputs/{}/{}/data.root".format(ntuple_version, tag) year = "2" + ntuple_version.split("_")[0].split("2")[1] prefix = "{}/{}".format(ntuple_version, tag) procs = [ "data_obs", "sig", "ttz", "zz", "wz", "twz", "rare", "dyttbar", "higgs" ] mcprocs = procs[1:] bkgprocs = procs[2:] fnames = [ fname_data, fname_sig, fname_ttz, fname_zz, fname_wz, fname_twz, fname_rare, fname_dyttbar, fname_higgs ] nonzzbkg = [ fname_sig, fname_ttz, fname_wz, fname_twz, fname_rare, fname_dyttbar, fname_higgs ] nonttzbkg = [ fname_sig, fname_zz, fname_wz, fname_twz, fname_rare, fname_dyttbar, fname_higgs ] if num_proc == "zz": h_denom_nonzzbkg = pr.get_summed_histogram(nonzzbkg, denominator) E_denom_nonzzbkg = pr.get_integral_as_E(h_denom_nonzzbkg) h_denom_data = pr.get_summed_histogram([fname_data], denominator) E_denom_data = pr.get_integral_as_E(h_denom_data) h_denom_zz = pr.get_summed_histogram([fname_zz], denominator) E_denom_zz = pr.get_integral_as_E(h_denom_zz) # print (E_denom_data - E_denom_nonzzbkg) # print E_denom_zz h_numer_nonzzbkg = pr.get_summed_histogram(nonzzbkg, numerator) E_numer_nonzzbkg = pr.get_integral_as_E(h_numer_nonzzbkg) h_numer_data = pr.get_summed_histogram([fname_data], numerator) E_numer_data = pr.get_integral_as_E(h_numer_data) h_numer_zz = pr.get_summed_histogram([fname_zz], numerator) E_numer_zz = pr.get_integral_as_E(h_numer_zz) # print (E_numer_data - E_numer_nonzzbkg) # print E_numer_zz data_eff = (E_numer_data - E_numer_nonzzbkg) / (E_denom_data - E_denom_nonzzbkg) mc_eff = E_numer_zz / E_denom_zz eff_ratio = data_eff / mc_eff # print E_numer_data, E_numer_zz # print "mc_eff:", mc_eff # print "data_eff:", data_eff if valopt == "eff": return mc_eff elif valopt == "den": return E_denom_zz elif valopt == "num": return E_numer_zz else: h_denom_nonttzbkg = pr.get_summed_histogram(nonttzbkg, denominator) E_denom_nonttzbkg = pr.get_integral_as_E(h_denom_nonttzbkg) h_denom_data = pr.get_summed_histogram([fname_data], denominator) E_denom_data = pr.get_integral_as_E(h_denom_data) h_denom_ttz = pr.get_summed_histogram([fname_ttz], denominator) E_denom_ttz = pr.get_integral_as_E(h_denom_ttz) # print (E_denom_data - E_denom_nonttzbkg) # print E_denom_ttz h_numer_nonttzbkg = pr.get_summed_histogram(nonttzbkg, numerator) E_numer_nonttzbkg = pr.get_integral_as_E(h_numer_nonttzbkg) h_numer_data = pr.get_summed_histogram([fname_data], numerator) E_numer_data = pr.get_integral_as_E(h_numer_data) h_numer_ttz = pr.get_summed_histogram([fname_ttz], numerator) E_numer_ttz = pr.get_integral_as_E(h_numer_ttz) # print (E_numer_data - E_numer_nonttzbkg) # print E_numer_ttz data_eff = (E_numer_data - E_numer_nonttzbkg) / (E_denom_data - E_denom_nonttzbkg) mc_eff = E_numer_ttz / E_denom_ttz eff_ratio = data_eff / mc_eff # print "mc_eff:", mc_eff # print "data_eff:", data_eff if valopt == "eff": return mc_eff elif valopt == "den": return E_denom_ttz elif valopt == "num": return E_numer_ttz