5 from functools
import wraps
8 from AnaAlgorithm.Logging
import logging
9 logCPAlgCfgBlock = logging.getLogger(
'CPAlgCfgBlock')
11 from AnalysisAlgorithmsConfig.ConfigAccumulator
import DataType, ExpertModeWarning
15 """check whether the sample being run passes a"""
16 """possible DSID filter on the block"""
17 if len(filterList) == 0:
19 for dsid_filter
in filterList:
21 if any(char
in str(dsid_filter)
for char
in "^$*+?.()|[]{}\\"):
22 pattern = re.compile(dsid_filter)
23 if pattern.match(
str(config.dsid())):
27 if str(dsid_filter) ==
str(config.dsid()):
32 """this wrapper ensures that the 'instanceName' of the various """
33 """config blocks is cleaned up of any non-alphanumeric characters """
34 """that may arise from using 'selectionName' in the naming."""
36 def wrapper(*args, **kwargs):
38 orig_name = func(*args, **kwargs)
50 """this meta class enforces the application of 'alphanumeric_block_names()' """
51 """to 'instanceName()' and will be used in the main ConfigBlock class in order """
52 """to propagate this rule also to all derived classes (the individual config blocks."""
55 if 'instanceName' in dct
and callable(dct[
'instanceName']):
57 return super().
__new__(cls, name, bases, dct)
60 """the information for a single option on a configuration block"""
62 def __init__ (self, type=None, info='', noneAction='ignore', required=False,
73 """Class encoding a blocks dependence on other blocks."""
89 return f
'ConfigBlockDependency(blockName="{self.blockName}", required={self.required})'
93 """the base class for classes implementing individual blocks of
96 A configuration block is a sequence of one or more algorithms that
97 should always be scheduled together, e.g. the muon four momentum
98 corrections could be a single block, muon selection could then be
99 another block. The blocks themselves generally have their own
100 configuration options/properties specific to the block, and will
101 perform a dynamic configuration based on those options as well as
104 The actual configuration of the algorithms in the block will
105 depend on what other blocks are scheduled before and afterwards,
106 most importantly some algorithms will introduce shallow copies
107 that subsequent algorithms will need to run on, and some
108 algorithms will add selection decorations that subquent algorithms
109 should use as preselections.
111 The algorithms get created in a multi-step process (that may be
112 extended in the future): As a first step each block retrieves
113 references to the containers it uses (essentially marking its spot
114 in the processing chain) and also registering any shallow copies
115 that will be made. In the second/last step each block then
116 creates the fully configured algorithms.
118 One goal is that when the algorithms get created they will have
119 their final configuration and there needs to be no
120 meta-configuration data attached to the algorithms, essentially an
121 inversion of the approach in AnaAlgSequence in which the
122 algorithms got created first with associated meta-configuration
123 and then get modified in susequent configuration steps.
125 For now this is mostly an empty base class, but another goal of
126 this approach is to make it easier to build another configuration
127 layer on top of this one, and this class will likely be extended
128 and get data members at that point.
130 The child class needs to implement the method `makeAlgs` which is
131 given a single `ConfigAccumulator` type argument. This is meant to
132 create the sequence of algorithms that this block configures. This
133 is currently (28 Jul 2025) called twice and should do the same thing
134 during both calls, but the plan is to change that to a single call.
136 The child class should also implement the method `getInstanceName`
137 which should return a string that is used to distinguish between
138 multiple instances of the same block. This is used to append the
139 instance name to the names of all algorithms created by this block,
140 and may in the future also be used to distinguish between multiple
141 instances of the block.
153 self.
addOption(
'groupName',
'', type=str,
154 info=(
'Used to specify this block when setting an'
155 ' option at an arbitrary location.'))
156 self.
addOption(
'skipOnData',
False, type=bool,
157 info=(
'User option to prevent the block from running'
158 ' on data. This only affects blocks that are'
159 ' intended to run on data.'))
160 self.
addOption(
'skipOnMC',
False, type=bool,
161 info=(
'User option to prevent the block from running'
162 ' on MC. This only affects blocks that are'
163 ' intended to run on MC.'))
164 self.
addOption(
'onlyForDSIDs', [], type=list,
165 info=(
'Used to specify which MC DSIDs to allow this'
166 ' block to run on. Each element of the list'
167 ' can be a full DSID (e.g. 410470), or a regex'
168 ' (e.g. 410.* to select all 410xxx DSIDs, or'
169 ' ^(?!410) to veto them). An empty list means no'
170 ' DSID restriction.'))
171 self.
addOption(
'propertyOverrides', {}, type=
None,
172 info=(
'EXPERT USE ONLY: A dictionary of properties to'
173 ' override at the end of configuration. This should'
175 ' {"algName.toolName.propertyName": value, ...},'
176 ' without any automatically applied postfixes for'
177 ' the algorithm name. THIS IS MEANT TO BE EXPERT'
178 ' USAGE ONLY. Properties that need to be set by'
179 ' the user should be declared as options on the'
180 ' block itself. EXPERT USE ONLY!'),
184 if cls
not in ConfigBlock.instance_counts:
185 ConfigBlock.instance_counts[cls] = 0
188 stack = inspect.stack()
189 for frame_info
in stack:
191 parent_cls = frame_info.frame.f_locals.get(
'self',
None)
192 if parent_cls
is None or not isinstance(parent_cls, ConfigBlock):
194 if frame_info.function ==
"makeConfig":
195 ConfigBlock.instance_counts[cls] += 1
208 """get the factory name for this block
210 This is mostly to give a reliable means of identifying the type
211 of block we have in error messages. This is meant to be
212 automatically set by the factory based on the requested block
213 name, but there are a number of fallbacks. It is best not to
214 assume a specific format, this is mostly meant to be used as an
215 identifier in output messages.
223 return self.__class__.__name__
226 """set the factory name for this block
228 This is meant to be called automatically by the factory based on
229 the requested block name. If you are creating a block without a factory,
230 you can call this method to set the factory name manually.
235 """Get the name of the instance
237 The name of the instance is used to distinguish between multiple
238 instances of the same block. Most importantly, this will be
239 appended to the names of all algorithms created by this block.
240 This defaults to an empty string, but block implementations
241 should override it with an appropriate name based on identifying
242 options set on this instance. A typical example would be the
243 name of the (main) container, plus potentially the selection or
246 Ideally all blocks should override this method, but for backward
247 compatibility (28 Jul 25) it defaults to an empty string.
253 whether this block should be used for the given configuration
255 This is used by `ConfigSequence` to determine whether this block
256 should be included in the configuration.
258 if self.skipOnData
and config.dataType()
is DataType.Data:
260 if self.skipOnMC
and config.dataType()
is not DataType.Data:
268 Apply any configuration overrides specified in the block's
269 `propertyOverrides` option. This is meant to be called at the
270 end of the configuration process, after all algorithms have been
271 created and configured.
273 for key, value
in self.propertyOverrides.
items():
275 parts = key.split(
'.')
277 raise Exception(f
"Invalid override key format: {key}")
278 alg = config.getAlgorithm(parts[0])
280 raise Exception(f
"Algorithm {parts[0]} not found in config for override: {key}")
281 for name
in parts[1:-1]:
283 if hasattr(alg, name):
284 alg = getattr(alg, name)
286 raise Exception(f
"Tool {name} not found for override: {key}")
289 alg.__setattr__(parts[-1], value)
293 Add a dependency for the block. Dependency is corresponds to the
294 blockName of another block. If required is True, will throw an
295 error if dependency is not present; otherwise will move this
296 block after the required block. If required is False, will do
297 nothing if required block is not present; otherwise, it will
298 move block after required block.
302 self.
addOption(
'ignoreDependencies', [], type=list,
303 info=
'List of dependencies defined in the ConfigBlock to ignore.')
307 """Return True if there is a dependency."""
311 """Return the list of dependencies. """
315 type, info='', noneAction='ignore', required=False, expertMode=None) :
316 """declare the given option on the configuration block
318 This should only be called in the constructor of the
321 NOTE: The backend to option handling is slated to be replaced
322 at some point. This particular function should essentially
323 stay the same, but some behavior may change.
326 raise KeyError (f
'duplicate option: {name}')
327 if type
not in [str, bool, int, float, list,
None] :
328 raise TypeError (f
'unknown option type: {type}')
329 noneActions = [
'error',
'set',
'ignore']
330 if noneAction
not in noneActions :
331 raise ValueError (f
'invalid noneAction: {noneAction} [allowed values: {noneActions}]')
334 if expertMode
is not None:
335 if expertMode
is True:
338 elif not isinstance(expertMode, list):
339 raise TypeError (f
'expertMode must be a list, got {type(expertMode)}')
344 setattr (self, name, defaultValue)
346 noneAction=noneAction, required=required, default=defaultValue)
350 """set the given option on the configuration block
352 NOTE: The backend to option handling is slated to be replaced
353 at some point. This particular function should essentially
354 stay the same, but some behavior may change.
358 raise KeyError (f
'unknown option "{name}" in block "{self.__class__.__name__}"')
359 noneAction = self.
_options[name].noneAction
360 if value
is not None or noneAction ==
'set' :
364 if optType
is float
and type(value)
is int:
366 if optType
is not None and optType !=
type(value):
367 raise ValueError(f
'{name} for block {self.__class__.__name__} should '
368 f
'be of type {optType} not {type(value)}')
369 setattr (self, name, value)
370 elif noneAction ==
'ignore' :
372 elif noneAction ==
'error' :
373 raise ValueError (f
'passed None for setting option {name} with noneAction=error')
377 """Returns config option value, if present; otherwise return None"""
379 return getattr(self, name)
383 """Return a copy of the options associated with the block"""
389 Prints options and their values
391 def printWrap(text, width=60, indent=" "):
392 wrapper = textwrap.TextWrapper(width=width, initial_indent=indent,
393 subsequent_indent=indent)
394 for line
in wrapper.wrap(text=text):
395 logCPAlgCfgBlock.info(line)
399 logCPAlgCfgBlock.info(indent + f
"\033[4m{opt}\033[0m: {self.getOptionValue(opt)}")
400 logCPAlgCfgBlock.info(indent*2 + f
"\033[4mtype\033[0m: {vals.type}")
401 logCPAlgCfgBlock.info(indent*2 + f
"\033[4mdefault\033[0m: {vals.default}")
402 logCPAlgCfgBlock.info(indent*2 + f
"\033[4mrequired\033[0m: {vals.required}")
403 logCPAlgCfgBlock.info(indent*2 + f
"\033[4mnoneAction\033[0m: {vals.noneAction}")
404 printWrap(f
"\033[4minfo\033[0m: {vals.info}", indent=indent*2)
406 logCPAlgCfgBlock.info(indent + f
"{ opt}: {self.getOptionValue(opt)}")
410 """whether the configuration block has the given option
412 WARNING: The backend to option handling is slated to be
413 replaced at some point. This particular function may change
414 behavior, interface or be removed/replaced entirely.
421 Implementation of == operator. Used for seaching configSeque.
422 E.g. if blockName in configSeq:
434 return ConfigBlock.instance_counts.get(cls, 0)
438 Check whether value matches an expert mode rule.
440 - A literal (compared with ==)
441 - A callable predicate (called with value)
442 - A special marker string (common callable)
447 if isinstance(rule, str):
448 if rule ==
"nonemptystring":
449 return isinstance(value, str)
and value !=
""
450 if rule ==
"nonemptylist":
451 return isinstance(value, list)
and value != []
452 if rule ==
"positiveint":
453 return isinstance(value, int)
and value > 0
460 Check if any settings require expert mode and validate accordingly.
461 If any setting is set to a value that requires expert mode but we're
462 not in expert mode, raise an error.
466 default_value = self.
_options[option_name].default
468 if expert_rule
is True:
470 if current_value != default_value:
472 f
"Block '{self.factoryName()}' option '{option_name}' "
473 f
"set to '{current_value}' (default '{default_value}'), "
474 f
"requires expert mode.",
475 ExpertModeWarning, stacklevel=2
479 for ev
in expert_rule:
482 f
"Block '{self.factoryName()}' option '{option_name}' "
483 f
"set to expert-only value '{current_value}'. "
484 f
"Requires expert mode.",
485 ExpertModeWarning, stacklevel=2