Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

etaphi maps [DO NOT MERGE] #71

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions analysis/wwz/etaphi_plotter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import os
import pickle
import gzip
import argparse
import matplotlib.pyplot as plt
import numpy as np
import awkward as ak

from coffea import lookup_tools

# This script is an example of how to dump some info from the btag eff histos (does not actually currently make a plot)
# Example usage:
# python btageff_plotter.py ../../ewkcoffea/data/btag_eff/btag_eff_ttZ_srpresel.pkl.gz -u run2

import numpy as np
import matplotlib.pyplot as plt

def make_2d_plot(hist, title, nbins=10):
# Get the original x and y range
x_min, x_max = ak.flatten(hist.axes.edges[0])[0], ak.flatten(hist.axes.edges[0])[-1]
y_min, y_max = ak.flatten(hist.axes.edges[1])[0], ak.flatten(hist.axes.edges[1])[-1]

# Define new bin edges using the same range but with fewer bins
xedges = np.linspace(x_min, x_max, nbins + 1)
yedges = np.linspace(y_min, y_max, nbins + 1)

# Aggregate the original bin values to match the new binning
bin_values = hist.values()
new_bin_values = np.zeros((nbins, nbins))
x_bin_width = bin_values.shape[0] // nbins
y_bin_width = bin_values.shape[1] // nbins

for i in range(nbins):
for j in range(nbins):
new_bin_values[i, j] = bin_values[i*x_bin_width:(i+1)*x_bin_width, j*y_bin_width:(j+1)*y_bin_width].sum()

# Plot with the new binning
fig, ax = plt.subplots()
X, Y = np.meshgrid(xedges, yedges)
mesh = ax.pcolormesh(X, Y, new_bin_values.T, cmap='viridis', shading='auto')

# Add color bar with customized range
cbar = plt.colorbar(mesh, ax=ax, extend='both')
cbar.set_label('Jet Counts')

# Set axis labels and title
ax.set_xlabel('ETA')
ax.set_ylabel('PHI')
ax.set_title(title)

# Annotate each bin with its value
#for i in range(nbins):
# for j in range(nbins):
# bin_value = f'{new_bin_values[i, j]:.2f}'
# x = (xedges[j] + xedges[j+1]) / 2
# y = (yedges[i] + yedges[i+1]) / 2
# ax.text(x, y, bin_value, ha='center', va='center', color='white', fontweight='bold', fontsize=8,
# bbox=dict(facecolor='black', alpha=0.5, edgecolor='none'))

return fig



#def make_2d_plot(hist, title):
# xedges = ak.flatten(hist.axes.edges[0])
# yedges = ak.flatten(hist.axes.edges[1])
# bin_values = hist.values()
#
# # Print bin values for debugging
# #print("Bin values:\n", bin_values)
#
# fig, ax = plt.subplots()
# X, Y = np.meshgrid(xedges, yedges)
# mesh = ax.pcolormesh(X, Y, bin_values.T, cmap='viridis', shading='auto')
#
# # Add color bar with customized range
# cbar = plt.colorbar(mesh, ax=ax, extend='both') # extend='both' for color bar extensions
# cbar.set_label('Jet Counts')
# #cbar.set_ticks(np.linspace(vmin, vmax, num=5)) # Example of setting ticks
#
# # Set color bar limits directly when creating it
# mesh.set_clim(vmin=0, vmax=3)
#
# # Set axis labels
# ax.set_xlabel('ETA')
# ax.set_ylabel('PHI')
# ax.set_title(title)
#
# # Annotate each bin with its value
# #for i in range(len(yedges)-1):
# # for j in range(len(xedges)-1):
# # bin_value = f'{bin_values.T[i, j]:.2f}'
# # x = (xedges[j] + xedges[j+1]) / 2
# # y = (yedges[i] + yedges[i+1]) / 2
# # print(f"Annotating bin at ({x}, {y}) with value {bin_value}")
# # ax.text(x, y, bin_value, ha='center', va='center', color='white', fontweight='bold', fontsize=8,
# # bbox=dict(facecolor='black', alpha=0.5, edgecolor='none'))
#
# return fig


