예제 #1
0
def plot_weights(sample,variation,indir,outdir):
    '''
    21.10.2020
    Just implemented all weights and uncertainties, version v12
    Goal: show all AK8 jet mass distributions in a single plot. Will be very busy but just want to check if any particular distribution sticks out
    '''
    plt.style.use([hep.cms.style.ROOT, {'font.size': 24}])
    f, ax = plt.subplots()
    hep.cms.label(data=False, paper=False, year=args.year, ax=ax, loc=0)
    with open( glob(os.path.join(indir,variation,f'*{sample}*json'))[0] ) as f:
        hist = json.load(f)
    weight_list = [w.split('hist_weights_')[1].split('_2J')[0] for w in hist if w.startswith('hist_weights_') and w.endswith('2J2WdeltaR_Pass_weights_nominal')]
    for w in ['ones','nominal']:
        if w in weight_list: weight_list.remove(w)
    for w in weight_list:
        wn = f'hist_weights_{w}_2J2WdeltaR_Pass_weights_nominal'
        hep.histplot(hist[wn]['contents'], hist[wn]['edges'], edges=True, label=w)
    plt.xlim(0,1.6)
    # Shrink current axis by 20% to make space for legend
    #plt.semilogy()
    plt.legend()
    ax.set_xlabel('Event weights', ha='right', x=1)
    binsize = hist[wn]['edges'][1] - hist[wn]['edges'][0]
    ax.set_ylabel(f'Entries / {binsize:.2f}', ha='right', y=1)
    #plt.show()
    for ext in ['.png','.pdf']:
      plt.savefig(os.path.join(outdir,f'weights_{args.mask}_{sample}{ext}'), bbox_inches='tight')
예제 #2
0
def plot_corrections(sample,histName,indir,outdir):
    '''
    21.10.2020
    Just implemented all weights and uncertainties, version v12
    Goal: show all AK8 jet mass distributions in a single plot. Will be very busy but just want to check if any particular distribution sticks out
    '''
    rebin_factor = 5
    plt.style.use([hep.cms.style.ROOT, {'font.size': 24}])
    mx = { 'sample' : '', 'max' : 0 }
    mn = { 'sample' : '', 'max' : 1000 }
    with open( glob(os.path.join(indir,'nominal',f'*{sample}*json'))[0] ) as f:
        nominal = json.load(f)[histName]
    nominal['edges'], nominal['contents'], nominal['contents_w2'] = rebin(nominal['edges'], nominal['contents'], nominal['contents_w2'], rebin_factor)
    for variation in [v for v in os.listdir(indir) if v.endswith('Up')]:
        f, ax = plt.subplots()
        hep.cms.label(data=False, paper=False, year=args.year, ax=ax, loc=0)
        variation = variation.replace('Up','')
        if variation.startswith('jesTotal'): continue
        hep.histplot(nominal['contents'], nominal['edges'], edges=True, label='nominal', color='k')
        for v in ['Up','Down']:
            with open( glob(os.path.join(indir,variation+v,f'*{sample}*json'))[0] ) as f:
                hist = json.load(f)[histName]
            hist['edges'], hist['contents'], hist['contents_w2'] = rebin(hist['edges'], hist['contents'], hist['contents_w2'], rebin_factor)
            hep.histplot(hist['contents'], hist['edges'], edges=True, label=variation+v, color=('r' if v=='Up' else 'b'), ls='--')
        plt.legend(fontsize=14)
        plt.xlim(100,160)
        ax.set_xlabel('Leading AK8 jet softdrop mass [GeV]', ha='right', x=1)
        ax.set_ylabel(f'Events / {rebin_factor} GeV', ha='right', y=1)
        #plt.show()
        for ext in ['.png','.pdf']:
          plt.savefig(os.path.join(outdir,f'{args.variable}_{args.mask}_{sample}_{variation}{ext}'), bbox_inches='tight')
        plt.close()
