def data_mc_ratio(ratio_groups, var, cut, **kwargs): ColorStyleGen.reset() samples_mc = ratio_groups.keys() ratio_groups["data"] = ["SingleMuAB", "SingleMuC", "SingleMuD"] plot_name = kwargs.get("name", "plot") samples_d = dict() for name, group in ratio_groups.items(): samples_d[name] = [ metadata_iso.get_histogram(sample_name, var, cut_str=cut.cut_str, weight=None) for sample_name in group ] if name != "data": for hist in samples_d[name]: hist.normalize_lumi(lumi_total) merged = merge_hists(samples_d[name], name) samples_d[name] = merged samples_d[name].pretty_name = name for name in samples_d.keys(): if name != "data": samples_d[name].hist.Divide(samples_d["data"].hist) samples_d["data"].hist.Divide(samples_d["data"].hist) canv = plot_hists(samples_d.values(), styles={"data": Styling.style["data"]}, **kwargs) leg = legend([r for r in reversed(samples_d.values())], styles=["p", "f"]) canv.SaveAs(plot_name + ".pdf") return canv, leg
def mc_amount(cut, weight, lumi, ref=None): histsD = dict() for samp in samples_mc: histsD[samp.name] = samp.drawHistogram("eta_lj", str(cut), weight=weight, plot_range=[100, -5, 5]) for name, hist in histsD.items(): hist.normalize_lumi(lumi) for name, hist in histsD.items(): histsD[name] = hist.hist merge_cmd = copy.deepcopy(merge_cmds) merge_cmd["QCD (MC)"] = ["QCDMu"] merge_cmd["t#bar{t} incl."] = ["TTJets_MassiveBinDECAY"] merge_cmd.pop("data") merged_hists = merge_hists(histsD, merge_cmd) interesting = [ "t#bar{t}", "t#bar{t} incl.", "W(#rightarrow l #nu) + jets", "QCD (MC)", "t-channel" ] for i in interesting: r = 0.0 r0 = 0.0 if ref and i in ref.keys(): r0 = ref[i] r = 100.0 * merged_hists[i].Integral() / float(r0) print "%s | %d | %d | %.2f %%" % (i, merged_hists[i].Integral(), r0, r)
def data_mc_ratio(ratio_groups, var, cut, **kwargs): ColorStyleGen.reset() samples_mc = ratio_groups.keys() ratio_groups["data"] = ["SingleMuAB", "SingleMuC", "SingleMuD"] plot_name = kwargs.get("name", "plot") samples_d = dict() for name, group in ratio_groups.items(): samples_d[name] = [ metadata_iso.get_histogram(sample_name, var, cut_str=cut.cut_str, weight=None) for sample_name in group ] if name != "data": for hist in samples_d[name]: hist.normalize_lumi(lumi_total) merged = merge_hists(samples_d[name], name) samples_d[name] = merged samples_d[name].pretty_name = name for name in samples_d.keys(): if name != "data": samples_d[name].hist.Divide(samples_d["data"].hist) samples_d["data"].hist.Divide(samples_d["data"].hist) canv = plot_hists( samples_d.values(), styles={"data": Styling.style["data"]}, **kwargs ) leg = legend([r for r in reversed(samples_d.values())], styles=["p", "f"]) canv.SaveAs(plot_name + ".pdf") return canv, leg
def plot_hists_stacked(hist_groups, **kwargs): import uuid stacks = dict() styles = kwargs.get("styles") draw_styles = kwargs.get("draw_styles", {}) do_log_y = kwargs.get("do_log_y", False) title = kwargs.get("title", "TITLE") x_label = kwargs.get("x_label", "XLABEL") name = kwargs.get("name", "NAME_%s" % uuid.uuid1()) min_bin = kwargs.get("min_bin", 10) canv = ROOT.TCanvas(name, name) canv.SetWindowSize(1000, 1000) canv.SetCanvasSize(1000, 1000) for name, group in hist_groups.items(): stacks[name] = ROOT.THStack(name, name) print name for hist in group: print hist.name if not styles: (color, style) = ColorStyleGen.next() hist.hist.SetLineColor(color) hist.hist.SetLineWidth(2) hist.hist.SetFillColor(color) hist.hist.SetFillStyle(style) else: styles[name](hist) stacks[name].Add(hist.hist) max_bin = get_max_bin(stacks.values()) for name, stack in stacks.items(): stack.Draw() canv.Draw() first = True for name, stack in stacks.items(): if name in draw_styles.keys(): drawcmd = draw_styles[name] else: drawcmd = "BAR HIST goff" if not first: drawcmd += " SAME" stack.Draw(drawcmd) if first: stack.SetMaximum(1.8 * max_bin) if do_log_y: stack.SetMinimum(min_bin) stack.SetTitle(title) stack.GetXaxis().SetTitle(x_label) first = False if do_log_y: canv.SetLogy() return canv, stacks
def plot_hists_stacked(hist_groups, **kwargs): import uuid stacks = dict() styles = kwargs.get("styles") draw_styles = kwargs.get("draw_styles", {}) do_log_y = kwargs.get("do_log_y", False) title = kwargs.get("title", "TITLE") x_label = kwargs.get("x_label", "XLABEL") name = kwargs.get("name", "NAME_%s" % uuid.uuid1()) min_bin = kwargs.get("min_bin", 10) canv = ROOT.TCanvas(name, name) canv.SetWindowSize(1000,1000) canv.SetCanvasSize(1000,1000) for name, group in hist_groups.items(): stacks[name] = ROOT.THStack(name, name) print name for hist in group: print hist.name if not styles: (color, style) = ColorStyleGen.next() hist.hist.SetLineColor(color) hist.hist.SetLineWidth(2) hist.hist.SetFillColor(color) hist.hist.SetFillStyle(style) else: styles[name](hist) stacks[name].Add(hist.hist) max_bin = get_max_bin(stacks.values()) for name, stack in stacks.items(): stack.Draw() canv.Draw() first = True for name, stack in stacks.items(): if name in draw_styles.keys(): drawcmd = draw_styles[name] else: drawcmd = "BAR HIST goff" if not first: drawcmd += " SAME" stack.Draw(drawcmd) if first: stack.SetMaximum(1.8*max_bin) if do_log_y: stack.SetMinimum(min_bin) stack.SetTitle(title) stack.GetXaxis().SetTitle(x_label) first = False if do_log_y: canv.SetLogy() return canv, stacks
def merge_hists_g(hists_d, merge_groups=g_merge_cmd): out_d = dict() for merge_name, items in merge_groups.items(): hist = hists_d[items[0]].hist.Clone() for item in items[1:]: hist.Add(hists_d[item].hist) out_d[merge_name] = Histogram() out_d[merge_name].setHist(hist, sample_name=hists_d[items[0]].sample_name, var=hists_d[items[0]].var, cut=hists_d[items[0]].cut) integral, err = out_d[merge_name].calc_int_err() out_d[merge_name].pretty_name = "%s : %.0f #pm %0.f" % (merge_name, integral, err) return out_d
def mc_amount(cut, weight, lumi, ref=None): histsD = dict() for samp in samples_mc: histsD[samp.name] = samp.drawHistogram("eta_lj", str(cut), weight=weight, plot_range=[100,-5,5]) for name, hist in histsD.items(): hist.normalize_lumi(lumi) for name, hist in histsD.items(): histsD[name] = hist.hist merge_cmd = copy.deepcopy(merge_cmds) merge_cmd["QCD (MC)"] = ["QCDMu"] merge_cmd["t#bar{t} incl."] = ["TTJets_MassiveBinDECAY"] merge_cmd.pop("data") merged_hists = merge_hists(histsD, merge_cmd) interesting = ["t#bar{t}", "t#bar{t} incl.", "W(#rightarrow l #nu) + jets", "QCD (MC)", "t-channel"] for i in interesting: r = 0.0 r0 = 0.0 if ref and i in ref.keys(): r0 = ref[i] r = 100.0*merged_hists[i].Integral() / float(r0) print "%s | %d | %d | %.2f %%" % (i, merged_hists[i].Integral(), r0, r)
n_true.Scale(1.0/sum(xs_s)) n_tagged.Scale(1.0/sum(xs_s)) #print "int_true=%.2f, int_tagged=%.2f" % (n_true.Integral(), n_tagged.Integral()) div = n_tagged.Clone("eff_%s" % flavour) div.Divide(n_true) #print "div=%.2f" % div.Integral() return div if __name__=="__main__": from glob import glob from common.utils import get_sample_dict import os import sys samples_d = dict() samples_d["ttbar"] = ["TTJets_FullLept", "TTJets_SemiLept"] samples_d["wjets"] = ["W1Jets_exclusive", "W2Jets_exclusive", "W3Jets_exclusive", "W4Jets_exclusive"] samples_d["t-channel"] = ["T_t_ToLeptons", "Tbar_t_ToLeptons"] sample_name = sys.argv[1] out_dir = "b_eff_plots/" import os if not os.path.exists(out_dir): os.mkdir(out_dir) axis = sys.argv[2] if axis == "x": xlab = "p_{T,q}" elif axis == "y": xlab = "|#eta_{q}|" else:
interesting = ["t#bar{t}", "t#bar{t} incl.", "W(#rightarrow l #nu) + jets", "QCD (MC)", "t-channel"] for i in interesting: r = 0.0 r0 = 0.0 if ref and i in ref.keys(): r0 = ref[i] r = 100.0*merged_hists[i].Integral() / float(r0) print "%s | %d | %d | %.2f %%" % (i, merged_hists[i].Integral(), r0, r) weight = "pu_weight*muon_IDWeight*muon_IsoWeight" doCutFlow = True if doCutFlow: print "1 iso mu, 0 veto mu/ele" ref = dict() ref["t#bar{t}"] = 261429 ref["t#bar{t} incl."] = ref["t#bar{t}"] ref["W(#rightarrow l #nu) + jets"] = 47758738 ref["QCD (MC)"] = 1873957 ref["t-channel"] = 54769 previous = Cuts.hlt_isomu * Cuts.one_muon * Cuts.lepton_veto mc_amount(previous, weight, 12210, ref=ref) print print "2 jets" ref = dict() ref["t#bar{t}"] = 78201 ref["t#bar{t} incl."] = ref["t#bar{t}"] ref["W(#rightarrow l #nu) + jets"] = 921051 ref["QCD (MC)"] = 67463
def stack_plot(var, cut, weight=None, **kwargs): qcd_weight = kwargs.get("qcd_weight", None) skip_samples = kwargs.get("skip_samples", []) doDataDrivenQCD = kwargs.get("doDataDrivenQCD", True) hists_mc = [ metadata_iso.get_histogram(sample_name, var, cut_str=cut.cut_str, weight=weight) for sample_name in mc_sample_names ] hists_qcd_iso = [ metadata_iso.get_histogram(sample_name, var, cut_str=cut.cut_str, weight=qcd_weight) for sample_name in qcd_sample_names ] if doDataDrivenQCD: hists_data_antiiso = [ metadata_antiiso.get_histogram(sample_name, var, cut_str=cut.cut_str, weight=None) for sample_name in data_sample_names ] hists_d = dict() for hist in hists_mc: hists_d[hist.sample_name] = hist if not doDataDrivenQCD: for hist in hists_qcd_iso: hists_d[hist.sample_name] = hist # for hist in hists_mc: # if hist.sample_name in mc_sample_titles.keys(): # hist.pretty_name = mc_sample_titles[hist.sample_name] # else: # hist.pretty_name = hist.sample_name hists_data = [ metadata_iso.get_histogram(sample_name, var, cut_str=cut.cut_str, weight=None) for sample_name in data_sample_names ] for hist in hists_data: hists_d[hist.sample_name] = hist for hist in hists_data: hist.pretty_name = "SingleMu" stack_group = dict() stack_group["mc"] = hists_mc #stack_group["data"] = hists_data stack_group["data"] = [hists_data[0]] for hist in stack_group["mc"]: hist.normalize_lumi(lumi_total) for hist in hists_qcd_iso: hist.normalize_lumi(lumi_total) merge_cmd = dict() merge_cmd["data"] = ["SingleMuAB", "SingleMuC", "SingleMuD"] merge_cmd["diboson"] = ["WW", "WZ", "ZZ"] merge_cmd["DY-jets"] = ["DYJets"] merge_cmd["s-channel"] = ["T_s", "Tbar_s"] merge_cmd["tW-channel"] = ["T_tW", "Tbar_tW"] merge_cmd["t#bar{t}"] = ["TTJets_FullLept", "TTJets_SemiLept"] merge_cmd["W+jets"] = [ "W1Jets_exclusive", "W2Jets_exclusive", "W3Jets_exclusive", "W4Jets_exclusive" ] #merge_cmd["W+jets"] = ["WJets_inclusive"] merge_cmd["t-channel"] = ["T_t", "Tbar_t"] if not doDataDrivenQCD: merge_cmd["QCD (MC)"] = ["QCDMu"] for sample in skip_samples: if sample in merge_cmd.keys(): merge_cmd.pop(sample) merged_hists = merge_hists_g(hists_d, merge_cmd) if doDataDrivenQCD: hist_data_antiiso = merge_hists(hists_data_antiiso, "data_antiiso") hist_qcd_iso = merge_hists(hists_qcd_iso, "qcd_iso") hist_data_antiiso.normalize(hist_qcd_iso.hist.Integral()) print hist_data_antiiso.hist.Integral() hist_data_antiiso.sample_name = "QCDMu" hist_data_antiiso.pretty_name = "QCD (dd.)" #for hist in hists_data[1:]: # stack_group["data"][0].hist.Add(hist.hist) stack_d = dict() if doDataDrivenQCD: stack_d["mc"] = [hist_data_antiiso] else: stack_d["mc"] = [] stack_d["mc"] += [ merged_hists[name] for name in merged_hists.keys() if name != "data" ] stack_d["data"] = [merged_hists["data"]] canv, stacks = plot_hists_stacked(stack_d, styles=Styling.style, draw_styles={"data": "E1"}, **kwargs) #canvas_margin(canv, side="R", margin=0.3) # leg_hists = ([hists_data[0]] + # [hists_d["T_t"]] + # [hists_d["TTJets_FullLept"]] + # [hists_d["W1Jets_exclusive"]] + # [hists_d["T_tW"]] + # [hists_d["T_s"]] + # [hists_d["WW"]] + # [hists_d["QCDMu"]]) stack_d["mc"].reverse() leg = legend(stack_d["data"] + stack_d["mc"], styles=["p", "f"]) text = lumi_textbox(lumi=lumi_total) canv.SaveAs(canv.GetName() + ".pdf")
def lumi_textbox(lumi=10.435, pos="top-center"): if pos == "top-center": coords = [0.25, 0.73, 0.71, 0.88] text = ROOT.TPaveText(coords[0], coords[1], coords[2], coords[3], "NDC") text.AddText("CMS preliminary #sqrt{s} = 8 TeV, #int L dt = %.1f fb^{-1}" % (float(lumi) / 1000.0)) text.SetShadowColor(ROOT.kWhite) text.SetLineColor(ROOT.kWhite) text.SetFillColor(ROOT.kWhite) text.Draw() return text g_merge_cmd = dict() g_merge_cmd["data"] = ["SingleMuAB", "SingleMuC", "SingleMuD"] g_merge_cmd["diboson"] = ["WW", "WZ", "ZZ"] g_merge_cmd["DY-jets"] = ["DYJets"] g_merge_cmd["s-channel"] = ["T_s", "Tbar_s"] g_merge_cmd["tW-channel"] = ["T_tW", "Tbar_tW"] g_merge_cmd["t#bar{t}"] = ["TTJets_FullLept", "TTJets_SemiLept"] g_merge_cmd["W+jets"] = [ "W1Jets_exclusive", "W2Jets_exclusive", "W3Jets_exclusive", "W4Jets_exclusive" ] #g_merge_cmd["W+jets"] = ["WJets_inclusive"] g_merge_cmd["t-channel"] = ["T_t", "Tbar_t"] def merge_hists_g(hists_d, merge_groups=g_merge_cmd):
def stack_plot(var, cut, weight=None, **kwargs): qcd_weight = kwargs.get("qcd_weight", None) skip_samples = kwargs.get("skip_samples", []) doDataDrivenQCD = kwargs.get("doDataDrivenQCD", True) hists_mc = [metadata_iso.get_histogram(sample_name, var, cut_str=cut.cut_str, weight=weight) for sample_name in mc_sample_names] hists_qcd_iso = [metadata_iso.get_histogram(sample_name, var, cut_str=cut.cut_str, weight=qcd_weight) for sample_name in qcd_sample_names] if doDataDrivenQCD: hists_data_antiiso = [metadata_antiiso.get_histogram(sample_name, var, cut_str=cut.cut_str, weight=None) for sample_name in data_sample_names] hists_d = dict() for hist in hists_mc: hists_d[hist.sample_name] = hist if not doDataDrivenQCD: for hist in hists_qcd_iso: hists_d[hist.sample_name] = hist # for hist in hists_mc: # if hist.sample_name in mc_sample_titles.keys(): # hist.pretty_name = mc_sample_titles[hist.sample_name] # else: # hist.pretty_name = hist.sample_name hists_data = [metadata_iso.get_histogram(sample_name, var, cut_str=cut.cut_str, weight=None) for sample_name in data_sample_names] for hist in hists_data: hists_d[hist.sample_name] = hist for hist in hists_data: hist.pretty_name = "SingleMu" stack_group = dict() stack_group["mc"] = hists_mc #stack_group["data"] = hists_data stack_group["data"] = [hists_data[0]] for hist in stack_group["mc"]: hist.normalize_lumi(lumi_total) for hist in hists_qcd_iso: hist.normalize_lumi(lumi_total) merge_cmd = dict() merge_cmd["data"] = ["SingleMuAB", "SingleMuC", "SingleMuD"] merge_cmd["diboson"] = ["WW", "WZ", "ZZ"] merge_cmd["DY-jets"] = ["DYJets"] merge_cmd["s-channel"] = ["T_s", "Tbar_s"] merge_cmd["tW-channel"] = ["T_tW", "Tbar_tW"] merge_cmd["t#bar{t}"] = ["TTJets_FullLept", "TTJets_SemiLept"] merge_cmd["W+jets"] = ["W1Jets_exclusive", "W2Jets_exclusive", "W3Jets_exclusive", "W4Jets_exclusive"] #merge_cmd["W+jets"] = ["WJets_inclusive"] merge_cmd["t-channel"] = ["T_t", "Tbar_t"] if not doDataDrivenQCD: merge_cmd["QCD (MC)"] = ["QCDMu"] for sample in skip_samples: if sample in merge_cmd.keys(): merge_cmd.pop(sample) merged_hists = merge_hists_g(hists_d, merge_cmd) if doDataDrivenQCD: hist_data_antiiso = merge_hists(hists_data_antiiso, "data_antiiso") hist_qcd_iso = merge_hists(hists_qcd_iso, "qcd_iso") hist_data_antiiso.normalize(hist_qcd_iso.hist.Integral()) print hist_data_antiiso.hist.Integral() hist_data_antiiso.sample_name = "QCDMu" hist_data_antiiso.pretty_name = "QCD (dd.)" #for hist in hists_data[1:]: # stack_group["data"][0].hist.Add(hist.hist) stack_d = dict() if doDataDrivenQCD: stack_d["mc"] = [hist_data_antiiso] else: stack_d["mc"] = [] stack_d["mc"] += [merged_hists[name] for name in merged_hists.keys() if name!="data"] stack_d["data"] = [merged_hists["data"]] canv, stacks = plot_hists_stacked( stack_d, styles=Styling.style, draw_styles={"data": "E1"}, **kwargs ) #canvas_margin(canv, side="R", margin=0.3) # leg_hists = ([hists_data[0]] + # [hists_d["T_t"]] + # [hists_d["TTJets_FullLept"]] + # [hists_d["W1Jets_exclusive"]] + # [hists_d["T_tW"]] + # [hists_d["T_s"]] + # [hists_d["WW"]] + # [hists_d["QCDMu"]]) stack_d["mc"].reverse() leg = legend(stack_d["data"] + stack_d["mc"], styles=["p", "f"]) text = lumi_textbox(lumi=lumi_total) canv.SaveAs(canv.GetName() + ".pdf")
if do_log_y: canv.SetLogy() return canv, stacks def lumi_textbox(lumi=10.435, pos="top-center"): if pos=="top-center": coords = [0.25, 0.73, 0.71, 0.88] text = ROOT.TPaveText(coords[0], coords[1], coords[2], coords[3], "NDC") text.AddText("CMS preliminary #sqrt{s} = 8 TeV, #int L dt = %.1f fb^{-1}" % (float(lumi)/1000.0)) text.SetShadowColor(ROOT.kWhite) text.SetLineColor(ROOT.kWhite) text.SetFillColor(ROOT.kWhite) text.Draw() return text g_merge_cmd = dict() g_merge_cmd["data"] = ["SingleMuAB", "SingleMuC", "SingleMuD"] g_merge_cmd["diboson"] = ["WW", "WZ", "ZZ"] g_merge_cmd["DY-jets"] = ["DYJets"] g_merge_cmd["s-channel"] = ["T_s", "Tbar_s"] g_merge_cmd["tW-channel"] = ["T_tW", "Tbar_tW"] g_merge_cmd["t#bar{t}"] = ["TTJets_FullLept", "TTJets_SemiLept"] g_merge_cmd["W+jets"] = ["W1Jets_exclusive", "W2Jets_exclusive", "W3Jets_exclusive", "W4Jets_exclusive"] #g_merge_cmd["W+jets"] = ["WJets_inclusive"] g_merge_cmd["t-channel"] = ["T_t", "Tbar_t"] def merge_hists_g(hists_d, merge_groups=g_merge_cmd): out_d = dict() for merge_name, items in merge_groups.items(): hist = hists_d[items[0]].hist.Clone()
] for i in interesting: r = 0.0 r0 = 0.0 if ref and i in ref.keys(): r0 = ref[i] r = 100.0 * merged_hists[i].Integral() / float(r0) print "%s | %d | %d | %.2f %%" % (i, merged_hists[i].Integral(), r0, r) weight = "pu_weight*muon_IDWeight*muon_IsoWeight" doCutFlow = True if doCutFlow: print "1 iso mu, 0 veto mu/ele" ref = dict() ref["t#bar{t}"] = 261429 ref["t#bar{t} incl."] = ref["t#bar{t}"] ref["W(#rightarrow l #nu) + jets"] = 47758738 ref["QCD (MC)"] = 1873957 ref["t-channel"] = 54769 previous = Cuts.hlt_isomu * Cuts.one_muon * Cuts.lepton_veto mc_amount(previous, weight, 12210, ref=ref) print print "2 jets" ref = dict() ref["t#bar{t}"] = 78201 ref["t#bar{t} incl."] = ref["t#bar{t}"] ref["W(#rightarrow l #nu) + jets"] = 921051 ref["QCD (MC)"] = 67463