Example #1
0
                        'jmult').integrate('lepcat').integrate('btag')

                    # split hslice into signal samples/SM MC+data
                    sig_hslice = hslice[Plotter.signal_samples]
                    SM_hslice = hslice[Plotter.nonsignal_samples]

                    if hname == 'Jets_njets':
                        print(jmult)
                        yields_txt, yields_json = Plotter.get_samples_yield_and_frac(
                            hslice,
                            data_lumi_year['%ss' % args.lepton] / 1000.)
                        yields_fname = os.path.join(
                            pltdir,
                            '%s_%s_yields_and_fracs' % (jmult, args.lepton))
                        plt_tools.print_table(yields_txt,
                                              filename='%s.txt' % yields_fname,
                                              print_output=True)
                        with open('%s.json' % yields_fname, 'w') as out:
                            out.write(prettyjson.dumps(yields_json))

                        # plot signal
                    if sig_hslice.values():
                        for signal in sig_hslice.values().keys():
                            fig, ax = plt.subplots()
                            fig.subplots_adjust(hspace=.07)

                            sig_hist = sig_hslice[signal].integrate('process')
                            Plotter.plot_1D(
                                *sig_hist.values().values(),
                                sig_hist.axis(xaxis_name).edges(),
                                xlabel=xtitle,
        mtt_ct_interp_yields == mtt_ct_interp_yields[0]
    )  # values are all equal up to specified decimal place
    if mtt_ct_interp_same:
        mtt_ct_interp_yield = mtt_ct_interp_yields[0]

    if not (nom_same and pt_same and mtt_ct_same and pt_interp_same
            and mtt_ct_interp_same):
        dec_tolerance -= 1

rows = [("Lumi: %s fb^-1" % format(lumi_to_use, ".1f"), "",
         "Values are the same up to %i decimal places" % dec_tolerance)]
rows += [("Reweighting", "Yield", "Yield/Nominal")]
rows += [("Nominal", format(nom_yield, ".%if" % dec_tolerance),
          format(nom_yield / nom_yield, ".%if" % dec_tolerance))]
rows += [("thad_pt", format(pt_yield, ".%if" % dec_tolerance),
          format(pt_yield / nom_yield, ".%if" % dec_tolerance))]
rows += [("mtt_vs_thad_ctstar", format(mtt_ct_yield, ".%if" % dec_tolerance),
          format(mtt_ct_yield / nom_yield, ".%if" % dec_tolerance))]
rows += [("thad_pt_interp", format(pt_interp_yield, ".%if" % dec_tolerance),
          format(pt_interp_yield / nom_yield, ".%if" % dec_tolerance))]
rows += [("mtt_vs_thad_ctstar_interp",
          format(mtt_ct_interp_yield, ".%if" % dec_tolerance),
          format(mtt_ct_interp_yield / nom_yield, ".%if" % dec_tolerance))]

yields_fname = os.path.join(outdir, "reweighting_yields.txt")
plt_tools.print_table(rows,
                      filename=yields_fname,
                      print_output=True,
                      header_line=1)
print(f"{yields_fname} written")
Example #3
0
                            fig, ax = plt.subplots()
                        fig.subplots_adjust(hspace=.07)
                        hslice = histo[:, btag_applied, jmult, lepcat,
                                       btagregion].integrate(
                                           'jmult'
                                       ).integrate('lepcat').integrate(
                                           'btag').integrate('btagging')

                        if hname == 'Jets_njets':
                            print(jmult)
                            yields_txt, yields_json = Plotter.get_samples_yield_and_frac(
                                hslice,
                                data_lumi_year['%ss' % args.lepton] / 1000.)
                            plt_tools.print_table(
                                yields_txt,
                                filename='%s/%s_%s_yields_and_fracs.txt' %
                                (pltdir, jmult, args.lepton),
                                print_output=True)
                            with open(
                                    '%s/%s_%s_yields_and_fracs.json' %
                                (pltdir, jmult, args.lepton), 'w') as out:
                                out.write(prettyjson.dumps(yields_json))

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

                        mc_opts = {
                            'mcorder': ['QCD', 'EWK', 'singlet', 'ttJets']
                        }
                        if withData:
