From 7122f702e591dd37b847c25aa795c67ce72b562d Mon Sep 17 00:00:00 2001 From: rkansal47 Date: Sat, 2 Sep 2023 12:29:17 -0500 Subject: [PATCH] resonant plots in same figure finally --- src/HHbbVV/postprocessing/PlotFitsRes.ipynb | 76 +++++++++++++-------- src/HHbbVV/postprocessing/plotting.py | 29 +++++--- 2 files changed, 69 insertions(+), 36 deletions(-) diff --git a/src/HHbbVV/postprocessing/PlotFitsRes.ipynb b/src/HHbbVV/postprocessing/PlotFitsRes.ipynb index 1fe3cbbf..bf436e2d 100644 --- a/src/HHbbVV/postprocessing/PlotFitsRes.ipynb +++ b/src/HHbbVV/postprocessing/PlotFitsRes.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -24,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -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": [ @@ -60,7 +60,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -86,7 +86,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -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": [ @@ -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" ] }, diff --git a/src/HHbbVV/postprocessing/plotting.py b/src/HHbbVV/postprocessing/plotting.py index 59a22ed1..255f232f 100644 --- a/src/HHbbVV/postprocessing/plotting.py +++ b/src/HHbbVV/postprocessing/plotting.py @@ -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 @@ -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 @@ -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: @@ -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(