cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
PythonUtils.h
Go to the documentation of this file.
1
/*
2
* CepGen: a central exclusive processes event generator
3
* Copyright (C) 2023 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 CepGenAddOns_BoostWrapper_PythonUtils_h
20
#define CepGenAddOns_BoostWrapper_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( \
40
"modules", +[]() { return std_vector_to_py_list(obj::get().modules()); })
41
42
namespace
cepgen
{
43
class
ParametersList;
44
}
45
46
namespace
py = boost::python;
47
48
template
<
class
T>
49
py::list
std_vector_to_py_list
(
const
std::vector<T>& vec) {
50
py::list list;
51
std::for_each(vec.begin(), vec.end(), [&list](
const
auto
& t) { list.append(t); });
52
return
list;
53
}
54
55
template
<
class
T>
56
std::vector<T>
py_tuple_to_std_vector
(
const
py::tuple& tuple) {
57
std::vector<T> vec;
58
for
(ssize_t i = 0; i < py::len(tuple); ++i)
59
vec.emplace_back(py::extract<T>(tuple[i]));
60
return
vec;
61
}
62
63
template
<
class
T>
64
std::vector<T>
py_list_to_std_vector
(
const
py::list& list) {
65
std::vector<T> vec;
66
for
(ssize_t i = 0; i < py::len(list); ++i)
67
vec.emplace_back(py::extract<T>(list[i]));
68
return
vec;
69
}
70
71
template
<
class
T>
72
py::tuple
std_vector_to_py_tuple
(
const
std::vector<T>& vec) {
73
return
py::tuple(
std_vector_to_py_list
(vec));
74
}
75
76
cepgen::ParametersList
py_dict_to_plist
(
const
py::dict&);
77
py::dict
plist_to_py_dict
(
const
cepgen::ParametersList
&);
78
79
template
<
typename
T,
typename
... Args>
80
py::object
adapt_unique
(std::unique_ptr<T> (*fn)(Args...)) {
81
return
py::make_function([fn](Args... args) { return fn(args...).release(); },
82
py::return_value_policy<py::manage_new_object>(),
83
boost::mpl::vector<T*, Args...>());
84
}
85
86
template
<
typename
T,
typename
C
,
typename
... Args>
87
py::object
adapt_unique
(std::unique_ptr<T> (C::*fn)(Args...)) {
88
return
py::make_function([fn](C& self, Args... args) { return (self.*fn)(args...).release(); },
89
py::return_value_policy<py::manage_new_object>(),
90
boost::mpl::vector<T*, C&, Args...>());
91
}
92
93
template
<
typename
T>
94
inline
py::object
adapt_reference
(T* ptr) {
95
typename
py::reference_existing_object::apply<T*>::type converter;
96
return
py::object(py::handle(converter(ptr)));
97
}
98
99
#endif
100
py_dict_to_plist
cepgen::ParametersList py_dict_to_plist(const py::dict &)
Definition
PythonUtils.cpp:23
py_list_to_std_vector
std::vector< T > py_list_to_std_vector(const py::list &list)
Definition
PythonUtils.h:64
plist_to_py_dict
py::dict plist_to_py_dict(const cepgen::ParametersList &)
Definition
PythonUtils.cpp:71
adapt_reference
py::object adapt_reference(T *ptr)
Definition
PythonUtils.h:94
py_tuple_to_std_vector
std::vector< T > py_tuple_to_std_vector(const py::tuple &tuple)
Definition
PythonUtils.h:56
std_vector_to_py_tuple
py::tuple std_vector_to_py_tuple(const std::vector< T > &vec)
Definition
PythonUtils.h:72
adapt_unique
py::object adapt_unique(std::unique_ptr< T >(*fn)(Args...))
Definition
PythonUtils.h:80
std_vector_to_py_list
py::list std_vector_to_py_list(const std::vector< T > &vec)
Definition
PythonUtils.h:49
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
cepgen::Element::C
@ C
CepGenAddOns
BoostWrapper
PythonUtils.h
Generated on Mon Jul 29 2024 for CepGen by
1.9.7