5 from functools
import wraps
7 from AnaAlgorithm.Logging
import logging
8 logCPAlgCfgBlock = logging.getLogger(
'CPAlgCfgBlock')
10 from AnalysisAlgorithmsConfig.ConfigAccumulator
import DataType
14 """check whether the sample being run passes a"""
15 """possible DSID filter on the block"""
16 if len(filterList) == 0:
18 for dsid_filter
in filterList:
20 if any(char
in str(dsid_filter)
for char
in "^$*+?.()|[]{}\\"):
21 pattern = re.compile(dsid_filter)
22 if pattern.match(
str(config.dsid())):
26 if str(dsid_filter) ==
str(config.dsid()):
31 """this wrapper ensures that the 'instanceName' of the various """
32 """config blocks is cleaned up of any non-alphanumeric characters """
33 """that may arise from using 'selectionName' in the naming."""
35 def wrapper(*args, **kwargs):
37 orig_name = func(*args, **kwargs)
49 """this meta class enforces the application of 'alphanumeric_block_names()' """
50 """to 'instanceName()' and will be used in the main ConfigBlock class in order """
51 """to propagate this rule also to all derived classes (the individual config blocks."""
54 if 'instanceName' in dct
and callable(dct[
'instanceName']):
56 return super().
__new__(cls, name, bases, dct)
59 """the information for a single option on a configuration block"""
61 def __init__ (self, type=None, info='', noneAction='ignore', required=False,
72 """Class encoding a blocks dependence on other blocks."""
88 return f
'ConfigBlockDependency(blockName="{self.blockName}", required={self.required})'
92 """the base class for classes implementing individual blocks of
95 A configuration block is a sequence of one or more algorithms that
96 should always be scheduled together, e.g. the muon four momentum
97 corrections could be a single block, muon selection could then be
98 another block. The blocks themselves generally have their own
99 configuration options/properties specific to the block, and will
100 perform a dynamic configuration based on those options as well as
103 The actual configuration of the algorithms in the block will
104 depend on what other blocks are scheduled before and afterwards,
105 most importantly some algorithms will introduce shallow copies
106 that subsequent algorithms will need to run on, and some
107 algorithms will add selection decorations that subquent algorithms
108 should use as preselections.
110 The algorithms get created in a multi-step process (that may be
111 extended in the future): As a first step each block retrieves
112 references to the containers it uses (essentially marking its spot
113 in the processing chain) and also registering any shallow copies
114 that will be made. In the second/last step each block then
115 creates the fully configured algorithms.
117 One goal is that when the algorithms get created they will have
118 their final configuration and there needs to be no
119 meta-configuration data attached to the algorithms, essentially an
120 inversion of the approach in AnaAlgSequence in which the
121 algorithms got created first with associated meta-configuration
122 and then get modified in susequent configuration steps.
124 For now this is mostly an empty base class, but another goal of
125 this approach is to make it easier to build another configuration
126 layer on top of this one, and this class will likely be extended
127 and get data members at that point.
129 The child class needs to implement the method `makeAlgs` which is
130 given a single `ConfigAccumulator` type argument. This is meant to
131 create the sequence of algorithms that this block configures. This
132 is currently (28 Jul 2025) called twice and should do the same thing
133 during both calls, but the plan is to change that to a single call.
135 The child class should also implement the method `getInstanceName`
136 which should return a string that is used to distinguish between
137 multiple instances of the same block. This is used to append the
138 instance name to the names of all algorithms created by this block,
139 and may in the future also be used to distinguish between multiple
140 instances of the block.
152 self.
addOption(
'groupName',
'', type=str,
153 info=(
'Used to specify this block when setting an'
154 ' option at an arbitrary location.'))
155 self.
addOption(
'skipOnData',
False, type=bool,
156 info=(
'User option to prevent the block from running'
157 ' on data. This only affects blocks that are'
158 ' intended to run on data.'))
159 self.
addOption(
'skipOnMC',
False, type=bool,
160 info=(
'User option to prevent the block from running'
161 ' on MC. This only affects blocks that are'
162 ' intended to run on MC.'))
163 self.
addOption(
'onlyForDSIDs', [], type=list,
164 info=(
'Used to specify which MC DSIDs to allow this'
165 ' block to run on. Each element of the list'
166 ' can be a full DSID (e.g. 410470), or a regex'
167 ' (e.g. 410.* to select all 410xxx DSIDs, or'
168 ' ^(?!410) to veto them). An empty list means no'
169 ' DSID restriction.'))
170 self.
addOption(
'propertyOverrides', {}, type=
None,
171 info=(
'EXPERT USE ONLY: A dictionary of properties to'
172 ' override at the end of configuration. This should'
174 ' {"algName.toolName.propertyName": value, ...},'
175 ' without any automatically applied postfixes for'
176 ' the algorithm name. THIS IS MEANT TO BE EXPERT'
177 ' USAGE ONLY. Properties that need to be set by'
178 ' the user should be declared as options on the'
179 ' block itself. EXPERT USE ONLY!'))
182 if cls
not in ConfigBlock.instance_counts:
183 ConfigBlock.instance_counts[cls] = 0
186 stack = inspect.stack()
187 for frame_info
in stack:
189 parent_cls = frame_info.frame.f_locals.get(
'self',
None)
190 if parent_cls
is None or not isinstance(parent_cls, ConfigBlock):
192 if frame_info.function ==
"makeConfig":
193 ConfigBlock.instance_counts[cls] += 1
206 """get the factory name for this block
208 This is mostly to give a reliable means of identifying the type
209 of block we have in error messages. This is meant to be
210 automatically set by the factory based on the requested block
211 name, but there are a number of fallbacks. It is best not to
212 assume a specific format, this is mostly meant to be used as an
213 identifier in output messages.
221 return self.__class__.__name__
224 """set the factory name for this block
226 This is meant to be called automatically by the factory based on
227 the requested block name. If you are creating a block without a factory,
228 you can call this method to set the factory name manually.
233 """Get the name of the instance
235 The name of the instance is used to distinguish between multiple
236 instances of the same block. Most importantly, this will be
237 appended to the names of all algorithms created by this block.
238 This defaults to an empty string, but block implementations
239 should override it with an appropriate name based on identifying
240 options set on this instance. A typical example would be the
241 name of the (main) container, plus potentially the selection or
244 Ideally all blocks should override this method, but for backward
245 compatibility (28 Jul 25) it defaults to an empty string.
251 whether this block should be used for the given configuration
253 This is used by `ConfigSequence` to determine whether this block
254 should be included in the configuration.
256 if self.skipOnData
and config.dataType()
is DataType.Data:
258 if self.skipOnMC
and config.dataType()
is not DataType.Data:
266 Apply any configuration overrides specified in the block's
267 `propertyOverrides` option. This is meant to be called at the
268 end of the configuration process, after all algorithms have been
269 created and configured.
271 for key, value
in self.propertyOverrides.
items():
273 parts = key.split(
'.')
275 raise Exception(f
"Invalid override key format: {key}")
276 alg = config.getAlgorithm(parts[0])
278 raise Exception(f
"Algorithm {parts[0]} not found in config for override: {key}")
279 for name
in parts[1:-1]:
281 if hasattr(alg, name):
282 alg = getattr(alg, name)
284 raise Exception(f
"Tool {name} not found for override: {key}")
287 alg.__setattr__(parts[-1], value)
291 Add a dependency for the block. Dependency is corresponds to the
292 blockName of another block. If required is True, will throw an
293 error if dependency is not present; otherwise will move this
294 block after the required block. If required is False, will do
295 nothing if required block is not present; otherwise, it will
296 move block after required block.
300 self.
addOption(
'ignoreDependencies', [], type=list,
301 info=
'List of dependencies defined in the ConfigBlock to ignore.')
305 """Return True if there is a dependency."""
309 """Return the list of dependencies. """
313 type, info='', noneAction='ignore', required=False) :
314 """declare the given option on the configuration block
316 This should only be called in the constructor of the
319 NOTE: The backend to option handling is slated to be replaced
320 at some point. This particular function should essentially
321 stay the same, but some behavior may change.
324 raise KeyError (f
'duplicate option: {name}')
325 if type
not in [str, bool, int, float, list,
None] :
326 raise TypeError (f
'unknown option type: {type}')
327 noneActions = [
'error',
'set',
'ignore']
328 if noneAction
not in noneActions :
329 raise ValueError (f
'invalid noneAction: {noneAction} [allowed values: {noneActions}]')
330 setattr (self, name, defaultValue)
332 noneAction=noneAction, required=required, default=defaultValue)
336 """set the given option on the configuration block
338 NOTE: The backend to option handling is slated to be replaced
339 at some point. This particular function should essentially
340 stay the same, but some behavior may change.
344 raise KeyError (f
'unknown option "{name}" in block "{self.__class__.__name__}"')
345 noneAction = self.
_options[name].noneAction
346 if value
is not None or noneAction ==
'set' :
350 if optType
is float
and type(value)
is int:
352 if optType
is not None and optType !=
type(value):
353 raise ValueError(f
'{name} for block {self.__class__.__name__} should '
354 f
'be of type {optType} not {type(value)}')
355 setattr (self, name, value)
356 elif noneAction ==
'ignore' :
358 elif noneAction ==
'error' :
359 raise ValueError (f
'passed None for setting option {name} with noneAction=error')
363 """Returns config option value, if present; otherwise return None"""
365 return getattr(self, name)
369 """Return a copy of the options associated with the block"""
375 Prints options and their values
377 def printWrap(text, width=60, indent=" "):
378 wrapper = textwrap.TextWrapper(width=width, initial_indent=indent,
379 subsequent_indent=indent)
380 for line
in wrapper.wrap(text=text):
381 logCPAlgCfgBlock.info(line)
385 logCPAlgCfgBlock.info(indent + f
"\033[4m{opt}\033[0m: {self.getOptionValue(opt)}")
386 logCPAlgCfgBlock.info(indent*2 + f
"\033[4mtype\033[0m: {vals.type}")
387 logCPAlgCfgBlock.info(indent*2 + f
"\033[4mdefault\033[0m: {vals.default}")
388 logCPAlgCfgBlock.info(indent*2 + f
"\033[4mrequired\033[0m: {vals.required}")
389 logCPAlgCfgBlock.info(indent*2 + f
"\033[4mnoneAction\033[0m: {vals.noneAction}")
390 printWrap(f
"\033[4minfo\033[0m: {vals.info}", indent=indent*2)
392 logCPAlgCfgBlock.info(indent + f
"{ opt}: {self.getOptionValue(opt)}")
396 """whether the configuration block has the given option
398 WARNING: The backend to option handling is slated to be
399 replaced at some point. This particular function may change
400 behavior, interface or be removed/replaced entirely.
407 Implementation of == operator. Used for seaching configSeque.
408 E.g. if blockName in configSeq:
420 return ConfigBlock.instance_counts.get(cls, 0)