3
3
"""
4
4
5
5
import law
6
+ import luigi
6
7
7
8
from functools import cached_property
8
9
@@ -39,10 +40,18 @@ class GetBtagNormalizationSF(
39
40
40
41
store_as_dict = False
41
42
42
- rescale_mode = "nevents"
43
-
44
43
reweighting_step = "selected_no_bjet"
45
44
45
+ rescale_mode = luigi .ChoiceParameter (
46
+ default = "nevents" ,
47
+ choices = ("nevents" , "xs" ),
48
+ )
49
+ base_key = luigi .ChoiceParameter (
50
+ default = "rescaled_sum_mc_weight" ,
51
+ # NOTE: "num_events" does not work because I did not store the corresponding key in the stats :/
52
+ choices = ("rescaled_sum_mc_weight" , "sum_mc_weight" , "num_events" ),
53
+ )
54
+
46
55
# default sandbox, might be overwritten by selector function
47
56
sandbox = dev_sandbox (law .config .get ("analysis" , "default_columnar_sandbox" ))
48
57
@@ -98,6 +107,9 @@ def store_parts(self):
98
107
processes_repr = "__" .join (self .processes )
99
108
parts .insert_before ("version" , "processes" , processes_repr )
100
109
110
+ significant_params = (self .rescale_mode , self .base_key )
111
+ parts .insert_before ("version" , "params" , "__" .join (significant_params ))
112
+
101
113
return parts
102
114
103
115
def output (self ):
@@ -143,23 +155,24 @@ def safe_div(num, den):
143
155
)
144
156
145
157
# rescale the histograms
146
- for dataset , hists in hists_per_dataset .items ():
147
- process = self .config_inst .get_dataset (dataset ).processes .get_first ()
148
- if self .rescale_mode == "xs" :
149
- # scale such that the sum of weights is the cross section
150
- xs = process .get_xsec (self .config_inst .campaign .ecm ).nominal
151
- dataset_factor = xs / hists ["sum_mc_weight" ][{"steps" : "Initial" }].value
152
- elif self .rescale_mode == "nevents" :
153
- # scale such that mean weight is 1
154
- n_events = hists ["num_events" ][{"steps" : self .reweighting_step }].value
155
- dataset_factor = n_events / hists ["sum_mc_weight" ][{"steps" : self .reweighting_step }].value
156
- else :
157
- raise ValueError (f"Invalid rescale mode { self .rescale_mode } " )
158
- for key in tuple (hists .keys ()):
159
- if "sum" not in key :
160
- continue
161
- h = hists [key ].copy () * dataset_factor
162
- hists [f"rescaled_{ key } " ] = h
158
+ if "rescaled" in self .base_key :
159
+ for dataset , hists in hists_per_dataset .items ():
160
+ process = self .config_inst .get_dataset (dataset ).processes .get_first ()
161
+ if self .rescale_mode == "xs" :
162
+ # scale such that the sum of weights is the cross section
163
+ xs = process .get_xsec (self .config_inst .campaign .ecm ).nominal
164
+ dataset_factor = xs / hists ["sum_mc_weight" ][{"steps" : "Initial" }].value
165
+ elif self .rescale_mode == "nevents" :
166
+ # scale such that mean weight is 1
167
+ n_events = hists ["num_events" ][{"steps" : self .reweighting_step }].value
168
+ dataset_factor = n_events / hists ["sum_mc_weight" ][{"steps" : self .reweighting_step }].value
169
+ else :
170
+ raise ValueError (f"Invalid rescale mode { self .rescale_mode } " )
171
+ for key in tuple (hists .keys ()):
172
+ if "sum" not in key :
173
+ continue
174
+ h = hists [key ].copy () * dataset_factor
175
+ hists [f"rescaled_{ key } " ] = h
163
176
164
177
# if necessary, merge the histograms across datasets
165
178
if len (hists_per_dataset ) > 1 :
@@ -183,18 +196,18 @@ def safe_div(num, den):
183
196
# ("",),
184
197
):
185
198
mode_str = "_" .join (mode )
186
- numerator = merged_hists ["rescaled_sum_mc_weight_per_process_ht_njet_nhf " ]
199
+ numerator = merged_hists [f" { self . base_key } _per_process_ht_njet_nhf " ]
187
200
numerator = self .reduce_hist (numerator , mode ).values ()
188
201
189
202
for key in merged_hists .keys ():
190
203
if (
191
- not key .startswith ("rescaled_sum_mc_weight_btag_weight " ) or
204
+ not key .startswith (f" { self . base_key } _btag_weight " ) or
192
205
not key .endswith ("_per_process_ht_njet_nhf" )
193
206
):
194
207
continue
195
208
196
209
# extract the weight name
197
- weight_name = key .replace ("rescaled_sum_mc_weight_ " , "" ).replace ("_per_process_ht_njet_nhf" , "" )
210
+ weight_name = key .replace (f" { self . base_key } _ " , "" ).replace ("_per_process_ht_njet_nhf" , "" )
198
211
199
212
# create the scale factor histogram
200
213
h = merged_hists [key ]
0 commit comments