Skip to content

Commit

Permalink
notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
jmduarte committed May 23, 2024
1 parent 4c4773f commit 02381b3
Show file tree
Hide file tree
Showing 2 changed files with 565 additions and 0 deletions.
299 changes: 299 additions & 0 deletions src/HH4b/postprocessing/PlotFits.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import argparse\n",
"import os\n",
"from pathlib import Path\n",
"from collections import OrderedDict\n",
"\n",
"import hist\n",
"import numpy as np\n",
"import uproot\n",
"\n",
"from HH4b import plotting\n",
"from HH4b.utils import ShapeVar\n",
"from HH4b.hh_vars import data_key"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"MAIN_DIR = Path(\"../../../\")\n",
"nTF = 1\n",
"\n",
"vbf = False\n",
"# k2v0sig = True\n",
"mreg = True\n",
"\n",
"plot_dir = MAIN_DIR / \"plots/PostFit/24Apr21_legacy_bdt_ggf_tighter\"\n",
"plot_dir.mkdir(exist_ok=True, parents=True)\n",
"\n",
"regions = \"all\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cards_dir = \"24Apr21_legacy_bdt_ggf_tighter\"\n",
"file = uproot.open(\n",
" f\"/uscms/home/rkansal/hhcombine/hh4b/cards/{cards_dir}/FitShapes.root\"\n",
" # f\"/uscms/home/rkansal/eos/bbVV/cards/{cards_dir}/FitShapes.root\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# (name in templates -> name in cards)\n",
"hist_label_map_inverse = OrderedDict(\n",
" [\n",
" (\"qcd\", \"CMS_bbbb_hadronic_qcd_datadriven\"),\n",
" (\"others\", \"others\"),\n",
" (\"ttbar\", \"ttbar\"),\n",
" (\"vhtobb\", \"VH_hbb\"),\n",
" (\"tthtobb\", \"ttH_hbb\"),\n",
" (\"data\", \"data_obs\"),\n",
" ]\n",
")\n",
"\n",
"if vbf:\n",
" hist_label_map_inverse[\"vbfhh4b-k2v0\"] = \"vbfhh4b-k2v0\"\n",
"else:\n",
" hist_label_map_inverse[\"hh4b\"] = \"hh4b\"\n",
"\n",
"hist_label_map = {val: key for key, val in hist_label_map_inverse.items()}\n",
"samples = list(hist_label_map.values())\n",
"\n",
"fit_shape_var_msd = ShapeVar(\n",
" \"H2Msd\",\n",
" r\"$m^{j2}_\\mathrm{SD}$ (GeV)\",\n",
" [16, 60, 220],\n",
" reg=True,\n",
" blind_window=[110, 140],\n",
")\n",
"\n",
"fit_shape_var_mreg = ShapeVar(\n",
" \"H2PNetMass\",\n",
" r\"$m^{j2}_\\mathrm{reg}$ (GeV)\",\n",
" [16, 60, 220],\n",
" reg=True,\n",
" blind_window=[110, 140],\n",
")\n",
"shape_vars = [fit_shape_var_msd] if not mreg else [fit_shape_var_mreg]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"shapes = {\n",
" \"prefit\": \"Pre-Fit\",\n",
" # \"postfit\": \"S+B Post-Fit\",\n",
" \"postfit\": \"B-only Post-Fit\",\n",
"}\n",
"\n",
"selection_regions_labels = {\n",
" \"passbin1\": \"Pass Bin1\",\n",
" \"passbin2\": \"Pass Bin2\",\n",
" \"passbin3\": \"Pass Bin3\",\n",
" \"fail\": \"Fail\",\n",
"}\n",
"\n",
"if vbf:\n",
" selection_regions_labels[\"passvbf\"] = \"Pass VBF\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if regions == \"all\":\n",
" signal_regions = [\"passbin1\", \"passbin2\", \"passbin3\"]\n",
" if vbf:\n",
" signal_regions = [\"passvbf\"] + signal_regions\n",
"else:\n",
" signal_regions = [regions]\n",
"\n",
"bins = [*signal_regions, \"fail\"]\n",
"selection_regions = {key: selection_regions_labels[key] for key in bins}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"hists = {}\n",
"for shape in shapes:\n",
" hists[shape] = {\n",
" region: hist.Hist(\n",
" hist.axis.StrCategory(samples, name=\"Sample\"),\n",
" *[shape_var.axis for shape_var in shape_vars],\n",
" storage=\"double\",\n",
" )\n",
" for region in selection_regions\n",
" }\n",
"\n",
" for region in selection_regions:\n",
" h = hists[shape][region]\n",
" templates = file[f\"{region}_{shape}\"]\n",
" # print(templates)\n",
" for key, file_key in hist_label_map_inverse.items():\n",
" if key != data_key:\n",
" if file_key not in templates:\n",
" print(f\"No {key} in {region}\")\n",
" continue\n",
"\n",
" data_key_index = np.where(np.array(list(h.axes[0])) == key)[0][0]\n",
" h.view(flow=False)[data_key_index, :] = templates[file_key].values()\n",
"\n",
" data_key_index = np.where(np.array(list(h.axes[0])) == data_key)[0][0]\n",
" h.view(flow=False)[data_key_index, :] = np.nan_to_num(\n",
" templates[hist_label_map_inverse[data_key]].values()\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Signal in mass window:\", np.sum(hists[\"postfit\"][\"passbin1\"][\"hh4b\", 5:8].values()))\n",
"\n",
"bg_tot = np.sum(\n",
" [\n",
" np.sum(hists[\"postfit\"][\"passbin1\"][key, 5:8].values())\n",
" for key in hist_label_map_inverse\n",
" if key not in [\"hh4b\", \"vbfhh4b-k2v0\", \"data\"]\n",
" ]\n",
")\n",
"print(\"BG in mass window:\", bg_tot)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print([key for key in hist_label_map_inverse if key not in [\"hh4b\", \"vbfhh4b-k2v0\", \"data\"]])\n",
"{\n",
" key: np.sum(hists[\"postfit\"][\"passbin1\"][key, 5:8].values())\n",
" for key in hist_label_map_inverse\n",
" if key not in [\"hh4b\", \"vbfhh4b-k2v0\", \"data\"]\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"year = \"2022-2023\"\n",
"pass_ratio_ylims = [0, 2]\n",
"fail_ratio_ylims = [0, 2]\n",
"signal_scale = 5.0\n",
"\n",
"ylims = {\n",
" \"passvbf\": 15,\n",
" \"passbin1\": 10,\n",
" \"passbin2\": 50,\n",
" \"passbin3\": 800,\n",
" \"fail\": 100000,\n",
"}\n",
"\n",
"for shape, shape_label in shapes.items():\n",
" for region, region_label in selection_regions.items():\n",
" pass_region = region.startswith(\"pass\")\n",
" for shape_var in shape_vars:\n",
" # print(hists[shape][region])\n",
" plot_params = {\n",
" \"hists\": hists[shape][region],\n",
" \"sig_keys\": [\"hh4b\"] if not vbf else [\"vbfhh4b-k2v0\"],\n",
" \"sig_scale_dict\": (\n",
" {\"hh4b\": signal_scale if pass_region else 1.0} if not vbf else None\n",
" ),\n",
" \"bg_keys\": [\"qcd\", \"ttbar\", \"vhtobb\", \"tthtobb\", \"others\"],\n",
" \"show\": True,\n",
" \"year\": year,\n",
" \"ylim\": ylims[region],\n",
" \"xlim\": 220,\n",
" # \"xlim_low\": 50,\n",
" \"xlim_low\": 60,\n",
" \"ratio_ylims\": pass_ratio_ylims if pass_region else fail_ratio_ylims,\n",
" \"title\": f\"{shape_label} {region_label} Region\",\n",
" \"name\": f\"{plot_dir}/{shape}_{region}_{shape_var.var}.pdf\",\n",
" \"bg_order\": [\"diboson\", \"vjets\", \"vhtobb\", \"ttbar\", \"qcd\"],\n",
" \"energy\": 13.6,\n",
" }\n",
"\n",
" plotting.ratioHistPlot(**plot_params)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "bbVV",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.15"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "5b9eab485576227e6cf1b964bb8855c46cbdf15c3e77cecdb2bb309145d3e8d8"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 02381b3

Please sign in to comment.