Belle II Software development
pEventProcessor Class Reference

This class provides the core event processing loop for parallel processing. More...

#include <pEventProcessor.h>

Inheritance diagram for pEventProcessor:
EventProcessor

Public Member Functions

 pEventProcessor ()
 Constructor.
 
virtual ~pEventProcessor () override
 Destructor.
 
void process (const PathPtr &spath, long maxEvent)
 Processes the full module chain, starting with the first module in the given path.
 
void gotSigINT ()
 signal handler for Ctrl+C (async-safe)
 
void killRingBuffers ()
 signal handler (async-safe)
 
void cleanup ()
 clean up IPC resources (should only be called in one process).
 
void setProfileModuleName (const std::string &name)
 Set the name of the module we want to profile.
 

Static Public Member Functions

static void writeToStdErr (const char msg[])
 async-safe method to write something to STDERR.
 
static void installSignalHandler (int sig, void(*fn)(int))
 Install a signal handler 'fn' for given signal.
 
static void installMainSignalHandlers (void(*fn)(int)=nullptr)
 Install signal handler for INT, TERM and QUIT signals.
 

Protected Member Functions

void processInitialize (const ModulePtrList &modulePathList, bool setEventInfo=true)
 Initializes the modules.
 
void processCore (const PathPtr &startPath, const ModulePtrList &modulePathList, long maxEvent=0, bool isInputProcess=true)
 Processes the full module chain consisting of an arbitrary number of connected paths, starting with the first module in the specified path.
 
bool processEvent (PathIterator moduleIter, bool skipMasterModule)
 Calls event() functions on all modules for the current event.
 
void callEvent (Module *module)
 Calls event() on one single module, setting up logging and statistics as needed.
 
void processTerminate (const ModulePtrList &modulePathList)
 Terminates the modules.
 
void processBeginRun (bool skipDB=false)
 Calls the begin run methods of all modules.
 
void processEndRun ()
 Calls the end run methods of all modules.
 

Static Protected Member Functions

static long getMaximumEventNumber (long maxEvent)
 Calculate the maximum event number out of the argument from command line and the environment.
 

Protected Attributes

const Module * m_master
 The master module that determines the experiment/run/event number.
 
ModulePtrList m_moduleList
 List of all modules in order initialized.
 
std::string m_profileModuleName
 Name of the module which should be profiled, empty if no profiling is requested.
 
Module * m_profileModule = nullptr
 Address of the module which we want to profile, nullptr if no profiling is requested.
 
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
 EventMetaData is used by processEvent()/processCore().
 
EventMetaData m_previousEventMetaData
 Stores state of EventMetaData before it was last changed.
 
StoreObjPtr< EventExtraInfo > m_eventExtraInfo
 event extra info object pointer
 
StoreObjPtr< ProcessStatistics > m_processStatisticsPtr
 Also used in a number of places.
 
bool m_inRun
 Are we currently in a run?
 
double m_lastMetadataUpdate
 Time in seconds of last call for metadata update in event loop.
 
double m_metadataUpdateInterval
 Minimal time difference in seconds for metadata updates in event loop.
 
bool m_steerRootInputModuleOn = false
 True if the SteerRootInputModule is in charge for event processing.
 

Private Member Functions

void analyzePath (const PathPtr &path)
 Analyze given path.
 
void preparePaths ()
 Adds internal modules to paths, prepare RingBuffers.
 
RingBuffer * connectViaRingBuffer (const char *name, const PathPtr &a, PathPtr &b)
 Create RingBuffer with name from given environment variable, add Tx and Rx modules to a and b.
 
void dump_modules (const std::string &, const ModulePtrList &)
 Dump module names in the ModulePtrList.
 

Static Private Member Functions

static void clearFileList ()
 TFiles are stored in a global list and cleaned up by root since this will happen in all forked processes, these will be corrupted if we don't clean the list!
 
static ModulePtrList getModulesWithFlag (const ModulePtrList &modules, Module::EModulePropFlags flag)
 Return only modules which have the given Module flag set.
 
static ModulePtrList getModulesWithoutFlag (const ModulePtrList &modules, Module::EModulePropFlags flag)
 Return only modules which do not have the given Module flag set.
 
static void prependModulesIfNotPresent (ModulePtrList *modules, const ModulePtrList &prependModules)
 Prepend given 'prependModules' to 'modules', if they're not already present.
 

Private Attributes

std::unique_ptr< ProcHandler > m_procHandler
 handler to fork and manage processes.
 
PathPtr m_inputPath
 Input path.
 
PathPtr m_mainPath
 Main (parallel section) path.
 
PathPtr m_outputPath
 Output path.
 
RingBuffer * m_rbin = nullptr
 input RingBuffer
 
RingBuffer * m_rbout = nullptr
 output RingBuffer
 
ModulePtr m_histoman
 Pointer to HistoManagerModule, or nullptr if not found.
 

Detailed Description

This class provides the core event processing loop for parallel processing.

Definition at line 28 of file pEventProcessor.h.

Constructor & Destructor Documentation

◆ pEventProcessor()

Constructor.

Definition at line 63 of file pEventProcessor.cc.

64 m_histoman(nullptr)
65{
66 g_pEventProcessor = this;
67}
EventProcessor()
Constructor.
ModulePtr m_histoman
Pointer to HistoManagerModule, or nullptr if not found.

◆ ~pEventProcessor()

~pEventProcessor ( )
overridevirtual

Destructor.

Definition at line 70 of file pEventProcessor.cc.

71{
72 cleanup();
73 g_pEventProcessor = nullptr;
74}
void cleanup()
clean up IPC resources (should only be called in one process).

Member Function Documentation

