def test_get_y_max(self):
        # First test the three types individually, then combined
        self.assertAlmostEqual(self.func_y_max, ph.get_y_max(self.func))
        self.assertAlmostEqual(self.hist_y_max, ph.get_y_max(self.hist))
        self.assertAlmostEqual(self.graph_y_max, ph.get_y_max(self.graph))
        self.assertAlmostEqual(self.hist2d_y_max, ph.get_y_max(self.hist2d))

        overall_max = max([
            self.graph_y_max, self.func_y_max, self.hist_y_max,
            self.hist2d_y_max
        ])

        self.assertAlmostEqual(
            overall_max,
            ph.get_y_max([self.hist, self.func, self.graph, self.hist2d]))
def make_rej_evs_w_state_prob_plot(rfile, state, var):
    """
    Make a plot showing the state probability of the rejected events in all the
    bins
    """
    hist_base = r'{}_JpsiPt_0_{}_([0-9]+)_state_prob_rej_ev'.format(state, var)
    hists = [rfile.Get(n) for n in list_obj(rfile, 'TH1D', hist_base)]

    can = mkplot(hists,
                 drawOpt='PEX0',
                 xRange=[0, 1.25],
                 xLabel=get_xlab(state),
                 legPos=[0.83, 0.2, 0.94, 0.2 + 0.04 * len(hists)],
                 legEntries=['bin {}'.format(i) for i in xrange(len(hists))],
                 yLabel='rejected events',
                 logy=True,
                 yRange=[0.1, None])
    mkplot(r.TLine(1, 0, 1,
                   get_y_max(hists) * 1.1),
           can=can,
           drawOpt='same',
           attr=[{
               'color': 12,
               'line': 7,
               'width': 2
           }])

    return can
def make_state_prob_plot(rfile, state, var):
    """
    Make a plot comparing (the normalized) state probability distributions in
    all the bins
    """
    hist_base = r'^{}_JpsiPt_0_{}_([0-9]+)_state_prob$'.format(state, var)
    hists = [rfile.Get(n) for n in list_obj(rfile, 'TH1D', hist_base)]
    [h.Scale(1.0 / h.Integral()) for h in hists]

    can = mkplot(hists,
                 drawOpt='hist',
                 xRange=[0, 1.25],
                 xLabel=get_xlab(state),
                 legPos=[0.83, 0.2, 0.94, 0.2 + 0.04 * len(hists)],
                 legOpt='l',
                 legEntries=['bin {}'.format(i) for i in xrange(len(hists))],
                 yLabel='normalized distributions',
                 logy=True,
                 yRange=[0.0003, None])
    mkplot(r.TLine(1, 0, 1,
                   get_y_max(hists) * 1.1),
           can=can,
           drawOpt='same',
           attr=[{
               'color': 12,
               'line': 7,
               'width': 2
           }])

    return can
