def make_2d_contour_plot(input_files, var_pair): """Make one plot of all input_files""" varx, vary = var_pair graphs = [ f.Get('corr_2d_{}_{}_1sigma'.format(varx, vary)) for f in input_files.values() ] # These are possibly stored in reversed order so the axis have to be # flipped if var_pair == ('r_chic1_jpsi', 'r_chic2_jpsi'): # try to get them the other way around graphs_2 = [ f.Get('corr_2d_{}_{}_1sigma'.format(vary, varx)) for f in input_files.values() ] # Check which graphs are not present and replace them with the flipped axis version for i, g in enumerate(graphs): if not g: graphs[i] = flip_axis(graphs_2[i]) if len(graphs) == 1: leg = None leg_entries = None else: leg = setup_legend(0.75, 0.8, 0.92, 0.8 + len(graphs) * 0.035) leg_entries = input_files.keys() can = mkplot(graphs, drawOpt='L', attr=default_attributes(line=1), leg=leg, legEntries=leg_entries, legOpt='L', yLabel=get_label(vary), yRange=get_range(vary), xLabel=get_label(varx), xRange=get_range(varx)) return can
def make_ratio_plot(uncorr_ratio, corr_ratio, variable): """ Make the comparison plot of corrected and uncorrected graphs """ leg = setup_legend(*LEGEND_POS[variable]) leg.SetTextSize(0.03) can = mkplot(corr_ratio, drawOpt='PE', attr=[{ 'color': 1, 'size': 1, 'marker': 20 }], yLabel=YLABELS.get('r_chic2_chic1'), yRange=[0, 0.75], leg=leg, legEntries=['corrected'], **VAR_PLOT[variable]) shift = HORIZONTAL_SHIFT[variable] mkplot([ shift_graph_horizontal(rescale_graph(uncorr_ratio, corr_ratio), shift), shift_graph_horizontal(uncorr_ratio, -shift) ], can=can, drawOpt='samePE', attr=default_attributes(open_markers=True), leg=leg, legEntries=['fitted (scaled)', 'fitted']) add_auxiliary_info(can, 2012, prelim=True) return can
def make_debug_plot(comb_ppd, inputfiles, var): """ Make a plot overlaying the combined ppd and the input ppds """ can = PLOT_FUNCTIONS[var](rebin(comb_ppd, [(0, 200)])) set_color(can.pltables[1], 1) # make the combined ppd black ppds = [get_scaled_ppd(f, var, 200) for f in inputfiles] mkplot(ppds, can=can, drawOpt='samehist', attr=default_attributes(linewidth=1)) return can
def make_ppd_comp_plot(nom_file, var_files, var): """ Make a plot comparing the full ppds directly (shifting all of them by the median of the nominal ppd). """ n_bins = {'dlth': 100, 'dlph': 400, 'dkappa': 400} nom_ppd = get_scaled_ppd(nom_file, var) nom_med = get_quantiles(nom_ppd, 0.5) nom_ppd = shift_by_median(rebin(nom_ppd, [(0, n_bins[var])]), nom_med) nom_ppd.Scale(1.0 / nom_ppd.Integral()) var_ppds = [ get_scaled_ppd(f, var, n_bins[var]) for f in var_files.values() ] var_ppds = [shift_by_median(p, nom_med) for p in var_ppds] [p.Scale(1.0 / p.Integral()) for p in var_ppds] xran = {'dlth': [-2, 2], 'dlph': [-0.5, 0.5], 'dkappa': [-0.5, 0.5]} leg = create_legend(0.69, 0.88, 0.94, len(var_ppds) + 1) label = '{0} - #bar{0}_{{nominal}}'.format(YLABELS.get(var)) can = mkplot(nom_ppd, attr=[{ 'color': 1, 'fillalpha': (1, 0.25) }], drawOpt='hist', yLabel='PPD [a.u.]', xLabel=label, xRange=xran[var], yRange=[0, None], leg=leg, legEntries=['nominal'], legOpt='F') mkplot(var_ppds, can=can, drawOpt='histsame', attr=default_attributes(linewidth=2), leg=leg, legEntries=var_files.keys(), legOpt='L') return can
def make_costh_mu_pt_plot(data): """Make a comparison of costh distributions for different muon pt cuts""" pt_cuts = [3.5, 4, 4.5, 5, 5.5] mu_pt_labels = ['p_{{T}}^{{#mu}} > {:.1f} GeV'.format(p) for p in pt_cuts] hists = [ hist1d(apply_selections(data, single_muon_sel(flat_pt(p, 1.6))).costh_HX_fold.abs(), nbins=24, min=0, max=1) for p in pt_cuts ] [h.Scale(1.0 / h.GetBinContent(1)) for h in hists] can = mkplot(hists, drawOpt='PE', xRange=[0, 1], xLabel=CTH_LAB, attr=default_attributes(size=1.0, width=2, open_markers=False), legPos=(0.65, 0.50, 0.8, 0.92), legEntries=mu_pt_labels, yLabel='normalized to {} = 0 [a.u.]'.format(CTH_LAB)) can.pltables[0].SetNdivisions(505, 'X') can.attached_tobjects[0].SetTextSize(0.05) return can
def make_var_plot(nom_file, var_files, var): """ Make a variation plot comparing the nominal PPD to the variations PPDs """ nom_ppd = get_scaled_ppd(nom_file, var) nom_quants = get_quantiles(nom_ppd, QUANTILES) nom_lo, nom_hi = np.diff(nom_quants) nom_graph = get_nom_graph(len(var_files), nom_lo, nom_hi) # var_graphs = get_var_graphs_wrt_nominal(var_files, nom_quants, var) var_graphs = get_var_graphs_wrt_nominal( var_files, [nom_ppd.GetMean(), nom_ppd.GetRMS()], var) xran = np.max([nom_lo, nom_hi]) * 1.75 yran = [-0.25, len(var_files) - 0.75] can = mkplot(nom_graph, attr=[{ 'fillalpha': (default_colors()[1], 0.5) }], drawOpt='E2', xLabel=get_label(var), xRange=[-xran, xran], yRange=yran) mkplot(r.TLine(0, yran[0], 0, yran[1]), can=can, drawOpt='same', attr=default_attributes(widht=2)[1:]) mkplot(var_graphs, drawOpt='samePE', can=can, attr=VAR_ATTR) phist = can.pltables[0] phist.GetYaxis().SetLabelSize(0) phist.GetYaxis().SetTickSize(0) ltx = setup_latex() put_on_latex(ltx, get_labels(var_files.keys()), ndc=True) ltx.Draw() can.Draw() return can
def main(args): """Main""" dfr = get_dataframe(args.mcfile) costh_bins = get_costh_binning(dfr, args.nbins) h_int = create_mass_hist(dfr) h_int.Scale(1.0 / args.nbins) h_bins = [create_mass_hist(dfr, cbin) for cbin in costh_bins] attr = [{'color': 1, 'marker': 20, 'size': 1.0}] attr += default_attributes(size=1.0, open_markers=True) * 3 legEntries = ['costh integrated'] legEntries += [ '{:.2f} < |cos#vartheta^{{HX}}| < {:.2f}'.format(*cbin) for cbin in costh_bins ] leg = setup_legend(0.675, 0.6, 0.95, 0.94) leg.SetTextSize(0.025) can = r.TCanvas('plot_can', '', 600, 600) can.SetRightMargin(0.02) can.SetTopMargin(0.05) can.SetLeftMargin(0.12) can = mkplot([h_int] + h_bins, can=can, xRange=[3.325, 3.725], yRange=[0.1, None], attr=attr, logy=args.logy, xLabel='M^{#chi}', yLabel='# events', legOpt='PLE', leg=leg, legEntries=legEntries) can.SaveAs(args.output)
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')
import itertools import ROOT as r r.PyConfig.IgnoreCommandLineOptions = True r.gROOT.SetBatch() from utils.PlotServer import PlotServer from utils.plot_helpers import mkplot, default_attributes, get_y_max from utils.misc_helpers import create_random_str, cond_mkdir from utils.setup_plot_style import set_TDR_style # plot gen curves as filled areas gen_attributes = [ {'color': 1, 'size': 0, 'fillalpha': (1, 0.5), 'width': 0} ] comp_attributes = default_attributes(size=1, linewidth=1) # provide consistent styles for the different def get_plot_histos(pserver, input_sets, plot): """ inputsets (list of tuples): which mc, reco, etc. and which selection selections (list), plot (tuple with var, frame, plot) Get all the plots for all selections """ hists = {} for iset, sel in input_sets: input_id = '_'.join([iset, sel]) hists[input_id] = pserver.get_hist(input_id, 2012, '8_Jpsi', 0,
leg = r.TLegend(0.15, 0.15, 0.25, 0.25) leg.SetBorderSize(0) can = r.TCanvas('c', 'c', 600, 600) can.cd() ratio_pad = r.TPad('ratio_pad', '', 0, 0.3, 1, 1) r.SetOwnership(ratio_pad, False) ratio_pad.Draw() ratio_pad.cd() ratio_pad = mkplot([gen_ratio, reco_ratio], yRange=[0, None], drawOpt='E1', legOpt='PLE', attr=default_attributes(size=1.0, linewidth=1), can=ratio_pad, leg=leg, legEntries=['gen', 'reco'], yLabel='N_{#chi_{c2}} / N_{#chi_{c1}}') can.cd() reco_gen_dr = reco_ratio.Clone() reco_gen_dr.Divide(gen_ratio) dr_pad = r.TPad('double_ratio_pad', '', 0, 0, 1, 0.3) r.SetOwnership(dr_pad, False) dr_pad.Draw() reco_gen_dr.GetYaxis().SetTitleSize(0.08)
r.gInterpreter.LoadFile(f'{THIS_DIR}/../src/read_sdcs.h') r.gInterpreter.LoadFile(f'{THIS_DIR}/../interface/nrqcd_helpers.h') r.gInterpreter.LoadText(AUXILIARY_CPP_FUNCS) from ROOT import sdc from ROOT import readPsiSDCs, readChic1SDCs, readChic2SDCs, lth_from_flong, scale_sdc XRANGE = (1.5, 14.5) # Same as in PLB 773 (2017) 476 # Load plotting helper from utils.plot_helpers import mkplot, default_attributes, setup_legend from utils.misc_helpers import cond_mkdir from utils.setup_plot_style import set_basic_style # Consistent attributes for plots involving multiple states ATTRS = { '3S1_1': [default_attributes(size=0.5)[0]], '3S1_8': [default_attributes(size=0.5)[1]], '3PJ_8': [default_attributes(size=0.5)[2]], '1S0_8': [default_attributes(size=0.5)[3]], '3P1_1': [default_attributes(size=0.5)[4]], '3P2_1': [default_attributes(size=0.5)[4]], } # Scale factors for comparability with the Shao SDCs from PLB 773 (2017) 476 RAP_SCALE = 1 / 2.4 * 1.5 SCALE_FACTORS = { '3S1_1': RAP_SCALE * 6, '3S1_8': RAP_SCALE, '3PJ_8': RAP_SCALE, '1S0_8': RAP_SCALE, '3P1_1': RAP_SCALE * 6,
def make_ratio_pull_plot(ratio, analytic_scens, variable): """ Make a comparison plot with an additional pull panel at the bottom """ # Scale all analytic scenarios first chi2_ndfs = get_chi2_ndf(ratio, analytic_scens) can = mkplot([]) # get a canvas that is already a TCanvasWrapper can.cd() rpad = r.TPad('ratio_pad', 'ratio pad', 0, 0.3, 1, 0.98) r.SetOwnership(rpad, False) rpad.SetBottomMargin(0) rpad.Draw() rpad = mkplot(ratio, drawOpt='PE', can=rpad, attr=[{ 'color': 1, 'size': 1, 'marker': 20 }], yLabel=YLABELS.get('r_chic2_chic1'), yRange=[0.001, 0.75], **VAR_PLOT[variable]) leg = setup_legend(0.2, 0.15, 0.65, 0.32) mkplot(analytic_scens.values(), drawOpt='sameL', can=rpad, attr=ANALYTIC_ATTRS, leg=leg, legEntries=analytic_scens.keys(), legOpt='L') can.cd() ppad = r.TPad('pull_pad', 'pull pad', 0, 0, 1, 0.3) r.SetOwnership(ppad, False) ppad.SetTopMargin(0) ppad.SetBottomMargin(0.3) ppad.Draw() # Draw grid first so that it is not overlaying the plotted points xran = VAR_PLOT[variable]['xRange'] ppad = mkplot( r.TLine(xran[0], 0, xran[1], 0), can=ppad, attr=[{ 'color': 1, 'width': 2, 'line': 1 }], yLabel='pulls', yRange=[-4.99, 4.99], # grid=True, **VAR_PLOT[variable]) mkplot([r.TLine(xran[0], v, xran[1], v) for v in [-3, 3]], drawOpt='same', can=ppad, attr=[{ 'color': 12, 'width': 2, 'line': 7 }]) pulls = [pull_graph(ratio, s) for s in analytic_scens.values()] ppad = mkplot(pulls, drawOpt='samePE', can=ppad, attr=default_attributes(open_markers=False), **VAR_PLOT[variable]) phist = ppad.pltables[0] phist.SetNdivisions(505, "XYZ") phist.SetLabelFont(42, "XYZ") phist.SetLabelOffset(0.007, "XYZ") phist.SetLabelSize(0.12, "XYZ") phist.GetXaxis().SetTitleOffset(0.95) phist.GetYaxis().SetTitleOffset(0.52) phist.SetTitleSize(0.14, "XYZ") # ppad.SetGridy() can.add_tobject(rpad) can.add_tobject(ppad) add_auxiliary_info(can, 2012, prelim=True) return can
import ROOT as r r.PyConfig.IgnoreCommandLineOptions = True r.gROOT.SetBatch() from utils.plot_helpers import mkplot, default_attributes, setup_legend from utils.setup_plot_style import add_auxiliary_info, set_TDR_style from utils.plot_decoration import YLABELS, VAR_PLOT from utils.misc_helpers import cond_mkdir_file, create_random_str, fmt_float from utils.graph_utils import pull_graph import utils.misc_helpers utils.misc_helpers.MAX_N_DIGITS = 1 from common_utils import get_graph ANALYTIC_ATTRS = default_attributes(size=0, line=7) LINE_STYLES = [7, 9, 2, 3, 5, 1] for iatt, att in enumerate(ANALYTIC_ATTRS): att['line'] = LINE_STYLES[iatt % len(LINE_STYLES)] def r_costh(set_vals, fix_lambdas=True): """ R(costh) with possible fixed values """ val_to_idx = {'norm': 0, 'lth1': 1, 'lth2': 2} func = r.TF1(create_random_str(), '[0] * (1 + [2] * x[0]*x[0]) / (1 + [1] * x[0]*x[0])', 0, 1) def_vals = {'norm': 1.0, 'lth1': 0, 'lth2': 0} def_vals.update(set_vals)
def main(args): c1f = r.TFile.Open(args.corrmaps1) c2f = r.TFile.Open(args.corrmaps2) corrmap1 = get_corrmap(c1f, 'HX', args.corr_var, args.mask_acc, args.use_acc_only, min_acc=args.min_acc, mask_prec=args.min_prec) corrmap2 = get_corrmap(c2f, 'HX', args.corr_var, args.mask_acc, args.use_acc_only, min_acc=args.min_acc, mask_prec=args.min_prec) corrmap1_PX = get_corrmap(c1f, 'PX', args.corr_var, args.mask_acc, args.use_acc_only, min_acc=args.min_acc, mask_prec=args.min_prec) corrmap2_PX = get_corrmap(c2f, 'PX', args.corr_var, args.mask_acc, args.use_acc_only, min_acc=args.min_acc, mask_prec=args.min_prec) selections = create_selection(args.selection) scenarios = [ ('chic1_R_2o3', 'chic2_R1_2o5_R2_2o5'), ('chic1_R_1', 'chic2_R1_0_R2_1'), ('chic1_R_0', 'chic2_R1_0_R2_0'), ] attr = default_attributes(size=1.0, open_markers=True)[:3] attr += [{'color': 13, 'line': 7, 'width': 2, 'marker': 1}] cond_mkdir(args.outbase) outfile = r.TFile('/'.join([args.outbase, 'costh_ratios_min_acc_{}_min_prec_{}_gen_{}.root'.format(pfloat(args.min_acc), pfloat(args.min_prec), args.gen_frame)]), 'recreate') for scen in scenarios: chi1_d, chi2_d = samples(args.toydatadir, scen, selections) print(scen) hists = OrderedDict() print('uncorr') hists['uncorr'] = (costh_h(chi2_d), costh_h(chi1_d)) print('corr HX') hists['corr HX'] = (costh_h(chi2_d, corrmap2, 'HX', args.corr_var), costh_h(chi1_d, corrmap1, 'HX', args.corr_var)) print('corr PX') hists['corr PX'] = (costh_h(chi2_d, corrmap2_PX, 'PX', args.corr_var), costh_h(chi1_d, corrmap1_PX, 'PX', args.corr_var)) if args.gen_frame == 'HX': ratio_a, delta_lth = analytical_ratio(*scen) if args.gen_frame == 'CS': ratio_a, delta_lth = ratio_shape(*scen) ratios = OrderedDict() for case in hists: ratios[case] = divide(*hists[case]) leg = setup_legend(0.15, 0.15, 0.55, 0.25) leg.SetNColumns(2) plots = scale_0(ratios.values()) leg_entries = ratios.keys() if args.gen_frame == 'HX': plots.append(scale_func(ratio_a)) leg_entries.append('analytical') can = mkplot(plots, attr=attr, xLabel='|cos#vartheta^{HX}|', yLabel='#chi_{c2} / #chi_{c1}', xRange=[0, 1], leg=leg, legEntries=leg_entries, drawOpt='E1', legOpt='PLE') if args.gen_frame == 'CS': can = mkplot(scale_0(ratio_a), attr=[attr[-1]], leg=leg, legEntries=['analytical'], drawOpt='HISTsame', legOpt='PLE', can=can) can.SaveAs(pdfname(args.outbase, args.gen_frame, delta_lth, args.min_prec, args.min_acc)) outfile.cd() for corr, ratio in ratios.iteritems(): ratio.SetName('_'.join([ scen[0], scen[1], 'ratio_costh_HX', corr.replace(' ', '_'), 'gen', args.gen_frame ])) ratio.Write() for corr in hists: hist1, hist2 = hists[corr] hist1.SetName('_'.join([ scen[0], scen[1], 'chi1_costh_HX', corr.replace(' ', '_'), 'gen', args.gen_frame ])) hist1.Write() hist2.SetName('_'.join([ scen[0], scen[1], 'chi2_costh_HX', corr.replace(' ', '_'), 'gen', args.gen_frame ])) hist2.Write() outfile.Close()
from collections import OrderedDict import numpy as np import ROOT as r r.PyConfig.IgnoreCommandLineOptions = True r.gROOT.SetBatch() from utils.plot_helpers import mkplot, default_attributes, setup_legend from utils.setup_plot_style import set_TDR_style from common_helpers import parse_inputs # define attributes for up to four different confidence levels per input CONT_ATTRS = [] CONT_ATTRS.append(default_attributes(line=1, width=2)) CONT_ATTRS.append(default_attributes(line=7, width=2)) CONT_ATTRS.append(default_attributes(line=2, width=2)) CONT_ATTRS.append(default_attributes(line=5, width=2)) # attributes for best fit BF_ATTRS = default_attributes(size=1.5, open_markers=False) for att in BF_ATTRS: att['marker'] = 34 # legend attributes for the contours and the best fit LEG_C_ATTR = [ {'color': 12, 'width': 2, 'line': 1}, {'color': 12, 'width': 2, 'line': 7}, {'color': 12, 'width': 2, 'line': 2}, {'color': 12, 'width': 2, 'line': 5}
r.TLine(1, 1, 1, -0.6), r.TLine(1, 1, -1. / 3., 1), r.TLine(-1. / 3., -0.6, 1, -0.6), r.TLine(-1. / 3., -0.6, -1. / 3., 1) ] def get_jz_graph(): """ Get a graph with the markers for the J_z components """ return r.TGraph(6, np.array([-1. / 3, -1. / 3, -1. / 3, 1, 1, 1]), np.array([-0.6, -1. / 3, 1, -0.6, -1. / 3, 1])) CONT_ATTR = default_attributes(linewidth=4) CONT_ATTR[0]['line'] = 1 CONT_ATTR[1]['line'] = 9 CONT_ATTR[2]['line'] = 2 CONT_ATTR[2]['color'] = 1 def main(args): """Main""" set_TDR_style() fitfile = r.TFile.Open(args.fitfile) cont_1sigma = get_contour(fitfile.Get('chi2_scan_contour_1sigma')) cont_2sigma = get_contour(fitfile.Get('chi2_scan_contour_2sigma')) cont_3sigma = get_contour(fitfile.Get('chi2_scan_contour_3sigma'))
('chic1_R_2o3', 'chic2_R1_2o5_R2_2o5'), # unpolarized ), 'costh': ( ('chic1_R_2o3', 'chic2_R1_2o5_R2_2o5'), # unpolarized ('chic1_R_14o73', 'chic2_R1_0_R2_25o106'), # nrqcd prediction ('chic1_R_0', 'chic2_R1_0_R2_0'), # max negative ('chic1_R_1', 'chic2_R1_0_R2_1') # max positive ) } ANALYSIS_BINNING = { 'phi': parse_binning("0:90,7"), 'costh': parse_binning("0,0.075,0.15,0.225,0.3,0.375,0.45,0.625") } SCEN_ATTR = default_attributes(size=0, line=7) LINE_STYLES = [7, 9, 2, 3, 5, 1] for iatt, att in enumerate(SCEN_ATTR): att['color'] = 12 att['line'] = LINE_STYLES[iatt % len(LINE_STYLES)] RATIO_ATTR = default_attributes(open_markers=False) LEG_POS = {'phi': (0.2, 0.15, 0.9, 0.33), 'costh': (0.2, 0.15, 0.65, 0.3)} def get_func(variable): """ Get the TF1 belonging to the passed variable, without fixing any parameters yet """