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)))
def do_jet_pt_migration_plot(input_filename, directory, title, output_dir): """Do migration stats plot""" tfile = cu.open_root_file(input_filename) h2d = tfile.Get("%s/jet_pt_vs_genjet_pt" % directory) h2d_new = h2d h2d_renorm_y = cu.make_normalised_TH2(h2d_new, 'Y', recolour=False, do_errors=True) h2d_renorm_x = cu.make_normalised_TH2(h2d_new, 'X', recolour=False, do_errors=True) # Plot 2D response matrix plot_jet_pt_response_matrix(h2d_new, h2d_renorm_x, h2d_renorm_y, title, output_dir) # Do migration metrics xlabel = 'p_{T}^{jet} [GeV]' qgp.make_migration_summary_plot(h2d_renorm_x, h2d_renorm_y, xlabel, title=title, log_var=True, output_filename=os.path.join(output_dir, "jet_pt_migration_summary.pdf"), do_reco_updown2=False, do_gen_updown=False) tfile.Close()
def print_plots(directory, output_dir, title=""): plot_names = get_list_of_obj(directory) cu.check_dir_exists_create(output_dir) for pname in plot_names: hist = directory.Get(pname) c = ROOT.TCanvas(str(uuid1()), "", 600, 600) c.SetLogz() hist.SetTitle(title) hist.Draw("COLZ TEXT89") c.SaveAs(os.path.join(output_dir, pname + "." + OUTPUT_FMT)) c.Clear() hist_normed = cu.make_normalised_TH2(hist, "Y", False) hist_normed.SetMinimum(1E-4) hist_normed.Draw("COLZ TEXT89") c.SaveAs(os.path.join(output_dir, pname + "_normed." + OUTPUT_FMT))
def do_2D_plot_with_contours(obj, contour_obj, output_filename, renorm_axis=None, title=None, rebin=None, recolour=True, xlim=None, ylim=None, logx=False, logy=False, logz=False): """Like normal 2D plotter but contour_obj will be plotted using contours""" if rebin: obj.Rebin2D(*rebin) if renorm_axis: obj_renorm = cu.make_normalised_TH2(obj, renorm_axis, recolour) else: obj_renorm = obj if title: obj_renorm.SetTitle(title) canvas = ROOT.TCanvas(ROOT.TUUID().AsString(), "", 800, 800) canvas.SetTicks(1, 1) canvas.SetLeftMargin(0.13) canvas.SetBottomMargin(0.11) if logx: canvas.SetLogx(1) if logy: canvas.SetLogy(1) if logz: canvas.SetLogz(1) obj_renorm.Draw("COLZ") if xlim is not None: obj_renorm.GetXaxis().SetRangeUser(*xlim) if ylim is not None: obj_renorm.GetYaxis().SetRangeUser(*ylim) obj_renorm.GetYaxis().SetTitleOffset(1.7) obj_renorm.GetXaxis().SetTitleOffset(1.2) if contour_obj: contour_obj.Draw("CONT3 SAME") output_filename = os.path.abspath(output_filename) odir = os.path.dirname(output_filename) if not os.path.isdir(odir): os.makedirs(odir) canvas.SaveAs(output_filename)
def do_2d_plot(h2d, output_filename, renorm=None, logx=False, logy=False, logz=False, xtitle=None, ytitle=None, rebinx=1, rebiny=1): canv = ROOT.TCanvas(cu.get_unique_str(), "", 800, 600) canv.SetLogx(logx) canv.SetLogy(logy) canv.SetLogz(logz) h2d_new = h2d.Clone(cu.get_unique_str()) h2d_new.RebinX(rebinx) h2d_new.RebinY(rebiny) if renorm.lower() in ['x', 'y']: h2d_new = cu.make_normalised_TH2(h2d_new, renorm, True) if xtitle: h2d_new.GetXaxis().SetTitle(xtitle); if ytitle: h2d_new.GetYaxis().SetTitle(ytitle); h2d_new.Draw("COLZ") dirname = os.path.dirname(os.path.abspath(output_filename)) cu.check_dir_exists_create(dirname) canv.SaveAs(output_filename)
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()
import argparse import os import ROOT ROOT.PyConfig.IgnoreCommandLineOptions = True ROOT.gROOT.SetBatch(1) ROOT.TH1.SetDefaultSumw2() # ROOT.gStyle.SetOptStat(0) ROOT.gStyle.SetOptFit(1111) # My stuff import common_utils as cu if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--input", help="Input ROOT file with response hist", required=True) parser.add_argument("--inputDir", help="TDirectory in ROOT file", required=True) parser.add_argument("--output", help="Output ROOT file to append hist to", required=True) args = parser.parse_args() hist = cu.grab_obj_from_file(args.input, "%s/pt_jet_response" % (args.inputDir)) hist.SetName("pt_jet_response") hist_rebin = cu.make_normalised_TH2(hist, 'X', recolour=False, do_errors=False) hist_rebin.SetName("pt_jet_response") outf = cu.open_root_file(args.output, "UPDATE") hist_rebin.Write() outf.Close()
def make_plots(h2d, var_dict, plot_dir, append="", plot_migrations=True): """Plot a 2D hist, with copies renormalised by row and by column. Also optionally plot migrations as 1D plot. """ canv = ROOT.TCanvas("c" + cu.get_unique_str(), "", 700, 600) canv.SetTicks(1, 1) if var_dict.get("log", False): canv.SetLogx() canv.SetLogy() pad = ROOT.gPad pad.SetBottomMargin(0.12) pad.SetLeftMargin(0.13) pad.SetRightMargin(0.12) xtitle_offset = 1.4 ytitle_offset = xtitle_offset * 1.1 h2d.SetTitleOffset(xtitle_offset, 'X') h2d.SetTitleOffset(ytitle_offset, 'Y') h2d.SetMinimum(1E-3) if var_dict.get('log', False): h2d.GetXaxis().SetLimits(1, 150) h2d.GetYaxis().SetLimits(1, 150) if "title" in var_dict: h2d.SetTitle(var_dict['title']) h2d.Draw("COLZ") output_filename = os.path.join( plot_dir, var_dict['name'] + "_%s.%s" % (append, OUTPUT_FMT)) output_dir = os.path.dirname(output_filename) if not os.path.isdir(output_dir): os.makedirs(output_dir) canv.SaveAs(output_filename) canv.SetLogz() output_filename = os.path.join( plot_dir, var_dict['name'] + "_%s_logZ.%s" % (append, OUTPUT_FMT)) canv.SaveAs(output_filename) # renorm by row canv.SetLogz(0) h2d_renorm_y = cu.make_normalised_TH2(h2d, 'Y', recolour=False, do_errors=False) marker_size = 0.8 h2d_renorm_y.SetMarkerSize(marker_size) h2d_renorm_y.SetMaximum(1) draw_opt = "COLZ" if plot_migrations: draw_opt += " TEXT45" h2d_renorm_y.SetMinimum(1E-3) h2d_renorm_y.Draw(draw_opt) xtitle_offset = 1.5 h2d_renorm_y.SetTitleOffset(xtitle_offset, 'X') yax = h2d_renorm_y.GetYaxis() h2d_renorm_y.SetTitleOffset(ytitle_offset, 'Y') canv.Update() canv.SaveAs( os.path.join( plot_dir, "%s_%s_renormY_linZ.%s" % (var_dict['name'], append, OUTPUT_FMT))) canv.SetLogz() h2d_renorm_y.SetMaximum(1) h2d_renorm_y.SetMinimum(1E-3) canv.SaveAs( os.path.join( plot_dir, "%s_%s_renormY_logZ.%s" % (var_dict['name'], append, OUTPUT_FMT))) # renorm by column canv.Clear() canv.SetLogz(0) h2d_renorm_x = cu.make_normalised_TH2(h2d, 'X', recolour=False, do_errors=False) h2d_renorm_x.SetMarkerSize(marker_size) h2d_renorm_x.SetMaximum(1) h2d_renorm_x.SetMinimum(1E-3) h2d_renorm_x.Draw(draw_opt) h2d_renorm_x.SetTitleOffset(xtitle_offset, 'X') yax = h2d_renorm_x.GetYaxis() h2d_renorm_x.SetTitleOffset(ytitle_offset, 'Y') yax.SetMoreLogLabels() canv.Update() canv.SaveAs( os.path.join( plot_dir, "%s_%s_renormX_linZ.%s" % (var_dict['name'], append, OUTPUT_FMT))) canv.SetLogz() h2d_renorm_x.SetMaximum(1) h2d_renorm_x.SetMinimum(1E-3) canv.SaveAs( os.path.join( plot_dir, "%s_%s_renormX_logZ.%s" % (var_dict['name'], append, OUTPUT_FMT))) # Plot migrations if plot_migrations: output_filename = os.path.join( plot_dir, "%s_migration_summary.%s" % (var_dict['name'], OUTPUT_FMT)) qgg.make_migration_summary_plot(h2d_renorm_x, h2d_renorm_y, var_dict['var_label'], output_filename)
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)))
angle_str = "p_{T} [GeV]" # put plots in subdir, to avoid overcrowding this_output_dir = "%s/%s/%s" % (output_dir, region['name'], angle_shortname) cu.check_dir_exists_create(this_output_dir) draw_response_matrix(hist_mc_gen_reco_map, region['label'], angle_str, draw_values=False, output_filename="%s/response_map_%s.%s" % (this_output_dir, append, OUTPUT_FMT)) draw_response_matrix(cu.make_normalised_TH2(hist_mc_gen_reco_map, 'X', recolour=False), region['label'], angle_str + " (normalised by gen bin)", draw_values=True, output_filename="%s/response_map_%s_normX.%s" % (this_output_dir, append, OUTPUT_FMT)) # Actually do folding! # -------------------- hist_mc_folded = get_folded_hist(hist_mc_gen_reco_map, hist_mc_gen) # hist_mc_folded = get_folded_hist_root(hist_mc_gen_reco_map, hist_mc_gen) # Do lots of plots # --------------- hist_mc_reco_1d = hist_mc_reco_bg_subtracted if subtract_fakes else hist_mc_reco
def make_mega_maps(filename, var, out_filename): rootfile = cu.open_root_file(filename) outfile = cu.open_root_file(out_filename, "RECREATE") plot_dirname = "Dijet_QG_tighter" plot_dir = cu.get_from_tfile(rootfile, plot_dirname) var_stem = "jet_%s_response_bin_" % (var) all_plot_names = [ x for x in cu.get_list_of_element_names(plot_dir) if var_stem in x ] n_bins = int(sqrt(len(all_plot_names))) if len(all_plot_names) == 0: raise RuntimeError("No response plots in file") mult_hists = { histname: cu.get_from_tfile(rootfile, os.path.join(plot_dirname, histname)) for histname in all_plot_names } example_hist = mult_hists[all_plot_names[0]] n_bins_mult_x = example_hist.GetNbinsX() x_min = example_hist.GetXaxis().GetXmin() x_max = example_hist.GetXaxis().GetXmax() n_bins_mult_y = example_hist.GetNbinsY() y_min = example_hist.GetYaxis().GetXmin() y_max = example_hist.GetYaxis().GetXmax() h_mega = ROOT.TH2F( "mega", ";" + example_hist.GetXaxis().GetTitle() + ";" + example_hist.GetYaxis().GetTitle(), n_bins_mult_x * n_bins, x_min, x_max * n_bins, n_bins_mult_y * n_bins, y_min, y_max * n_bins) print("mega hist has", h_mega.GetNbinsX(), "xbins and", h_mega.GetNbinsY(), "ybins") do_inds = list(range(0, n_bins)) for xind in do_inds: for yind in do_inds: histname = "jet_%s_response_bin_%s_%s" % (var, xind, yind) print(histname) this_hist = mult_hists[histname] x_offset = (xind - do_inds[0]) * n_bins_mult_x y_offset = (yind - do_inds[0]) * n_bins_mult_y for x, y in product(range(1, n_bins_mult_x + 1), range(1, n_bins_mult_y + 1)): h_mega.SetBinContent(x + x_offset, y + y_offset, this_hist.GetBinContent(x, y)) del mult_hists canv = ROOT.TCanvas("c", "", 5000, 5000) canv.SetLogz(1) # h_mega.Draw("COLZ") # canv.SaveAs(os.path.join(os.path.dirname(filename), "mega_map_%s.%s" % (var, OUTPUT_FMT))) # outfile.cd() # h_mega.Write() canv.Clear() h_renormx = cu.make_normalised_TH2(h_mega, "X", False) h_renormx.SetMinimum(1E-5) h_renormx.Draw("COLZ") # h_renormx.SetMinimum() canv.SaveAs( os.path.join(os.path.dirname(filename), "mega_map_%s_renormX.%s" % (var, OUTPUT_FMT))) outfile.cd() h_renormx.Write() del h_renormx # canv.Clear() # h_renormy = cu.make_normalised_TH2(h_mega, "Y", False) # h_renormy.Draw("COLZ") # # h_renormx.SetMinimum() # canv.SaveAs(os.path.join(os.path.dirname(filename), "mega_map_%s_renormY.%s" % (var, OUTPUT_FMT))) rootfile.Close()