def get_datadriven_fakerate(samples, cutloose, cuttight, var, dosf=False): # Retrieve histograms h_loose_data = get_histogram(samples, "data" , cutloose , var , dosf) h_loose_top = get_histogram(samples, "top" , cutloose , var , dosf) h_loose_w = get_histogram(samples, "w" , cutloose , var , dosf) h_loose_dy = get_histogram(samples, "dy" , cutloose , var , dosf) # Retrieve histograms h_tight_data = get_histogram(samples, "data" , cuttight , var , dosf) h_tight_top = get_histogram(samples, "top" , cuttight , var , dosf) h_tight_w = get_histogram(samples, "w" , cuttight , var , dosf) h_tight_dy = get_histogram(samples, "dy" , cuttight , var , dosf) # Get total ewk h_loose_ewk = p.get_total_hist([h_loose_top, h_loose_dy, h_loose_w]) h_tight_ewk = p.get_total_hist([h_tight_top, h_tight_dy, h_tight_w]) # Get data - ewk h_loose_data.Add(h_loose_ewk, -1) h_tight_data.Add(h_tight_ewk, -1) # Compute fakerate h_tight_data.Divide(h_loose_data) return h_tight_data
def hlt_prescale(): cut = "TwoMuHLT17" var = "Mll_Z" # Retrieve histograms h_cn_data = get_histogram(samples_cn, "data" , cut , var) h_cn_top = get_histogram(samples_cn, "top" , cut , var) h_cn_w = get_histogram(samples_cn, "w" , cut , var) h_cn_dy = get_histogram(samples_cn, "dy" , cut , var) h_cn_qcd = get_histogram(samples_cn, "qcd" , cut , var) b = p.get_total_hist([h_cn_top, h_cn_dy, h_cn_w]) b.Rebin(b.GetNbinsX()) h_cn_data.Rebin(h_cn_data.GetNbinsX()) b.Divide(h_cn_data) print cut, var, b.GetBinContent(1), b.GetBinError(1) cut = "TwoElHLT17" var = "Mll_Z" # Retrieve histograms h_cn_data = get_histogram(samples_cn, "data" , cut , var) h_cn_top = get_histogram(samples_cn, "top" , cut , var) h_cn_w = get_histogram(samples_cn, "w" , cut , var) h_cn_dy = get_histogram(samples_cn, "dy" , cut , var) h_cn_qcd = get_histogram(samples_cn, "qcd" , cut , var) b = p.get_total_hist([h_cn_top, h_cn_dy, h_cn_w]) b.Rebin(b.GetNbinsX()) h_cn_data.Rebin(h_cn_data.GetNbinsX()) b.Divide(h_cn_data) print cut, var, b.GetBinContent(1), b.GetBinError(1)
def get_datadriven_fakerate(channel, is3l, samples): cut = ("OneElLoose" if channel == "e" else "OneMuLoose") if not is3l else ("OneEl3lLoose" if channel == "e" else "OneMu3lLoose") histname = cut+"/lep_ptcorrcoarse_vs_etacoarse" dataname = "/data/{}{}".format(channel, channel) elsf = ewksf("OneElTightEWKCR", "e", samples) if not is3l else ewksf("OneEl3lTightEWKCR", "e", samples) musf = ewksf("OneMuTightEWKCR", "m", samples) if not is3l else ewksf("OneMu3lTightEWKCR", "m", samples) sf = -elsf if channel == "e" else -musf bgs = [ samples.getHistogram("/top", histname).Clone("Top"), samples.getHistogram("/Zonelep", histname).Clone("DY"), samples.getHistogram("/Wonelep", histname).Clone("W"), ] ddqcd = samples.getHistogram(dataname, histname).Clone("Data") totalbkg = p.get_total_hist(bgs) move_in_overflow(totalbkg) move_in_overflow(ddqcd) ddqcd.Add(totalbkg, sf) bgstight = [ samples.getHistogram("/top", str(histname).replace("Loose","Tight")).Clone("Top"), samples.getHistogram("/Zonelep", str(histname).replace("Loose","Tight")).Clone("DY"), samples.getHistogram("/Wonelep", str(histname).replace("Loose","Tight")).Clone("W"), ] ddqcdtight = samples.getHistogram(dataname, str(histname).replace("Loose","Tight")).Clone("Data") totalbkgtight = p.get_total_hist(bgstight) move_in_overflow(totalbkgtight) move_in_overflow(ddqcdtight) ddqcdtight.Add(totalbkgtight, sf) ddqcdtight.Divide(ddqcd) #compute_fake_factor(ddqcdtight) return ddqcdtight
def compute_nvtx_reweighting(): f = ROOT.TFile("nvtxreweight.root", "recreate") cut = "OneMuTightEWKCR3NoNvtxRewgt" var = "nvtx" # Retrieve histograms h_cn_data = get_histogram(samples_cn, "data" , cut , var) h_cn_top = get_histogram(samples_cn, "top" , cut , var) h_cn_w = get_histogram(samples_cn, "w" , cut , var) h_cn_dy = get_histogram(samples_cn, "dy" , cut , var) h_cn_qcd = get_histogram(samples_cn, "qcd" , cut , var) b = p.get_total_hist([h_cn_top, h_cn_dy, h_cn_w]) # Get total integral to get the total offset bi = b.Integral() di = h_cn_data.Integral() ratio = di / bi # Compute reweighting h_cn_data.Divide(b) h_cn_data.Scale(1./ratio) h_cn_data.SetName(cut+"_"+var) h_cn_data.SetTitle(cut+"_"+var) h_cn_data.Write() cut = "OneElTightEWKCR3NoNvtxRewgt" var = "nvtx" # Retrieve histograms h_cn_data = get_histogram(samples_cn, "data" , cut , var) h_cn_top = get_histogram(samples_cn, "top" , cut , var) h_cn_w = get_histogram(samples_cn, "w" , cut , var) h_cn_dy = get_histogram(samples_cn, "dy" , cut , var) h_cn_qcd = get_histogram(samples_cn, "qcd" , cut , var) b = p.get_total_hist([h_cn_top, h_cn_dy, h_cn_w]) # Get total integral to get the total offset bi = b.Integral() di = h_cn_data.Integral() ratio = di / bi # Compute reweighting h_cn_data.Divide(b) h_cn_data.Scale(1./ratio) h_cn_data.SetName(cut+"_"+var) h_cn_data.SetTitle(cut+"_"+var) h_cn_data.Write() f.Close()
def ewksf(cut, channel, samples): data = samples.getHistogram("/data/{}{}".format(channel, channel) , cut+"/MTOneLepFixed").Clone("Data") bgs = [] bgs.append(samples.getHistogram("/top" , cut+"/MTOneLepFixed").Clone("Top")) bgs.append(samples.getHistogram("/Zonelep" , cut+"/MTOneLepFixed").Clone("DY")) bgs.append(samples.getHistogram("/Wonelep" , cut+"/MTOneLepFixed").Clone("W")) totalbkg = p.get_total_hist(bgs) data.Rebin(4) totalbkg.Rebin(4) data.Divide(totalbkg) return data.GetBinContent(3)
def plot_stack(histname, output_name, systs=None, options={}, plotfunc=p.plot_hist): ismu = str(histname).find("Mu") != -1 is2d = str(histname).find("_vs_") != -1 # Options alloptions = { "ratio_range": [0.4, 1.6], #"nbins": 30, #"autobin": True, "legend_scalex": 1.0, "legend_scaley": 1.0, "legend_smart": True, "legend_alignment": "topleft", "output_name": "{}/{}_stack.pdf".format(output_plot_dir, output_name), "bkg_sort_method": "unsorted", "divide_by_bin_width": True if output_name.find("varbin") != -1 else False, "yaxis_log": True if output_name.find("varbin") != -1 else False, "yaxis_range": [1e3, 1e10] if output_name.find("varbin") != -1 else [], } alloptions.update(options) sigs = [] bgs = [ samples.getHistogram("/top", histname).Clone("Top") if not is2d else p.flatten_th2(samples.getHistogram("/top", histname).Clone("Top")), samples.getHistogram("/Zonelep", histname).Clone("DY") if not is2d else p.flatten_th2(samples.getHistogram("/Zonelep", histname).Clone("DY")), samples.getHistogram("/Wonelep", histname).Clone("W") if not is2d else p.flatten_th2(samples.getHistogram("/Wonelep", histname).Clone("W")), samples.getHistogram("/qcd/mu" if ismu else "/qcd/el", histname).Clone("QCD") if not is2d else p.flatten_th2( samples.getHistogram("/qcd/mu" if ismu else "/qcd/el", histname).Clone("QCD")), ] dataname = "/data" if histname.find("El") != -1: dataname = "/data/ee" if histname.find("Mu") != -1: dataname = "/data/mm" data = samples.getHistogram( dataname, histname).Clone("Data") if not is2d else p.flatten_th2( samples.getHistogram(dataname, histname).Clone("Data")) if histname.find("HLT") != -1: totalbkg = p.get_total_hist(bgs) ratio = samples.getHistogram( dataname, histname).Clone("Data") if not is2d else p.flatten_th2( samples.getHistogram(dataname, histname).Clone("Data")) ratio.Rebin(ratio.GetNbinsX()) totalbkg.Rebin(totalbkg.GetNbinsX()) totalbkg.Divide(ratio) print histname totalbkg.Print("all") if (histname.find("OneMuTightEWKCR3NoNvtxRewgt/nvtx") != -1 or histname.find("OneElTightEWKCR3NoNvtxRewgt/nvtx") != -1 ) and options["nbins"] != 5: totalbkg = p.get_total_hist(bgs[:-1]) nvtxweight = samples.getHistogram( dataname, histname).Clone(output_name) if not is2d else p.flatten_th2( samples.getHistogram(dataname, histname).Clone(output_name)) scale = nvtxweight.Integral() / totalbkg.Integral() nvtxweight.Divide(totalbkg) nvtxweight.Scale(1. / scale) #nvtxweight.Print("all") #f = ROOT.TFile("{}.root".format(output_name),"recreate") #nvtxweight.Write() #f.Close() if options["nbins"] == 5 and histname.find("TightEWKCR/MT") != -1: totalbkg = p.get_total_hist(bgs[:-1]) ratio = samples.getHistogram( dataname, histname).Clone("Data") if not is2d else p.flatten_th2( samples.getHistogram(dataname, histname).Clone("Data")) ratio.Rebin(4) totalbkg.Rebin(4) ratio.Divide(totalbkg) print "EWK NF", histname, ratio.GetBinContent(3), ratio.GetBinError(3) colors = [2005, 2003, 2001, 920] sf = musf if ismu else elsf if histname.find("MTOneLep") == -1: bgs[0].Scale(sf) bgs[1].Scale(sf) bgs[2].Scale(sf) if histname.find("HLT") != -1: bgs = bgs[:-1] plotfunc(sigs=sigs, bgs=bgs, data=data, colors=colors, syst=systs, options=alloptions)
def plot_datadriven_fakeratecomp(histname, output_name, systs=None, options={}, plotfunc=p.plot_hist): if str(histname).find("Loose") == -1: return ismu = str(histname).find("Mu") != -1 is2d = str(histname).find("_vs_") != -1 # Options alloptions = { "ratio_range": [0.0, 2.0], #"nbins": 30, "autobin": True, "legend_scalex": 1.0, "legend_scaley": 1.0, "legend_smart": True, "legend_alignment": "topleft", "legend_datalabel": "Data FR", "output_name": "{}/{}_fakeratecomp.pdf".format(output_plot_dir, output_name), "bkg_sort_method": "unsorted", "draw_points": True, } alloptions.update(options) sigs = [] # Loose bgs = [ samples.getHistogram("/top", histname).Clone("Top") if not is2d else p.flatten_th2(samples.getHistogram("/top", histname).Clone("Top")), samples.getHistogram("/Zonelep", histname).Clone("DY") if not is2d else p.flatten_th2(samples.getHistogram("/Zonelep", histname).Clone("DY")), samples.getHistogram("/Wonelep", histname).Clone("W") if not is2d else p.flatten_th2(samples.getHistogram("/Wonelep", histname).Clone("W")), #samples.getHistogram("/qcd/mu" if ismu else "/qcd/el", histname).Clone("QCD") if not is2d else p.flatten_th2(samples.getHistogram("/qcd/mu" if ismu else "/qcd/el", histname).Clone("QCD")), ] dataname = "/data" if histname.find("El") != -1: dataname = "/data/ee" if histname.find("Mu") != -1: dataname = "/data/mm" ddqcd = samples.getHistogram( dataname, histname).Clone("Data") if not is2d else p.flatten_th2( samples.getHistogram(dataname, histname).Clone("Data")) qcd = samples.getHistogram( "/qcd/mu" if ismu else "/qcd/el", histname).Clone("QCD") if not is2d else p.flatten_th2( samples.getHistogram("/qcd/mu" if ismu else "/qcd/el", histname).Clone("QCD")) totalbkg = p.get_total_hist(bgs) # tight bgstight = [ samples.getHistogram( "/top", str(histname).replace( "Loose", "Tight")).Clone("Top") if not is2d else p.flatten_th2( samples.getHistogram( "/top", str(histname).replace("Loose", "Tight")).Clone("Top")), samples.getHistogram( "/Zonelep", str(histname).replace( "Loose", "Tight")).Clone("DY") if not is2d else p.flatten_th2( samples.getHistogram( "/Zonelep", str(histname).replace("Loose", "Tight")).Clone("DY")), samples.getHistogram( "/Wonelep", str(histname).replace( "Loose", "Tight")).Clone("W") if not is2d else p.flatten_th2( samples.getHistogram( "/Wonelep", str(histname).replace("Loose", "Tight")).Clone("W")), #samples.getHistogram("/qcd/mu" if ismu else "/qcd/el", str(histname).replace("Loose","Tight")).Clone("QCD") if not is2d else p.flatten_th2(samples.getHistogram("/qcd/mu" if ismu else "/qcd/el", str(histname).replace("Loose","Tight")).Clone("QCD")), ] dataname = "/data" if str(histname).replace("Loose", "Tight").find("El") != -1: dataname = "/data/ee" if str(histname).replace("Loose", "Tight").find("Mu") != -1: dataname = "/data/mm" ddqcdtight = samples.getHistogram( dataname, str(histname).replace( "Loose", "Tight")).Clone("Data") if not is2d else p.flatten_th2( samples.getHistogram(dataname, str(histname).replace( "Loose", "Tight")).Clone("Data")) qcdtight = samples.getHistogram( "/qcd/mu" if ismu else "/qcd/el", str(histname).replace( "Loose", "Tight")).Clone("QCD") if not is2d else p.flatten_th2( samples.getHistogram("/qcd/mu" if ismu else "/qcd/el", str(histname).replace( "Loose", "Tight")).Clone("QCD")) totalbkgtight = p.get_total_hist(bgstight) # Get data fakerate p.remove_overflow(totalbkg) p.remove_overflow(totalbkgtight) p.remove_overflow(ddqcd) p.remove_overflow(ddqcdtight) sf = -musf if ismu else -elsf ddqcd.Add(totalbkg, sf) ddqcdtight.Add(totalbkgtight, sf) ddqcdtight.Divide(ddqcd) # Get QCD fakerate p.remove_overflow(qcd) p.remove_overflow(qcdtight) qcdtight.Divide(qcd) bgs = [qcdtight.Clone("QCD FR")] data = ddqcdtight # Compute data driven fake rate colors = [2] plotfunc(sigs=sigs, bgs=bgs, data=data, colors=colors, syst=systs, options=alloptions)
def plot_closure(cut, var, options={}, doprintclosure=False): # Sanity check that the name of the cut contains "Predict" # If not, then quietly exit the function without doing anything ispredict = cut.find("Predict") != -1 if not ispredict: print "ERROR - requested plot_closure on", cut, var, "but did not find Predict in name" return # Sanity check that the name of the cut contains "Loose" # If not, then quietly exit the function without doing anything isloose = cut.find("Loose") != -1 if not isloose: print "ERROR - requested plot_closure on", cut, var, "but did not find Loose in name" return # Setting variables to be used for option setting output_name = cut + "_" + var isvarbin = output_name.find("varbin") != -1 divide_by_bin_width = isvarbin yaxis_log = isvarbin yaxis_range = [1e3, 1e10] if isvarbin else [] # Options alloptions= { "ratio_range":[0.0,2.0], "nbins": 9, "legend_scalex": 1.4, "legend_scaley": 1.0, "legend_smart": True, "legend_alignment": "topleft", "output_name": "plots/{}_closure.pdf".format(output_name), "bkg_sort_method": "unsorted", "divide_by_bin_width": divide_by_bin_width, "yaxis_log": yaxis_log, "yaxis_range": yaxis_range, "legend_datalabel": "t#bar{t} + W prediction", "xaxis_ndivisions": 101, } # Whatever the option passed through the argument can override anything alloptions.update(options) # Colors of the MC histograms colors = [ 2005, 2001 ] # in order of top, Z, W, QCD # Retrieve histograms (loose means "estimation", i.e. apply the "tight" selection. I KNOW IT'S A TERRIBLE NAMING....) h_loose_cn_top = get_histogram(samples_cn, "top" , cut , var) h_loose_cn_w = get_histogram(samples_cn, "w" , cut , var) # Retrieve histograms (tight means "prediction", i.e. loose!tight * fake-factor) h_tight_cn_top = get_histogram(samples_cn, "top" , cut.replace("Loose", "Tight") , var) h_tight_cn_w = get_histogram(samples_cn, "w" , cut.replace("Loose", "Tight") , var) # Form the "prediction" which is the sum of 'tight' h_tight_cn_top.Add(h_tight_cn_w) # Name the histograms h_loose_cn_w.SetName("W estimation") h_loose_cn_top.SetName("t#bar{t} estimation") # Arrange the histograms sigs = [] bgs = [ h_loose_cn_top, h_loose_cn_w ] data = h_tight_cn_top # if do print closure then print the ratio if doprintclosure: prd = p.get_total_hist(bgs) est = h_tight_cn_top.Clone("estimate") prd_num = E(prd.GetBinContent(1), prd.GetBinError(1)) est_num = E(est.GetBinContent(1), est.GetBinError(1)) print est_num, prd_num, est_num / prd_num # Plot! p.plot_hist( sigs = sigs, bgs = bgs, data = data, colors = colors, options=alloptions)