cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Integrator.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 <Math/Integrator.h>
20#include <Math/IntegratorMultiDim.h>
21
26
27namespace cepgen {
28 namespace root {
30 class Integrator final : public cepgen::Integrator {
31 public:
32 explicit Integrator(const ParametersList& params)
33 : cepgen::Integrator(params),
34 type_(steer<std::string>("type")),
35 absTol_(steer<double>("absTol")),
36 relTol_(steer<double>("relTol")),
37 size_(steer<int>("size")) {
38 {
39 auto type = ROOT::Math::IntegratorMultiDim::Type::kDEFAULT;
40 if (type_ == "adaptive")
41 type = ROOT::Math::IntegratorMultiDim::Type::kADAPTIVE;
42 else if (type_ == "plain")
43 type = ROOT::Math::IntegratorMultiDim::Type::kPLAIN;
44 else if (type_ == "miser")
45 type = ROOT::Math::IntegratorMultiDim::Type::kMISER;
46 else if (type_ == "vegas")
47 type = ROOT::Math::IntegratorMultiDim::Type::kVEGAS;
48 integr_.reset(new ROOT::Math::IntegratorMultiDim(type, absTol_, relTol_, size_));
49 }
50 {
51 auto type = ROOT::Math::IntegratorOneDim::Type::kDEFAULT;
52 if (type_ == "gauss")
53 type = ROOT::Math::IntegratorOneDim::Type::kGAUSS;
54 else if (type_ == "legendre")
55 type = ROOT::Math::IntegratorOneDim::Type::kLEGENDRE;
56 else if (type_ == "adaptive")
57 type = ROOT::Math::IntegratorOneDim::Type::kADAPTIVE;
58 else if (type_ == "adaptiveSingular")
59 type = ROOT::Math::IntegratorOneDim::Type::kADAPTIVESINGULAR;
60 else if (type_ == "nonAdaptive")
61 type = ROOT::Math::IntegratorOneDim::Type::kNONADAPTIVE;
62 integr_1d_.reset(new ROOT::Math::IntegratorOneDim(type, absTol_, relTol_, size_));
63 }
64 //--- a bit of printout for debugging
65 CG_DEBUG("Integrator:build") << "ROOT generic integrator built\n\t"
66 << "N-dimensional type: " << integr_->Name() << ",\n\t"
67 << "1-dimensional type: " << integr_1d_->Name() << ",\n\t"
68 << "Absolute tolerance: " << absTol_ << ",\n\t"
69 << "Relative tolerance: " << relTol_ << ",\n\t"
70 << "Number of sub-intervals: " << size_ << ".";
71 }
72
75 desc.setDescription("ROOT general purpose MC integrator");
76 desc.add<std::string>("type", "default");
77 desc.add<double>("absTol", -1.);
78 desc.add<double>("relTol", -1.);
79 desc.add<int>("size", 0);
80 return desc;
81 }
82
83 void setLimits(const std::vector<Limits>& lims) override {
85 xlow_.clear();
86 xhigh_.clear();
87 for (const auto& lim : limits_) {
88 xlow_.emplace_back(lim.min());
89 xhigh_.emplace_back(lim.max());
90 }
91 }
92 Value integrate(Integrand& integrand) override {
93 checkLimits(integrand);
94
95 if (integrand.size() == 1) {
96 auto funct = [&](double x) -> double { return integrand.eval(std::vector<double>{x}); };
97 integr_1d_->SetFunction(funct);
98 return Value{integr_1d_->Integral(limits_.at(0).min(), limits_.at(0).max()), integr_1d_->Error()};
99 }
100 auto funct = [&](const double* x) -> double {
101 return integrand.eval(std::vector<double>(x, x + integrand.size()));
102 };
103 integr_->SetFunction(funct, integrand.size());
104 return Value{integr_->Integral(xlow_.data(), xhigh_.data()), integr_->Error()};
105 }
106
107 private:
108 const std::string type_;
109 const double absTol_;
110 const double relTol_;
111 const unsigned int size_;
112
113 std::vector<double> xlow_, xhigh_;
114 std::unique_ptr<ROOT::Math::IntegratorMultiDim> integr_;
115 std::unique_ptr<ROOT::Math::IntegratorOneDim> integr_1d_;
116 };
117 } // namespace root
118} // namespace cepgen
#define REGISTER_INTEGRATOR(name, obj)
Add a generic process definition to the list of handled processes.
#define CG_DEBUG(mod)
Definition Message.h:220
An integrand wrapper placeholder.
Definition Integrand.h:27
virtual double eval(const std::vector< double > &)=0
Compute the integrand for a given coordinates set.
virtual size_t size() const =0
Phase space dimension.
Monte-Carlo integration algorithm.
Definition Integrator.h:28
void checkLimits(const Integrand &)
Ensure the integration bounds are properly set.
virtual void setLimits(const std::vector< Limits > &limits)
Specify the variables limits on integration.
Definition Integrator.h:38
std::vector< Limits > limits_
List of per-variable integration limits.
Definition Integrator.h:58
static ParametersDescription description()
A description object for parameters collection.
T steer(const std::string &key) const
Retrieve a parameters as previously steered.
Definition Steerable.h:39
A scalar value with its uncertainty.
Definition Value.h:26
ROOT general-purpose integration algorithm.
Integrator(const ParametersList &params)
Value integrate(Integrand &integrand) override
Perform the multidimensional Monte Carlo integration.
static ParametersDescription description()
void setLimits(const std::vector< Limits > &lims) override
Specify the variables limits on integration.
Common namespace for this Monte Carlo generator.