cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
N/A
Central exclusive processes event generator
Toggle main menu visibility
Main Page
Related Pages
Packages
Package List
Package Members
All
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
x
y
Functions
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
x
y
Variables
Typedefs
Enumerations
Classes
Class List
Class Index
Class Hierarchy
Class Members
All
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Functions
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Variables
a
b
c
d
e
f
g
h
i
k
l
m
n
p
q
r
s
t
u
v
w
x
y
z
Typedefs
Enumerations
Enumerator
b
c
d
e
g
h
i
l
m
n
p
r
t
u
w
x
y
z
Related Symbols
d
g
h
o
u
v
Files
File List
File Members
All
_
a
b
c
d
e
h
o
p
r
s
Functions
Variables
Macros
_
b
c
d
e
p
r
s
▼
CepGen
Reference manual
Bibliography
►
Packages
►
Classes
▼
Files
▼
File List
▼
include
►
CepGen
▼
CepGenBoost
►
BoostTreeUtils.h
►
PythonObjectsWrappers.h
►
PythonUtils.h
►
CepGenHepMC2
►
CepGenHepMC3
►
CepGenHerwig6
►
CepGenMadGraph
►
CepGenPythia6
►
CepGenPythia8
►
CepGenPython
►
CepGenRoot
►
File Members
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
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-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()); })
25
#define EXPOSE_FACTORY(obj, key, name, description) \
…
40
41
namespace
cepgen
{
42
class
ParametersList;
43
}
44
45
namespace
py = boost::python;
46
47
template
<
class
T>
48
py::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
}
48
py::list
std_vector_to_py_list
(
const
std::vector<T>& vec) {
…
}
53
54
template
<
class
T>
55
std::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
}
55
std::vector<T>
py_tuple_to_std_vector
(
const
py::tuple& tuple) {
…
}
61
62
template
<
class
T>
63
std::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
}
63
std::vector<T>
py_list_to_std_vector
(
const
py::list& list) {
…
}
69
70
template
<
class
T>
71
py::tuple
std_vector_to_py_tuple
(
const
std::vector<T>& vec) {
72
return
py::tuple(
std_vector_to_py_list
(vec));
73
}
71
py::tuple
std_vector_to_py_tuple
(
const
std::vector<T>& vec) {
…
}
74
75
cepgen::ParametersList
py_dict_to_plist
(
const
py::dict&);
76
py::dict
plist_to_py_dict
(
const
cepgen::ParametersList
&);
77
78
template
<
typename
T,
typename
... Args>
79
py::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
}
79
py::object
adapt_unique
(std::unique_ptr<T> (*fn)(Args...)) {
…
}
84
85
template
<
typename
T,
typename
C
,
typename
... Args>
86
py::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
}
86
py::object
adapt_unique
(std::unique_ptr<T> (C::*fn)(Args...)) {
…
}
91
92
template
<
typename
T>
93
py::object
adapt_reference
(T* ptr) {
94
typename
py::reference_existing_object::apply<T*>::type converter;
95
return
py::object(py::handle(converter(ptr)));
96
}
93
py::object
adapt_reference
(T* ptr) {
…
}
97
98
#endif
py_dict_to_plist
cepgen::ParametersList py_dict_to_plist(const py::dict &)
py_list_to_std_vector
std::vector< T > py_list_to_std_vector(const py::list &list)
Definition
PythonUtils.h:63
plist_to_py_dict
py::dict plist_to_py_dict(const cepgen::ParametersList &)
adapt_reference
py::object adapt_reference(T *ptr)
Definition
PythonUtils.h:93
py_tuple_to_std_vector
std::vector< T > py_tuple_to_std_vector(const py::tuple &tuple)
Definition
PythonUtils.h:55
std_vector_to_py_tuple
py::tuple std_vector_to_py_tuple(const std::vector< T > &vec)
Definition
PythonUtils.h:71
adapt_unique
py::object adapt_unique(std::unique_ptr< T >(*fn)(Args...))
Definition
PythonUtils.h:79
std_vector_to_py_list
py::list std_vector_to_py_list(const std::vector< T > &vec)
Definition
PythonUtils.h:48
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen
Common namespace for this Monte Carlo generator.
Definition
Handler.h:26
cepgen::Element::C
@ C
carbon
include
CepGenBoost
PythonUtils.h
Generated on Tue Apr 22 2025 for CepGen by
1.10.0