cepgen is hosted by Hepforge, IPPP Durham
CepGen N/A
Central exclusive processes event generator
PythonUtils.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2023-2025 Laurent Forthomme
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 * 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#ifndef CepGenBoost_PythonUtils_h
20#define CepGenBoost_PythonUtils_h
21
22#include <boost/mpl/vector.hpp>
23#include <boost/python.hpp>
24
25#define EXPOSE_FACTORY(obj, key, name, description) \
26 py::class_<obj, boost::noncopyable>(name, description, py::no_init) \
27 .def("build", adapt_unique(+[](const key& mod) { return obj::get().build(mod); })) \
28 .def("build", adapt_unique(+[](const key& mod, const py::dict& dict) { \
29 return obj::get().build(mod, py_dict_to_plist(dict)); \
30 })) \
31 .def("build", adapt_unique(+[](const py::dict& dict) { return obj::get().build(py_dict_to_plist(dict)); })) \
32 .def( \
33 "describe", \
34 +[](const key& mod) { \
35 std::ostringstream os; \
36 os << obj::get().describeParameters(mod); \
37 return os.str(); \
38 }) \
39 .add_static_property("modules", +[]() { return std_vector_to_py_list(obj::get().modules()); })
40
41namespace cepgen {
42 class ParametersList;
43}
44
45namespace py = boost::python;
46
47template <class T>
48py::list std_vector_to_py_list(const std::vector<T>& vec) {
49 py::list list;
50 std::for_each(vec.begin(), vec.end(), [&list](const auto& t) { list.append(t); });
51 return list;
52}
53
54template <class T>
55std::vector<T> py_tuple_to_std_vector(const py::tuple& tuple) {
56 std::vector<T> vec;
57 for (ssize_t i = 0; i < len(tuple); ++i)
58 vec.emplace_back(py::extract<T>(tuple[i]));
59 return vec;
60}
61
62template <class T>
63std::vector<T> py_list_to_std_vector(const py::list& list) {
64 std::vector<T> vec;
65 for (ssize_t i = 0; i < len(list); ++i)
66 vec.emplace_back(py::extract<T>(list[i]));
67 return vec;
68}
69
70template <class T>
71py::tuple std_vector_to_py_tuple(const std::vector<T>& vec) {
72 return py::tuple(std_vector_to_py_list(vec));
73}
74
77
78template <typename T, typename... Args>
79py::object adapt_unique(std::unique_ptr<T> (*fn)(Args...)) {
80 return py::make_function([fn](Args... args) { return fn(args...).release(); },
81 py::return_value_policy<py::manage_new_object>(),
82 boost::mpl::vector<T*, Args...>());
83}
84
85template <typename T, typename C, typename... Args>
86py::object adapt_unique(std::unique_ptr<T> (C::*fn)(Args...)) {
87 return py::make_function([fn](C& self, Args... args) { return (self.*fn)(args...).release(); },
88 py::return_value_policy<py::manage_new_object>(),
89 boost::mpl::vector<T*, C&, Args...>());
90}
91
92template <typename T>
93py::object adapt_reference(T* ptr) {
94 typename py::reference_existing_object::apply<T*>::type converter;
95 return py::object(py::handle(converter(ptr)));
96}
97
98#endif
cepgen::ParametersList py_dict_to_plist(const py::dict &)
std::vector< T > py_list_to_std_vector(const py::list &list)
Definition PythonUtils.h:63
py::dict plist_to_py_dict(const cepgen::ParametersList &)
py::object adapt_reference(T *ptr)
Definition PythonUtils.h:93
std::vector< T > py_tuple_to_std_vector(const py::tuple &tuple)
Definition PythonUtils.h:55
py::tuple std_vector_to_py_tuple(const std::vector< T > &vec)
Definition PythonUtils.h:71
py::object adapt_unique(std::unique_ptr< T >(*fn)(Args...))
Definition PythonUtils.h:79
py::list std_vector_to_py_list(const std::vector< T > &vec)
Definition PythonUtils.h:48
Common namespace for this Monte Carlo generator.
Definition Handler.h:26