PYTHIA  8.312
PythiaStdlib.h
1 // PythiaStdlib.h is a part of the PYTHIA event generator.
2 // Copyright (C) 2024 Torbjorn Sjostrand.
3 // PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
4 // Please respect the MCnet Guidelines, see GUIDELINES for details.
5 
6 // Header file for functionality pulled in from Stdlib,
7 // plus a few useful utilities (small powers; positive square root,
8 // convert strings to lowercase, Gamma function).
9 
10 #ifndef Pythia8_PythiaStdlib_H
11 #define Pythia8_PythiaStdlib_H
12 
13 // Stdlib header files for mathematics.
14 #include <cstddef>
15 #include <cmath>
16 #include <cstdlib>
17 #include <algorithm>
18 #include <memory>
19 #include <functional>
20 #include <limits>
21 #include <utility>
22 
23 // Stdlib header files for strings and containers.
24 #include <string>
25 #include <vector>
26 #include <array>
27 #include <map>
28 #include <unordered_map>
29 #include <deque>
30 #include <queue>
31 #include <set>
32 #include <list>
33 #include <functional>
34 
35 // Stdlib header file for dynamic library loading.
36 #include <dlfcn.h>
37 
38 // Stdlib header file for input and output.
39 #include <iostream>
40 #include <iomanip>
41 #include <fstream>
42 #include <sstream>
43 
44 // Thread header files.
45 #include <mutex>
46 #include <atomic>
47 #include <thread>
48 
49 // Define pi if not yet done.
50 #ifndef M_PI
51 #define M_PI 3.1415926535897932385
52 #endif
53 
54 // Define the default subrun.
55 #ifndef SUBRUNDEFAULT
56 #define SUBRUNDEFAULT -999
57 #endif
58 
59 // Set floating point exceptions from the gcc compiler for debug
60 // purposes. Use the compilation flag -DGCCFPDEBUG to enable.
61 #ifdef GCCFPDEBUG
62 #ifndef __ENABLE_FP_DEBUG__
63 #define __ENABLE_FP_DEBUG__
64 #include <fenv.h>
65 static void __attribute__((constructor)) raisefpe() {
66  feenableexcept (FE_DIVBYZERO | FE_OVERFLOW | FE_INVALID);
67 }
68 #endif
69 #endif
70 
71 // By this declaration you do not need to use std:: qualifier everywhere.
72 // using namespace std;
73 
74 // Alternatively you can specify exactly which std:: methods will be used.
75 // Now made default so std does not spill outside namespace Pythia8.
76 namespace Pythia8 {
77 
78 // Generic utilities and mathematical functions.
79 using std::swap;
80 using std::max;
81 using std::min;
82 using std::abs;
83 using std::sort;
84 using std::function;
85 using std::isnan;
86 using std::isinf;
87 using std::isfinite;
88 using std::numeric_limits;
89 
90 // Strings and containers.
91 using std::pair;
92 using std::make_pair;
93 using std::string;
94 using std::to_string;
95 using std::vector;
96 using std::array;
97 using std::map;
98 using std::multimap;
99 using std::unordered_map;
100 using std::deque;
101 using std::priority_queue;
102 using std::set;
103 using std::multiset;
104 using std::list;
105 using std::tuple;
106 using std::function;
107 using std::fill;
108 using std::end;
109 using std::begin;
110 
111 // Input/output streams.
112 using std::cin;
113 using std::cout;
114 using std::cerr;
115 using std::streambuf;
116 using std::istream;
117 using std::ostream;
118 using std::fstream;
119 using std::ifstream;
120 using std::ofstream;
121 using std::stringstream;
122 using std::istringstream;
123 using std::ostringstream;
124 using std::ios;
125 
126 // Input/output formatting.
127 using std::endl;
128 using std::fixed;
129 using std::scientific;
130 using std::left;
131 using std::right;
132 using std::setw;
133 using std::setprecision;
134 
135 // Pointers
136 using std::shared_ptr;
137 using std::weak_ptr;
138 using std::unique_ptr;
139 using std::dynamic_pointer_cast;
140 using std::make_shared;
141 
142 // Threading.
143 using std::queue;
144 using std::mutex;
145 using std::thread;
146 using std::atomic;
147 using std::lock_guard;
148 
149 } // end namespace Pythia8
150 
151 namespace Pythia8 {
152 
153 // Define conversion hbar * c = 0.2 GeV * fm = 1 and related.
154 constexpr double HBARC = 0.19732698;
155 constexpr double GEV2FMINV = 1. / HBARC;
156 constexpr double GEVINV2FM = HBARC;
157 constexpr double FM2GEVINV = 1./HBARC;
158 constexpr double FMINV2GEV = HBARC;
159 
160 // Define conversion (hbar * c)^2 = 0.4 GeV^2 * mb = 1 and related.
161 constexpr double HBARCSQ = 0.38937937;
162 constexpr double GEVSQ2MBINV = 1. / HBARCSQ;
163 constexpr double GEVSQINV2MB = HBARCSQ;
164 constexpr double MB2GEVSQINV = 1. / HBARCSQ;
165 constexpr double MBINV2GEVSQ = HBARCSQ;
166 
167 // Define conversion between fm and mm, in both directions.
168 constexpr double FM2MM = 1e-12;
169 constexpr double MM2FM = 1e12;
170 
171 // Define conversion between mb and pb or fb, in both directions.
172 constexpr double MB2PB = 1e9;
173 constexpr double PB2MB = 1e-9;
174 constexpr double MB2FB = 1e12;
175 constexpr double FB2MB = 1e-12;
176 
177 // Define conversion between fm^2 and mb, in both directions.
178 constexpr double FMSQ2MB = 10.;
179 constexpr double MB2FMSQ = 0.1;
180 
181 // Powers of small integers - for balance speed/code clarity.
182 constexpr double pow2(const double& x) {return x*x;}
183 constexpr double pow3(const double& x) {return x*x*x;}
184 constexpr double pow4(const double& x) {return x*x*x*x;}
185 constexpr double pow5(const double& x) {return x*x*x*x*x;}
186 constexpr double pow6(const double& x) {return x*x*x*x*x*x;}
187 constexpr double pow7(const double& x) {return x*x*x*x*x*x*x;}
188 constexpr double pow8(const double& x) {return x*x*x*x*x*x*x*x;}
189 
190 // Avoid problem with negative square root argument (from roundoff).
191 inline double sqrtpos(const double& x) {return sqrt( max( 0., x));}
192 
193 // Explicitly return NaN if negative square root argument, without FPE.
194 inline double sqrtnan(const double& x) {
195  return x > 0 ? sqrt(x) : numeric_limits<double>::quiet_NaN(); }
196 
197 // Restrinct value to lie in specified range.
198 inline double clamp(const double& x, const double& xmin, const double& xmax) {
199  return (x < xmin) ? xmin : (x > xmax) ? xmax : x; }
200 
201 // Convert a string to lowercase for case-insensitive comparisons.
202 // By default remove any initial and trailing blanks or escape characters.
203 string toLower(const string& name, bool trim = true);
204 
205 // Variant of above, with in-place replacement.
206 inline void toLowerRep(string& name, bool trim = true) {
207  name = toLower( name, trim);}
208 
209 // Convert a boolean to a string.
210 inline string toString(bool val) {return val ? "on" : "off";}
211 
212 // Convert an integer to a string.
213 inline string toString(int val) {return to_string(val);}
214 
215 // Convert a double to a string.
216 string toString(double val);
217 
218 //==========================================================================
219 
220 // Print a method name using the appropriate pre-processor macro.
221 
222 // The following method was modified from
223 // boost/current_function.hpp - BOOST_CURRENT_FUNCTION
224 //
225 // Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
226 //
227 // Distributed under the Boost Software License, Version 1.0.
228 // Boost Software License - Version 1.0 - August 17th, 2003
229 //
230 // Permission is hereby granted, free of charge, to any person or
231 // organization obtaining a copy of the software and accompanying
232 // documentation covered by this license (the "Software") to use,
233 // reproduce, display, distribute, execute, and transmit the
234 // Software, and to prepare derivative works of the Software, and to
235 // permit third-parties to whom the Software is furnished to do so,
236 // all subject to the following:
237 //
238 // The copyright notices in the Software and this entire statement,
239 // including the above license grant, this restriction and the
240 // following disclaimer, must be included in all copies of the
241 // Software, in whole or in part, and all derivative works of the
242 // Software, unless such copies or derivative works are solely in the
243 // form of machine-executable object code generated by a source
244 // language processor.
245 //
246 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
247 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
248 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
249 // NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
250 // ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR
251 // OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING
252 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
253 // OTHER DEALINGS IN THE SOFTWARE.
254 //
255 // http://www.boost.org/libs/utility/current_function.html
256 //
257 // Note that Boost Software License - Version 1.0 is fully compatible
258 // with GPLV2
259 // For more information see https://www.gnu.org/licenses/license-list.en.html
260 
261 #ifndef __METHOD_NAME__
262 
263 #ifndef PYTHIA_FUNCTION
264 #if ( defined(__GNUC__) || (defined(__MWERKS__) && (__MWERKS__ >= 0x3000)) \
265 || (defined(__ICC) && (__ICC >= 600)) )
266 # define PYTHIA_FUNCTION __PRETTY_FUNCTION__
267 #elif defined(__DMC__) && (__DMC__ >= 0x810)
268 # define PYTHIA_FUNCTION __PRETTY_FUNCTION__
269 #elif defined(__FUNCSIG__)
270 # define PYTHIA_FUNCTION __FUNCSIG__
271 #elif ( (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 600)) \
272 || (defined(__IBMCPP__) && (__IBMCPP__ >= 500)) )
273 # define PYTHIA_FUNCTION __FUNCTION__
274 #elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x550)
275 # define PYTHIA_FUNCTION __FUNC__
276 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
277 # define PYTHIA_FUNCTION __func__
278 #else
279 # define PYTHIA_FUNCTION "unknown"
280 #endif
281 #endif // end PYTHIA_FUNCTION
282 
283 inline std::string methodName(const std::string& prettyFunction, bool
284  withNamespace=false) {
285 
286  // Find the beginning of the argument list.
287  size_t end = prettyFunction.rfind(')');
288  int bracketCount = 1;
289  while (bracketCount > 0) {
290  --end;
291  if (prettyFunction[end] == ')') ++bracketCount;
292  else if (prettyFunction[end] == '(') --bracketCount;
293  }
294 
295  // Find the start of the function name.
296  size_t begin = prettyFunction.rfind(' ', end) + 1;
297  if (!withNamespace)
298  begin = prettyFunction.find("::", begin) + 2;
299 
300  // Return the correct substring.
301  return prettyFunction.substr(begin, end - begin);
302 }
303 
304 #define __METHOD_NAME__ methodName(PYTHIA_FUNCTION)
305 #endif // __METHOD_NAME__
306 
307 //==========================================================================
308 
309 } // end namespace Pythia8
310 
311 // Define the hash for a pair.
312 namespace std {
313  template <class T1, class T2> struct hash<pair<T1, T2> > {
314  public:
315  size_t operator()(const pair<T1, T2>& p) const {
316  return hash<T1>{}(p.first) ^ hash<T2>{}(p.second);
317  }
318  };
319 
320 //==========================================================================
321 
322 } // end namespace std
323 
324 #endif // Pythia8_PythiaStdlib_H
constexpr double pow2(const double &x)
Powers of small integers - for balance speed/code clarity.
Definition: PythiaStdlib.h:182
end namespace Pythia8
Definition: PythiaStdlib.h:312
void toLowerRep(string &name, bool trim=true)
Variant of above, with in-place replacement.
Definition: PythiaStdlib.h:206
string toLower(const string &name, bool trim=true)
Definition: PythiaStdlib.cc:17
constexpr double MB2PB
Define conversion between mb and pb or fb, in both directions.
Definition: PythiaStdlib.h:172
constexpr double FM2MM
Define conversion between fm and mm, in both directions.
Definition: PythiaStdlib.h:168
constexpr double HBARC
Define conversion hbar * c = 0.2 GeV * fm = 1 and related.
Definition: PythiaStdlib.h:154
string toString(bool val)
Convert a boolean to a string.
Definition: PythiaStdlib.h:210
std::string methodName(const std::string &prettyFunction, bool withNamespace=false)
end PYTHIA_FUNCTION
Definition: PythiaStdlib.h:283
double sqrtnan(const double &x)
Explicitly return NaN if negative square root argument, without FPE.
Definition: PythiaStdlib.h:194
constexpr double FMSQ2MB
Define conversion between fm^2 and mb, in both directions.
Definition: PythiaStdlib.h:178
double clamp(const double &x, const double &xmin, const double &xmax)
Restrinct value to lie in specified range.
Definition: PythiaStdlib.h:198
Header for classes to set beam momentum and interaction vertex spread.
Definition: Analysis.h:20
constexpr double HBARCSQ
Define conversion (hbar * c)^2 = 0.4 GeV^2 * mb = 1 and related.
Definition: PythiaStdlib.h:161
double sqrtpos(const double &x)
Avoid problem with negative square root argument (from roundoff).
Definition: PythiaStdlib.h:191