@@ -31,6 +31,7 @@ def data_mc(
31
31
log_scale_x : bool = False ,
32
32
label : str = "" ,
33
33
colors : Optional [Dict [str , str ]] = None ,
34
+ plot_options : Optional [Dict [str , Any ]] = None ,
34
35
close_figure : bool = False ,
35
36
) -> mpl .figure .Figure :
36
37
"""Draws a data/MC histogram with uncertainty bands and ratio panel.
@@ -51,6 +52,8 @@ def data_mc(
51
52
label (str, optional): label written on the figure, defaults to ""
52
53
colors (Optional[Dict[str, str]], optional): map of sample names and colors to
53
54
use in plot, defaults to None (uses default colors)
55
+ plot_options (Optional[Dict[str, Any]], optional): plotting configuration for
56
+ this figure, defaults to None (no additional configuration)
54
57
close_figure (bool, optional): whether to close each figure immediately after
55
58
saving it, defaults to False (enable when producing many figures to avoid
56
59
memory issues, prevents rendering in notebooks)
@@ -61,15 +64,26 @@ def data_mc(
61
64
Returns:
62
65
matplotlib.figure.Figure: the data/MC figure
63
66
"""
67
+ plot_options = plot_options or {} # no additional plot options by default
68
+
69
+ if "normalize_binwidth" in plot_options :
70
+ rescaling_factor , unit = plot_options ["normalize_binwidth" ]
71
+ bin_width_norm = (bin_edges [1 :] - bin_edges [:- 1 ]) / rescaling_factor
72
+ else :
73
+ unit = None
74
+ bin_width_norm = np .ones_like (bin_edges )
75
+
76
+ total_model_unc /= bin_width_norm # apply bin width normalization
77
+
64
78
mc_histograms_yields = []
65
79
mc_labels = []
66
80
for h in histogram_dict_list :
67
81
if h ["isData" ]:
68
- data_histogram_yields = h ["yields" ]
82
+ data_histogram_yields = h ["yields" ] / bin_width_norm
69
83
data_histogram_stdev = np .sqrt (data_histogram_yields )
70
84
data_label = h ["label" ]
71
85
else :
72
- mc_histograms_yields .append (h ["yields" ])
86
+ mc_histograms_yields .append (h ["yields" ] / bin_width_norm )
73
87
mc_labels .append (h ["label" ])
74
88
75
89
mpl .style .use (MPL_STYLE )
@@ -229,8 +243,12 @@ def data_mc(
229
243
all_containers , all_labels , frameon = False , fontsize = "large" , loc = "upper right"
230
244
)
231
245
246
+ vertical_axis_label = "events"
247
+ if unit is not None : # bin width normalization
248
+ vertical_axis_label += f" / { rescaling_factor } { unit } "
249
+
232
250
ax1 .set_xlim (bin_edges [0 ], bin_edges [- 1 ])
233
- ax1 .set_ylabel ("events" )
251
+ ax1 .set_ylabel (vertical_axis_label )
234
252
ax1 .set_xticklabels ([])
235
253
ax1 .set_xticklabels ([], minor = True )
236
254
ax1 .tick_params (axis = "both" , which = "major" , pad = 8 ) # tick label - axis padding
0 commit comments