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)
Ejemplo n.º 2
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_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)
Ejemplo n.º 4
0
def do_zerobias_per_run_comparison_plot(dirname_label_pairs,
                                        output_dir,
                                        append="",
                                        title="",
                                        **plot_kwargs):
    runs = [
        (qgc.ZEROBIAS_RUNB_FILENAME, 'B'),
        (qgc.ZEROBIAS_RUNC_FILENAME, 'C'),
        (qgc.ZEROBIAS_RUND_FILENAME, 'D'),
        (qgc.ZEROBIAS_RUNE_FILENAME, 'E'),
        (qgc.ZEROBIAS_RUNF_FILENAME, 'F'),
        (qgc.ZEROBIAS_RUNG_FILENAME, 'G'),
        (qgc.ZEROBIAS_RUNH_FILENAME, 'H'),
    ]
    zb_entry = {
        'label': 'HLT_ZeroBias',
        'color': ROOT.kMagenta - 9,
        # 'scale': 35918219492.947 / 29048.362
        'scale': 1
    }

    for filename, run_period in runs:
        zb_root_files = [
            cu.open_root_file(os.path.join(dl[0], filename))
            for dl in dirname_label_pairs
        ]

        # PT JET 1
        zb_hist_names = [
            "Dijet_jet_hist_0/pt_1", "Dijet_jet_hist_unweighted_0/pt_1"
        ][1:]
        N = len(dirname_label_pairs)
        rebin = 2
        for zb_name in zb_hist_names:
            # add zeero bias ones
            this_data_entries = [
                Contribution(
                    cu.get_from_tfile(zb_root_files[i], zb_name),
                    label=zb_entry['label'] + " Run %s: " % run_period + l,
                    marker_color=zb_entry['color'],
                    line_color=zb_entry['color'],
                    line_style=1 + i,
                    rebin_hist=rebin,
                ) for i, (d, l) in enumerate(dirname_label_pairs)
            ]
            for c in this_data_entries[1:]:
                c.subplot = this_data_entries[0].obj

            plot = Plot(
                this_data_entries,
                what='hist',
                title=title,
                xtitle="p_{T}^{jet 1} [GeV]",
                ytitle="N",
                xlim=[30, 1000],
                ylim=[1E3, None],
                # ylim=[10, 1E8] if 'unweighted' in ht_name else [1, 1E12],
                subplot_type='ratio',
                subplot_title='* / %s' % dirname_label_pairs[0][1],
                **plot_kwargs)
            plot.subplot_maximum_ceil = 10
            plot.default_canvas_size = (800, 600)
            plot.y_padding_max_log = 500
            plot.legend.SetY1(0.7)
            plot.legend.SetY2(0.88)
            plot.legend.SetX1(0.5)
            plot.legend.SetNColumns(2)
            plot.plot("NOSTACK HISTE")
            plot.set_logx(do_more_labels=False)
            plot.set_logy(do_more_labels=False)
            output_filename = "%s/DataJetHTZB-pt_jet1%s_Run%s%s.pdf" % (
                output_dir, "_unweighted" if 'unweighted' in zb_name else "",
                run_period, append)
            plot.save(output_filename)

        # ETA JET 1
        zb_hist_names = ["Dijet_jet_hist_unweighted_0/eta_1"]

        N = len(dirname_label_pairs)
        rebin = 2
        for zb_name in zb_hist_names:
            # add zero bias ones
            this_data_entries = [
                Contribution(
                    cu.get_from_tfile(zb_root_files[i], zb_name),
                    label=zb_entry['label'] + " Run %s: " % run_period + l,
                    marker_color=zb_entry['color'],
                    line_color=zb_entry['color'],
                    line_style=1 + i,
                    rebin_hist=rebin,
                ) for i, (d, l) in enumerate(dirname_label_pairs)
            ]
            for c in this_data_entries[1:]:
                c.subplot = this_data_entries[0].obj

            # plot zb
            plot = Plot(
                this_data_entries,
                what='hist',
                title=title,
                xtitle="y^{jet 1}",
                ytitle="N",
                subplot_type='ratio',
                subplot_title='* / %s' % dirname_label_pairs[0][1],
                # subplot_limits=(0, 5),
                **plot_kwargs)
            plot.subplot_maximum_ceil = 5
            plot.default_canvas_size = (800, 600)
            plot.y_padding_max_log = 500
            plot.legend.SetY1(0.7)
            plot.legend.SetY2(0.88)
            plot.legend.SetX1(0.5)
            plot.legend.SetNColumns(2)
            plot.plot("NOSTACK HISTE")
            output_filename = "%s/DataZB-eta_jet1%s_Run%s%s.pdf" % (
                output_dir, "_unweighted" if 'unweighted' in zb_name else "",
                run_period, append)
            plot.save(output_filename)