def main():

# Set up the command line parser
parser = argparse.ArgumentParser()
parser.add_argument("pkl_file_path", help = "The path to the pkl file")
parser.add_argument('-u', "--ul-year", default='run2', help = "Which year to process", choices=["UL18","UL17","UL16","UL16APV"])
parser.add_argument('-p', "--make-plots", action='store_true', help = "Make plots from the pkl file")
args = parser.parse_args()

year = args.ul_year


# Get the counts from the input hiso
histo = pickle.load(gzip.open(args.pkl_file_path))["etaphi_all"]

cat_list = [
# "all_events",
"cr_4l_sf",
# "cr_4l_btag_of",
# "cr_4l_btag_sf_offZ_met80"
]

for cat in cat_list:
print(f"\n{cat}")

histo_proc = histo[{"category":cat}]

if args.make_plots:
save_dir = "etaphi_plots"
os.makedirs(save_dir, exist_ok=True)

fig = make_2d_plot(histo_proc,f"Year:{year} Category:{cat}")
save_path = os.path.join(save_dir, f"{year}_{cat}_etaphi.png")
fig.savefig(save_path)
plt.close(fig)
plt.show()


if __name__ == "__main__":
main()
93 changes: 93 additions & 0 deletions analysis/wwz/for_jec_11_var.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

# This is a file for things related to the 27 JEC variations
# - For things that would be too long and messy to keep in our regular scripts, but want to keep them around for reference

