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) 2013-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 <TFormula.h>
20
24#include "CepGen/Utils/String.h"
25
26namespace cepgen {
27 namespace root {
30 public:
31 explicit Functional(const ParametersList& params) : cepgen::utils::Functional(params) {
32 for (auto& var : vars_)
33 func_.AddVariable(var, 0.);
34 auto expr = expression_;
35 expr = utils::replaceAll(expr, {{"min(", "TMath::Min("}, {"max(", "TMath::Max("}});
36 if (func_.Compile(expr.c_str()) != 0)
37 throw CG_ERROR("root:Functional") << "Failed to define the function\n\t" << expression_;
38 CG_DEBUG("root:Functional") << "Successfully defined a dimension-" << vars_.size()
39 << " function with arguments " << vars_ << ": " << expr << ".";
40 }
41 inline double eval() const override {
42 if (!func_.IsValid())
43 throw CG_WARNING("root:Functional") << "Cannot evaluate the invalid function at " << values_ << ".";
44 return func_.EvalPar(values_.data());
45 }
46
49 desc.setDescription("Plain old TFormula evaluator from ROOT");
50 return desc;
51 }
52
53 private:
54 TFormula func_;
55 };
56 } // namespace root
57} // namespace cepgen
#define CG_ERROR(mod)
Definition Exception.h:60
#define REGISTER_FUNCTIONAL(name, obj)
Add a generic functional object builder definition.
#define CG_WARNING(mod)
Definition Message.h:228
#define CG_DEBUG(mod)
Definition Message.h:220
A description object for parameters collection.
Functional evaluator defined from a ROOT TFormula.
double eval() const override
Compute the functional for a given value of the variables.
static ParametersDescription description()
Functional(const ParametersList &params)
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
Common namespace for this Monte Carlo generator.