cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Limits.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2018-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 <cmath>
20
22#include "CepGen/Utils/Limits.h"
23#include "CepGen/Utils/String.h"
24
25namespace cepgen {
26 Limits::Limits(double min, double max) : std::pair<double, double>(min, max) {}
27
28 Limits::Limits(const Limits& rhs) : std::pair<double, double>(rhs.first, rhs.second) {}
29
30 Limits Limits::constant(double val) { return Limits{val, val}; }
31
32 bool Limits::operator<(const Limits& oth) const {
33 if (first < oth.first)
34 return true;
35 return second < oth.second;
36 }
37
38 Limits Limits::operator-() const { return Limits(-second, -first); }
39
41 first += c;
42 second += c;
43 return *this;
44 }
45
47 first -= c;
48 second -= c;
49 return *this;
50 }
51
53 first *= c;
54 second *= c;
55 if (c < 0.)
56 std::swap(first, second);
57 return *this;
58 }
59
60 void Limits::in(double low, double up) {
61 first = low;
62 second = up;
63 }
64
65 double Limits::range() const {
66 if (!hasMin() && hasMax()) // if no lower limit, assume 0
67 return second;
68 if (!hasMin() || !hasMax())
69 return 0.;
70 return second - first;
71 }
72
73 bool Limits::hasMin() const { return first != INVALID; }
74
75 bool Limits::hasMax() const { return second != INVALID; }
76
77 bool Limits::contains(double val, bool exclude_boundaries) const {
78 if (hasMin() && (val < min() || (exclude_boundaries && val == min())))
79 return false;
80 if (hasMax() && (val > max() || (exclude_boundaries && val == max())))
81 return false;
82 return true;
83 }
84
85 bool Limits::valid() const {
86 if (min() == max())
87 return false;
88 return hasMin() || hasMax();
89 }
90
92 if (second < first)
93 second = INVALID;
94 return *this;
95 }
96
97 double Limits::x(double v) const {
98 static const Limits x_limits{0., 1.};
99 if (!x_limits.contains(v))
100 throw CG_ERROR("Limits:shoot") << "x = " << v << " must be inside " << x_limits << ".";
101 if (!hasMin() && hasMax()) { // if no lower limit, assume 0
102 CG_WARNING("Limits:shoot") << "Requested to give a value inside interval while no lower limit is set. "
103 << "Assuming this latter is equal to 0.";
104 return second * v;
105 }
106 if (!valid()) {
107 CG_WARNING("Limits:shoot") << "Requested to give a value inside interval although this latter is invalid.";
108 return INVALID;
109 }
110 return first + (second - first) * v;
111 }
112
113 std::vector<double> Limits::generate(size_t num_bins, bool log_scale) const {
114 std::vector<double> out;
115 const auto min_val = (!log_scale) ? min() : std::log10(min());
116 const auto rng = ((!log_scale) ? max() - min() : std::log10(max()) - std::log10(min())) / (num_bins - 1);
117 for (size_t i = 0; i < num_bins; ++i)
118 out.emplace_back((!log_scale) ? min_val + i * rng : std::pow(10, min_val + i * rng));
119 return out;
120 }
121
122 std::vector<Limits> Limits::split(size_t num_bins, bool log_scale) const {
123 const auto& gen = generate(num_bins, log_scale);
124 std::vector<Limits> out;
125 for (size_t i = 0; i < gen.size() - 1; ++i)
126 out.emplace_back(gen.at(i), gen.at(i + 1));
127 return out;
128 }
129
130 Limits Limits::truncate(const Limits& ext) const {
131 auto out = *this;
132 if (ext.hasMin() && (!out.hasMin() || out.min() < ext.min()))
133 out.min() = ext.min();
134 if (ext.hasMax() && (!out.hasMax() || out.max() > ext.max()))
135 out.max() = ext.max();
136 return out;
137 }
138
139 double Limits::trim(double val) const {
140 if (hasMin() && val < min())
141 return min();
142 if (hasMax() && val > max())
143 return max();
144 return val;
145 }
146
147 Limits& Limits::apply(double (*op)(double)) {
148 (*this) = compute(op);
149 return *this;
150 }
151
152 Limits Limits::compute(double (*op)(double)) const {
153 return compute([&op](double ext) { return op(ext); });
154 }
155
156 std::ostream& operator<<(std::ostream& os, const Limits& lim) {
157 if (!lim.hasMin() && !lim.hasMax())
158 return os << "no cuts";
159 if (!lim.hasMin())
160 return os << utils::format("below %g", lim.max());
161 if (!lim.hasMax())
162 return os << utils::format("above %g", lim.min());
163 return os << utils::format("%g to %g", lim.min(), lim.max());
164 }
165
166 Limits operator+(Limits lim, double c) {
167 lim += c;
168 return lim;
169 }
170
171 Limits operator-(Limits lim, double c) {
172 lim -= c;
173 return lim;
174 }
175
176 Limits operator*(Limits lim, double c) {
177 lim *= c;
178 return lim;
179 }
180} // namespace cepgen
#define CG_ERROR(mod)
Definition Exception.h:60
#define CG_WARNING(mod)
Definition Message.h:228
Validity interval for a variable.
Definition Limits.h:28
Limits & apply(double(*)(double))
Apply an operator on limits boundaries.
Definition Limits.cpp:147
std::vector< Limits > split(size_t num_bins, bool log_scale=false) const
Split the limits into sub-limits objects.
Definition Limits.cpp:122
double x(double v) const
Find the [0,1] value scaled between minimum and maximum.
Definition Limits.cpp:97
Limits & operator+=(double)
Add a constant to this limit.
Definition Limits.cpp:40
bool operator<(const Limits &) const
Comparison operator.
Definition Limits.cpp:32
bool valid() const
Is there a lower and upper limit?
Definition Limits.cpp:85
static constexpr double INVALID
Invalid value placeholder (single-edged or invalid limits)
Definition Limits.h:86
std::vector< double > generate(size_t num_bins, bool log_scale=false) const
Generate a collection of values from a number of bins.
Definition Limits.cpp:113
Limits operator-() const
Invert this limit.
Definition Limits.cpp:38
double range() const
Full variable range allowed.
Definition Limits.cpp:65
bool hasMin() const
Have a lower limit?
Definition Limits.cpp:73
bool hasMax() const
Have an upper limit?
Definition Limits.cpp:75
Limits & operator*=(double)
Multiply this limit by a constant.
Definition Limits.cpp:52
double min() const
Lower limit to apply on the variable.
Definition Limits.h:52
Limits compute(double(*)(double)) const
Compute a copy of limits with an operator applied on boundaries Compute a copy of limits with an oper...
Definition Limits.cpp:152
double trim(double) const
Limit a value to boundaries.
Definition Limits.cpp:139
Limits & operator-=(double)
Subtract a constant to this limit.
Definition Limits.cpp:46
Limits truncate(const Limits &) const
Truncate limits to minimal/maximal values.
Definition Limits.cpp:130
Limits & validate()
Ensure the limit object is valid by correcting it if necessary.
Definition Limits.cpp:91
bool contains(double val, bool exclude_boundaries=false) const
Check if value is inside limits' boundaries.
Definition Limits.cpp:77
Limits(double min=INVALID, double max=INVALID)
Define lower and upper limits on a quantity.
Definition Limits.cpp:26
double max() const
Upper limit to apply on the variable.
Definition Limits.h:54
static Limits constant(double)
Build dimension-0 limits (constant)
Definition Limits.cpp:30
void in(double low, double up)
Specify the lower and upper limits on the variable.
Definition Limits.cpp:60
std::string format(const std::string &fmt, Args... args)
Format a string using a printf style format descriptor.
Definition String.h:61
Common namespace for this Monte Carlo generator.
Matrix operator+(const Matrix &lhs, const Matrix &rhs)
Definition Algebra.cpp:237
Momentum operator*(double c, const Momentum &mom)
Definition Momentum.cpp:134
std::ostream & operator<<(std::ostream &os, const Exception::Type &type)
Definition Exception.cpp:59
Matrix operator-(const Matrix &lhs, const Matrix &rhs)
Definition Algebra.cpp:243