예제 #3
0
def compareShapes(h1,h2,outFile,xTitle="",yTitle="",yRange=[],xRange=[],log=False,label1="",label2="",labelR="Data/MC",data=False):
    hRpf_mj = createRatio(h1,h2)
    hRatio = []
    hRatioErrors = []
    hDataErrors = []
    hData = []
    hDataCentres = []
    for i in range(1,hRpf_mj.GetNbinsX()+1):
        hRatio.append(hRpf_mj.GetBinContent(i))
        hRatioErrors.append(hRpf_mj.GetBinError(i))
    for i in range(1,h2.GetNbinsX()+1):
        hData.append(h2.GetBinContent(i))
        hDataCentres.append(h2.GetBinCenter(i))
        hDataErrors.append(h2.GetBinError(i))
    #numpy histograms
    h1, h1Edges = hist2array(h1,return_edges=True)
    h2, h2Edges = hist2array(h2,return_edges=True)
    hRatio = np.asarray(hRatio)
    hRatioErrors = np.asarray(hRatioErrors)
    centresRatio = (h1Edges[0][:-1] + h1Edges[0][1:])/2.

    plt.style.use([hep.style.CMS])
    f, axs = plt.subplots(2,1, sharex=True, sharey=False,gridspec_kw={'height_ratios': [4, 1],'hspace': 0.05})
    axs = axs.flatten()
    plt.sca(axs[0])
    if(log):
        axs[0].set_yscale("log")
    hep.histplot(h1,h1Edges[0],stack=False,ax=axs[0],label = label1, histtype="step",color="r")

    if(label2=="data_obs"):
        print("Plotting data")
        plt.errorbar(hDataCentres,hData, yerr=hDataErrors, fmt='o',color="k",label = label2)
    else:
        hep.histplot(h2,h2Edges[0],stack=False,ax=axs[0],label = label2, histtype="step",color="k")
    plt.ylabel(yTitle, horizontalalignment='right', y=1.0)
    axs[0].legend()
    axs[1].set_xlabel(xTitle)
    #axs[0].set_ylabel(yTitle)
    axs[1].set_ylabel(labelR)
    if(yRange):
        axs[0].set_ylim(yRange)
    if(xRange):
        axs[0].set_xlim(xRange)
    if data:
        hep.cms.text("WiP",loc=1)
        hep.cms.lumitext(text='59.8 $fb^{-1} (13 TeV)$', ax=axs[0], fontname=None, fontsize=None)
    else:
        hep.cms.lumitext(text='2018', ax=axs[0], fontname=None, fontsize=None)
        hep.cms.text("Simulation WiP",loc=1)
    plt.legend(loc="best")#loc = (0.4,0.2))

    plt.sca(axs[1])#switch to lower pad
    axs[1].axhline(y=1.0, xmin=0, xmax=1, color="r")
    axs[1].set_ylim([0.0,2.0])
    plt.xlabel(xTitle, horizontalalignment='right', x=1.0)

    plt.errorbar(centresRatio,hRatio, yerr=hRatioErrors, fmt='o',color="k")    

    print("Saving {0}".format(outFile))
    plt.savefig(outFile)
