Пример #1
0
def sum_over_inds(tobj, template, bins):

    final = None
    for b in bins:
        if final is None:
            final = cu.get_from_tfile(tobj, template.format(b)).Clone(
                template.format("all"))
        else:
            final.Add(cu.get_from_tfile(tobj, template.format(b)))
    return final
Пример #2
0
def do_data_mc_plot(dirname, histname, output_filename, **plot_kwargs):
    data_file = cu.open_root_file(os.path.join(dirname, qgc.JETHT_ZB_FILENAME))
    qcd_file = cu.open_root_file(os.path.join(dirname, qgc.QCD_FILENAME))
    qcd_py_file = cu.open_root_file(
        os.path.join(dirname, qgc.QCD_PYTHIA_ONLY_FILENAME))
    qcd_hpp_file = cu.open_root_file(
        os.path.join(dirname, qgc.QCD_HERWIG_FILENAME))

    data_hist = cu.get_from_tfile(data_file, histname)
    qcd_hist = cu.get_from_tfile(qcd_file, histname)
    qcd_py_hist = cu.get_from_tfile(qcd_py_file, histname)
    qcd_hpp_hist = cu.get_from_tfile(qcd_hpp_file, histname)
    conts = [
        Contribution(data_hist,
                     label="Data",
                     line_color=ROOT.kBlack,
                     marker_size=0,
                     marker_color=ROOT.kBlack),
        Contribution(qcd_hist,
                     label="QCD MG+PYTHIA8 MC",
                     line_color=qgc.QCD_COLOUR,
                     subplot=data_hist,
                     marker_size=0,
                     marker_color=qgc.QCD_COLOUR),
        Contribution(qcd_py_hist,
                     label="QCD PYTHIA8 MC",
                     line_color=qgc.QCD_COLOURS[2],
                     subplot=data_hist,
                     marker_size=0,
                     marker_color=qgc.QCD_COLOURS[2]),
        # Contribution(qcd_hpp_hist, label="QCD HERWIG++ MC", line_color=qgc.HERWIGPP_QCD_COLOUR, subplot=data_hist, marker_size=0, marker_color=qgc.HERWIGPP_QCD_COLOUR),
    ]
    plot = Plot(conts,
                what='hist',
                ytitle="N",
                xtitle="p_{T}^{Leading jet} [GeV]",
                subplot_type="ratio",
                subplot_title="Simulation / data",
                ylim=[1E3, None],
                lumi=cu.get_lumi_str(do_dijet=True, do_zpj=False),
                **plot_kwargs)
    plot.y_padding_max_log = 500

    plot.legend.SetX1(0.55)
    plot.legend.SetX2(0.98)
    plot.legend.SetY1(0.7)
    # plot.legend.SetY2(0.88)
    plot.plot("NOSTACK HIST E")
    plot.set_logx(do_more_labels=True, do_exponent=False)
    plot.set_logy(do_more_labels=False)

    plot.save(output_filename)
def do_weight_vs_var_plot_per_pt(histname, input_filename, output_filename):
    ROOT.gStyle.SetPalette(palette_2D)
    tf = cu.open_root_file(input_filename)
    h3d = cu.get_from_tfile(tf, histname)
    if h3d.GetEntries() == 0:
        return

    canv = ROOT.TCanvas(cu.get_unique_str(), "", 800, 600)
    canv.SetTicks(1, 1)
    canv.SetLeftMargin(0.1)
    canv.SetRightMargin(0.15)
    canv.SetLogz()

    weight_str = "(unweighted)" if "unweighted" in histname else "(weighted)"
    for ibin in range(1, h3d.GetNbinsY()+1):
        h3d.GetYaxis().SetRange(ibin, ibin+1)
        h2d = h3d.Project3D("xz")
        if h2d.GetEntries() == 0:
            continue
        pt_low = h3d.GetYaxis().GetBinLowEdge(ibin)
        pt_high = h3d.GetYaxis().GetBinLowEdge(ibin+1)
        jet_str = pt_genjet_str if "_vs_pt_genjet_vs_" in histname else pt_str
        h2d.SetTitle("%g < %s < %g GeV %s" % (pt_low, jet_str, pt_high, weight_str))
        h2d.GetXaxis().SetTitle(get_var_str(histname))
        h2d.Draw("COLZ")
        jet_app = "_genjet" if "_vs_pt_genjet_vs" in histname else ""
        this_output_filename = output_filename.replace(".pdf", "_pt%s%gto%g.pdf" % (jet_app, pt_low, pt_high))
        canv.SaveAs(this_output_filename)
        canv.Clear()

    tf.Close()
Пример #4
0
def do_mc_pt_comparison_plot(dirname_label_pairs, output_filename,
                             qcd_filename, **plot_kwargs):
    # qcd_files = [cu.open_root_file(os.path.join(dl[0], qgc.QCD_FILENAME)) for dl in dirname_label_pairs]
    qcd_files = [
        cu.open_root_file(os.path.join(dl[0], qgc.QCD_PYTHIA_ONLY_FILENAME))
        for dl in dirname_label_pairs
    ]
    histname = "Dijet_tighter/pt_jet1"
    qcd_hists = [cu.get_from_tfile(qf, histname) for qf in qcd_files]
    N = len(dirname_label_pairs)
    conts = [
        Contribution(qcd_hists[i],
                     label=lab,
                     marker_color=cu.get_colour_seq(i, N),
                     line_color=cu.get_colour_seq(i, N),
                     line_style=(i % 3) + 1,
                     line_width=2,
                     rebin_hist=1,
                     subplot=qcd_hists[0] if i != 0 else None)
        for i, (d, lab) in enumerate(dirname_label_pairs)
    ]
    plot = Plot(conts,
                what='hist',
                ytitle="N",
                subplot_limits=(0.5, 1.5),
                subplot_type="ratio",
                subplot_title="* / %s" % (dirname_label_pairs[0][1]),
                **plot_kwargs)
    plot.y_padding_max_log = 500
    plot.legend.SetY1(0.7)
    plot.plot("NOSTACK HIST E")
    plot.set_logx(do_more_labels=False)
    plot.set_logy(do_more_labels=False)

    plot.save(output_filename)
def do_dijet_gen_distributions(root_dir):
    """Do plots comparing different different inputs in dijet region"""
    root_files = [
        qgc.QCD_FILENAME, qgc.QCD_PYTHIA_ONLY_FILENAME, qgc.QCD_HERWIG_FILENAME
    ][:]
    root_files = [
        cu.open_root_file(os.path.join(root_dir, r)) for r in root_files
    ]

    directories = [
        cu.get_from_tfile(rf, "Dijet_tighter") for rf in root_files[:]
    ]
    mc_col = qgc.QCD_COLOUR
    mc_col2 = qgc.QCD_COLOURS[2]
    mc_col3 = qgc.QCD_COLOURS[3]
    msize = 1
    lw = 2
    csd = [
        {
            "label": "QCD MC [MG+PY8]",
            "line_color": mc_col,
            "fill_color": mc_col,
            "marker_color": mc_col,
            "marker_style": 22,
            "fill_style": 0,
            "marker_size": msize,
            'line_width': lw
        },
        {
            "label": "QCD MC [PY8]",
            "line_color": mc_col2,
            "fill_color": mc_col2,
            "marker_color": mc_col2,
            "marker_style": 21,
            "fill_style": 0,
            "marker_size": msize,
            'line_width': lw
        },
        {
            "label": "QCD MC [H++]",
            "line_color": mc_col3,
            "fill_color": mc_col3,
            "marker_color": mc_col3,
            "marker_style": 23,
            "fill_style": 0,
            "marker_size": msize,
            'line_width': lw
        },
    ]
    jet_config_str = qgc.extract_jet_config(root_dir)

    # Compare shapes
    do_all_1D_plots_in_dir(directories=directories,
                           output_dir=os.path.join(
                               root_dir,
                               "Dijet_gen_kin_comparison_normalised"),
                           components_styles_dicts=csd,
                           jet_config_str=jet_config_str,
                           normalise_hists=True)
def do_jet_pt_rel_error_with_var_cuts(histname, cuts, input_filename, output_filename):
    ROOT.gStyle.SetPalette(palette_1D)
    tf = cu.open_root_file(input_filename)
    h3d = cu.get_from_tfile(tf, histname)
    if h3d.GetEntries() == 0:
        return
    pt_hists = []
    for cut in cuts:
        max_bin = h3d.GetZaxis().FindFixBin(cut)
        # print("cut:", cut, "bin:", max_bin)
        h = h3d.ProjectionY("pt_var_lt_%g" % cut, 0, -1, 0, max_bin, "e")
        h2 = h.Clone()
        h2.Rebin(2)
        if h.GetEntries() > 0:
            h3 = qgp.hist_divide_bin_width(h2)
        # convert bin contents to bin error/bin contents
        for ibin in range(1, h2.GetNbinsX()+1):
            if h3.GetBinContent(ibin) == 0:
                continue
            h3.SetBinContent(ibin, h3.GetBinError(ibin) / h3.GetBinContent(ibin))
            h3.SetBinError(ibin, 0)
        pt_hists.append(h3)

    line_styles = [1, 2, 3]
    n_line_styles = len(line_styles)
    conts = [Contribution(h, label=" < %g" % cut,
                          line_color=cu.get_colour_seq(ind, len(cuts)),
                          line_style=line_styles[ind % n_line_styles],
                          line_width=2,
                          marker_color=cu.get_colour_seq(ind, len(cuts)),
                          subplot=pt_hists[-1])
             for ind, (h, cut) in enumerate(zip(pt_hists, cuts))]

    jet_str = pt_genjet_str if "_vs_pt_genjet_vs_" in histname else pt_str
    weight_str = "(unweighted)" if "unweighted" in histname else "(weighted)"
    ratio_lims = (0.98, 1.02) if "unweighted" in histname else None
    plot = Plot(conts, what='hist',
                title='%s for cuts on %s %s' % (jet_str, get_var_str(histname), weight_str),
                xtitle=None,
                ytitle='Relative error',
                # xlim=None, ylim=None,
                legend=True,
                subplot_type='ratio',
                subplot_title='* / var < %g' % cuts[-1],
                subplot_limits=ratio_lims,
                has_data=False)
    plot.y_padding_max_log = 200
    plot.subplot_maximum_ceil = 2
    plot.subplot_maximum_floor = 1.02
    plot.subplot_minimum_ceil = 0.98
    plot.legend.SetY1(0.7)
    plot.legend.SetY2(0.89)
    plot.legend.SetX1(0.78)
    plot.legend.SetX2(0.88)
    plot.plot("NOSTACK HISTE", "NOSTACK HIST")
    plot.set_logx(True, do_more_labels=True)
    plot.set_logy(True, do_more_labels=False)
    plot.save(output_filename)
Пример #7
0
def do_jet_pt_vs_genht_plot(dirname, output_filename, title=""):
    """2D heat map of genHT vs jet pt"""
    canv = ROOT.TCanvas(cu.get_unique_str(), "", 800, 600)
    qcd_file = cu.open_root_file(os.path.join(dirname, qgc.QCD_FILENAME))
    histname = "Dijet_tighter/pt_jet_vs_genHT"
    canv.SetRightMargin(0.15)
    h = cu.get_from_tfile(qcd_file, histname)
    h.SetTitle(title + ";p_{T}^{Leading jet} [GeV]; H_{T}^{Gen} [GeV]")
    h.Draw("COLZ")
    h.GetXaxis().SetRangeUser(0, 200)
    h.GetYaxis().SetRangeUser(0, 200)
    canv.SaveAs(output_filename)
Пример #8
0
def grab_obj(file_name, obj_name):
    """Get object names obj_name from ROOT file file_name"""
    # TODO: checks!
    input_file = cu.open_root_file(file_name)
    obj = cu.get_from_tfile(input_file, obj_name)
    # print("Getting", obj_name, "from", file_name)
    if isinstance(obj, (ROOT.TH1, ROOT.TGraph)):
        obj.SetDirectory(0)  # Ownership kludge
        input_file.Close()
        return obj.Clone(ROOT.TUUID().AsString())
    else:
        return obj
