Beispiel #1
0
def plot_mc1d(ax, hdict, xlabel="", ylabel="Events", xlimits=None, ylimits=None, hist_styles=None, **mc_opts):

    #set_trace()
    mcorder = mc_opts.get("mcorder")
    stack = mc_opts.get("stack", True)
    normalize = mc_opts.get("normalize", False)
    err_opts = mc_opts.get("error_opts", stack_error_opts)
    overflow = mc_opts.get("overflow", "none")
    logy = mc_opts.get("logy", False)
    leg_ncols = mc_opts.get("leg_ncols", 1)

        ## plot MC and data
    plot.plot1d(hdict[mc_samples],
        overlay=hdict.axes()[0].name,
        ax=ax,
        clear=False,
        stack=stack,
        line_opts=None,
        fill_opts=stack_fill_opts,
        error_opts=err_opts,
        order=mcorder,
        density=normalize,
        overflow=overflow,
        overlay_overflow=overflow,
    )
    #ax.autoscale(axis="x", tight=True)
    if logy:
        ax.set_yscale("log")
    ax.autoscale()
    ylims = (0, None) if ylimits is None else ylimits
    ax.set_ylim(ylims)
    #ax.set_ylim(0, ax.get_ylim()[1]*1.3)
    ax.set_ylabel(ylabel)
    ax.set_xlabel(xlabel)
    ax.set_xlim(xlimits)

        ## set legend and corresponding colors
    handles, labels = ax.get_legend_handles_labels()
    for idx, sample in enumerate(labels):
        if sample == "data" or sample == "Observed": continue
        facecolor, legname = plt_tools.get_styles(sample, hstyles) if hist_styles is None else plt_tools.get_styles(sample, hist_styles)
        handles[idx].set_facecolor(facecolor)
        labels[idx] = legname
    # call ax.legend() with the new values
    #labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: t[0]))
    ax.legend(handles,labels, loc="upper right", ncol=leg_ncols)
    
    return ax
        histo = histo.rebin(*axes_to_sum, rebinning)

        # orig
    fig, (ax, rax) = plt.subplots(2,
                                  1,
                                  gridspec_kw={"height_ratios": (3, 1)},
                                  sharex=True)
    fig.subplots_adjust(hspace=.07)

    for rewt_key in sorted(histo.values().keys()):
        # plot yields
        ax = plot.plot1d(
            histo[rewt_key].integrate("rewt"),
            ax=ax,
            clear=False,
            line_opts={
                "linestyle": "-",
                "color": rewt_style_dict[rewt_key[0]][1]
            },
        )
        ## plot ratios
        if rewt_key == ("Nominal", ): continue
        # orig
        ratio_masked_vals, ratio_bins = Plotter.get_ratio_arrays(
            num_vals=histo.values()[rewt_key],
            denom_vals=histo.values()[("Nominal", )],
            input_bins=histo.dense_axes()[0].edges())
        rax.step(ratio_bins,
                 ratio_masked_vals,
                 where="post",
                 **{
Beispiel #3
0
 if str(s) == "QCD": continue
 fig, (ax,
       rax) = plt.subplots(2,
                           1,
                           figsize=(11, 13),
                           gridspec_kw={"height_ratios": (3, 1)},
                           sharex=True)
 fig.subplots_adjust(hspace=.07)
 ax.set_prop_cycle(cycler(color=colors))
 if hists[key].integrate('category', cat).integrate(
         'control_region', s).dim() != 2:
     continue
 plot.plot1d(hists[key].integrate('category', cat).integrate(
     'control_region', s),
             overlay="process",
             ax=ax,
             clear=False,
             stack=True,
             fill_opts=fill_opts,
             error_opts=error_opts)
 if str(s) != "signal":
     plot.plot1d(data_hists[key].integrate('category',
                                           cat).integrate(
                                               'control_region', s),
                 overlay="process",
                 ax=ax,
                 clear=False,
                 error_opts=data_err_opts)
     plot.plotratio(data_hists[key].integrate(
         'category', cat).integrate('control_region',
                                    s).integrate('process'),
                    hists[key].integrate('category', cat).integrate(
                        xaxis_name = hslice.dense_axes()[0].name
                        hslice = hslice.rebin(xaxis_name, rebinning)

                        # save distribution to dict
                    if save_dist and (lepcat == 'Tight'):
                        hcor = hslice['Correct'].integrate('permcat')
                        edges = hcor.dense_axes()[0].edges()
                        lookup = dense_lookup(*(hcor.values().values()),
                                              edges)  # not normalized
                        permProbs[year]['3Jets'].update({hname: lookup})

                        ## plot comparison of perm categories
                    plot.plot1d(
                        hslice,
                        overlay=hslice.axes()[0].name,
                        ax=ax,
                        clear=False,
                        line_opts={'linestyle': '-'},
                        density=True,  # normalized to 1,
                    )
                    ax.autoscale()  #, tight=True)
                    ax.set_ylim(0, ax.get_ylim()[1] * 1.15)
                    ax.set_ylabel('Probability Density')
                    ax.set_xlabel(xtitle)
                    ax.set_xlim(x_lims)

                    #set_trace()
                    ## set legend and corresponding colors
                    if hname == 'Lost_mTHadProxy':
                        h_opts = {
                            key: hstyles['%s_THad' % key]
                            for key in pcat_groups.keys()
        scale_ggH = 100
        ggH_rescaled.scale(scale_ggH)
        maxY = 1.2 * max([
            max(h[args.data].sum('flavor').values()[('BTagMu', )]),
            max(QCDALL_rescaled.values()[()])
        ])

        fig, (ax, rax) = plt.subplots(2,
                                      1,
                                      figsize=(12, 12),
                                      gridspec_kw={"height_ratios": (3, 1)},
                                      sharex=True)
        fig.subplots_adjust(hspace=.07)
        plot.plot1d(QCD_rescaled,
                    ax=ax,
                    legend_opts={'loc': 1},
                    fill_opts=flavor_opts,
                    order=flavors,
                    stack=True)
        plot.plot1d(h[args.data].sum('flavor'),
                    ax=ax,
                    legend_opts={'loc': 1},
                    error_opts=data_err_opts,
                    clear=False)
        #plot.plot1d(ggH_rescaled, ax=ax, legend_opts={'loc':1}, fill_opts=signal_opts, stack=False, clear=False)
        plot.plotratio(num=h[datasets_data].sum('dataset', 'flavor'),
                       denom=QCDALL_rescaled,
                       ax=rax,
                       error_opts=data_err_opts,
                       denom_fill_opts={},
                       guide_opts={},
                       unc='num')
fitdir = f'/work/mmarcheg/BTVNanoCommissioning/fitdir/{args.year}/msd100tau06{tagger}/'
f = uproot.open(args.input)
for region in ['sfpass', 'sffail']:
    fig, axes = plt.subplots(1, 2, figsize=(16,6), sharey=True)
    #print(axes)
    #fig, (ax1, ax2, rax1, rax2) = plt.subplots(2, 2, figsize=(16,6), sharey=True)
    #fig, axes = plt.subplots(1, 2, figsize=(16,6), sharey=True)
    for i, fit in enumerate(['prefit', 'fit_s']):
        ax = axes[i]
        for (j, flavor) in enumerate(flavors):
            h = f[f'shapes_{fit}/{region}/{flavor};1']
            weights, bins = h.to_numpy()
            binsize = bins[1] - bins[0]
            values = (bins - 0.5*binsize)[1:]
            output[f'shape_{fit}_{region}{tagger}{args.year}'].fill(flavor=flavor, jetproba=values, weight=weights)
            #ax.hist(values, bins, weights=weights, color=flavor_opts['facecolor'][::-1][j], stacked=True, label=flavor)

        plot.plot1d(output[f'shape_{fit}_{region}{tagger}{args.year}'][flavors], ax=ax, legend_opts={'loc':1}, fill_opts=flavor_opts, order=flavors, stack=True)
        data = f[f'shapes_{fit}/{region}/data;1']
        output[f'shape_{fit}_{region}{tagger}{args.year}'].fill(flavor=args.data, jetproba=data.values()[0], weight=data.values()[1])
        errors = data.errors('mean')[1]
        #plot.plot1d(output[f'shape_{fit}_{region}{tagger}{args.year}'][args.data], ax=ax, legend_opts={'loc':1}, fill_opts=data_err_opts, clear=False)
        #plot.plot1d(output[f'shape_{fit}_{region}{tagger}{args.year}'][args.data], ax=ax, legend_opts={'loc':1}, clear=False)
        ax.errorbar(data.values()[0], data.values()[1], yerr=errors, marker='.', linestyle='', markersize=10, elinewidth=1, color='black', label=args.data)
        ax.set_title(f'shapes_{fit} ' + region + f' ({tagger}, {args.year})')
        ax.legend(loc='upper right')
        #plot.plotratio(num=output[f'shape_{fit}_{selection}{tagger}{year}']['data'].sum('flavor'), denom=output[f'shape_{fit}_{selection}{tagger}{year}'][flavors].sum('flavor'), ax=axes[i+2],
               #unc='num')
    histname = f'shapes_{region}{tagger}_{args.year}.png'
    plt.savefig(plot_dir + histname, dpi=300, format="png")
Beispiel #7
0
            new_xtitle = xtitle.replace("obj", objlabel)
            if hname == "mass":
                x_lims = mass_range
            tt_histo = histo[:, genobj].integrate("objtype")

            fig, ax = plt.subplots()
            fig.subplots_adjust(hspace=.07)

            if ("Int" in dataset) and (not args.indiv_int):
                # plot negative weights first
                plot.plot1d(
                    tt_histo[re.compile(r".*(neg)")].integrate("dataset"),
                    overlay=tt_histo.integrate("dataset").axes()[0].name,
                    line_opts={
                        "color": ["#e41a1c", "#e41a1c"],
                        "linestyle": ["-", "--"]
                    },  # red
                    ax=ax,
                    clear=False,
                    stack=False,
                )
                # plot positive weights
                plot.plot1d(
                    tt_histo[re.compile(r".*(pos)")].integrate("dataset"),
                    overlay=tt_histo.integrate("dataset").axes()[0].name,
                    line_opts={
                        "color": ["#377eb8", "#377eb8"],
                        "linestyle": ["-", "--"]
                    },  # blue
                    ax=ax,
                    clear=False,
        msg = c.poll(timeout=timeout)
        if msg is None:
            running = False
            continue
        if msg.error():
            raise KafkaException(msg.error())
        else:
            # Proper message
            sys.stderr.write(
                '%% %s [%d] at offset %d with key %s:\n' %
                (msg.topic(), msg.partition(), msg.offset(), str(msg.key())))

            buf = msg.value()
            hist = pickle.loads(lz4f.decompress(buf))
            if accumulate:
                accumulate = accumulate.add(hist)
            else:
                accumulate = hist

            # Once we are assigned a partition and start getting messages
            # we can tighten up the timeout
            timeout = 10.0

    print(accumulate.values())
    fig, ax, _ = coffeaplot.plot1d(accumulate)
    plt.savefig("/servicex/plot.png")

except Exception as ex:
    print(ex)
    raise
Beispiel #9
0
            for cat in sorted(
                    set([key[0] for key in nosys_histo.values().keys()])):
                pltdir = os.path.join(outdir, jmult, cat)
                if not os.path.isdir(pltdir):
                    os.makedirs(pltdir)

                hslice = nosys_histo[cat, jmult].integrate(
                    'process').integrate('jmult')

                fig, ax = plt.subplots()
                fig.subplots_adjust(hspace=.07)

                plot.plot1d(
                    hslice,
                    overlay=hslice.axes()[0].name,
                    ax=ax,
                    clear=False,
                    line_opts={'linestyle': '-'},
                )
                ax.autoscale(axis='x', tight=True)
                ax.set_ylim(0, ax.get_ylim()[1] * 1.15)
                ax.set_xlabel(xtitle)
                ax.set_xlim(x_lims)

                #set_trace()
                ## set legend and corresponding colors
                handles, labels = ax.get_legend_handles_labels()
                for idx, label in enumerate(labels):
                    labels[idx] = alpha_corrections[label][0]
                    handles[idx].set_color(alpha_corrections[label][1])
                    handles[idx].set_linewidth(alpha_corrections[label][2])
     nnlo_histo.dense_axes()[0].name, png_ext))
fig_norm.savefig(figname_norm)
print('%s written' % figname_norm)

### plot linearized version of hist ###
nnlo_lin_hist = nnlo_lin_dict[nnlo_var]

# original hist
fig, ax = plt.subplots()
fig.subplots_adjust(hspace=.07)

plot.plot1d(
    nnlo_lin_hist,
    ax=ax,
    clear=False,
    line_opts={
        'linestyle': '-',
        'color': 'k'
    },
)
#plot.plot1d(nnlo_lin_hist, ## need to plot errorbar separately
#    ax=ax, clear=False,
#    error_opts={'color': 'k', 'marker' : None},
#)

# format axes
ax.autoscale()
ax.set_ylim(None, ax.get_ylim()[1] * 1.15)
ax.set_xlabel("%s $\otimes$ %s" % (xtitle, ytitle))
ax.set_ylabel('%s [pb/GeV]' % ztitle)
ax.get_legend().remove()
                            out.write(prettyjson.dumps(yields_json))

                    if rebinning != 1:
                        xaxis_name = hslice.dense_axes()[0].name
                        hslice = hslice.rebin(xaxis_name, rebinning)

                    #set_trace()
                    #mc_dict = {key:np.sum(list(hslice[key].values().values()), axis=1) for key in [key[0] for key in list(hslice[mc_samples].values().keys())]} # make {topo: sum events} dict for sorting
                    #mc_order = sorted(mc_dict, key=mc_dict.get, reverse=False)
                        ## plot MC and data
                    plot.plot1d(hslice[mc_samples],
                        overlay=hslice.axes()[0].name,
                        ax=ax,
                        clear=False,
                        stack=True,
                        line_opts=None,
                        fill_opts=stack_fill_opts,
                        error_opts=stack_error_opts,
                        order=['QCD', 'EWK', 'singlet', 'ttJets'],
                        #order=mc_order,
                    )
                    if withData:
                        plot.plot1d(hslice[data_samples],
                            overlay=hslice.axes()[0].name,
                            ax=ax,
                            clear=False,
                            error_opts=hstyles['data_err_opts']
                        )
                    ax.autoscale(axis='x', tight=True)
                    ax.set_ylim(0, None)
                    ax.set_xlabel(None)
Beispiel #12
0
def plot_stack1d(ax, rax, hdict, xlabel="", ylabel="", sys="nosys", xlimits=None, ylimits=None, **mc_opts):

    mcorder = mc_opts.get("mcorder")
    maskData = mc_opts.get("maskData", False)
    overflow = mc_opts.get("overflow", "none")
    logy = mc_opts.get("logy", False)
    leg_ncols = mc_opts.get("leg_ncols", 3)

    #set_trace()
    if hdict.sparse_dim() > 1:
        mc_dict = hdict[:, sys].integrate("sys")
        mc_dict = mc_dict[mc_samples]
        data_dict = hdict[:, "nosys"].integrate("sys")
        data_dict = data_dict[data_samples]
    
    else:
        mc_dict = hdict[mc_samples]
        data_dict = hdict[data_samples]

        ## plot MC and data
    plot.plot1d(mc_dict,
        overlay=mc_dict.axes()[0].name,
        ax=ax,
        clear=False,
        stack=True,
        line_opts=None,
        fill_opts=stack_fill_opts,
        error_opts=stack_error_opts,
        order=mcorder,
        overflow=overflow,
        overlay_overflow=overflow,
    )
    if not maskData:
        plot.plot1d(data_dict,
            overlay=data_dict.axes()[0].name,
            ax=ax,
            clear=False,
            error_opts=hstyles["data_err_opts"],
            overflow=overflow,
            overlay_overflow=overflow,
        )
    if logy:
        ax.set_yscale("log")
    ax.autoscale()
    ax.set_ylim(0, ax.get_ylim()[1]*1.3)
    #ax.set_ylim(0, None)
    ax.set_xlabel(None)
    ax.set_xlim(xlimits)
    
        ## set legend and corresponding colors
    handles, labels = ax.get_legend_handles_labels()
    for idx, sample in enumerate(labels):
        if sample == "data" or sample == "Observed": continue
        if isinstance(handles[idx], matplotlib.lines.Line2D): continue
        facecolor, legname = plt_tools.get_styles(sample, hstyles)
        handles[idx].set_facecolor(facecolor)
        labels[idx] = legname
    # call ax.legend() with the new values
    ax.legend(handles,labels, loc="upper right", title=mc_opts["legend_title"], ncol=leg_ncols) if "legend_title" in mc_opts.keys() else ax.legend(handles,labels, loc="upper right", ncol=leg_ncols)
    #ax.legend(handles,labels, loc="upper right", title=mc_opts["legend_title"]) if "legend_title" in mc_opts.keys() else ax.legend(handles,labels, loc="upper right")
    #set_trace()
    
        ## plot data/MC ratio
    if maskData:
        rax.axhspan(0.5, 1.5, **{"linestyle": "--", "color": (0, 0, 0, 0.5), "linewidth": 1})
    else:
        plot.plotratio(data_dict.sum(data_dict.axes()[0].name), mc_dict.sum(mc_dict.axes()[0].name),
            ax=rax,
            error_opts=hstyles["data_err_opts"],
            denom_fill_opts={},
            guide_opts={},
            unc="num",
            overflow=overflow,
        )
    rax.set_ylabel("data/MC")
    rax.set_ylim(0.5, 1.5)
    rax.set_xlim(xlimits)
    
        ## set axes labels and titles
    rax.set_xlabel(xlabel)

    return ax, rax
 fig_norm.subplots_adjust(hspace=.07)
     # norm logy
 fig_norm_logy, (ax_norm_logy, rax_norm_logy) = plt.subplots(2, 1, gridspec_kw={"height_ratios": (3, 1)}, sharex=True)
 fig_norm_logy.subplots_adjust(hspace=.07)
 
 
     ## get values from NNLO root file
 #set_trace()
 #nnlo_binning = nnlo_dict[nnlo_var].axis("xaxis").edges()
 #nnlo_bins = hist.Bin("xaxis", "xaxis", nnlo_binning) # axis needs same name as nnlo hist
 
     # raw histo
 nnlo_histo = nnlo_dict[nnlo_var]#.integrate("dataset")
         # orig
 plot.plot1d(nnlo_histo,
     ax=ax, clear=False,
     line_opts={"linestyle" : "-", "color" : "k"},
 )
 plot.plot1d(nnlo_histo, ## need to plot errorbar separately
     ax=ax, clear=False,
     error_opts={"color": "k", "marker" : None},
 )
         # orig logy
 plot.plot1d(nnlo_histo,
     ax=ax_logy, clear=False,
     line_opts={"linestyle" : "-", "color" : "k"},
 )
 plot.plot1d(nnlo_histo, ## need to plot errorbar separately
     ax=ax_logy, clear=False,
     error_opts={"color": "k", "marker" : None},
 )
    # normalized
                         hslice = hslice.rebin(yaxis_name, y_rebinning)
 
 
                         # make 1D projection along dense axes
                     for dax in range(2):
                         fig, (ax, rax) = plt.subplots(2, 1, gridspec_kw={"height_ratios": (3, 1)}, sharex=True)
                         fig.subplots_adjust(hspace=.07)
 
                         #set_trace()
                         hproj = hslice.integrate(hslice.dense_axes()[dax].name)
                             ## plot comparison of perm categories
                         plot.plot1d(hproj[mc_samples],
                             overlay=hproj.axes()[0].name,
                             ax=ax,
                             clear=False,
                             stack=True,
                             line_opts=None,
                             fill_opts=stack_fill_opts,
                             error_opts=stack_error_opts,
                             order=['QCD', 'singlet', 'EWK', 'ttJets'],
                         )
                         if withData:
                             plot.plot1d(hproj[data_samples],
                                 overlay=hproj.axes()[0].name,
                                 ax=ax,
                                 clear=False,
                                 error_opts=hstyles['data_err_opts']
                             )
                         ax.autoscale(axis='x', tight=True)
                         ax.set_ylim(0, None)
                         ax.set_ylabel(None)
                         ax.set_xlabel(None)
    open(os.path.join(proj_dir, 'inputs',
                      '%s_lumis_data.json' % base_jobid)).read())
lumi_to_use = (data_lumi_dict[args.year]['Muons'] +
               data_lumi_dict[args.year]['Electrons']) / 2000.

## plot histograms
## plot simulation and nominal data
fig, ax = plt.subplots()
fig.subplots_adjust(hspace=.07)

# plot data
plot.plot1d(
    data_pu_histos[('data', )],
    ax=ax,
    clear=False,
    line_opts={
        'linestyle': '-',
        'color': 'k'
    },
    density=True,
)

# plot MC
mc_pu = mc_nTrueInt_histo['ttJets_PS'] if (
    args.year == '2016'
    and base_jobid == 'NanoAODv6') else mc_nTrueInt_histo['ttJetsSL']
plot.plot1d(
    mc_pu,
    ax=ax,
    clear=False,
    fill_opts={'facecolor': '#4daf4a'},  # green
    density=True,