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
19
#include "
CepGen/Core/Exception.h
"
20
#include "
CepGen/Modules/FunctionalFactory.h
"
21
#include "
CepGen/Utils/Functional.h
"
22
#include "
CepGen/Utils/String.h
"
23
#include "
CepGenAddOns/PythonWrapper/Environment.h
"
24
#include "
CepGenAddOns/PythonWrapper/Error.h
"
25
#include "
CepGenAddOns/PythonWrapper/Utils.h
"
26
27
using namespace
std::string_literals;
28
29
namespace
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
{
49
auto
args =
ObjectPtr::tupleFromVector
(
values_
);
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
61
inline
static
ParametersDescription
description
() {
62
auto
desc =
utils::Functional::description
();
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
:
71
Environment
env_{
ParametersList
()};
72
ObjectPtr mod_{
nullptr
};
73
ObjectPtr func_{
nullptr
};
74
};
75
}
// namespace python
76
}
// namespace cepgen
77
using
PythonFunctional
=
cepgen::python::Functional
;
78
REGISTER_FUNCTIONAL
(
"python"
,
PythonFunctional
);
Error.h
PY_ERROR
#define PY_ERROR
Definition
Error.h:25
Exception.h
CG_ERROR
#define CG_ERROR(mod)
Definition
Exception.h:60
FunctionalFactory.h
REGISTER_FUNCTIONAL
#define REGISTER_FUNCTIONAL(name, obj)
Add a generic functional object builder definition.
Definition
FunctionalFactory.h:25
Functional.h
CG_DEBUG
#define CG_DEBUG(mod)
Definition
Message.h:220
String.h
cepgen::LoggedMessage::message
std::string message() const
Human-readable message.
Definition
Message.h:158
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::python::Environment
Definition
Environment.h:28
cepgen::python::Error
Definition
Error.h:29
cepgen::python::Functional
Definition
Functional.cpp:31
cepgen::python::Functional::eval
double eval() const
Compute the functional for a given value of the variables.
Definition
Functional.cpp:48
cepgen::python::Functional::description
static ParametersDescription description()
Definition
Functional.cpp:61
cepgen::python::Functional::Functional
Functional(const ParametersList ¶ms)
Definition
Functional.cpp:33
cepgen::python::ObjectPtr::attribute
ObjectPtr attribute(const std::string &) const
Retrieve the attribute from a python object.
Definition
ObjectPtr.cpp:368
cepgen::python::ObjectPtr::defineModule
static ObjectPtr defineModule(const std::string &, const std::string &)
Define a Python module from a Python code in a new reference-counted Python object.
Definition
ObjectPtr.cpp:378
cepgen::python::ObjectPtr::call
ObjectPtr call(const ObjectPtr &) const
Call a python function with a tuple of arguments.
Definition
ObjectPtr.cpp:364
cepgen::python::ObjectPtr::tupleFromVector
static ObjectPtr tupleFromVector(const std::vector< T > &)
Build a Python tuple from a (uniform) vector of objects.
Definition
ObjectPtr.cpp:336
cepgen::utils::Functional
A string-to-functional parser.
Definition
Functional.h:32
cepgen::utils::Functional::vars_
std::vector< std::string > vars_
Computer-readable variable to be reached.
Definition
Functional.h:62
cepgen::utils::Functional::expression_
std::string expression_
Computer-readable expression.
Definition
Functional.h:63
cepgen::utils::Functional::description
static ParametersDescription description()
Definition
Functional.cpp:57
cepgen::utils::Functional::values_
std::vector< double > values_
Last arguments list fed to the functional.
Definition
Functional.h:64
Environment.h
Utils.h
cepgen::utils::replaceAll
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
cepgen::utils::merge
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
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
python
Definition
__init__.py:1
CepGenAddOns
PythonWrapper
Functional.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7