Belle II Software  release-05-02-19
Module.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Andreas Moll, Martin Heck *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/core/ModuleParamList.h>
14 #include <framework/core/ModuleParamList.templateDetails.h>
15 #include <framework/core/ModuleCondition.h>
16 #include <framework/core/PathElement.h>
17 
18 #include <framework/logging/LogConfig.h>
19 
20 #include <list>
21 #include <string>
22 #include <memory>
23 
24 namespace boost {
25  namespace python {
26  class list;
27  class dict;
28  namespace api {
29  class object;
30  }
31  using api::object;
32  }
33 }
34 
35 namespace Belle2 {
41  class Module;
42  class Path;
43 
45  typedef std::shared_ptr<Module> ModulePtr;
46 
74  class Module : public PathElement {
75 
76  public:
77 
80  c_Input = 1,
81  c_Output = 2,
87  };
88 
91 
101  Module();
102 
111  virtual void initialize() {};
112 
135  [[deprecated("will be removed in release-05.")]]
136  virtual std::vector<std::string> getFileNames(__attribute__((unused)) bool outputFiles)
137  {
138  return std::vector<std::string>();
139  }
140 
149  virtual void beginRun() {};
150 
159  virtual void event() {};
160 
168  virtual void endRun() {};
169 
178  virtual void terminate() {};
179 
189  const std::string& getName() const {return m_name;}
190 
194  const std::string& getType() const;
195 
199  const std::string& getPackage() const {return m_package;}
200 
204  const std::string& getDescription() const {return m_description;}
205 
216  void setName(const std::string& name) { m_name = name; };
222  void setPropertyFlags(unsigned int propertyFlags);
223 
228 
232  void setLogConfig(const LogConfig& logConfig) {m_logConfig = logConfig;}
233 
237  void setLogLevel(int logLevel);
238 
242  void setDebugLevel(int debugLevel);
243 
247  void setAbortLevel(int abortLevel);
248 
255  void setLogInfo(int logLevel, unsigned int logInfo);
256 
257 
273  void if_value(const std::string& expression, const std::shared_ptr<Path>& path,
274  EAfterConditionPath afterConditionPath = EAfterConditionPath::c_End);
275 
291  void if_false(const std::shared_ptr<Path>& path, EAfterConditionPath afterConditionPath = EAfterConditionPath::c_End);
292 
308  void if_true(const std::shared_ptr<Path>& path, EAfterConditionPath afterConditionPath = EAfterConditionPath::c_End);
309 
313  bool hasCondition() const { return not m_conditions.empty(); };
314 
317  {
318  if (m_conditions.empty()) {
319  return nullptr;
320  } else {
321  return &m_conditions.front();
322  }
323  }
324 
326  const std::vector<ModuleCondition>& getAllConditions() const
327  {
328  return m_conditions;
329  }
330 
341  bool evalCondition() const;
342 
344  std::shared_ptr<Path> getConditionPath() const;
345 
348 
350  std::vector<std::shared_ptr<Path>> getAllConditionPaths() const;
351 
357  bool hasProperties(unsigned int propertyFlags) const;
358 
362  bool hasUnsetForcedParams() const;
363 
365  const ModuleParamList& getParamList() const { return m_moduleParamList; }
366 
376  template<typename T>
377  ModuleParam<T>& getParam(const std::string& name) const;
378 
380  bool hasReturnValue() const { return m_hasReturnValue; }
383  int getReturnValue() const { return m_returnValue; }
384 
390  std::shared_ptr<PathElement> clone() const override;
391 
392 
393  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
394  // Python API
395  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
396 
404  std::shared_ptr<boost::python::list> getParamInfoListPython() const;
405 
409  static void exposePythonAPI();
410 
411  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
412 
413 
414  protected:
416 
422  virtual void def_initialize() { initialize(); }
423 
428  virtual void def_beginRun() { beginRun(); }
429 
434  virtual void def_event() { event(); }
435 
441  virtual void def_endRun() { endRun(); }
442 
447  virtual void def_terminate() { terminate(); }
449 
450 
456  void setDescription(const std::string& description);
457 
459  void setType(const std::string& type);
460 
469  template<typename T>
470  void addParam(const std::string& name, T& paramVariable, const std::string& description, const T& defaultValue);
471 
480  template<typename T>
481  void addParam(const std::string& name, T& paramVariable, const std::string& description);
482 
490  void setReturnValue(int value);
491 
500  void setReturnValue(bool value);
501 
503  void setParamList(const ModuleParamList& params) { m_moduleParamList = params; }
504 
505 
506  private:
508  std::list<ModulePtr> getModules() const override { return std::list<ModulePtr>(); }
509 
510  std::string m_name;
511  std::string m_type;
512  std::string m_package;
513  std::string m_description;
514  unsigned int m_propertyFlags;
523  std::vector<ModuleCondition> m_conditions;
525  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
526  // Python API
527  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
528 
530  std::string getPathString() const override;
531 
541  void setParamPython(const std::string& name, const boost::python::object& pyObj);
542 
550  void setParamPythonDict(const boost::python::dict& dictionary);
551 
552  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
553 
554  friend class ModuleProxyBase;
555  };
556 
557 
558  //------------------------------------------------------
559  // Implementation of template based methods
560  //------------------------------------------------------
561  template<typename T>
562  void Module::addParam(const std::string& name, T& paramVariable, const std::string& description, const T& defaultValue)
563  {
564  m_moduleParamList.addParameter(name, paramVariable, description, defaultValue);
565  }
566 
567 
568  template<typename T>
569  void Module::addParam(const std::string& name, T& paramVariable, const std::string& description)
570  {
571  m_moduleParamList.addParameter(name, paramVariable, description);
572  }
573 
574  template<typename T>
575  ModuleParam<T>& Module::getParam(const std::string& name) const
576  {
577  return m_moduleParamList.getParameter<T>(name);
578  }
579 
580 
581  //------------------------------------------------------
582  // Define convenient typdefs
583  //------------------------------------------------------
584 
586  typedef std::list<ModulePtr> ModulePtrList;
587 
588 
589  //------------------------------------------------------
590  // Proxy class for creating an instance of the module
591  //------------------------------------------------------
600 
601  public:
608  ModuleProxyBase(std::string moduleType, std::string package);
609 
613  virtual ~ModuleProxyBase() {};
614 
621  {
623  nm->setType(m_moduleType);
624  nm->setName(m_moduleType);
625  nm->m_package = m_package;
626  return nm;
627  }
628 
632  const std::string& getModuleName() const {return m_moduleType; }
633 
634 
635  protected:
637  virtual Module* createInstance() const = 0;
638 
639  std::string m_moduleType;
640  std::string m_package;
641  };
642 
643 
644 #ifndef _PACKAGE_
645 #define _PACKAGE_ ""
646 #endif
647 
648  //------------------------------------------------------
649  // Define convenient macro
650  //------------------------------------------------------
652 #define REG_MODULE(moduleName) namespace { struct ModuleProxy##moduleName: public ModuleProxyBase { \
653  ModuleProxy##moduleName(): ModuleProxyBase(#moduleName, "" _PACKAGE_) {} \
654  virtual ::Belle2::Module* createInstance() const override final { return new moduleName##Module(); } \
655  } proxy##moduleName##Module; }
656 
658 } // end namespace Belle2
Belle2::ModuleProxyBase::createInstance
virtual Module * createInstance() const =0
create a new instance of the module in question
Belle2::Module::getAfterConditionPath
Module::EAfterConditionPath getAfterConditionPath() const
What to do after the conditional path is finished.
Definition: Module.cc:135
Belle2::Module::getConditionPath
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:115
Belle2::Module::if_false
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:87
Belle2::Module::setLogLevel
void setLogLevel(int logLevel)
Configure the log level.
Definition: Module.cc:57
Belle2::Module::c_Output
@ c_Output
This module is an output module (writes data).
Definition: Module.h:81
Belle2::Module::setParamPython
void setParamPython(const std::string &name, const boost::python::object &pyObj)
Implements a method for setting boost::python objects.
Definition: Module.cc:236
Belle2::Module::terminate
virtual void terminate()
This method is called at the end of the event processing.
Definition: Module.h:178
Belle2::ModuleProxyBase::~ModuleProxyBase
virtual ~ModuleProxyBase()
The destructor of the ModuleProxyBase class.
Definition: Module.h:613
Belle2::ModuleProxyBase::m_package
std::string m_package
Package this module is found in (may be empty).
Definition: Module.h:640
Belle2::Module::if_true
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:92
Belle2::Module::setDescription
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:216
Belle2::Module::c_ParallelProcessingCertified
@ 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:82
Belle2::Module::event
virtual void event()
This method is the core of the module.
Definition: Module.h:159
Belle2::PathElement
Base for classes that can be elements of a Path.
Definition: PathElement.h:37
Belle2::Module::getParamInfoListPython
std::shared_ptr< boost::python::list > getParamInfoListPython() const
Returns a python list of all parameters.
Definition: Module.cc:281
Belle2::Module::m_returnValue
int m_returnValue
The return value.
Definition: Module.h:521
Belle2::ModuleProxyBase::createModule
ModulePtr createModule() const
Abstract method which creates a new module and returns a shared pointer to it.
Definition: Module.h:620
Belle2::ModuleParamList::getParameter
ModuleParam< T > & getParameter(const std::string &name) const
Returns a reference to a parameter.
Definition: ModuleParamList.templateDetails.h:90
Belle2::Module::hasCondition
bool hasCondition() const
Returns true if at least one condition was set for the module.
Definition: Module.h:313
Belle2::Module::c_TerminateInAllProcesses
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
Definition: Module.h:85
Belle2::Module::m_logConfig
LogConfig m_logConfig
The log system configuration of the module.
Definition: Module.h:516
Belle2::Module::def_initialize
virtual void def_initialize()
Wrappers to make the methods without "def_" prefix callable from Python.
Definition: Module.h:422
Belle2::ModuleCondition::EAfterConditionPath::c_End
@ c_End
End current event after the conditional path.
Belle2::Module::c_InternalSerializer
@ c_InternalSerializer
This module is an internal serializer/deserializer for parallel processing.
Definition: Module.h:84
Belle2::Module::m_package
std::string m_package
Package this module is found in (may be empty).
Definition: Module.h:512
Belle2::Module::exposePythonAPI
static void exposePythonAPI()
Exposes methods of the Module class to Python.
Definition: Module.cc:327
Belle2::Module::getType
const std::string & getType() const
Returns the type of the module (i.e.
Definition: Module.cc:43
Belle2::ModuleProxyBase::ModuleProxyBase
ModuleProxyBase(std::string moduleType, std::string package)
The constructor of the ModuleProxyBase class.
Definition: Module.cc:499
Belle2::ModuleProxyBase
The base module proxy class is used to create new instances of a module.
Definition: Module.h:599
Belle2::ModuleProxyBase::getModuleName
const std::string & getModuleName() const
Returns the module name of the module associated to this proxy.
Definition: Module.h:632
Belle2::Module::if_value
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:81
Belle2::Module::hasReturnValue
bool hasReturnValue() const
Return true if this module has a valid return value set.
Definition: Module.h:380
Belle2::Module::getReturnValue
int getReturnValue() const
Return the return value set by this module.
Definition: Module.h:383
Belle2::Module::getPackage
const std::string & getPackage() const
Returns the package this module is in.
Definition: Module.h:199
Belle2::Module::setAbortLevel
void setAbortLevel(int abortLevel)
Configure the abort log level.
Definition: Module.cc:69
Belle2::Module::setDebugLevel
void setDebugLevel(int debugLevel)
Configure the debug messaging level.
Definition: Module.cc:63
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::Module::hasUnsetForcedParams
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:168
Belle2::Module::setPropertyFlags
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:210
Belle2::Module::getDescription
const std::string & getDescription() const
Returns the description of the module.
Definition: Module.h:204
Belle2::Module::c_HistogramManager
@ c_HistogramManager
This module is used to manage histograms accumulated by other modules.
Definition: Module.h:83
Belle2::Module::clone
std::shared_ptr< PathElement > clone() const override
Create an independent copy of this module.
Definition: Module.cc:181
Belle2::ModuleParamList::addParameter
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Definition: ModuleParamList.templateDetails.h:38
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Module::getFileNames
virtual std::vector< std::string > getFileNames(__attribute__((unused)) bool outputFiles)
Return a list of output filenames for this modules.
Definition: Module.h:136
Belle2::Module::endRun
virtual void endRun()
This method is called if the current run ends.
Definition: Module.h:168
Belle2::ModulePtrList
std::list< ModulePtr > ModulePtrList
Defines a std::list of shared module pointers.
Definition: Module.h:586
Belle2::Module::setType
void setType(const std::string &type)
Set the module type.
Definition: Module.cc:50
Belle2::Module::EAfterConditionPath
ModuleCondition::EAfterConditionPath EAfterConditionPath
Forward the EAfterConditionPath definition from the ModuleCondition.
Definition: Module.h:90
Belle2::Module::m_hasReturnValue
bool m_hasReturnValue
True, if the return value is set.
Definition: Module.h:520
Belle2::ModulePtr
std::shared_ptr< Module > ModulePtr
Defines a pointer to a module object as a boost shared pointer.
Definition: Module.h:42
Belle2::Module::m_moduleParamList
ModuleParamList m_moduleParamList
List storing and managing all parameter of the module.
Definition: Module.h:518
Belle2::Module::getCondition
const ModuleCondition * getCondition() const
Return a pointer to the first condition (or nullptr, if none was set)
Definition: Module.h:316
Belle2::Module::m_name
std::string m_name
The name of the module, saved as a string (user-modifiable)
Definition: Module.h:510
Belle2::Module::getAllConditions
const std::vector< ModuleCondition > & getAllConditions() const
Return all set conditions for this module.
Definition: Module.h:326
Belle2::Module::getModules
std::list< ModulePtr > getModules() const override
no submodules, return empty list
Definition: Module.h:508
Belle2::ModuleParam
A single parameter of the module.
Definition: DisplayUI.h:34
Belle2::Module::getParam
ModuleParam< T > & getParam(const std::string &name) const
Returns a reference to a parameter.
Definition: Module.h:575
Belle2::Module::setReturnValue
void setReturnValue(int value)
Sets the return value for this module as integer.
Definition: Module.cc:222
Belle2::Module::def_endRun
virtual void def_endRun()
This method can receive that the current run ends as a call from the Python side.
Definition: Module.h:441
Belle2::Module::m_type
std::string m_type
The type of the module, saved as a string.
Definition: Module.h:511
Belle2::Module::setLogInfo
void setLogInfo(int logLevel, unsigned int logInfo)
Configure the printed log information for the given level.
Definition: Module.cc:75
Belle2::Module::m_conditions
std::vector< ModuleCondition > m_conditions
Module condition, only non-null if set.
Definition: Module.h:523
Belle2::Module::beginRun
virtual void beginRun()
Called when entering a new run.
Definition: Module.h:149
Belle2::Module::Module
Module()
Constructor.
Definition: Module.cc:32
Belle2::Module::addParam
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:562
Belle2::Module::EModulePropFlags
EModulePropFlags
Each module can be tagged with property flags, which indicate certain features of the module.
Definition: Module.h:79
Belle2::Module::getParamList
const ModuleParamList & getParamList() const
Return module param list.
Definition: Module.h:365
Belle2::ModuleCondition::EAfterConditionPath
EAfterConditionPath
Different options for behaviour after a conditional path was executed.
Definition: ModuleCondition.h:35
Belle2::Path
Implements a path consisting of Module and/or Path objects.
Definition: Path.h:40
Belle2::Module::c_Input
@ c_Input
This module is an input module (reads data).
Definition: Module.h:80
Belle2::Module::def_terminate
virtual void def_terminate()
Wrapper method for the virtual function terminate() that has the implementation to be used in a call ...
Definition: Module.h:447
Belle2::ModuleCondition
Wraps a condition set on a Module instance.
Definition: ModuleCondition.h:22
Belle2::Module::def_beginRun
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:428
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46
Belle2::Module::m_propertyFlags
unsigned int m_propertyFlags
The properties of the module as bitwise or (with |) of EModulePropFlags.
Definition: Module.h:514
Belle2::Module::def_event
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:434
Belle2::LogConfig
The LogConfig class.
Definition: LogConfig.h:32
Belle2::Module::initialize
virtual void initialize()
Initialize the Module.
Definition: Module.h:111
Belle2::Module::m_description
std::string m_description
The description of the module.
Definition: Module.h:513
Belle2::Module::getName
const std::string & getName() const
Returns the name of the module.
Definition: Module.h:189
Belle2::Module::setName
void setName(const std::string &name)
Set the name of the module.
Definition: Module.h:216
Belle2::Module::getLogConfig
LogConfig & getLogConfig()
Returns the log system configuration.
Definition: Module.h:227
Belle2::Module::evalCondition
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:98
Belle2::Module::c_DontCollectStatistics
@ c_DontCollectStatistics
No statistics is collected for this module.
Definition: Module.h:86
Belle2::Module::setParamPythonDict
void setParamPythonDict(const boost::python::dict &dictionary)
Implements a method for reading the parameter values from a boost::python dictionary.
Definition: Module.cc:251
Belle2::Module::getPathString
std::string getPathString() const override
return the module name.
Definition: Module.cc:194
Belle2::Module::getAllConditionPaths
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:152
Belle2::ModuleProxyBase::m_moduleType
std::string m_moduleType
The type name of the module.
Definition: Module.h:639
Belle2::Module::setLogConfig
void setLogConfig(const LogConfig &logConfig)
Set the log system configuration.
Definition: Module.h:232
Belle2::Module::hasProperties
bool hasProperties(unsigned int propertyFlags) const
Returns true if all specified property flags are available in this module.
Definition: Module.cc:162
Belle2::Module::setParamList
void setParamList(const ModuleParamList &params)
Replace existing parameter list.
Definition: Module.h:503