◆ analyzePath()

void analyzePath ( const PathPtr & path)
private

Analyze given path.

Fills m_*path objects.

Definition at line 297 of file pEventProcessor.cc.

298{
299 //NOTE: PathUtils::splitPath() is a copy of this function, used by the ZMQ processor. The two
300 //share PathUtils::isWorthParallelising() but nothing else; keep them in sync.
301
302 PathPtr inpath(new Path);
303 PathPtr mainpath(new Path);
304 PathPtr outpath(new Path);
305
306 // only collected here, and added below once the splitting is done: the merge-back
307 // discards and restarts the main path, taking anything put on it along
308 ModulePtrList histogramManagers;
309
310 int stage = 0; //0: in, 1: event/main, 2: out
311 for (const ModulePtr& module : path->getModules()) {
312 bool hasParallelFlag = module->hasProperties(Module::c_ParallelProcessingCertified);
313 // entire conditional path must also be compatible
314 if (hasParallelFlag and module->hasCondition()) {
315 for (const auto& conditionPath : module->getAllConditionPaths()) {
317 hasParallelFlag = false;
318 }
319 }
320 }
321
322 // update stage?
323 if ((stage == 0 and hasParallelFlag) or (stage == 1 and !hasParallelFlag)) {
324 stage++;
325
326 if (stage == 2) {
327 if (not PathUtils::isWorthParallelising(*mainpath)) {
328 // merge mainpath back into input path
329 inpath->addPath(mainpath);
330 mainpath.reset(new Path);
331 // and search for further parallel sections
332 stage = 0;
333 }
334 }
335 }
336
337 if (stage == 0) { //fill input path
338 inpath->addModule(module);
339
340 if (module->hasProperties(Module::c_HistogramManager)) {
341 // Initialize histogram manager if found in the path
342 m_histoman = module;
343 histogramManagers.push_back(module);
344 }
345 }
346
347 if (stage == 1)
348 mainpath->addModule(module);
349 if (stage == 2)
350 outpath->addModule(module);
351 }
352
353 // the histogram manager has to run in every process
354 PathUtils::prependModules(mainpath, histogramManagers);
355 PathUtils::prependModules(outpath, histogramManagers);
356
357 bool createAllPaths = false; // usually we might not need e.g. an output path
358 for (const ModulePtr& module : path->getModules()) {
359 if (module->hasProperties(Module::c_TerminateInAllProcesses))
360 createAllPaths = true; // ensure there are all kinds of processes
361 }
362
363 // if main path is empty, createAllPaths doesn't really matter, since we'll fall back to single-core processing
364 if (!mainpath->isEmpty())
365 m_mainPath = mainpath;
366 if (createAllPaths or !inpath->isEmpty())
367 m_inputPath = inpath;
368 if (createAllPaths or !outpath->isEmpty())
369 m_outputPath = outpath;
370}
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).
@ c_HistogramManager
This module is used to manage histograms accumulated by other modules.
Definition Module.h:81
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition Module.h:80
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
Definition Module.h:83
static void prependModules(PathPtr &path, const ModulePtrList &modules)
Prepend the given modules to the path, keeping their order.
Definition PathUtils.cc:216
static bool isWorthParallelising(const Path &path)
Is a parallel section made up of these modules worth creating processes for?
Definition PathUtils.cc:16
PathPtr m_outputPath
Output path.
PathPtr m_mainPath
Main (parallel section) path.
PathPtr m_inputPath
Input path.
std::shared_ptr< Path > PathPtr
Defines a pointer to a path object as a boost shared pointer.
Definition Path.h:35
std::shared_ptr< Module > ModulePtr
Defines a pointer to a module object as a boost shared pointer.
Definition Module.h:43
std::list< ModulePtr > ModulePtrList
Defines a std::list of shared module pointers.
Definition Module.h:584

◆ callEvent()

void callEvent ( Module * module)
protectedinherited

Calls event() on one single module, setting up logging and statistics as needed.

Parameters
moduleModule to call the event() function

Definition at line 226 of file EventProcessor.cc.

227{
228 LogSystem& logSystem = LogSystem::Instance();
229 const bool collectStats = Environment::Instance().getStats();
230 // set up logging
231 logSystem.updateModule(&(module->getLogConfig()), module->getName());
232 // set up statistics is requested
233 if (collectStats) m_processStatisticsPtr->startModule();
234 // call module
235 CALL_MODULE(module, event);
236 // stop timing
237 if (collectStats) m_processStatisticsPtr->stopModule(module, ModuleStatistics::c_Event);
238 // reset logging
239 logSystem.updateModule(nullptr);
240};
bool getStats() const
Is collection of statistics during event processing enabled?
static Environment & Instance()
Static method to get a reference to the Environment instance.
StoreObjPtr< ProcessStatistics > m_processStatisticsPtr
Also used in a number of places.
void updateModule(const LogConfig *moduleLogConfig=nullptr, const std::string &moduleName="")
Sets the log configuration to the given module log configuration and sets the module name This method...
Definition LogSystem.h:200
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
Definition LogSystem.cc:28
@ c_Event
Counting time/calls in event()

◆ cleanup()

void cleanup ( )

clean up IPC resources (should only be called in one process).

Definition at line 76 of file pEventProcessor.cc.

77{
79 delete m_rbin;
80 m_rbin = nullptr;
81 delete m_rbout;
82 m_rbout = nullptr;
83 }
84}
static bool isOutputProcess()
Return true if the process is an output process.
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
RingBuffer * m_rbin
input RingBuffer
RingBuffer * m_rbout
output RingBuffer

◆ clearFileList()

void clearFileList ( )
staticprivate