예제 #4
0
def plot(var: str, xlabel: str, weight: str,
         bins: tuple = (50, 0, 5000)) -> None:
    # plot
    t0 = time.time()
    for dsid in df['mcChannelNumber'].unique():
        # per dsid
        df_dsid = df.loc[df['mcChannelNumber'] == dsid]
        hist = bh.Histogram(bh.axis.Regular(*bins))
        hist.fill(df_dsid[var], weight=df_dsid[weight], threads=N_THREADS // 2)
        hep.histplot(hist, label=dsid)
    # inclusive
    hist = bh.Histogram(bh.axis.Regular(*bins))
    hist.fill(df[var], weight=df[weight])
    hep.histplot(hist, label='Inclusive', color='k')
    plt.xlabel(xlabel)
    plt.ylabel("Entries")
    plt.semilogy()
    plt.legend(fontsize=10, ncol=2)
    hep.atlas.label(italic=(True, True),
                    llabel='Internal',
                    rlabel=r'$W^+\rightarrow\tau\nu$ 13TeV')
    plt.savefig(OUT_DIR + 'wplustaunu_' + var + '.png')
    plt.show()
    print(f"Making {var} plot: {time.time() - t0:.3f}s")
    plt.clf()
예제 #5
0
def main():
    rebsmear_file = uproot.open(
        rebsmear_path('plot/output/merged_2021-07-01_rebsmear_v2_run/rebsmear_qcd_estimate.root')
    )

    qcd_file = uproot.open(
        rebsmear_path('input/qcd_from_ic/out_MTR_2017.root_qcdDD.root')
    )

    outdir = 'output/merged_2021-07-01_rebsmear_v2_run'

    h_qcd = qcd_file['rebin_QCD_hist_counts']
    h_rebsmear = rebsmear_file['rebsmear_qcd_2017']

    fig, ax = plt.subplots()
    hep.histplot(h_qcd.values, h_qcd.edges, yerr=np.sqrt(h_qcd.variances), binwnorm=1, label='QCD (IC)')
    hep.histplot(h_rebsmear.values, h_rebsmear.edges, yerr=np.sqrt(h_rebsmear.variances), binwnorm=1, label='Rebalance & Smear')

    ax.set_xlabel(r'$M_{jj} \ (GeV)$')
    ax.set_ylabel('Events / GeV')
    ax.legend()

    ax.set_yscale('log')
    ax.set_ylim(1e-4,1e2)

    outpath = pjoin(outdir, 'qcd_comparison.pdf')
    fig.savefig(outpath)
    plt.close(fig)
    print(f'File saved: {outpath}')
예제 #6
0
def scale_and_plot(frame,
                   new_lumi: float = 1.,
                   c=None,
                   linewidth=1,
                   label=None):
    xs = frame['weight_mc'].abs().sum() / len(frame.index)
    lumi = frame['weight'].sum() / xs

    hist = bh.Histogram(bh.axis.Regular(100,
                                        300,
                                        10000,
                                        transform=bh.axis.transform.log),
                        storage=bh.storage.Weight())
    hist.fill(frame['MC_WZ_dilep_m_born'],
              weight=new_lumi * frame['weight'],
              threads=6)

    # scale cross-section
    hist /= hist.axes[0].widths

    hep.histplot(hist.view().value,
                 bins=hist.axes[0].edges,
                 color=c,
                 linewidth=linewidth,
                 label=label)
    return xs, lumi
예제 #7
0
def plot_dist(yvals_f, var, bin, label, outpath, sample):

    hists_gen = get_distribution(yvals_f, "gen", bin, var)
    # hists_cand = get_distribution(yvals_f, "cand", bin, var)
    hists_pred = get_distribution(yvals_f, "pred", bin, var)

    plt.figure()
    ax = plt.axes()
    v1 = mplhep.histplot(
        [h[bh.rebin(2)] for h in hists_gen],
        stack=True,
        label=[class_names[k] for k in [13, 11, 22, 1, 2, 130, 211]],
        lw=1)
    mplhep.histplot(
        [h[bh.rebin(2)] for h in hists_pred],
        stack=True,
        color=[x.stairs.get_edgecolor() for x in v1],
        lw=2,
        histtype="errorbar",
    )

    legend1 = plt.legend(v1, [x.legend_artist.get_label() for x in v1],
                         loc=(0.60, 0.44),
                         title="true")
    # legend2 = plt.legend(v2, [x.legend_artist.get_label() for x in v1], loc=(0.8, 0.44), title="pred")
    plt.gca().add_artist(legend1)
    plt.ylabel("Total number of particles / bin")
    cms_label(ax)
    sample_label(sample, ax)

    plt.yscale("log")
    plt.ylim(top=1e9)
    plt.xlabel(f"PFCandidate {label} [GeV]")
    plt.savefig(f"{outpath}/pfcand_{var}.pdf", bbox_inches="tight")
    plt.close()
예제 #8
0
def test_basic():
    fig, ax = plt.subplots(figsize=(10, 10))
    h = [1, 3, 2]
    bins = [0, 1, 2, 3]
    hep.histplot(h, bins, yerr=True, label="X")
    ax.legend()
    return fig
예제 #9
0
def plot_htmiss_before_and_after(outdir,
                                 infile,
                                 dataset_tag='jetht',
                                 plot_gen=True):
    '''Do the actual plotting of distributions.'''
    f = uproot.open(infile)
    htmiss_bef = f['htmiss_before']
    htmiss_aft = f['htmiss_after']

    fig, ax = plt.subplots()

    hep.histplot(htmiss_bef.values,
                 htmiss_bef.edges,
                 ax=ax,
                 label='Before rebalancing')
    hep.histplot(htmiss_aft.values,
                 htmiss_aft.edges,
                 ax=ax,
                 label='After rebalancing')

    ax.set_xlabel(r'$H_T^{miss} \ (GeV)$', fontsize=14)
    ax.set_ylabel(r'Counts', fontsize=14)
    ax.set_yscale('log')
    ax.set_ylim(1e-1, 1e7)

    ax.legend(title='Rebalancing')

    ax.text(0.,
            1.,
            f'{tag_to_plottag(dataset_tag)} 2017',
            fontsize=14,
            ha='left',
            va='bottom',
            transform=ax.transAxes)

    # If we're looking at QCD and plot_gen=True, plot the GEN HTmiss distribution as well
    if dataset_tag == 'qcd' and plot_gen:
        # Coffea file to take GEN HT-miss distribution from
        accpath = './input/qcd_QCD_HT700to1000-mg_new_pmx_2017.coffea'
        acc = load(accpath)

        distribution = 'gen_htmiss_noweight'
        h = acc[distribution].integrate('dataset').integrate(
            'region', 'inclusive')

        hist.plot1d(h, ax=ax, clear=False)

    handles, labels = ax.get_legend_handles_labels()
    for handle, label in zip(handles, labels):
        if label == 'None':
            handle.set_label(r'GEN $H_T^{miss}$')

    ax.legend(handles=handles)

    outpath = pjoin(outdir, f'htmiss_before_after_reb.pdf')
    fig.savefig(outpath)
    plt.close(fig)
    print(f'File saved: {outpath}')
예제 #10
0
def plotSidebandsMJY_mpl(h_3d,outFile,xTitle="",yTitle="",yRange=[],xRange=[],log=False):
    nBinsX = h_3d.GetNbinsX()
    nBinsY = h_3d.GetNbinsY()
    nBinsZ = h_3d.GetNbinsZ()
    wp = 0.8
    point = 0.0
    taggerLowBin = h_3d.GetXaxis().FindBin(point)
    taggerHighBin = h_3d.GetXaxis().FindBin(wp) - 1

    h_mj_fail  = h_3d.ProjectionZ("mj_failProjection" ,taggerLowBin,taggerHighBin,1,nBinsY)
    h_mj_pass  = h_3d.ProjectionZ("mj_passProjection" ,taggerHighBin,nBinsX,1,nBinsY)
    h_mj_fail.Scale(1/h_mj_fail.Integral())
    h_mj_pass.Scale(1/h_mj_pass.Integral())
    h_mj_pass.Rebin(2)
    h_mj_fail.Rebin(2)
    hRpf_mj = createRatio(h_mj_pass,h_mj_fail)
    hRatio = []
    hRatioErrors = []
    for i in range(1,hRpf_mj.GetNbinsX()+1):
        hRatio.append(hRpf_mj.GetBinContent(i))
        hRatioErrors.append(hRpf_mj.GetBinError(i))
    #numpy histograms
    hMJYFail, hMJYFailEdges = hist2array(h_mj_fail,return_edges=True)
    hMJYPass, hMJYPassEdges = hist2array(h_mj_pass,return_edges=True)
    hRatio = np.asarray(hRatio)
    hRatioErrors = np.asarray(hRatioErrors)
    centresRatio = (hMJYPassEdges[0][:-1] + hMJYPassEdges[0][1:])/2.

    plt.style.use([hep.style.CMS])
    f, axs = plt.subplots(2,1, sharex=True, sharey=False,gridspec_kw={'height_ratios': [4, 1],'hspace': 0.05})
    axs = axs.flatten()
    plt.sca(axs[0])
    if(log):
        axs[0].set_yscale("log")
    hep.histplot(hMJYPass,hMJYPassEdges[0],stack=False,ax=axs[0],label = "Pass", histtype="step",color="r")
    hep.histplot(hMJYFail,hMJYFailEdges[0],stack=False,ax=axs[0],label = "Fail", histtype="step",color="b")
    axs[0].legend()
    axs[1].set_xlabel(xTitle)
    axs[0].set_ylabel(yTitle)
    axs[1].set_ylabel("Data/MC")
    plt.ylabel(yTitle, horizontalalignment='right', x=1.0)
    if(yRange):
        axs[0].set_ylim(yRange)
    if(xRange):
        axs[0].set_xlim(xRange)
    hep.cms.lumitext(text='59.8 $fb^{-1} (13 TeV)$', ax=axs[0], fontname=None, fontsize=None)
    hep.cms.text("Simulation WiP",loc=1)
    plt.legend(loc="best")#loc = (0.4,0.2))

    plt.sca(axs[1])#switch to lower pad
    axs[1].axhline(y=1.0, xmin=0, xmax=1, color="r")
    axs[1].set_ylim([0.0,2.0])
    plt.xlabel(xTitle, horizontalalignment='right', x=1.0)

    plt.errorbar(centresRatio,hRatio, yerr=hRatioErrors, fmt='o',color="k")    

    print("Saving {0}".format(outFile))
    plt.savefig(outFile)
예제 #11
0
def test_log():
    fig, axs = plt.subplots(2, 2, figsize=(10, 10))
    for ax in axs[0]:
        hep.histplot([1, 2, 3, 2], range(5), ax=ax)
    plt.semilogy()
    for ax in axs[1]:
        hep.histplot([1, 2, 3, 2], range(5), ax=ax, edges=False)
    plt.semilogy()
    return fig
예제 #12
0
def effCutflow(inFile,
               sample,
               outFile,
               xTitle="",
               yTitle="",
               label="",
               yRange=[],
               xRange=[],
               log=False):
    tempFile = r.TFile.Open(inFile)
    hTemp = tempFile.Get("{0}_cutflow".format(sample))
    hist, edges = hist2array(hTemp, return_edges=True)
    #print(sample)
    #print(hist)
    for i in range(1, 5):  #tagger regions only pnet and dak8 TT/LL
        hist[-i] = hist[-i] / hist[0]
    for i in range(5, len(hist)):  #skip tagging regions
        hist[-i] = hist[-i] / hist[0]
    hist[0] = 1.
    #print(hist)

    plt.style.use([hep.style.CMS])
    f, ax = plt.subplots()
    ax.locator_params(nbins=12, axis='x')
    hep.histplot(hist,
                 edges[0],
                 ax=ax,
                 label=label,
                 histtype="step",
                 color='black')
    if (log):
        ax.set_yscale("log")
    ax.legend()
    ax.set_xlabel(xTitle)
    ax.set_ylabel(yTitle)

    hep.cms.lumitext(text='2018', ax=None, fontname=None, fontsize=None)
    hep.cms.text("Simulation WiP", loc=1)
    plt.legend(loc="best")  #loc = (0.4,0.2))

    axisTicks = ax.get_xticks().tolist()
    axisTicks = [
        0, "No cuts", "Triggers", "Jets definition", "Preselection",
        "H-jet $m_{SD}$", "H,Y pT cut", "ParticlNet TT", "ParticlNet LL",
        "DeepAK8 TT", "DeepAK8 LL"
    ]
    ax.set_xticklabels(axisTicks, rotation=45, fontsize=14)
    if (yRange):
        ax.set_ylim(yRange)
    if (xRange):
        ax.set_xlim(xRange)
    print("Saving {0}".format(outFile))
    plt.savefig(outFile)
    plt.clf()
예제 #13
0
def plot_unc(indir, histName, uncName, outdir):
    sample = 'ttHTobb'
    if uncName=='msd':
        hist = {
                'nominal' : load_mc(indir, 'msd_nanoAOD', histName, sample),
                'up'      : load_mc(indir, 'msd_nom',     histName, sample),
                'down'    : load_mc(indir, 'msd_raw',     histName, sample)
                }
    else:
        hist = {
                'nominal' : load_mc(indir,  'msd_nom',   histName, sample),
                'up'      : load_mc(indir, f'{uncName}Up',   histName, sample),
                'down'    : load_mc(indir, f'{uncName}Down', histName, sample)
                }

    color = {
            'nominal' : 'k',
            'up'      : 'r',
            'down'    : 'b'
            }
    ls    = {
            'nominal' : '-',
            'up'      : '--',
            'down'    : '-.'
            }

    rebin_factor = 5

    plt.style.use([hep.cms.style.ROOT, {'font.size': 24}])
    f, ax = plt.subplots()
    hep.cms.label(data=False, paper=False, year=args.year, ax=ax, loc=0)
    for hn,h in hist.items():
        h['edges'], h['contents'], h['contents_w2'] = rebin(h['edges'], h['contents'], h['contents_w2'], rebin_factor)
        if uncName=='msd':
            if   hn=='nominal': label = 'nanoAOD'
            elif hn=='up':      label = 'nom'
            elif hn=='down':    label = 'raw'
        else:
            if    hn=='nominal': label = hn
            else: label = f'{uncName} {hn}'

        hep.histplot(h['contents'], h['edges'], edges=True, label=label,ls=ls[hn],color=color[hn])
        #print(uncName, hn, h['contents'][-3:])
    plt.xlim(90,170)
    #plt.xlim(0,1000)
    #ax.set_ylim(ymin=.5e-1)
    #plt.semilogy()
    plt.legend()
    #import pdb
    #pdb.set_trace()
    ax.set_xlabel('Leading AK8 jet softdrop mass [GeV]', ha='right', x=1)
    ax.set_ylabel(f'Events / {rebin_factor} GeV', ha='right', y=1)
    for ext in ['.png','.pdf']:
         plt.savefig(os.path.join(outdir,f'{sample}_{args.variable}_{args.mask}_{uncName}{ext}'))
예제 #14
0
def plot_projections(data_counts, model_prediction, data_bin_edges, model_prediction_grid,
                     fit_params=None, close_image=False, save_fig=True, output_folder='.', image_name=None):
    fig, axs = plt.subplots(1, 2, figsize=(20,7))
    data_counts_x = data_counts['x']
    data_counts_y = data_counts['y']
    #
    model_prediction_x = model_prediction['x']['model']
    # model_prediction_x_sig = model_prediction['x']['sig']
    # model_prediction_x_bkgr = model_prediction['x']['bkgr']
    #
    model_prediction_y = model_prediction['y']['model']
    # model_prediction_y_sig = model_prediction['y']['sig']
    # model_prediction_y_bkgr = model_prediction['y']['bkgr']
    ##
    axs[0].plot(model_prediction_grid, model_prediction_x, label="Model", linewidth=5)
    # axs[0].plot(model_prediction_grid, model_prediction_x_sig, label="Signal", linewidth=5)
    # axs[0].plot(model_prediction_grid, model_prediction_x_bkgr, label="Background", linewidth=5)
    mplhep.histplot(data_counts_x, data_bin_edges, yerr=True, color='black', histtype='errorbar',
                    markersize=17, capsize=2.5,
                    markeredgewidth=1.5, zorder=1,
                    elinewidth=1.5, ax=axs[0]
                    )
    axs[0].set_title('X projection')
    if fit_params:
        assert 'x' in fit_params
        assert 'mu' in fit_params['x'] and 'sigma' in fit_params['x'] and 'fr' in fit_params['x']
        mu_patch = mpatches.Patch(color='none', label=f"mu = {fit_params['x']['mu']:.2f}")
        sigma_patch = mpatches.Patch(color='none', label=f"sigma = {fit_params['x']['sigma']:.2f}")
        fr_patch = mpatches.Patch(color='none', label=f"fr = {fit_params['x']['fr']:.4f}")
        N_patch = mpatches.Patch(color='none', label=f"N = {fit_params['x']['N']:.0f}")
        axs[0].legend(handles=[mu_patch, sigma_patch, fr_patch, N_patch])
    ##
    axs[1].plot(model_prediction_grid, model_prediction_y, label="Model", linewidth=5)
    # axs[1].plot(model_prediction_grid, model_prediction_y_sig, label="Signal", linewidth=5)
    # axs[1].plot(model_prediction_grid, model_prediction_y_bkgr, label="Background", linewidth=5)
    mplhep.histplot(data_counts_y, data_bin_edges, yerr=True, color='black', histtype='errorbar',
                    markersize=17, capsize=2.5,
                    markeredgewidth=1.5, zorder=1,
                    elinewidth=1.5, ax=axs[1]
                    )
    axs[1].set_title('Y projection')
    if fit_params:
        assert 'y' in fit_params
        assert 'mu' in fit_params['y'] and 'sigma' in fit_params['y'] and 'fr' in fit_params['y']
        mu_patch = mpatches.Patch(color='none', label=f"mu = {fit_params['y']['mu']:.2f}")
        sigma_patch = mpatches.Patch(color='none', label=f"sigma = {fit_params['y']['sigma']:.2f}")
        fr_patch = mpatches.Patch(color='none', label=f"fr = {fit_params['y']['fr']:.4f}")
        N_patch = mpatches.Patch(color='none', label=f"N = {fit_params['y']['N']:.0f}")
        axs[1].legend(handles=[mu_patch, sigma_patch, fr_patch, N_patch])
    if save_fig:
        fig.savefig(f"{output_folder}/{image_name}_fitted.png")
    if close_image:
        plt.close(fig)
예제 #15
0
def create_plot1d(hist, save_name, log=False):
    import matplotlib.pyplot as plt
    import mplhep as hep
    plt.style.use(hep.style.CMS)
    # plot
    ax = plt.gca()

    plt.errorbar(
        hist.axes[0].centers,
        hist.view(),
        np.sqrt(hist.view()),
        fmt='.',
        color='blue',
    )

    hep.histplot(hist, ax=ax, color='blue')

    if log:
        ax.set_yscale('log')
    else:
        ax.ticklabel_format(axis='y',
                            style='sci',
                            scilimits=(0, 3),
                            useMathText=True)

    ax.set_xlabel(hist.axes[0].metadata, loc='right')
    ax.set_ylabel("Counts", loc='top')

    # compute mean and std:
    mean = (hist.view() * hist.axes[0].centers).sum() / hist.sum()
    std = np.sqrt(
        (hist.view() * ((hist.axes[0].centers - mean)**2)).sum() / hist.sum())

    annotation = f"Total {hist.sum()}" \
                    + "\n" + f"Mean: {round(mean,2)}" \
                    + "\n" + f"Std: {round(std,2)}"

    ax.annotate(annotation,
                xy=(0.85, 0.85),
                xycoords='axes fraction',
                fontsize="small",
                ha='center',
                annotation_clip=False,
                bbox=dict(boxstyle='round', fc='None'))

    ax.set_xlim(hist.axes[0].edges[0],
                hist.axes[0].edges[-1] + hist.axes[0].widths[-1])

    fig = ax.get_figure()
    fig.savefig(save_name)
    ax.clear()
    fig.clear()
예제 #16
0
def plot_fit_result(models, data, p_params, obs):

    plt_name = "mtw"

    lower, upper = obs.limits

    h_bin_width = hist_dicts[plt_name]["bin_width"]
    h_num_bins = hist_dicts[plt_name]["numbins"]
    h_xmin = hist_dicts[plt_name]["xmin"]
    h_xmax = hist_dicts[plt_name]["xmax"]
    h_xlabel = hist_dicts[plt_name]["xlabel"]
    plt_label = "$W \\rightarrow l\\nu$"

    bins = [h_xmin + x * h_bin_width for x in range(h_num_bins + 1)]
    bin_centers = [
        h_xmin + h_bin_width / 2 + x * h_bin_width for x in range(h_num_bins)
    ]

    data_x, _ = np.histogram(data.values, bins=bins)
    data_sum = data_x.sum()
    plot_scale = data_sum * obs.area() / h_num_bins

    plt.clf()
    plt.style.use(hep.style.ATLAS)
    plt.axes([0.1, 0.30, 0.85, 0.65])
    main_axes = plt.gca()
    hep.histplot(main_axes.hist(data, bins=bins, log=False, facecolor="none"),
                 color="black",
                 yerr=True,
                 histtype="errorbar",
                 label="data")

    main_axes.set_xlim(h_xmin, h_xmax)
    main_axes.xaxis.set_minor_locator(AutoMinorLocator())
    main_axes.set_xlabel(h_xlabel)
    main_axes.set_title("W Transverse Mass Fit")
    main_axes.set_ylabel("Events/4 GeV")
    main_axes.ticklabel_format(axis='y', style='sci', scilimits=[-2, 2])

    x_plot = np.linspace(lower[-1][0], upper[0][0], num=1000)
    for model_name, model in models.items():
        if model.is_extended:
            main_axes.plot(x_plot,
                           model.ext_pdf(x_plot) * obs.area() / h_num_bins,
                           label=model_name)
        else:
            main_axes.plot(x_plot,
                           model.pdf(x_plot) * plot_scale,
                           label=model_name)
    main_axes.legend(title=plt_label, loc="best")
    plt.savefig(f"../Results/fit_plot_{plt_name}_Complex.pdf")
    plt.close()
예제 #17
0
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
예제 #18
0
파일: sf.py 프로젝트: vcepaitis/histo
def plot1d(arrays, labels, bins, title, x_label, log_x):
    plt.style.use(hep.style.ROOT)
    fig, ax = plt.subplots()
    if log_x:
        ax.set_xscale('log')
    #hep.cms.label(loc=0)
    for array, label in zip(arrays, labels):
        h, bins = np.histogram(array, bins, density=True)
        print(h, bins)
        hep.histplot(h, bins, label=label)
    ax.set_xlabel(x_label)
    ax.legend()
    plt.savefig(f"tagger_efficiency/{title}.pdf")
예제 #19
0
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
예제 #20
0
def test_histplot_stack():
    np.random.seed(0)
    h, bins = np.histogram(np.random.normal(10, 3, 400), bins=10)

    fig, axs = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(10, 10))
    axs = axs.flatten()

    axs[0].set_title("Default", fontsize=18)
    hep.histplot([h, 1.5 * h], bins, stack=True, ax=axs[0])

    axs[1].set_title("Plot No Edges", fontsize=18)
    hep.histplot([h, 1.5 * h], bins, edges=False, stack=True, ax=axs[1])

    axs[2].set_title("Plot Errorbars", fontsize=18)
    hep.histplot([h, 1.5 * h],
                 bins,
                 yerr=[np.sqrt(h), np.sqrt(h)],
                 stack=True,
                 ax=axs[2])

    axs[3].set_title("Filled Histogram", fontsize=18)
    hep.histplot([1.5 * h, h], bins, histtype="fill", stack=True, ax=axs[3])

    fig.subplots_adjust(hspace=0.1, wspace=0.1)
    return fig
