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