HOPS
HOPS class reference
MHO_PyUnaryTableOperator.hh
Go to the documentation of this file.
1 #ifndef MHO_PyUnaryTableOperator_HH__
2 #define MHO_PyUnaryTableOperator_HH__
3 
5 #include "MHO_UnaryOperator.hh"
6 
7 #include <pybind11/numpy.h> //this is important to have for std::complex<T> support!
8 #include <pybind11/pybind11.h>
9 namespace py = pybind11;
10 
11 namespace hops
12 {
13 
23 {
24  public:
25  MHO_PyUnaryTableOperator(): fHelper(nullptr), fInitialized(false){};
26 
27  virtual ~MHO_PyUnaryTableOperator() { delete fHelper; };
28 
29  template< typename XTableType > void SetInput(XTableType* in)
30  {
31  if(fHelper != nullptr)
32  {
33  delete fHelper;
34  fHelper = nullptr;
35  }
36  fHelper = new helper_specific< XTableType >(in);
37  };
38 
39  void SetModuleFunctionName(std::string module_name, std::string function_name)
40  {
41  fModuleName = module_name;
42  fFunctionName = function_name;
43  }
44 
45  //for now this does nothing, as the interpreter lifetime is confined to
46  //the Execute function, we may want to re-think this.
47  virtual bool Initialize() override
48  {
49  if(fHelper != nullptr)
50  {
51  fInitialized = true;
52  }
53  return fInitialized;
54  };
55 
56  virtual bool Execute() override
57  {
58  if(fInitialized)
59  {
60  fHelper->SetModuleName(fModuleName);
61  fHelper->SetFunctionName(fFunctionName);
62  fHelper->exe();
63  }
64  return false;
65  }
66 
67  private:
68  class helper_base
69  {
70  public:
71  helper_base(){};
72  virtual ~helper_base(){};
73 
74  void SetModuleName(std::string mod) { fModuleName = mod; }
75 
76  void SetFunctionName(std::string func) { fFunctionName = func; };
77 
78  virtual bool exe() = 0;
79 
80  protected:
81  std::string fModuleName;
82  std::string fFunctionName;
83  };
84 
85  template< typename XTableType > class helper_specific: public helper_base
86  {
87  public:
88  helper_specific(XTableType* ptr): fPtr(ptr){};
89  virtual ~helper_specific(){};
90 
91  virtual bool exe() override
92  {
93  if(!(fPtr->template HasExtension< MHO_PyTableContainer< XTableType > >()))
94  {
95  fPtr->template MakeExtension< MHO_PyTableContainer< XTableType > >();
96  }
97  //assume the python interpreter is already running (should we use try/catch?)
98  auto mod = py::module::import(fModuleName.c_str());
99  auto extension = fPtr->template AsExtension< MHO_PyTableContainer< XTableType > >();
100  MHO_PyTableContainer< XTableType >* container =
101  dynamic_cast< MHO_PyTableContainer< XTableType >* >(extension);
102  if(container != nullptr)
103  {
104  mod.attr(fFunctionName.c_str())(*container);
105  return true;
106  }
107  else
108  {
109  return false;
110  }
111  }
112 
113  private:
114  XTableType* fPtr;
115  };
116 
117  helper_base* fHelper;
118 
119  bool fInitialized;
120  std::string fModuleName;
121  std::string fFunctionName;
122 };
123 
124 } // namespace hops
125 
126 #endif
Class MHO_Operator.
Definition: MHO_Operator.hh:21
Definition: MHO_PyUnaryTableOperator.hh:23
MHO_PyUnaryTableOperator()
Definition: MHO_PyUnaryTableOperator.hh:25
virtual bool Initialize() override
Function Initialize.
Definition: MHO_PyUnaryTableOperator.hh:47
void SetModuleFunctionName(std::string module_name, std::string function_name)
Definition: MHO_PyUnaryTableOperator.hh:39
void SetInput(XTableType *in)
Definition: MHO_PyUnaryTableOperator.hh:29
virtual ~MHO_PyUnaryTableOperator()
Definition: MHO_PyUnaryTableOperator.hh:27
virtual bool Execute() override
Function Execute.
Definition: MHO_PyUnaryTableOperator.hh:56
Definition: MHO_ChannelLabeler.hh:17