cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
FunctionalTinyExpr.cpp
Go to the documentation of this file.
1
/*
2
* CepGen: a central exclusive processes event generator
3
* Copyright (C) 2013-2022 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 <tinyexpr.h>
20
21
#include "
CepGen/Core/Exception.h
"
22
#include "
CepGen/Modules/FunctionalFactory.h
"
23
#include "
CepGen/Utils/Functional.h
"
24
#include "
CepGen/Utils/String.h
"
25
26
namespace
cepgen
{
27
namespace
utils {
28
class
FunctionalTinyExpr
final :
public
Functional
{
29
public
:
30
explicit
FunctionalTinyExpr
(
const
ParametersList
&);
31
double
eval
()
const override
;
32
33
static
ParametersDescription
description
();
34
35
private
:
36
struct
te_deleter {
37
void
operator()(te_expr* expr) { te_free(expr); }
38
};
39
std::unique_ptr<te_expr, te_deleter> eval_;
40
};
41
42
FunctionalTinyExpr::FunctionalTinyExpr
(
const
ParametersList
& params) :
Functional
(params) {
43
std::vector<te_variable> te_vars;
44
for
(
size_t
i = 0; i <
vars_
.size(); ++i)
45
te_vars.emplace_back(te_variable{vars_.at(i).c_str(), &values_.at(i), TE_VARIABLE, nullptr});
46
const
auto
expr =
utils::replaceAll
(
expression_
, {{
"**"
,
"^"
}});
47
int
error;
48
eval_.reset(te_compile(expr.c_str(), te_vars.data(),
vars_
.size(), &error));
49
if
(!eval_) {
50
const
std::string pre_syntax_err =
"A syntax error was detected in the expression \""
;
51
const
std::string postfix = (expr !=
expression_
) ?
" (adapted from \""
+
expression_
+
"\")"
:
""
;
52
throw
CG_ERROR
(
"FunctionalTinyExpr"
) <<
"Evaluator was not properly initialised.\n"
53
<< pre_syntax_err << expr <<
"\""
<< postfix <<
"\n"
54
<< std::string(pre_syntax_err.size() + error - 1,
' '
) +
"^"
;
55
}
56
}
57
58
double
FunctionalTinyExpr::eval
()
const
{
return
te_eval(eval_.get()); }
59
60
ParametersDescription
FunctionalTinyExpr::description
() {
61
auto
desc =
Functional::description
();
62
desc.setDescription(
"TinyExpr evaluator"
);
63
return
desc;
64
}
65
}
// namespace utils
66
}
// namespace cepgen
67
typedef
cepgen::utils::FunctionalTinyExpr
TinyExprFunctional
;
68
REGISTER_FUNCTIONAL
(
"tinyexpr"
,
TinyExprFunctional
);
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
TinyExprFunctional
cepgen::utils::FunctionalTinyExpr TinyExprFunctional
Definition
FunctionalTinyExpr.cpp:67
Functional.h
String.h
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::utils::FunctionalTinyExpr
Definition
FunctionalTinyExpr.cpp:28
cepgen::utils::FunctionalTinyExpr::eval
double eval() const override
Compute the functional for a given value of the variables.
Definition
FunctionalTinyExpr.cpp:58
cepgen::utils::FunctionalTinyExpr::FunctionalTinyExpr
FunctionalTinyExpr(const ParametersList &)
Definition
FunctionalTinyExpr.cpp:42
cepgen::utils::FunctionalTinyExpr::description
static ParametersDescription description()
Definition
FunctionalTinyExpr.cpp:60
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::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
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
CepGenAddOns
Functionals
FunctionalTinyExpr.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7