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 Apr 29, 2024
2 parents af3dbb5 + 4946784 commit 0c1d6d6
Show file tree
Hide file tree
Showing 9 changed files with 423 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ jobs:
--durations=20
- name: Upload coverage report
uses: codecov/codecov-action@v4.2.0
uses: codecov/codecov-action@v4.3.0
1 change: 0 additions & 1 deletion inference_scans/nuisance_renames.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations


years = ["2016APV", "2016", "2017", "2018"]

mc_label_map = {
Expand Down
265 changes: 259 additions & 6 deletions src/HHbbVV/postprocessing/CombineTemplates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -25,7 +25,17 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -44,10 +54,12 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"templates = {}\n",
"\n",
"for year in years:\n",
" combined_templates = {}\n",
"\n",
Expand Down Expand Up @@ -75,7 +87,9 @@
"\n",
" # save combined templates\n",
" with (templates_path / f\"{year}_templates.pkl\").open(\"wb\") as f:\n",
" pickle.dump(combined_templates, f)"
" pickle.dump(combined_templates, f)\n",
"\n",
" templates[year] = combined_templates"
]
},
{
Expand All @@ -87,7 +101,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -137,8 +151,247 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Combining cutflows"
"TODO: Combining cutflows"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Testing JEC JMC plots"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import hist\n",
"from hist import Hist\n",
"import numpy as np\n",
"from HHbbVV.hh_vars import jecs, jmsr, bg_keys, qcd_key, LUMI\n",
"import utils, plotting, postprocessing, regions, datacardHelpers\n",
"import warnings\n",
"from pathlib import Path"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"MAIN_DIR = Path(\"../../../\")\n",
"\n",
"main_plot_dir = MAIN_DIR / f\"plots/PostProcessing/24Apr12NonresCombinedggFOR/Templates\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def combine_hists(*hists):\n",
" csamples = []\n",
" for h in hists:\n",
" csamples += list(h.axes[0])\n",
"\n",
" reth = Hist(\n",
" hist.axis.StrCategory(csamples, name=\"Sample\"),\n",
" *hists[0].axes[1:],\n",
" storage=\"weight\",\n",
" )\n",
"\n",
" for h in hists:\n",
" for sample in h.axes[0]:\n",
" reth.view(flow=True)[utils.get_key_index(reth, sample), ...] = h[sample, ...].view(\n",
" flow=True\n",
" )\n",
"\n",
" return reth"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib as mpl\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.ticker as mticker\n",
"import mplhep as hep\n",
"\n",
"plt.style.use(hep.style.CMS)\n",
"hep.style.use(\"CMS\")\n",
"formatter = mticker.ScalarFormatter(useMathText=True)\n",
"formatter.set_powerlimits((-3, 3))\n",
"\n",
"# this is needed for some reason to update the font size for the first plot\n",
"fig, ax = plt.subplots(1, 1, figsize=(12, 12))\n",
"plt.rcParams.update({\"font.size\": 24})\n",
"plt.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"HHbbVV\" in cjt.axes[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"warnings.filterwarnings(\"ignore\")\n",
"\n",
"p_sig_keys = [\"HHbbVV\", \"VBFHHbbVV\"]\n",
"p_bg_keys = [key for key in bg_keys if key != qcd_key]\n",
"p_all_keys = p_sig_keys + p_bg_keys\n",
"\n",
"sig_scale_dict = {\n",
" \"HHbbVV\": 50,\n",
" \"VBFHHbbVV\": 100,\n",
" # \"qqHH_CV_1_C2V_0_kl_1_HHbbVV\": 10,\n",
" # \"qqHH_CV_1_C2V_2_kl_1_HHbbVV\": 10,\n",
"}\n",
"\n",
"sig_key = \"HHbbVV\"\n",
"\n",
"for year, temps in templates.items():\n",
" # if year != \"2017\":\n",
" # continue\n",
"\n",
" plot_dir = main_plot_dir / year / \"wshifts\"\n",
" plot_dir.mkdir(exist_ok=True, parents=True)\n",
" for rname, region in regions.get_nonres_selection_regions(year).items():\n",
" if region.lpsf:\n",
" continue\n",
"\n",
" # if rname != \"passggf\":\n",
" # continue\n",
"\n",
" for wshift in list((jecs | jmsr).keys()) + [\"pileup\"]:\n",
" if wshift in jecs | jmsr:\n",
" continue\n",
" # adding jshift-ed histograms into the same histogram\n",
" cjshift_templates = [temps[rname]]\n",
" for shift in [\"up\", \"down\"]:\n",
" # new histogram with sample names renamed to \"{sample}_{jsf}_{shift}\"\n",
" jt = temps[f\"{rname}_{wshift}_{shift}\"]\n",
" slabels = [f\"{s}_{wshift}_{shift}\" for s in jt.axes[0]]\n",
" rjt = Hist(\n",
" hist.axis.StrCategory(slabels, name=\"Sample\"),\n",
" *jt.axes[1:],\n",
" storage=\"weight\",\n",
" )\n",
" rjt.view()[...] = jt.view()\n",
" cjshift_templates.append(rjt)\n",
"\n",
" cjt = combine_hists(*cjshift_templates)\n",
" shift_label = wshift\n",
" else:\n",
" shift_label = postprocessing.weight_shifts[wshift].label\n",
" cjt = temps[rname]\n",
"\n",
" plotting.sigErrRatioPlot(\n",
" cjt,\n",
" year,\n",
" sig_key,\n",
" wshift,\n",
" f\"{region.label} Region {shift_label} Variations\",\n",
" plot_dir,\n",
" f\"{rname}_sig_{wshift}\",\n",
" show=False,\n",
" )\n",
"\n",
" plotting.ratioHistPlot(\n",
" cjt,\n",
" year,\n",
" [],\n",
" p_bg_keys,\n",
" sig_scale_dict=sig_scale_dict,\n",
" syst=(wshift, p_all_keys),\n",
" title=f\"{region.label} Region {wshift} Variations\",\n",
" plot_ratio=False,\n",
" plot_data=True,\n",
" bg_err_type=\"line\",\n",
" # plot_signal=False,\n",
" name=f\"{plot_dir}/{rname}_bg_{wshift}.pdf\",\n",
" show=False,\n",
" )\n",
"\n",
" # break\n",
" # break\n",
" # break"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plot_dir = main_plot_dir / \"wshifts\"\n",
"plot_dir.mkdir(exist_ok=True, parents=True)\n",
"\n",
"temps = datacardHelpers.sum_templates(templates, years)\n",
"\n",
"for rname, region in regions.get_nonres_selection_regions(year).items():\n",
" if region.lpsf:\n",
" continue\n",
"\n",
" if rname != \"passggf\":\n",
" continue\n",
"\n",
" for wshift in [\"FSRPartonShower\", \"ISRPartonShower\"]:\n",
" shift_label = postprocessing.weight_shifts[wshift].label\n",
" cjt = temps[rname]\n",
"\n",
" plotting.sigErrRatioPlot(\n",
" cjt,\n",
" \"all\",\n",
" sig_key,\n",
" wshift,\n",
" f\"{region.label} Region {shift_label} Variations\",\n",
" plot_dir,\n",
" f\"{rname}_sig_{wshift}\",\n",
" show=False,\n",
" )\n",
"\n",
" plotting.ratioHistPlot(\n",
" cjt,\n",
" \"all\",\n",
" [],\n",
" p_bg_keys,\n",
" sig_scale_dict=sig_scale_dict,\n",
" syst=(wshift, p_all_keys),\n",
" title=f\"{region.label} Region {shift_label} Variations\",\n",
" plot_ratio=False,\n",
" plot_data=True,\n",
" bg_err_type=\"line\",\n",
" # plot_signal=False,\n",
" name=f\"{plot_dir}/{rname}_bg_{wshift}.pdf\",\n",
" show=False,\n",
" )\n",
"\n",
" # break\n",
" # break"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
2 changes: 1 addition & 1 deletion src/HHbbVV/postprocessing/PostProcess.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand Down
Loading

0 comments on commit 0c1d6d6

Please sign in to comment.