-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvariations.py
464 lines (401 loc) · 16.4 KB
/
variations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
from copy import deepcopy
from .booking import Unit
from .booking import dataset_from_crownoutput
from .booking import dataset_from_artusoutput
from .utils import Selection
from .utils import Variation
import logging
import re
logger = logging.getLogger(__name__)
def get_quantities_from_expression(expression):
# first change all operators to &&
for operator in [
"<",
">",
"=",
"!=",
"+",
"-",
"*",
"/",
"||",
">=",
"<=",
"&&",
"(",
")",
"!",
]:
expression = expression.replace(operator, "&&")
# then remove all brackets and spaces
expression = expression.replace(" ", "")
# then split by && and remove empty strings and numbers
quantities = [
q
for q in expression.split("&&")
if not q.replace(".", "", 1).isdigit() and q != ""
]
# special keywords can also be filtered out
special_keywords = ["true", "false", "abs"]
quantities = [q for q in quantities if q not in special_keywords]
# return a set of quantities
return set(quantities)
class ReplaceVariable(Variation):
"""
Variation that with the method create makes a deepcopy of
the selections and actions inside the unit passed as argument and substitutes
the directory attribute with the according variation.
Args:
name (str): name used to identify the instance of
this class
variation (str)
"""
def __init__(self, name, variation):
Variation.__init__(self, name)
self.variation = variation
def create(self, unit):
new_selections = deepcopy(unit.selections)
new_actions = deepcopy(unit.actions)
replaced = False
if self.variation not in unit.dataset.quantities_per_vars:
logger.fatal(
f"Variation {self.variation} not found in ntuple for dataset {unit.dataset.name}"
)
logger.fatal(
f"Available variations are: {unit.dataset.quantities_per_vars.keys()}"
)
raise NameError
else:
list_of_quantities = set(unit.dataset.quantities_per_vars[self.variation])
for sel_obj in new_selections:
for cut in sel_obj.cuts:
# if any quantities used in the cut are in the list of quantities affected by the variation, replace them
for quantity in list_of_quantities & get_quantities_from_expression(
cut.expression
):
cut.expression = re.sub(
rf"\b({quantity})\b",
f"{quantity}__{self.variation}",
cut.expression,
)
replaced = True
logger.debug(
f"Replaced expression {quantity} with {cut.expression} ( quantity: {quantity}, var: {self.variation})"
)
for weight in sel_obj.weights:
# if any quantities used in the weight are in the list of quantities affected by the variation, replace them
for quantity in list_of_quantities & get_quantities_from_expression(
weight.expression
):
logger.debug(f"Initial weight: {weight.expression}")
weight.expression = re.sub(
rf"\b({quantity})\b",
f"{quantity}__{self.variation}",
weight.expression,
)
replaced = True
logger.debug(
f"Replaced weight {quantity} with {weight.expression} ( quantity: {quantity}, var: {self.variation})"
)
for act in new_actions:
old_act = deepcopy(act.variable)
# if any quantities used in the action variables are in the list of quantities affected by the variation, replace them
for quantity in list_of_quantities & get_quantities_from_expression(
act.variable
):
logger.debug(
f"Replacing quantity {quantity} with {quantity}__{self.variation}"
)
act.variable = re.sub(
rf"\b({quantity})\b",
f"{quantity}__{self.variation}",
act.variable,
)
replaced = True
logger.debug(f"Replaced act {old_act} with {act.variable}")
old_act = deepcopy(act.variable)
if not replaced:
logger.warning(
f"[Unused Variation] For variation {self.variation} on unit {unit.dataset.name} no quantities were replaced, the shift has no effect.."
)
logger.warning(
f"Quantities affected by {self.variation}: {list_of_quantities} "
)
return Unit(unit.dataset, new_selections, new_actions, self)
class ChangeDataset(Variation):
"""
Variation that with the method create makes a deepcopy of
the dataset inside the unit passed as argument and substitutes
the directory attribute with folder_name.
Args:
name (str): name used to identify the instance of
this class
folder_name (str): part of the name of the TDirectoryFile
in the new dataset following the prefix 'channel_' and
preceding the suffix '/tree_name', e.g. 'NewFolder' in
'mt_NewFolder/ntuple'
Attributes:
name (str): name used to identify the instance of
this class
folder_name (str): part of the name of the TDirectoryFile
in the new dataset following the prefix 'channel_' and
preceding the suffix '/tree_name', e.g. 'NewFolder' in
'mt_NewFolder/ntuple'
"""
def __init__(self, name, folder_name):
Variation.__init__(self, name)
self.folder_name = folder_name
def create(self, unit):
# A perfect copy of the daatset is exactly what
# we need, thus using deepcopy is fine
def change_folder(ntuple):
folder, tree = ntuple.directory.split("/")
ntuple.directory = "{}_{}/{}".format(
folder.split("_")[0], self.folder_name, tree
)
new_dataset = deepcopy(unit.dataset)
for ntuple in new_dataset.ntuples:
change_folder(ntuple)
for friend in ntuple.friends:
change_folder(friend)
return Unit(new_dataset, unit.selections, unit.actions, self)
class ReplaceCut(Variation):
def __init__(self, name, replaced_name, cut):
Variation.__init__(self, name)
self.replaced_name = replaced_name
self.cut = cut
def create(self, unit):
# Check that the name is present in at least one of the selections and raise an
# error if not
if not set(
[
cut.name
for selection in unit.selections
for cut in selection.cuts
if cut.name == self.replaced_name
]
):
logger.fatal(
"Cut {} not found in any selection of this Unit".format(
self.replaced_name
)
)
raise NameError
new_selections = list()
for selection in unit.selections:
copy_cuts = list()
for cut in selection.cuts:
if cut.name == self.replaced_name:
logger.debug(
"Substitute {} with {} in selection {}".format(
cut, self.cut, selection
)
)
copy_cuts.append(self.cut)
else:
copy_cuts.append(cut)
new_selections.append(
Selection(selection.name, copy_cuts, selection.weights)
)
return Unit(unit.dataset, new_selections, unit.actions, self)
class ReplaceWeight(Variation):
def __init__(self, name, replaced_name, weight):
Variation.__init__(self, name)
self.replaced_name = replaced_name
self.weight = weight
def create(self, unit):
# Check that the name is present in at least one of the selections and raise an
# error if not
if not set(
[
weight.name
for selection in unit.selections
for weight in selection.weights
if weight.name == self.replaced_name
]
):
logger.fatal(
"Weight {} not found in any selection of this Unit".format(
self.replaced_name
)
)
raise NameError
new_selections = list()
for selection in unit.selections:
copy_weights = list()
for weight in selection.weights:
if weight.name == self.replaced_name:
logger.debug(
"Substitute {} with {} in selection {}".format(
weight, self.weight, selection
)
)
copy_weights.append(self.weight)
else:
copy_weights.append(weight)
new_selections.append(
Selection(selection.name, selection.cuts, copy_weights)
)
return Unit(unit.dataset, new_selections, unit.actions, self)
class RemoveCut(Variation):
def __init__(self, name, removed_name):
Variation.__init__(self, name)
self.removed_name = removed_name
def create(self, unit):
# Check that the name is present in at least one of the selections and raise an
# error if not
if not set(
[
cut.name
for selection in unit.selections
for cut in selection.cuts
if cut.name == self.removed_name
]
):
logger.fatal(
"Cut {} not found in any selection of this Unit".format(
self.removed_name
)
)
raise NameError
new_selections = [deepcopy(selection) for selection in unit.selections]
for new_selection in new_selections:
new_selection.remove_cut(self.removed_name)
return Unit(unit.dataset, new_selections, unit.actions, self)
class RemoveWeight(Variation):
def __init__(self, name, removed_name):
Variation.__init__(self, name)
self.removed_name = removed_name
def create(self, unit):
# Check that the name is present in at least one of the selections and raise an
# error if not
if not set(
[
weight.name
for selection in unit.selections
for weight in selection.weights
if weight.name == self.removed_name
]
):
logger.fatal(
"Weight {} not found in any selection of this Unit".format(
self.removed_name
)
)
raise NameError
new_selections = [deepcopy(selection) for selection in unit.selections]
for new_selection in new_selections:
new_selection.remove_weight(self.removed_name)
return Unit(unit.dataset, new_selections, unit.actions, self)
class AddCut(Variation):
def __init__(self, name, cut):
Variation.__init__(self, name)
self.cut = cut
def create(self, unit):
new_selections = [selection for selection in unit.selections]
new_selections.append(Selection(name=self.cut.name, cuts=[self.cut]))
return Unit(unit.dataset, new_selections, unit.actions, self)
class AddWeight(Variation):
def __init__(self, name, weight):
Variation.__init__(self, name)
self.weight = weight
def create(self, unit):
new_selections = [selection for selection in unit.selections]
new_selections.append(Selection(name=self.weight.name, weights=[self.weight]))
return Unit(unit.dataset, new_selections, unit.actions, self)
class SquareWeight(Variation):
def __init__(self, name, weight_name):
Variation.__init__(self, name)
self.weight_name = weight_name
def create(self, unit):
# Check that the name is present in at least one of the selections and raise an
# error if not
if not set(
[
weight.name
for selection in unit.selections
for weight in selection.weights
if weight.name == self.weight_name
]
):
logger.fatal(
"Weight {} not found in any selection of this Unit".format(
self.weight_name
)
)
raise NameError
new_selections = [deepcopy(selection) for selection in unit.selections]
for new_selection in new_selections:
for weight in new_selection.weights:
if weight.name == self.weight_name:
weight.square()
return Unit(unit.dataset, new_selections, unit.actions, self)
class ReplaceCutAndAddWeight(Variation):
def __init__(self, name, replaced_name, cut, weight):
Variation.__init__(self, name)
self.replace_cut = ReplaceCut(name, replaced_name, cut)
self.add_weight = AddWeight(name, weight)
def create(self, unit):
unit = self.replace_cut.create(unit)
return self.add_weight.create(unit)
class ReplaceMultipleCuts(Variation):
def __init__(self, name, replaced_names, cuts):
Variation.__init__(self, name)
self.replace_cuts = [
ReplaceCut(name, rep_name, cut)
for rep_name, cut in zip(replaced_names, cuts)
]
def create(self, unit):
for replace_cut in self.replace_cuts:
unit = replace_cut.create(unit)
return unit
class ReplaceMultipleCutsAndAddWeight(Variation):
def __init__(self, name, replaced_names, cuts, weight):
Variation.__init__(self, name)
self.replace_cuts = [
ReplaceCut(name, rep_name, cut)
for rep_name, cut in zip(replaced_names, cuts)
]
self.add_weight = AddWeight(name, weight)
def create(self, unit):
for replace_cut in self.replace_cuts:
unit = replace_cut.create(unit)
return self.add_weight.create(unit)
class ChangeDatasetReplaceCutAndAddWeight(Variation):
def __init__(self, name, folder_name, replaced_name, cut, weight):
Variation.__init__(self, name)
self.change_dataset = ChangeDataset(name, folder_name)
self.repl_and_add_weight = ReplaceCutAndAddWeight(
name, replaced_name, cut, weight
)
def create(self, unit):
unit = self.change_dataset.create(unit)
return self.repl_and_add_weight.create(unit)
class ChangeDatasetReplaceMultipleCutsAndAddWeight(Variation):
def __init__(self, name, folder_name, replaced_names, cuts, weight):
Variation.__init__(self, name)
self.change_dataset = ChangeDataset(name, folder_name)
self.repl_and_add_weight = ReplaceMultipleCutsAndAddWeight(
name, replaced_names, cuts, weight
)
def create(self, unit):
unit = self.change_dataset.create(unit)
return self.repl_and_add_weight.create(unit)
class ReplaceVariableReplaceCut(Variation):
def __init__(self, name, variation, replaced_name, cut):
Variation.__init__(self, name)
self.replace_variable = ReplaceVariable(name, variation)
self.replace_cut = ReplaceCut(name, replaced_name, cut)
def create(self, unit):
unit = self.replace_variable.create(unit)
return self.replace_cut.create(unit)
class ReplaceVariableReplaceCutAndAddWeight(Variation):
def __init__(self, name, variation, replaced_name, cut, weight):
Variation.__init__(self, name)
self.replace_variable = ReplaceVariable(name, variation)
self.repl_and_add_weight = ReplaceCutAndAddWeight(
name, replaced_name, cut, weight
)
def create(self, unit):
unit = self.replace_variable.create(unit)
return self.repl_and_add_weight.create(unit)