Skip to content

Commit

Permalink
resonant plots in same figure finally
Browse files Browse the repository at this point in the history
  • Loading branch information
rkansal47 committed Sep 2, 2023
1 parent c94b3b1 commit 7122f70
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 36 deletions.
76 changes: 49 additions & 27 deletions src/HHbbVV/postprocessing/PlotFitsRes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -24,7 +24,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -34,19 +34,19 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"MAIN_DIR = \"../../../\"\n",
"\n",
"plot_dir = f\"{MAIN_DIR}/plots/PostFit/23Sep1\"\n",
"plot_dir = f\"{MAIN_DIR}/plots/PostFit/23Sep2\"\n",
"_ = os.system(f\"mkdir -p {plot_dir}\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -60,7 +60,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -86,7 +86,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -98,17 +98,19 @@
"\n",
"shape_vars = res_shape_vars\n",
"\n",
"selection_regions = {\n",
" \"pass\": \"Pass\",\n",
" \"fail\": \"Fail\",\n",
" \"passBlinded\": \"Validation Pass\",\n",
" \"failBlinded\": \"Validation Fail\",\n",
"}"
"selection_regions = OrderedDict(\n",
" [\n",
" (\"fail\", \"Fail\"),\n",
" (\"pass\", \"Pass\"),\n",
" (\"failBlinded\", \"Validation Fail\"),\n",
" (\"passBlinded\", \"Validation Pass\"),\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -156,37 +158,57 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pass_ylims = [25, 4]\n",
"fail_ylims = [17000, 1700]\n",
"figs = []\n",
"pass_ylims = [16, 3]\n",
"fail_ylims = [14000, 1700]\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",
" # if region == \"pass\" or region == \"fail\":\n",
" # continue\n",
" for i, shape_var in enumerate(shape_vars):\n",
" for i, shape_var in enumerate(shape_vars):\n",
" # add \"invisible\" subplots between main plots to add spacing\n",
" fig, axs = plt.subplots(\n",
" 5,\n",
" 3,\n",
" figsize=(25, 29),\n",
" gridspec_kw=dict(\n",
" height_ratios=[3, 1, 0.5, 3, 1], width_ratios=[1, 0.12, 1], hspace=0, wspace=0\n",
" ),\n",
" )\n",
"\n",
" for ax in axs[2]:\n",
" ax.set_visible(False)\n",
"\n",
" for ax in axs[:, 1]:\n",
" ax.set_visible(False)\n",
"\n",
" for j, (region, region_label) in enumerate(selection_regions.items()):\n",
" row = (j // 2) * 3\n",
" col = (j % 2) * 2\n",
" pass_region = region.startswith(\"pass\")\n",
"\n",
" plot_params = {\n",
" \"hists\": hists[shape][region].project(0, i + 1),\n",
" \"sig_keys\": [f\"X[{mx}]->H(bb)Y[{my}](VV)\"],\n",
" \"bg_keys\": [\"QCD\", \"V+Jets\", \"TT\", \"ST\"],\n",
" \"sig_scale_dict\": {f\"X[{mx}]->H(bb)Y[{my}](VV)\": 10},\n",
" \"show\": False,\n",
" # \"show\": False,\n",
" \"year\": \"all\",\n",
" \"ylim\": pass_ylims[i] if pass_region else fail_ylims[i],\n",
" \"plot_data\": region != \"pass\",\n",
" \"title\": f\"{shape_label} {region_label} Region\",\n",
" \"name\": f\"{plot_dir}/{shape}_{region}_{shape_var.var}.pdf\",\n",
" # \"title\": f\"{shape_label} {region_label} Region\",\n",
" # \"name\": f\"{plot_dir}/{shape}_{region}_{shape_var.var}.pdf\",\n",
" \"divide_bin_width\": True,\n",
" \"axrax\": (axs[row, col], axs[row + 1, col]),\n",
" }\n",
"\n",
" plotting.ratioHistPlot(**plot_params)\n",
" axs[row, col].set_title(region_label, x=0.55, y=1.002)\n",
"\n",
" # break\n",
" plt.savefig(f\"{plot_dir}/{shape}_{shape_var.var}.pdf\", bbox_inches=\"tight\")\n",
" plt.show()\n",
" # break\n",
" # break"
]
},
Expand Down
29 changes: 20 additions & 9 deletions src/HHbbVV/postprocessing/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def ratioHistPlot(
bg_order: List[str] = bg_order,
ratio_ylims: List[float] = [0, 2],
divide_bin_width: bool = False,
axrax: Tuple = None,
):
"""
Makes and saves a histogram plot, with backgrounds stacked, signal separate (and optionally
Expand Down Expand Up @@ -168,6 +169,10 @@ def ratioHistPlot(
variation (Tuple): Tuple of
(wshift: name of systematic e.g. pileup, shift: up or down, wsamples: list of samples which are affected by this)
plot_data (bool): plot data
bg_order (List[str]): order in which to plot backgrounds
ratio_ylims (List[float]): y limits on the ratio plots
divide_bin_width (bool): divide yields by the bin width (for resonant fit regions)
axrax (Tuple): optionally input ax and rax instead of creating new ones
"""

# copy hists so input object is not changed
Expand Down Expand Up @@ -199,9 +204,13 @@ def ratioHistPlot(
del sig_scale_dict[sig_key], sig_labels[sig_key]

# set up plots
fig, (ax, rax) = plt.subplots(
2, 1, figsize=(12, 14), gridspec_kw=dict(height_ratios=[3, 1], hspace=0), sharex=True
)
if axrax is None:
fig, (ax, rax) = plt.subplots(
2, 1, figsize=(12, 14), gridspec_kw=dict(height_ratios=[3, 1], hspace=0), sharex=True
)
else:
ax, rax = axrax
ax.sharex(rax)

# plot histograms
if divide_bin_width:
Expand Down Expand Up @@ -309,13 +318,15 @@ def ratioHistPlot(
hep.cms.label(
"Work in Progress", data=True, lumi=f"{LUMI[year] / 1e3:.0f}", year=year, ax=ax
)
if len(name):
plt.savefig(name, bbox_inches="tight")

if show:
plt.show()
else:
plt.close()
if axrax is None:
if len(name):
plt.savefig(name, bbox_inches="tight")

if show:
plt.show()
else:
plt.close()


def ratioLinePlot(
Expand Down

0 comments on commit 7122f70

Please sign in to comment.