Пример #9
0
def do_genht_plot(dirname, output_filename, **plot_kwargs):
    qcd_file = cu.open_root_file(os.path.join(dirname, qgc.QCD_FILENAME))
    histname = "Dijet_gen/gen_ht"
    qcd_hist = cu.get_from_tfile(qcd_file, histname)
    conts = [Contribution(qcd_hist, label="QCD MC", line_color=ROOT.kRed)]
    plot = Plot(conts, what='hist', ytitle="N", **plot_kwargs)
    plot.y_padding_max_log = 500
    plot.legend.SetY1(0.7)
    plot.plot("NOSTACK HIST E")
    plot.set_logx(do_more_labels=False)
    plot.set_logy(do_more_labels=False)

    plot.save(output_filename)
Пример #10
0
def grab_obj(file_name, obj_name):
    """Get object names obj_name from ROOT file file_name"""
    # TODO: checks!
    input_file = cu.open_root_file(file_name)
    obj = cu.get_from_tfile(input_file, obj_name)
    # print("Getting", obj_name, "from", file_name)
    if isinstance(obj, (ROOT.TH1, ROOT.TGraph, ROOT.TH2)):
        # THIS ORDER IS VERY IMPORTANT TO AVOID MEMORY LEAKS
        new_obj = obj.Clone(ROOT.TUUID().AsString())
        new_obj.SetDirectory(0)
        input_file.Close()
        return new_obj
    else:
        return obj
Пример #11
0
def do_plot(entries, output_file, hist_name=None, xlim=None, ylim=None, rebin=2, is_data=True, is_ak8=False):
    components = []
    do_unweighted = any(["unweighted" in e.get('hist_name', hist_name) for e in entries])
    for ent in entries:
        if 'tfile' not in ent:
            ent['tfile'] = cu.open_root_file(ent['filename'])
        ent['hist'] = cu.get_from_tfile(ent['tfile'], ent.get('hist_name', hist_name))
        if not do_unweighted and 'scale' in ent:
            ent['hist'].Scale(ent.get('scale', 1))
        components.append(
            Contribution(ent['hist'],
                         fill_color=ent['color'],
                         line_color=ent['color'],
                         marker_color=ent['color'],
                         marker_size=0,
                         line_width=2,
                         label=ent['label'],
                         rebin_hist=rebin
                        )
        )
        # print stats
        print(ent['hist_name'], ent['label'], ent['hist'].Integral())
    title = 'AK8 PUPPI' if is_ak8 else 'AK4 PUPPI'
    plot = Plot(components,
                what='hist',
                has_data=is_data,
                title=title,
                xlim=xlim,
                ylim=ylim,
                xtitle="p_{T}^{jet 1} [GeV]",
                ytitle="Unweighted N" if do_unweighted else 'N')
    # plot.y_padding_min_log = 10 if 'unweighted' in hist_name else 10
    plot.default_canvas_size = (700, 600)
    plot.legend.SetNColumns(2)
    plot.legend.SetX1(0.55)
    plot.legend.SetY1(0.7)
    plot.legend.SetY2(0.88)
    plot.plot("HISTE")
    plot.set_logx()
    plot.set_logy(do_more_labels=False)
    plot.save(output_file)

    # do non-stacked version
    stem, ext = os.path.splitext(output_file)
    plot.plot("HISTE NOSTACK")
    plot.set_logx()
    plot.set_logy(do_more_labels=False)
    plot.save(stem+"_nostack" + ext)
def do_weight_vs_pt_plot(input_filename, output_filename):
    ROOT.gStyle.SetPalette(palette_2D)
    histname = "Weight_Presel/weight_vs_pt_vs_pt_jet_qScale_ratio"
    tf = cu.open_root_file(input_filename)
    h3d = cu.get_from_tfile(tf, histname)
    if h3d.GetEntries() == 0:
        return
    h2d = h3d.Project3D("xy")

    canv = ROOT.TCanvas(cu.get_unique_str(), "", 800, 600)
    canv.SetTicks(1, 1)
    canv.SetLeftMargin(0.1)
    canv.SetRightMargin(0.15)
    canv.SetLogz()
    canv.SetLogx()
    h2d.Draw("COLZ")
    canv.SaveAs(output_filename)
    tf.Close()
def do_jet_index_plots(tdir, plot_dir):
    """Do 2D plots of genjet index vs recojet index"""
    plot_dir = os.path.join(plot_dir, tdir.GetName())
    cu.check_dir_exists_create(plot_dir)
    stem = "genjet_ind_recojet_ind_pt_"
    for plot_name in cu.get_list_of_element_names(tdir):
        if not plot_name.startswith(stem):
            continue
        h2d = cu.get_from_tfile(tdir, plot_name)
        h2d.SetTitle(h2d.GetTitle())
        renorm_h2d = cu.make_normalised_TH2(h2d, 'X', recolour=False)
        # renorm_h2d = h2d
        canv = ROOT.TCanvas(cu.get_unique_str(), "", 800, 600)
        pad = ROOT.gPad
        pad.SetBottomMargin(0.12)
        pad.SetLeftMargin(0.13)
        pad.SetRightMargin(0.12)
        canv.SetTicks(1, 1)
        renorm_h2d.Draw("COLZ TEXT")
        renorm_h2d.SetMaximum(1)
        renorm_h2d.SetMinimum(0)
        title_offset = 1.5
        renorm_h2d.SetTitleOffset(title_offset, 'X')
        renorm_h2d.SetTitleOffset(title_offset * 1.15, 'Y')

        canv.SaveAs(
            os.path.join(plot_dir,
                         "%s_renormX_linZ.%s" % (plot_name, OUTPUT_FMT)))
        # canv.SetLogz()
        # renorm_h2d.SetMinimum(1E-3)
        # canv.SaveAs(os.path.join(plot_dir, "%s_renormX_logZ.%s" % (plot_name, OUTPUT_FMT)))

        canv.Clear()
        renorm_h2d = cu.make_normalised_TH2(h2d, 'Y', recolour=False)
        renorm_h2d.Draw("COLZ TEXT")
        renorm_h2d.SetMaximum(1)
        renorm_h2d.SetMinimum(0)
        title_offset = 1.5
        renorm_h2d.SetTitleOffset(title_offset, 'X')
        renorm_h2d.SetTitleOffset(title_offset * 1.15, 'Y')
        canv.SaveAs(
            os.path.join(plot_dir,
                         "%s_renormY_linZ.%s" % (plot_name, OUTPUT_FMT)))
Пример #14
0
def do_pthat_comparison_plot(dirname_label_pairs, output_filename,
                             **plot_kwargs):
    qcd_files = [
        cu.open_root_file(os.path.join(dl[0], qgc.QCD_PYTHIA_ONLY_FILENAME))
        for dl in dirname_label_pairs
    ]
    histname = "Dijet_gen/ptHat"
    qcd_hists = [cu.get_from_tfile(qf, histname) for qf in qcd_files]
    N = len(dirname_label_pairs)
    pthat_rebin = array('d', [
        15, 30, 50, 80, 120, 170, 300, 470, 600, 800, 1000, 1400, 1800, 2400,
        3200, 5000
    ])
    nbins = len(pthat_rebin) - 1
    qcd_hists = [
        h.Rebin(nbins, cu.get_unique_str(), pthat_rebin) for h in qcd_hists
    ]
    conts = [
        Contribution(qcd_hists[i],
                     label=lab,
                     marker_color=cu.get_colour_seq(i, N),
                     line_color=cu.get_colour_seq(i, N),
                     line_style=i + 1,
                     line_width=2,
                     subplot=qcd_hists[0] if i != 0 else None)
        for i, (d, lab) in enumerate(dirname_label_pairs)
    ]
    plot = Plot(conts,
                what='hist',
                ytitle="N",
                subplot_limits=(0.75, 1.25),
                subplot_type="ratio",
                subplot_title="* / %s" % (dirname_label_pairs[0][1]),
                **plot_kwargs)
    plot.y_padding_max_log = 500
    plot.legend.SetY1(0.7)
    plot.plot("NOSTACK HIST E")
    plot.set_logx(do_more_labels=False)
    plot.set_logy(do_more_labels=False)

    plot.save(output_filename)
def do_weight_vs_var_plot(histname, input_filename, output_filename):
    ROOT.gStyle.SetPalette(palette_2D)
    tf = cu.open_root_file(input_filename)
    h3d = cu.get_from_tfile(tf, histname)
    if h3d.GetEntries() == 0:
        return
    h2d = h3d.Project3D("xz")
    if "unweighted" in histname:
        h2d.SetTitle("Unweighted")
    else:
        h2d.SetTitle("Weighted")
    h2d.GetXaxis().SetTitle(get_var_str(histname))

    canv = ROOT.TCanvas(cu.get_unique_str(), "", 800, 600)
    canv.SetTicks(1, 1)
    canv.SetLeftMargin(0.1)
    canv.SetRightMargin(0.15)
    canv.SetLogz()
    h2d.Draw("COLZ")
    canv.SaveAs(output_filename)
    tf.Close()
Пример #16
0
def do_genht_comparison_plot(dirname_label_pairs, output_filename,
                             **plot_kwargs):
    """Like do_genht but for multiple samples"""
    qcd_files = [
        cu.open_root_file(os.path.join(dl[0], qgc.QCD_FILENAME))
        for dl in dirname_label_pairs
    ]
    histname = "Dijet_gen/gen_ht"
    qcd_hists = [cu.get_from_tfile(qf, histname) for qf in qcd_files]
    N = len(dirname_label_pairs)
    conts = [
        Contribution(qcd_hists[i],
                     label=lab,
                     marker_color=cu.get_colour_seq(i, N),
                     line_color=cu.get_colour_seq(i, N),
                     line_style=i + 1,
                     line_width=2,
                     subplot=qcd_hists[0] if i != 0 else None)
        for i, (d, lab) in enumerate(dirname_label_pairs)
    ]
    plot = Plot(
        conts,
        what='hist',
        ytitle="N",
        # subplot_limits=(0.75, 1.25),
        subplot_type="ratio",
        subplot_title="* / %s" % (dirname_label_pairs[0][1]),
        ylim=[1E6, None],
        **plot_kwargs)
    plot.y_padding_max_log = 500
    plot.legend.SetY1(0.7)
    plot.subplot_maximum_ceil = 5
    plot.plot("NOSTACK HIST E")
    plot.set_logx(do_more_labels=False)
    plot.set_logy(do_more_labels=False)

    plot.save(output_filename)
def do_pt_transfer_plot(tdir, plot_dir):
    """Plot ratio between pt bins of the spectrum. Check to make sure xfer factor << drop in pt"""
    plot_dir = os.path.join(plot_dir, tdir.GetName())
    cu.check_dir_exists_create(plot_dir)
    hist_name = "pt_jet_response_binning"
    h = cu.get_from_tfile(tdir, hist_name)
    binning = [
        h.GetXaxis().GetBinLowEdge(bin_ind)
        for bin_ind in range(1,
                             h.GetNbinsX() + 1)
    ]
    hist_factors = ROOT.TH1F(
        "hist_factors" + cu.get_unique_str(),
        ";p_{T}^{Reco} [GeV];Fraction rel to previous bin",
        len(binning) - 1, array('d', binning))
    for bin_ind in range(2, h.GetNbinsX() + 1):
        cont = h.GetBinContent(bin_ind)
        cont_prev = h.GetBinContent(bin_ind - 1)
        if cont == 0 or cont_prev == 0:
            continue
        factor = cont / cont_prev
        hist_factors.SetBinContent(bin_ind, factor)
        hist_factors.SetBinError(bin_ind, 0)

    col_purity = ROOT.kBlack
    conts = [
        Contribution(hist_factors,
                     label="Factor relative to previous bin",
                     line_color=col_purity,
                     marker_color=col_purity),
        # Contribution(hist_purity, label="Purity (gen in right bin)", line_color=col_purity, marker_color=col_purity),
    ]
    xlim = [30, binning[-1]]
    plot = Plot(conts, what='hist', xlim=xlim)
    plot.plot()
    plot.set_logx()
    plot.save(os.path.join(plot_dir, 'pt_migration_factors.%s' % (OUTPUT_FMT)))