Example #4
0
                        ax = Plotter.plot_mc1d(ax,
                                               hslice,
                                               xlabel=xtitle,
                                               xlimits=x_lims,
                                               **mc_opts)

                    if hname == 'Jets_njets':
                        print(jmult)
                        yields_txt, yields_json = Plotter.get_samples_yield_and_frac(
                            hslice,
                            data_lumi_year['%ss' % args.lepton] / 1000.,
                            promptmc=True)
                        frac_name = '%s_yields_and_fracs' % '_'.join(
                            [jmult, args.lepton, lepcat, btagregion])
                        plt_tools.print_table(yields_txt,
                                              filename='%s/%s.txt' %
                                              (pltdir, frac_name),
                                              print_output=True)
                        print('%s/%s.txt written' % (pltdir, frac_name))
                        with open('%s/%s.json' % (pltdir, frac_name),
                                  'w') as out:
                            out.write(prettyjson.dumps(yields_json))

                        # add lepton/jet multiplicity label
                    #set_trace()
                    ax.text(0.02,
                            0.88 if withData else 0.90,
                            "%s, %s\n%s" % (lep_cats[lepcat], jet_mults[jmult],
                                            btag_cats[btagregion]),
                            fontsize=rcParams['font.size'] * 0.75,
                            horizontalalignment='left',
                            verticalalignment='bottom',
Example #5
0
            ax_norm.text(
                0.02, 0.90, "$e/\mu$, %s\nParton Level" % jet_mults[jmult],
                fontsize=rcParams['font.size'], horizontalalignment='left', verticalalignment='bottom', transform=ax_norm.transAxes
            )
                ## set axes labels and titles
            hep.cms.label(ax=ax_norm, data=False, paper=False, year=args.year, lumi=round(lumi_to_use, 1))

            #set_trace()
            figname_norm = os.path.join(pltdir, '_'.join([args.year, jobid, jmult, 'Group_Cats', hname, 'Norm']))
            fig_norm.savefig(figname_norm)
            print('%s written' % figname_norm)
            plt.close()


                    ## make table
            rows = [("Lumi: %s fb^-1" % format(round(lumi_to_use, 1), '.1f'), 'e/mu', jmult, "")]
            rows += [("Event Type", "Yield", "Error", "Frac of l+jets ttbar events")]
            for cat in evt_groups.keys():
                rows += [(cat, format(general_cat_histo.values(overflow='all', sumw2=True)[(cat,)][0].sum(), '.3f'), format(np.sqrt(general_cat_histo.values(overflow='all', sumw2=True)[(cat,)][1].sum()), '.3f'), format(general_cat_histo.values(overflow='all', sumw2=True)[(cat,)][0].sum()/general_cat_histo.integrate('gen_evt_cat').values(overflow='all')[()].sum(), '.5f'))]

            rows += [("", "", "", "")]
            for cat in indiv_evt_groups.keys():
                if cat == 'Other': continue
                rows += [(cat, format(indiv_cat_histo.values(overflow='all', sumw2=True)[(cat,)][0].sum(), '.3f'), format(np.sqrt(indiv_cat_histo.values(overflow='all', sumw2=True)[(cat,)][1].sum()), '.3f'), format(indiv_cat_histo.values(overflow='all', sumw2=True)[(cat,)][0].sum()/indiv_cat_histo.integrate('all_evt_cat').values(overflow='all')[()].sum(), '.5f'))]

            #set_trace()
            cat_fractions_name = os.path.join(pltdir, '%s_%s_Event_Categories_%s_yields_and_fractions.txt' % (args.year, jobid, jmult))
            plt_tools.print_table(rows, filename=cat_fractions_name, header_line=1, print_output=True)
            print('%s written' % cat_fractions_name)

                                        '%s %s' % (proc, decay), )][1].sum()
                                ), hslice.values(overflow='all', sumw2=True)[
                                    ('%s %s' %
                                     (proc, decay), )][0].sum() / sigreg_ntot
                            rows += [("", name, format(proc_yield, '.2f'),
                                      format(proc_err,
                                             '.2f'), format(proc_frac, '.6f'))]
                        rows += [("", "", "", "", "")]

                    #set_trace()
                    # nevents
                    ttdecay_name = '%s_ttdecay_yields' % '_'.join(
                        [jmult, args.lepton, lepcat, btagregion])
                    plt_tools.print_table(rows,
                                          filename='%s/%s.txt' %
                                          (pltdir, ttdecay_name),
                                          header_line=1,
                                          print_output=True)
                    print('%s/%s.txt written' % (pltdir, ttdecay_name))

        ## combine jet channels, so only depends on lepton
        #set_trace()
        lep_histo = histo[:, :, 'btagPass', 'Tight'].integrate(
            'process').integrate('jmult').integrate('lepcat').integrate('btag')

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

        rows = [("Lumi: %s fb^-1" %
                 format(data_lumi_year['%ss' % args.lepton] / 1000., '.1f'),
                    Plotter.plot_1D(hslice.values()[('Before',)], hslice.axis(xaxis_name).edges(), xlimits=x_lims, ax=ax, label='Runs $<$ 319077')
                    Plotter.plot_1D(hslice.values()[('After',)], hslice.axis(xaxis_name).edges(), xlimits=x_lims, xlabel=xtitle, color='r', ax=ax, label='Runs $\\geq$ 319077')
                    ax.legend(loc='upper right')

                    if hname == 'Jets_njets':
                        print(jmult)
                        #set_trace() 
                        rows = [("Lumi: %s fb^-1" % format(data_lumi_year['%ss' % args.lepton]/1000., '.1f'), "Yield", "Error", "Frac")]
                        rows += [("Runs < 319077", format(sum(hslice.values(overflow='all')[('Before',)]), '.1f'), format(np.sqrt(sum(hslice.values(overflow='all', sumw2=True)[('Before',)][1])), '.1f'),
                            format(sum(hslice.values(overflow='all')[('Before',)])/sum(hslice.sum('hem').values(overflow='all')[()]), '.3f'))]
                        rows += [("Runs >= 319077", format(sum(hslice.values(overflow='all')[('After',)]), '.1f'), format(np.sqrt(sum(hslice.values(overflow='all', sumw2=True)[('After',)][1])), '.1f'),
                            format(sum(hslice.values(overflow='all')[('After',)])/sum(hslice.sum('hem').values(overflow='all')[()]), '.3f'))]
                        rows += [("Total", format(sum(hslice.sum('hem').values(overflow='all')[()]), '.1f'), format(np.sqrt(sum(hslice.sum('hem').values(overflow='all', sumw2=True)[()][1])), '.1f'), "")]

                        frac_name = '%s_yields_and_fracs.txt' % '_'.join([jmult, args.lepton, lepcat, btagregion])
                        plt_tools.print_table(rows, filename=os.path.join(pltdir, frac_name), print_output=True)
                        print('%s/%s.txt written' % (pltdir, frac_name))

                        # add lepton/jet multiplicity label
                    ax.text(
                        0.02, 0.90, "%s, %s\n%s" % (lep_cats[lepcat], jet_mults[jmult], btag_cats[btagregion]),
                        fontsize=rcParams['font.size']*0.75, horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes
                    )
                    hep.cms.label(ax=ax, data=True, paper=False, year=args.year, lumi=round(data_lumi_year['%ss' % args.lepton]/1000., 1))

                    #set_trace()
                    figname = os.path.join(pltdir, '_'.join([jmult, args.lepton, lepcat, btagregion, hname]))
                    fig.savefig(figname)
                    print('%s written' % figname)
                    plt.close()