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 void MoMEMta_setEvent_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 void MoMEMta_setEvent(
MoMEMta& m, bp::list particles_) {
58 MoMEMta_setEvent_MET(m, particles_, bp::list());
61 bp::list MoMEMta_evaluateIntegrand(
MoMEMta& m, bp::list psPoint_) {
62 std::vector<double> psPoint;
63 for (ssize_t i = 0; i < bp::len(psPoint_); i++) {
64 psPoint.push_back(bp::extract<double>(psPoint_[i]));
70 for (
const auto& integrand: integrands) {
71 result.append(integrand);
77 bp::list MoMEMta_computeWeights_MET(
MoMEMta& m, bp::list particles_, bp::list met_) {
78 std::vector<Particle> particles;
79 for (ssize_t i = 0; i < bp::len(particles_); i++) {
80 particles.push_back(bp::extract<Particle>(particles_[i]));
84 bp::extract<LorentzVector> lorentzVectorExtractor(met_);
85 if (lorentzVectorExtractor.check())
86 met = lorentzVectorExtractor();
91 for (
const auto& weight: weights) {
92 bp::tuple pair = bp::make_tuple(weight.first, weight.second);
99 bp::list MoMEMta_computeWeights(
MoMEMta& m, bp::list particles) {
100 return MoMEMta_computeWeights_MET(m, particles, bp::list());
104 const T& ParameterSet_get(
ParameterSet& p,
const std::string& name) {
105 return p.get<T>(name);
109 void ParameterSet_set(
ParameterSet& p,
const std::string& name,
const T& value) {
110 return p.
set<T>(name, value);
114 static PyObject* convert(LorentzVector
const& s) {
116 result.append(s.X());
117 result.append(s.Y());
118 result.append(s.Z());
119 result.append(s.T());
121 return boost::python::incref(result.ptr());
127 bp::converter::registry::push_back(
130 boost::python::type_id<LorentzVector>());
133 static void* convertible(PyObject* obj_ptr) {
134 if (! PyList_Check(obj_ptr))
137 if (PyList_Size(obj_ptr) != 4)
143 static void construct(
145 bp::converter::rvalue_from_python_stage1_data* data) {
149 (bp::converter::rvalue_from_python_storage<LorentzVector>*)
150 data)->storage.bytes;
151 new (storage) LorentzVector(
152 bp::extract<double>(PyList_GET_ITEM(obj_ptr, 0)),
153 bp::extract<double>(PyList_GET_ITEM(obj_ptr, 1)),
154 bp::extract<double>(PyList_GET_ITEM(obj_ptr, 2)),
155 bp::extract<double>(PyList_GET_ITEM(obj_ptr, 3))
157 data->convertible = storage;
161 #ifdef ROOT_HAS_PYROOT 162 template <
typename T>
163 struct convert_py_root_to_cpp_root {
164 convert_py_root_to_cpp_root() {
165 bp::converter::registry::push_back(&convertible, &construct,
168 static void* convertible(PyObject* obj_ptr) {
169 return TPython::ObjectProxy_Check(obj_ptr) ? obj_ptr :
nullptr;
172 static void construct(PyObject* obj_ptr,
173 bp::converter::rvalue_from_python_stage1_data* data) {
174 T* TObj =
static_cast<T*
>(TPython::ObjectProxy_AsVoidPtr(obj_ptr));
175 data->convertible = TObj;
184 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(MoMEMta_setEvent_overloads,
MoMEMta::setEvent, 1, 2)
188 using namespace boost::python;
190 to_python_converter<LorentzVector, LorentzVector_to_python>();
193 #ifdef ROOT_HAS_PYROOT 194 convert_py_root_to_cpp_root<LorentzVector>();
197 enum_<logging::level::level_enum>(
"log_level")
198 .value(
"trace", logging::level::trace)
199 .value(
"debug", logging::level::debug)
200 .value(
"info", logging::level::info)
201 .value(
"warning", logging::level::warning)
202 .value(
"error", logging::level::error)
203 .value(
"fatal", logging::level::fatal)
204 .value(
"off", logging::level::off);
206 def(
"set_log_level", set_log_level);
208 class_<ParameterSet>(
"ParameterSet", no_init)
209 .def(
"exists", &ParameterSet::exists)
210 .def(
"getDouble", ParameterSet_get<double>, return_value_policy<copy_const_reference>())
211 .def(
"getInt", ParameterSet_get<int>, return_value_policy<copy_const_reference>())
212 .def(
"getString", ParameterSet_get<std::string>, return_value_policy<copy_const_reference>())
213 .def(
"getInputTag", ParameterSet_get<InputTag>, return_value_policy<copy_const_reference>())
214 .def(
"getParameterSet", ParameterSet_get<ParameterSet>, return_value_policy<copy_const_reference>())
215 .def(
"setDouble", ParameterSet_set<double>)
216 .def(
"setInt", ParameterSet_set<int>)
217 .def(
"setString", ParameterSet_set<std::string>)
218 .def(
"setInputTag", ParameterSet_set<InputTag>);
220 class_<Configuration>(
"Configuration", no_init)
222 return_internal_reference<>())
224 return_internal_reference<>());
226 class_<ConfigurationReader>(
"ConfigurationReader", init<std::string>())
228 .def(
"getGlobalParameters", &ConfigurationReader::getGlobalParameters,
229 return_value_policy<reference_existing_object>())
230 .def(
"getCubaConfiguration", &ConfigurationReader::getCubaConfiguration,
231 return_value_policy<reference_existing_object>());
233 enum_<MoMEMta::IntegrationStatus>(
"IntegrationStatus")
241 class_<Particle>(
"Particle", init<std::string>())
242 .def(init<std::string, LorentzVector>())
243 .def(init<std::string, LorentzVector, int64_t>())
244 .def_readonly(
"name", &Particle::name)
245 .add_property(
"p4", make_getter(&Particle::p4, return_value_policy<return_by_value>()), &Particle::p4)
246 .def_readwrite(
"type", &Particle::type);
248 class_<MoMEMta>(
"MoMEMta", init<Configuration>())
251 .def(
"computeWeights", MoMEMta_computeWeights)
252 .def(
"computeWeights", MoMEMta_computeWeights_MET)
254 .def(
"setEvent", MoMEMta_setEvent)
255 .def(
"setEvent", MoMEMta_setEvent_MET)
257 .def(
"evaluateIntegrand", MoMEMta_evaluateIntegrand);
Configuration freeze() const
Freeze the configuration.
const ParameterSet & getGlobalParameters() const
IntegrationStatus getIntegrationStatus() const
Return the status of the integration.
std::vector< double > evaluateIntegrand(const std::vector< double > &psPoints)
Evaluate the integrand on a single phase-space point.
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.
void setEvent(const std::vector< momemta::Particle > &particles, const LorentzVector &met=LorentzVector())
Set the event particles' momenta.
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.