Belle II Software  release-05-02-19
PyModule.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2012 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Christian Pulvermacher *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <boost/python/call_method.hpp>
14 #include <framework/core/Module.h>
15 
16 namespace Belle2 {
31  class PyModule : public Module {
32  public:
34  explicit PyModule(PyObject* p):
35  Module(),
36  m_self(p)
37  {
38  setName(p->ob_type->tp_name);
39  setType("PyModule");
40  }
41 
43  PyModule(PyObject* p, const Module& m):
44  Module(m),
45  m_self(p)
46  {
47  setName(p->ob_type->tp_name);
48  setType("PyModule");
49  }
50 
51 
52  /* reimplement all virtual functions of base class and call the corresponding method in the python class
53  * note that all methods must be registered in Module::exposePythonAPI()
54  */
55 
56  virtual void initialize() override final
57  {
58  boost::python::call_method<void>(m_self, "initialize");
59  }
60 
61  virtual void beginRun() override final
62  {
63  boost::python::call_method<void>(m_self, "beginRun");
64  }
65 
66  virtual void event() override final
67  {
68  boost::python::call_method<void>(m_self, "event");
69  }
70 
71  virtual void endRun() override final
72  {
73  boost::python::call_method<void>(m_self, "endRun");
74  }
75 
76  virtual void terminate() override final
77  {
78  boost::python::call_method<void>(m_self, "terminate");
79  }
80 
81  private:
83 
84  virtual void def_initialize() override final
85  {
86  // Call empty base implementation if initialize() is not overriden from Python.
88  }
89 
90  virtual void def_beginRun() override final
91  {
92  // Call empty base implementation if beginRun() is not overriden from Python.
94  }
95 
96  virtual void def_event() override final
97  {
98  // Call empty base implementation if event() is not overriden from Python.
99  Module::event();
100  }
101 
102  virtual void def_endRun() override final
103  {
104  // Call empty base implementation if endRun() is not overriden from Python.
105  Module::endRun();
106  }
107 
108  virtual void def_terminate() override final
109  {
110  // Call empty base implementation if terminate() is not overriden from Python.
112  }
114 
115  PyObject* m_self;
116  };
118 }
Belle2::Module::terminate
virtual void terminate()
This method is called at the end of the event processing.
Definition: Module.h:178
Belle2::PyModule::PyModule
PyModule(PyObject *p)
constructor
Definition: PyModule.h:42
Belle2::PyModule::def_endRun
virtual void def_endRun() override final
This method can receive that the current run ends as a call from the Python side.
Definition: PyModule.h:110
Belle2::PyModule::beginRun
virtual void beginRun() override final
Called when entering a new run.
Definition: PyModule.h:69
Belle2::Module::event
virtual void event()
This method is the core of the module.
Definition: Module.h:159
Belle2::PyModule::def_beginRun
virtual void def_beginRun() override final
Wrapper method for the virtual function beginRun() that has the implementation to be used in a call f...
Definition: PyModule.h:98
Belle2::PyModule::endRun
virtual void endRun() override final
This method is called if the current run ends.
Definition: PyModule.h:79
Belle2::PyModule::initialize
virtual void initialize() override final
Initialize the Module.
Definition: PyModule.h:64
Belle2::PyModule::def_terminate
virtual void def_terminate() override final
Wrapper method for the virtual function terminate() that has the implementation to be used in a call ...
Definition: PyModule.h:116
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Module::endRun
virtual void endRun()
This method is called if the current run ends.
Definition: Module.h:168
Belle2::PyModule::def_initialize
virtual void def_initialize() override final
default implementation used when Python module doesn't supply its own
Definition: PyModule.h:92
Belle2::Module::setType
void setType(const std::string &type)
Set the module type.
Definition: Module.cc:50
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::initialize
virtual void initialize()
Initialize the Module.
Definition: Module.h:111
Belle2::PyModule::terminate
virtual void terminate() override final
This method is called at the end of the event processing.
Definition: PyModule.h:84
Belle2::PyModule::event
virtual void event() override final
This method is the core of the module.
Definition: PyModule.h:74
Belle2::Module::setName
void setName(const std::string &name)
Set the name of the module.
Definition: Module.h:216
Belle2::PyModule::m_self
PyObject * m_self
the actual python module
Definition: PyModule.h:123
Belle2::PyModule::def_event
virtual void def_event() override final
Wrapper method for the virtual function event() that has the implementation to be used in a call from...
Definition: PyModule.h:104