예제 #21
0
def test_histplot_multiple():
    np.random.seed(0)
    h, bins = np.histogram(np.random.normal(10, 3, 400), bins=10)

    fig, axs = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(10, 10))
    axs = axs.flatten()

    axs[0].set_title("Default Overlay", fontsize=18)
    hep.histplot([h, 1.5 * h], bins, ax=axs[0])

    axs[1].set_title("Default Overlay w/ Errorbars", fontsize=18)
    hep.histplot([h, 1.5 * h],
                 bins,
                 yerr=[np.sqrt(h), np.sqrt(1.5 * h)],
                 ax=axs[1])

    axs[2].set_title("Automatic Errorbars", fontsize=18)
    hep.histplot([h, 1.5 * h], bins, yerr=True, ax=axs[2])

    axs[3].set_title("With Labels", fontsize=18)
    hep.histplot([h, 1.5 * h],
                 bins,
                 yerr=True,
                 ax=axs[3],
                 label=["First", "Second"])
    axs[3].legend(fontsize=16, prop={"family": "Tex Gyre Heros"})

    fig.subplots_adjust(hspace=0.1, wspace=0.1)
    return fig
예제 #22
0
def test_histplot_kwargs():
    np.random.seed(0)
    h, bins = np.histogram(np.random.normal(10, 3, 1000), bins=10)

    fig, axs = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(10, 10))
    axs = axs.flatten()

    hep.histplot(
        [h * 2, h * 1, h * 0.5],
        bins,
        label=["1", "2", "3"],
        stack=True,
        histtype="step",
        linestyle="--",
        color=["green", "black", (1, 0, 0, 0.4)],
        ax=axs[0],
    )
    axs[0].legend()

    hep.histplot(
        [h, h, h],
        bins,
        label=["1", "2", "3"],
        stack=True,
        histtype="step",
        linestyle=["--", ":"],
        color=(1, 0, 0, 0.8),
        ax=axs[1],
    )
    axs[1].legend()

    hep.histplot(
        [h, h, h],
        bins,
        label=["1", "2", "3"],
        histtype="step",
        binwnorm=[0.5, 3, 6],
        linestyle=["--", ":"],
        color=(1, 0, 0, 0.8),
        ax=axs[2],
    )
    axs[2].legend()

    hep.histplot(
        [h, h, h],
        bins,
        label=["1", "2", "3"],
        histtype="fill",
        binwnorm=[0.5, 3, 6],
        linestyle=["--", ":"],
        color=["green", "darkorange", "red"],
        alpha=[0.4, 0.7, 0.2],
        ax=axs[3],
    )
    axs[3].legend()

    fig.subplots_adjust(hspace=0.1, wspace=0.1)
    return fig
