 |
Belle II Software
release-05-02-19
|
11 #include <framework/core/SubEventModule.h>
13 #include <framework/core/ModuleManager.h>
14 #include <framework/core/ProcessStatistics.h>
15 #include <framework/pcore/ProcHandler.h>
16 #include <framework/datastore/DataStore.h>
17 #include <framework/datastore/StoreObjPtr.h>
33 setDescription(R
"DOC(Internal module to handle Path.for_each() and Path.do_while().
36 Don't add this module directly with `Path.add_module` or
37 `basf2.register_module` but use `Path.for_each()` and `Path.do_while()`
39 This module shouldn't appear in ``basf2 -m`` output.
40 If it does, check REG_MODULE() handling.)DOC");
42 addParam("mode", m_mode,
"SubEvent mode: 0 = for_each, 1 = do_while", m_mode);
43 addParam(
"loopOver", m_loopOverName,
"Name of array to iterate over.", m_loopOverName);
44 addParam(
"objectName", m_objectName,
"Name of the object holding the current iteration's item.", m_objectName);
45 addParam(
"path", m_path,
"Path to execute for each iteration.",
PathPtr(
nullptr));
46 addParam(
"maxIterations", m_maxIterations,
"Maximum iterations in case of do_while", m_maxIterations);
47 addParam(
"loopCondition", m_loopConditionString,
"Loop condition in case of do_while", m_loopConditionString);
50 SubEventModule::~SubEventModule() =
default;
57 setName(
"for_each(" + objectName +
" : " + loopOver +
")");
75 B2FATAL(
"No Loop conditions specified");
97 B2FATAL(
"Modules in a Path.do_while() cannot have any conditions");
103 B2FATAL(
"do_while(): Modules in a loop cannot have conditions which end processing");
111 B2FATAL(
"do_while(): Cannot loop over empty path");
119 B2FATAL(
"do_while(): The last module in the loop path (" <<
137 for (
const auto& entry : orig) {
139 auto& destEntry = dest[entry.first];
140 auto& srcEntry = entry.second;
141 if (srcEntry.ptr ==
nullptr)
142 destEntry.ptr =
nullptr;
150 B2FATAL(
"SubEvent module not initialised properly.");
153 if (
m_mode == c_DoWhile) {
155 }
else if (
m_mode == c_ForEach) {
157 B2FATAL(
"loopOver and objectName parameters not setup");
160 B2FATAL(
"Invalid SubEvent mode: " <<
m_mode);
164 processStatistics->suspendGlobal();
166 if (
m_mode == c_ForEach) {
177 processStatistics->startModule();
178 processStatistics->resumeGlobal();
184 processStatistics->suspendGlobal();
198 tmpModuleList.push_back(m);
203 if (
m_mode == c_ForEach) {
204 restoreContents(persistentMapCopy, persistentMap);
208 processStatistics->startModule();
209 processStatistics->resumeGlobal();
217 processStatistics->suspendGlobal();
221 processStatistics->startModule();
222 processStatistics->resumeGlobal();
227 processStatistics->suspendGlobal();
231 processStatistics->startModule();
232 processStatistics->resumeGlobal();
248 if (
m_mode == c_ForEach) {
257 TObject* prevObject = objectEntry.
object;
258 TObject* prevPtr = objectEntry.
ptr;
261 for (
int i = 0; i < numEntries; i++) {
271 restoreContents(eventMapCopy, eventMap);
275 objectEntry.
object = prevObject;
276 objectEntry.
ptr = prevPtr;
278 int returnValue{
false};
279 unsigned int curIteration{0};
294 processStatistics->startModule();
TObject * object
The pointer to the actual object.
std::unique_ptr< ModuleCondition > m_loopCondition
Condition object to evaluate if the loop is finished in case of doWhile()
void processTerminate(const ModulePtrList &modulePathList)
Terminates the modules.
unsigned int m_maxIterations
maximum number of iterations before giving up in case of doWhile()
int m_mode
Mode for this module.
static DataStore & Instance()
Instance of singleton Store.
boost::optional< std::string > m_objectName
name of our loop variable in case of forEach.
std::map< std::string, StoreEntry > StoreEntryMap
Map for StoreEntries.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Module * m_loopConditionModule
pointer to the module to provide the returnValue in case of doWhile()
bool hasCondition() const
Returns true if at least one condition was set for the module.
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
boost::optional< std::string > m_loopOverName
name for m_loopOver in case of forEach.
@ c_End
End current event after the conditional path.
Wraps a stored array/object, stored under unique (name, durability) key.
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
virtual void endRun() override
This method is called if the current run ends.
boost::optional< std::string > m_loopConditionString
String for the condition when looping.
bool processEvent(PathIterator moduleIter, bool skipMasterModule)
Calls event() functions on all modules for the current event.
StoreEntryMap & getStoreEntryMap(EDurability durability)
Get a reference to the object/array map.
static bool allModulesHaveFlag(const std::list< std::shared_ptr< Module >> &list, unsigned int flag)
Returns true if and only if all modules in list have the given flag (or list is empty).
bool hasReturnValue() const
Return true if this module has a valid return value set.
int getReturnValue() const
Return the return value set by this module.
virtual void terminate() override
This method is called at the end of the event processing.
virtual void initialize() override
Initialize the Module.
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
void initSubLoop(std::shared_ptr< Path > path, const std::string &condition, unsigned int maxIterations)
ised by Path::doWhile() to actually set parameters
virtual void event() override
This method is the core of the module.
void processBeginRun(bool skipDB=false)
Calls the begin run methods of all modules.
void initSubEvent(const std::string &objectName, const std::string &loopOver, std::shared_ptr< Path > path)
used by Path::forEach() to actually set parameters.
Abstract base class for different kinds of events.
Type-safe access to single objects in the data store.
std::list< ModulePtr > ModulePtrList
Defines a std::list of shared module pointers.
std::shared_ptr< Path > PathPtr
Defines a pointer to a path object as a boost shared pointer.
std::shared_ptr< Module > ModulePtr
Defines a pointer to a module object as a boost shared pointer.
ModulePtrList m_moduleList
List of all modules in order initialized.
const std::vector< ModuleCondition > & getAllConditions() const
Return all set conditions for this module.
Module * get() const
dereference.
void setDoWhileConditions()
Set the necessary pointers for do_while(): the pointer to the module whose return value we'll use as ...
static int EvtProcID()
Return ID of the current process.
virtual void beginRun() override
Called when entering a new run.
bool registerEntry(const std::string &name, EDurability durability, TClass *objClass, bool array, EStoreFlags storeFlags)
Register an entry in the DataStore map.
void processEndRun()
Calls the end run methods of all modules.
int m_processID
when using multi-processing contains the ID of the process where event() is called (in that process o...
void processInitialize(const ModulePtrList &modulePathList, bool setEventInfo=true)
Initializes the modules.
StoreArray< TObject > m_loopOver
array looped over in case of forEach
@ c_Persistent
Object is available during entire execution time.
TObject * ptr
The pointer to the returned object, either equal to 'object' or null, depending on wether the object ...
void setProperties()
Set properties for this module based on the modules found in m_path.
std::shared_ptr< Path > m_path
Path to execute.
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
TClass * objClass
class of object.
Iterator over a Path (returning Module pointers).
const Module * m_master
The master module that determines the experiment/run/event number.
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
EventMetaData m_previousEventMetaData
Stores state of EventMetaData before it was last changed.
const std::string & getName() const
Returns the name of the module.
bool isDone() const
Are we finished iterating?
void setName(const std::string &name)
Set the name of the module.
int getEntries() const
Get the number of objects in the array.
provides the core event processing loop.