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