예제 #23
0
파일: plot_sig_bkg.py 프로젝트: zfit/zfit
def plot_pdf(title):
    plt.figure()
    plt.title(title)
    y = model.pdf(x).numpy()
    y_gauss = (gauss.pdf(x) * frac).numpy()
    y_exp = (exponential.pdf(x) * (1 - frac)).numpy()
    plt.plot(x, y * plot_scaling, label="Sum - Model")
    plt.plot(x, y_gauss * plot_scaling, label="Gauss - Signal")
    plt.plot(x, y_exp * plot_scaling, label="Exp - Background")
    mplhep.histplot(
        np.histogram(data_np, bins=n_bins),
        yerr=True,
        color="black",
        histtype="errorbar",
    )
    plt.ylabel("Counts")
    plt.xlabel("obs: $B_{mass}$")
예제 #24
0
def plotWeights(model, nBins=20):
    plt.figure(figsize=(8, 6))
    for ilayer in range(1, len(model.layers)):
        if len(model.layers[ilayer].get_weights()) > 0:
            label = model.layers[ilayer].name
            data = np.histogram(model.layers[ilayer].get_weights()[0])
            print(ilayer, label, 'unique weights',
                  len(np.unique(model.layers[ilayer].get_weights()[0])))
            hep.histplot(data[0], data[1], label=label)
        else:
            print(ilayer, 'no weights')
    plt.xlabel('weights')
    plt.ylabel('Entries')
    plt.yscale('log')
    plt.legend()
    plt.savefig("%s_weights.pdf" % model.name)
    plt.clf()
