cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
PythonUtils.cpp
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
22
25 for (auto it = py::stl_input_iterator<py::tuple>(dict.attr("items")()); it != py::stl_input_iterator<py::tuple>();
26 ++it) {
27 py::tuple kv = *it;
28 const std::string key{py::extract<const char*>(py::str(kv[0]))};
29 py::extract<py::object> val_ext(kv[1]);
30 const std::string val_type{py::extract<const char*>(val_ext().attr("__class__").attr("__name__"))};
31 if (val_type == "int")
32 plist.set<int>(key, py::extract<int>(val_ext()));
33 else if (val_type == "str")
34 plist.set<std::string>(key, std::string{py::extract<const char*>(val_ext())});
35 else if (val_type == "float")
36 plist.set<double>(key, py::extract<double>(val_ext()));
37 else if (val_type == "dict")
38 plist.set<cepgen::ParametersList>(key, py_dict_to_plist(py::extract<py::dict>(val_ext())));
39 else if (val_type == "tuple") {
40 const py::tuple& tuple = py::extract<py::tuple>(val_ext());
41 const std::string el_type{py::extract<const char*>(tuple[0].attr("__class__").attr("__name__"))};
42 if (el_type == "float") {
43 const auto items = py_tuple_to_std_vector<double>(py::extract<py::tuple>(val_ext()));
44 if (items.size() == 2)
45 plist.set<cepgen::Limits>(key, cepgen::Limits(items.at(0), items.at(1)));
46 } else
47 throw CG_FATAL("py_dict_to_plist")
48 << "Tuple unpacking is not (yet) handling the Python '" << val_type << "' type for key='" << key << "'.";
49 } else if (val_type == "list") {
50 // find the type of list from its first element
51 const py::list& list = py::extract<py::list>(val_ext());
52 const std::string el_type{py::extract<const char*>(list[0].attr("__class__").attr("__name__"))};
53 if (el_type == "int")
54 plist.set(key, py_list_to_std_vector<int>(py::extract<py::list>(val_ext())));
55 else if (el_type == "str")
56 plist.set(key, py_list_to_std_vector<std::string>(py::extract<py::list>(val_ext())));
57 else if (el_type == "float")
58 plist.set(key, py_list_to_std_vector<double>(py::extract<py::list>(val_ext())));
59 else if (el_type == "dict")
60 plist.set(key, py_list_to_std_vector<cepgen::ParametersList>(py::extract<py::list>(val_ext())));
61 else
62 throw CG_FATAL("py_dict_to_plist")
63 << "Failed to unpack a Python list for elements of '" << val_type << "' type.";
64 } else
65 throw CG_FATAL("py_dict_to_plist") << "Failed to unpack a Python '" << val_type << "' type for key='" << key
66 << "'.";
67 }
68 return plist;
69}
70
72 py::dict dict;
73 for (const auto& key : plist.keysOf<int>())
74 dict.setdefault<std::string, int>(key, plist.get<int>(key));
75 for (const auto& key : plist.keysOf<std::string>())
76 dict.setdefault<std::string, std::string>(key, plist.get<std::string>(key));
77 for (const auto& key : plist.keysOf<double>())
78 dict.setdefault<std::string, double>(key, plist.get<double>(key));
79 for (const auto& key : plist.keysOf<cepgen::Limits>()) {
80 const auto& lim = plist.get<cepgen::Limits>(key);
81 dict.setdefault<std::string, py::tuple>(key, std_vector_to_py_tuple(std::vector<double>(lim.min(), lim.max())));
82 }
83 for (const auto& key : plist.keysOf<cepgen::ParametersList>())
84 dict.setdefault<std::string, py::dict>(key, plist_to_py_dict(plist.get<cepgen::ParametersList>(key)));
85 for (const auto& key : plist.keysOf<std::vector<int> >())
86 dict.setdefault<std::string, py::list>(key, std_vector_to_py_list(plist.get<std::vector<int> >(key)));
87 for (const auto& key : plist.keysOf<std::vector<std::string> >())
88 dict.setdefault<std::string, py::list>(key, std_vector_to_py_list(plist.get<std::vector<std::string> >(key)));
89 for (const auto& key : plist.keysOf<std::vector<double> >())
90 dict.setdefault<std::string, py::list>(key, std_vector_to_py_list(plist.get<std::vector<double> >(key)));
91 return dict;
92}
#define CG_FATAL(mod)
Definition Exception.h:61
py::dict plist_to_py_dict(const cepgen::ParametersList &plist)
cepgen::ParametersList py_dict_to_plist(const py::dict &dict)
py::tuple std_vector_to_py_tuple(const std::vector< T > &vec)
Definition PythonUtils.h:72
py::list std_vector_to_py_list(const std::vector< T > &vec)
Definition PythonUtils.h:49
Validity interval for a variable.
Definition Limits.h:28
T get(const std::string &key, const T &def=default_arg< T >::get()) const
Get a parameter value.
ParametersList & set(const std::string &, const T &)
Set a parameter value Set a recast parameter value.
std::vector< std::string > keysOf() const
List of keys for one type in this list of parameters List of keys handled in this list of parameters.