def photon_sf_plot(tag): for year in [2017, 2018]: opts = markers('data') # opts['markersize'] = 5 # opts['fillstyle'] = 'none' opts.pop('emarker', '') fig = plt.gcf() ax = plt.gca() x, y, yerr = {}, {}, {} fnum = f'output/{tag}/table_g_HLT_PFHT1050_photon_pt0_JetHT_{year}.txt' fden = f'output/{tag}/table_g_HLT_PFHT590_photon_pt0_GJets_HT_MLM_{year}.txt' xnum, _, ynum, yerrnum = get_xy(fnum) xden, _, yden, yerrden = get_xy(fden) x = xnum y = ynum / yden dy = ratio_unc(ynum, yden, yerrnum, yerrden) ax.errorbar(x, y, yerr=dy, label=f'{year}', **opts) # for f in files: plot(f) outdir = f"./output/{tag}" # ax.set_ylim(0.9,1) ax.legend() ax.set_ylabel("Data / MC SF") ax.xaxis.set_major_locator(MultipleLocator(200)) ax.xaxis.set_minor_locator(MultipleLocator(50)) ax.yaxis.set_major_locator(MultipleLocator(0.05)) ax.yaxis.set_minor_locator(MultipleLocator(0.01)) ax.set_ylim(0.95, 1.02) ax.grid(1) # rax.set_ylim(0.95,1.05) # rax.yaxis.set_major_locator(MultipleLocator(0.05)) # rax.yaxis.set_minor_locator(MultipleLocator(0.01)) # rax.grid(1) # rax.set_xlabel("Recoil or $p_{T}^{miss}$ (GeV)") # rax.set_ylabel(r"Ratio to single-$\mu$") # rax.plot([0,1000],[0.99,0.99],color='blue') # rax.plot([0,1000],[1.01,1.01],color='blue') # rax.plot([250,250],[0.95,0.95],color='blue') ax.plot([215, 215], [0.9, 1.1], color='red', zorder=-1) plt.text(1., 1., r"%.1f fb$^{-1}$ (13 TeV)" % lumi(year), fontsize=16, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes) plt.text(0., 1., f'{year}', fontsize=16, horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes) fig.savefig(pjoin(outdir, f'photon_sf_{year}.pdf')) fig.clear() plt.close(fig)
def region_comparison_plot(tag): for year in [2017,2018]: regions = ['1m', '2m', '1e','1m_hlt','2m_hlt'] opts = markers('data') opts['markersize'] = 5. opts['fillstyle'] = 'none' emarker = opts.pop('emarker', '') fig, ax, rax = fig_ratio() x, y, yerr = {}, {}, {} for region in regions: if region.endswith('e'): file = f'output/{tag}/table_{region}_EGamma_{year}.txt' else: file = f'output/{tag}/table_{region}_SingleMuon_{year}.txt' x[region], y[region], yerr[region] = get_xy(file) opts['color'] = colors[region] ax.errorbar(x[region], y[region], yerr=yerr[region],label=f'{region} region', **opts) # opts.pop('elinewidth') if region=='1m': continue rax.errorbar(x['1m'], y[region]/y['1m'], yerr[region]/y['1m'], **opts) # for f in files: plot(f) outdir = f"./output/{tag}" # ax.set_ylim(0.9,1) ax.legend() ax.set_ylabel("Efficiency") ax.xaxis.set_major_locator(MultipleLocator(200)) ax.xaxis.set_minor_locator(MultipleLocator(50)) ax.yaxis.set_major_locator(MultipleLocator(0.05)) ax.yaxis.set_minor_locator(MultipleLocator(0.01)) ax.set_ylim(0.9,1.02) ax.grid(1) rax.set_ylim(0.9,1.1) rax.grid(1) rax.set_xlabel("Recoil or $p_{T}^{miss}$ (GeV)") rax.set_ylabel(r"Ratio to single-$\mu$") plt.text(1., 1., r"$\approx$ %.1f fb$^{-1}$ (13 TeV)" % lumi(year), fontsize=16, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes ) plt.text(0., 1., f'{year}', fontsize=16, horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes ) fig.savefig(pjoin(outdir, f'region_comparison_data_{year}.pdf')) fig.clear() plt.close(fig)
def plot_recoil(acc, region_tag="1m", dataset='SingleMuon', year=2018, tag="test", distribution="recoil"): h = acc[distribution] h = merge_extensions(h) # h = scale_to_xs(h, acc) h = merge_datasets(h) newbin = hist.Bin(distribution,f"{distribution} (GeV)",np.array(list(range(0,400,20)) + list(range(400,1100,100)))) h = h.rebin(h.axis(distribution), newbin) ds = f'{dataset}_{year}' h = h.project(h.axis('dataset'), ds) # print(h) hnum = h.project(h.axis('region'),f'tr_{region_tag}_num') hden = h.project(h.axis('region'),f'tr_{region_tag}_den') # print(hden, hnum) # Recoil plot try: fig, ax,_ = hist.plot1d(hnum, binwnorm=True) except KeyError: print(f'ERROR: {region_tag}, {dataset}, {year}') return hist.plot1d(hden, ax=ax, clear=False, binwnorm=True) plt.yscale('log') plt.gca().set_ylim(0.1,1e6) outdir = f"./output/{tag}" if not os.path.exists(outdir): os.makedirs(outdir) fig.savefig(pjoin(outdir, f'{distribution}_{region_tag}_{dataset}_{year}.pdf')) with open(pjoin(outdir,f'table_{region_tag}_{dataset}_{year}.txt'),"w") as f: f.write(content_table(hnum, hden) + "\n") plt.close(fig) # Efficiency fig, ax,_ = hist.plotratio(hnum, hden, guide_opts={}, unc='clopper-pearson', error_opts=markers('data') ) ax.set_ylim(0,1.1) ax.set_xlim(0,xmax) ax.set_ylabel("Efficiency") plt.text(1., 1., r"$\approx$ %.1f fb$^{-1}$ (13 TeV)" % lumi(year), fontsize=16, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes ) plt.text(0., 1., f'{region_tag}, {year}', fontsize=16, horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes ) plt.text(1., 0., f'{trgname(year, tag)}', fontsize=10, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes ) plt.plot([0,xmax],[0.95,0.95],'r-') fig.savefig(pjoin(outdir, f'eff_{region_tag}_{dataset}_{year}.pdf')) plt.close(fig)
def sf_comparison_plot(tag): for year in [2017,2018]: regions = ['1m', '2m', '1m_hlt', '2m_hlt'] opts = markers('data') opts['markersize'] = 5 opts['fillstyle'] = 'none' emarker = opts.pop('emarker', '') fig, ax, rax = fig_ratio() x, y, yerr = {}, {}, {} for region in regions: if '1e' in region: fnum = f'output/{tag}/table_{region}_EGamma_{year}.txt' fden = f'output/{tag}/table_{region}_WJetsToLNu-MLM_{year}.txt' elif '1m' in region: fnum = f'output/{tag}/table_{region}_SingleMuon_{year}.txt' fden = f'output/{tag}/table_{region}_WJetsToLNu-MLM_{year}.txt' elif '2m' in region: fnum = f'output/{tag}/table_{region}_SingleMuon_{year}.txt' fden = f'output/{tag}/table_{region}_DYNJetsToLL_M-50-MLM_{year}.txt' xnum, ynum, yerrnum = get_xy(fnum) xden, yden, yerrden = get_xy(fden) x[region] = xnum y[region] = ynum / yden yerr[region] = ratio_unc(ynum, yden, yerrnum, yerrden) opts['color'] = colors[region] opts['marker'] = region_marker[region] ax.errorbar(x[region], y[region], yerr=yerr[region],label=f'{region} region', **opts) # opts.pop('elinewidth') if region=='1m': continue rax.errorbar(x['1m'], y[region]/y['1m'], ratio_unc(y[region],y['1m'],yerr[region],yerr['1m']), **opts) # for f in files: plot(f) outdir = f"./output/{tag}" # ax.set_ylim(0.9,1) ax.legend() ax.set_ylabel("Data / MC SF") ax.xaxis.set_major_locator(MultipleLocator(200)) ax.xaxis.set_minor_locator(MultipleLocator(50)) ax.yaxis.set_major_locator(MultipleLocator(0.05)) ax.yaxis.set_minor_locator(MultipleLocator(0.01)) ax.set_ylim(0.9,1.1) ax.grid(1) rax.set_ylim(0.95,1.05) rax.yaxis.set_major_locator(MultipleLocator(0.05)) rax.yaxis.set_minor_locator(MultipleLocator(0.01)) rax.grid(1) rax.set_xlabel("Recoil or $p_{T}^{miss}$ (GeV)") rax.set_ylabel(r"Ratio to single-$\mu$") plt.text(1., 1., r"$\approx$ %.1f fb$^{-1}$ (13 TeV)" % lumi(year), fontsize=16, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes ) plt.text(0., 1., f'{year}', fontsize=16, horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes ) fig.savefig(pjoin(outdir, f'sf_comparison_{year}.pdf')) fig.clear() plt.close(fig)
def plot_recoil(acc, xmax=1e3, ymin=0, ymax=1.1, region_tag="1m", dataset='SingleMuon', year=2018, tag="test", distribution="recoil", axis_name=None, noscale=False, jeteta_config=None, output_format='pdf'): # Select and prepare histogram h = copy.deepcopy(acc[distribution]) h = merge_extensions(h, acc, reweight_pu=('nopu' in distribution), noscale=noscale) if not noscale: scale_xs_lumi(h) h = merge_datasets(h) # Rebinning axis_name = distribution if not axis_name else axis_name if 'photon' in distribution: newbin = hist.Bin( axis_name, f"{axis_name} (GeV)", np.array( list(range(0, 250, 10)) + list(range(250, 400, 50)) + list(range(400, 1100, 100)))) elif distribution == 'mjj': newbin = hist.Bin( axis_name, r'$M_{jj}$ (GeV)', np.array( list(range(200, 600, 200)) + list(range(600, 1500, 300)) + [1500, 2000, 2750, 3500])) else: newbin = hist.Bin( axis_name, f"{axis_name} (GeV)", np.array(list(range(0, 500, 25)) + list(range(500, 1100, 100)))) h = h.rebin(h.axis(axis_name), newbin) ds = f'{dataset}_{year}' # Pick dataset and regions h = h.integrate(h.axis('dataset'), ds) if jeteta_config: hnum = h.integrate(h.axis('region'), f'tr_{region_tag}_num_{jeteta_config}') hden = h.integrate(h.axis('region'), f'tr_{region_tag}_den_{jeteta_config}') else: hnum = h.integrate(h.axis('region'), f'tr_{region_tag}_num') hden = h.integrate(h.axis('region'), f'tr_{region_tag}_den') # Recoil plot try: fig, ax, _ = hist.plot1d(hnum, binwnorm=True) except KeyError: pprint(h.axis('region').identifiers()) print(f'ERROR: {region_tag}, {dataset}, {year}') return hist.plot1d(hden, ax=ax, clear=False, binwnorm=True) plt.yscale('log') plt.gca().set_ylim(0.1, 1e6) outdir = f"./output/{tag}" if not os.path.exists(outdir): os.makedirs(outdir) outname = f'{region_tag}{"_noscale_" if noscale else "_"}{distribution}_{dataset}_{year}{"_"+jeteta_config if jeteta_config else ""}' fig.savefig(pjoin(outdir, f'{outname}.{output_format}')) with open(pjoin(outdir, f'table_{outname}.txt'), "w") as f: f.write(content_table(hnum, hden, axis_name) + "\n") plt.close(fig) # Efficiency plot fig, ax, _ = hist.plotratio(hnum, hden, guide_opts={}, unc='clopper-pearson', error_opts=markers('data')) ax.set_ylim(ymin, ymax) ax.set_xlim(0, xmax) ax.set_ylabel("Efficiency") plt.text(1., 1., r"%.1f fb$^{-1}$ (13 TeV)" % lumi_by_region(region_tag, year), fontsize=16, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes) plt.text(1., 0.95, f'{jeteta_config if jeteta_config else ""}', fontsize=12, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes) plt.text(0., 1., f'{region_tag}, {year}', fontsize=16, horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes) plt.text(1., 0., f'{trgname(year, tag)}', fontsize=10, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes) if 'g_' in region_tag: plt.plot([215, 215], [0.8, 1.1], 'r-') plt.plot([0, xmax], [0.95, 0.95], 'r-') fig.savefig(pjoin(outdir, f'eff_{outname}.pdf')) plt.close(fig)
def data_mc_comparison_plot(tag, ymin=0, ymax=1.1, distribution='recoil', jeteta_config=None, output_format='pdf'): if 'gamma' in tag: regions = [ 'g_HLT_PFHT1050', 'g_HLT_PFHT590', 'g_HLT_PFHT680', 'g_HLT_PFHT780', 'g_HLT_PFHT890' ] elif 'recoil' in tag: regions = ['1m', '2m'] else: regions = ['1m', '2m', '1e', '2m_hlt'] opts = markers('data') # opts['markersize'] = 5 # opts['fillstyle'] = 'none' emarker = opts.pop('emarker', '') outdir = f"./output/{tag}" outpath = pjoin(outdir, f'trig_sf.root') try: outfile = uproot.recreate(outpath) except OSError: outfile = uproot.update(outpath) for year in [2017, 2018]: for region in regions: fig, ax, rax = fig_ratio() if '1e' in region: fnum = f'output/{tag}/table_{region}_met_EGamma_{year}.txt' fden = f'output/{tag}/table_{region}_met_WJetsToLNu_HT_MLM_{year}.txt' xlabel = "$p_{T}^{miss}$ (GeV)" elif '1m' in region: fnum = f'output/{tag}/table_{region}_recoil_SingleMuon_{year}{"_"+jeteta_config if jeteta_config else ""}.txt' fden = f'output/{tag}/table_{region}_recoil_WJetsToLNu_HT_MLM_{year}{"_"+jeteta_config if jeteta_config else ""}.txt' xlabel = "Recoil (GeV)" elif '2m' in region: fnum = f'output/{tag}/table_{region}_recoil_SingleMuon_{year}{"_"+jeteta_config if jeteta_config else ""}.txt' fden = f'output/{tag}/table_{region}_recoil_VDYJetsToLL_M-50_HT_MLM_{year}{"_"+jeteta_config if jeteta_config else ""}.txt' xlabel = "Recoil (GeV)" elif 'g_' in region: fnum = f'output/{tag}/table_{region}_photon_pt0_JetHT_{year}.txt' fden = f'output/{tag}/table_{region}_photon_pt0_GJets_HT_MLM_{year}.txt' xlabel = "Photon $p_{T}$ (GeV)" if not os.path.exists(fnum): print(f"File not found {fnum}") continue if not os.path.exists(fden): print(f"File not found {fden}") continue xnum, xedgnum, ynum, yerrnum = get_xy(fnum) xden, xedgden, yden, yerrden = get_xy(fden) xsf = xnum ysf = ynum / yden ysferr = ratio_unc(ynum, yden, yerrnum, yerrden) opts['color'] = 'k' ax.errorbar(xnum, ynum, yerr=yerrnum, label=f'Data, {region} region', **opts) opts['color'] = 'r' ax.errorbar(xden, yden, yerr=yerrden, label=f'MC, {region} region', **opts) # rax.plot([0,1000],[0.98,0.98],color='blue') # rax.plot([0,1000],[0.99,0.99],color='blue',linestyle='--') if 'g_' in region: ax.plot([215, 215], [0.9, 1.1], color='blue') rax.plot([215, 215], [0.95, 1.05], color='blue') elif distribution == 'recoil': ax.plot([250, 250], [0.0, 1.1], color='blue') rax.plot([250, 250], [0.95, 1.05], color='blue') else: ax.plot([250, 250], [0.9, 1.1], color='blue') rax.plot([250, 250], [0.95, 1.05], color='blue') opts['color'] = 'k' rax.errorbar(xsf, ysf, ysferr, **opts) ax.legend() ax.set_ylabel("Efficiency") ax.xaxis.set_major_locator(MultipleLocator(200)) ax.xaxis.set_minor_locator(MultipleLocator(50)) ax.set_ylim(ymin, ymax) ax.grid(1) if distribution == 'mjj': ax.yaxis.set_major_locator(MultipleLocator(0.05)) ax.yaxis.set_minor_locator(MultipleLocator(0.01)) elif distribution == 'recoil': ax.yaxis.set_major_locator(MultipleLocator(0.1)) ax.yaxis.set_minor_locator(MultipleLocator(0.05)) rax.set_xlabel(xlabel) rax.set_ylabel("Data / MC SF") rax.set_ylim(0.95, 1.05) rax.yaxis.set_major_locator(MultipleLocator(0.05)) rax.yaxis.set_minor_locator(MultipleLocator(0.01)) rax.grid(1) plt.text(1., 1., r"$\approx$ %.1f fb$^{-1}$ (13 TeV)" % lumi_by_region(region, year), fontsize=16, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes) plt.text(1., 0.95, f'{jeteta_config if jeteta_config else ""}', fontsize=12, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes) plt.text(0., 1., f'{year}', fontsize=16, horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes) fig.savefig( pjoin( outdir, f'data_mc_comparison_{region}_{year}{"_"+jeteta_config if jeteta_config else ""}.{output_format}' )) fig.clear() plt.close(fig) vals = np.array(sorted(list(set(list(xedgnum.flatten()))))) ysf[np.isnan(ysf) | np.isinf(np.abs(ysf))] = 1 outfile[f'{tag}_{region}_{year}'] = (ysf, vals)
def plot_scalefactors(tag, ymin=0.9, ymax=1.1, distribution='recoil', output_format='pdf'): regions = ['1m', '2m'] opts = markers('data') emarker = opts.pop('emarker', '') outdir = f"./output/{tag}" jeteta_configs = [ 'two_central_jets', 'two_forward_jets', 'one_jet_forward_one_jet_central' ] for year in [2017, 2018]: for region in regions: fig, ax = plt.subplots(1, 1) ynum = {} yden = {} yerrnum = {} yerrden = {} ysf = {} ysferr = {} for jeteta_config in jeteta_configs: print(f'{year}, {region}, {jeteta_config}') if region == '1m': fnum = f'output/{tag}/table_{region}_recoil_SingleMuon_{year}_{jeteta_config}.txt' fden = f'output/{tag}/table_{region}_recoil_WJetsToLNu_HT_MLM_{year}_{jeteta_config}.txt' xlabel = f"{distribution} (GeV)" elif region == '2m': fnum = f'output/{tag}/table_{region}_recoil_SingleMuon_{year}_{jeteta_config}.txt' fden = f'output/{tag}/table_{region}_recoil_VDYJetsToLL_M-50_HT_MLM_{year}_{jeteta_config}.txt' xlabel = f"{distribution} (GeV)" if not os.path.exists(fnum): print(f"File not found {fnum}") continue if not os.path.exists(fden): print(f"File not found {fden}") continue xnum, xedgnum, ynum[f'{jeteta_config}'], yerrnum[ f'{jeteta_config}'] = get_xy(fnum) xden, xedgden, yden[f'{jeteta_config}'], yerrden[ f'{jeteta_config}'] = get_xy(fden) xsf = xnum ysf[f'{jeteta_config}'] = ynum[f'{jeteta_config}'] / yden[ f'{jeteta_config}'] ysferr[f'{jeteta_config}'] = ratio_unc( ynum[f'{jeteta_config}'], yden[f'{jeteta_config}'], yerrnum[f'{jeteta_config}'], yerrden[f'{jeteta_config}']) # Only consider the values above 250 GeV if distribution == 'recoil': recoil_cut = xedgnum[0] >= 250 xsf = xsf[recoil_cut] ysferr_new = {} ysferr_new['two_central_jets'] = [ ysferr['two_central_jets'][0][recoil_cut], ysferr['two_central_jets'][1][recoil_cut] ] ysferr_new['two_forward_jets'] = [ ysferr['two_forward_jets'][0][recoil_cut], ysferr['two_forward_jets'][1][recoil_cut] ] ysferr_new['one_jet_forward_one_jet_central'] = [ ysferr['one_jet_forward_one_jet_central'][0][recoil_cut], ysferr['one_jet_forward_one_jet_central'][1][recoil_cut] ] opts['color'] = 'k' ax.errorbar(xsf, ysf['two_central_jets'][recoil_cut], yerr=ysferr_new['two_central_jets'], label=f'Two central jets', **opts) opts['color'] = 'g' ax.errorbar(xsf, ysf['two_forward_jets'][recoil_cut], yerr=ysferr_new['two_forward_jets'], label=f'Two forward jets', **opts) opts['color'] = 'r' ax.errorbar(xsf, ysf['one_jet_forward_one_jet_central'][recoil_cut], yerr=ysferr_new['one_jet_forward_one_jet_central'], label=f'Mixed', **opts) ax.legend() ax.set_ylabel('Data / MC SF') ax.set_xlabel(f'{distribution.capitalize()} (GeV)') ax.set_ylim(ymin, ymax) ax.grid(1) ax.xaxis.set_major_locator(MultipleLocator(200)) ax.xaxis.set_minor_locator(MultipleLocator(50)) ax.yaxis.set_major_locator(MultipleLocator(0.05)) ax.yaxis.set_minor_locator(MultipleLocator(0.01)) plt.text(1., 1., r"$\approx$ %.1f fb$^{-1}$ (13 TeV)" % lumi_by_region(region, year), fontsize=16, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes) plt.text(0., 1., f'{year} {region}', fontsize=16, horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes) fig.savefig( pjoin(outdir, f'scale_factors_{region}_{year}.{output_format}')) fig.clear() plt.close(fig)