def plot_pdf(title):
    plt.figure()
    plt.title(title)
    y = model.pdf(x).numpy()
    y_gauss = (gauss.pdf(x) * model_unbinned.params["frac_0"]).numpy()
    y_exp = (exponential.pdf(x) * model_unbinned.params["frac_1"]).numpy()
    plt.plot(x, y * plot_scaling, label="Sum - Model")
    plt.plot(x, y_gauss * plot_scaling, label="Gauss - Signal")
    plt.plot(x, y_exp * plot_scaling, label="Exp - Background")
    # mplhep.histplot(np.histogram(data_np, bins=n_bins), yerr=True, color='black', histtype='errorbar')
    mplhep.histplot(data.to_hist(),
                    yerr=True,
                    color="black",
                    histtype="errorbar")
    plt.ylabel("Counts")
    plt.xlabel("obs: $B_{mass}$")
    plt.legend()
예제 #26
0
    def shape_in_bins(self, shape_of="dimuon_mass", nbins=4):
        for model_name in self.models.keys():
            score_name = f"{model_name}_score"
            for cls in self.df["class_name"].dropna().unique():
                df = self.df[self.df["class_name"] == cls]
                score = df[score_name]
                fig = plt.figure()
                fig, ax = plt.subplots()

                for i in range(nbins):
                    cut_lo = score.quantile(i / nbins)
                    cut_hi = score.quantile((i + 1) / nbins)
                    cut = (score > cut_lo) & (score < cut_hi)
                    if shape_of == "max_abs_eta":
                        data = df.loc[cut].apply(max_abs_eta, axis=1)
                    else:
                        data = df.loc[cut, shape_of]

                    if shape_of == "dimuon_mass":
                        data_range = (115, 135)
                        data_bins = 80
                    else:
                        data_range = (data.min(), data.max())
                        data_bins = 25
                    weights = (df.lumi_wgt).loc[cut].values
                    # weights = (df.lumi_wgt * df.mc_wgt).loc[cut].values
                    hist = np.histogram(
                        data.values,
                        bins=data_bins,
                        range=data_range,
                        weights=weights,
                        density=True,
                    )
                    hep.histplot(
                        hist[0],
                        hist[1],
                        histtype="step",
                        label=f"{score_name} bin #{i}",
                    )
                ax.legend(prop={"size": "x-small"})
                ax.set_xlabel(shape_of)
                ax.set_yscale("log")
                ax.set_ylim(0.0001, 1)
                out_name = f"{self.out_path}/shapes_{score_name}_{cls}_{shape_of}.png"
                fig.savefig(out_name)