TFiles are stored in a global list and cleaned up by root since this will happen in all forked processes, these will be corrupted if we don't clean the list!

needs to be called at the end of every process.

Definition at line 100 of file pEventProcessor.cc.

101{
102 //B2WARNING("list of files: " << gROOT->GetListOfFiles()->GetEntries());
103 //clear list, but don't actually delete the objects
104 gROOT->GetListOfFiles()->Clear("nodelete");
105}

◆ connectViaRingBuffer()

RingBuffer * connectViaRingBuffer ( const char * name,
const PathPtr & a,
PathPtr & b )
private

Create RingBuffer with name from given environment variable, add Tx and Rx modules to a and b.

Definition at line 372 of file pEventProcessor.cc.

373{
374 //create ringbuffers and add rx/tx where needed
375 const char* inrbname = getenv(name);
376 RingBuffer* rbuf;
377 if (inrbname == nullptr) {
378 rbuf = new RingBuffer();
379 } else {
380 string rbname(inrbname + to_string(0)); //currently at most one input, one output buffer
381 rbuf = new RingBuffer(rbname.c_str(), RingBuffer::c_DefaultSize);
382 }
383
384 // Insert Tx at the end of current path
385 ModulePtr txptr(new TxModule(rbuf));
386 a->addModule(txptr);
387 // Insert Rx at beginning of next path
388 ModulePtr rxptr(new RxModule(rbuf));
389 PathPtr newB(new Path());
390 newB->addModule(rxptr);
391 newB->addPath(b);
392 b.swap(newB);
393
394 return rbuf;
395}
static const int c_DefaultSize
Standard size of buffer, in integers (~60MB).
Definition RingBuffer.h:42

◆ dump_modules()

void dump_modules ( const std::string & title,
const ModulePtrList & modlist )
private

Dump module names in the ModulePtrList.

Definition at line 412 of file pEventProcessor.cc.

413{
414 ModulePtrList::const_iterator it;
415 std::ostringstream strbuf;
416 strbuf << title << " : ";
417 for (it = modlist.begin(); it != modlist.end(); ++it) {
418 const Module* module = it->get();
419 strbuf << module->getName();
420 if (module->hasCondition()) {
421 for (const auto& conditionPath : module->getAllConditionPaths()) {
422 strbuf << "[->" << conditionPath.get() << "] ";
423 }
424 }
425 if (*it != modlist.back())
426 strbuf << " -> ";
427 }
428 B2INFO(strbuf.str());
429}

◆ getMaximumEventNumber()

long getMaximumEventNumber ( long maxEvent)
staticprotectedinherited

Calculate the maximum event number out of the argument from command line and the environment.

Definition at line 113 of file EventProcessor.cc.

114{
115 //Check whether the number of events was set via command line argument
116 unsigned int numEventsArgument = Environment::Instance().getNumberEventsOverride();
117 if ((numEventsArgument > 0) && ((maxEvent == 0) || (maxEvent > numEventsArgument))) {
118 return numEventsArgument;
119 }
120 return maxEvent;
121}
unsigned int getNumberEventsOverride() const
Returns number of events in run 1 for EventInfoSetter module, or 0 for no override.
Definition Environment.h:68

◆ getModulesWithFlag()

ModulePtrList getModulesWithFlag ( const ModulePtrList & modules,
Module::EModulePropFlags flag )
staticprivate

Return only modules which have the given Module flag set.

Definition at line 432 of file pEventProcessor.cc.

433{
434 ModulePtrList tmpModuleList;
435 for (const ModulePtr& m : modules) {
436 if (m->hasProperties(flag))
437 tmpModuleList.push_back(m);
438 }
439
440 return tmpModuleList;
441}

◆ getModulesWithoutFlag()

ModulePtrList getModulesWithoutFlag ( const ModulePtrList & modules,
Module::EModulePropFlags flag )
staticprivate

Return only modules which do not have the given Module flag set.

Definition at line 442 of file pEventProcessor.cc.

443{
444 ModulePtrList tmpModuleList;
445 for (const ModulePtr& m : modules) {
446 if (!m->hasProperties(flag))
447 tmpModuleList.push_back(m);
448 }
449 return tmpModuleList;
450}

◆ gotSigINT()

void gotSigINT ( )

signal handler for Ctrl+C (async-safe)

When called the first time, does nothing (input process handles SIGINT by itself). On subsequent calls, RingBuffers are cleared, discarding any events that have been partly produced (mostly equivalent to previous behaviour on Ctrl+C)

Definition at line 87 of file pEventProcessor.cc.

88{
89 EventProcessor::writeToStdErr("\nStopping basf2...\n");
91}
static void writeToStdErr(const char msg[])
async-safe method to write something to STDERR.
void killRingBuffers()
signal handler (async-safe)

◆ installMainSignalHandlers()

void installMainSignalHandlers ( void(* fn )(int) = nullptr)
staticinherited

Install signal handler for INT, TERM and QUIT signals.

If argument is NULL, EventProcessor's own signal handler will be installed.

Definition at line 315 of file EventProcessor.cc.

316{
317 if (!fn)
318 fn = signalHandler;
319 installSignalHandler(SIGINT, fn);
320 installSignalHandler(SIGTERM, fn);
321 installSignalHandler(SIGQUIT, fn);
322}
static void installSignalHandler(int sig, void(*fn)(int))
Install a signal handler 'fn' for given signal.

◆ installSignalHandler()

void installSignalHandler ( int sig,
void(* fn )(int) )
staticinherited

Install a signal handler 'fn' for given signal.

Definition at line 300 of file EventProcessor.cc.

