11"""This file contains python modules that generally decorate other modules and paths
12to serve a slightly changed purpose or circumstance.
15 A great deal of the these modules are quite general purpose helper constructs,
16 which might be helpful in other parts of the basf2,
and might therefore be better
17 placed
in the framework folder.
21from ROOT import Belle2
30 return logging.getLogger(__name__)
35 """Wrapping module base class that wraps a single module to slightly change its behaviour.
37 The base implementation does nothing, but forward the relevant methods to the wrapped module.
40 Implements the decorator/wrapper pattern.
43 module (basf2.Module): The wrapped module instance.
47 """Create a new wrapper module around the given module. This base implementation does not change anything,
48 so there is no reason to use this base implementation alone.
"""
56 self.set_debug_level(self.
module.logging.debug_level)
57 self.set_abort_level(self.
module.logging.abort_level)
59 if self.
module.logging.log_level != basf2.LogLevel.default:
60 self.set_log_level(self.
module.logging.log_level)
61 self.set_log_info(self.
module.logging.log_level,
62 self.
module.logging.get_info(self.
module.logging.log_level))
69 """Name of the wrapper class."""
70 return self.__class__.__name__
74 """Forwards the parameters"""
79 """Forwards the available parameters"""
80 return self.
module.available_params
83 """Compose a name that indicates the wrapped module."""
84 return f
"{self.wrapper_name}({module.name()})"
87 """Forwards the name()."""
91 """Initialize method of the module"""
95 """Begin run method of the module"""
99 """Event method of the module"""
103 """End run method of the module"""
107 """Terminate method of the module"""
113 """Wrapper module that evaluates the computational performance of python modules.
118 module (basf2.Module): The wrapped module that should be profiled.
119 Should be a module written in Python, since the profile interacts
120 with the interpreter
for the measurements, but cannot look into c++ implementations.
122 output_file_name (str, optional): Path to the file where the profiling information
123 shall be stored. Defaults to profile.txt.
125 profiler (cProfile.Profile): Profiler instance to manage
and extract the profiling statistics.
129 default_output_file_name = "profile.txt"
132 """Create a new PyProfilingModule wrapped around the given module
133 which outputs its results to the output_file_name of given (if not, to profile.txt).
"""
139 if output_file_name
is not None:
143 """Initialize method of the module"""
149 """Event method of the module"""
156 """Terminate method of the module"""
158 sortby =
'cumulative'
160 profile_stats = pstats.Stats(self.
profiler, stream=profile_output_file)
161 profile_stats.sort_stats(sortby)
162 profile_stats.print_stats()
167 """Wrapper module to conditionally execute module and continue with the normal path afterwards.
169 There are two ways to specify the condition.
170 One way is to override the
condition(self) method
in a subclass.
171 The second way
is to give a function
as the second argument to the module constructor,
172 which
is called each event.
175 module (basf2.Module): The module executed,
if the condition
is met.
177 condition (function() -> bool, optional): Function executed at each event to determine,
178 if the given module shall be executed.
179 If
None call the condition method instead, which can be overridden by subclasses.
184 """Initialisation method taking the module instance to be wrapped.
187 module (basf2.Module): The module executed, if the condition
is met.
188 condition (function() -> bool, optional): Function executed at each event to determine,
189 if the given module shall be executed.
190 If
None call the condition method instead, which can be overridden by subclasses.
194 if condition
is not None:
200 """Condition method called if not given a condition function during construction.
202 Can be overridden by a concrete subclass to specify
203 under which condition the wrapped module should be executed.
204 It can also be shadowed by a condition function given to the constructor of this module.
207 bool: The indication if the wrapped module should be executed.
208 Always
True in the base implementation
213 """Event method of the module
215 Evaluates the condition and sets the
return value of this module
216 to trigger the execution of the wrapped module.
223def is_storearray_present(storearray_name,
224 storearray_durability=0):
225 """Checks if a StoreArray with name and durability is present in the DataStore.
227 Only works during the event processing phase, but not on initialisation,
228 due to limitation of the python interface to the framework
231 return storearray_name
in storearray_list
236 """Conditional execution of the wrapped module if a StoreArray is present.
239 storearray_name (str): The name of the StoreArray which presence has to be checked.
240 storearray_durability (int): The durability of the StoreArray
241 storearray_is_present (bool): The flag whether the StoreArray is present.
242 Set during initialisation.
248 storearray_durability=0):
251 module (basf2.Module): The module executed, if the condition
is met.
252 storearray_name (str): The name of the StoreArray which presence has to be checked.
253 storearray_durability (int, optional): The durability of the StoreArray. Default 0.
268 """Initialize the contained module (only if the condition is true)."""
273 """Returns true if the StoreArray is present.
275 Checks presence of the StoreArray once and remembers the result
for all following events.
285 """Conditional execution of the wrapped module based if a StoreArray is not present."""
288 """Returns false if the StoreArray is present.
290 Checks presence of the StoreArray once and remembers the result
for all following events.
292 return not IfStoreArrayPresentModule.condition(self)
297 """Conditional execution of the wrapped module based on the presence of Monte Carlo information.
303 module (basf2.Module): The module executed, if the condition
is met.
305 super().__init__(module, "MCParticles")
310 """Wrapper for a basf2 path into a module such that
311 it can be passed around and added to a basf2 path
as a basf2 module.
313 The wrapper
is implement
in such a way that it unconditionally executes
314 the contained path by means of a positive
return value.
315 The calling path
is continued after the execution of the wrapped path.
318 _path (basf2.Path): The wrapped execution path.
322 """Initialises the module with a path to be wrapped.
324 May also give a list of modules that should be appended to the path.
325 If the path is omitted a new basf2.Path
is constructed.
328 path (basf2.Path): The execution path to be wrapped.
329 If no path
is given create a new one.
331 modules (iterable of basf2.Module): Module to be added to the path.
337 path = basf2.create_path()
343 for module
in modules:
344 path.add_module(module)
346 self.if_true(path, basf2.AfterConditionPath.CONTINUE)
352 itemCount = len(modules)
353 self.set_name(f
"{self.__class__.__name__} ({itemCount} modules):")
356 """Event method of the module
358 Sets the return value of this module to true
and triggers the execution of the wrapped path.
361 self.return_value(True)
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available arrays for given durability.
def __init__(self, module)