Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/rkansal47/HHbbVV
Browse files Browse the repository at this point in the history
  • Loading branch information
rkansal47 committed Mar 5, 2024
2 parents 05d6774 + ef415fa commit 737da14
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 94 deletions.
119 changes: 56 additions & 63 deletions src/HHbbVV/postprocessing/PostProcessRes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"import corrections\n",
"\n",
"from utils import CUT_MAX_VAL, ShapeVar\n",
"from hh_vars import (\n",
"from HHbbVV.hh_vars import (\n",
" years,\n",
" data_key,\n",
" qcd_key,\n",
Expand Down Expand Up @@ -92,6 +92,7 @@
"source": [
"MAIN_DIR = Path(\"../../../\")\n",
"samples_dir = MAIN_DIR / \"../data/skimmer/24Feb25_update_skimmer\"\n",
"sig_samples_dir = MAIN_DIR / \"../data/skimmer/24Mar5_update_lp\"\n",
"# samples_dir = f\"{MAIN_DIR}/../data/skimmer/Feb24\"\n",
"# nonres_signal_samples_dir = f\"{MAIN_DIR}/../data/skimmer/Jun10\"\n",
"# res_signal_samples_dir = f\"{MAIN_DIR}/../data/skimmer/Apr11\"\n",
Expand All @@ -100,7 +101,7 @@
"# res_signal_samples_dir = \"/eos/uscms/store/user/rkansal/bbVV/skimmer/Apr11/\"\n",
"year = \"2017\"\n",
"\n",
"date = \"24Mar2\"\n",
"date = \"24Mar5_update_lp\"\n",
"plot_dir = MAIN_DIR / f\"plots/PostProcessing/{date}/\"\n",
"templates_dir = Path(f\"templates/{date}/\")\n",
"\n",
Expand Down Expand Up @@ -133,22 +134,34 @@
" index=list(samples.keys()) + list(nonres_samples.keys()) + list(res_samples.keys())\n",
")\n",
"\n",
"# hem cleaning in load_samples not implemented yet for res samples\n",
"hem_cleaning = True\n",
"\n",
"# utils.remove_empty_parquets(samples_dir, year)\n",
"events_dict = postprocessing.load_samples(\n",
" sig_samples_dir,\n",
" {**nonres_samples, **res_samples},\n",
" year,\n",
" new_filters,\n",
")\n",
"\n",
"events_dict |= postprocessing.load_samples(\n",
" samples_dir,\n",
" {**nonres_samples, **res_samples, **samples},\n",
" samples,\n",
" year,\n",
" new_filters,\n",
" hem_cleaning=hem_cleaning,\n",
")\n",
"\n",
"utils.add_to_cutflow(events_dict, \"Preselection\", \"finalWeight\", cutflow)\n",
"cutflow"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"events = pd.read_parquet(f\"{sig_samples_dir}/{year}/GluGluToHHTobbVV_node_cHHH1/parquet\")\n",
"events"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand Down Expand Up @@ -226,7 +239,7 @@
" bb_masks,\n",
" nonres_sig_keys + res_sig_keys,\n",
" control_plot_vars,\n",
" f\"{plot_dir}/ControlPlots/{year}/\",\n",
" plot_dir / f\"ControlPlots/{year}\",\n",
" year,\n",
" bg_keys=bg_keys,\n",
" sig_scale_dict={\"HHbbVV\": 1e5, \"VBFHHbbVV\": 2e6} | {key: 2e4 for key in res_sig_keys},\n",
Expand All @@ -243,6 +256,32 @@
"Overall LP SF"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from postprocessing import Region, nonres_shape_vars\n",
"\n",
"# temp region to check systematics\n",
"selection_regions = {\n",
" \"pass\": Region(\n",
" cuts={\n",
" \"bbFatJetParticleNetMD_Txbb\": [0.97, CUT_MAX_VAL],\n",
" \"VVFatJetParTMD_THWWvsT\": [0.8, CUT_MAX_VAL],\n",
" },\n",
" label=\"Pass\",\n",
" ),\n",
" \"lpsf\": Region(\n",
" cuts={\n",
" \"VVFatJetParTMD_THWWvsT\": [0.8, CUT_MAX_VAL],\n",
" },\n",
" label=\"LP SF\",\n",
" ),\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -311,45 +350,26 @@
"metadata": {},
"outputs": [],
"source": [
"from postprocessing import Region, nonres_shape_vars\n",
"\n",
"# temp region to check systematics\n",
"selection_regions = {\n",
" \"pass\": Region(\n",
" cuts={\n",
" \"bbFatJetParticleNetMD_Txbb\": [0.97, CUT_MAX_VAL],\n",
" \"VVFatJetParTMD_THWWvsT\": [0.8, CUT_MAX_VAL],\n",
" },\n",
" label=\"Pass\",\n",
" )\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h = postprocessing.get_templates(\n",
"ht = postprocessing.get_templates(\n",
" events_dict,\n",
" bb_masks,\n",
" year,\n",
" nonres_sig_keys + res_sig_keys,\n",
" [\"HHbbVV\"],\n",
" # nonres_sig_keys + res_sig_keys,\n",
" # res_sig_keys,\n",
" selection_regions,\n",
" # res_shape_vars[:1],\n",
" nonres_shape_vars,\n",
" systematics,\n",
" templates_dir,\n",
" # bg_keys=[\"QCD\", \"TT\", \"V+Jets\", \"Diboson\", \"Hbb\"],\n",
" plot_dir=f\"{plot_dir}/templates/\",\n",
" plot_dir=plot_dir / \"templates\",\n",
" prev_cutflow=cutflow,\n",
" sig_scale_dict={\"HHbbVV\": 1e3, \"VBFHHbbVV\": 1e4} | {key: 1e2 for key in res_sig_keys},\n",
" # sig_splits=sig_splits[:2],\n",
" weight_shifts={},\n",
" jshift=\"\",\n",
" lpsfs=False,\n",
" lpsfs=True,\n",
" plot_shifts=False,\n",
" pass_ylim=500,\n",
" fail_ylim=40000,\n",
Expand Down Expand Up @@ -379,12 +399,12 @@
" # res_shape_vars,\n",
" systematics,\n",
" templates_dir,\n",
" plot_dir=f\"{plot_dir}/templates\",\n",
" plot_dir=plot_dir / \"templates\",\n",
" prev_cutflow=cutflow,\n",
" sig_scale_dict={\"HHbbVV\": 1e3, \"VBFHHbbVV\": 1e4} | {key: 1e2 for key in res_sig_keys},\n",
" sig_scale_dict={\"HHbbVV\": 1e3, \"VBFHHbbVV\": 2e4} | {key: 1e2 for key in res_sig_keys},\n",
" weight_shifts=postprocessing.weight_shifts,\n",
" jshift=jshift,\n",
" lpsfs=False,\n",
" lpsfs=True,\n",
" pass_ylim=500,\n",
" fail_ylim=40000,\n",
" # blind_pass=True,\n",
Expand All @@ -395,33 +415,6 @@
" templates = {**templates, **ttemps}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ttemps"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.linalg.norm((ttemps[1].values() - ttemps[0])[:, 10]) / "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.linalg.norm([1, 1])"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
13 changes: 8 additions & 5 deletions src/HHbbVV/postprocessing/corrections.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _load_txbb_sfs(year: str):
"""Create 2D lookup tables in [Txbb, pT] for Txbb SFs from given year"""

# https://coli.web.cern.ch/coli/.cms/btv/boohft-calib/20221201_bb_ULNanoV9_PNetXbbVsQCD_ak8_ext_2016APV/4_fit/
with (package_path / f"/corrections/txbb_sfs/txbb_sf_ul_{year}.json").open() as f:
with (package_path / f"corrections/txbb_sfs/txbb_sf_ul_{year}.json").open() as f:
txbb_sf = json.load(f)

wps = ["LP", "MP", "HP"]
Expand Down Expand Up @@ -81,9 +81,12 @@ def apply_txbb_sfs(
events[f"{weight_key}_txbb_{var}"] = events[weight_key]

if len(events[weight_key]):
events[weight_key] = events[weight_key] * txbb_sf_lookups[year]["nom"](bb_txbb, bb_pt)
else:
events[weight_key] = events[weight_key]
txbb_nom = txbb_sf_lookups[year]["nom"](bb_txbb, bb_pt)
for wkey in utils.get_all_weights(events):
if len(events[wkey].shape) > 1:
events[wkey] *= txbb_nom[:, np.newaxis]
else:
events[wkey] *= txbb_nom


trig_effs = {}
Expand Down Expand Up @@ -334,7 +337,7 @@ def get_lpsf(
# pt extrapolation uncertainty is the std of all pt param variations
uncs["sj_pt_unc"] = (
np.std(
np.sum(weight[:, np.newaxis] * events[f"{jet}_lp_pt_extrap_vars"].to_numpy(), axis=0)
np.sum(weight[:, np.newaxis] * events[f"{jet}_lp_sf_pt_extrap_vars"].to_numpy(), axis=0)
)
/ tot_post
)
Expand Down
41 changes: 24 additions & 17 deletions src/HHbbVV/postprocessing/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def ratioHistPlot(
divide_bin_width: bool = False,
plot_significance: bool = False,
significance_dir: str = "right",
plot_ratio: bool = True,
axrax: tuple = None,
):
"""
Expand Down Expand Up @@ -320,10 +321,12 @@ def ratioHistPlot(
gridspec_kw={"height_ratios": [3, 1, 1], "hspace": 0},
sharex=True,
)
else:
elif plot_ratio:
fig, (ax, rax) = plt.subplots(
2, 1, figsize=(12, 14), gridspec_kw={"height_ratios": [3, 1], "hspace": 0}, sharex=True
)
else:
fig, ax = plt.subplots(1, 1, figsize=(12, 11))

plt.rcParams.update({"font.size": 24})

Expand Down Expand Up @@ -385,6 +388,7 @@ def ratioHistPlot(
alpha=0.2,
hatch="//",
linewidth=0,
label="Total Background Uncertainty",
)

# plot data
Expand Down Expand Up @@ -412,24 +416,27 @@ def ratioHistPlot(
ax.set_ylim(y_lowlim)

# plot ratio below
if plot_data:
bg_tot = sum([pre_divide_hists[sample, :] for sample in bg_keys])
yerr = ratio_uncertainty(pre_divide_hists[data_key, :].values(), bg_tot.values(), "poisson")
if plot_ratio:
if plot_data:
bg_tot = sum([pre_divide_hists[sample, :] for sample in bg_keys])
yerr = ratio_uncertainty(
pre_divide_hists[data_key, :].values(), bg_tot.values(), "poisson"
)

hep.histplot(
pre_divide_hists[data_key, :] / (bg_tot.values() + 1e-5),
yerr=yerr,
ax=rax,
histtype="errorbar",
color="black",
capsize=4,
)
else:
rax.set_xlabel(hists.axes[1].label)
hep.histplot(
pre_divide_hists[data_key, :] / (bg_tot.values() + 1e-5),
yerr=yerr,
ax=rax,
histtype="errorbar",
color="black",
capsize=4,
)
else:
rax.set_xlabel(hists.axes[1].label)

rax.set_ylabel("Data/MC")
rax.set_ylim(ratio_ylims)
rax.grid()
rax.set_ylabel("Data/MC")
rax.set_ylim(ratio_ylims)
rax.grid()

if plot_significance:
bg_tot = sum([pre_divide_hists[sample, :] for sample in bg_keys]).values()
Expand Down
Loading

0 comments on commit 737da14

Please sign in to comment.