12 """This file contains python modules that generally decorate other modules and paths
13 to serve a slightly changed purpose or circumstance.
16 A great deal of the these modules are quite general purpose helper constructs,
17 which might be helpful in other parts of the basf2, and might therefore be better
18 placed in the framework folder.
22 from ROOT
import Belle2
31 return logging.getLogger(__name__)
36 """Wrapping module base class that wraps a single module to slightly change its behaviour.
38 The base implementation does nothing, but forward the relevant methods to the wrapped module.
41 Implements the decorator/wrapper pattern.
44 module (basf2.Module): The wrapped module instance.
48 """Create a new wrapper module around the given module. This base implementation does not change anything,
49 so there is no reason to use this base implementation alone."""
50 super(WrapperModule, self).
__init__()
57 self.set_debug_level(self.
modulemodule.logging.debug_level)
58 self.set_abort_level(self.
modulemodule.logging.abort_level)
60 if self.
modulemodule.logging.log_level != basf2.LogLevel.default:
61 self.set_log_level(self.
modulemodule.logging.log_level)
62 self.set_log_info(self.
modulemodule.logging.log_level,
63 self.
modulemodule.logging.get_info(self.
modulemodule.logging.log_level))
70 """Name of the wrapper class."""
71 return self.__class__.__name__
75 """Forwards the parameters"""
76 return self.
modulemodule.param
80 """Forwards the avaiilable parameters"""
81 return self.
modulemodule.available_params
84 """Compose a name that indicates the wrapped module."""
85 return "{wrapper_name}({module_name})".format(module_name=module.name(),
89 """Forwards the name()."""
93 """Initialize method of the module"""
97 """Begin run method of the module"""
101 """Event method of the module"""
105 """End run method of the module"""
109 """Terminate method of the module"""
115 """Wrapper module that evaluates the computational performance of python modules.
120 module (basf2.Module): The wrapped module that should be profiled.
121 Should be a module written in Python, since the profile interacts
122 with the interpretor for the measurements, but cannot look into c++ implementations.
124 output_file_name (str, optional): Path to the file where the profiling information
125 shall be stored. Defaults to profile.txt.
127 profiler (cProfile.Profile): Profiler instance to manage and extract the profiling statistics.
131 default_output_file_name =
"profile.txt"
134 """Create a new PyProfilingModule wrapped around the given module
135 which outputs its results to the output_file_name of given (if not, to profile.txt)."""
136 super(PyProfilingModule, self).
__init__(module)
141 if output_file_name
is not None:
145 """Initialize method of the module"""
151 """Event method of the module"""
154 super(PyProfilingModule, self).
event()
158 """Terminate method of the module"""
159 super(PyProfilingModule, self).
terminate()
160 sortby =
'cumulative'
161 with open(self.
output_file_nameoutput_file_name,
'w')
as profile_output_file:
162 profile_stats = pstats.Stats(self.
profilerprofiler, stream=profile_output_file)
163 profile_stats.sort_stats(sortby)
164 profile_stats.print_stats()
169 """Wrapper module to conditionally execute module and continue with the normal path afterwards.
171 There are two ways to specify the condition.
172 One way is to override the condtion(self) method in a subclass.
173 The second way is to give a function as the second argument to the module constructor,
174 which is called each event.
177 module (basf2.Module): The module executed, if the condition is met.
179 condition (function() -> bool, optional): Function executed at each event to determine,
180 if the given module shall be executed.
181 If None call the condition method instead, which can be overridden by subclasses.
186 """Initialisation method taking the module instance to be wrapped.
189 module (basf2.Module): The module executed, if the condition is met.
190 condition (function() -> bool, optional): Function executed at each event to determine,
191 if the given module shall be executed.
192 If None call the condition method instead, which can be overridden by subclasses.
195 super(IfModule, self).
__init__(module)
196 if condition
is not None:
202 """Condition method called if not given a condition function during construction.
204 Can be overridden by a concrete subclass to specify
205 under which condition the wrapped module should be executed.
206 It can also be shadowed by a condition function given to the constructor of this module.
209 bool: The indication if the wrapped module should be executed.
210 Always True in the base implementation
215 """Event method of the module
217 Evaluates the condition and sets the return value of this module
218 to trigger the execution of the wrapped module.
222 super(IfModule, self).
event()
225 def is_storearray_present(storearray_name,
226 storearray_durability=0):
227 """Checks if a StoreArray with name and durability is present in the DataStore.
229 Only works during the event processing phase, but not on initialisation,
230 due to limitation of the python interface to the framework
233 return storearray_name
in storearray_list
238 """Conditional execution of the wrapped module if a StoreArray is present.
241 storearray_name (str): The name of the StoreArray which presence has to be checked.
242 storearray_durability (int): The durability of the StoreArray
243 storearray_is_present (bool): The flag whether the StoreArray is present.
244 Set during initialisation.
250 storearray_durability=0):
253 module (basf2.Module): The module executed, if the condition is met.
254 storearray_name (str): The name of the StoreArray which presence has to be checked.
255 storearray_durability (int, optional): The durability of the StoreArray. Default 0.
258 super(IfStoreArrayPresentModule, self).
__init__(module)
270 """Initialize the contianed module (only of the condition is true)."""
275 """Returns true if the StoreArray is present.
277 Checks presence of the StoreArray once and remembers the result for all following events.
287 """Conditional execution of the wrapped module based if a StoreArray is not present."""
290 """Returns false if the StoreArray is present.
292 Checks presence of the StoreArray once and remembers the result for all following events.
294 return not IfStoreArrayPresentModule.condition(self)
299 """Conditional execution of the wrapped module based on the presence of Monte Carlo information.
305 module (basf2.Module): The module executed, if the condition is met.
307 super(IfMCParticlesPresentModule, self).
__init__(module,
"MCParticles")
312 """Wrapper for a basf2 path into a module such that
313 it can be passed around and added to a basf2 path as a basf2 module.
315 The wrapper is implement in such a way that it unconditionally executes
316 the contained path by means of a positive return value.
317 The calling path is continued after the execution of the wrapped path.
320 _path (basf2.Path): The wrapped execution path.
324 """Initialises the module with a path to be wrapped.
326 May also give a list of modules that should be appended to the path.
327 If the path is omitted a new basf2.Path is constructed.
330 path (basf2.Path): The execution path to be wrapped.
331 If no path is given create a new one.
333 modules (iterable of basf2.Module): Module to be added to the path.
339 path = basf2.create_path()
345 for module
in modules:
346 path.add_module(module)
348 self.if_true(path, basf2.AfterConditionPath.CONTINUE)
354 itemCount = len(modules)
355 self.set_name(
"{path_module} ({items} modules):".format(path_module=self.__class__.__name__,
359 """Event method of the module
361 Sets the return value of this module to true and triggers the execution of the wrapped path.
364 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)