SYSTS_SPECIAL = {
"UL16APV" : {
"CMS_btag_fixedWP_incl_light_uncorrelated_2016APV" : {"yr_rel":"UL16APV", "yr_notrel": []},
"CMS_btag_fixedWP_comb_bc_uncorrelated_2016APV" : {"yr_rel":"UL16APV", "yr_notrel": []},
"Regrouped_Absolute_2016APV_uncorrelated" : {"yr_rel":"UL16APV", "yr_notrel": []},
"Regrouped_HF_2016APV_uncorrelated" : {"yr_rel":"UL16APV", "yr_notrel": []},
"Regrouped_EC2_2016APV_uncorrelated" : {"yr_rel":"UL16APV", "yr_notrel": []},
"Regrouped_RelativeSample_2016APV_uncorrelated" : {"yr_rel":"UL16APV", "yr_notrel": []},
"Regrouped_BBEC1_2016APV_uncorrelated" : {"yr_rel":"UL16APV", "yr_notrel": []},
"CMS_res_j_2016APV" : {"yr_rel":"UL16APV", "yr_notrel": []},
"CMS_scale_met_unclustered_energy_2016APV" : {"yr_rel":"UL16APV", "yr_notrel": []},
},
"UL16" : {
"CMS_btag_fixedWP_incl_light_uncorrelated_2016" : {"yr_rel":"UL16", "yr_notrel": []},
"CMS_btag_fixedWP_comb_bc_uncorrelated_2016" : {"yr_rel":"UL16", "yr_notrel": []},
"Regrouped_Absolute_2016_uncorrelated" : {"yr_rel":"UL16", "yr_notrel": []},
"Regrouped_HF_2016_uncorrelated" : {"yr_rel":"UL16", "yr_notrel": []},
"Regrouped_EC2_2016_uncorrelated" : {"yr_rel":"UL16", "yr_notrel": []},
"Regrouped_RelativeSample_2016_uncorrelated" : {"yr_rel":"UL16", "yr_notrel": []},
"Regrouped_BBEC1_2016_uncorrelated" : {"yr_rel":"UL16", "yr_notrel": []},
"CMS_res_j_2016" : {"yr_rel":"UL16", "yr_notrel": []},
"CMS_scale_met_unclustered_energy_2016" : {"yr_rel":"UL16", "yr_notrel": []},
},
"UL17" : {
"CMS_btag_fixedWP_incl_light_uncorrelated_2017" : {"yr_rel":"UL17", "yr_notrel": []},
"CMS_btag_fixedWP_comb_bc_uncorrelated_2017" : {"yr_rel":"UL17", "yr_notrel": []},
"Regrouped_Absolute_2017_uncorrelated" : {"yr_rel":"UL17", "yr_notrel": []},
"Regrouped_HF_2017_uncorrelated" : {"yr_rel":"UL17", "yr_notrel": []},
"Regrouped_EC2_2017_uncorrelated" : {"yr_rel":"UL17", "yr_notrel": []},
"Regrouped_RelativeSample_2017_uncorrelated" : {"yr_rel":"UL17", "yr_notrel": []},
"Regrouped_BBEC1_2017_uncorrelated" : {"yr_rel":"UL17", "yr_notrel": []},
"CMS_res_j_2017" : {"yr_rel":"UL17", "yr_notrel": []},
"CMS_scale_met_unclustered_energy_2017" : {"yr_rel":"UL17", "yr_notrel": []},
},
"UL18" : {
"CMS_btag_fixedWP_incl_light_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"CMS_btag_fixedWP_comb_bc_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"Regrouped_Absolute_2018_uncorrelated" : {"yr_rel":"UL18", "yr_notrel": []},
"Regrouped_HF_2018_uncorrelated" : {"yr_rel":"UL18", "yr_notrel": []},
"Regrouped_EC2_2018_uncorrelated" : {"yr_rel":"UL18", "yr_notrel": []},
"Regrouped_RelativeSample_2018_uncorrelated" : {"yr_rel":"UL18", "yr_notrel": []},
"Regrouped_BBEC1_2018_uncorrelated" : {"yr_rel":"UL18", "yr_notrel": []},
"CMS_res_j_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"CMS_scale_met_unclustered_energy_2018" : {"yr_rel":"UL18", "yr_notrel": []},
},
"run2" : {
"CMS_btag_fixedWP_incl_light_uncorrelated_2016APV" : {"yr_rel":"UL16APV", "yr_notrel": ["UL16", "UL17", "UL18"]},
"CMS_btag_fixedWP_comb_bc_uncorrelated_2016APV" : {"yr_rel":"UL16APV", "yr_notrel": ["UL16", "UL17", "UL18"]},
"CMS_res_j_2016APV" : {"yr_rel":"UL16APV", "yr_notrel": ["UL16", "UL17", "UL18"]},
"CMS_scale_met_unclustered_energy_2016APV" : {"yr_rel":"UL16APV", "yr_notrel": ["UL16", "UL17", "UL18"]},
"Regrouped_Absolute_2018_uncorrelated" : {"yr_rel":"UL16APV", "yr_notrel": ["UL16", "UL17", "UL18"]},
"Regrouped_HF_2018_uncorrelated" : {"yr_rel":"UL16APV", "yr_notrel": ["UL16", "UL17", "UL18"]},
"Regrouped_EC2_2018_uncorrelated" : {"yr_rel":"UL16APV", "yr_notrel": ["UL16", "UL17", "UL18"]},
"Regrouped_RelativeSample_2018_uncorrelated" : {"yr_rel":"UL16APV", "yr_notrel": ["UL16", "UL17", "UL18"]},
"Regrouped_BBEC1_2018_uncorrelated" : {"yr_rel":"UL16APV", "yr_notrel": ["UL16", "UL17", "UL18"]},

"CMS_btag_fixedWP_incl_light_uncorrelated_2016" : {"yr_rel":"UL16", "yr_notrel": ["UL16APV", "UL17", "UL18"]},
"CMS_btag_fixedWP_comb_bc_uncorrelated_2016" : {"yr_rel":"UL16", "yr_notrel": ["UL16APV", "UL17", "UL18"]},
"CMS_res_j_2016" : {"yr_rel":"UL16", "yr_notrel": ["UL16APV", "UL17", "UL18"]},
"CMS_scale_met_unclustered_energy_2016" : {"yr_rel":"UL16", "yr_notrel": ["UL16APV", "UL17", "UL18"]},
"Regrouped_Absolute_2018_uncorrelated" : {"yr_rel":"UL16", "yr_notrel": ["UL16APV", "UL17", "UL18"]},
"Regrouped_HF_2018_uncorrelated" : {"yr_rel":"UL16", "yr_notrel": ["UL16APV", "UL17", "UL18"]},
"Regrouped_EC2_2018_uncorrelated" : {"yr_rel":"UL16", "yr_notrel": ["UL16APV", "UL17", "UL18"]},
"Regrouped_RelativeSample_2018_uncorrelated" : {"yr_rel":"UL16", "yr_notrel": ["UL16APV", "UL17", "UL18"]},
"Regrouped_BBEC1_2018_uncorrelated" : {"yr_rel":"UL16", "yr_notrel": ["UL16APV", "UL17", "UL18"]},

"CMS_btag_fixedWP_incl_light_uncorrelated_2017" : {"yr_rel":"UL17", "yr_notrel": ["UL16APV", "UL16", "UL18"]},
"CMS_btag_fixedWP_comb_bc_uncorrelated_2017" : {"yr_rel":"UL17", "yr_notrel": ["UL16APV", "UL16", "UL18"]},
"CMS_res_j_2017" : {"yr_rel":"UL17", "yr_notrel": ["UL16APV", "UL16", "UL18"]},
"CMS_scale_met_unclustered_energy_2017" : {"yr_rel":"UL17", "yr_notrel": ["UL16APV", "UL16", "UL18"]},
"Regrouped_Absolute_2018_uncorrelated" : {"yr_rel":"UL17", "yr_notrel": ["UL16APV", "UL16", "UL18"]},
"Regrouped_HF_2018_uncorrelated" : {"yr_rel":"UL17", "yr_notrel": ["UL16APV", "UL16", "UL18"]},
"Regrouped_EC2_2018_uncorrelated" : {"yr_rel":"UL17", "yr_notrel": ["UL16APV", "UL16", "UL18"]},
"Regrouped_RelativeSample_2018_uncorrelated" : {"yr_rel":"UL17", "yr_notrel": ["UL16APV", "UL16", "UL18"]},
"Regrouped_BBEC1_2018_uncorrelated" : {"yr_rel":"UL17", "yr_notrel": ["UL16APV", "UL16", "UL18"]},

"CMS_btag_fixedWP_incl_light_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": ["UL16APV", "UL16", "UL17"]},
"CMS_btag_fixedWP_comb_bc_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": ["UL16APV", "UL16", "UL17"]},
"CMS_res_j_2018" : {"yr_rel":"UL18", "yr_notrel": ["UL16APV", "UL16", "UL17"]},
"CMS_scale_met_unclustered_energy_2018" : {"yr_rel":"UL18", "yr_notrel": ["UL16APV", "UL16", "UL17"]},
"Regrouped_Absolute_2018_uncorrelated" : {"yr_rel":"UL18", "yr_notrel": ["UL16APV", "UL16", "UL17"]},
"Regrouped_HF_2018_uncorrelated" : {"yr_rel":"UL18", "yr_notrel": ["UL16APV", "UL16", "UL17"]},
"Regrouped_EC2_2018_uncorrelated" : {"yr_rel":"UL18", "yr_notrel": ["UL16APV", "UL16", "UL17"]},
"Regrouped_RelativeSample_2018_uncorrelated" : {"yr_rel":"UL18", "yr_notrel": ["UL16APV", "UL16", "UL17"]},
"Regrouped_BBEC1_2018_uncorrelated" : {"yr_rel":"UL18", "yr_notrel": ["UL16APV", "UL16", "UL17"]},

},

}
17 changes: 16 additions & 1 deletion analysis/wwz/for_jec_27_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,22 @@
]