301{
302 struct sigaction s;
303 memset(&s, '\0', sizeof(s));
304
305 s.sa_handler = fn;
306 sigemptyset(&s.sa_mask);
307 if (sig == SIGCHLD)
308 s.sa_flags |= SA_NOCLDSTOP; //don't produce signal when children are stopped
309
310 if (sigaction(sig, &s, nullptr) != 0) {
311 B2FATAL("Cannot setup signal handler for signal " << sig);
312 }
313}

◆ killRingBuffers()

void killRingBuffers ( )

signal handler (async-safe)

Fairly abrupt termination after the current event.

Definition at line 93 of file pEventProcessor.cc.

94{
95 m_rbin->kill();
96 //these might be locked by _this_ process, so we cannot escape
97 m_rbout->kill(); //atomic, so doesn't lock
98}

◆ preparePaths()

void preparePaths ( )
private

Adds internal modules to paths, prepare RingBuffers.

Definition at line 397 of file pEventProcessor.cc.

398{
399 if (m_histoman) {
400 m_histoman->initialize();
401 }
402 if (not m_mainPath)
403 return; //we'll fall back to single-core
404
405 if (m_inputPath)
407 if (m_outputPath)
409}
RingBuffer * connectViaRingBuffer(const char *name, const PathPtr &a, PathPtr &b)
Create RingBuffer with name from given environment variable, add Tx and Rx modules to a and b.

◆ prependModulesIfNotPresent()

void prependModulesIfNotPresent ( ModulePtrList * modules,
const ModulePtrList & prependModules )
staticprivate

Prepend given 'prependModules' to 'modules', if they're not already present.

Definition at line 452 of file pEventProcessor.cc.

453{
454 for (const ModulePtr& m : prependModules) {
455 if (std::find(modules->begin(), modules->end(), m) == modules->end()) { //not present
456 modules->push_front(m);
457 }
458 }
459}

◆ process()

void process ( const PathPtr & spath,
long maxEvent )

Processes the full module chain, starting with the first module in the given path.

Processes all events for the given run number and for events from 0 to maxEvent.

Parameters
spathThe processing starts with the first module of this path.
maxEventThe maximum number of events that will be processed. If the number is smaller or equal 0, all events will be processed.

Definition at line 108 of file pEventProcessor.cc.

