4 """This file contains python modules that generally decorate other modules and paths
5 to serve a slightly changed purpose or circumstance.
8 A great deal of the these modules are quite general purpose helper constructs,
9 which might be helpful in other parts of the BASF2, and might therefore be better
10 placed in the framework folder.
15 from ROOT
import Belle2
22 from ROOT
import Belle2
26 return logging.getLogger(__name__)
31 """Wrapping module base class that wraps a single module to slightly change its behaviour.
33 The base implementation does nothing, but forward the relevant methods to the wrapped module.
36 Implements the decorator/wrapper pattern.
39 module (basf2.Module): The wrapped module instance.
43 """Create a new wrapper module around the given module. This base implementation does not change anything,
44 so there is no reason to use this base implementation alone."""
45 super(WrapperModule, self).
__init__()
52 self.set_debug_level(self.
module.logging.debug_level)
53 self.set_abort_level(self.
module.logging.abort_level)
55 if self.
module.logging.log_level != basf2.LogLevel.default:
56 self.set_log_level(self.
module.logging.log_level)
57 self.set_log_info(self.
module.logging.log_level,
58 self.
module.logging.get_info(self.
module.logging.log_level))
65 """Name of the wrapper class."""
66 return self.__class__.__name__
70 """Forwards the parameters"""
75 """Forwards the avaiilable parameters"""
76 return self.
module.available_params
79 """Compose a name that indicates the wrapped module."""
80 return "{wrapper_name}({module_name})".format(module_name=module.name(),
84 """Forwards the name()."""
88 """Initialize method of the module"""
92 """Begin run method of the module"""
96 """Event method of the module"""
100 """End run method of the module"""
104 """Terminate method of the module"""
110 """Wrapper module that evaluates the computational performance of python modules.
115 module (basf2.Module): The wrapped module that should be profiled.
116 Should be a module written in Python, since the profile interacts
117 with the interpretor for the measurements, but cannot look into c++ implementations.
119 output_file_name (str, optional): Path to the file where the profiling information
120 shall be stored. Defaults to profile.txt.
122 profiler (cProfile.Profile): Profiler instance to manage and extract the profiling statistics.
126 default_output_file_name =
"profile.txt"
129 """Create a new PyProfilingModule wrapped around the given module
130 which outputs its results to the output_file_name of given (if not, to profile.txt)."""
131 super(PyProfilingModule, self).
__init__(module)
136 if output_file_name
is not None:
140 """Initialize method of the module"""
146 """Event method of the module"""
149 super(PyProfilingModule, self).
event()
153 """Terminate method of the module"""
154 super(PyProfilingModule, self).
terminate()
155 sortby =
'cumulative'
157 profile_stats = pstats.Stats(self.
profiler, stream=profile_output_file)
158 profile_stats.sort_stats(sortby)
159 profile_stats.print_stats()
164 """Wrapper module to conditionally execute module and continue with the normal path afterwards.
166 There are two ways to specify the condition.
167 One way is to override the condtion(self) method in a subclass.
168 The second way is to give a function as the second argument to the module constructor,
169 which is called each event.
172 module (basf2.Module): The module executed, if the condition is met.
174 condition (function() -> bool, optional): Function executed at each event to determine,
175 if the given module shall be executed.
176 If None call the condition method instead, which can be overridden by subclasses.
181 """Initialisation method taking the module instance to be wrapped.
184 module (basf2.Module): The module executed, if the condition is met.
185 condition (function() -> bool, optional): Function executed at each event to determine,
186 if the given module shall be executed.
187 If None call the condition method instead, which can be overridden by subclasses.
190 super(IfModule, self).
__init__(module)
191 if condition
is not None:
197 """Condition method called if not given a condition function during construction.
199 Can be overridden by a concrete subclass to specify
200 under which condition the wrapped module should be executed.
201 It can also be shadowed by a condition function given to the constructor of this module.
204 bool: The indication if the wrapped module should be executed.
205 Always True in the base implementation
210 """Event method of the module
212 Evaluates the condition and sets the return value of this module
213 to trigger the execution of the wrapped module.
217 super(IfModule, self).
event()
220 def is_storearray_present(storearray_name,
221 storearray_durability=0):
222 """Checks if a StoreArray with name and durability is present in the DataStore.
224 Only works during the event processing phase, but not on initialisation,
225 due to limitation of the python interface to the framework
228 return storearray_name
in storearray_list
233 """Conditional execution of the wrapped module if a StoreArray is present.
236 storearray_name (str): The name of the StoreArray which presence has to be checked.
237 storearray_durability (int): The durability of the StoreArray
238 storearray_is_present (bool): The flag whether the StoreArray is present.
239 Set during initialisation.
245 storearray_durability=0):
248 module (basf2.Module): The module executed, if the condition is met.
249 storearray_name (str): The name of the StoreArray which presence has to be checked.
250 storearray_durability (int, optional): The durability of the StoreArray. Default 0.
253 super(IfStoreArrayPresentModule, self).
__init__(module)
265 """Initialize the contianed module (only of the condition is true)."""
270 """Returns true if the StoreArray is present.
272 Checks presence of the StoreArray once and remembers the result for all following events.
282 """Conditional execution of the wrapped module based if a StoreArray is not present."""
285 """Returns false if the StoreArray is present.
287 Checks presence of the StoreArray once and remembers the result for all following events.
289 return not IfStoreArrayPresentModule.condition(self)
294 """Conditional execution of the wrapped module based on the presence of Monte Carlo information.
300 module (basf2.Module): The module executed, if the condition is met.
302 super(IfMCParticlesPresentModule, self).
__init__(module,
"MCParticles")
307 """Wrapper for a BASF2 path into a module such that
308 it can be passed around and added to a BASF2 path as a BASF2 module.
310 The wrapper is implement in such a way that it unconditionally executes
311 the contained path by means of a positive return value.
312 The calling path is continued after the execution of the wrapped path.
315 _path (basf2.Path): The wrapped execution path.
319 """Initialises the module with a path to be wrapped.
321 May also give a list of modules that should be appended to the path.
322 If the path is omitted a new basf2.Path is constructed.
325 path (basf2.Path): The execution path to be wrapped.
326 If no path is given create a new one.
328 modules (iterable of basf2.Module): Module to be added to the path.
334 path = basf2.create_path()
340 for module
in modules:
341 path.add_module(module)
343 self.if_true(path, basf2.AfterConditionPath.CONTINUE)
349 itemCount = len(modules)
350 self.set_name(
"{path_module} ({items} modules):".format(path_module=self.__class__.__name__,
354 """Event method of the module
356 Sets the return value of this module to true and triggers the execution of the wrapped path.
359 self.return_value(
True)