PYTHIA  8.316
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 // Split a string by a delimiter.
217 vector<string> splitString(string val, string delim);
218 
219 //==========================================================================
220 
221 // Print a method name using the appropriate pre-processor macro.
222 
223 // The following method was modified from
224 // boost/current_function.hpp - BOOST_CURRENT_FUNCTION
225 //
226 // Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
227 //
228 // Distributed under the Boost Software License, Version 1.0.
229 // Boost Software License - Version 1.0 - August 17th, 2003
230 //
231 // Permission is hereby granted, free of charge, to any person or
232 // organization obtaining a copy of the software and accompanying
233 // documentation covered by this license (the "Software") to use,
234 // reproduce, display, distribute, execute, and transmit the
235 // Software, and to prepare derivative works of the Software, and to
236 // permit third-parties to whom the Software is furnished to do so,
237 // all subject to the following:
238 //
239 // The copyright notices in the Software and this entire statement,
240 // including the above license grant, this restriction and the
241 // following disclaimer, must be included in all copies of the
242 // Software, in whole or in part, and all derivative works of the
243 // Software, unless such copies or derivative works are solely in the
244 // form of machine-executable object code generated by a source
245 // language processor.
246 //
247 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
248 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
249 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
250 // NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
251 // ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR
252 // OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING
253 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
254 // OTHER DEALINGS IN THE SOFTWARE.
255 //
256 // http://www.boost.org/libs/utility/current_function.html
257 //
258 // Note that Boost Software License - Version 1.0 is fully compatible
259 // with GPLV2
260 // For more information see https://www.gnu.org/licenses/license-list.en.html
261 
262 #ifndef __METHOD_NAME__
263 
264 #ifndef PYTHIA_FUNCTION
265 #if ( defined(__GNUC__) || (defined(__MWERKS__) && (__MWERKS__ >= 0x3000)) \
266 || (defined(__ICC) && (__ICC >= 600)) )
267 # define PYTHIA_FUNCTION __PRETTY_FUNCTION__
268 #elif defined(__DMC__) && (__DMC__ >= 0x810)
269 # define PYTHIA_FUNCTION __PRETTY_FUNCTION__
270 #elif defined(__FUNCSIG__)
271 # define PYTHIA_FUNCTION __FUNCSIG__
272 #elif ( (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 600)) \
273 || (defined(__IBMCPP__) && (__IBMCPP__ >= 500)) )
274 # define PYTHIA_FUNCTION __FUNCTION__
275 #elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x550)
276 # define PYTHIA_FUNCTION __FUNC__
277 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
278 # define PYTHIA_FUNCTION __func__
279 #else
280 # define PYTHIA_FUNCTION "unknown"
281 #endif
282 #endif // end PYTHIA_FUNCTION
283 
284 inline std::string methodName(const std::string& prettyFunction, bool
285  withNamespace=false) {
286 
287  // Find the beginning of the argument list.
288  size_t end = prettyFunction.rfind(')');
289  int bracketCount = 1;
290  while (bracketCount > 0) {
291  --end;
292  if (prettyFunction[end] == ')') ++bracketCount;
293  else if (prettyFunction[end] == '(') --bracketCount;
294  }
295 
296  // Find the start of the function name.
297  size_t begin = prettyFunction.rfind(' ', end) + 1;
298  if (!withNamespace)
299  begin = prettyFunction.find("::", begin) + 2;
300 
301  // Return the correct substring.
302  return prettyFunction.substr(begin, end - begin);
303 }
304 
305 #define __METHOD_NAME__ methodName(PYTHIA_FUNCTION)
306 #endif // __METHOD_NAME__
307 
308 //==========================================================================
309 
310 } // end namespace Pythia8
311 
312 // Define the hash for a pair.
313 namespace std {
314  template <class T1, class T2> struct hash<pair<T1, T2> > {
315  public:
316  size_t operator()(const pair<T1, T2>& p) const {
317  return hash<T1>{}(p.first) ^ hash<T2>{}(p.second);
318  }
319  };
320 
321 //==========================================================================
322 
323 } // end namespace std
324 
325 #endif // Pythia8_PythiaStdlib_H
constexpr double pow2(const double &x)
Powers of small integers - for balance speed/code clarity.
Definition: PythiaStdlib.h:173
vector< string > splitString(string val, string delim)
Split a string by a delimiter.
Definition: PythiaStdlib.cc:72
end namespace Pythia8
Definition: PythiaStdlib.h:313
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:20
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:284
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:37
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