109{
110 if (spath->getModules().size() == 0) return;
111
112 const int numProcesses = Environment::Instance().getNumberProcesses();
113
114 //Check whether the number of events was set via command line argument
115 unsigned int numEventsArgument = Environment::Instance().getNumberEventsOverride();
116 if ((numEventsArgument > 0) && ((maxEvent == 0) || (maxEvent > numEventsArgument))) {
117 maxEvent = numEventsArgument;
118 }
119
120 if (numProcesses == 0)
121 B2FATAL("pEventProcessor::process() called for serial processing! Most likely a bug in Framework.");
122
123 // 1. Analyze start path and split into parallel paths
124 analyzePath(spath);
125
126
127 if (m_inputPath) {
128 B2INFO("Input Path " << m_inputPath->getPathString());
129 }
130 if (m_mainPath) {
131 if (m_mainPath->getModules().size() <= 5) {
132 B2INFO("Main Path " << m_mainPath->getPathString());
133 } else {
134 B2INFO("Main Path [" << m_mainPath->getModules().front()->getName() << " -> ... (" << m_mainPath->getModules().size() - 2 <<
135 " further modules) ... -> " << m_mainPath->getModules().back()->getName() << " ]");
136 }
137 }
138 if (m_outputPath) {
139 B2INFO("Output Path " << m_outputPath->getPathString());
140 }
141 if (not m_mainPath) {
142 B2WARNING("Cannot run any modules in parallel (no c_ParallelProcessingCertified flag), falling back to single-core mode.");
143 EventProcessor::process(spath, maxEvent);
144 return;
145 }
146
147 installMainSignalHandlers(cleanupAndStop);
148
149 //inserts Rx/Tx modules into path (sets up IPC structures)
150 preparePaths();
151
152 // ensure that we free the IPC resources!
153 atexit(cleanupIPC);
154
155
156 //init statistics
157 {
158 m_processStatisticsPtr.registerInDataStore();
160 m_processStatisticsPtr.create();
161 Path mergedPath;
162 if (m_inputPath)
163 mergedPath.addPath(m_inputPath);
164 mergedPath.addPath(m_mainPath);
165 if (m_outputPath)
166 mergedPath.addPath(m_outputPath);
167 for (const ModulePtr& module : mergedPath.buildModulePathList())
168 m_processStatisticsPtr->initModule(module.get());
169 }
170
171 // 2. Initialization
172 ModulePtrList modulelist = spath->buildModulePathList();
174 //dump_modules("Initializing globally: ", initGlobally);
175 processInitialize(initGlobally);
176
178
179 //Don't start processing in case of no master module
180 if (!m_master) {
181 B2ERROR("There is no module that provides event and run numbers. You must either add the EventInfoSetter module to your path, or, if using an input module, read EventMetaData objects from file.");
182 }
183
184 //Check if errors appeared. If yes, don't start the event processing.
186 if (numLogError != 0) {
187 B2FATAL(numLogError << " ERROR(S) occurred! The processing of events will not be started.");
188 }
189
190 //disable ROOT's management of TFiles
192
193
194 //install new signal handlers before forking
195 installMainSignalHandlers(parentSignalHandler);
196
197 //Path for current process
198 PathPtr localPath;
199
200 m_procHandler = std::make_unique<ProcHandler>(numProcesses);
201
202 // 3. Fork input path
203 m_procHandler->startInputProcess();
204 if (m_procHandler->isInputProcess()) { // In input process
205 localPath = m_inputPath;
206 } else {
207 // This is not the input path, clean up datastore to not contain the first event
209 }
210
211 if (localPath == nullptr) { //not forked yet
212 // 5. Fork out worker path (parallel section)
213 m_procHandler->startWorkerProcesses();
214 if (m_procHandler->isWorkerProcess()) {
215 localPath = m_mainPath;
216 m_master = localPath->getModules().begin()->get(); //set Rx as master
217 }
218 }
219
220 if (localPath == nullptr) { //not forked yet -> this process is the output process
221 m_procHandler->startOutputProcess();
222 if (m_outputPath) {
223 localPath = m_outputPath;
224 m_master = localPath->getModules().begin()->get(); //set Rx as master
225 }
226 }
227
228
229 if (!m_procHandler->isOutputProcess()) {
231 }
232
233 bool gotSigINT = false;
234 if (localPath != nullptr) {
235 ModulePtrList localModules = localPath->buildModulePathList();
236 ModulePtrList procinitmodules = getModulesWithFlag(localModules, Module::c_InternalSerializer);
237 //dump_modules("processInitialize for ", procinitmodules);
238 processInitialize(procinitmodules);
239
240 //input: handle signals normally, will slowly cascade down
241 if (m_procHandler->isInputProcess())
243 //workers will have to ignore the signals, there's no good way to do this safely
244 if (m_procHandler->isWorkerProcess())
246
247 try {
248 processCore(localPath, localModules, maxEvent, m_procHandler->isInputProcess());
249 } catch (StoppedBySignalException& e) {
250 if (e.signal != SIGINT) {
251 B2FATAL(e.what());
252 }
253 //in case of SIGINT, we move on to processTerminate() to shut down safely
254 gotSigINT = true;
255 }
256 prependModulesIfNotPresent(&localModules, terminateGlobally);
257 processTerminate(localModules);
258 }
259
260 B2INFO(m_procHandler->getProcessName() << " process finished.");
261
262 //all processes except output stop here
263 if (!m_procHandler->isOutputProcess()) {
264 if (gotSigINT) {
265 installSignalHandler(SIGINT, SIG_DFL);
266 raise(SIGINT);
267 } else {
268 exit(0);
269 }
270 }
271
272 //output process: do final cleanup
273 m_procHandler->waitForAllProcesses();
274 B2INFO("All processes completed");
275
276 //finished, disable handler again
277 installSignalHandler(SIGINT, SIG_IGN);
278
279 cleanup();
280 B2INFO("Global process: completed");
281
282 if (m_histoman) {
283 B2INFO("HistoManager:: adding histogram files");
285 }
286
287 //did anything bad happen?
288 if (gSignalReceived) {
289 B2ERROR("Processing aborted via signal " << gSignalReceived <<
290 ", terminating. Output files have been closed safely and should be readable.");
291 installSignalHandler(gSignalReceived, SIG_DFL);
292 raise(gSignalReceived);
293 }
294
295}
static void removeSideEffects()
call clear() and removeSideEffects() for all Mergeable objects in datastore (for c_Persistent durabil...
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition DataStore.h:59
static DataStore & Instance()
Instance of singleton Store.
Definition DataStore.cc:53
void invalidateData(EDurability durability)
Clears all registered StoreEntry objects of a specified durability, invalidating all objects.
Definition DataStore.cc:713
int getNumberProcesses() const
Returns the number of worker processes which should be used for the parallel processing.
void processInitialize(const ModulePtrList &modulePathList, bool setEventInfo=true)
Initializes the modules.
void processCore(const PathPtr &startPath, const ModulePtrList &modulePathList, long maxEvent=0, bool isInputProcess=true)
Processes the full module chain consisting of an arbitrary number of connected paths,...
void process(const PathPtr &startPath, long maxEvent=0)
Processes the full module chain, starting with the first module in the given path.
void processTerminate(const ModulePtrList &modulePathList)
Terminates the modules.
const Module * m_master
The master module that determines the experiment/run/event number.
static void installMainSignalHandlers(void(*fn)(int)=nullptr)
Install signal handler for INT, TERM and QUIT signals.
@ c_Error
Error: for things that went wrong and have to be fixed.
Definition LogConfig.h:30
int getMessageCounter(LogConfig::ELogLevel logLevel) const
Returns the number of logging calls per log level.
Definition LogSystem.cc:158
@ c_InternalSerializer
This module is an internal serializer/deserializer for parallel processing.
Definition Module.h:82
std::list< std::shared_ptr< Module > > buildModulePathList(bool unique=true) const
Builds a list of all modules which could be executed during the data processing.
Definition Path.cc:67
void addPath(const PathPtr &path)
See 'pydoc3 basf2.Path'.
Definition Path.cc:37
std::list< std::shared_ptr< Module > > getModules() const override
Returns a list of the modules in this path.
Definition Path.cc:50
static RbTupleManager & Instance()
Access to singleton.
Definition RbTuple.cc:38
int hadd(bool deleteflag=true)
Functions to add up all histogram files.
Definition RbTuple.cc:136
void preparePaths()
Adds internal modules to paths, prepare RingBuffers.
static ModulePtrList getModulesWithFlag(const ModulePtrList &modules, Module::EModulePropFlags flag)
Return only modules which have the given Module flag set.
std::unique_ptr< ProcHandler > m_procHandler
handler to fork and manage processes.
void analyzePath(const PathPtr &path)
Analyze given path.
void gotSigINT()
signal handler for Ctrl+C (async-safe)
static void prependModulesIfNotPresent(ModulePtrList *modules, const ModulePtrList &prependModules)
Prepend given 'prependModules' to 'modules', if they're not already present.
static ModulePtrList getModulesWithoutFlag(const ModulePtrList &modules, Module::EModulePropFlags flag)
Return only modules which do not have the given Module flag set.
static void clearFileList()
TFiles are stored in a global list and cleaned up by root since this will happen in all forked proces...

◆ processBeginRun()

void processBeginRun ( bool skipDB = false)
protectedinherited

Calls the begin run methods of all modules.

Loops over all module instances specified in a list and calls their beginRun() method. Please note: the beginRun() method of the module which triggered the beginRun() loop will also be called.

Definition at line 470 of file EventProcessor.cc.

471{
472 MetadataService::Instance().addBasf2Status("beginning run");
473
474 m_inRun = true;
476
477 LogSystem& logSystem = LogSystem::Instance();
478 m_processStatisticsPtr->startGlobal();
479
480 if (!skipDB) DBStore::Instance().update();
481
482 //initialize random generator for end run
484
485 for (const ModulePtr& modPtr : m_moduleList) {
486 Module* module = modPtr.get();
487
488 //Set the module dependent log level
489 logSystem.updateModule(&(module->getLogConfig()), module->getName());
490
491 //Do beginRun() call
492 m_processStatisticsPtr->startModule();
493 CALL_MODULE(module, beginRun);
495
496 //Set the global log level
497 logSystem.updateModule(nullptr);
498 }
499
501}
bool m_inRun
Are we currently in a run?
ModulePtrList m_moduleList
List of all modules in order initialized.
void addBasf2Status(const std::string &message="")
Add metadata of basf2 status.
static MetadataService & Instance()
Static method to get a reference to the MetadataService instance.
@ c_BeginRun
Counting time/calls in beginRun()
static void initializeBeginRun()
Initialize run independent random generator for begin run.
static Database & Instance()
Instance of a singleton Database.
Definition Database.cc:42
static DBStore & Instance()
Instance of a singleton DBStore.
Definition DBStore.cc:26
void update()
Updates all objects that are outside their interval of validity.
Definition DBStore.cc:77
ScopeGuard createScopedUpdateSession()
Make sure we have efficient http pipelinging during initialize/beginRun but don't keep session alive ...
Definition Database.cc:62

◆ processCore()

void processCore ( const PathPtr & startPath,
const ModulePtrList & modulePathList,
long maxEvent = 0,
bool isInputProcess = true )
protectedinherited

Processes the full module chain consisting of an arbitrary number of connected paths, starting with the first module in the specified path.

Parameters
startPathThe processing starts with the first module of this path.
modulePathListA list of all modules which could be executed during the data processing (used for calling the beginRun() and endRun() method).
maxEventThe maximum number of events that will be processed. If the number is smaller or equal 0, all events are processed.
isInputProcesstrue when this is either the only or the input process

Definition at line 409 of file EventProcessor.cc.

410{
412 m_moduleList = modulePathList;
413 //Remember the previous event meta data, and identify end of data meta data
414 m_previousEventMetaData.setEndOfData(); //invalid start state
415
416 const bool collectStats = Environment::Instance().getStats();
417
418 //Loop over the events
419 long currEvent = 0;
420 bool endProcess = false;
421 while (!endProcess) {
422 if (collectStats)
423 m_processStatisticsPtr->startGlobal();
424
425 PathIterator moduleIter(startPath);
426 endProcess = processEvent(moduleIter, isInputProcess && currEvent == 0);
427
428 //Delete event related data in DataStore
430
431 currEvent++;
432 if ((maxEvent > 0) && (currEvent >= maxEvent)) endProcess = true;
433 if (collectStats)
435 } //end event loop
436
437 //End last run
438 m_eventMetaDataPtr.create();
440}
void setInitializeActive(bool active)
Setter for m_initializeActive.
Definition DataStore.cc:93
void processEndRun()
Calls the end run methods of all modules.
bool processEvent(PathIterator moduleIter, bool skipMasterModule)
Calls event() functions on all modules for the current event.
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
EventMetaData is used by processEvent()/processCore().
EventMetaData m_previousEventMetaData
Stores state of EventMetaData before it was last changed.

◆ processEndRun()

void processEndRun ( )
protectedinherited

Calls the end run methods of all modules.

Loops over all module instances specified in a list and calls their endRun() method. Please note: the endRun() method of the module which triggered the endRun() loop will also be called.

Definition at line 504 of file EventProcessor.cc.

505{
507
508 if (!m_inRun)
509 return;
510 m_inRun = false;
511
512 LogSystem& logSystem = LogSystem::Instance();
513 m_processStatisticsPtr->startGlobal();
514
515 const EventMetaData newEventMetaData = *m_eventMetaDataPtr;
517
518 //initialize random generator for end run
520
521 for (const ModulePtr& modPtr : m_moduleList) {
522 Module* module = modPtr.get();
523
524 //Set the module dependent log level
525 logSystem.updateModule(&(module->getLogConfig()), module->getName());
526
527 //Do endRun() call
528 m_processStatisticsPtr->startModule();
529 CALL_MODULE(module, endRun);
531
532 //Set the global log level
533 logSystem.updateModule(nullptr);
534 }
535 *m_eventMetaDataPtr = newEventMetaData;
536
538}
@ c_EndRun
Counting time/calls in endRun()
static void initializeEndRun()
Initialize run independent random generator for end run.

◆ processEvent()

bool processEvent ( PathIterator moduleIter,
bool skipMasterModule )
protectedinherited

Calls event() functions on all modules for the current event.

Used by processCore.

Parameters
moduleIteriterator of the path containing all the modules
skipMasterModuleskip the execution of the master module, presumably because this is the first event and it's already been done in initialize()
Returns
true if execution should stop.

Definition at line 324 of file EventProcessor.cc.

325{
326 double time = Utils::getClock() / Unit::s;
328 MetadataService::Instance().addBasf2Status("running event loop");
330 }
331
332 const bool collectStats = Environment::Instance().getStats();
333
334 while (!moduleIter.isDone()) {
335 Module* module = moduleIter.get();
336
337 // run the module ... unless we don't want to
338 if (!(skipMasterModule && module == m_master)) {
339 callEvent(module);
340 }
341
342 //Check for end of data
343 if ((m_eventMetaDataPtr && (m_eventMetaDataPtr->isEndOfData())) ||
344 ((module == m_master) && !m_eventMetaDataPtr)) {
345 if ((module != m_master) && !m_steerRootInputModuleOn) {
346 B2WARNING("Event processing stopped by module '" << module->getName() <<
347 "', which is not in control of event processing (does not provide EventMetaData)");
348 }
349 return true;
350 }
351
352 //Handle EventMetaData changes by master module
353 if (module == m_master) {
354
355 //initialize random number state for the event
357
358 //Check for a change of the run
359 if ((m_eventMetaDataPtr->getExperiment() != m_previousEventMetaData.getExperiment()) ||
360 (m_eventMetaDataPtr->getRun() != m_previousEventMetaData.getRun())) {
361
362 if (collectStats)
363 m_processStatisticsPtr->suspendGlobal();
364
366 processBeginRun(skipMasterModule);
367
368 if (collectStats)
369 m_processStatisticsPtr->resumeGlobal();
370 }
371
373
374 //make sure we use the event dependent generator again
376
378
379 } else {
380 //Check for a second master module. Cannot do this if we skipped the
381 //master module as the EventMetaData is probably set before we call this
382 //function
383 if (!skipMasterModule && m_eventMetaDataPtr &&
385 B2FATAL("Two modules setting EventMetaData were discovered: " << m_master->getName() << " and " << module->getName());
386 }
387 }
388
389 if (gSignalReceived != 0) {
390 throw StoppedBySignalException(gSignalReceived);
391 }
392
393 //Check for the module conditions, evaluate them and if one is true switch to the new path
394 if (module->evalCondition()) {
395 PathPtr condPath = module->getConditionPath();
396 //continue with parent Path after condition path is executed?
397 if (module->getAfterConditionPath() == Module::EAfterConditionPath::c_Continue) {
398 moduleIter = PathIterator(condPath, moduleIter);
399 } else {
400 moduleIter = PathIterator(condPath);
401 }
402 } else {
403 moduleIter.next();
404 }
405 } //end module loop
406 return false;
407}
Exception thrown when execution is stopped by a signal.
void processBeginRun(bool skipDB=false)
Calls the begin run methods of all modules.
double m_lastMetadataUpdate
Time in seconds of last call for metadata update in event loop.
void callEvent(Module *module)
Calls event() on one single module, setting up logging and statistics as needed.
bool m_steerRootInputModuleOn
True if the SteerRootInputModule is in charge for event processing.
double m_metadataUpdateInterval
Minimal time difference in seconds for metadata updates in event loop.
void next()
increment.
bool isDone() const
Are we finished iterating?
Module * get() const
dereference.
static void useEventDependent()
Set Event dependent Random Generator as current one.
static void initializeEvent(bool force=false)
Initialize event information.
static const double s
[second]
Definition Unit.h:95
void updateEvent()
Updates all intra-run dependent objects.
Definition DBStore.cc:140
double getClock()
Return current value of the real-time clock.
Definition Utils.cc:75

