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