bindings.cc
Go to the documentation of this file.
1 /*
2  * MoMEMta: a modular implementation of the Matrix Element Method
3  * Copyright (C) 2016 Universite catholique de Louvain (UCL), Belgium
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <momemta/Configuration.h>
20 #include <momemta/ConfigurationReader.h>
21 #include <momemta/MoMEMta.h>
22 #include <momemta/Logging.h>
23 
24 #include <boost/python.hpp>
25 
26 #ifdef ROOT_HAS_PYROOT
27 #include <TPython.h>
28 #endif
29 
36 namespace bp = boost::python;
37 using namespace momemta;
38 
39 void set_log_level(logging::level::level_enum lvl) {
40  logging::set_level(lvl);
41 }
42 
43 bp::list MoMEMta_computeWeights_MET(MoMEMta& m, bp::list particles_, bp::list met_) {
44  std::vector<Particle> particles;
45  for (ssize_t i = 0; i < bp::len(particles_); i++) {
46  particles.push_back(bp::extract<Particle>(particles_[i]));
47  }
48 
49  LorentzVector met;
50  bp::extract<LorentzVector> lorentzVectorExtractor(met_);
51  if (lorentzVectorExtractor.check())
52  met = lorentzVectorExtractor();
53 
54  auto weights = m.computeWeights(particles, met);
55 
56  bp::list result;
57  for (const auto& weight: weights) {
58  bp::tuple pair = bp::make_tuple(weight.first, weight.second);
59  result.append(pair);
60  }
61 
62  return result;
63 }
64 
65 bp::list MoMEMta_computeWeights(MoMEMta& m, bp::list particles) {
66  return MoMEMta_computeWeights_MET(m, particles, bp::list());
67 }
68 
69 template<typename T>
70 const T& ParameterSet_get(ParameterSet& p, const std::string& name) {
71  return p.get<T>(name);
72 }
73 
74 template<typename T>
75 void ParameterSet_set(ParameterSet& p, const std::string& name, const T& value) {
76  return p.set<T>(name, value);
77 }
78 
80  static PyObject* convert(LorentzVector const& s) {
81  bp::list result;
82  result.append(s.X());
83  result.append(s.Y());
84  result.append(s.Z());
85  result.append(s.T());
86 
87  return boost::python::incref(result.ptr());
88  }
89 };
90 
93  bp::converter::registry::push_back(
94  &convertible,
95  &construct,
96  boost::python::type_id<LorentzVector>());
97  }
98 
99  static void* convertible(PyObject* obj_ptr) {
100  if (! PyList_Check(obj_ptr))
101  return nullptr;
102 
103  if (PyList_Size(obj_ptr) != 4)
104  return nullptr;
105 
106  return obj_ptr;
107  }
108 
109  static void construct(
110  PyObject* obj_ptr,
111  bp::converter::rvalue_from_python_stage1_data* data) {
112  //if (value == 0) boost::python::throw_error_already_set();
113 
114  void* storage = (
115  (bp::converter::rvalue_from_python_storage<LorentzVector>*)
116  data)->storage.bytes;
117  new (storage) LorentzVector(
118  bp::extract<double>(PyList_GET_ITEM(obj_ptr, 0)),
119  bp::extract<double>(PyList_GET_ITEM(obj_ptr, 1)),
120  bp::extract<double>(PyList_GET_ITEM(obj_ptr, 2)),
121  bp::extract<double>(PyList_GET_ITEM(obj_ptr, 3))
122  );
123  data->convertible = storage;
124  }
125 };
126 
127 #ifdef ROOT_HAS_PYROOT
128 template <typename T>
129 struct convert_py_root_to_cpp_root {
130  convert_py_root_to_cpp_root() {
131  bp::converter::registry::push_back(&convertible, &construct,
132  bp::type_id<T>());
133  }
134  static void* convertible(PyObject* obj_ptr) {
135  return TPython::ObjectProxy_Check(obj_ptr) ? obj_ptr : nullptr;
136  }
137 
138  static void construct(PyObject* obj_ptr,
139  bp::converter::rvalue_from_python_stage1_data* data) {
140  T* TObj = static_cast<T*>(TPython::ObjectProxy_AsVoidPtr(obj_ptr));
141  data->convertible = TObj;
142  }
143 };
144 #endif
145 
146 // Overloads for MoMEMta::computeWeights
147 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(MoMEMta_computeWeights_overloads, MoMEMta::computeWeights, 1, 2)
148 
149 BOOST_PYTHON_MODULE(momemta) {
150 
151  using namespace boost::python;
152 
153  to_python_converter<LorentzVector, LorentzVector_to_python>();
155 
156 #ifdef ROOT_HAS_PYROOT
157  convert_py_root_to_cpp_root<LorentzVector>();
158 #endif
159 
160  enum_<logging::level::level_enum>("log_level")
161  .value("trace", logging::level::trace)
162  .value("debug", logging::level::debug)
163  .value("info", logging::level::info)
164  .value("warning", logging::level::warning)
165  .value("error", logging::level::error)
166  .value("fatal", logging::level::fatal)
167  .value("off", logging::level::off);
168 
169  def("set_log_level", set_log_level);
170 
171  class_<ParameterSet>("ParameterSet", no_init)
172  .def("exists", &ParameterSet::exists)
173  .def("getDouble", ParameterSet_get<double>, return_value_policy<copy_const_reference>())
174  .def("getInt", ParameterSet_get<int>, return_value_policy<copy_const_reference>())
175  .def("getString", ParameterSet_get<std::string>, return_value_policy<copy_const_reference>())
176  .def("getInputTag", ParameterSet_get<InputTag>, return_value_policy<copy_const_reference>())
177  .def("getParameterSet", ParameterSet_get<ParameterSet>, return_value_policy<copy_const_reference>())
178  .def("setDouble", ParameterSet_set<double>)
179  .def("setInt", ParameterSet_set<int>)
180  .def("setString", ParameterSet_set<std::string>)
181  .def("setInputTag", ParameterSet_set<InputTag>);
182 
183  class_<Configuration>("Configuration", no_init)
184  .def("getGlobalParameters", &Configuration::getGlobalParameters,
185  return_internal_reference<>())
186  .def("getCubaConfiguration", &Configuration::getCubaConfiguration,
187  return_internal_reference<>());
188 
189  class_<ConfigurationReader>("ConfigurationReader", init<std::string>())
190  .def("freeze", &ConfigurationReader::freeze)
191  .def("getGlobalParameters", &ConfigurationReader::getGlobalParameters,
192  return_value_policy<reference_existing_object>())
193  .def("getCubaConfiguration", &ConfigurationReader::getCubaConfiguration,
194  return_value_policy<reference_existing_object>());
195 
196  enum_<MoMEMta::IntegrationStatus>("IntegrationStatus")
197  .value("ABORTED", MoMEMta::IntegrationStatus::ABORTED)
198  .value("ACCURACY_NOT_REACHED", MoMEMta::IntegrationStatus::ACCURACY_NOT_REACHED)
199  .value("DIM_OUT_OF_RANGE", MoMEMta::IntegrationStatus::DIM_OUT_OF_RANGE)
200  .value("FAILED", MoMEMta::IntegrationStatus::FAILED)
201  .value("NONE", MoMEMta::IntegrationStatus::NONE)
202  .value("SUCCESS", MoMEMta::IntegrationStatus::SUCCESS);
203 
204  class_<Particle>("Particle", init<std::string>())
205  .def(init<std::string, LorentzVector>())
206  .def(init<std::string, LorentzVector, int64_t>())
207  .def_readonly("name", &Particle::name)
208  .add_property("p4", make_getter(&Particle::p4, return_value_policy<return_by_value>()), &Particle::p4)
209  .def_readwrite("type", &Particle::type);
210 
211  class_<MoMEMta>("MoMEMta", init<Configuration>())
212  .def("getIntegrationStatus", &MoMEMta::getIntegrationStatus)
213  //.def("getPool", &MoMEMta::getPool, return_value_policy<copy_const_reference>())
214  .def("computeWeights", MoMEMta_computeWeights)
215  .def("computeWeights", MoMEMta_computeWeights_MET)
216  .def("computeWeights", &MoMEMta::computeWeights, MoMEMta_computeWeights_overloads());
217 }
Configuration freeze() const
Freeze the configuration.
const ParameterSet & getGlobalParameters() const
A MoMEMta instance.
Definition: MoMEMta.h:44
IntegrationStatus getIntegrationStatus() const
Return the status of the integration.
Definition: MoMEMta.cc:467
std::enable_if< std::is_same< T, bool >::value||std::is_same< T, InputTag >::value >::type set(const std::string &name, const T &value)
Change the value of a given parameter. If the parameter does not exist, it&#39;s first created...
Definition: ParameterSet.h:160
Definition: Graph.h:21
No integration was performed.
const ParameterSet & getCubaConfiguration() const
A class encapsulating a lua table.
Definition: ParameterSet.h:82
Integration was successful.
Integration was stopped before desired accuracy was reached.
std::vector< std::pair< double, double > > computeWeights(const std::vector< momemta::Particle > &particles, const LorentzVector &met=LorentzVector())
Compute the weights in the current configuration.
Definition: MoMEMta.cc:157