◆ processInitialize()

void processInitialize ( const ModulePtrList & modulePathList,
bool setEventInfo = true )
protectedinherited

Initializes the modules.

Loops over all module instances specified in a list and calls their initialize() method.

Parameters
modulePathListA list of all modules which could be executed during the data processing.
setEventInfoif true the first event call of the master module will be called immediately to load the event info right away so that it's available for subsequent modules

Definition at line 242 of file EventProcessor.cc.

243{
244 LogSystem& logSystem = LogSystem::Instance();
246
247 m_processStatisticsPtr.registerInDataStore();
248 //TODO I might want to overwrite it in initialize (e.g. if read from file)
249 // For parallel processing or subevents, I don't want that, though.
250 // Maybe make this a function argument?
252 m_processStatisticsPtr.create();
253 m_processStatisticsPtr->startGlobal();
254
256
257 // EventExtraInfo is needed in several modules so register it here
258 m_eventExtraInfo.registerInDataStore();
259
260 for (const ModulePtr& modPtr : modulePathList) {
261 Module* module = modPtr.get();
262
263 if (module->hasUnsetForcedParams()) {
264 //error message was printed by module
265 continue;
266 }
267
268 //Set the module dependent log level
269 logSystem.updateModule(&(module->getLogConfig()), module->getName());
271
272 //Do initialization
273 m_processStatisticsPtr->initModule(module);
274 m_processStatisticsPtr->startModule();
275 CALL_MODULE(module, initialize);
277
278 //Set the global log level
279 logSystem.updateModule(nullptr);
280
281 //Check whether this is the master module
282 if (!m_master && DataStore::Instance().getEntry(m_eventMetaDataPtr) != nullptr) {
283 B2DEBUG(100, "Found module providing EventMetaData: " << module->getName());
284 m_master = module;
285 if (setEventInfo) {
286 callEvent(module);
287 // update Database payloads: we now have valid event meta data unless
288 // we don't process any events
290 }
291 }
292
293 if (gSignalReceived != 0) {
294 throw StoppedBySignalException(gSignalReceived);
295 }
296 }
298}
DependencyMap & getDependencyMap()
Return map of dependencies between modules.
Definition DataStore.h:524
void setModule(const Module &mod)
Set the current module (for getCurrentModuleInfo())
StoreObjPtr< EventExtraInfo > m_eventExtraInfo
event extra info object pointer
@ c_Init
Counting time/calls in initialize()

