19 #include <momemta/Configuration.h> 20 #include <momemta/ConfigurationReader.h> 21 #include <momemta/MoMEMta.h> 22 #include <momemta/Logging.h> 24 #include <boost/python.hpp> 26 #ifdef ROOT_HAS_PYROOT 36 namespace bp = boost::python;
39 void set_log_level(logging::level::level_enum lvl) {
40 logging::set_level(lvl);
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]));
50 bp::extract<LorentzVector> lorentzVectorExtractor(met_);
51 if (lorentzVectorExtractor.check())
52 met = lorentzVectorExtractor();
57 for (
const auto& weight: weights) {
58 bp::tuple pair = bp::make_tuple(weight.first, weight.second);
65 bp::list MoMEMta_computeWeights(
MoMEMta& m, bp::list particles) {
66 return MoMEMta_computeWeights_MET(m, particles, bp::list());
70 const T& ParameterSet_get(
ParameterSet& p,
const std::string& name) {
71 return p.get<T>(name);
75 void ParameterSet_set(
ParameterSet& p,
const std::string& name,
const T& value) {
76 return p.
set<T>(name, value);
80 static PyObject* convert(LorentzVector
const& s) {
87 return boost::python::incref(result.ptr());
93 bp::converter::registry::push_back(
96 boost::python::type_id<LorentzVector>());
99 static void* convertible(PyObject* obj_ptr) {
100 if (! PyList_Check(obj_ptr))
103 if (PyList_Size(obj_ptr) != 4)
109 static void construct(
111 bp::converter::rvalue_from_python_stage1_data* data) {
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))
123 data->convertible = storage;
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,
134 static void* convertible(PyObject* obj_ptr) {
135 return TPython::ObjectProxy_Check(obj_ptr) ? obj_ptr :
nullptr;
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;
151 using namespace boost::python;
153 to_python_converter<LorentzVector, LorentzVector_to_python>();
156 #ifdef ROOT_HAS_PYROOT 157 convert_py_root_to_cpp_root<LorentzVector>();
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);
169 def(
"set_log_level", set_log_level);
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>);
183 class_<Configuration>(
"Configuration", no_init)
185 return_internal_reference<>())
187 return_internal_reference<>());
189 class_<ConfigurationReader>(
"ConfigurationReader", init<std::string>())
191 .def(
"getGlobalParameters", &ConfigurationReader::getGlobalParameters,
192 return_value_policy<reference_existing_object>())
193 .def(
"getCubaConfiguration", &ConfigurationReader::getCubaConfiguration,
194 return_value_policy<reference_existing_object>());
196 enum_<MoMEMta::IntegrationStatus>(
"IntegrationStatus")
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);
211 class_<MoMEMta>(
"MoMEMta", init<Configuration>())
214 .def(
"computeWeights", MoMEMta_computeWeights)
215 .def(
"computeWeights", MoMEMta_computeWeights_MET)
Configuration freeze() const
Freeze the configuration.
const ParameterSet & getGlobalParameters() const
IntegrationStatus getIntegrationStatus() const
Return the status of the integration.
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's first created...
No integration was performed.
const ParameterSet & getCubaConfiguration() const
A class encapsulating a lua table.
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.