def do_jet_pt_with_var_cuts(histname, cuts, input_filename, output_filename):
    ROOT.gStyle.SetPalette(palette_1D)
    total = len(cuts) - 1 + .1 # slight offset to not hit the maximum or minimum
    # if len(cuts) <= 3:
        # ROOT.gStyle.SetPalette(ROOT.kCool)
        # num_colours = ROOT.TColor.GetPalette().fN - 1
        # print('num_colours:', num_colours)
        # for index in range(len(cuts)):
        #     print(num_colours, index, len(cuts), index / len(cuts), num_colours * index / total)
        #     print(index, ROOT.TColor.GetColorPalette(int(num_colours * 1. * index / total)))
    tf = cu.open_root_file(input_filename)
    h3d = cu.get_from_tfile(tf, histname)
    if h3d.GetEntries() == 0:
        return
    pt_hists = []
    for cut in cuts:
        max_bin = h3d.GetZaxis().FindFixBin(cut)
        # print("cut:", cut, "bin:", max_bin)
        h = h3d.ProjectionY("pt_var_lt_%g" % cut, 0, -1, 0, max_bin, "e")
        h2 = h.Clone()
        h2.Rebin(2)
        if h.GetEntries() > 0:
            h3 = qgp.hist_divide_bin_width(h2)
        pt_hists.append(h3)

    line_styles = [1, 2, 3]
    if len(cuts) <= 3:
        line_styles = [1]
    n_line_styles = len(line_styles)
    ref_ind = 0
    conts = [Contribution(h, label=" < %g" % cut,
                          line_color=cu.get_colour_seq(ind, total),
                          line_style=line_styles[ind % n_line_styles],
                          line_width=2,
                          marker_color=cu.get_colour_seq(ind, total),
                          subplot=pt_hists[ref_ind] if ind != ref_ind else None)
             for ind, (h, cut) in enumerate(zip(pt_hists, cuts))]

    jet_str = pt_genjet_str if "_vs_pt_genjet_vs_" in histname else pt_str
    weight_str = "(unweighted)" if "unweighted" in histname else "(weighted)"
    ratio_lims = (0.5, 2.5)
    ratio_lims = (0.5, 1.1)
    plot = Plot(conts, what='hist',
                title='%s for cuts on %s %s' % (jet_str, get_var_str(histname), weight_str),
                xtitle=None,
                ytitle='N',
                # xlim=None, ylim=None,
                legend=True,
                subplot_type='ratio',
                subplot_title='* / var < %g' % cuts[ref_ind],
                subplot_limits=ratio_lims,
                has_data=False)
    plot.y_padding_max_log = 200
    plot.subplot_maximum_ceil = 4
    plot.subplot_maximum_floor = 1.02
    plot.subplot_minimum_ceil = 0.98
    plot.legend.SetY1(0.7)
    plot.legend.SetY2(0.89)
    plot.legend.SetX1(0.78)
    plot.legend.SetX2(0.88)
    plot.plot("NOSTACK HISTE", "NOSTACK HIST")
    plot.set_logx(True, do_more_labels=True)
    plot.set_logy(True, do_more_labels=False)
    plot.save(output_filename)
def do_plots(root_dir):
    # QG variable plots
    pt_bins = qgc.PT_BINS[:]
    var_list = qgc.COMMON_VARS
    var_prepend = ""

    radius, pus = cu.get_jet_config_from_dirname(root_dir)
    jet_str = "AK%s PF %s" % (radius.upper(), pus.upper())

    for gr_append in ["", "_groomed"][:1]:
        if gr_append == "_groomed":
            print("Doing groomed plots...")
        else:
            print("Doing ungroomed plots...")

        zpj_dirname = "ZPlusJets_QG%s" % (gr_append)
        dj_cen_dirname = "Dijet_QG_central_tighter%s" % (gr_append)
        dj_fwd_dirname = "Dijet_QG_forward_tighter%s" % (gr_append)

        # tfiles = {
        #     "data_run_B": cu.open_root_file(os.path.join(root_dir, "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_RunB.root")),
        #     "data_run_C": cu.open_root_file(os.path.join(root_dir, "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_RunC.root")),
        #     "data_run_D": cu.open_root_file(os.path.join(root_dir, "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_RunD.root")),
        #     "data_run_E": cu.open_root_file(os.path.join(root_dir, "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_RunE.root")),
        #     "data_run_F": cu.open_root_file(os.path.join(root_dir, "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_RunF.root")),
        #     "data_run_G": cu.open_root_file(os.path.join(root_dir, "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_RunG.root")),
        #     "data_run_H": cu.open_root_file(os.path.join(root_dir, "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_RunH.root")),
        # }

        tfiles = {
            "data_run_B":
            cu.open_root_file(
                os.path.join(
                    root_dir,
                    "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_ZeroBias_RunB.root"
                )),
            "data_run_C":
            cu.open_root_file(
                os.path.join(
                    root_dir,
                    "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_ZeroBias_RunC.root"
                )),
            "data_run_D":
            cu.open_root_file(
                os.path.join(
                    root_dir,
                    "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_ZeroBias_RunD.root"
                )),
            "data_run_E":
            cu.open_root_file(
                os.path.join(
                    root_dir,
                    "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_ZeroBias_RunE.root"
                )),
            "data_run_F":
            cu.open_root_file(
                os.path.join(
                    root_dir,
                    "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_ZeroBias_RunF.root"
                )),
            "data_run_G":
            cu.open_root_file(
                os.path.join(
                    root_dir,
                    "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_ZeroBias_RunG.root"
                )),
            "data_run_H":
            cu.open_root_file(
                os.path.join(
                    root_dir,
                    "uhh2.AnalysisModuleRunner.DATA.Data_JetHT_ZeroBias_RunH.root"
                )),
        }

        for ang in var_list[:]:
            print("...Doing", ang.name)
            v = "%s%s_vs_pt" % (var_prepend, ang.var)

            zpj_1d_entries = []
            dijet_cen_1d_entries = []
            dijet_fwd_1d_entries = []

            for pt_ind, (start_val, end_val) in enumerate(pt_bins):
                entries = []
                zpj_entries = []
                dijet_cen_entries = []
                dijet_fwd_entries = []

                # Get all plots
                lw = 2
                msize = 1.1
                data_line_width = lw

                colours = [
                    ROOT.kBlack, ROOT.kRed, ROOT.kBlue, ROOT.kGreen + 2,
                    ROOT.kOrange + 1, ROOT.kAzure + 1, ROOT.kGreen + 3
                ]

                # --------------------------------------------------------------
                # Create reference hists, for Run G+H
                # --------------------------------------------------------------
                ####################
                # DIJET CENTRAL REGION
                ####################
                h2d_qcd_cen_data_g = cu.get_from_tfile(
                    tfiles['data_run_G'], "%s/%s" % (dj_cen_dirname, v))
                h2d_qcd_cen_data_h = cu.get_from_tfile(
                    tfiles['data_run_H'], "%s/%s" % (dj_cen_dirname, v))
                colour = colours[0]
                qcd_cen_kwargs_data = dict(line_color=colour,
                                           line_width=data_line_width,
                                           fill_color=colour,
                                           marker_color=colour,
                                           marker_style=cu.Marker.get(
                                               qgc.QCD_MARKER),
                                           marker_size=0,
                                           label="Runs G+H")
                dijet_cen_data_hist_g = qgp.get_projection_plot(
                    h2d_qcd_cen_data_g, start_val, end_val)
                dijet_cen_data_hist_h = qgp.get_projection_plot(
                    h2d_qcd_cen_data_h, start_val, end_val)
                dijet_cen_data_hist_g.Add(dijet_cen_data_hist_h)
                dijet_cen_entries.append(
                    (dijet_cen_data_hist_g, qcd_cen_kwargs_data))

                ####################
                # DIJET FORWARD REGION
                ####################
                h2d_qcd_fwd_data_g = cu.get_from_tfile(
                    tfiles['data_run_G'], "%s/%s" % (dj_fwd_dirname, v))
                h2d_qcd_fwd_data_h = cu.get_from_tfile(
                    tfiles['data_run_H'], "%s/%s" % (dj_fwd_dirname, v))
                qcd_fwd_kwargs_data = dict(line_color=colour,
                                           line_width=data_line_width,
                                           fill_color=colour,
                                           marker_color=colour,
                                           marker_style=cu.Marker.get(
                                               qgc.QCD_MARKER),
                                           marker_size=0,
                                           label="Runs G+H")
                dijet_fwd_data_hist_g = qgp.get_projection_plot(
                    h2d_qcd_fwd_data_g, start_val, end_val)
                dijet_fwd_data_hist_h = qgp.get_projection_plot(
                    h2d_qcd_fwd_data_h, start_val, end_val)
                dijet_fwd_data_hist_g.Add(dijet_fwd_data_hist_h)
                dijet_fwd_entries.append(
                    (dijet_fwd_data_hist_g, qcd_fwd_kwargs_data))

                # --------------------------------------------------------------
                # FOR SUMMED B-F:
                # --------------------------------------------------------------
                dijet_cen_data_hist_btof, dijet_fwd_data_hist_btof = None, None
                runs = ["B", "C", "D", "E", "F"]
                for run in runs:

                    # DIJET CENTRAL REGION
                    h2d_qcd_cen_data = cu.get_from_tfile(
                        tfiles['data_run_%s' % run],
                        "%s/%s" % (dj_cen_dirname, v))
                    this_dijet_cen_data_hist = qgp.get_projection_plot(
                        h2d_qcd_cen_data, start_val, end_val)
                    if dijet_cen_data_hist_btof is None:
                        dijet_cen_data_hist_btof = this_dijet_cen_data_hist.Clone(
                        )
                    else:
                        dijet_cen_data_hist_btof.Add(this_dijet_cen_data_hist)

                    # DIJET FORWARD REGION
                    h2d_qcd_fwd_data = cu.get_from_tfile(
                        tfiles['data_run_%s' % run], "%s/%s" %
                        (dj_fwd_dirname, v))  # use already merged jetht+zb
                    this_dijet_fwd_data_hist = qgp.get_projection_plot(
                        h2d_qcd_fwd_data, start_val, end_val)
                    if dijet_fwd_data_hist_btof is None:
                        dijet_fwd_data_hist_btof = this_dijet_fwd_data_hist.Clone(
                        )
                    else:
                        dijet_fwd_data_hist_btof.Add(this_dijet_fwd_data_hist)

                colour = colours[1]
                mark = cu.Marker.get('circle')
                btof_cen_kwargs_data = dict(line_color=colour,
                                            line_width=data_line_width,
                                            fill_color=colour,
                                            marker_color=colour,
                                            marker_style=mark,
                                            marker_size=msize,
                                            label="Runs %s-%s" %
                                            (runs[0], runs[-1]),
                                            subplot=dijet_cen_data_hist_g)
                dijet_cen_entries.append(
                    (dijet_cen_data_hist_btof, btof_cen_kwargs_data))

                btof_fwd_kwargs_data = dict(line_color=colour,
                                            line_width=data_line_width,
                                            fill_color=colour,
                                            marker_color=colour,
                                            marker_style=mark,
                                            marker_size=msize,
                                            label="Runs %s-%s" %
                                            (runs[0], runs[-1]),
                                            subplot=dijet_fwd_data_hist_g)
                dijet_fwd_entries.append(
                    (dijet_fwd_data_hist_btof, btof_fwd_kwargs_data))

                # FOR INDIVIDUAL RUN PERIODS:
                # runs = ["B", "C", "D", "E", "F"]
                # for run, colour, mark in zip(runs, colours[1:], cu.Marker().cycle()):

                #     ####################
                #     # DIJET CENTRAL REGION
                #     ####################
                #     # JETHT DATA
                #     h2d_qcd_cen_data = cu.get_from_tfile(tfiles['data_run_%s' % run], "%s/%s" % (dj_cen_dirname, v))
                #     qcd_cen_kwargs_data = dict(line_color=colour, line_width=data_line_width, fill_color=colour,
                #                                marker_color=colour, marker_style=mark, marker_size=msize,
                #                                label="Run %s" % run,
                #                                subplot=dijet_cen_data_hist_g)
                #     dijet_cen_data_hist = qgp.get_projection_plot(h2d_qcd_cen_data, start_val, end_val)
                #     dijet_cen_entries.append((dijet_cen_data_hist, qcd_cen_kwargs_data))

                #     ####################
                #     # DIJET FORWARD REGION
                #     ####################
                #     # JETHT DATA
                #     h2d_qcd_fwd_data = cu.get_from_tfile(tfiles['data_run_%s' % run], "%s/%s" % (dj_fwd_dirname, v))  # use already merged jetht+zb
                #     qcd_fwd_kwargs_data = dict(line_color=colour, line_width=data_line_width, fill_color=colour,
                #                                marker_color=colour, marker_style=mark, marker_size=msize,
                #                                label="Run %s" % run,
                #                                subplot=dijet_fwd_data_hist_g)
                #     dijet_fwd_data_hist = qgp.get_projection_plot(h2d_qcd_fwd_data, start_val, end_val)
                #     dijet_fwd_entries.append((dijet_fwd_data_hist, qcd_fwd_kwargs_data))

                #     ####################
                #     # Z+JETS REGION
                #     ####################

                #     # SINGLE MU DATA
                #     # h2d_dyj_data = cu.get_from_tfile(os.path.join(root_dir, "uhh2.AnalysisModuleRunner.DATA.Data_SingleMu_Run%s.root" % run), "%s/%s" % (zpj_dirname, v))
                #     # dy_kwargs_data = dict(line_color=qgc.colour, line_width=data_line_width, fill_color=qgc.colour,
                #     #                       marker_color=qgc.colour, marker_style=cu.Marker.get(qgc.DY_MARKER), marker_size=msize,
                #     #                       label="SingleMu Run %s" % run)
                #     # zpj_data_hist = qgp.get_projection_plot(h2d_dyj_data, start_val, end_val)
                #     # zpj_entries.append((zpj_data_hist, dy_kwargs_data))

                #################
                # SETUP PLOTTING
                #################
                rebin = 2
                v_lower = v.lower()
                if "multiplicity" in v_lower:
                    rebin = 2
                elif "flavour" in v_lower or "thrust" in v_lower:
                    rebin = 1
                elif 'ptd' in v_lower:
                    rebin = 2

                xlim = None
                if "width" in v_lower or "ptd" in v_lower:
                    xlim = (0, 1)
                elif "thrust" in v_lower:
                    xlim = (0, 0.5)
                elif "multiplicity" in v_lower and "ak4" in root_dir.lower():
                    if end_val <= 150:
                        xlim = (0, 50)
                    else:
                        xlim = (0, 80)

                ylim = None
                if "flavour" in v_lower:
                    ylim = (0, 1)
                # elif "lha" in v_lower:
                # ylim = (0, 5)

                plot_dir = os.path.join(root_dir,
                                        "plots_run_dependent%s" % (gr_append))
                plot_dir = os.path.join(
                    root_dir, "plots_run_dependent_btof%s" % (gr_append))

                subplot_title = "Run * / G+H"
                subplot_title = "B-F / G+H"
                subplot_limits = (0.5, 1.5)

                xlabel = ang.name + " (" + ang.lambda_str + ")"
                if gr_append is not "":
                    xlabel = "Groomed " + ang.name + " (" + ang.lambda_str + ")"

                # dj central only
                dijet_cen_entries = dijet_cen_entries[1:] + dijet_cen_entries[
                    0:1]  # move G+H to last
                qgp.do_comparison_plot(
                    dijet_cen_entries,
                    "%s/ptBinned/%s_pt%dto%d_dijet_central.%s" %
                    (plot_dir, v, start_val, end_val, OUTPUT_FMT),
                    rebin=rebin,
                    title="%d < p_{T}^{jet} < %d GeV\n%s\n%s" %
                    (start_val, end_val, jet_str, qgc.Dijet_CEN_LABEL),
                    xtitle=xlabel,
                    xlim=xlim,
                    ylim=ylim,
                    subplot_type='ratio',
                    subplot_title=subplot_title,
                    subplot_limits=subplot_limits)

                # dj forward only
                dijet_fwd_entries = dijet_fwd_entries[1:] + dijet_fwd_entries[
                    0:1]  # move G+H to last
                qgp.do_comparison_plot(
                    dijet_fwd_entries,
                    "%s/ptBinned/%s_pt%dto%d_dijet_forward.%s" %
                    (plot_dir, v, start_val, end_val, OUTPUT_FMT),
                    rebin=rebin,
                    title="%d < p_{T}^{jet} < %d GeV\n%s\n%s" %
                    (start_val, end_val, jet_str, qgc.Dijet_FWD_LABEL),
                    xtitle=xlabel,
                    xlim=xlim,
                    ylim=ylim,
                    subplot_type='ratio',
                    subplot_title=subplot_title,
                    subplot_limits=subplot_limits)