◆ processTerminate()

void processTerminate ( const ModulePtrList & modulePathList)
protectedinherited

Terminates the modules.

Loops over all module instances in reverse order specified in a list and calls their terminate() method.

Parameters
modulePathListA list of all modules which could be executed during the data processing.

Definition at line 443 of file EventProcessor.cc.

444{
446
447 LogSystem& logSystem = LogSystem::Instance();
448 ModulePtrList::const_reverse_iterator listIter;
449 m_processStatisticsPtr->startGlobal();
450
451 for (listIter = modulePathList.rbegin(); listIter != modulePathList.rend(); ++listIter) {
452 Module* module = listIter->get();
453
454 //Set the module dependent log level
455 logSystem.updateModule(&(module->getLogConfig()), module->getName());
456
457 //Do termination
458 m_processStatisticsPtr->startModule();
459 CALL_MODULE(module, terminate);
461
462 //Set the global log level
463 logSystem.updateModule(nullptr);
464 }
465
467}
@ c_Term
Counting time/calls in terminate()

◆ setProfileModuleName()

void setProfileModuleName ( const std::string & name)
inlineinherited

Set the name of the module we want to profile.

Parameters
nameName of the module as returned by getName()

Definition at line 58 of file EventProcessor.h.

58{ m_profileModuleName = name; }

