def plot_rsp_pt(pairs_tree, pt_var='pt', pt_title='p_{T}', pt_min=0, pt_max=400, logZ=True, normX=True, cut=None, cleaning_cut=None, title=None, oDir=os.getcwd(), output='pt_ptRef.png'): """ Plot response vs pt """ cut = cut or '1' cleaning_cut = cleaning_cut or '1' total_cut = '(%s) && (%s)' % (cut, cleaning_cut) title = title or '' c = generate_canvas() if logZ: c.SetLogz() nbins_pt = 200 nbins_rsp, rsp_min, rsp_max = 100, 0, 5 # With cleaning cut hname = 'hrsp_pt' pairs_tree.Draw("rsp:%s>>%s(%d, %f, %f, %d, %f, %f)" % (pt_var, hname, nbins_pt, pt_min, pt_max, nbins_rsp, rsp_min, rsp_max), total_cut, "COLZ") h = ROOT.gROOT.FindObject(hname) h.SetTitle('%s;%s;response (p_{T}^{L1} / p_{T}^{PF})' % (total_cut, pt_title)) h.SetTitleOffset(1.2, 'XY') if normX: hnew = cu.norm_vertical_bins(h) hnew.Draw("COLZ") draw_horizontal_rsp(pt_max) c.SaveAs(os.path.join(oDir, output)) if cleaning_cut != '1': # Inverse of cleaning cut inv_cut = '(%s) && !(%s)' % (cut, cleaning_cut) hname = 'hrsp_pt_diff' pairs_tree.Draw("rsp:%s>>%s(%d, %f, %f, %d, %f, %f)" % (pt_var, hname, nbins_pt, pt_min, pt_max, nbins_rsp, rsp_min, rsp_max), inv_cut, "COLZ") h = ROOT.gROOT.FindObject(hname) h.SetTitle('%s;%s;response (p_{T}^{L1} / p_{T}^{PF})' % (total_cut, pt_title)) h.SetTitleOffset(1.2, 'XY') if normX: hnew = cu.norm_vertical_bins(h) hnew.Draw("COLZ") draw_horizontal_rsp(pt_max) output_name = os.path.splitext(output)[0] + '_diff' + os.path.splitext(output)[1] c.SaveAs(os.path.join(oDir, output_name))
def plot_rsp_Vs_ref(check_file, eta_min, eta_max, normX, logZ, oDir, oFormat="pdf"): """Plot response (l1/ref) Vs ref pt""" app = "_normX" if normX else "" hname = "eta_%g_%g/Histograms/h2d_rsp_gen%s" % (eta_min, eta_max, app) fwd_bin = abs(eta_min)>2.9 if cu.exists_in_file(check_file, hname): h2d_rsp_ref_orig = cu.get_from_file(check_file, hname) else: h2d_rsp_ref_orig = cu.get_from_file(check_file, "eta_%g_%g/Histograms/h2d_rsp_gen" % (eta_min, eta_max)) if normX: h2d_rsp_ref_orig = cu.norm_vertical_bins(h2d_rsp_ref_orig, rescale_peaks=True) h2d_rsp_ref = h2d_rsp_ref_orig.Rebin2D(2, 1, "hnew") c = generate_canvas() if logZ: c.SetLogz() app += "_log" h2d_rsp_ref.SetTitle("%s: %g-%g;%s;%s" % (eta_l1_str, eta_min, eta_max, pt_ref_str, rsp_str)) if normX: h2d_rsp_ref.Draw("COL") else: h2d_rsp_ref.Draw("COLZ") h2d_rsp_ref.SetAxisRange(rsp_min, rsp_max, 'Y') if fwd_bin: h2d_rsp_ref.SetAxisRange(0, 254, 'X') line = ROOT.TLine(0, 1, 1022, 1) line.SetLineStyle(2) line.SetLineWidth(2) line.Draw() c.SaveAs("%s/h2d_rsp_ref_%g_%g%s.%s" % (oDir, eta_min, eta_max, app, oFormat))
def plot_mult_Vs_var(pairs_tree, var, var_min=0, var_max=10, var_title=None, logY=False, logZ=False, normX=False, cut=None, title=None, oDir=os.getcwd(), output='rsp_mult.pdf'): """Plot multiplicities vs variable 2D hist, for each mulitplicity, on one big canvas. var: string of varibale name in tree var_min, var_max: cuts to put on response range var_title: variable description for axis title logZ: make Z axis logartithmic normX: normalise each X bin such that integral of all y values = 1. cut: cut to apply (no need to include var range) title: title to put on each plot oDir: output directory for plot output: output filename """ var_cut = '%s < %d && %s > %d' % (var, var_max, var, var_min) cut = var_cut if not cut else '%s && %s' % (cut, var_cut) var_title = var_title or '' ncol, nrow = 4, 2 c = generate_canvas(width=600 * ncol, height=600 * nrow) c.Divide(ncol, nrow) hists = [] for i, mult in enumerate(multiplicities): c.cd(i + 1) ROOT.gPad.SetTicks(1, 1) if logY: ROOT.gPad.SetLogy() if logZ: ROOT.gPad.SetLogz() hname = 'h2d' + mult # nbins_var = (var_max - var_min) / 0.05 nbins_var = 100 nbins_mult, mult_min, mult_max = 20, 0, 20 pairs_tree.Draw( '%s:%s>>%s(%d, %f, %f, %d, %f, %f)' % (mult, var, hname, nbins_var, var_min, var_max, nbins_mult, mult_min, mult_max), cut, 'GOFF' if normX else 'COLZ') h = ROOT.gROOT.FindObject(hname) h.SetTitle('%s;%s;%s Multiplicity' % (title, var_title, sources[i])) h.SetTitleOffset(1.2, 'XY') if normX: hnew = cu.norm_vertical_bins(h) hnew.Draw('COLZ') ROOT.SetOwnership(hnew, False) # hists.append(hnew) c.SaveAs(os.path.join(oDir, output))
def plot_energy_Vs_var(pairs_tree, var, var_min=0, var_max=10, var_title=None, logY=False, logZ=False, normX=False, cut=None, title=None, oDir=os.getcwd(), output='rsp_energy.pdf'): """Plot energy fractions vs variable 2D hist, for each energy source, on one big canvas. var: string of varibale name in tree var_min, var_max: cuts to put on response range var_title: variable description for axis title logZ: make Z axis logartithmic normX: normalise each X bin such that integral of all y values = 1. cut: cut to apply (no need to include var range) title: title to put on each plot oDir: output directory for plot output: output filename """ var_cut = '%s < %d && %s > %d' % (var, var_max, var, var_min) cut = var_cut if not cut else '%s && %s' % (cut, var_cut) var_title = var_title or '' ncol, nrow = 4, 2 c = generate_canvas(width=600*ncol, height=600*nrow) c.Divide(ncol, nrow) hists = [] for i, fraction in enumerate(fractions): c.cd(i+1) ROOT.gPad.SetTicks(1, 1) if logY: ROOT.gPad.SetLogy() if logZ: ROOT.gPad.SetLogz() hname = 'h2d' + fraction # nbins_var = (var_max - var_min) / 0.05 nbins_var = 100 nbins_frac, fmin, fmax = 200, 0, 1.2 pairs_tree.Draw('%s:%s>>%s(%d, %f, %f, %d, %f, %f)' % (fraction, var, hname, nbins_var, var_min, var_max, nbins_frac, fmin, fmax), cut, 'GOFF' if normX else 'COLZ') h = ROOT.gROOT.FindObject(hname) h.SetTitle('%s;%s;%s Energy Fraction' % (title, var_title, sources[i])) h.SetTitleOffset(1.2, 'XY') if normX: hnew = cu.norm_vertical_bins(h) hnew.Draw('COLZ') ROOT.SetOwnership(hnew, False) # hists.append(hnew) c.SaveAs(os.path.join(oDir, output))
def plot_checks(inputfile, outputfile, absetamin, absetamax, max_pt, pu_min, pu_max): """ Do all the relevant response 1D and 2D hists, for one eta bin. Can optionally impose maximum pt cut on L1 jets (to avoid problems with saturation), and on number of PU vertices. """ print "Doing eta bin: %g - %g, max L1 jet pt: %g" % (absetamin, absetamax, max_pt) # Input tree tree_raw = inputfile.Get("valid") # Output folders output_f = outputfile.mkdir('eta_%g_%g' % (absetamin, absetamax)) output_f_hists = output_f.mkdir("Histograms") # Eta cut string eta_cutStr = " TMath::Abs(eta)<%g && TMath::Abs(eta) > %g " % (absetamax, absetamin) # Pt cut string pt_cutStr = "pt < %g" % max_pt # PU cut string pu_cutStr = "numPUVertices <= %f && numPUVertices >= %f" % (pu_max, pu_min) cutStr = " && ".join([eta_cutStr, pt_cutStr, pu_cutStr]) # Draw response (pT^L1/pT^Gen) for all pt bins tree_raw.Draw("rsp>>hrsp_eta_%g_%g(100,0,5)" % (absetamin, absetamax), cutStr) hrsp_eta = ROOT.gROOT.FindObject("hrsp_eta_%g_%g" % (absetamin, absetamax)) hrsp_eta.SetTitle(";response (p_{T}^{L1}/p_{T}^{Ref});") if absetamin < 2.9: fit_result = hrsp_eta.Fit("gaus", "QER", "", hrsp_eta.GetMean() - hrsp_eta.GetRMS(), hrsp_eta.GetMean() + hrsp_eta.GetRMS()) else: peak = hrsp_eta.GetBinCenter(hrsp_eta.GetMaximumBin()) fit_result = hrsp_eta.Fit("gaus", "QER", "", peak - (0.5 * hrsp_eta.GetRMS()), peak + (0.5 * hrsp_eta.GetRMS())) # mean = hrsp_eta.GetFunction("gaus").GetParameter(1) # err = hrsp_eta.GetFunction("gaus").GetParError(1) output_f_hists.WriteTObject(hrsp_eta) # nb_pt, pt_min, pt_max = 63, 0, 252 # for GCT/Stage 1 nb_pt, pt_min, pt_max = 512, 0, 1024 # for Stage 2 nb_rsp, rsp_min, rsp_max = 100, 0, 5 # Draw rsp (pT^L1/pT^Gen) Vs GenJet pT tree_raw.Draw("rsp:ptRef>>h2d_rsp_gen(%d,%g,%g,%d,%g,%g)" % (nb_pt, pt_min, pt_max, nb_rsp, rsp_min, rsp_max), cutStr) h2d_rsp_gen = ROOT.gROOT.FindObject("h2d_rsp_gen") h2d_rsp_gen.SetTitle(";p_{T}^{Ref} [GeV];response (p_{T}^{L1}/p_{T}^{Ref})") output_f_hists.WriteTObject(h2d_rsp_gen) h2d_rsp_gen_norm = cu.norm_vertical_bins(h2d_rsp_gen) output_f_hists.WriteTObject(h2d_rsp_gen_norm) # Draw rsp (pT^L1/pT^Gen) Vs L1 pT tree_raw.Draw("rsp:pt>>h2d_rsp_l1(%d,%g,%g,%d,%g,%g)" % (nb_pt, pt_min, pt_max, nb_rsp, rsp_min, rsp_max), cutStr) h2d_rsp_l1 = ROOT.gROOT.FindObject("h2d_rsp_l1") h2d_rsp_l1.SetTitle(";p_{T}^{L1} [GeV];response (p_{T}^{L1}/p_{T}^{Ref})") output_f_hists.WriteTObject(h2d_rsp_l1) h2d_rsp_l1_norm = cu.norm_vertical_bins(h2d_rsp_l1) output_f_hists.WriteTObject(h2d_rsp_l1_norm) # Draw pT^Gen Vs pT^L1 tree_raw.Draw("pt:ptRef>>h2d_gen_l1(%d,%g,%g,%d,%g,%g)" % (nb_pt, pt_min, pt_max, nb_pt, pt_min, pt_max), cutStr) h2d_gen_l1 = ROOT.gROOT.FindObject("h2d_gen_l1") h2d_gen_l1.SetTitle(";p_{T}^{Ref} [GeV];p_{T}^{L1} [GeV]") output_f_hists.WriteTObject(h2d_gen_l1)
def make_2d_plot(tree, xvar, xtitle, xbins, xmin, xmax, yvar, ytitle, ybins, ymin, ymax, output_filename, cut='', title='', logz=False, normx=False, rescale_peak=True, horizontal_line=False, diagonal_line=False): """Make a 2D heat map. Parameters ---------- tree : ROOT.TTree TTree with branches to plots xvar : str Variable to plot on x axis xtitle : str Title of x axis xbins : int Number of bins on x axis xmin : int Minimum of x axis range xmax : int Maximum of x axis range yvar : str Varaible to plot on y axis ytitle : str Title of y axis ybins : int Number of bins on y axis ymin : int Minimum of y axis range ymax : int Maximum of y axis range output_filename : str Output filename for plot cut : str, optional Cut string to apply when doing Draw() title : str, optional Title of plot logz : bool, optional Make Z axis log normx : bool, optional Normalise each x axis bin s.t. integral over y bins = 1 rescale_peak : bool, optional (Only applies if nromx=True). Rescale peak value in each x bin to be uniform across all bins, i.e. sets peak color same in all x bins. horizontal_line : bool, optional Draw a horizontal line at y = 1 diagonal_line : bool, optional Draw a diagonal line for y = x Returns ------- str Filename of plot. """ canv = generate_canvas() if logz: canv.SetLogz() hname = str(uuid.uuid1()) if normx and not logz: title += " (NormX)" if normx and logz: title += " (NormX, log Z scale)" h = ROOT.TH2D(hname, ';'.join([title, xtitle, ytitle]), xbins, xmin, xmax, ybins, ymin, ymax) tree.Draw("%s:%s>>%s" % (yvar, xvar, hname), cut, 'COLZ') h.SetTitleOffset(1.15, 'X') h.SetTitleOffset(1.2, 'Y') if normx: h = cu.norm_vertical_bins(h, rescale_peaks=True) h.Draw("COL") line = None if horizontal_line: line = ROOT.TLine(xmin, 1, xmax, 1) if diagonal_line: upper_lim = xmax if ymax > xmax else ymax line = ROOT.TLine(xmin, ymin, upper_lim, upper_lim) if line: line.SetLineWidth(2) line.SetLineStyle(2) line.Draw() out_stem, ext = os.path.splitext(output_filename) app = '' if logz: app += '_log' if normx: app += "_normX" final_filename = out_stem + app + ext canv.SaveAs(final_filename) return final_filename
def plot_checks(inputfile, outputfile, absetamin, absetamax, max_pt, pu_min, pu_max): """ Do all the relevant response 1D and 2D hists, for one eta bin. Can optionally impose maximum pt cut on L1 jets (to avoid problems with saturation), and on number of PU vertices. """ print "Doing eta bin: %g - %g, max L1 jet pt: %g" % (absetamin, absetamax, max_pt) # Input tree tree_raw = inputfile.Get("valid") # Output folders output_f = outputfile.mkdir('eta_%g_%g' % (absetamin, absetamax)) output_f_hists = output_f.mkdir("Histograms") # Eta cut string eta_cutStr = " TMath::Abs(eta)<%g && TMath::Abs(eta) > %g " % (absetamax, absetamin) # Pt cut string pt_cutStr = "pt < %g" % max_pt # PU cut string pu_cutStr = "numPUVertices <= %f && numPUVertices >= %f" % (pu_max, pu_min) # Avoid L1 saturated jets cut (from 2017 any l1 jet with a saturated tower is auto given pt=1024GeV) avoidSaturation_cut = "pt < 1023.1" cutStr = " && ".join( [eta_cutStr, pt_cutStr, pu_cutStr, avoidSaturation_cut]) # Draw response (pT^L1/pT^Gen) for all pt bins tree_raw.Draw("rsp>>hrsp_eta_%g_%g(100,0,5)" % (absetamin, absetamax), cutStr) hrsp_eta = ROOT.gROOT.FindObject("hrsp_eta_%g_%g" % (absetamin, absetamax)) hrsp_eta.SetTitle(";response (p_{T}^{L1}/p_{T}^{Ref});") if absetamin < 2.9: fit_result = hrsp_eta.Fit("gaus", "QER", "", hrsp_eta.GetMean() - hrsp_eta.GetRMS(), hrsp_eta.GetMean() + hrsp_eta.GetRMS()) else: peak = hrsp_eta.GetBinCenter(hrsp_eta.GetMaximumBin()) fit_result = hrsp_eta.Fit("gaus", "QER", "", peak - (0.5 * hrsp_eta.GetRMS()), peak + (0.5 * hrsp_eta.GetRMS())) # mean = hrsp_eta.GetFunction("gaus").GetParameter(1) # err = hrsp_eta.GetFunction("gaus").GetParError(1) output_f_hists.WriteTObject(hrsp_eta) # nb_pt, pt_min, pt_max = 63, 0, 252 # for GCT/Stage 1 nb_pt, pt_min, pt_max = 512, 0, 1024 # for Stage 2 nb_rsp, rsp_min, rsp_max = 100, 0, 5 # Draw rsp (pT^L1/pT^Gen) Vs GenJet pT tree_raw.Draw( "rsp:ptRef>>h2d_rsp_gen(%d,%g,%g,%d,%g,%g)" % (nb_pt, pt_min, pt_max, nb_rsp, rsp_min, rsp_max), cutStr) h2d_rsp_gen = ROOT.gROOT.FindObject("h2d_rsp_gen") h2d_rsp_gen.SetTitle( ";p_{T}^{Ref} [GeV];response (p_{T}^{L1}/p_{T}^{Ref})") output_f_hists.WriteTObject(h2d_rsp_gen) h2d_rsp_gen_norm = cu.norm_vertical_bins(h2d_rsp_gen) output_f_hists.WriteTObject(h2d_rsp_gen_norm) # Draw rsp (pT^L1/pT^Gen) Vs L1 pT tree_raw.Draw( "rsp:pt>>h2d_rsp_l1(%d,%g,%g,%d,%g,%g)" % (nb_pt, pt_min, pt_max, nb_rsp, rsp_min, rsp_max), cutStr) h2d_rsp_l1 = ROOT.gROOT.FindObject("h2d_rsp_l1") h2d_rsp_l1.SetTitle(";p_{T}^{L1} [GeV];response (p_{T}^{L1}/p_{T}^{Ref})") output_f_hists.WriteTObject(h2d_rsp_l1) h2d_rsp_l1_norm = cu.norm_vertical_bins(h2d_rsp_l1) output_f_hists.WriteTObject(h2d_rsp_l1_norm) # Draw pT^Gen Vs pT^L1 tree_raw.Draw( "pt:ptRef>>h2d_gen_l1(%d,%g,%g,%d,%g,%g)" % (nb_pt, pt_min, pt_max, nb_pt, pt_min, pt_max), cutStr) h2d_gen_l1 = ROOT.gROOT.FindObject("h2d_gen_l1") h2d_gen_l1.SetTitle(";p_{T}^{Ref} [GeV];p_{T}^{L1} [GeV]") output_f_hists.WriteTObject(h2d_gen_l1)
def plot_rsp_pt(pairs_tree, pt_var='pt', pt_title='p_{T}', pt_min=0, pt_max=400, logZ=True, normX=True, cut=None, cleaning_cut=None, title=None, oDir=os.getcwd(), output='pt_ptRef.png'): """ Plot response vs pt """ cut = cut or '1' cleaning_cut = cleaning_cut or '1' total_cut = '(%s) && (%s)' % (cut, cleaning_cut) title = title or '' c = generate_canvas() if logZ: c.SetLogz() nbins_pt = 200 nbins_rsp, rsp_min, rsp_max = 100, 0, 5 # With cleaning cut hname = 'hrsp_pt' pairs_tree.Draw( "rsp:%s>>%s(%d, %f, %f, %d, %f, %f)" % (pt_var, hname, nbins_pt, pt_min, pt_max, nbins_rsp, rsp_min, rsp_max), total_cut, "COLZ") h = ROOT.gROOT.FindObject(hname) h.SetTitle('%s;%s;response (p_{T}^{L1} / p_{T}^{PF})' % (total_cut, pt_title)) h.SetTitleOffset(1.2, 'XY') if normX: hnew = cu.norm_vertical_bins(h) hnew.Draw("COLZ") draw_horizontal_rsp(pt_max) c.SaveAs(os.path.join(oDir, output)) if cleaning_cut != '1': # Inverse of cleaning cut inv_cut = '(%s) && !(%s)' % (cut, cleaning_cut) hname = 'hrsp_pt_diff' pairs_tree.Draw( "rsp:%s>>%s(%d, %f, %f, %d, %f, %f)" % (pt_var, hname, nbins_pt, pt_min, pt_max, nbins_rsp, rsp_min, rsp_max), inv_cut, "COLZ") h = ROOT.gROOT.FindObject(hname) h.SetTitle('%s;%s;response (p_{T}^{L1} / p_{T}^{PF})' % (total_cut, pt_title)) h.SetTitleOffset(1.2, 'XY') if normX: hnew = cu.norm_vertical_bins(h) hnew.Draw("COLZ") draw_horizontal_rsp(pt_max) output_name = os.path.splitext(output)[0] + '_diff' + os.path.splitext( output)[1] c.SaveAs(os.path.join(oDir, output_name))