HOPS
HOPS class reference
MHO_PyGenericOperator.hh
Go to the documentation of this file.
1 #ifndef MHO_PyGenericOperator_HH__
2 #define MHO_PyGenericOperator_HH__
3 
4 #include "MHO_Operator.hh"
5 
10 #include "MHO_FringeData.hh"
11 
12 #include "MHO_PyTableContainer.hh"
13 
14 #include <pybind11/numpy.h> //this is important to have for std::complex<T> support!
15 #include <pybind11/pybind11.h>
16 namespace py = pybind11;
17 
18 namespace hops
19 {
20 
34 {
35  public:
37  : fInitialized(false),
38  fFringeData(nullptr),
39  fFringeDataInterface(nullptr)
40  {
41  fModuleName = "";
42  fFunctionName = "";
43  };
44 
46  {
47  delete fFringeDataInterface;
48  };
49 
50  void SetFringeData(MHO_FringeData* fdata){fFringeData = fdata;}
51 
52  // void SetParameterStore(MHO_ParameterStore* pstore) { fParameterStore = pstore; };
53  //
54  // void SetContainerStore(MHO_ContainerStore* cstore) { fContainerStore = cstore; };
55 
56  void SetModuleName(std::string module_name) { fModuleName = module_name; }
57 
58  void SetFunctionName(std::string function_name) { fFunctionName = function_name; }
59 
60  virtual bool Initialize() override
61  {
62  fInitialized = false;
63  if(fModuleName == "")
64  {
65  return false;
66  }
67  if(fFunctionName == "")
68  {
69  return false;
70  }
71 
72  //construct the python interface exposing the parameter and container store
73  if(fFringeData != nullptr)
74  {
75  if(fFringeDataInterface == nullptr){fFringeDataInterface = new MHO_PyFringeDataInterface(fFringeData); }
76  fInitialized = true;
77  }
78 
79  // //construct the python interface exposing the parameter and container store
80  // if(fContainerStore != nullptr && fParameterStore != nullptr)
81  // {
82  // if(fContainerInterface == nullptr){fContainerInterface = new MHO_PyContainerStoreInterface(fContainerStore); }
83  // if(fParameterInterface == nullptr){fParameterInterface = new MHO_PyParameterStoreInterface(fParameterStore); }
84  // fInitialized = true;
85  // }
86  return fInitialized;
87  };
88 
89  virtual bool Execute() override
90  {
91  if(fInitialized)
92  {
93  bool success = false;
94  if(Py_IsInitialized() == 0)
95  {
96  //the internal python interpreter has not been started, bail out
97  msg_error("python_bindings", "python interpreter not running/initialized, "
98  << "cannot call python subroutine (" << fModuleName << "," << fFunctionName
99  << ")." << eom);
100  return success;
101  }
102 
103  //calling user code, so use try-catch in case there are errors
104  try
105  {
106  auto mod = py::module::import(fModuleName.c_str());
107  // mod.attr(fFunctionName.c_str())(*fContainerInterface, *fParameterInterface);
108  mod.attr(fFunctionName.c_str())(*fFringeDataInterface);
109  success = true;
110  }
111  catch(py::error_already_set& excep)
112  {
113  success = false;
114  msg_error("python_bindings", "python exception when calling subroutine (" << fModuleName << ","
115  << fFunctionName << ")" << eom);
116  msg_error("python_bindings", "python error message: " << excep.what() << eom);
117  msg_warn("python_bindings", "attempting to continue, but in-memory data may be in unknown state." << eom);
118  PyErr_Clear(); //clear the error and attempt to continue
119  }
120 
121  return success;
122  }
123  return false;
124  }
125 
126  private:
127  bool fInitialized;
128  std::string fModuleName;
129  std::string fFunctionName;
130 
131  MHO_FringeData* fFringeData;
132  MHO_PyFringeDataInterface* fFringeDataInterface;
133 
134  // MHO_ContainerStore* fContainerStore;
135  // MHO_ParameterStore* fParameterStore;
136  // MHO_PyParameterStoreInterface* fParameterInterface;
137  // MHO_PyContainerStoreInterface* fContainerInterface;
138 };
139 
140 } // namespace hops
141 
142 #endif
#define msg_warn(xKEY, xCONTENT)
Definition: MHO_Message.hh:254
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
Class MHO_FringeData.
Definition: MHO_FringeData.hh:30
Class MHO_Operator.
Definition: MHO_Operator.hh:21
python bindings for the MHO_FringeData class
Definition: MHO_PyFringeDataInterface.hh:34
this class allows a user to inject a python function of the form: func(fringe_data_interface) into th...
Definition: MHO_PyGenericOperator.hh:34
virtual bool Initialize() override
Function Initialize.
Definition: MHO_PyGenericOperator.hh:60
virtual ~MHO_PyGenericOperator()
Definition: MHO_PyGenericOperator.hh:45
void SetFunctionName(std::string function_name)
Definition: MHO_PyGenericOperator.hh:58
void SetModuleName(std::string module_name)
Definition: MHO_PyGenericOperator.hh:56
void SetFringeData(MHO_FringeData *fdata)
Definition: MHO_PyGenericOperator.hh:50
virtual bool Execute() override
Function Execute.
Definition: MHO_PyGenericOperator.hh:89
MHO_PyGenericOperator()
Definition: MHO_PyGenericOperator.hh:36
Definition: MHO_ChannelLabeler.hh:17