◆ writeToStdErr()

void writeToStdErr ( const char msg[])
staticinherited

async-safe method to write something to STDERR.

Definition at line 72 of file EventProcessor.cc.

73{
74 //signal handlers are called asynchronously, making many standard functions (including output) dangerous
75 //write() is, however, safe, so we'll use that to write to stderr.
76
77 //strlen() not explicitly in safe list, but doesn't have any error handling routines that might alter global state
78 const int len = strlen(msg);
79
80 int rc = write(STDERR_FILENO, msg, len);
81 (void) rc; //ignore return value (there's nothing we can do about a failed write)
82
83}

Member Data Documentation

◆ m_eventExtraInfo

StoreObjPtr<EventExtraInfo> m_eventExtraInfo
protectedinherited

event extra info object pointer

Definition at line 170 of file EventProcessor.h.

◆ m_eventMetaDataPtr

StoreObjPtr<EventMetaData> m_eventMetaDataPtr
protectedinherited

EventMetaData is used by processEvent()/processCore().

Definition at line 164 of file EventProcessor.h.

◆ m_histoman

ModulePtr m_histoman
private

Pointer to HistoManagerModule, or nullptr if not found.

Definition at line 109 of file pEventProcessor.h.

◆ m_inputPath

PathPtr m_inputPath
private

Input path.

Definition at line 97 of file pEventProcessor.h.

◆ m_inRun

bool m_inRun
protectedinherited

Are we currently in a run?

If yes, processEndRun() needs to do something.

Definition at line 176 of file EventProcessor.h.

◆ m_lastMetadataUpdate

double m_lastMetadataUpdate
protectedinherited

Time in seconds of last call for metadata update in event loop.

Definition at line 179 of file EventProcessor.h.

◆ m_mainPath

PathPtr m_mainPath
private

Main (parallel section) path.

Definition at line 99 of file pEventProcessor.h.

◆ m_master

const Module* m_master
protectedinherited

The master module that determines the experiment/run/event number.

Definition at line 154 of file EventProcessor.h.

◆ m_metadataUpdateInterval

double m_metadataUpdateInterval
protectedinherited

Minimal time difference in seconds for metadata updates in event loop.

Definition at line 182 of file EventProcessor.h.

◆ m_moduleList

ModulePtrList m_moduleList
protectedinherited

List of all modules in order initialized.

Definition at line 155 of file EventProcessor.h.

◆ m_outputPath

PathPtr m_outputPath
private

Output path.

Definition at line 101 of file pEventProcessor.h.

◆ m_previousEventMetaData

EventMetaData m_previousEventMetaData
protectedinherited

Stores state of EventMetaData before it was last changed.

Useful since processEndRun() needs info about which run it needs to end.

Definition at line 167 of file EventProcessor.h.

◆ m_processStatisticsPtr

StoreObjPtr<ProcessStatistics> m_processStatisticsPtr
protectedinherited

Also used in a number of places.

Definition at line 173 of file EventProcessor.h.

◆ m_procHandler

std::unique_ptr<ProcHandler> m_procHandler
private

handler to fork and manage processes.

Definition at line 94 of file pEventProcessor.h.

◆ m_profileModule

Module* m_profileModule = nullptr
protectedinherited

Address of the module which we want to profile, nullptr if no profiling is requested.

Definition at line 161 of file EventProcessor.h.

◆ m_profileModuleName

std::string m_profileModuleName
protectedinherited

Name of the module which should be profiled, empty if no profiling is requested.

Definition at line 158 of file EventProcessor.h.

◆ m_rbin

RingBuffer* m_rbin = nullptr
private

input RingBuffer

Definition at line 104 of file pEventProcessor.h.

◆ m_rbout

RingBuffer* m_rbout = nullptr
private

output RingBuffer

Definition at line 106 of file pEventProcessor.h.

◆ m_steerRootInputModuleOn

bool m_steerRootInputModuleOn = false
protectedinherited

True if the SteerRootInputModule is in charge for event processing.

Definition at line 185 of file EventProcessor.h.


The documentation for this class was generated from the following files: