cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Functional.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2022-2024 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#include "CepGen/Utils/String.h"
26
27using namespace std::string_literals;
28
29namespace cepgen {
30 namespace python {
31 class Functional final : public utils::Functional {
32 public:
33 explicit Functional(const ParametersList& params) : utils::Functional(params) {
34 const auto cmd = "from math import *\n"s + "def " + steer<std::string>("functionName") + "("s +
35 utils::merge(vars_, ",") + ") -> float:\n" + "\treturn " +
36 utils::replaceAll(expression_, {{"^", "**"}}) + "\n";
37 CG_DEBUG("python:Functional") << "Will compile Python expression:\n" << cmd;
38 mod_ = ObjectPtr::defineModule("functional", cmd);
39 try {
40 if (func_ = mod_.attribute(steer<std::string>("functionName")); !func_ || !PyCallable_Check(func_.get()))
41 throw PY_ERROR << "Failed to retrieve/cast the object to a Python functional.";
42 } catch (const Error& err) {
43 throw CG_ERROR("python:Functional")
44 << "Failed to initialise the Python functional with \"" << expression_ << "\".\n"
45 << err.message();
46 }
47 }
48 inline double eval() const {
50 try {
51 if (auto value = func_.call(args); value)
52 return value.value<double>();
53 throw PY_ERROR;
54 } catch (const Error& err) {
55 throw CG_ERROR("python:Functional:eval")
56 << "Failed to call the function with arguments=" << args.vector<double>() << ".\n"
57 << err.message();
58 }
59 }
60
63 desc.setDescription("Python mathematical expression evaluator");
64 desc.add<std::string>("functionName", "custom_functional")
65 .setDescription(
66 "Python function name (in case multiple instance have to be declared in a same environment)");
67 return desc;
68 }
69
70 private:
72 ObjectPtr mod_{nullptr};
73 ObjectPtr func_{nullptr};
74 };
75 } // namespace python
76} // namespace cepgen
#define PY_ERROR
Definition Error.h:25
#define CG_ERROR(mod)
Definition Exception.h:60
#define REGISTER_FUNCTIONAL(name, obj)
Add a generic functional object builder definition.
#define CG_DEBUG(mod)
Definition Message.h:220
std::string message() const
Human-readable message.
Definition Message.h:158
A description object for parameters collection.
double eval() const
Compute the functional for a given value of the variables.
static ParametersDescription description()
Functional(const ParametersList &params)
ObjectPtr attribute(const std::string &) const
Retrieve the attribute from a python object.
static ObjectPtr defineModule(const std::string &, const std::string &)
Define a Python module from a Python code in a new reference-counted Python object.
ObjectPtr call(const ObjectPtr &) const
Call a python function with a tuple of arguments.
static ObjectPtr tupleFromVector(const std::vector< T > &)
Build a Python tuple from a (uniform) vector of objects.
A string-to-functional parser.
Definition Functional.h:32
std::vector< std::string > vars_
Computer-readable variable to be reached.
Definition Functional.h:62
std::string expression_
Computer-readable expression.
Definition Functional.h:63
static ParametersDescription description()
std::vector< double > values_
Last arguments list fed to the functional.
Definition Functional.h:64
size_t replaceAll(std::string &str, const std::string &from, const std::string &to)
Replace all occurrences of a text by another.
Definition String.cpp:118
std::string merge(const std::vector< T > &vec, const std::string &delim)
Merge a collection of a printable type in a single string.
Definition String.cpp:248
Common namespace for this Monte Carlo generator.