def make_plot(hists, savename, reference, doleg=False):
    """
    Make plot
    """
    set_TDR_style()

    can = r.TCanvas(create_random_str(16), '', 50, 50, 600, 600)
    can.cd()

    # preliminary collect them and delete entries if necessary
    legentries = []

    with_ref = False
    if reference is not None:
        ref_hist = hists[reference]
        with_ref = True
        rest_hists = [hists[k] for k in hists if k != reference]
        pull_hists = [do_ratio(ref_hist, h) for h in rest_hists]
        legentries = [reference] + [n for n in hists if n != reference]
    else:
        rest_hists = hists.values()
        legentries = hists.keys()

    if doleg:
        for i, entry in enumerate(legentries):
            legentries[i] = entry.replace('jpsi_kin_sel', '').replace('__', '_')
    else:
        legentries = []

    y_max = get_y_max(hists.values()) * 1.1
    leg=setup_legend()

    if with_ref:
        pad = r.TPad('ratio_pad', 'ratio_pad', 0, 0.3, 1, 1)
        r.SetOwnership(pad, False)
        pad.Draw()
        pad.cd()
        # passing all legentries here, since indexing doesn't work with an empty
        # list and mkplot picks only the first in this case
        make_ratio_plot(pad, [ref_hist], leg, legentries,
                        yRange=[0, y_max], attr=gen_attributes, drawOpt='E2',
                        legOpt='F')
        make_ratio_plot(pad, rest_hists, leg, legentries[1:],
                        yRange=[0, y_max], attr=comp_attributes, drawOpt='sameE1',
                        legOpt='PLE')

        can.cd()
        pull_pad = r.TPad('pull_pad', 'pull_pad', 0, 0, 1, 0.3)
        r.SetOwnership(pull_pad, False)
        pull_pad.Draw()
        pull_pad.cd()
        make_ratio_plot(pull_pad, pull_hists, None, [], yRange=[None, None],
                        attr=comp_attributes)

    else:
        make_ratio_plot(can, rest_hists, leg, legentries,
                        yRange=[0, y_max], attr=comp_attributes)

    can.SaveAs(savename)
def make_dlth_plot(ppd_files):
    """Make the plots for Delta lambda theta"""
    ppds = [get_scaled_ppd(f, 'dlth', 200) for f in ppd_files.values()]

    can = mkplot([r.TLine(v, 0, v, get_y_max(ppds) * 1.1) for v in [-1.6, 1.333]],
                 attr=BOUND_ATTR,
                 xRange=[-2, 2], xLabel='#Delta#lambda_{#vartheta}',
                 yRange=[0, get_y_max(ppds) * 1.1], yLabel='PPD [a.u.]')


    leg = setup_legend(0.56, 0.7, 0.88, 0.84)
    leg.SetTextSize(0.0425)
    leg.SetFillStyle(1001)

    can = mkplot(ppds, drawOpt='hist same', can=can,
                 leg=leg, legOpt='L',
                 legEntries=['{} < p_{{T}} < {} GeV'.format(*p) for p in ppd_files.keys()])

    return add_info(can)
def make_dlph_plot(ppd_files):
    """Make the plots for Delta lambda phi"""
    ppds = [get_scaled_ppd(f, 'dlph', 400) for f in ppd_files.values()]

    can = mkplot([r.TLine(v, 0, v, get_y_max(ppds) * 1.1) for v in [-MAX_DLPH, MAX_DLPH]],
                 attr=BOUND_ATTR,
                 xRange=[-0.85, 0.85], xLabel='#Delta#lambda_{#varphi}',
                 yRange=[0, get_y_max(ppds) * 1.1], yLabel='PPD [a.u.]')

    leg = setup_legend(0.18, 0.7, 0.48, 0.84)
    leg.SetTextSize(0.0425)
    leg.SetFillStyle(1001)

    can = mkplot(ppds, drawOpt='hist same', can=can,
                 leg=leg, legOpt='L',
                 legEntries=['{} < p_{{T}} < {} GeV'.format(*p) for p in ppd_files.keys()])
    can.pltables[0].GetXaxis().SetNdivisions(507)

    return add_info(can, 'left')
def make_lph_plot(ppd_files):
    """Make the plots for lph1 ppds"""
    ppds = [get_scaled_ppd(f, 'lph', 50) for f in ppd_files.values()]

    can = mkplot([r.TLine(v, 0, v, get_y_max(ppds) * 1.1) for v in [-1/3., 1/3.]],
                 xRange=[-1, 1], xLabel='#lambda_{#varphi}^{#chi_{c1}}',
                 yRange=[0, get_y_max(ppds) * 1.1], yLabel='PPD [a.u.]',
                 attr=BOUND_ATTR)

    leg = setup_legend(0.39, 0.25, 0.75, 0.39)
    leg.SetTextSize(0.0425)
    leg.SetFillStyle(1001)

    can = mkplot(ppds, drawOpt='hist same', can=can,
                 leg=leg, legOpt='L',
                 legEntries=['{} < p_{{T}} < {} GeV'.format(*p) for p in ppd_files.keys()])
    can.pltables[0].GetYaxis().SetNdivisions(505)
    can.pltables[0].GetXaxis().SetNdivisions(505)

    return add_info(can)