SYSTS_SPECIAL = {

"UL18" : {
"CMS_btag_fixedWP_incl_light_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"CMS_btag_fixedWP_comb_bc_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"AbsoluteStat_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"RelativeJEREC1_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"RelativeJEREC2_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"RelativePtEC1_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"RelativePtEC2_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"TimePtEta_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"RelativeSample_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"RelativeStatEC_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"RelativeStatFSR_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"RelativeStatHF_uncorrelated_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"CMS_res_j_2018" : {"yr_rel":"UL18", "yr_notrel": []},
"CMS_scale_met_unclustered_energy_2018" : {"yr_rel":"UL18", "yr_notrel": []},
},
"run2" : {
"btagSFlight_uncorrelated_2016APV" : {"yr_rel":"UL16APV", "yr_notrel": ["UL16", "UL17", "UL18"]},
"btagSFbc_uncorrelated_2016APV" : {"yr_rel":"UL16APV", "yr_notrel": ["UL16", "UL17", "UL18"]},
Expand Down
46 changes: 27 additions & 19 deletions analysis/wwz/get_wwz_yields.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,24 +518,31 @@ def make_syst_fig(histo_mc,mc_up_arr,mc_do_arr,syst,histo_data=None,title="test"
def make_syst_plots(histo_dict,grouping_mc,grouping_data,save_dir_path,year):

for var_name in histo_dict.keys():
#print(f"\n{var_name}")
print(f"\n{var_name}")
#if var_name != "njets": continue
if var_name not in TMP_VAR_LST: continue
#if var_name not in TMP_VAR_LST: continue
if var_name not in ["bdt_of_bin","bdt_sf_bin","njets","nleps"]: continue
#if (var_name not in BDT_INPUT_LST) and (var_name not in BDT_BINSUMMARY_LST): continue
histo = histo_dict[var_name]

cat_lst = [
"sr_4l_sf_A",
"sr_4l_sf_B",
"sr_4l_sf_C",
"sr_4l_of_1",
"sr_4l_of_2",
"sr_4l_of_3",
"sr_4l_of_4",
"cr_4l_sf",
"cr_4l_btag_sf_offZ_met80",
"cr_4l_btag_of",
"sr_4l_of_incl",
"sr_4l_sf_incl",
#"sr_4l_sf_A",
#"sr_4l_sf_B",
#"sr_4l_sf_C",
#"sr_4l_of_1",
#"sr_4l_of_2",
#"sr_4l_of_3",
#"sr_4l_of_4",
#"cr_4l_sf",
#"cr_4l_btag_sf_offZ_met80",
#"cr_4l_btag_of",
#"sr_4l_of_incl",
#"sr_4l_sf_incl",
"cr_4l_btag_of",
"cr_4l_btag_sf_offZ_met80",
"cr_4l_sf",
"sr_4l_bdt_sf_trn",
"sr_4l_bdt_of_trn"
]

# Rebin if continous variable
Expand Down Expand Up @@ -563,8 +570,8 @@ def make_syst_plots(histo_dict,grouping_mc,grouping_data,save_dir_path,year):
data_nom = merge_overflow(histo_grouped_data[{"systematic":"nominal"}])

for syst in syst_var_lst:
if syst not in jecref.JERC_LST: continue
#if "btag" not in syst: continue
#if syst not in jecref.JERC_LST: continue
if "Regrouped" not in syst: continue
#if "uncorrelated" not in syst: continue
#if "lepSF" not in syst: continue
#if "PreFiring" not in syst: continue
Expand All @@ -586,7 +593,8 @@ def make_syst_plots(histo_dict,grouping_mc,grouping_data,save_dir_path,year):
if year == "all": blacklist_years = []
skip = False
for y in blacklist_years:
if syst.endswith(y):
#if syst.endswith(y):
if y in syst:
skip = True
if skip: continue

Expand Down Expand Up @@ -955,8 +963,8 @@ def main():

# Make plots
if args.make_plots:
make_plots(histo_dict,sample_dict_mc,sample_dict_data,save_dir_path=out_path,apply_nsf_to_cr=False)
#make_syst_plots(histo_dict,sample_dict_mc,sample_dict_data,out_path,args.ul_year) # Check on individual systematics
#make_plots(histo_dict,sample_dict_mc,sample_dict_data,save_dir_path=out_path,apply_nsf_to_cr=False)
make_syst_plots(histo_dict,sample_dict_mc,sample_dict_data,out_path,args.ul_year) # Check on individual systematics
#make_sr_comb_plot(histo_dict,sample_dict_mc,sample_dict_data,args.ul_year,ana_type="cb") # Make plot of all SR yields in one plot


Expand Down
Loading
Loading