9#include <framework/pcore/pEventProcessor.h>
10#include <framework/pcore/ProcHandler.h>
11#include <framework/pcore/RingBuffer.h>
12#include <framework/pcore/RxModule.h>
13#include <framework/pcore/TxModule.h>
14#include <framework/pcore/DataStoreStreamer.h>
15#include <framework/pcore/RbTuple.h>
16#include <framework/pcore/PathUtils.h>
18#include <framework/core/ModuleManager.h>
19#include <framework/core/Environment.h>
20#include <framework/logging/LogSystem.h>
32 static int gSignalReceived = 0;
38 if (g_pEventProcessor)
41 void cleanupAndStop(
int sig)
50 static void parentSignalHandler(
int signal)
53 if (signal == SIGINT) {
55 }
else if (signal == SIGTERM or signal == SIGQUIT) {
58 if (gSignalReceived == 0)
59 gSignalReceived = signal;
66 g_pEventProcessor =
this;
73 g_pEventProcessor =
nullptr;
104 gROOT->GetListOfFiles()->Clear(
"nodelete");
116 if ((numEventsArgument > 0) && ((maxEvent == 0) || (maxEvent > numEventsArgument))) {
117 maxEvent = numEventsArgument;
120 if (numProcesses == 0)
121 B2FATAL(
"pEventProcessor::process() called for serial processing! Most likely a bug in Framework.");
128 B2INFO(
"Input Path " <<
m_inputPath->getPathString());
132 B2INFO(
"Main Path " <<
m_mainPath->getPathString());
134 B2INFO(
"Main Path [" <<
m_mainPath->getModules().front()->getName() <<
" -> ... (" <<
m_mainPath->getModules().size() - 2 <<
135 " further modules) ... -> " <<
m_mainPath->getModules().back()->getName() <<
" ]");
139 B2INFO(
"Output Path " <<
m_outputPath->getPathString());
142 B2WARNING(
"Cannot run any modules in parallel (no c_ParallelProcessingCertified flag), falling back to single-core mode.");
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.");
186 if (numLogError != 0) {
187 B2FATAL(numLogError <<
" ERROR(S) occurred! The processing of events will not be started.");
211 if (localPath ==
nullptr) {
220 if (localPath ==
nullptr) {
234 if (localPath !=
nullptr) {
250 if (e.signal != SIGINT) {
260 B2INFO(
m_procHandler->getProcessName() <<
" process finished.");
274 B2INFO(
"All processes completed");
280 B2INFO(
"Global process: completed");
283 B2INFO(
"HistoManager:: adding histogram files");
288 if (gSignalReceived) {
289 B2ERROR(
"Processing aborted via signal " << gSignalReceived <<
290 ", terminating. Output files have been closed safely and should be readable.");
292 raise(gSignalReceived);
311 for (
const ModulePtr& module : path->getModules()) {
312 bool hasParallelFlag =
module->hasProperties(Module::c_ParallelProcessingCertified);
314 if (hasParallelFlag and module->hasCondition()) {
315 for (
const auto& conditionPath : module->getAllConditionPaths()) {
317 hasParallelFlag =
false;
323 if ((stage == 0 and hasParallelFlag) or (stage == 1 and !hasParallelFlag)) {
330 mainpath.reset(
new Path);
343 histogramManagers.push_back(module);
350 outpath->addModule(module);
357 bool createAllPaths =
false;
358 for (
const ModulePtr& module : path->getModules()) {
360 createAllPaths =
true;
366 if (createAllPaths or !inpath->
isEmpty())
368 if (createAllPaths or !outpath->isEmpty())
375 const char* inrbname = getenv(name);
377 if (inrbname ==
nullptr) {
380 string rbname(inrbname + to_string(0));
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() <<
"] ";
425 if (*it != modlist.back())
428 B2INFO(strbuf.str());
436 if (m->hasProperties(flag))
437 tmpModuleList.push_back(m);
440 return tmpModuleList;
446 if (!m->hasProperties(flag))
447 tmpModuleList.push_back(m);
449 return tmpModuleList;
454 for (
const ModulePtr& m : prependModules) {
455 if (std::find(modules->begin(), modules->end(), m) == modules->end()) {
456 modules->push_front(m);
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...
static DataStore & Instance()
Instance of singleton Store.
void invalidateData(EDurability durability)
Clears all registered StoreEntry objects of a specified durability, invalidating all objects.
int getNumberProcesses() const
Returns the number of worker processes which should be used for the parallel processing.
static Environment & Instance()
Static method to get a reference to the Environment instance.
unsigned int getNumberEventsOverride() const
Returns number of events in run 1 for EventInfoSetter module, or 0 for no override.
Exception thrown when execution is stopped by a signal.
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,...
static void installSignalHandler(int sig, void(*fn)(int))
Install a signal handler 'fn' for given signal.
StoreObjPtr< ProcessStatistics > m_processStatisticsPtr
Also used in a number of places.
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.
static void writeToStdErr(const char msg[])
async-safe method to write something to STDERR.
const Module * m_master
The master module that determines the experiment/run/event number.
EventProcessor()
Constructor.
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.
int getMessageCounter(LogConfig::ELogLevel logLevel) const
Returns the number of logging calls per log level.
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
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).
EModulePropFlags
Each module can be tagged with property flags, which indicate certain features of the module.
@ c_HistogramManager
This module is used to manage histograms accumulated by other modules.
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
@ c_InternalSerializer
This module is an internal serializer/deserializer for parallel processing.
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
static void prependModules(PathPtr &path, const ModulePtrList &modules)
Prepend the given modules to the path, keeping their order.
static bool isWorthParallelising(const Path &path)
Is a parallel section made up of these modules worth creating processes for?
Implements a path consisting of Module and/or Path objects.
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.
void addPath(const PathPtr &path)
See 'pydoc3 basf2.Path'.
void addModule(const std::shared_ptr< Module > &module)
Adds a module to the path.
bool isEmpty() const
Returns true if this Path doesn't contain any elements.
std::list< std::shared_ptr< Module > > getModules() const override
Returns a list of the modules in this path.
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.
static RbTupleManager & Instance()
Access to singleton.
int hadd(bool deleteflag=true)
Functions to add up all histogram files.
Class to manage a Ring Buffer placed in an IPC shared memory.
static const int c_DefaultSize
Standard size of buffer, in integers (~60MB).
Module to decode data store contents from RingBuffer.
Module for encoding data store contents into a RingBuffer.
This class provides the core event processing loop for parallel processing.
void preparePaths()
Adds internal modules to paths, prepare RingBuffers.
void dump_modules(const std::string &, const ModulePtrList &)
Dump module names in the ModulePtrList.
PathPtr m_outputPath
Output path.
static ModulePtrList getModulesWithFlag(const ModulePtrList &modules, Module::EModulePropFlags flag)
Return only modules which have the given Module flag set.
RingBuffer * m_rbin
input RingBuffer
RingBuffer * m_rbout
output RingBuffer
std::unique_ptr< ProcHandler > m_procHandler
handler to fork and manage processes.
void cleanup()
clean up IPC resources (should only be called in one process).
void process(const PathPtr &spath, long maxEvent)
Processes the full module chain, starting with the first module in the given path.
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.
pEventProcessor()
Constructor.
ModulePtr m_histoman
Pointer to HistoManagerModule, or nullptr if not found.
void analyzePath(const PathPtr &path)
Analyze given path.
virtual ~pEventProcessor() override
Destructor.
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.
PathPtr m_mainPath
Main (parallel section) path.
static ModulePtrList getModulesWithoutFlag(const ModulePtrList &modules, Module::EModulePropFlags flag)
Return only modules which do not have the given Module flag set.
PathPtr m_inputPath
Input path.
void killRingBuffers()
signal handler (async-safe)
static void clearFileList()
TFiles are stored in a global list and cleaned up by root since this will happen in all forked proces...
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.
Abstract base class for different kinds of events.