Esempio n. 8
0
def make_lph_plot(hfile):
    """
    Make the lambda_phi 1 plot
    """
    ppd = get_scaled_ppd(hfile, 'lph', 50)
    ppdmax = get_y_max(ppd)
    can = plot_lph(ppd)

    mkplot([r.TLine(v, 0, v, ppdmax * 1.1) for v in [-1./3, 1./3]], can=can,
           drawOpt='same', attr=[{'color': 12, 'line': 7, 'width': 2}])

    make_nice(can)
    return can
Esempio n. 9
0
def make_lth_plot(hfile):
    """
    Make the lambda1 plot
    """
    ppd = get_scaled_ppd(hfile, 'lth', 100)
    ppdmax = get_y_max(ppd)
    can = plot_lth(ppd)

    mkplot(r.TLine(-0.3333, 0, -0.3333, ppdmax * 1.1, ), can=can, drawOpt='same',
           attr=[{'color': 12, 'line': 7, 'width': 2}])

    make_nice(can)
    return can
Esempio n. 10
0
def make_dlph_plot(hfile):
    """
    Make the dlph plot
    """
    ppd = get_scaled_ppd(hfile, 'dlph', 800)
    can = plot_dlph(ppd)

    ppdmax = get_y_max(ppd)

    mkplot([r.TLine(v, 0, v, ppdmax * 1.1) for v in [-0.78, 0.78]], can=can,
           drawOpt='same', attr=[{'color': 12, 'line': 7, 'width': 2}])

    make_nice(can)
    return can
def make_lth_plot(ppd_files):
    """Make the plots for the lth1 ppds"""
    ppds = [get_scaled_ppd(f, 'lth') for f in ppd_files.values()]
    quantiles = [get_quantiles(p, 0.1) for p in ppds]
    ppds = [rebin(p, [(0, 100)]) for p in ppds] # for plotting

    leg = setup_legend(0.18, 0.7, 0.48, 0.84)
    leg.SetTextSize(0.0425)
    leg.SetFillColor(0)
    leg.SetFillStyle(1001)

    # Draw boundaries first to not draw it over the legend in the end
    can = mkplot(r.TLine(-1./3., 0, -1./3., get_y_max(ppds) * 1.1),
                 attr=BOUND_ATTR,
                 xRange=[-1, 1], xLabel='#lambda_{#vartheta}^{#chi_{c1}}',
                 yRange=[0, get_y_max(ppds) * 1.1], yLabel='PPD [a.u.]')

    can = mkplot(ppds, drawOpt='hist same', can=can,
                 leg=leg, legOpt='L',
                 legEntries=['{} < p_{{T}} < {} GeV'.format(*p) for p in ppd_files.keys()])
    can.pltables[0].GetYaxis().SetNdivisions(505)
    can.pltables[0].GetXaxis().SetNdivisions(505)

    mkplot([r.TLine(q, 0, q, 0.01) for q in quantiles], can=can, drawOpt='same')

    arrow_attr = default_attributes(open_markers=False)
    for att in arrow_attr:
        att['fill'] = att['color']
        att['width'] = 2

    mkplot([
        r.TArrow(q, v, q + 0.125, v, 0.025, '<|') for q, v in zip(quantiles, [0.0015, 0.003, 0.0045])
    ],
           can=can, drawOpt='same <|', attr=arrow_attr)

    return add_info(can, 'left')
Esempio n. 12
0
def _make_dlth_plot(hfile, shift=None):
    """
    Make the dlth plot
    """
    ppd = get_scaled_ppd(hfile, 'dlth')
    ppd = rebin(shift_by_median(ppd, shift), [(0, 200)])
    ppdmax = get_y_max(ppd)
    can = mkplot(ppd,
                 xLabel=YLABELS['dlth'],
                 xRange=[-2, 2],
                 drawOpt='hist', yLabel='PPD [a.u.]')

    mkplot([r.TLine(v, 0, v, ppdmax * 1.1) for v in [-1.6, 1.3333]], can=can,
           drawOpt='same', attr=[{'color': 12, 'line': 7, 'width': 2}])

    # ltx = setup_latex()
    # put_on_latex(ltx, [
    #     (0.65, 0.8, '+{:.2f}'.format(np.diff(get_quantiles(ppd, [0.5, 0.84]))[0])),
    #     (0.40, 0.8, '#minus{:.2f}'.format(np.diff(get_quantiles(ppd, [0.16, 0.5]))[0]))
    # ], ndc=True)

    make_nice(can)
    return can
Esempio n. 13
0
def main(args):
    """Main"""
    # need PlotServer here, to keep the root file open
    pserver = PlotServer(args.histfile)

    norm_mode = 'at0' if args.normalize_at_zero else 'nsignal'

    year_trg_ids, leg_entries = get_trigger_year_info(args.input)
    hists = get_plot_hists(pserver,
                           year_trg_ids,
                           args.ratio,
                           args.variable,
                           args.frame,
                           args.ptbin,
                           state=args.state,
                           norm_mode=norm_mode,
                           norm_ratio=args.norm_ratio)

    # split into data and mc hist due to different plotting styles for the two
    data_hists = {k: hists[k] for k in hists if 'data' in k}
    mc_hists = {k: hists[k] for k in hists if 'mc' in k}

    leg = None
    data_leg = []
    mc_leg = []
    if args.legend:
        leg = setup_legend(args.frame, args.ratio)
        if args.ratio:
            data_leg = [leg_entries[(k[0], k[1]), ] for k in data_hists]
            mc_leg = [leg_entries[(k[0], k[1]), ] for k in mc_hists]
        else:
            data_leg = [
                ', '.join([leg_entries[(k[0], k[1]), ], k[2]])
                for k in data_hists
            ]
            data_leg = nice_leg_entries(data_leg)
            mc_leg = [
                ', '.join([leg_entries[(k[0], k[1]), ], k[2]])
                for k in mc_hists
            ]
            mc_leg = nice_leg_entries(mc_leg)

    dpl_attr = [get_plot_attributes(*k) for k in data_hists]
    mpl_attr = [get_plot_attributes(*k) for k in mc_hists]

    y_label = get_ylabel(not args.ratio, args.normalize_at_zero,
                         args.norm_ratio)
    if args.state == 'chib':
        y_label = re.sub(r'c(\d)', r'b\1', y_label)

    x_label = get_xlabel(args.variable, args.frame)

    # to have same y-range for everything, have to do it manually, as
    # mkplot can't handle it in two different calls
    y_max = get_y_max(data_hists.values() + mc_hists.values()) * 1.1

    set_TDR_style()
    can = None  # declare here to be able to switch the two calls below
    can = mkplot(mc_hists.values(),
                 yRange=[0, y_max],
                 drawOpt='E2',
                 attr=mpl_attr,
                 can=can,
                 leg=leg,
                 legEntries=mc_leg,
                 legOpt='F',
                 xLabel=x_label,
                 yLabel=y_label)
    can = mkplot(data_hists.values(),
                 yRange=[0, y_max],
                 drawOpt='E1',
                 attr=dpl_attr,
                 can=can,
                 leg=leg,
                 legEntries=data_leg,
                 legOpt='PLE',
                 xLabel=x_label,
                 yLabel=y_label)

    lumi_text = get_lumi_text(year_trg_ids)
    add_lumi_info(can, lumi_text)
    can.SaveAs(args.output)