Ejemplo n.º 5
0
def do_jetht_trigger_comparison_plot(dirname_label_pairs,
                                     output_dir,
                                     append="",
                                     title="",
                                     **plot_kwargs):
    # Unweighted pt, showing contributions from different triggers
    # Have to add in ZB manually
    zb_entry = {
        'label': 'HLT_ZeroBias',
        'color': ROOT.kMagenta - 9,
        # 'scale': 35918219492.947 / 29048.362
        'scale': 1
    }

    jet_ht_entries = [
        {
            'ind': '0',
            'label': "PFJet40",
            'color': ROOT.kRed,
        },
        {
            'ind': '1',
            'label': "PFJet60",
            'color': ROOT.kBlue,
        },
        {
            'ind': '2',
            'label': "PFJet80",
            'color': ROOT.kGreen + 2,
        },
        {
            'ind': '3',
            'label': "PFJet140",
            'color': ROOT.kViolet + 5,
        },
        {
            'ind': '4',
            'label': "PFJet200",
            'color': ROOT.kOrange,
        },
        {
            'ind': '5',
            'label': "PFJet260",
            'color': ROOT.kTeal,
        },
        {
            'ind': '6',
            'label': "PFJet320",
            'color': ROOT.kViolet,
        },
        {
            'ind': '7',
            'label': "PFJet400",
            'color': ROOT.kOrange - 6
        },
        {
            'ind': '8',
            'label': "PFJet450",
            'color': ROOT.kAzure + 1,
        },
    ]

    # PT JET 1
    zb_hist_names = [
        "Dijet_jet_hist_0/pt_1", "Dijet_jet_hist_unweighted_0/pt_1"
    ]
    jet_ht_hist_names = [
        "Dijet_jet_hist_{ind}/pt_1", "Dijet_jet_hist_unweighted_{ind}/pt_1"
    ]

    zb_root_files = [
        cu.open_root_file(os.path.join(dl[0], qgc.ZB_FILENAME))
        for dl in dirname_label_pairs
    ]
    jetht_root_files = [
        cu.open_root_file(os.path.join(dl[0], qgc.JETHT_FILENAME))
        for dl in dirname_label_pairs
    ]
    N = len(dirname_label_pairs)
    rebin = 2
    for zb_name, ht_name in zip(zb_hist_names, jet_ht_hist_names):
        # add zeero bias ones
        this_data_entries = [
            Contribution(
                cu.get_from_tfile(zb_root_files[i], zb_name),
                label=zb_entry['label'] + ": " + l,
                marker_color=zb_entry['color'],
                line_color=zb_entry['color'],
                line_style=1 + i,
                rebin_hist=rebin,
            ) for i, (d, l) in enumerate(dirname_label_pairs)
        ]
        for c in this_data_entries[1:]:
            c.subplot = this_data_entries[0].obj

        # # add jet ht ones
        for ent in jet_ht_entries:
            histname = ht_name.format(ind=ent['ind'])
            new_entries = [
                Contribution(
                    cu.get_from_tfile(jetht_root_files[i], histname),
                    label=ent['label'] + ": " + l,
                    marker_color=ent['color'],
                    line_color=ent['color'],
                    line_style=1 + i,
                    rebin_hist=rebin,
                ) for i, (d, l) in enumerate(dirname_label_pairs)
            ]
            for c in new_entries[1:]:
                c.subplot = new_entries[0].obj

            this_data_entries.extend(new_entries)

        plot = Plot(
            this_data_entries,
            what='hist',
            title=title,
            ytitle="N",
            xtitle="p_{T}^{jet 1} [GeV]",
            xlim=[30, 1000],
            ylim=[1E3, None],
            # ylim=[10, 1E8] if 'unweighted' in ht_name else [1, 1E12],
            subplot_type='ratio',
            subplot_title='* / %s' % dirname_label_pairs[0][1],
            **plot_kwargs)
        plot.default_canvas_size = (800, 600)
        plot.subplot_maximum_ceil = 10
        plot.y_padding_max_log = 500
        plot.legend.SetY1(0.7)
        plot.legend.SetY2(0.88)
        plot.legend.SetX1(0.5)
        plot.legend.SetNColumns(2)
        plot.plot("NOSTACK HISTE")
        plot.set_logx(do_more_labels=False)
        plot.set_logy(do_more_labels=False)
        output_filename = "%s/DataJetHTZB-pt_jet1%s%s.pdf" % (
            output_dir, "_unweighted" if 'unweighted' in zb_name else "",
            append)
        plot.save(output_filename)

    # ETA JET 1
    zb_hist_names = ["Dijet_jet_hist_unweighted_0/eta_1"]
    jet_ht_hist_names = ["Dijet_jet_hist_unweighted_{ind}/eta_1"]

    zb_root_files = [
        cu.open_root_file(os.path.join(dl[0], qgc.ZB_FILENAME))
        for dl in dirname_label_pairs
    ]
    jetht_root_files = [
        cu.open_root_file(os.path.join(dl[0], qgc.JETHT_FILENAME))
        for dl in dirname_label_pairs
    ]
    N = len(dirname_label_pairs)
    rebin = 2
    for zb_name, ht_name in zip(zb_hist_names, jet_ht_hist_names):
        # add zero bias ones
        this_data_entries = [
            Contribution(
                cu.get_from_tfile(zb_root_files[i], zb_name),
                label=zb_entry['label'] + ": " + l,
                marker_color=zb_entry['color'],
                line_color=zb_entry['color'],
                line_style=1 + i,
                rebin_hist=rebin,
            ) for i, (d, l) in enumerate(dirname_label_pairs)
        ]
        for c in this_data_entries[1:]:
            c.subplot = this_data_entries[0].obj

        # plot zb
        plot = Plot(
            this_data_entries,
            what='hist',
            title=title,
            xtitle="y^{jet 1}",
            ytitle="N",
            subplot_type='ratio',
            subplot_title='* / %s' % dirname_label_pairs[0][1],
            # subplot_limits=(0, 5),
            **plot_kwargs)
        plot.subplot_maximum_ceil = 5
        plot.default_canvas_size = (800, 600)
        plot.y_padding_max_log = 500
        plot.legend.SetY1(0.7)
        plot.legend.SetY2(0.88)
        plot.legend.SetX1(0.5)
        plot.legend.SetNColumns(2)
        plot.plot("NOSTACK HISTE")
        output_filename = "%s/DataZB-eta_jet1%s%s.pdf" % (
            output_dir, "_unweighted" if 'unweighted' in zb_name else "",
            append)
        plot.save(output_filename)

        # add jet ht ones
        for ent in jet_ht_entries:
            histname = ht_name.format(ind=ent['ind'])
            this_data_entries = [
                Contribution(
                    cu.get_from_tfile(jetht_root_files[i], histname),
                    label=ent['label'] + ": " + l,
                    marker_color=ent['color'],
                    line_color=ent['color'],
                    line_style=1 + i,
                    rebin_hist=rebin,
                ) for i, (d, l) in enumerate(dirname_label_pairs)
            ]
            for c in this_data_entries[1:]:
                c.subplot = this_data_entries[0].obj

            plot = Plot(
                this_data_entries,
                what='hist',
                title=title,
                xtitle="y^{jet 1}",
                ytitle="N",
                # xlim=[30, 1000],
                # ylim=[10, 1E8] if 'unweighted' in ht_name else [1, 1E12],
                subplot_type='ratio',
                subplot_title='* / %s' % dirname_label_pairs[0][1],
                **plot_kwargs)
            plot.default_canvas_size = (800, 600)
            plot.y_padding_max_log = 500
            plot.legend.SetY1(0.7)
            plot.legend.SetY2(0.88)
            plot.legend.SetX1(0.5)
            plot.legend.SetNColumns(2)
            plot.plot("NOSTACK HISTE")
            output_filename = "%s/DataJetHTZB-%s_eta_jet1%s%s.pdf" % (
                output_dir, ent['label'],
                "_unweighted" if 'unweighted' in zb_name else "", append)
            plot.save(output_filename)
