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",
                 **{
                     "linestyle": "-",
                     "color": rewt_style_dict[rewt_key[0]][1]
                 })

    #set_trace()
    # plot yields
    # format axes
    ax.autoscale(axis="x", tight=True)
    ax.set_xlim(x_lims)
    ax.set_ylim(None)
Exemplo n.º 2
0
                                      **smooth_styles)  # smoothed template
                if not np.array_equal(flat_sys.values()[()],
                                      orig_sys.values()[()]):
                    hep.plot.histplot(flat_sys.values()[()],
                                      nominal.dense_axes()[0].edges(),
                                      ax=ax,
                                      histtype="step",
                                      **flat_styles)  # flattened template
                ax.legend(loc="upper right", title=f"{sys}, {proc}")
                ax.set_ylabel("Events")
                ax.autoscale()
                ax.set_ylim(ax.get_ylim()[0], ax.get_ylim()[1] * 1.15)

                # plot relative deviation
                orig_masked_vals, orig_masked_bins = Plotter.get_ratio_arrays(
                    num_vals=orig_sys.values()[()] - nominal.values()[()],
                    denom_vals=nominal.values()[()],
                    input_bins=nominal.dense_axes()[0].edges())
                rax.step(orig_masked_bins,
                         orig_masked_vals,
                         where='post',
                         **orig_styles)
                if not np.array_equal(smooth_sys.values()[()],
                                      orig_sys.values()[()]):
                    smooth_masked_vals, smooth_masked_bins = Plotter.get_ratio_arrays(
                        num_vals=smooth_sys.values()[()] -
                        nominal.values()[()],
                        denom_vals=nominal.values()[()],
                        input_bins=nominal.dense_axes()[0].edges())
                    rax.step(smooth_masked_bins,
                             smooth_masked_vals,
                             where='post',
                            vals, bins = hslice.values()[(
                            )], hslice.axis(xaxis_name).edges()
                            ax = Plotter.plot_1D(
                                vals,
                                bins,
                                xlimits=x_lims,
                                ax=ax,
                                histtype='step',
                                label=rewt_style_dict[rewt][0],
                                color=rewt_style_dict[rewt][1])

                            if (rewt != nominal_jobid) and (
                                    nominal_jobid in rewt_style_dict.keys()):
                                ratio_vals, ratio_bins = Plotter.get_ratio_arrays(
                                    num_vals=vals,
                                    denom_vals=allTT_hslices[nominal_jobid].
                                    values()[()],
                                    input_bins=bins)
                                rax.step(ratio_bins,
                                         ratio_vals,
                                         where='post',
                                         **{
                                             'linestyle': '-',
                                             'color': rewt_style_dict[rewt][1]
                                         })

                        ax.autoscale(axis='x', tight=True)
                        rax.autoscale(axis='x', tight=True)
                        ax.set_ylim(None)
                        rax.axhline(
                            1, **{
Exemplo n.º 4
0
                    clear=False,
                    line_opts={'linestyle': '-'},
                )
                ax.autoscale(axis='x', tight=True)
                ax.set_ylim(0, ax.get_ylim()[1] * 1.15)
                ax.set_xlabel(None)
                ax.set_xlim(x_lims)

                # plot ratios
                for sys in sorted([key[0] for key in hslice.values().keys()]):
                    if sys == 'nosys': continue
                    nom_histo = hslice['nosys'].integrate('sys')
                    sys_histo = hslice[sys].integrate('sys')

                    ratio_masked_vals, ratio_bins = Plotter.get_ratio_arrays(
                        num_vals=sys_histo.values()[()],
                        denom_vals=nom_histo.values()[()],
                        input_bins=nom_histo.dense_axes()[0].edges())
                    rax.step(ratio_bins,
                             ratio_masked_vals,
                             where='post',
                             **{
                                 'linestyle': '-',
                                 'color': systematics[sys][1],
                                 'linewidth': systematics[sys][2]
                             })

                rax.set_xlabel(xtitle)
                rax.set_ylabel('Sys/Nominal')
                rax.set_xlim(x_lims)
                rax.set_ylim(0.5, 1.5)
                rax.axhline(
                            vals, bins = (
                                allTT_hslice[rewt].integrate('rewt')).values()[
                                    ()], allTT_hslice.axis(xaxis_name).edges()
                            ax = Plotter.plot_1D(
                                vals,
                                bins,
                                xlimits=x_lims,
                                ax=ax,
                                histtype='step',
                                label=rewt_style_dict[rewt][0],
                                color=rewt_style_dict[rewt][1])

                            if (rewt != 'Nominal'):
                                ratio_vals, ratio_bins = Plotter.get_ratio_arrays(
                                    num_vals=vals,
                                    denom_vals=(
                                        allTT_hslice['Nominal'].integrate(
                                            'rewt')).values()[()],
                                    input_bins=bins)
                                rax.step(ratio_bins,
                                         ratio_vals,
                                         where='post',
                                         **{
                                             'linestyle': '-',
                                             'color': rewt_style_dict[rewt][1]
                                         })

                        ax.autoscale(axis='x', tight=True)
                        rax.autoscale(axis='x', tight=True)
                        ax.set_ylim(None)
                        rax.axhline(
                            1, **{
def get_bkg_templates(fnames_to_run):
    """
    Function that writes linearized mtt vs costheta distributions to root file.
    """

    #set_trace()
    for bkg_file in fnames_to_run:
        hdict = load(bkg_file)
        jmult = "3Jets" if "3Jets" in os.path.basename(bkg_file) else "4PJets"
        for lep in hdict.keys():
            for tname, orig_template in hdict[lep].items():

                proc = tname.split(
                    "_")[0] if not "data_obs" in tname else "data_obs"
                sys = sorted(filter(None, tname.split(f"{proc}_")))[0]

                #if not ((sys == "ueDOWN") and (proc == "ttJets")): continue
                if sys == "nosys": continue
                print(lep, jmult, sys, proc)

                nominal_hist = hdict[lep][f"{proc}_nosys"].copy()

                x_lims = (0, nominal_hist.dense_axes()[0].centers().size)

                # perform smoothing
                smoothed_histos_list = [(Plotter.smoothing_mttbins(
                    nosys=nominal_hist,
                    systematic=orig_template,
                    mtt_centers=mtt_centers,
                    nbinsx=len(linearize_binning[0]) - 1,
                    nbinsy=len(linearize_binning[1]) - 1,
                    **{"frac": frac_val / 10.}), frac_val / 10.)
                                        for frac_val in np.arange(2, 7, 2)]
                #smoothed_histos_chi2 = {frac_val :  find_chi2(h_fitted=smooth_histo, h_unc=orig_template) for smooth_histo, frac_val in smoothed_histos_list}
                # perform flattening
                flattened_histo = Plotter.flatten(nosys=nominal_hist,
                                                  systematic=orig_template)
                #flat_chi2 = find_chi2(h_fitted=flattened_histo, h_unc=orig_template)

                # plot relative deviation
                fig, ax = plt.subplots()
                fig.subplots_adjust(hspace=.07)

                # plot original dist
                orig_masked_vals, orig_masked_bins = Plotter.get_ratio_arrays(
                    num_vals=orig_template.values()[()] -
                    nominal_hist.values()[()],
                    denom_vals=nominal_hist.values()[()],
                    input_bins=nominal_hist.dense_axes()[0].edges())
                ax.fill_between(orig_masked_bins,
                                orig_masked_vals,
                                facecolor="k",
                                step="post",
                                alpha=0.5,
                                label="Unsmoothed")

                # plot smoothed versions
                for smooth_histo, frac_val in smoothed_histos_list:
                    smooth_masked_vals, smooth_masked_bins = Plotter.get_ratio_arrays(
                        num_vals=smooth_histo.values()[()] -
                        nominal_hist.values()[()],
                        denom_vals=nominal_hist.values()[()],
                        input_bins=nominal_hist.dense_axes()[0].edges())
                    ax.step(smooth_masked_bins,
                            smooth_masked_vals,
                            where="post",
                            **{
                                "linestyle": "-",
                                "label": f"Frac={frac_val}",
                                "linewidth": 2
                            })

                # plot flattened val
                flat_masked_vals, flat_masked_bins = Plotter.get_ratio_arrays(
                    num_vals=flattened_histo.values()[()] -
                    nominal_hist.values()[()],
                    denom_vals=nominal_hist.values()[()],
                    input_bins=nominal_hist.dense_axes()[0].edges())
                ax.step(flat_masked_bins,
                        flat_masked_vals,
                        where="post",
                        **{
                            "linestyle": "-",
                            "label": "Flat",
                            "linewidth": 2
                        })

                ax.legend(loc="upper right", title=f"{sys}, {proc}")
                ax.axhline(
                    0, **{
                        "linestyle": "--",
                        "color": (0, 0, 0, 0.5),
                        "linewidth": 1
                    })
                ax.autoscale()
                ax.set_xlim(x_lims)
                ax.set_xlabel(
                    "$m_{t\\bar{t}}$ $\otimes$ |cos($\\theta^{*}_{t_{l}}$)|")
                ax.set_ylabel("Rel. Deviaton from Nominal")

                # add lepton/jet multiplicity label
                ax.text(0.02,
                        0.94,
                        f"{leptypes[lep]}, {jet_mults[jmult]}",
                        fontsize=rcParams["font.size"] * 0.9,
                        horizontalalignment="left",
                        verticalalignment="bottom",
                        transform=ax.transAxes)
                ## draw vertical lines for distinguishing different ctstar bins
                vlines = [x_lims[1] * ybin / 5 for ybin in range(1, 5)]
                for vline in vlines:
                    ax.axvline(vline, color="k", linestyle="--")
                hep.cms.label(ax=ax,
                              data=False,
                              paper=False,
                              year=args.year,
                              lumi=round(data_lumi_year[f"{lep}s"] / 1000., 1))

                #set_trace()
                pltdir = os.path.join(outdir, lep, jmult, sys)
                if not os.path.isdir(pltdir):
                    os.makedirs(pltdir)

                #figname = os.path.join(pltdir, "_".join([jmult, lep, sys, proc, "BinWidths_Comp"]))
                #figname = os.path.join(pltdir, "_".join([jmult, lep, sys, proc, "SmoothValues_Comp"]))
                #figname = os.path.join(pltdir, "_".join([jmult, lep, sys, proc, "MttBinWidths_SmoothValues_Comp"]))
                figname = os.path.join(
                    pltdir,
                    "_".join([jmult, lep, sys, proc, "SmoothedFlatVals_Comp"]))
                fig.savefig(figname)
                print(f"{figname} written")
                plt.close()
def plot_bkg_templates(fnames_to_run):
    """
    Runs LOWESS smoothing algorithm ntoys times and finds 1 and 2 sigma bands for interpolation
    """

    for bkg_file in fnames_to_run:
        hdict = load(bkg_file)
        jmult = "3Jets" if "3Jets" in os.path.basename(bkg_file) else "4PJets"
        for tname, orig_template in hdict[args.lepton].items():

            proc = tname.split(
                "_")[0] if not "data_obs" in tname else "data_obs"
            sys = sorted(filter(None, tname.split(f"{proc}_")))[0]

            if proc == "BKG": continue
            #if sys not in ["hdampUP", "hdampDOWN", "mtop1665", "mtop1695", "mtop1715", "mtop1735", "mtop1755", "mtop1785", "ueUP", "ueDOWN"]: continue
            if sys == "nosys": continue
            print(args.lepton, jmult, sys, proc)

            nosys_hist = hdict[args.lepton][f"{proc}_nosys"].copy()
            orig_smooth_hist = Plotter.smoothing_mttbins(
                nosys=nosys_hist,
                systematic=orig_template,
                mtt_centers=mtt_centers,
                nbinsx=nbinsx,
                nbinsy=nbinsy)

            x_lims = (0, nosys_hist.dense_axes()[0].centers().size)

            # get vals and errors of systematic variation
            sys_histo_vals, sys_histo_sumw2 = orig_template.values(
                sumw2=True)[()]
            sys_histo_errs = np.sqrt(sys_histo_sumw2)

            # make toys based on Gaussian distribution of mu=bin_val, sigma=bin_error
            toy_arrays = np.zeros((nbins, ntoys))
            for idx in range(nbins):
                toy_arrays[idx] = np.random.normal(sys_histo_vals[idx],
                                                   sys_histo_errs[idx],
                                                   size=ntoys)

                # get smoothed relative deviation distributions from toys
            smoothed_rel_dev_arrays = np.zeros((ntoys, nbins))
            chi2_pvals = np.zeros((ntoys, 2))
            for idx in range(ntoys):
                smoothed_array = Plotter.smoothing_mttbins(
                    nosys=nosys_hist,
                    systematic=(toy_arrays.T)[idx],
                    mtt_centers=mtt_centers,
                    nbinsx=nbinsx,
                    nbinsy=nbinsy)
                chi2_pval = chisquare(
                    f_obs=smoothed_array, f_exp=orig_smooth_hist.values()[()]
                )  # convert to expected yields so inputs are greater than 5
                chi2_pvals[idx] = np.array(
                    [chi2_pval.statistic, chi2_pval.pvalue])
                smoothed_rel_dev_arrays[idx] = (
                    smoothed_array -
                    nosys_hist.values()[()]) / nosys_hist.values()[()]

                ## find 68% and 95% intervals
            plus_one_sigma_smooth_vals, minus_one_sigma_smooth_vals = np.zeros(
                nbins), np.zeros(nbins)
            plus_two_sigma_smooth_vals, minus_two_sigma_smooth_vals = np.zeros(
                nbins), np.zeros(nbins)
            for bin in range(nbins):
                plus_one_sigma_smooth_vals[bin] = np.sort(
                    smoothed_rel_dev_arrays[:, bin])[plus_one_sigma_ind]
                minus_one_sigma_smooth_vals[bin] = np.sort(
                    smoothed_rel_dev_arrays[:, bin])[minus_one_sigma_ind]
                plus_two_sigma_smooth_vals[bin] = np.sort(
                    smoothed_rel_dev_arrays[:, bin])[plus_two_sigma_ind]
                minus_two_sigma_smooth_vals[bin] = np.sort(
                    smoothed_rel_dev_arrays[:, bin])[minus_two_sigma_ind]

            # plot relative deviation
            fig, ax = plt.subplots()
            fig.subplots_adjust(hspace=.07)

            # original relative deviations
            orig_masked_vals, orig_masked_bins = Plotter.get_ratio_arrays(
                num_vals=orig_template.values()[()] - nosys_hist.values()[()],
                denom_vals=nosys_hist.values()[()],
                input_bins=nosys_hist.dense_axes()[0].edges())
            ax.step(orig_masked_bins,
                    orig_masked_vals,
                    where="post",
                    **{
                        "color": "k",
                        "linestyle": "-",
                        "label": "Original"
                    })
            # original smoothing relative deviations
            orig_smoothed_masked_vals, orig_smoothed_masked_bins = Plotter.get_ratio_arrays(
                num_vals=orig_smooth_hist.values()[()] -
                nosys_hist.values()[()],
                denom_vals=nosys_hist.values()[()],
                input_bins=nosys_hist.dense_axes()[0].edges())
            ax.step(orig_smoothed_masked_bins,
                    orig_smoothed_masked_vals,
                    where="post",
                    **{
                        "color": "r",
                        "linestyle": "-",
                        "label": "Original Smoothing"
                    })
            # plot 68 and 95% intervals for yields
            ax.fill_between(nosys_hist.dense_axes()[0].edges(),
                            np.r_[minus_one_sigma_smooth_vals,
                                  minus_one_sigma_smooth_vals[-1]],
                            np.r_[plus_one_sigma_smooth_vals,
                                  plus_one_sigma_smooth_vals[-1]],
                            where=np.r_[plus_one_sigma_smooth_vals,
                                        plus_one_sigma_smooth_vals[-1]] >
                            np.r_[minus_one_sigma_smooth_vals,
                                  minus_one_sigma_smooth_vals[-1]],
                            step="post",
                            **{
                                "label": "68%",
                                "facecolor": "#00cc00",
                                "alpha": 0.5
                            })
            ax.fill_between(nosys_hist.dense_axes()[0].edges(),
                            np.r_[minus_two_sigma_smooth_vals,
                                  minus_two_sigma_smooth_vals[-1]],
                            np.r_[plus_two_sigma_smooth_vals,
                                  plus_two_sigma_smooth_vals[-1]],
                            where=np.r_[plus_two_sigma_smooth_vals,
                                        plus_two_sigma_smooth_vals[-1]] >
                            np.r_[minus_two_sigma_smooth_vals,
                                  minus_two_sigma_smooth_vals[-1]],
                            step="post",
                            **{
                                "label": "95%",
                                "facecolor": "#ffcc00",
                                "alpha": 0.5
                            })

            ax.legend(loc="upper right", title=f"{sys}, {proc}")
            ax.axhline(
                0, **{
                    "linestyle": "--",
                    "color": (0, 0, 0, 0.5),
                    "linewidth": 1
                })
            ax.autoscale()
            ax.set_ylim(ax.get_ylim()[0], ax.get_ylim()[1] * 1.15)
            ax.set_xlim(x_lims)
            ax.set_xlabel(
                "$m_{t\\bar{t}}$ $\otimes$ |cos($\\theta^{*}_{t_{l}}$)|")
            ax.set_ylabel("Rel. Deviaton from Nominal")

            # add lepton/jet multiplicity label
            ax.text(0.02,
                    0.94,
                    f"{leptypes[args.lepton]}, {jet_mults[jmult]}",
                    fontsize=rcParams["font.size"] * 0.9,
                    horizontalalignment="left",
                    verticalalignment="bottom",
                    transform=ax.transAxes)
            ## draw vertical lines for distinguishing different ctstar bins
            vlines = [x_lims[1] * ybin / 5 for ybin in range(1, 5)]
            for vline in vlines:
                ax.axvline(vline, color="k", linestyle="--")
            hep.cms.label(ax=ax,
                          data=False,
                          paper=False,
                          year=args.year,
                          lumi=round(data_lumi_year[f"{args.lepton}s"] / 1000.,
                                     1))

            #set_trace()
            pltdir = os.path.join(outdir, args.lepton, jmult, sys)
            if not os.path.isdir(pltdir):
                os.makedirs(pltdir)

            figname = os.path.join(
                pltdir, "_".join([
                    jmult, args.lepton, sys, proc,
                    "SmoothingConfidenceIntervals"
                ]))
            fig.savefig(figname)
            print(f"{figname} written")
            plt.close()
        plot.plot1d(tune_histo, ## need to plot errorbar separately
            ax=ax, clear=False,
            error_opts={"color": style_dict[year][1], "marker" : None},
        )
                # logy
        plot.plot1d(tune_histo,
            ax=ax_logy, clear=False,
            line_opts={"linestyle" : "-", "color" : style_dict[year][1]},
        )
        plot.plot1d(tune_histo, ## need to plot errorbar separately
            ax=ax_logy, clear=False,
            error_opts={"color": style_dict[year][1], "marker" : None},
        )

                ## plot ratios
        ratio_vals, ratio_bins = Plotter.get_ratio_arrays(num_vals=nnlo_histo.values()[()], denom_vals=tune_histo.values()[()], input_bins=nnlo_histo.dense_axes()[0].edges())
                    # orig
        rax.step(ratio_bins, ratio_vals, where="post", **{"linestyle" : "-", "color" : style_dict[year][1]})
                    # logy
        rax_logy.step(ratio_bins, ratio_vals, where="post", **{"linestyle" : "-", "color" : style_dict[year][1]})

            # plot normalized
        tune_normed_histo = tune_histo.copy()
        tune_normed_histo.scale(1./tune_normed_histo.values()[()].sum())
                    # orig
        plot.plot1d(tune_normed_histo,
            ax=ax_norm, clear=False,
            line_opts={"linestyle" : "-", "color" : style_dict[year][1]},
        )
        plot.plot1d(tune_normed_histo, ## need to plot errorbar separately
            ax=ax_norm, clear=False,