def test_bh_to_root_with_weights_1d():
    bh_hist = bh.Histogram(bh.axis.Regular(20, -1, 1),
                           storage=bh.storage.Weight())
    bh_hist.fill(data, weight=2)
    hep.histplot(bh_hist.view().value,
                 bins=bh_hist.axes[0].edges,
                 yerr=np.sqrt(bh_hist.view().variance))
    plt.savefig(out_dir + 'bh_to_root_bh_hist_1d.png')
    plt.clf()

    root_hist = bh_to_TH1(bh_hist, 'test', 'test;x;entries')
    c1 = ROOT.TCanvas()
    c1.cd()
    root_hist.Draw("histE")
    c1.Print(out_dir + 'bh_to_root_root_hist_1d.png')

    for i, binval in enumerate(bh_hist.view(flow=True)):
        assert binval.value == root_hist.GetBinContent(i)
        assert np.sqrt(binval.variance) == root_hist.GetBinError(i)
def test_root_to_bh_with_weights_1d():
    root_hist = ROOT.TH1F('test', 'test;x;entries', 20, -1, 1)

    rnp.fill_hist(root_hist, data, weights=np.full(data.shape[0], 2))  # fill
    c3 = ROOT.TCanvas()
    c3.cd()
    root_hist.Draw("histE")
    c3.Print(out_dir + 'root_to_bh_root_hist_1d.png')

    bh_hist = TH1_to_bh(root_hist)
    hep.histplot(bh_hist.view().value,
                 bins=bh_hist.axes[0].edges,
                 yerr=np.sqrt(bh_hist.view().variance))
    plt.savefig(out_dir + 'root_to_bh_hist_1d.png')
    plt.clf()

    for i, binval in enumerate(bh_hist.view(flow=True)):
        assert binval.value == root_hist.GetBinContent(i)
        assert np.sqrt(binval.variance) == root_hist.GetBinError(i)
