-
Notifications
You must be signed in to change notification settings - Fork 21
/
quantity.py
330 lines (283 loc) · 11.3 KB
/
quantity.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
from __future__ import annotations # needed for type annotations in > python 3.7
import logging
from typing import Dict, List, Set, Union
log = logging.getLogger(__name__)
class Quantity:
def __init__(self, name: str):
self.name = name
self.shifts: Dict[str, Set[str]] = {}
self.ignored_shifts: Dict[str, Set[str]] = {}
self.children: Dict[str, List[Quantity]] = {}
self.defined_for_scopes: List[str] = []
log.debug("Setting up new Quantity {}".format(self.name))
def __str__(self) -> str:
return self.name
def __repr__(self) -> str:
return self.name
def __lt__(self, other: Quantity) -> bool:
return self.name < other.name
def add(self, name: str) -> None:
"""
Function is not used for a base quantity
"""
pass
def reserve_scope(self, scope: str) -> None:
"""
Function to reserve a scope for a given quantity. The scopes, in which a quantity is used
as an output are tracked in the output_scopes list.
If a quantity is already used within a given scope as output, this will result in an exception.
This check is triggered for every Producer.
"""
log.debug("Checking {} / scope {}".format(self.name, scope))
if (scope == "global" and self.defined_for_scopes == []) or (
scope != "global" and scope not in self.defined_for_scopes
):
self.defined_for_scopes.append(scope)
else:
log.error(
"Quantity {} is already defined in {} scope !".format(self.name, scope)
)
raise Exception
def get_leaf(self, shift: str, scope: str) -> str:
"""
Function to get the leaf of a given shift within a given scope.
A leaf is the name of the quantity used for that scope/shift combination.
If no shift is defined, the name of the quantity is returned.
Args:
shift (str): Name of the shift for which the leaf should be returned
scope (str): Scope for which the leaf should be returned
Returns:
str. Name of the leaf
"""
if shift in self.get_shifts(scope):
return self.name + shift
return self.name
def get_leaves_of_scope(self, scope: str) -> List[str]:
"""
Function returns a list of all leaves, which are defined for a given scope.
Args:
scope (str): Scope for which the leaves should be returned
Returns:
list. List of leaves
"""
result = [self.name] + [
self.get_leaf(shift, scope) for shift in self.get_shifts(scope)
]
return result
def shift(self, name: str, scope: str) -> None:
"""
Function to define a shift for a given scope. If the shift is marked as ignored, nothing will be added.
When a new shift is defined, all child quantities of a given quantity will be shifted as well.
Args:
name (str): Name of the shift
scope (str): Scope for which the shift should be defined
Returns:
None
"""
if scope in self.ignored_shifts.keys():
if name in self.ignored_shifts[scope]:
log.debug("Ignoring shift {} for quantity {}".format(name, self.name))
return
log.debug("Adding shift {} to quantity {}".format(name, self.name))
if scope not in self.shifts.keys():
self.shifts[scope] = set()
if name not in self.shifts[scope]:
self.shifts[scope].add(name)
if scope == "global": # shift children in all scopes if scope is global
for any_scope in self.children:
for c in self.children[any_scope]:
c.shift(name, any_scope)
else:
if scope in self.children.keys():
for c in self.children[scope]:
c.shift(name, scope)
def ignore_shift(self, name: str, scope: str) -> None:
"""
Function to ignore a shift for a given scope.
Args:
name (str): Name of the shift to be ignored
scope (str): Scope for which the shift should be ignored
Returns:
None
"""
log.debug("Make quantity {} ignore shift {}".format(self.name, name))
if scope not in self.ignored_shifts.keys():
self.ignored_shifts[scope] = set()
self.ignored_shifts[scope].add(name)
def copy(self, name: str) -> Quantity:
"""
Generate a copy of the current quantity with a new name.
Args:
name (str): Name of the new quantity
Returns:
Quantity. a new Quantity object.
"""
copy = Quantity(name)
copy.shifts = self.shifts
copy.children = self.children
copy.ignored_shifts = self.ignored_shifts
copy.defined_for_scopes = self.defined_for_scopes
return copy
def adopt(self, child: Quantity, scope: str) -> None:
"""
Adopt a child quantity to the current quantity.
An adopted quantity will inherit all shifts of the partent quantity.
Args:
child (Quantity): The child quantity
scope (str): Scope for which the child should be adopted
Returns:
None
"""
log.debug(
"Adopting child quantity {} to quantity {}".format(child.name, self.name)
)
if scope not in self.children.keys():
self.children[scope] = []
self.children[scope].append(child)
def get_shifts(self, scope: str) -> List[str]:
"""
Function returns a list of all shifts, which are defined for a given scope.
Args:
scope (str): Scope for which shifts should be returned
Returns:
list: List of all shifts, which are defined for a given scope.
"""
if "global" in self.shifts.keys():
if scope != "global" and scope in self.shifts.keys():
log.error(
"Quantity {} has shifts in global and {}. Something must be broken!".format(
self.name, scope
)
)
raise Exception
return list(self.shifts["global"])
elif scope in self.shifts.keys():
return list(self.shifts[scope])
else:
return []
class QuantityGroup(Quantity):
"""
A Quantity Group is a group of quantities, that all have the same settings, but different names.
"""
def __init__(self, name: str):
super().__init__(name)
self.quantities: List[Quantity] = []
self.vec_config: str = ""
def set_vec_config(self, vec_config: str) -> None:
"""
Function to set the vec config key
Args:
vec_config (str): Name of the vec config key
Returns:
None
"""
self.vec_config = vec_config
def copy(self, name: str) -> Quantity:
"""
Copy is not allowed for Quantity Groups.
"""
log.error("Copy is not allowed for a Quantity Group !")
raise Exception
def add(self, name: str) -> None:
"""
Function to add a new Quantity to the group. This quantity contains the identical shifts as the group itself
Args:
name (str): Name of the new Quantity
Returns:
None
"""
if name not in [q.name for q in self.quantities]:
quantity = Quantity(name)
quantity.shifts = self.shifts
quantity.children = self.children
quantity.ignored_shifts = self.ignored_shifts
quantity.defined_for_scopes = self.defined_for_scopes
self.quantities.append(quantity)
def get_leaves_of_scope(self, scope: str) -> List[str]:
"""
Function returns a list of all leaves, which are defined for a given scope.
This is an overload of the function used for the quantity class.
For the writeout, loop over all quantities in the group and
return them all (plus their shifts) in a list.
Args:
scope (str): Scope for which the leaves should be returned
Returns:
list. List of leaves
"""
output: List[str] = []
for quantity in self.quantities:
output.extend(quantity.get_leaves_of_scope(scope))
return output
class NanoAODQuantity(Quantity):
"""
A NanoAODQuantity is a quantity that comes directly from the NanoAOD file.
Normally, these quantities are not suited to be used in the output ntuple,
are therefore shielded from using them directly as a output.
"""
def __init__(self, name: str):
super().__init__(name)
self.shifted_naming: Dict[str, str] = {}
def reserve_scope(self, scope: str) -> None:
"""
Function used to ensure, that NanoAOD quantities are not used as output
in a producer. If this is attempted, an Exception will be raised.
Args:
scope (str): Scope for which the quantity should be reserved (not used)
Returns:
None
"""
log.error(
"Quantity {} is a NanoAOD quantity and cant be used as output !".format(
self.name
)
)
raise Exception
def register_external_shift(
self, shift_name: str, external_name: Union[str, NanoAODQuantity]
) -> None:
"""
Function used to register a NanoAOD quantity as a shift of another quantity.
Iif the shifted version of a quantity already exists in the input,
this function can be used to register an
branch from the input as a shifted version of a quantity
Args:
shift_name (str): Name of the shift
external_name (str): Name of the shifted quantity in the NanoAOD
Returns:
None
"""
if shift_name not in self.shifted_naming.keys():
if isinstance(external_name, NanoAODQuantity):
self.shifted_naming[shift_name] = str(external_name)
else:
self.shifted_naming[shift_name] = external_name
for any_scope in self.children:
for c in self.children[any_scope]:
c.shift(shift_name, any_scope)
def get_leaf(self, shift: str, scope: str) -> str:
"""
Overloaded version for the `get_leaf` function to return the
shifted versions of the quantity if needed.
Args:
shift (str): Name of the shift for which the leaf should be returned
scope (str): Scope for which the leaf should be returned (not used)
Returns:
str. Name of the leaf
"""
if shift in self.shifted_naming.keys():
return self.shifted_naming[shift]
return self.name
def get_shifts(self, scope: str) -> List[str]:
"""
Overloaded version of the `get_shifts` function
Args:
scope (str): Scope for which shifts should be returned (not used)
Returns:
list: List of all shifts
"""
return list(self.shifted_naming.keys())
# Definitions for type annotations
QuantitiesInput = Union[
Quantity, NanoAODQuantity, List[Union[Quantity, NanoAODQuantity]]
]
QuantitiesStore = Dict[str, Set[Quantity]]