def do_var_vs_pt_plot(histname, input_filename, output_filename):
    ROOT.gStyle.SetPalette(palette_2D)
    tf = cu.open_root_file(input_filename)
    h3d = cu.get_from_tfile(tf, histname)
    if h3d.GetEntries() == 0:
        return
    h2d = h3d.Project3D("zy")

    xlabel = h2d.GetXaxis().GetTitle()
    ylabel = h2d.GetYaxis().GetTitle()
    ylabel = get_var_str(histname)

    # find largest var value (ie row) that has a filled bin
    h2d_ndarray = cu.th2_to_ndarray(h2d)[0]

    xbins = np.array(cu.get_bin_edges(h2d, 'x'))
    ybins = np.array(cu.get_bin_edges(h2d, 'y'))

    # remove dodgy bins with 0 width cos I was an idiot and duplicated some bins
    n_deleted = 0
    # weight bin
    # xax = h2d.GetXaxis()
    # for ix in range(1, h2d.GetNbinsX()+1):
    #     if xax.GetBinWidth(ix) == 0:
    #         h2d_ndarray = np.delete(h2d_ndarray, ix-1-n_deleted, axis=1)
    #         xbins = np.delete(xbins, ix-1-n_deleted, axis=0)
    #         n_deleted += 1
    #         print("Deleting bin", ix)

    # pt bin
    # n_deleted = 0
    # yax = h2d.GetYaxis()
    # for iy in range(1, h2d.GetNbinsY()+1):
    #     if yax.GetBinWidth(iy) == 0:
    #         h2d_ndarray = np.delete(h2d_ndarray, iy-1-n_deleted, axis=0)
    #         ybins = np.delete(ybins, iy-1-n_deleted, axis=0)
    #         n_deleted += 1
    #         print("Deleting bin", iy)

    # nonzero returns (row #s)(col #s) of non-zero elements
    # we only want the largest row #
    max_filled_row_ind = int(np.nonzero(h2d_ndarray)[0].max())
    h2d = cu.ndarray_to_th2(h2d_ndarray, binsx=xbins, binsy=ybins)

    if "unweighted" in histname:
        h2d.SetTitle("Unweighted;%s;%s" % (xlabel, ylabel))
    else:
        h2d.SetTitle("Weighted;%s;%s" % (xlabel, ylabel))

    h2d.GetYaxis().SetRange(1, max_filled_row_ind+2)  # +1 as ROOT 1-indexed, +1 for padding
    h2d.GetYaxis().SetTitle(get_var_str(histname))

    xmin = 15 if "pt_genjet_vs" in histname else 30
    xmax = 300

    canv = ROOT.TCanvas(cu.get_unique_str(), "", 800, 600)
    canv.SetTicks(1, 1)
    canv.SetLeftMargin(0.12)
    canv.SetRightMargin(0.15)
    # canv.SetLogz()
    # canv.SetLogy()
    h2d_copy = h2d.Clone()
    # h2d_copy.Scale(1, "width")
    h2d_copy.Draw("COLZ")
    canv.SetLogx()
    h2d_copy.GetXaxis().SetMoreLogLabels()
    canv.SaveAs(output_filename)

    zoom_ymin, zoom_ymax = 0.1, 5

    h2d_copy.SetAxisRange(zoom_ymin, zoom_ymax,"Y")
    h2d_copy.SetAxisRange(xmin, xmax, "X")
    canv.SaveAs(output_filename.replace(".pdf", "_zoomY.pdf"))
    
    canv.SetLogz()
    canv.SaveAs(output_filename.replace(".pdf", "_zoomY_logZ.pdf"))

    canv.SetLogz(False)
    # h2d.Scale(1, "width")
    h2d_normed = cu.make_normalised_TH2(h2d, norm_axis='x', recolour=True)
    h2d_normed.Draw("COLZ")
    h2d_normed.GetXaxis().SetMoreLogLabels()
    # h2d_normed.SetMinimum(1E-5)
    h2d_normed.SetAxisRange(xmin, xmax, "X")
    canv.SaveAs(output_filename.replace(".pdf", "_normX.pdf"))
    
    h2d_normed.SetAxisRange(zoom_ymin, zoom_ymax,"Y")
    canv.SaveAs(output_filename.replace(".pdf", "_normX_zoomY.pdf"))

    # Do cumulative plot per column (ie fraction of events passing cut < y)
    h2d_ndarray_cumsum = h2d_ndarray.cumsum(axis=0)
    nonzero_mask = h2d_ndarray_cumsum[-1] > 0
    h2d_ndarray_cumsum[:, nonzero_mask] /= h2d_ndarray_cumsum[-1][nonzero_mask] # scale so total is 1
    
    h2d_cumsum = cu.ndarray_to_th2(h2d_ndarray_cumsum, binsx=xbins, binsy=ybins)
    # Get max row ind
    max_filled_row_ind = int(h2d_ndarray_cumsum.argmax(axis=0).max())
    h2d_cumsum.GetYaxis().SetRange(1, max_filled_row_ind+1)  # +1 as ROOT 1-indexed

    # ROOT.gStyle.SetPalette(ROOT.kBird)
    ylabel = "Fraction of events with " + ylabel + " < y"
    if "unweighted" in histname:
        h2d_cumsum.SetTitle("Unweighted;%s;%s" % (xlabel, ylabel))
    else:
        h2d_cumsum.SetTitle("Weighted;%s;%s" % (xlabel, ylabel))
    canv.Clear()
    canv.SetLogz(False)

    h2d_cumsum.SetContour(20)
    h2d_cumsum.Draw("CONT1Z")
    h2d_cumsum.SetAxisRange(xmin, xmax, "X")
    canv.SetLogx()
    h2d_cumsum.GetXaxis().SetMoreLogLabels()
    canv.SaveAs(output_filename.replace(".pdf", "_cumulY.pdf"))

    h2d_cumsum.SetAxisRange(zoom_ymin, zoom_ymax,"Y")
    canv.SaveAs(output_filename.replace(".pdf", "_cumulY_zoomY.pdf"))
    canv.Clear()

    h2d_normed.Draw("COL")
    h2d_cumsum.Draw("CONT1Z SAME")
    h2d_cumsum.SetAxisRange(xmin, xmax, "X")
    canv.SetLogx()
    h2d_cumsum.GetXaxis().SetMoreLogLabels()
    canv.SaveAs(output_filename.replace(".pdf", "_cumulY_normX.pdf"))

    h2d_cumsum.SetAxisRange(zoom_ymin, zoom_ymax,"Y")
    canv.SaveAs(output_filename.replace(".pdf", "_cumulY_normX_zoomY.pdf"))

    tf.Close()