예제 #29
0
def plot_shape(y, ynew, yerr, yerrnew, name):
    plt.figure()
    hep.histplot(y,
                 range(0,
                       len(y) + 1),
                 yerr=yerr,
                 histtype='step',
                 label='before')
    hep.histplot(ynew,
                 range(0,
                       len(ynew) + 1),
                 yerr=yerrnew,
                 histtype='step',
                 label='after',
                 ls='--')
    plt.legend(title=name)
    plt.ylim(bottom=0)
    plt.savefig('{name}.png'.format(name=name))
    plt.close()
예제 #30
0
def plot_component(dfs, component):
    print("###==========####")
    print("Started plotting")

    plot_label = "$W \\rightarrow l\\nu$"
    hist = hist_dicts['mtw']

    h_bin_width = hist["bin_width"]
    h_num_bins = hist["numbins"]
    h_xmin = hist["xmin"]
    h_xmax = hist["xmax"]
    h_xlabel = hist["xlabel"]
    x_var = hist["xvariable"]
    h_title = hist["title"]

    bins = [h_xmin + x * h_bin_width for x in range(h_num_bins + 1)]
    bin_centers = [
        h_xmin + h_bin_width / 2 + x * h_bin_width for x in range(h_num_bins)
    ]

    data_x, _ = np.histogram(dfs[component][x_var].values, bins=bins)

    plt.clf()
    plt.style.use(hep.style.ATLAS)
    _ = plt.figure(figsize=(9.5, 9))
    plt.axes([0.1, 0.30, 0.85, 0.65])
    plt.yscale("linear")
    main_axes = plt.gca()
    main_axes.set_title(h_title)
    hep.histplot(main_axes.hist(data[component][x_var],
                                bins=bins,
                                log=False,
                                facecolor="none"),
                 color="black",
                 yerr=True,
                 histtype="errorbar",
                 label='Данные')

    main_axes.set_xlim(h_xmin * 0.9, h_xmax * 1.1)

    main_axes.xaxis.set_minor_locator(AutoMinorLocator())
    main_axes.set_ylabel(f"События/{h_bin_width} ГэВ")
    plt.savefig(f"../FitResults_8TeV/{component}_mtw.jpg")