Belle II Software development
Module.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <framework/core/ModuleParamList.h>
12#include <framework/core/ModuleParamList.templateDetails.h>
13#include <framework/core/ModuleCondition.h>
14#include <framework/core/PathElement.h>
15
16#include <framework/logging/LogConfig.h>
17
18#include <list>
19#include <string>
20#include <memory>
21
22namespace boost {
23 namespace python {
24 class list;
25 class dict;
26 namespace api {
27 class object;
28 }
29 using api::object;
30 }
31}
32
33namespace Belle2 {
39 class Module;
40 class Path;
41
43 typedef std::shared_ptr<Module> ModulePtr;
44
72 class Module : public PathElement {
73
74 public:
75
78 c_Input = 1,
85 };
86
89
99 Module();
100
109 virtual void initialize() {};
110
133 virtual std::vector<std::string> getFileNames(__attribute__((unused)) bool outputFiles)
134 {
135 return std::vector<std::string>();
136 }
137
146 virtual void beginRun() {};
147
156 virtual void event() {};
157
165 virtual void endRun() {};
166
175 virtual void terminate() {};
176
186 const std::string& getName() const {return m_name;}
187
191 const std::string& getType() const;
192
196 const std::string& getPackage() const {return m_package;}
197
201 const std::string& getDescription() const {return m_description;}
202
213 void setName(const std::string& name) { m_name = name; };
219 void setPropertyFlags(unsigned int propertyFlags);
220
225
229 void setLogConfig(const LogConfig& logConfig) {m_logConfig = logConfig;}
230
234 void setLogLevel(int logLevel);
235
239 void setDebugLevel(int debugLevel);
240
244 void setAbortLevel(int abortLevel);
245
252 void setLogInfo(int logLevel, unsigned int logInfo);
253
254
270 void if_value(const std::string& expression, const std::shared_ptr<Path>& path,
271 EAfterConditionPath afterConditionPath = EAfterConditionPath::c_End);
272
288 void if_false(const std::shared_ptr<Path>& path, EAfterConditionPath afterConditionPath = EAfterConditionPath::c_End);
289
305 void if_true(const std::shared_ptr<Path>& path, EAfterConditionPath afterConditionPath = EAfterConditionPath::c_End);
306
310 bool hasCondition() const { return not m_conditions.empty(); };
311
314 {
315 if (m_conditions.empty()) {
316 return nullptr;
317 } else {
318 return &m_conditions.front();
319 }
320 }
321
323 const std::vector<ModuleCondition>& getAllConditions() const
324 {
325 return m_conditions;
326 }
327
338 bool evalCondition() const;
339
341 std::shared_ptr<Path> getConditionPath() const;
342
345
347 std::vector<std::shared_ptr<Path>> getAllConditionPaths() const;
348
354 bool hasProperties(unsigned int propertyFlags) const;
355
359 bool hasUnsetForcedParams() const;
360
363
373 template<typename T>
374 ModuleParam<T>& getParam(const std::string& name) const;
375
377 bool hasReturnValue() const { return m_hasReturnValue; }
380 int getReturnValue() const { return m_returnValue; }
381
387 std::shared_ptr<PathElement> clone() const override;
388
389
390 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
391 // Python API
392 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
393
401 std::shared_ptr<boost::python::list> getParamInfoListPython() const;
402
406 static void exposePythonAPI();
407
408 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
409
410
411 protected:
413
419 virtual void def_initialize() { initialize(); }
420
425 virtual void def_beginRun() { beginRun(); }
426
431 virtual void def_event() { event(); }
432
438 virtual void def_endRun() { endRun(); }
439
444 virtual void def_terminate() { terminate(); }
446
447
453 void setDescription(const std::string& description);
454
456 void setType(const std::string& type);
457
466 template<typename T>
467 void addParam(const std::string& name, T& paramVariable, const std::string& description, const T& defaultValue);
468
477 template<typename T>
478 void addParam(const std::string& name, T& paramVariable, const std::string& description);
479
487 void setReturnValue(int value);
488
497 void setReturnValue(bool value);
498
500 void setParamList(const ModuleParamList& params) { m_moduleParamList = params; }
501
502
503 private:
505 std::list<ModulePtr> getModules() const override { return std::list<ModulePtr>(); }
506
507 std::string m_name;
508 std::string m_type;
509 std::string m_package;
510 std::string m_description;
511 unsigned int m_propertyFlags;
520 std::vector<ModuleCondition> m_conditions;
522 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
523 // Python API
524 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
525
527 std::string getPathString() const override;
528
538 void setParamPython(const std::string& name, const boost::python::object& pyObj);
539
547 void setParamPythonDict(const boost::python::dict& dictionary);
548
549 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
550
551 friend class ModuleProxyBase;
552 };
553
554
555 //------------------------------------------------------
556 // Implementation of template based methods
557 //------------------------------------------------------
558 template<typename T>
559 void Module::addParam(const std::string& name, T& paramVariable, const std::string& description, const T& defaultValue)
560 {
561 m_moduleParamList.addParameter(name, paramVariable, description, defaultValue);
562 }
563
564
565 template<typename T>
566 void Module::addParam(const std::string& name, T& paramVariable, const std::string& description)
567 {
568 m_moduleParamList.addParameter(name, paramVariable, description);
569 }
570
571 template<typename T>
572 ModuleParam<T>& Module::getParam(const std::string& name) const
573 {
574 return m_moduleParamList.getParameter<T>(name);
575 }
576
577
578 //------------------------------------------------------
579 // Define convenient typdefs
580 //------------------------------------------------------
581
583 typedef std::list<ModulePtr> ModulePtrList;
584
585
586 //------------------------------------------------------
587 // Proxy class for creating an instance of the module
588 //------------------------------------------------------
597
598 public:
605 ModuleProxyBase(std::string moduleType, std::string package);
606
610 virtual ~ModuleProxyBase() {};
611
618 {
620 nm->setType(m_moduleType);
621 nm->setName(m_moduleType);
622 nm->m_package = m_package;
623 return nm;
624 }
625
629 const std::string& getModuleName() const {return m_moduleType; }
630
631
632 protected:
634 virtual Module* createInstance() const = 0;
635
636 std::string m_moduleType;
637 std::string m_package;
638 };
639
640
641#ifndef _PACKAGE_
642#define _PACKAGE_ ""
643#endif
644
645 //------------------------------------------------------
646 // Define convenient macro
647 //------------------------------------------------------
649#define REG_MODULE(moduleName) namespace { struct ModuleProxy##moduleName: public ModuleProxyBase { \
650 ModuleProxy##moduleName(): ModuleProxyBase(#moduleName, "" _PACKAGE_) {} \
651 virtual ::Belle2::Module* createInstance() const override final { return new moduleName##Module(); } \
652 } proxy##moduleName##Module; }
653
655} // end namespace Belle2
The LogConfig class.
Definition: LogConfig.h:22
Wraps a condition set on a Module instance.
EAfterConditionPath
Different options for behaviour after a conditional path was executed.
The Module parameter list class.
A single parameter of the module.
Definition: ModuleParam.h:34
The base module proxy class is used to create new instances of a module.
Definition: Module.h:596
const std::string & getModuleName() const
Returns the module name of the module associated to this proxy.
Definition: Module.h:629
virtual ~ModuleProxyBase()
The destructor of the ModuleProxyBase class.
Definition: Module.h:610
ModulePtr createModule() const
Abstract method which creates a new module and returns a shared pointer to it.
Definition: Module.h:617
virtual Module * createInstance() const =0
create a new instance of the module in question
std::string m_moduleType
The type name of the module.
Definition: Module.h:636
std::string m_package
Package this module is found in (may be empty).
Definition: Module.h:637
Base class for Modules.
Definition: Module.h:72
void setLogLevel(int logLevel)
Configure the log level.
Definition: Module.cc:55
std::shared_ptr< boost::python::list > getParamInfoListPython() const
Returns a python list of all parameters.
Definition: Module.cc:279
LogConfig & getLogConfig()
Returns the log system configuration.
Definition: Module.h:224
const ModuleCondition * getCondition() const
Return a pointer to the first condition (or nullptr, if none was set)
Definition: Module.h:313
void if_value(const std::string &expression, const std::shared_ptr< Path > &path, EAfterConditionPath afterConditionPath=EAfterConditionPath::c_End)
Add a condition to the module.
Definition: Module.cc:79
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
const ModuleParamList & getParamList() const
Return module param list.
Definition: Module.h:362
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
const std::string & getName() const
Returns the name of the module.
Definition: Module.h:186
void if_true(const std::shared_ptr< Path > &path, EAfterConditionPath afterConditionPath=EAfterConditionPath::c_End)
A simplified version to set the condition of the module.
Definition: Module.cc:90
virtual void def_endRun()
This method can receive that the current run ends as a call from the Python side.
Definition: Module.h:438
std::vector< std::shared_ptr< Path > > getAllConditionPaths() const
Return all condition paths currently set (no matter if the condition is true or not).
Definition: Module.cc:150
int m_returnValue
The return value.
Definition: Module.h:518
Module()
Constructor.
Definition: Module.cc:30
const std::string & getType() const
Returns the type of the module (i.e.
Definition: Module.cc:41
void setReturnValue(int value)
Sets the return value for this module as integer.
Definition: Module.cc:220
EModulePropFlags
Each module can be tagged with property flags, which indicate certain features of the module.
Definition: Module.h:77
@ c_HistogramManager
This module is used to manage histograms accumulated by other modules.
Definition: Module.h:81
@ c_Input
This module is an input module (reads data).
Definition: Module.h:78
@ c_DontCollectStatistics
No statistics is collected for this module.
Definition: Module.h:84
@ 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_InternalSerializer
This module is an internal serializer/deserializer for parallel processing.
Definition: Module.h:82
@ c_Output
This module is an output module (writes data).
Definition: Module.h:79
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
Definition: Module.h:83
void setLogConfig(const LogConfig &logConfig)
Set the log system configuration.
Definition: Module.h:229
const std::string & getDescription() const
Returns the description of the module.
Definition: Module.h:201
virtual void event()
This method is the core of the module.
Definition: Module.h:156
virtual void initialize()
Initialize the Module.
Definition: Module.h:109
void setAbortLevel(int abortLevel)
Configure the abort log level.
Definition: Module.cc:67
virtual void def_terminate()
Wrapper method for the virtual function terminate() that has the implementation to be used in a call ...
Definition: Module.h:444
virtual void beginRun()
Called when entering a new run.
Definition: Module.h:146
void setDebugLevel(int debugLevel)
Configure the debug messaging level.
Definition: Module.cc:61
void setParamPython(const std::string &name, const boost::python::object &pyObj)
Implements a method for setting boost::python objects.
Definition: Module.cc:234
virtual void def_initialize()
Wrappers to make the methods without "def_" prefix callable from Python.
Definition: Module.h:419
unsigned int m_propertyFlags
The properties of the module as bitwise or (with |) of EModulePropFlags.
Definition: Module.h:511
void if_false(const std::shared_ptr< Path > &path, EAfterConditionPath afterConditionPath=EAfterConditionPath::c_End)
A simplified version to add a condition to the module.
Definition: Module.cc:85
ModuleParamList m_moduleParamList
List storing and managing all parameter of the module.
Definition: Module.h:515
bool hasReturnValue() const
Return true if this module has a valid return value set.
Definition: Module.h:377
virtual void endRun()
This method is called if the current run ends.
Definition: Module.h:165
bool hasCondition() const
Returns true if at least one condition was set for the module.
Definition: Module.h:310
std::shared_ptr< Path > getConditionPath() const
Returns the path of the last true condition (if there is at least one, else reaturn a null pointer).
Definition: Module.cc:113
std::string m_type
The type of the module, saved as a string.
Definition: Module.h:508
bool m_hasReturnValue
True, if the return value is set.
Definition: Module.h:517
virtual void def_beginRun()
Wrapper method for the virtual function beginRun() that has the implementation to be used in a call f...
Definition: Module.h:425
const std::string & getPackage() const
Returns the package this module is in.
Definition: Module.h:196
virtual std::vector< std::string > getFileNames(bool outputFiles)
Return a list of output filenames for this modules.
Definition: Module.h:133
void setName(const std::string &name)
Set the name of the module.
Definition: Module.h:213
void setType(const std::string &type)
Set the module type.
Definition: Module.cc:48
static void exposePythonAPI()
Exposes methods of the Module class to Python.
Definition: Module.cc:325
LogConfig m_logConfig
The log system configuration of the module.
Definition: Module.h:513
Module::EAfterConditionPath getAfterConditionPath() const
What to do after the conditional path is finished.
Definition: Module.cc:133
std::vector< ModuleCondition > m_conditions
Module condition, only non-null if set.
Definition: Module.h:520
void setParamList(const ModuleParamList &params)
Replace existing parameter list.
Definition: Module.h:500
std::shared_ptr< PathElement > clone() const override
Create an independent copy of this module.
Definition: Module.cc:179
bool hasProperties(unsigned int propertyFlags) const
Returns true if all specified property flags are available in this module.
Definition: Module.cc:160
std::string m_description
The description of the module.
Definition: Module.h:510
virtual void def_event()
Wrapper method for the virtual function event() that has the implementation to be used in a call from...
Definition: Module.h:431
std::string m_name
The name of the module, saved as a string (user-modifiable)
Definition: Module.h:507
ModuleCondition::EAfterConditionPath EAfterConditionPath
Forward the EAfterConditionPath definition from the ModuleCondition.
Definition: Module.h:88
std::list< ModulePtr > getModules() const override
no submodules, return empty list
Definition: Module.h:505
bool evalCondition() const
If at least one condition was set, it is evaluated and true returned if at least one condition return...
Definition: Module.cc:96
virtual void terminate()
This method is called at the end of the event processing.
Definition: Module.h:175
int getReturnValue() const
Return the return value set by this module.
Definition: Module.h:380
void setParamPythonDict(const boost::python::dict &dictionary)
Implements a method for reading the parameter values from a boost::python dictionary.
Definition: Module.cc:249
std::string m_package
Package this module is found in (may be empty).
Definition: Module.h:509
void setLogInfo(int logLevel, unsigned int logInfo)
Configure the printed log information for the given level.
Definition: Module.cc:73
bool hasUnsetForcedParams() const
Returns true and prints error message if the module has unset parameters which the user has to set in...
Definition: Module.cc:166
const std::vector< ModuleCondition > & getAllConditions() const
Return all set conditions for this module.
Definition: Module.h:323
std::string getPathString() const override
return the module name.
Definition: Module.cc:192
Base for classes that can be elements of a Path.
Definition: PathElement.h:27
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:559
ModuleParam< T > & getParameter(const std::string &name) const
Returns a reference to a parameter.
ModuleParam< T > & getParam(const std::string &name) const
Returns a reference to a parameter.
Definition: Module.h:572
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:583
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Abstract base class for different kinds of events.