Пример #21
0
def do_all_2d_plots(tfile, output_dir):
    rebin = 2
    hist_dicts = [
    {
    "hname": "JtcefVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtcef",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtchfVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtchf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtchmultVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtchmult",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JthfefVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jthfef",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JthfhfVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jthfhf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtmufVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtmuf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtnefVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtnef",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtnhfVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtnhf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtnmultVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtnmult",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefcefRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefcefRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefchfRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefchfRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefchmultRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefchmultRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefmufRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefmufRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefnefRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefnefRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefnhfRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefnhfRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefnmultRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefnmultRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefcefVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refcef",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefchfVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refchf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefchmultVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refchmult",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefmufVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refmuf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefnefVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refnef",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefnhfVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refnhf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefnmultVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refnmult",
    "rebinx": rebin,
    "rebiny": rebin,
    },



    ]
    for hdict in hist_dicts:
        for renorm in ['', 'X', 'Y']:
            h2d = cu.get_from_tfile(tfile, "ak4pfchs/"+hdict['hname'])
            ofile = output_dir+"/"+hdict.get('ofile', hdict['hname'])
            if renorm == "X":
                ofile += "_renormX"
            elif renorm == "Y":
                ofile += "_renormY"
            ofile +=".pdf"
            do_2d_plot(h2d, output_filename=ofile,
                       renorm=renorm,
                       logz=hdict.get('logz', False),
                       xtitle=hdict.get('xtitle', ''), ytitle=hdict.get('ytitle', ''),
                       rebinx=hdict.get('rebinx', 1), rebiny=hdict.get('rebiny', 1),
                       )
Пример #22
0
def make_plot(entries,
              output_filename,
              plot_kwargs,
              projection_axis='x',
              start_val=None,
              end_val=None,
              is_pdgid_plot=False,
              logy=False):
    """Make a plot from entries. Each one is a projection of a 2D plot.
    
    Parameters
    ----------
    entries : list[dict]
        List of plot entries. Each is represented by a dict
    output_filename : str
        Filename for output plot
    plot_kwargs : dict
        kwargs for Plot constructor
    projection_axis : str, optional
        Axis to use for projection. If None, must be specified in entry.
    start_val : None, optional
        If set, use this to make 1D projection plot. Cuts on X axis. 
        Otherwise should be set per entry.
    end_val : None, optional
        If set, use this to make 1D projection plot. Cuts on X axis. 
        Otherwise should be set per entry.
    is_pdgid_plot : bool, optional
        True if for PDGIDs (sets special size & binning)
    logy : bool, optional
        Description
    
    Raises
    ------
    RuntimeError
        Description
    """
    conts = []
    if not is_pdgid_plot:
        for ent in entries:
            h2d = cu.get_from_tfile(
                ent['file'],
                "ak4pfchsl1/" + ent['histname'] + "_JetEta0to0.783")
            start_val = ent.get('start_val', start_val)
            end_val = ent.get('end_val', end_val)
            if start_val is None or end_val is None:
                raise RuntimeError("Expected start_val and end_val")
            hist = cu.get_projection_plot(
                h2d, start_val, end_val,
                ent.get('projection_axis', projection_axis))
            if hist.GetEntries() == 0:
                ent['used'] = False
                continue
            contrib = Contribution(hist,
                                   label=ent['label'],
                                   line_width=lw,
                                   line_color=ent['colour'],
                                   line_style=ent.get('line_style', 1),
                                   marker_size=ent.get('marker_size', 0),
                                   marker_color=ent['colour'],
                                   marker_style=ent.get('marker_style', 1),
                                   normalise_hist=True,
                                   rebin_hist=ent.get('rebin', None))
            conts.append(contrib)
            ent['used'] = True

    else:
        # For PDGID plots, we want a different canvas aspect ratio, and only to include bins
        # with non-0 contents. We also relabel bins.
        custom_bins = []
        for ent in entries:
            h2d = cu.get_from_tfile(
                ent['file'],
                "ak4pfchsl1/" + ent['histname'] + "_JetEta0to0.783")
            start_val = ent.get('start_val', start_val)
            end_val = ent.get('end_val', end_val)
            if not start_val or not end_val:
                raise RuntimeError("Expected start_val and end_val")
            hist = cu.get_projection_plot(
                h2d, start_val, end_val,
                ent.get('projection_axis', projection_axis))
            ax = hist.GetXaxis()
            bins = dict()
            for i in range(1, hist.GetNbinsX() + 1):
                value = ax.GetBinLowEdge(i)
                cont = hist.GetBinContent(i)
                err = hist.GetBinError(i)
                if cont > 0:
                    bins[value] = [cont, err]
                    # print(custom_bins[-1])

            custom_bins.append(bins)

        nbins = max([len(d) for d in custom_bins])
        first_keys = set(custom_bins[0].keys())
        for cb in custom_bins[1:]:
            first_keys = first_keys.union(set(cb.keys()))
        all_keys = sorted(list(first_keys))
        print(all_keys)

        for cbin, ent in zip(custom_bins, entries):
            h = ROOT.TH1D(cu.get_unique_str(), ";PDGID;N", nbins, 0, nbins)
            for ind, k in enumerate(all_keys, 1):
                content, error = cbin.get(k, [0, 0])
                h.SetBinContent(ind, content)
                h.SetBinError(ind, error)
            ax = h.GetXaxis()
            for i in range(1, nbins + 1):
                # ax.SetBinLabel(i, str(int(all_keys[i-1])))
                pdgid = int(all_keys[i - 1])
                ax.SetBinLabel(i, PDGID_STR.get(pdgid, str(pdgid)))
            h.LabelsOption("v")
            contrib = Contribution(h,
                                   label=ent['label'],
                                   line_width=lw,
                                   line_color=ent['colour'],
                                   line_style=ent.get('line_style', 1),
                                   marker_size=ent.get('marker_size', 0),
                                   marker_color=ent['colour'],
                                   marker_style=ent.get('marker_style', 1),
                                   normalise_hist=True,
                                   rebin_hist=ent.get('rebin', None))
            conts.append(contrib)
            ent['used'] = True

    entries = [e for e in entries if e['used']]
    for ind, ent in enumerate(entries):
        if not ent['used']:
            continue
        if ent.get('subplot_ind', -1) >= 0:
            conts[ind].subplot = conts[ent['subplot_ind']].obj

    plot = Plot(conts,
                what="hist",
                ytitle="p.d.f.",
                has_data=False,
                **plot_kwargs)
    if is_pdgid_plot and conts[0].obj.GetNbinsX() > 10:
        plot.default_canvas_size = (800, 800)
    else:
        plot.default_canvas_size = (450, 600)
    plot.legend.SetX1(0.5)
    plot.legend.SetX2(0.97)
    if len(entries) > 4:
        plot.legend.SetY1(0.7)
        plot.legend.SetNColumns(2)
    else:
        plot.legend.SetY1(0.75)
    plot.legend.SetY2(0.9)
    plot.plot("HISTE NOSTACK")
    if logy:
        plot.set_logy()
    plot.save(output_filename)
Пример #23
0
ROOT.gStyle.SetOptStat(0)
My_Style.cd()


FONT_SIZE = 0.032


f_hpp = cu.open_root_file("QCD_Pt_Herwigpp_NoJEC_relPtHatCut2p5_jtptmin4_HadronParton_nbinsrelrsp_10k_L1FastJet_Summer16_07Aug2017_V20_MC/jra_QCD_Pt_15to7000_Herwigpp_NoJEC_newFlav_ak4pfchs_rspRangeLarge_absEta_hadronParton.root")
f_hpp = cu.open_root_file("QCD_Pt_Pythia8_CP5_NoJEC_relPtHatCut2p5_jtptmin4_HadronParton_nbinsrelrsp_10k_L1FastJet_Autumn18_V3_MC/jra_QCD_Pt_15toInf_NoJEC_newFlav_ak4pfchs_rspRangeLarge_absEta_hadronParton.root")
f_h7 = cu.open_root_file("QCD_Pt_Herwig7_NoJEC_relPtHatCut2p5_jtptmin4_HadronParton_nbinsrelrsp_10k_L1FastJet_Autumn18_V3_MC/jra_QCD_Pt_15to7000_Herwig7_NoJEC_newFlav_ak4pfchs_rspRangeLarge_absEta_hadronParton.root")

for flav in ['b', 'ud', 'g', 'c', 's']:
    for ptmin, ptmax in [(750, 1000), (72, 90)]:
        ptbin = "%dto%d" % (ptmin, ptmax)
        histname = "ak4pfchsl1/%s_RelRsp_JetEta0to0.783_RefPt%s" % (flav, ptbin)
        hpp = cu.get_from_tfile(f_hpp, histname)
        h7 = cu.get_from_tfile(f_h7, histname)

        entries = [
        (hpp, {"label":"Pythia8 (CP5)", "line_color": ROOT.kAzure+10, "line_width": 2}),
        (h7, {"label":"Herwig7", "line_color": ROOT.kBlue, "line_width": 2, "subplot": hpp})
        ]

        plot_kwargs = {
            "title": "0 < |#eta| < 0.783\n%d < p_{T}^{Gen} < %d GeV\n%s jets (+ L1FastJet JEC)" %(ptmin, ptmax, flav),
            "xlim": [0.5, 1.5],
            "xtitle": "Response (p_{T}^{Reco} / p_{T}^{Gen})",
            "subplot_type": "ratio",
            # "subplot_title": "H7 / H++",
            "subplot_title": "H7 / Py8",
        }
Пример #24
0
def do_all_2d_plots(tfile, output_dir):
    rebin = 2
    hist_dicts = [
    {
    "hname": "JtcefVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtcef",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtchfVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtchf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtchmultVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtchmult",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JthfefVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jthfef",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JthfhfVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jthfhf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtmufVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtmuf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtnefVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtnef",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtnhfVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtnhf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtnmultVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Jtnmult",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefcefRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefcefRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefchfRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefchfRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefchmultRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefchmultRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefmufRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefmufRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefnefRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefnefRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefnhfRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefnhfRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "JtRefnmultRatioVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "JtRefnmultRatio",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefcefVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refcef",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefchfVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refchf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefchmultVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refchmult",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefmufVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refmuf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefnefVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refnef",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefnhfVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refnhf",
    "rebinx": rebin,
    "rebiny": rebin,
    },
    {
    "hname": "RefnmultVsRsp_JetEta0to0.783",
    "renorm": "X",
    "xtitle": "Response",
    "ytitle": "Refnmult",
    "rebinx": rebin,
    "rebiny": rebin,
    },



    ]
    for hdict in hist_dicts:
        h2d = cu.get_from_tfile(tfile, "ak4pfchs/"+hdict['hname'])
        for renorm in ['', 'X', 'Y']:
            ofile = output_dir+"/"+hdict.get('ofile', hdict['hname'])
            if renorm == "X":
                ofile += "_renormX"
            elif renorm == "Y":
                ofile += "_renormY"
            ofile +=".pdf"
            do_2d_plot(h2d, output_filename=ofile,
                       renorm=renorm,
                       logz=hdict.get('logz', False),
                       xtitle=hdict.get('xtitle', ''), ytitle=hdict.get('ytitle', ''),
                       rebinx=hdict.get('rebinx', 1), rebiny=hdict.get('rebiny', 1),
                       )

        # 1D plots cut on response
        h_rsp_low = cu.get_projection_plot(h2d, 0.9, 1.1, 'x')
        h_rsp_high = cu.get_projection_plot(h2d, 1.1, 2, 'x')
        entries = [
            (h_rsp_low, {'label': "0.9<rsp<1.1", 'line_color': ROOT.kBlue, 'line_width': 2, }),
            (h_rsp_high, {'label': "rsp>1.1", 'line_color': ROOT.kRed, 'line_width': 2, }),
        ]
        plot_kwargs = {
            "xtitle": hdict.get('ytitle', ''),
            'has_data': False,
        }
        ofile = output_dir + "/" + hdict.get('ofile', hdict['hname']) + "_1DrspCut.pdf"
        cu.do_comparison_plot(entries, ofile, rebin=2, **plot_kwargs)
    
    h2d = cu.get_from_tfile(tfile, "ak4pfchs/RelRspVsRefPt_JetEta0to0.783")
    h_rsp = cu.get_projection_plot(h2d, 0, 100000, 'x')

    entries = [
        (h_rsp, {'line_width':2})
    ]
    plot_kwargs = {
    'xtitle':'Response',
    'has_data': False,
    }
    ofile = output_dir + "/response.pdf"
    cu.do_comparison_plot(entries, ofile, rebin=2, **plot_kwargs)
def do_comparison_plots(workdir_label_pairs, output_dir):
    dirnames = [w[0] for w in workdir_label_pairs]

    # templates, we'll change the filename/dir as per instance
    total_len = len(workdir_label_pairs)
    mark = cu.Marker()
    sources = [
       {
           # "root_dir": wd,
           'label': label,
           "style": {
               'line_style': 1,
               'line_color': cu.get_colour_seq(ind, total_len),
               'marker_color': cu.get_colour_seq(ind, total_len),
               'marker_style': m,
               'marker_size': 0.75,
           }

       }
       for ind, ((wd, label), m) in enumerate(zip(workdir_label_pairs, mark.cycle(cycle_filling=True)))
    ]

    jet_config_str = qgc.extract_jet_config(dirnames[0])
    if len(dirnames) >1 and qgc.extract_jet_config(dirnames[1]) != jet_config_str:
        print("Conflicting jet config str, not adding")
        jet_config_str = None

    # COMPARE NOMINAL QCD
    if exists_in_all(qgc.QCD_FILENAME, dirnames):
        print("Found", qgc.QCD_FILENAME, "in all dirs")
        root_files = [cu.open_root_file(os.path.join(d, qgc.QCD_FILENAME)) for d in dirnames]
        directories = [cu.get_from_tfile(rf, "Dijet_tighter") for rf in root_files]

        this_sources = deepcopy(sources)
        for s in this_sources:
            s['label'] = "QCD [MG+PY8] [%s]" % s['label']

        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_qcd_compare_dijet_tighter_kinematics_normalised"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=True,
                                          bin_by='ave')

        directories = [cu.get_from_tfile(rf, "Dijet_eta_ordered") for rf in root_files]
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_qcd_compare_dijet_eta_ordered_kinematics_normalised"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=True,
                                          bin_by='ave')


    # COMPARE NOMINAL DY
    if exists_in_all(qgc.DY_FILENAME, dirnames):
        root_files = [cu.open_root_file(os.path.join(d, qgc.DY_FILENAME)) for d in dirnames]
        directories = [cu.get_from_tfile(rf, "ZPlusJets") for rf in root_files]

        this_sources = deepcopy(sources)
        for s in this_sources:
            s['label'] = "Z+Jet [MG+PY8] [%s]" % s['label']

        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_dy_compare_kinematics_absolute"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=False,
                                          bin_by='Z')
        
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_dy_compare_kinematics_normalised"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=True,
                                          bin_by='Z')

    # COMPARE JETHT+ZEROBIAS
    if exists_in_all(qgc.JETHT_ZB_FILENAME, dirnames):
        print("Found", qgc.JETHT_ZB_FILENAME, "in all dirs")
        root_files = [cu.open_root_file(os.path.join(d, qgc.JETHT_ZB_FILENAME)) for d in dirnames]
        directories = [cu.get_from_tfile(rf, "Dijet_tighter") for rf in root_files]

        this_sources = deepcopy(sources)
        for s in this_sources:
            s['label'] = "Data [%s]" % s['label']

        directories = [cu.get_from_tfile(rf, "Dijet_Presel") for rf in root_files]
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_jetht_zb_compare_dijet_presel_kinematics_normalised"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=False,
                                          bin_by='ave')

        directories = [cu.get_from_tfile(rf, "Dijet_tighter") for rf in root_files]
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_jetht_zb_compare_dijet_tighter_kinematics_normalised"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=True,
                                          bin_by='ave')

        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_jetht_zb_compare_dijet_tighter_kinematics_abs"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=False,
                                          bin_by='ave')


    # COMPARE SINGLEMU

    # COMPARE HERWIG++ QCD
    if exists_in_all(qgc.QCD_HERWIG_FILENAME, dirnames):
        root_files = [cu.open_root_file(os.path.join(d, qgc.QCD_HERWIG_FILENAME)) for d in dirnames]
        directories = [cu.get_from_tfile(rf, "Dijet_tighter") for rf in root_files]

        this_sources = deepcopy(sources)
        for s in this_sources:
            s['label'] = "QCD [H++] [%s]" % s['label']

        # do_all_1D_projection_plots_in_dir(directories=directories,
        #                                   components_styles_dicts=this_sources,
        #                                   output_dir=os.path.join(output_dir, "plots_qcd_hpp_compare_dijet_tighter_kinematics_normalised"),
        #                                   jet_config_str=jet_config_str,
        #                                   normalise_hists=True,
        #                                   bin_by='ave')

        # directories = [cu.get_from_tfile(rf, "Dijet_eta_ordered") for rf in root_files]
        # do_all_1D_projection_plots_in_dir(directories=directories,
        #                                   components_styles_dicts=this_sources,
        #                                   output_dir=os.path.join(output_dir, "plots_qcd_hpp_compare_dijet_eta_ordered_kinematics_normalised"),
        #                                   jet_config_str=jet_config_str,
        #                                   normalise_hists=True,
        #                                   bin_by='ave')
        
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_qcd_hpp_compare_dijet_tighter_kinematics_absolute"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=False,
                                          bin_by='ave')

        directories = [cu.get_from_tfile(rf, "Dijet_eta_ordered") for rf in root_files]
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_qcd_hpp_compare_dijet_eta_ordered_kinematics_absolute"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=False,
                                          bin_by='ave')

    # COMPARE HERWIG++ DY
    if exists_in_all(qgc.DY_HERWIG_FILENAME, dirnames):
        root_files = [cu.open_root_file(os.path.join(d, qgc.DY_HERWIG_FILENAME)) for d in dirnames]
        directories = [cu.get_from_tfile(rf, "ZPlusJets") for rf in root_files]

        this_sources = deepcopy(sources)
        for s in this_sources:
            s['label'] = "Z+Jet [H++] [%s]" % s['label']

        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_dy_hpp_compare_kinematics_absolute"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=False,
                                          bin_by='Z')
        
        do_all_1D_projection_plots_in_dir(directories=directories,
                                          components_styles_dicts=this_sources,
                                          output_dir=os.path.join(output_dir, "plots_dy_hpp_compare_kinematics_normalised"),
                                          jet_config_str=jet_config_str,
                                          normalise_hists=True,
                                          bin_by='Z')
def do_cut_scan_per_pt(histname, input_filename, output_filename):
    ROOT.gStyle.SetPalette(palette_1D)
    tf = cu.open_root_file(input_filename)
    h3d = cu.get_from_tfile(tf, histname)
    h3d_unweighted = cu.get_from_tfile(tf, histname+"_unweighted")
    if h3d.GetEntries() == 0:
        return

    canv = ROOT.TCanvas(cu.get_unique_str(), "", 800, 600)
    canv.SetTicks(1, 1)
    canv.SetLeftMargin(0.12)
    canv.SetRightMargin(0.12)
    # canv.SetLogz()
    h2d = h3d.Project3D("zy") # var vs pt
    h2d_unweighted = h3d_unweighted.Project3D("zy") # var vs pt

    var_name = os.path.basename(histname).replace("weight_vs_pt_vs_", "").replace("weight_vs_pt_genjet_vs_", "")

    for ibin in range(3, h2d.GetNbinsX()+1):  # iterate over pt bins
        if h2d.Integral(ibin, ibin+1, 0, -1) == 0:
            # data.append(None)
            continue

        pt_low = h3d.GetYaxis().GetBinLowEdge(ibin)
        pt_high = h3d.GetYaxis().GetBinLowEdge(ibin+1)

        data = []
        data_unweighted = []
        # Do integral, error for increasingly looser cuts
        # find maximum in this pt bin
        # yes I probably should collapse to a 1D hist and use GetMaximumBin
        max_val, max_bin = 0, 0
        for icut in range(1, h2d.GetNbinsY()+1):
            val =  h2d.GetBinContent(ibin, icut)
            if val > max_val:
                max_val = val
                max_bin = icut

        for icut in range(max_bin + 5, h2d.GetNbinsY()+2):
            err = array('d', [0])
            count = h2d.IntegralAndError(ibin, ibin+1, 1, icut-1, err)
            if count == 0:
                continue
            data.append([count, err[0], h2d.GetYaxis().GetBinLowEdge(icut)])

            err = array('d', [0])
            count = h2d_unweighted.IntegralAndError(ibin, ibin+1, 1, icut, err)
            data_unweighted.append([count, err[0], h2d.GetYaxis().GetBinLowEdge(icut)])

        # Plot count, rel error vs cut value
        cuts = [d[2] for d in data]
        gr_count = ROOT.TGraph(len(data), array('d', cuts), array('d', [d[0] / data[-1][0] for d in data]))

        gr_count.SetMarkerColor(ROOT.kRed)
        gr_count.SetMarkerStyle(22)
        gr_count.SetLineColor(ROOT.kRed)
        gr_count.SetTitle("%g < p_{T} < %g GeV;%s cut (<);Count (relative to loosest cut)" % (pt_low, pt_high, get_var_str(histname)))

        gr_count_unweighted = ROOT.TGraph(len(data), array('d', cuts), array('d', [d[0] / data_unweighted[-1][0] for d in data_unweighted]))
        gr_count_unweighted.SetMarkerColor(ROOT.kBlack)
        gr_count_unweighted.SetMarkerStyle(23)
        gr_count_unweighted.SetLineColor(ROOT.kBlack)
        gr_count_unweighted.SetTitle("%g < p_{T} < %g GeV;%s cut (<);Count (relative to loosest cut)" % (pt_low, pt_high, get_var_str(histname)))

        leg = ROOT.TLegend(0.7, 0.5, 0.85, 0.65)
        leg.AddEntry(gr_count, "Weighted", "LP")
        leg.AddEntry(gr_count_unweighted, "Unweighted", "LP")

        # gr_rel_err = ROOT.TGraph(len(data), array('d', cuts), array('d', [(d[1] / d[0]) if d[0] != 0 else 0 for d in data ]))
        # gr_rel_err.SetMarkerColor(ROOT.kRed)
        # gr_rel_err.SetMarkerStyle(22)
        # gr_rel_err.SetLineColor(ROOT.kRed)
        # gr_rel_err.SetTitle("%g < p_{T} < %g GeV;%s cut (<);Rel. error" % (pt_low, pt_high, var_name))

        canv.SetLogy(False)

        gr_count.Draw("ALP")
        gr_count_unweighted.Draw("LP")
        gr_count.Draw("LP")
        leg.Draw()
        canv.SaveAs(output_filename.replace(".pdf", "_count_pt%gto%g.pdf" % (pt_low, pt_high)))

        # canv.Clear()
        # gr_rel_err.Draw("ALP")
        # canv.SetLogy()
        # gr_rel_err.GetYaxis().SetMoreLogLabels()
        # canv.SaveAs(output_filename.replace(".pdf", "_rel_err_pt%gto%g.pdf" % (pt_low, pt_high)))


    tf.Close()
def do_cut_roc_per_pt(histname, input_filename, output_filename):
    """Plot fractional # unweighted vs fraction # weighted, for different cuts
    Not a true ROC, but kinda like one
    """
    ROOT.gStyle.SetPalette(palette_1D)
    tf = cu.open_root_file(input_filename)
    h3d = cu.get_from_tfile(tf, histname)
    h3d_unweighted = cu.get_from_tfile(tf, histname+"_unweighted")
    if h3d.GetEntries() == 0:
        return

    canv = ROOT.TCanvas(cu.get_unique_str(), "", 800, 600)
    canv.SetTicks(1, 1)
    canv.SetLeftMargin(0.12)
    canv.SetRightMargin(0.12)
    # canv.SetLogz()
    h2d = h3d.Project3D("zy") # var vs pt
    h2d_unweighted = h3d_unweighted.Project3D("zy") # var vs pt

    var_name = os.path.basename(histname).replace("weight_vs_pt_vs_", "").replace("weight_vs_pt_genjet_vs_", "")

    for ibin in range(3, h2d.GetNbinsX()+1):  # iterate over pt bins
        if h2d.Integral(ibin, ibin+1, 0, -1) == 0:
            # data.append(None)
            continue

        pt_low = h3d.GetYaxis().GetBinLowEdge(ibin)
        pt_high = h3d.GetYaxis().GetBinLowEdge(ibin+1)

        data = []
        data_unweighted = []
        # Do integral, error for increasingly looser cuts
        # find maximum in this pt bin
        # yes I probably should collapse to a 1D hist and use GetMaximumBin
        max_val, max_bin = 0, 0
        for icut in range(1, h2d.GetNbinsY()+1):
            val =  h2d.GetBinContent(ibin, icut)
            if val > max_val:
                max_val = val
                max_bin = icut

        for icut in range(max_bin + 5, h2d.GetNbinsY()+2, 2):
        # for icut in range(2, h2d.GetNbinsY()+2):
            err = array('d', [0])
            count = h2d.IntegralAndError(ibin, ibin+1, 1, icut-1, err)
            if count == 0:
                continue
            data.append([count, err[0], h2d.GetYaxis().GetBinLowEdge(icut)])

            err = array('d', [0])
            count = h2d_unweighted.IntegralAndError(ibin, ibin+1, 1, icut-1, err)
            data_unweighted.append([count, err[0], h2d.GetYaxis().GetBinLowEdge(icut)])

        cuts = np.array([d[2] for d in data][1:])
        # cuts = np.array([d[2] for d in data])

        weighted_fractions = np.array([abs(d[0]-dd[0]) / dd[0] for d, dd in zip(data[:-1], data[1:])])
        unweighted_fractions = np.array([abs(d[0]-dd[0]) / dd[0] for d, dd in zip(data_unweighted[:-1], data_unweighted[1:])])

        non_zero_mask = (unweighted_fractions>0) & (weighted_fractions>0)
        non_zero_weighted = weighted_fractions[non_zero_mask]
        weight_min_pow = math.floor(math.log10(min(non_zero_weighted))) if len(non_zero_weighted) > 0 else -10
        weight_max_pow = math.floor(math.log10(max(non_zero_weighted))) if len(non_zero_weighted) > 0 else 0
        assert(weight_max_pow>=weight_min_pow)

        non_zero_unweighted = unweighted_fractions[non_zero_mask]
        unweight_min_pow = math.floor(math.log10(min(non_zero_unweighted))) if len(non_zero_unweighted) > 0 else -10
        unweight_max_pow = math.floor(math.log10(max(non_zero_unweighted))) if len(non_zero_unweighted) > 0 else 0
        assert(unweight_max_pow>=unweight_min_pow)

        mask = unweighted_fractions < 10**(unweight_min_pow+1)  # last decade of unweighted drops
        mask &= weighted_fractions > 10**(weight_max_pow-1)  # largest decades of weighted drops

        if np.sum(mask) == 0:
            continue

        # weighted_fractions = np.array([d[0] / data[-1][0] for d in data])
        # unweighted_fractions = np.array([d[0] / data_unweighted[-1][0] for d in data_unweighted])

        unweighted_useful = unweighted_fractions[mask & non_zero_mask]
        weighted_useful = weighted_fractions[mask & non_zero_mask]
        if "pt_jet_genHT_ratio" in histname and pt_low == 800:
            print("weight_min_pow:", weight_min_pow)
            print("weight_max_pow:", weight_max_pow)
            print("unweight_min_pow:", unweight_min_pow)
            print("unweight_max_pow:", unweight_max_pow)
            print("unweight_max_pow:", unweight_max_pow)
            print("weighted_useful:", weighted_useful)
            print("unweighted_useful:", unweighted_useful)

        gr_count = ROOT.TGraph(len(unweighted_useful), unweighted_useful, weighted_useful)
        gr_count.SetMarkerColor(ROOT.kRed)
        gr_count.SetMarkerSize(0)
        gr_count.SetMarkerStyle(21)
        gr_count.SetLineColor(ROOT.kRed)
        gr_count.SetTitle("%s, %g < p_{T} < %g GeV;Relative unweighted count;Relative weighted count" % (get_var_str(histname), pt_low, pt_high))
        gr_count.SetTitle("%s, %g < p_{T} < %g GeV;Unweighted fractional drop;Weighted fractional drop" % (get_var_str(histname), pt_low, pt_high))


        # add annotations of cuts
        latexs = []
        for i, cut in enumerate(cuts[mask * non_zero_mask]):
            latex = ROOT.TLatex(gr_count.GetX()[i], gr_count.GetY()[i], " < %.2f" % cut)
            latex.SetTextSize(0.02)
            latex.SetTextColor(ROOT.kBlue)
            gr_count.GetListOfFunctions().Add(latex)
            latexs.append(latex)

        # canv.SetLogx(False)
        # canv.SetLogy(False)

        # ROOT.TGaxis.SetMaxDigits(2)

        # gr_count.Draw("ALP")

        # ROOT.TGaxis.SetMaxDigits(2)
        # unweighted_min = 0.9999

        # # Calculate differences between points
        # unweighted_diffs = unweighted_fractions[1:] - unweighted_fractions[:-1]
        # weighted_diffs = weighted_fractions[1:] - weighted_fractions[:-1]
        # big_diff_inds = []
        # for ind, (u, w) in enumerate(zip(unweighted_diffs, weighted_diffs)):
        #     # look for big diff in weighted frac, small diff in unweighted,
        #     # with a limit on the minimum size of unweighted frac
        #     # (only trying to remove a few events)
        #     if u > 0 and w / u > 100 and u < 0.005 and unweighted_fractions[ind] > unweighted_min:
        #         big_diff_inds.append(ind)

        # if "pt_jet_genHT_ratio" in histname and pt_low == 186:
        #     for u, w in zip(unweighted_diffs, weighted_diffs):
        #         print(u, w)
        #     print(big_diff_inds)

        # make graph of big diff points, add annotations of cuts
        # if len(big_diff_inds) > 0:
        #     gr_big_diffs = ROOT.TGraph(len(big_diff_inds), array('d', [unweighted_fractions[i+1] for i in big_diff_inds]), array('d', [weighted_fractions[i+1] for i in big_diff_inds]))
        #     gr_big_diffs.SetLineWidth(0)
        #     gr_big_diffs.SetMarkerColor(ROOT.kBlue)
        #     gr_big_diffs.SetMarkerStyle(25)
        #     latexs = []
        #     for i, ind in enumerate(big_diff_inds[:]):
        #         latex = ROOT.TLatex(gr_big_diffs.GetX()[i], gr_big_diffs.GetY()[i], " < %.2f" % cuts[ind+1])
        #         latex.SetTextSize(0.02)
        #         latex.SetTextColor(ROOT.kBlue)
        #         gr_big_diffs.GetListOfFunctions().Add(latex)
        #         latexs.append(latex)
        #     gr_big_diffs.Draw("*")

        # gr_count.GetXaxis().SetLimits(unweighted_min, 1)

        # find corresponding value for weighted to set axis range
        # weighted_min = 0
        # for ind, u in enumerate(unweighted_fractions):
        #     if u >= unweighted_min:
        #         weighted_min = weighted_fractions[ind-1]
        #         if ind == len(unweighted_fractions) - 1:
        #             weighted_min = 0
        #         break
        # gr_count.GetHistogram().SetMinimum(weighted_min*1.1 - 0.1)
        # gr_count.GetHistogram().SetMaximum(1)
        # canv.SaveAs(output_filename.replace(".pdf", "_count_pt%gto%g.pdf" % (pt_low, pt_high)))

        # do a version zoomed out
        canv.Clear()
        gr_count.SetMarkerSize(0.5)
        gr_count.Draw("AP")

        # unweighted_min = 0.
        # gr_count.GetXaxis().SetLimits(unweighted_min, 1)
        # weighted_min = 0
        # for ind, u in enumerate(unweighted_fractions):
        #     if u >= unweighted_min:
        #         weighted_min = weighted_fractions[ind-1]
        #         if ind == len(unweighted_fractions) - 1:
        #             weighted_min = 0
        #         break
        # gr_count.GetHistogram().SetMinimum(weighted_min*1.1 - 0.1)

        gr_count.GetXaxis().SetMoreLogLabels()
        gr_count.GetYaxis().SetMoreLogLabels()

        weight_min_pow = math.floor(math.log10(min(weighted_useful))) if len(weighted_useful) > 0 else -10
        weight_max_pow = math.floor(math.log10(max(weighted_useful))) if len(weighted_useful) > 0 else 0

        unweight_min_pow = math.floor(math.log10(min(unweighted_useful))) if len(unweighted_useful) > 0 else -10
        unweight_max_pow = math.floor(math.log10(max(unweighted_useful))) if len(unweighted_useful) > 0 else 0
        gr_count.GetHistogram().SetMinimum(10**weight_min_pow)
        gr_count.GetHistogram().SetMaximum(10**(weight_max_pow+1))
        gr_count.GetXaxis().SetLimits(10**unweight_min_pow, 10**(unweight_max_pow+1))
        canv.SetLogy()
        canv.SetLogx()
        canv.SaveAs(output_filename.replace(".pdf", "_count_pt%gto%g_all.pdf" % (pt_low, pt_high)))


    tf.Close()