Ejemplo n.º 6
0
def do_pileup_plot(input_dir, trigger_names, output_filename):
    # get histograms
    hists = [
        cu.grab_obj_from_file(
            os.path.join(input_dir, 'MyDataPileupHistogram_%s.root' % t),
            'pileup') for t in trigger_names
    ]
    h_up = cu.grab_obj_from_file(
        os.path.join(input_dir, 'MyDataPileupHistogram_PFJet500_72383.root'),
        'pileup')
    h_down = cu.grab_obj_from_file(
        os.path.join(input_dir, 'MyDataPileupHistogram_PFJet500_66017.root'),
        'pileup')
    h_down3 = cu.grab_obj_from_file(
        os.path.join(input_dir, 'MyDataPileupHistogram_PFJet500_59650.root'),
        'pileup')
    ratio_up = h_up.Clone()
    ratio_up.Divide(hists[-1])
    ratio_down = h_down.Clone()
    ratio_down.Divide(hists[-1])
    # hists.append(h_up)
    # hists.append(h_down)
    # trigger_names.append('72.383')
    # trigger_names.append('66.017')
    # Create contributions
    mark = cu.Marker()
    n_hists = len(hists)
    conts = [
        Contribution(h,
                     label=t,
                     line_width=2,
                     line_color=cu.get_colour_seq(ind, n_hists),
                     marker_color=cu.get_colour_seq(ind, n_hists),
                     marker_style=m,
                     normalise_hist=True)
        for ind, (h, t, m) in enumerate(
            zip(hists, trigger_names, mark.cycle(cycle_filling=True)))
    ]

    conts.insert(
        -1,
        Contribution(
            h_up,
            label='72.383 (+4.6%)',
            line_width=2,
            line_color=ROOT.kRed,
            marker_color=ROOT.kRed,
            normalise_hist=True,
        ))
    conts.insert(
        -1,
        Contribution(
            h_down,
            label='66.017 (-4.6%)',
            line_width=2,
            line_color=ROOT.kMagenta,
            marker_color=ROOT.kMagenta,
            normalise_hist=True,
        ))
    conts.insert(
        -1,
        Contribution(
            h_down3,
            label='59.650 (-13.8%)',
            line_width=2,
            line_style=1,
            line_color=ROOT.kMagenta + 3,
            marker_color=ROOT.kMagenta + 3,
            normalise_hist=True,
        ))
    print([h.Integral() for h in hists])
    plot = Plot(
        conts,
        what='hist',
        xtitle='Pileup',
        lumi=cu.get_lumi_str(do_dijet=True, do_zpj=True),
        subplot_type='ratio',
        subplot=conts[-1],
    )
    plot.subplot_maximum_ceil = 4.5
    plot.plot("NOSTACK HISTE", "NOSTACK HIST")
    plot.subplot_pad.cd()

    # To shade region between up and down hists, need to create graph with
    # the error bars as the up/down variations

    x_low = np.array([
        ratio_up.GetBinLowEdge(i) for i in range(1,
                                                 ratio_up.GetNbinsX() + 1)
    ],
                     dtype=float)
    x_high = np.array([
        ratio_up.GetBinLowEdge(i) for i in range(2,
                                                 ratio_up.GetNbinsX() + 2)
    ],
                      dtype=float)
    x_mid = 0.5 * (x_low + x_high)
    x_err = x_high - x_low

    y_high = np.array([
        max(ratio_up.GetBinContent(i), ratio_down.GetBinContent(i))
        for i in range(1,
                       ratio_up.GetNbinsX() + 1)
    ])
    y_high -= 1
    y_low = np.array([
        min(ratio_up.GetBinContent(i), ratio_down.GetBinContent(i))
        for i in range(1,
                       ratio_up.GetNbinsX() + 1)
    ])
    y_low = 1 - y_low
    y_mid = np.array([1. for i in range(1, ratio_up.GetNbinsX() + 1)])

    gr = ROOT.TGraphAsymmErrors(len(x_mid), x_mid, y_mid, x_err, x_err, y_low,
                                y_high)
    gr.SetFillColor(ROOT.kGray + 2)
    gr.SetFillStyle(3254)
    gr.Draw('2 SAME')
    plot.subplot_container.Draw("SAME NOSTACK HIST")
    plot.subplot_line.Draw()
    plot.save(output_filename)