def test_hist2dplot(): np.random.seed(0) xedges = np.arange(0, 11.5, 1.5) yedges = [0, 2, 3, 4, 6, 7] x = np.random.normal(5, 1.5, 100) y = np.random.normal(4, 1, 100) H, xedges, yedges = np.histogram2d(x, y, bins=(xedges, yedges)) fig, ax = plt.subplots() hep.hist2dplot(H, xedges, yedges, labels=True) return fig
def test_hist2dplot_cbar_subplots(): np.random.seed(0) xedges = np.arange(0, 11.5, 1.5) yedges = [0, 2, 3, 4, 6, 7] x = np.random.normal(5, 1.5, 100) y = np.random.normal(4, 1, 100) H, xedges, yedges = np.histogram2d(x, y, bins=(xedges, yedges)) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5)) hep.hist2dplot(H, xedges, yedges, labels=True, cbar=True, ax=ax1) hep.hist2dplot(H * 2, xedges, yedges, labels=True, cbar=True, ax=ax2) return fig
def test_inputs_uproot(): import uproot4 from skhep_testdata import data_path fname = data_path("uproot-hepdata-example.root") f = uproot4.open(fname) fig, axs = plt.subplots(1, 2, figsize=(14, 5)) TH1, TH2 = f["hpx"], f["hpxpy"] hep.histplot(TH1, ax=axs[0]) hep.hist2dplot(TH2, ax=axs[1], cbar=False) return fig
def test_inputs_bh(): import boost_histogram as bh hist2d = bh.Histogram(bh.axis.Regular(10, 0.0, 1.0), bh.axis.Regular(10, 0, 1)) hist2d.fill(np.random.normal(0.5, 0.2, 1000), np.random.normal(0.5, 0.2, 1000)) fig, axs = plt.subplots(1, 2, figsize=(14, 5)) hep.histplot(hist2d.project(0), ax=axs[0]) hep.hist2dplot(hist2d, labels=True, cbar=False, ax=axs[1]) return fig
def create_plot2d(hist, save_name): import matplotlib.pyplot as plt import mplhep as hep plt.style.use(hep.style.CMS) # plot ax = plt.gca() hep.hist2dplot(hist, ax=ax) ax.set_xlabel(hist.axes[0].metadata, loc='right') ax.set_ylabel(hist.axes[1].metadata, loc='top') fig = ax.get_figure() fig.savefig(save_name) ax.clear() fig.clear()
def plotMatrix(biasMatrix, funcs, funcDegs, outdir): hep.set_style(hep.style.CMS) fig, ax = plt.subplots() ax.set_xticklabels(funcs) ax.set_yticklabels(funcs) hep.hist2dplot(biasMatrix, labels=True, vmin=-1, vmax=1, cmap='RdBu_r') ax.set_xlabel('gen function', ha='right', x=1) ax.set_ylabel('fit function', ha='right', y=1) #ax.set_title('MC') if not os.path.isdir(outdir): os.makedirs(outdir) for ext in ['png', 'pdf']: fig.savefig( os.path.join( outdir, f'{args.dataOrMC}_' + '_'.join([f'{f}{d}' for f, d in funcDegs.items()]) + f'_r{args.rInjected}_{args.signal}.{ext}'))
def test_hist2dplot_custom_labels(): np.random.seed(0) xedges = np.arange(0, 11.5, 1.5) yedges = [0, 2, 3, 4, 6, 7] x = np.random.normal(5, 1.5, 100) y = np.random.normal(4, 1, 100) H, xedges, yedges = np.histogram2d(x, y, bins=(xedges, yedges)) fig, ax = plt.subplots() @np.vectorize def _fmt(x): return f"${x:.2f}$" hep.hist2dplot(H, xedges, yedges, labels=_fmt(H).T) return fig
def make2Dplot(numx, numy, denx, deny, tagger="", outname="", masks=[]): numx = numx.flatten() numy = numy.flatten() denx = denx.flatten() deny = deny.flatten() if masks != []: numx[masks[0].flatten()] = -1 numy[masks[0].flatten()] = -1 denx[masks[1].flatten()] = -1 deny[masks[1].flatten()] = -1 histnum, binx, biny = np.histogram2d(numx, numy, range=[(0, 1), (0, 1)], bins=(50, 10)) histden, binx, biny = np.histogram2d(denx, deny, range=[(0, 1), (0, 1)], bins=(50, 10)) numm1 = numx[numx < 0].size denm1 = denx[denx < 0].size if batch: pickle.dump( [histnum, numx.size, histden, denx.size, numm1, denm1], open( "%s/%s_%s.pkl" % (outdir, outname, fname.split('/')[-1].rstrip(".root")), "wb")) binfl = "%s/bin.pkl" % outdir if not os.path.isfile(binfl): pickle.dump([binx, biny], open(binfl, "wb")) else: fig, ax = plt.subplots() histnum = histnum / numx.size histden = histden / denx.size ratio = np.where(histden > 0, histnum / histden, 1.) hep.hist2dplot(ratio, binx, biny, vmin=0.5, vmax=1.5) ax.set_ylabel("%s CvsB" % tagger) ax.set_xlabel("%s CvsL" % tagger) hep.cms.label(ax=ax) # hep.r_align() plt.savefig(outname + ".png")
def test_uproot_versions(fig_test, fig_ref): import uproot import uproot4 from skhep_testdata import data_path fname = data_path("uproot-hepdata-example.root") f4 = uproot4.open(fname) f3 = uproot.open(fname) fig_test.set_size_inches(14, 5) fig_ref.set_size_inches(14, 5) test_axs = fig_test.subplots(1, 2) TH1u4, TH2u4 = f4["hpx"], f4["hpxpy"] hep.histplot(TH1u4, ax=test_axs[0]) hep.hist2dplot(TH2u4, ax=test_axs[1], cbar=False) ref_axs = fig_ref.subplots(1, 2) TH1u3, TH2u3 = f3["hpx"], f3["hpxpy"] hep.histplot(TH1u3, ax=ref_axs[0]) hep.hist2dplot(TH2u3, ax=ref_axs[1], cbar=False)
def plotTF_ratio(in_ratio, mask, region): fig, ax = plt.subplots() H = np.ma.masked_where(in_ratio * mask <= 0.01, in_ratio * mask) zmin, zmax = np.floor(10 * np.min(TFres)) / 10, np.ceil( 10 * np.max(TFres)) / 10 zmin, zmax = zmin + 0.001, zmax - 0.001 clim = np.max([.3, np.min([abs(zmin - 1), abs(zmax - 1)])]) ptbins = np.array([450, 500, 550, 600, 675, 800, 1200]) msdbins = np.linspace(40, 201, 24) hep.hist2dplot(H.T, msdbins, ptbins, vmin=1 - clim, vmax=1 + clim, cmap='RdBu_r', cbar=False) cax = hep.make_square_add_cbar(ax, pad=0.2, size=0.5) if abs(1 - zmin) > .3 and abs(1 - zmax) > .3: c_extend = 'both' elif abs(1 - zmin) > .3: c_extend = 'min' elif abs(1 - zmax) > .3: c_extend = 'max' else: c_extend = 'neither' cbar = fig.colorbar(ax.get_children()[0], cax=cax, extend=c_extend) ax.set_xticks(np.arange(40, 220, 20)) ax.tick_params(axis='y', which='minor', left=False, right=False) ax.invert_yaxis() ax.set_title('{} QCD Ratio'.format(region), pad=15, fontsize=26) ax.set_xlabel(r'Jet $\mathrm{m_{SD}}$', ha='right', x=1) ax.set_ylabel(r'Jet $\mathrm{p_{T}}$', ha='right', y=1) cbar.set_label(r'(Pass QCD) / (Fail QCD * eff)', ha='right', y=1) fig.savefig('{}/{}{}.png'.format(args.output_folder, "TF_ratio_", region), bbox_inches="tight")
def plotTF_ratio(in_ratio, mask, region, args=None, zrange=None): fig, ax = plt.subplots() H = np.ma.masked_where(in_ratio * mask <= 0.01, in_ratio * mask) zmin, zmax = np.nanmin(H), np.nanmax(H) if zrange is None: # Scale clim to fit range up to a max of 0.6 clim = np.max([.3, np.min([0.6, 1 - zmin, zmax - 1])]) else: clim = zrange ptbins = np.array([450, 500, 550, 600, 675, 800, 1200]) msdbins = np.linspace(40, 201, 24) hep.hist2dplot(H.T, msdbins, ptbins, vmin=1 - clim, vmax=1 + clim, cmap='RdBu_r', cbar=False) cax = hep.make_square_add_cbar(ax, pad=0.2, size=0.5) if abs(1 - zmin) > .3 and abs(1 - zmax) > .3: c_extend = 'both' elif abs(1 - zmin) > .3: c_extend = 'min' elif abs(1 - zmax) > .3: c_extend = 'max' else: c_extend = 'neither' cbar = fig.colorbar(ax.get_children()[0], cax=cax, extend=c_extend) ax.set_xticks(np.arange(40, 220, 20)) ax.tick_params(axis='y', which='minor', left=False, right=False) ax.invert_yaxis() ax.set_title('{} QCD Ratio'.format(region), pad=15, fontsize=26) ax.set_xlabel(r'Jet $\mathrm{m_{SD}}$', ha='right', x=1) ax.set_ylabel(r'Jet $\mathrm{p_{T}}$', ha='right', y=1) cbar.set_label(r'(Pass QCD) / (Fail QCD * eff)', ha='right', y=1) return ax
def test_binned_from_unbinned_2D(): zfit.run.set_graph_mode(True) n = 100000 mu = zfit.Parameter("mu", 1, 0, 19) sigma = zfit.Parameter("sigma", 6, 0, 120) obsx = zfit.Space("x", (-5, 10)) obsy = zfit.Space("y", (-50, 100)) gaussx = zfit.pdf.Gauss(mu=mu, sigma=sigma, obs=obsx) muy = mu + 3 sigmay = sigma * 20 gaussy = zfit.pdf.Gauss(mu=muy, sigma=sigmay, obs=obsy) gauss2D = zfit.pdf.ProductPDF([gaussx, gaussy]) axisx = zfit.binned.VariableBinning( np.concatenate([np.linspace(-5, 5, 43), np.linspace(5, 10, 30)[1:]], axis=0), name="x", ) axisxhist = hist.axis.Variable( np.concatenate([np.linspace(-5, 5, 43), np.linspace(5, 10, 30)[1:]], axis=0), name="x", ) axisy = zfit.binned.RegularBinning(15, -50, 100, name="y") axisyhist = hist.axis.Regular(15, -50, 100, name="y") obs_binnedx = zfit.Space(["x"], binning=axisx) obs_binnedy = zfit.Space("y", binning=axisy) obs_binned = obs_binnedx * obs_binnedy gauss_binned = BinnedFromUnbinnedPDF(pdf=gauss2D, space=obs_binned, extended=n) values = gauss_binned.rel_counts(obs_binned) # TODO: good test? start = time.time() ntrial = 10 for _ in range(ntrial): values = gauss_binned.rel_counts(obs_binned) print(f"Time taken {(time.time() - start) / ntrial}") hist2d = hist.Hist(axisxhist, axisyhist) nruns = 5 npoints = 5_000_000 for _ in range(nruns): normal2d = np.random.normal( [float(mu), float(muy)], [float(sigma), float(sigmay)], size=(npoints, 2) ) hist2d.fill(*normal2d.T, threads=4) diff = np.abs(values * hist2d.sum() - hist2d.counts()) - 6.5 * np.sqrt( hist2d.counts() ) # 5 sigma for 1000 bins print(diff) np.testing.assert_array_less(diff, 0) sample = gauss_binned.sample(n, limits=obs_binned) hist_sampled = sample.to_hist() hist_pdf = gauss_binned.to_hist() max_error = hist_sampled.values() * 6**2 # 6 sigma away np.testing.assert_array_less( (hist_sampled.values() - hist_pdf.values()) ** 2, max_error ) plt.figure() plt.title("Gauss 2D binned sampled.") mplhep.hist2dplot(hist_sampled) pytest.zfit_savefig() plt.figure() plt.title("Gauss 2D binned plot, irregular (x<4.5 larger bins than x>4.5) binning.") mplhep.hist2dplot(hist_pdf) pytest.zfit_savefig()
htot = f_gen['totxsecs'][:] h = f_gen['xsecs'][:] htot_PDF = f_gen['totxsecs_LHEPdfWeight'][:] h_PDF = f_gen['xsecs_LHEPdfWeight'][:] yBins = f_gen['edges_totxsecs_0'][:] qtBins = f_gen['edges_totxsecs_1'][:] factors = np.array([[20. / 3., 1. / 10], [5., 0.], [20., 0.], [4., 0.], [4., 0.], [5., 0.], [5., 0.], [4., 0.], [1., 0.]]) factors = factors[np.newaxis, np.newaxis, ...] h = (h / htot[..., np.newaxis] + factors[..., 1]) * factors[..., 0] fig, ax1 = plt.subplots() hep.hist2dplot(h[..., 4], yBins, qtBins) plt.tight_layout() plt.savefig("testprefit/A0_{}.png".format(era)) plt.clf() fig, ax1 = plt.subplots() hep.hist2dplot(htot, yBins, qtBins) plt.tight_layout() plt.savefig("testprefit/htot_{}.png".format(era)) plt.clf() factors = factors[..., np.newaxis] h_PDF = h_PDF.reshape(len(yBins) - 1, len(qtBins) - 1, 9, 103) h_PDF = (h_PDF / htot_PDF[:, :, np.newaxis, :] + factors[:, :, :, 1, ...]) * factors[:, :, :, 0, ...]
def plot_phi_reso(big_df, pid, v0, msk_true, msk_pred, msk_both, bins, target='target', outpath='./'): plt.figure(figsize=(4, 4)) ax = plt.axes() hist = np.histogram2d(v0[msk_both, 0], v0[msk_both, 1], bins=(bins["phi_val"], bins["phi_val"])) mplhep.hist2dplot(hist[0], hist[1], hist[2], cmap="Blues", cbar=False) plt.xlabel(bins["true_val"] + " " + bins["phi_xlabel"]) plt.ylabel(bins["pred_val"] + " " + bins["phi_xlabel"]) cms_label() sample_label(ax) particle_label(ax, pid) plt.plot([bins["phi_val"][0], bins["phi_val"][-1]], [bins["phi_val"][0], bins["phi_val"][-1]], color="black", ls="--", lw=0.5) plt.savefig(osp.join(outpath, "phi_2d_pid{}.pdf".format(pid)), bbox_inches="tight") plt.figure(figsize=(4, 4)) ax = plt.axes() plt.hist(v0[msk_true, 0], bins=bins["phi_val"], density=1.0, histtype="step", lw=2, label=bins["true_val"]) plt.hist(v0[msk_pred, 1], bins=bins["phi_val"], density=1.0, histtype="step", lw=2, label=bins["pred_val"]) plt.xlabel(bins["phi_xlabel"]) plt.ylabel("number of particles\n(normalized, a.u.)") plt.legend(frameon=False) cms_label() sample_label(ax) particle_label(ax, pid) plt.savefig(osp.join(outpath, "phi_hist_pid{}.pdf".format(pid)), bbox_inches="tight") ax.set_ylim(ax.get_ylim()[0], 1.5 * ax.get_ylim()[1]) res = (v0[msk_both, 1] - v0[msk_both, 0]) res[np.isnan(res)] = -1 plt.figure(figsize=(4, 4)) ax = plt.axes() ax.text(0.98, 0.98, "avg. $\Delta \phi$\n$%.2f \pm %.2f$" % (np.mean(res), np.std(res)), transform=ax.transAxes, ha="right", va="top") plt.hist(res, bins=bins["phi_res"], density=1.0) plt.xlabel("$\Delta \phi$") plt.ylabel("number of particles\n(normalized, a.u.)") cms_label() sample_label(ax) particle_label(ax, pid) plt.savefig(osp.join(outpath, "phi_ratio_pid{}.pdf".format(pid)), bbox_inches="tight") #efficiency vs fake rate plt.figure(figsize=(4, 4)) ax = plt.axes() big_df["bins_{}_phi".format(target)] = np.searchsorted( bins["phi_val"], big_df["{}_phi".format(target)]) big_df["bins_pred_phi"] = np.searchsorted(bins["phi_val"], big_df["pred_phi"]) vals_eff = big_df[(big_df["{}_pid".format(target)] == pid)].groupby( "bins_{}_phi".format(target))["pred_pid"].apply(get_eff, pid) vals_fake = big_df[(big_df["pred_pid"] == pid)].groupby("bins_pred_phi")[ "{}_pid".format(target)].apply(get_fake, pid) out_eff = np.zeros((len(bins["phi_val"]), 2)) out_fake = np.zeros((len(bins["phi_val"]), 2)) for ib in range(len(bins["phi_val"])): if ib in vals_eff.keys(): out_eff[ib, 0] = vals_eff[ib][0] out_eff[ib, 1] = vals_eff[ib][1] if ib in vals_fake.keys(): out_fake[ib, 0] = vals_fake[ib][0] out_fake[ib, 1] = vals_fake[ib][1] cms_label() sample_label(ax) particle_label(ax, pid) plt.errorbar(bins["phi_val"], out_eff[:, 0], out_eff[:, 1], marker=".", lw=0, elinewidth=1.0, color="green", label="efficiency") plt.ylabel("efficiency\nN(pred|true) / N(true)") ax.set_ylim(0, 1.5) plt.xlabel(bins["phi_xlabel"]) ax2 = ax.twinx() col = "red" plt.errorbar(bins["phi_val"], out_fake[:, 0], out_fake[:, 1], marker=".", lw=0, elinewidth=1.0, color=col, label="fake rate") plt.ylabel("fake rate\nN(true|pred) / N(pred)") plt.xlabel(bins["phi_xlabel"]) ax2.set_ylim(0, 1.5) lines, labels = ax.get_legend_handles_labels() lines2, labels2 = ax2.get_legend_handles_labels() ax2.legend(lines + lines2, labels + labels2, loc=0, frameon=False) plt.savefig(osp.join(outpath, "phi_eff_fake_pid{}.pdf".format(pid)), bbox_inches="tight")
htot = f_gen['totxsecs'][:] h = f_gen['xsecs'][:] htot_PDF = f_gen['totxsecs_LHEPdfWeight'][:] h_PDF = f_gen['xsecs_LHEPdfWeight'][:] yBins = f_gen['edges_totxsecs_0'][:] qtBins = f_gen['edges_totxsecs_1'][:] factors = np.array([[20. / 3., 1. / 10], [5., 0.], [20., 0.], [4., 0.], [4., 0.], [5., 0.], [5., 0.], [4., 0.], [1., 0.]]) factors = factors[np.newaxis, np.newaxis, ...] h = (h / htot[..., np.newaxis] + factors[..., 1]) * factors[..., 0] fig, ax1 = plt.subplots() hep.hist2dplot(h[..., 4], yBins, qtBins) plt.tight_layout() plt.savefig("testprefitWminus/A0_{}.png".format(era)) plt.clf() fig, ax1 = plt.subplots() hep.hist2dplot(htot, yBins, qtBins) plt.tight_layout() plt.savefig("testprefitWminus/htot_{}.png".format(era)) plt.clf() factors = factors[..., np.newaxis] h_PDF = h_PDF.reshape(len(yBins) - 1, len(qtBins) - 1, 9, 103) h_PDF = (h_PDF / htot_PDF[:, :, np.newaxis, :] + factors[:, :, :, 1, ...]) * factors[:, :, :, 0, ...]
import h5py import numpy as np import matplotlib.pyplot as plt import mplhep as hep import ROOT from root_numpy import array2hist from array import array plt.style.use([hep.style.ROOT, hep.style.firamath]) #hep.cms.label(loc=0) hep.cms.text('Simulation') qtBins = ROOT.vector('float')( [0., 4., 8., 12., 16., 20., 24., 28., 32., 40., 60., 100., 200.]) yBins = ROOT.vector('float')([0, 0.4, 0.8, 1.2, 1.6, 2.0, 2.4, 3.0, 6.0]) ptBins = ROOT.vector('float')([25. + i * 0.5 for i in range(61)]) etaBins = ROOT.vector('float')([-2.4 + i * 0.1 for i in range(49)]) fewk = h5py.File('PLOTS/SignalTemplates.hdf5', mode='r+') helxsecs = ["L", "I", "T", "A", "P", "UL"] for hel in helxsecs: h = np.array(fewk['xsecs_{}'.format(hel)][:].reshape( (len(etaBins) - 1, len(ptBins) - 1, len(yBins) - 1, len(qtBins) - 1), order='F'), order='C') hep.hist2dplot(np.sum(h, axis=(0, 1))[:, :], yBins, qtBins) plt.savefig('PLOTS/xsecs_{}.png'.format(hel)) plt.clf() #plt.show()
def run(self, d): file_in = '/scratchnvme/emanca/wproperties-analysis/config/valeriosbins_{}/WPlusJetsToMuNu_costheta.hdf5'.format( self.era) file_in_0J = '/scratchnvme/emanca/wproperties-analysis/config/alternatesample_{}/WJetsToLNu_0J_helweights.hdf5'.format( self.era) file_in_1J = '/scratchnvme/emanca/wproperties-analysis/config/alternatesample_{}/WJetsToLNu_1J_helweights.hdf5'.format( self.era) file_in_2J = '/scratchnvme/emanca/wproperties-analysis/config/alternatesample_{}/WJetsToLNu_2J_helweights.hdf5'.format( self.era) f = h5py.File(file_in, mode='r+') f0J = h5py.File(file_in_0J, mode='r+') f1J = h5py.File(file_in_1J, mode='r+') f2J = h5py.File(file_in_2J, mode='r+') costheta_powheg = f['costheta'][:] costheta_aMC = f0J['costheta'][:] + f1J['costheta'][:] + f2J[ 'costheta'][:] cosThetaBins = np.array( [round(-1. + 2. * i / 100, 2) for i in range(101)]) yBins = np.array([0., 0.4, 0.8, 1.2, 1.6, 2.0, 2.4, 3.0, 10.0]) fig, ax1 = plt.subplots() ax1.set_title("costheta powheg", fontsize=18) hep.hist2dplot(costheta_powheg, yBins, cosThetaBins) plt.tight_layout() plt.savefig("costheta_powheg_{}".format(self.era)) plt.clf() fig, ax1 = plt.subplots() ax1.set_title("costheta aMC@NLO", fontsize=18) hep.hist2dplot(costheta_aMC, yBins, cosThetaBins) plt.tight_layout() plt.savefig("costheta_aMC{}".format(self.era)) plt.clf() h = costheta_aMC / costheta_powheg # print(h) if self.era == "preVFP": @ROOT.Numba.Declare(["float", "float"], "float") def getWeight_preVFP(y, costheta): biny = np.digitize(np.array([y]), yBins)[0] - 1 bincostheta = np.digitize(np.array([costheta]), cosThetaBins)[0] - 1 if not (y == 4.717765 and costheta == 1.): return h[biny, bincostheta] else: return 1. self.d = d self.d = self.d.Define( "ycosthetaweight", "Numba::getWeight_preVFP(Vrap_preFSR_abs,CStheta_preFSR)" ) #.Filter("ycosthetaweight>2.") else: @ROOT.Numba.Declare(["float", "float"], "float") def getWeight_postVFP(y, costheta): biny = np.digitize(np.array([y]), yBins)[0] - 1 bincostheta = np.digitize(np.array([costheta]), cosThetaBins)[0] - 1 if not (y == 4.717765 and costheta == 1.): return h[biny, bincostheta] else: return 1. self.d = d self.d = self.d.Define( "ycosthetaweight", "Numba::getWeight_postVFP(Vrap_preFSR_abs,CStheta_preFSR)" ) #.Filter("ycosthetaweight>2.") # node = self.d.AsNumpy() # print('getWeight({},{}) = {}'.format(node['Vrap_preFSR_abs'],node['CStheta_preFSR'], node['ycosthetaweight'])) return self.d
def plotTF(rootFilePath, nptbins): outdir = makeOutdir(rootFilePath) plt.style.use([hep.cms.style.ROOT, {'font.size': 24}]) plt.switch_backend('agg') rootfile = rt.TFile(rootFilePath) ptdeg = rootFilePath.split('polyDegs')[1][0] rhodeg = rootFilePath.split('polyDegs')[1][1] tf, dataRatio = [], [] for ibin in range(2): #[1,0]: msd_hist = {} data_hist = {} for region in ['Pass', 'Fail']: SorB = 's' p = f'ptbin{ibin}{region}_msd_fit_{SorB}' b_suffix = '_model_bonly__' if SorB == 'b' else '' hist_name = f'pdf_binptbin{ibin}{region}_{b_suffix}Norm[msd]' msd_hist[region] = TH1fromRooObj( rootfile.Get(p).getCurve(hist_name), returnHistArray=True) data_hist[region] = TH1fromRooObj( rootfile.Get(p).getHist(f'h_ptbin{ibin}{region}'), returnHistArray=True) tf.append(msd_hist['Pass'][:, 1] / msd_hist['Fail'][:, 1]) dataRatio.append(data_hist['Pass'][:, 1] / data_hist['Fail'][:, 1]) tf = np.array(tf) dataRatio = np.array(dataRatio) residuals = (tf - dataRatio) / dataRatio msdbins = np.round(msd_hist['Pass'][:, 0]) msdbins = np.append(msdbins, msdbins[-1] + np.diff(msdbins)[0]) msdsampl = msdbins[:-1] + 0.5 * np.diff(msdbins) pt_start = 200 if 'v06' in rootFilePath else 250 ptbins = np.array([pt_start, 300, 2000]) ptsampl = ptbins[:-1] + 0.3 * np.diff(ptbins) sampling = np.meshgrid(msdsampl, ptsampl) rho = 2 * np.log(sampling[0] / sampling[1]) rho_max = 0 if 'v06' in rootFilePath else -1.2 mask = (rho > rho_max) | (rho < -6) for arr in [tf, residuals]: fig, ax = plt.subplots() fig.subplots_adjust(right=.85) ax = hep.cms.cmslabel(data=args.isData, paper=False, year=args.year, ax=ax, loc=1) ptbins[-1] = 400 vmin = np.floor(100 * min(arr[~mask])) / 100 vmax = np.ceil(100 * max(arr[~mask])) / 100 if arr is residuals: colmap = 'RdBu_r' if abs(vmin) > abs(vmax): vmax = abs(vmin) else: vmin = -vmax else: colmap = 'inferno' hist2d = hep.hist2dplot(arr.T, msdbins, ptbins, vmin=vmin, vmax=vmax, cmap=colmap, ax=ax) for ibin in range(len(ptbins) - 1): pt = ptbins[ibin] ptnext = ptbins[ibin + 1] ax.fill_between(msdbins, np.full_like(msdbins, pt), np.full_like(msdbins, ptnext), where=np.append(mask[ibin], True), facecolor='w', edgecolor='k', linewidth=0, hatch='xx') ax.set_title( fr'{"" if args.isData else "MC "}TF{" residuals" if arr is residuals else ""}, $\deg( p_\mathrm{{T}}, \rho ) = ({ptdeg},{rhodeg})$', pad=9, fontsize=22, loc='left') #ax.set_title('2017',pad=9, fontsize=22, loc='right') ax.set_xlabel(r'Jet $\mathrm{m_{SD}}$ [GeV]', ha='right', x=1) ax.set_ylabel(r'Jet $\mathrm{p_{T}}$ [GeV]', ha='right', y=1) ax.yaxis.set_ticks([pt_start, 300]) cax = fig.axes[1] cax.set_ylabel('(TF - data)/data' if arr is residuals else 'TF', ha='right', y=1) for ext in ['pdf', 'png']: fig.savefig( os.path.join( outdir, f'TF{"_residuals" if arr is residuals else ""}.{ext}'))
histConfig = json.load(config_file) histograms = uproot.open(args.input_file) # Create the plots numberOfHists = len(histograms.keys()) for figureNumber, key in enumerate(histograms.keys()): if(args.verbose): print(key) else: print("[" + "H" * figureNumber + "h" * (numberOfHists - figureNumber) + "] %i" % int(figureNumber / numberOfHists * 100) + '%', end="\r") plt.figure(figureNumber) if re.search("_vs_", key): hep.hist2dplot(histograms[key], color = histConfig[key]["color"]) else: hep.histplot(histograms[key], color = histConfig[key]["color"], histtype = histConfig[key]["histtype"]) #plt.legend(fontsize = args.font_size, ncol = args.number_of_cols) plt.ylabel(histConfig[key]["ylabel"]) plt.style.use(hep.style.CMS) if args.cms_label: hep.cms.label() plt.xlabel(histConfig[key]["xlabel"]) plt.savefig(args.output_directory + "/" + histConfig[key]["subdir"] + "/" + re.sub(";1", "", key) + ".png") plt.savefig(args.output_directory + "/" + histConfig[key]["subdir"] + "/" + re.sub(";1", "", key) + ".pdf") plt.yscale("log")
P3w = np.outer(np.sqrt(1. - cosThetaBinsC * cosThetaBinsC), np.cos(phiBinsC)) P4w = np.outer(cosThetaBinsC, np.ones(len(phiBinsC))) P5w = np.outer((1. - cosThetaBinsC * cosThetaBinsC), np.sin(2. * phiBinsC)) P6w = np.outer( 2. * cosThetaBinsC * np.sqrt(1. - cosThetaBinsC * cosThetaBinsC), np.sin(phiBinsC)) P7w = np.outer(np.sqrt(1. - cosThetaBinsC * cosThetaBinsC), np.sin(phiBinsC)) P8w = np.outer(1 + cosThetaBinsC * cosThetaBinsC, np.ones(len(phiBinsC))) wharmonics = [P0w, P1w, P2w, P3w, P4w, P5w, P6w, P7w] hharmonics = [] totalxsec = np.sum(hFullAcc, axis=(2, 3)) fig, ax1 = plt.subplots() ax1.set_title("total xsec", fontsize=18) hep.hist2dplot(totalxsec, yBins, qtBins) plt.savefig("total_xsec") plt.clf() factors = [(20. / 3., 1. / 10), (5., 0.), (20., 0.), (4., 0.), (4., 0.), (5., 0.), (5., 0.), (4., 0.)] for i, hw in enumerate(wharmonics): htmp = np.einsum('ijkm,km->ij', hFullAcc, hw) / totalxsec htmp = factors[i][0] * (htmp + factors[i][1]) hharmonics.append(htmp) fig, ax1 = plt.subplots() ax1.set_title("A{}".format(i), fontsize=18) hep.hist2dplot(htmp, yBins, qtBins) plt.savefig("A{}".format(i)) plt.cla()
bins_dxy = np.geomspace(0.01, 20., num=5) track_pt = np.concatenate(df_bkg['track_pt'].to_numpy().flatten()) track_dxy = np.concatenate(df_bkg['track_dxy'].to_numpy().flatten()) weights = np.concatenate(df_bkg['weight'].to_numpy().flatten()) track_pt_data = np.concatenate(df_data['track_pt'].to_numpy().flatten()) track_dxy_data = np.concatenate(df_data['track_dxy'].to_numpy().flatten()) hist = histogram2d(track_pt, track_dxy, bins_pt, bins_dxy, weights=weights) hist_data = histogram2d(track_pt_data, track_dxy_data, bins_pt, bins_dxy) fig, ax = plt.subplots() fig.suptitle(cut.title, fontsize=30) hep.cms.label(loc=0) hep.hist2dplot(hist_data, bins_pt, bins_dxy, labels=True) ax.set_xticklabels(labels=bins_pt, rotation=45) ax.set_xlabel(r'leading track $p_\mathrm{T}$ [GeV]') ax.set_ylabel(r'leading track $|d_{xy}| [cm]$') ax.set_xscale('log') ax.set_yscale('log') fig.tight_layout() plt.show() plt.savefig(f'tagger_efficiency/counts_numerator_data_{cut.label}.pdf') hist_ratio = [] labels = [] for nums, denoms in zip(hist_data, hist): row = [] label_row = []
def test_simple2d(): fig, ax = plt.subplots() h = [[1, 3, 2], [1, 3, 2]] hep.hist2dplot(h) return fig
fig, ax = plt.subplots(figsize=(12, 12)) if norm: histnum = histnum / numsize histden = histden / densize numm1 = numm1 / numsize denm1 = denm1 / densize # We decided to flip Num and Den, hence things got weird here. ratio = histden / histnum mn = np.nanmean(ratio) print(mn) ratio = np.where(histnum > 0, ratio, -10) m1ratio = denm1 / numm1 if norm: hep.hist2dplot(ratio, binx, biny, vmin=0.5, vmax=1.5, cmap=my_cmap) else: hep.hist2dplot(ratio, binx, biny, vmin=max(mn / 2, 2 * mn - 1), vmax=min(1, mn * 3 / 2), cmap=my_cmap) # pickle.dump([ratio,m1ratio],open(f.rstrip('*')+".pkl",'wb')) ax.set_ylabel("%s CvsB" % tagger) ax.set_xlabel("%s CvsL" % tagger) zlabel = r"$\frac{\#\ %s\ jets\ with\ MUSTATUS\ muNORM}{\#\ %s\ jetsNORM}$" % ( flav, flav) if "nomu" in f: zlabel = zlabel.replace("MUSTATUS", "no")