def do_counting(zb_input_file,
                jetht_input_file,
                trig_info,
                eta_min=-2.4,
                eta_max=2.4,
                title="",
                output_filename="pt.pdf"):
    """Get hists of pt spectrum for each trigger, plot, and make & return cumulative hist"""

    trig_names = list(trig_info.keys())

    # first get num event passing ZeroBias trigger, up to the first trigger threshold
    h_zb = cu.get_from_tfile(zb_input_file, "ZeroBiasRef/pt_vs_eta_all")
    pt_min, pt_max = 50, trig_info[trig_names[0]]['actual_threshold']
    h_zb_pt = get_xprojection_hist(h_zb, 50, pt_max, eta_min, eta_max)
    h_zb_pt.SetName("ZeroBias")
    h_zb_pt.Sumw2()
    zb_color = 17
    h_zb_pt.SetFillColor(zb_color)
    h_zb_pt.SetLineColor(zb_color)

    print("WARNING: rounding to nearest bin width", h_zb_pt.GetBinWidth(1))

    hst = ROOT.THStack("hst", title + ";Leading jet p_{T} [GeV];N")
    hst.Add(h_zb_pt)

    # for printout at end
    total = []
    this_num_entries = h_zb_pt.Integral()
    total.append(("ZeroBias", this_num_entries))

    leg = ROOT.TLegend(0.6, 0.5, 0.95, 0.88)
    leg.AddEntry(h_zb_pt,
                 "ZeroBias (p_{T} > %g GeV): %g" % (pt_min, this_num_entries),
                 "LF")

    # Now go through each of the triggers and get their hists,
    # using next trigger actual threshold as upper limit on pt
    for this_trig_name, next_trig_name in zip(trig_names[:-1], trig_names[1:]):
        h2d = cu.get_from_tfile(jetht_input_file,
                                this_trig_name + "_v*Ref/pt_vs_eta_all")

        pt_min = trig_info[this_trig_name]['actual_threshold']
        pt_max = trig_info[next_trig_name]['actual_threshold']
        h_pt = get_xprojection_hist(h2d, pt_min, pt_max, eta_min, eta_max)
        h_pt.SetName(this_trig_name)
        h_pt.SetFillColor(trig_info[this_trig_name]['color'])
        h_pt.SetLineColor(trig_info[this_trig_name]['color'])

        this_num_entries = h_pt.Integral()
        total.append((this_trig_name, this_num_entries))

        hst.Add(h_pt)
        leg.AddEntry(h_pt, this_trig_name + ": %g" % (this_num_entries), "LF")

    # Do the last trigger manually
    last_trig_name = trig_names[-1]
    h2d = cu.get_from_tfile(jetht_input_file,
                            last_trig_name + "_v*Ref/pt_vs_eta_all")
    pt_min = trig_info[last_trig_name]['actual_threshold']
    h_pt = get_xprojection_hist(h2d, pt_min, 10000, eta_min, eta_max)
    h_pt.SetName(last_trig_name)
    h_pt.SetFillColor(trig_info[last_trig_name]['color'])
    h_pt.SetLineColor(trig_info[last_trig_name]['color'])

    this_num_entries = h_pt.Integral()
    total.append((last_trig_name, this_num_entries))

    hst.Add(h_pt)
    leg.AddEntry(h_pt, last_trig_name + ": %g" % (this_num_entries), "LF")

    # Now print a plot showing distribution of each trigger
    c = ROOT.TCanvas("c", "", 800, 600)
    c.SetTicks(1, 1)
    # c.SetLogx()
    c.SetLogy()

    hst.Draw("HISTE NOSTACK")
    hst.SetMinimum(10)
    hst.SetMaximum(1E6)

    total_count = sum(x[1] for x in total)
    leg.AddEntry(0, "Total: " + str(total_count), "")
    leg.Draw()
    c.SaveAs(output_filename)

    for count in total:
        print(count[0], "=", count[1])
    print("Total:", total_count)

    # Now make a cumulative histogram of all triggers, need to convert to
    # single TH1 first though from THStack
    htotal = cu.thstack_to_th1(hst)
    cumul = htotal.GetCumulative()
    cumul.SetTitle(title + " (cumulative);Leading jet p_{T} [GeV];N")
    c.Clear()
    c.SetLogx()
    cumul.SetFillStyle(0)
    cumul.SetLineColor(ROOT.kRed)
    cumul.Draw("HISTE")
    cumul.SetMinimum(5E5)
    cumul.SetMaximum(5E7)
    cumul.SetAxisRange(50, 2000, "X")
    c.SaveAs(output_filename.replace(".pdf", "_cumul.pdf"))

    return cumul
Пример #29
0
                "name":
                "%s/%s%s" %
                (source_plot_dir_name, angle.var, pt_region_dict['append']),
                "var_label":
                "%s (%s)" % (angle.name, angle.lambda_str),
                "title":
                "%s\n%s" % (region_label, pt_region_dict['title']),
            }

            # Need both to do corrections
            # this is reco vs gen
            full_var_name = var_dict['name'] + "_response"
            # relative response is reco/gen vs gen
            full_var_name_rel = var_dict['name'] + "_rel_response"

            h2d_orig = cu.get_from_tfile(input_tfile, full_var_name)
            h2d_rel_orig = cu.get_from_tfile(input_tfile, full_var_name_rel)

            h2d_orig.Rebin2D(4, 4)
            h2d_rel_orig.Rebin2D(4, 4)

            metric = "rawmean"
            metric = "median"
            x_values, corrections, reco_hists, response_hists = get_corrections(
                h2d_orig, h2d_rel_orig, metric)

            corrections_graph = ROOT.TGraphErrors(
                len(x_values), array('d', x_values[:, 0]),
                array('d', corrections[:, 0]), array('d', x_values[:, 1]),
                array('d', corrections[:, 1]))
def do_response_plot(tdir,
                     plot_dir,
                     var_name,
                     xlabel,
                     log_var=False,
                     rebinx=1,
                     rebiny=1,
                     do_migration_summary_plots=True,
                     do_resolution_plots=True,
                     save_response_hists=False):
    """Do 2D plots of genjet pt vs recojet pt"""
    h2d_orig = cu.get_from_tfile(tdir, var_name)
    h2d = h2d_orig.Clone(cu.get_unique_str())
    h2d.RebinX(rebinx)
    h2d.RebinY(rebiny)

    pt_regions = [
        {
            "append": "_lowPt",
            "title": "30 < p_{T}^{Reco} < 100 GeV",
        },
        {
            "append": "_midPt",
            "title": "100 < p_{T}^{Reco} < 250 GeV",
        },
        {
            "append": "_highPt",
            "title": "p_{T}^{Reco} > 250 GeV",
        },
    ]

    pt_bin_name = ""
    for region in pt_regions:
        if region['append'] in var_name:
            pt_bin_name = region['title']

    title = "%s;%s (GEN);%s (RECO)" % (pt_bin_name, xlabel, xlabel)
    # title = pt_bin_name

    canv = ROOT.TCanvas(cu.get_unique_str(), "", 800, 600)
    draw_opt = "COLZ TEXT"
    draw_opt = "COLZ"
    pad = ROOT.gPad
    pad.SetBottomMargin(0.12)
    pad.SetLeftMargin(0.13)
    pad.SetRightMargin(0.12)
    canv.SetTicks(1, 1)
    if log_var:
        canv.SetLogx()
        canv.SetLogy()

    # un normalised
    h2d.SetTitle(title)
    h2d.Draw(draw_opt)
    xax = h2d.GetXaxis()
    upper_lim = xax.GetBinUpEdge(xax.GetLast())
    # print(upper_lim)
    # upper_lim = 5000
    title_offset = 1.5
    h2d.SetTitleOffset(title_offset, 'X')
    xax.SetMoreLogLabels()

    yax = h2d.GetYaxis()
    h2d.SetTitleOffset(title_offset * 1.15, 'Y')
    yax.SetMoreLogLabels()
    canv.Update()

    plot_dir = os.path.join(plot_dir, tdir.GetName())
    cu.check_dir_exists_create(plot_dir)
    canv.SaveAs(os.path.join(plot_dir, "%s_linZ.%s" % (var_name, OUTPUT_FMT)))
    canv.SetLogz()
    canv.SaveAs(os.path.join(plot_dir, "%s_logZ.%s" % (var_name, OUTPUT_FMT)))

    #  renorm by row (reco bins)
    canv.SetLogz(0)
    h2d_renorm_y = cu.make_normalised_TH2(h2d,
                                          'Y',
                                          recolour=True,
                                          do_errors=True)
    h2d_renorm_y.SetMarkerSize(0.5)
    h2d_renorm_y.SetMaximum(1)
    h2d_renorm_y.Draw(draw_opt)
    xax = h2d_renorm_y.GetXaxis()
    upper_lim = xax.GetBinUpEdge(xax.GetLast())
    # print(upper_lim)
    # upper_lim = 5000
    title_offset = 1.5
    h2d_renorm_y.SetTitleOffset(title_offset, 'X')
    # xax.SetRangeUser(0, upper_lim)
    xax.SetMoreLogLabels()

    yax = h2d_renorm_y.GetYaxis()
    h2d_renorm_y.SetTitleOffset(title_offset * 1.15, 'Y')
    # yax.SetRangeUser(0, upper_lim)
    yax.SetMoreLogLabels()
    canv.Update()

    canv.SaveAs(
        os.path.join(plot_dir, "%s_renormY_linZ.%s" % (var_name, OUTPUT_FMT)))
    canv.SetLogz()
    h2d_renorm_y.SetMaximum(1)
    h2d_renorm_y.SetMinimum(1E-3)
    canv.SaveAs(
        os.path.join(plot_dir, "%s_renormY_logZ.%s" % (var_name, OUTPUT_FMT)))

    # renorm by column (gen bins)
    canv.Clear()
    canv.SetLogz(0)
    h2d_renorm_x = cu.make_normalised_TH2(h2d,
                                          'X',
                                          recolour=True,
                                          do_errors=True)
    h2d_renorm_x.SetMarkerSize(0.5)
    h2d_renorm_x.SetMaximum(1)
    h2d_renorm_x.Draw(draw_opt)

    xax = h2d_renorm_x.GetXaxis()
    upper_lim = xax.GetBinUpEdge(xax.GetLast())
    # upper_lim = 5000
    title_offset = 1.5
    h2d_renorm_x.SetTitleOffset(title_offset, 'X')
    # xax.SetRangeUser(0, upper_lim)
    xax.SetMoreLogLabels()

    yax = h2d_renorm_x.GetYaxis()
    h2d_renorm_x.SetTitleOffset(title_offset * 1.15, 'Y')
    # yax.SetRangeUser(0, upper_lim)
    yax.SetMoreLogLabels()
    canv.Update()
    canv.SaveAs(
        os.path.join(plot_dir, "%s_renormX_linZ.%s" % (var_name, OUTPUT_FMT)))
    canv.SetLogz()
    h2d_renorm_x.SetMaximum(1)
    h2d_renorm_x.SetMinimum(1E-3)
    canv.SaveAs(
        os.path.join(plot_dir, "%s_renormX_logZ.%s" % (var_name, OUTPUT_FMT)))

    # Now do plot of purity, etc
    if do_migration_summary_plots:
        qgg.make_migration_summary_plot(
            h2d_renorm_x,
            h2d_renorm_y,
            xlabel=xlabel,
            output_filename=os.path.join(
                plot_dir, '%s_migration_summary.%s' % (var_name, OUTPUT_FMT)),
            log_var=log_var)

    # Do resolution plots
    if do_resolution_plots:
        res_rms, rel_res_rms = make_resolution_plots(
            h2d_orig,
            xlabel=xlabel,
            output_filename=os.path.join(
                plot_dir,
                '%s_resolution_summary_rms.%s' % (var_name, OUTPUT_FMT)),
            do_fit=False,
            do_rms=True,
            log_var=log_var,
            save_response_hists=False)

        res_quantiles, rel_res_quantiles = make_resolution_plots(
            h2d_orig,
            xlabel=xlabel,
            output_filename=os.path.join(
                plot_dir,
                '%s_resolution_summary_quantiles.%s' % (var_name, OUTPUT_FMT)),
            do_fit=False,
            do_rms=False,
            quantiles=None,  # auto determine
            log_var=log_var,
            save_response_hists=save_response_hists)

        # compare RMS and quantile results
        conts = [
            Contribution(
                res_rms,
                label="#sqrt{RMS} #pm #frac{#sqrt{#delta RMS}}{#LT %s #GT}" %
                (xlabel),
                line_color=ROOT.kRed,
                marker_color=ROOT.kRed),
            Contribution(res_quantiles,
                         label="68% quantile",
                         line_color=ROOT.kBlue,
                         marker_color=ROOT.kBlue)
        ]
        ylabel = "#frac{#sigma(RECO/GEN)}{GEN}" if "_rel_" in var_name else "#frac{#sigma(RECO)}{GEN}"
        res_plot = Plot(conts,
                        what='graph',
                        xtitle=xlabel,
                        ytitle=ylabel,
                        legend=True,
                        ylim=[0, 3])
        res_plot.legend.SetX1(0.5)
        res_plot.legend.SetX2(0.9)
        res_plot.legend.SetY1(0.7)
        res_plot.legend.SetY2(0.85)
        res_plot.plot('ALP')
        res_plot.save(
            os.path.join(
                plot_dir,
                '%s_resolution_rms_vs_quantiles.%s' % (var_name, OUTPUT_FMT)))