cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
STLRandomGenerator.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2023 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 <memory>
20#include <random>
21
25
26namespace cepgen {
28 public:
29 explicit STLRandomGenerator(const ParametersList& params) : utils::RandomGenerator(params) {
30 std::random_device rd;
31 const auto seed = seed_ > 0ull ? seed_ : rd();
32 const auto& type = steer<std::string>("type");
33 if (type == "default")
34 gen_.reset(new Generator<std::default_random_engine>(seed));
35 else if (type == "minstd_rand0")
36 gen_.reset(new Generator<std::minstd_rand0>(seed));
37 else if (type == "minstd_rand")
38 gen_.reset(new Generator<std::minstd_rand>(seed));
39 else if (type == "mt19937")
40 gen_.reset(new Generator<std::mt19937>(seed));
41 else if (type == "mt19937_64")
42 gen_.reset(new Generator<std::mt19937_64>(seed));
43 else if (type == "ranlux24_base")
44 gen_.reset(new Generator<std::ranlux24_base>(seed));
45 else if (type == "ranlux48_base")
46 gen_.reset(new Generator<std::ranlux48_base>(seed));
47 else if (type == "ranlux24")
48 gen_.reset(new Generator<std::ranlux24>(seed));
49 else if (type == "ranlux48")
50 gen_.reset(new Generator<std::ranlux48>(seed));
51 else if (type == "knuth_b")
52 gen_.reset(new Generator<std::knuth_b>(seed));
53 else
54 throw CG_FATAL("STLRandomGenerator") << "Random number generator engine invalid: '" << type << "'.";
55
56 CG_DEBUG("STLRandomGenerator") << "Random numbers generator with seed: " << seed_ << ".";
57 }
58
61 desc.setDescription("STL random number generator engine");
62 desc.add<std::string>("type", "default")
63 .allow("default", "implementation-defined algorithm")
64 .allow("minstd_rand0",
65 "Discovered in 1969 by Lewis, Goodman and Miller, adopted as \"Minimal standard\" in 1988 by Park and "
66 "Miller")
67 .allow("minstd_rand", "Newer \"Minimum standard\", recommended by Park, Miller, and Stockmeyer in 1993")
68 .allow("mt19937", "32-bit Mersenne Twister by Matsumoto and Nishimura, 1998")
69 .allow("mt19937_64", "64-bit Mersenne Twister by Matsumoto and Nishimura, 2000")
70 .allow("ranlux24_base", "subtract-w/-carry algorithm (24, 10, 24)")
71 .allow("ranlux48_base", "subtract-w/-carry algorithm (48, 5, 12)")
72 .allow("ranlux24", "24-bit RANLUX generator by Martin Lüscher and Fred James, 1994")
73 .allow("ranlux48", "48-bit RANLUX generator by Martin Lüscher and Fred James, 1994")
74 .allow("knuth_b", "PRN engine adaptor discarding a certain amount of data produced by base engine (389, 11)")
75 .setDescription("random number engine");
76 return desc;
77 }
78
79 int uniformInt(int min, int max) override { return gen_->uniformInt(min, max); }
80 double uniform(double min, double max) override { return gen_->uniform(min, max); }
81 double normal(double mean, double rms) override { return gen_->normal(mean, rms); }
82 double exponential(double exponent) override { return gen_->exponential(exponent); }
83 double breitWigner(double mean, double scale) override { return gen_->breitWigner(mean, scale); }
84 int poisson(double mean) override { return gen_->poisson(mean); }
85
86 private:
87 template <typename T>
88 class Generator : public utils::RandomGenerator {
89 public:
90 explicit Generator(unsigned long int value) : utils::RandomGenerator(ParametersList()), rng_(value) {}
91 int uniformInt(int min, int max) override { return std::uniform_int_distribution<>(min, max)(rng_); }
92 double uniform(double min, double max) override { return std::uniform_real_distribution<>(min, max)(rng_); }
93 double normal(double mean, double rms) override { return std::normal_distribution<>(mean, rms)(rng_); }
94 double exponential(double exponent) override { return std::exponential_distribution<>(exponent)(rng_); }
95 double breitWigner(double mean, double scale) override { return std::cauchy_distribution<>(mean, scale)(rng_); }
96 int poisson(double mean) override { return std::poisson_distribution<>(mean)(rng_); }
97
98 private:
99 T rng_;
100 };
101
102 std::unique_ptr<utils::RandomGenerator> gen_;
103 };
104} // namespace cepgen
105
106REGISTER_RANDOM_GENERATOR("stl", STLRandomGenerator);
#define CG_FATAL(mod)
Definition Exception.h:61
#define CG_DEBUG(mod)
Definition Message.h:220
#define REGISTER_RANDOM_GENERATOR(name, obj)
Add a generic random number generator definition to the list of handled modules.
Core generator object allowing for process definition, cross section computation, and event generatio...
Definition Generator.h:48
A description object for parameters collection.
double normal(double mean, double rms) override
int poisson(double mean) override
static ParametersDescription description()
STLRandomGenerator(const ParametersList &params)
int uniformInt(int min, int max) override
double uniform(double min, double max) override
double exponential(double exponent) override
double breitWigner(double mean, double scale) override
A random number generator.
static ParametersDescription description()
RandomGenerator(const ParametersList &)
Default constructor.
bool uniform(const std::vector< T > &coll)
Check if all elements of a collection are uniform.
Definition Collections.h:72
Common namespace for this Monte Carlo generator.