PYTHIA  8.313
Basics.h
1 // Basics.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 basic, often-used helper classes.
7 // RndmEngine: base class for external random number generators.
8 // Rndm: random number generator.
9 // Vec4: simple four-vectors.
10 // RotBstMatrix: matrices encoding rotations and boosts of Vec4 objects.
11 // Hist: simple one-dimensional histograms.
12 
13 #ifndef Pythia8_Basics_H
14 #define Pythia8_Basics_H
15 
16 #include "Pythia8/PythiaStdlib.h"
17 #include "Pythia8/SharedPointers.h"
18 
19 namespace Pythia8 {
20 
21 //==========================================================================
22 
23 // Forward reference to RotBstMatrix class; needed in Vec4 class.
24 class RotBstMatrix;
25 
26 //--------------------------------------------------------------------------
27 
28 // Vec4 class.
29 // This class implements four-vectors, in energy-momentum space.
30 // (But can equally well be used to hold space-time four-vectors.)
31 
32 class Vec4 {
33 
34 public:
35 
36  // Constructors.
37  Vec4(double xIn = 0., double yIn = 0., double zIn = 0., double tIn = 0.)
38  : xx(xIn), yy(yIn), zz(zIn), tt(tIn) { }
39  Vec4(const Vec4& v) : xx(v.xx), yy(v.yy), zz(v.zz), tt(v.tt) { }
40  Vec4& operator=(const Vec4& v) { if (this != &v) { xx = v.xx; yy = v.yy;
41  zz = v.zz; tt = v.tt; } return *this; }
42  Vec4& operator=(double value) { xx = value; yy = value; zz = value;
43  tt = value; return *this; }
44 
45  // Member functions for input.
46  void reset() {xx = 0.; yy = 0.; zz = 0.; tt = 0.;}
47  void p(double xIn, double yIn, double zIn, double tIn)
48  {xx = xIn; yy = yIn; zz = zIn; tt = tIn;}
49  void p(Vec4 pIn) {xx = pIn.xx; yy = pIn.yy; zz = pIn.zz; tt = pIn.tt;}
50  void px(double xIn) {xx = xIn;}
51  void py(double yIn) {yy = yIn;}
52  void pz(double zIn) {zz = zIn;}
53  void e(double tIn) {tt = tIn;}
54 
55  // Member functions for output.
56  double px() const {return xx;}
57  double py() const {return yy;}
58  double pz() const {return zz;}
59  double e() const {return tt;}
60  double& operator[](int i) {
61  switch(i) {
62  case 1: return xx;
63  case 2: return yy;
64  case 3: return zz;
65  default: return tt;
66  }
67  }
68  double mCalc() const {double temp = tt*tt - xx*xx - yy*yy - zz*zz;
69  return (temp >= 0.) ? sqrt(temp) : -sqrt(-temp);}
70  double m2Calc() const {return tt*tt - xx*xx - yy*yy - zz*zz;}
71  double pT() const {return sqrt(xx*xx + yy*yy);}
72  double pT2() const {return xx*xx + yy*yy;}
73  double pAbs() const {return sqrt(xx*xx + yy*yy + zz*zz);}
74  double pAbs2() const {return xx*xx + yy*yy + zz*zz;}
75  double eT() const {double temp = xx*xx + yy*yy;
76  return tt * sqrt( temp / (temp + zz*zz) );}
77  double eT2() const {double temp = xx*xx + yy*yy;
78  return tt*tt * temp / (temp + zz*zz);}
79  double theta() const {return atan2(sqrt(xx*xx + yy*yy), zz);}
80  double phi() const {return atan2(yy,xx);}
81  double thetaXZ() const {return atan2(xx,zz);}
82  double pPos() const {return tt + zz;}
83  double pNeg() const {return tt - zz;}
84  double rap() const {
85  double txyz = (tt > 0.) ? tt : sqrt(xx*xx + yy*yy + zz*zz);
86  if (zz >= txyz) return 20.;
87  if (zz <= -txyz) return -20.;
88  return 0.5 * log( (txyz + zz) / (txyz - zz) );}
89  double eta() const {double xyz = sqrt(xx*xx + yy*yy + zz*zz);
90  if (zz >= xyz) return 20.;
91  if (zz <= -xyz) return -20.;
92  return 0.5 * log( (xyz + zz) / (xyz - zz) );}
93 
94  // Member functions that perform operations.
95  void rescale3(double fac) {xx *= fac; yy *= fac; zz *= fac;}
96  void rescale4(double fac) {xx *= fac; yy *= fac; zz *= fac; tt *= fac;}
97  void flip3() {xx = -xx; yy = -yy; zz = -zz;}
98  void flip4() {xx = -xx; yy = -yy; zz = -zz; tt = -tt;}
99  void rot(double thetaIn, double phiIn);
100  void rotaxis(double phiIn, double nx, double ny, double nz);
101  void rotaxis(double phiIn, const Vec4& n);
102  void bst(double betaX, double betaY, double betaZ);
103  void bst(double betaX, double betaY, double betaZ, double gamma);
104  void bst(const Vec4& pIn);
105  void bst(const Vec4& pIn, double mIn);
106  void bstback(const Vec4& pIn);
107  void bstback(const Vec4& pIn, double mIn);
108  void rotbst(const RotBstMatrix& M);
109  double eInFrame(const Vec4& pIn) const;
110 
111  // Operator overloading with member functions
112  inline Vec4 operator-() const {Vec4 tmp; tmp.xx = -xx; tmp.yy = -yy;
113  tmp.zz = -zz; tmp.tt = -tt; return tmp;}
114  inline Vec4& operator+=(const Vec4& v) {xx += v.xx; yy += v.yy; zz += v.zz;
115  tt += v.tt; return *this;}
116  inline Vec4& operator-=(const Vec4& v) {xx -= v.xx; yy -= v.yy; zz -= v.zz;
117  tt -= v.tt; return *this;}
118  inline Vec4& operator*=(double f) {xx *= f; yy *= f; zz *= f;
119  tt *= f; return *this;}
120  inline Vec4& operator/=(double f) {xx /= f; yy /= f; zz /= f;
121  tt /= f; return *this;}
122  inline Vec4 operator+(const Vec4& v) const {
123  Vec4 tmp = *this; return tmp += v;}
124  inline Vec4 operator-(const Vec4& v) const {
125  Vec4 tmp = *this; return tmp -= v;}
126  inline Vec4 operator*(double f) const {
127  Vec4 tmp = *this; return tmp *= f;}
128  inline Vec4 operator/(double f) const {
129  Vec4 tmp = *this; return tmp /= f;}
130  inline double operator*(const Vec4& v) const {
131  return tt*v.tt - xx*v.xx - yy*v.yy - zz*v.zz;}
132 
133  // Operator overloading with friends.
134  friend Vec4 operator*(double f, const Vec4& v1);
135 
136  // Print a four-vector.
137  friend ostream& operator<<(ostream&, const Vec4& v) ;
138 
139  // Check if NaN, INF, or finite.
140  friend inline bool isnan(const Vec4 &v) {
141  return isnan(v.tt) || isnan(v.xx) || isnan(v.yy) || isnan(v.zz);}
142  friend inline bool isinf(const Vec4 &v) {
143  return isinf(v.tt) || isinf(v.xx) || isinf(v.yy) || isinf(v.zz);}
144  friend inline bool isfinite(const Vec4 &v) {
145  return isfinite(v.tt) && isfinite(v.xx) && isfinite(v.yy)
146  && isfinite(v.zz);}
147 
148  // Invariant mass and its square.
149  friend double m(const Vec4& v1);
150  friend double m(const Vec4& v1, const Vec4& v2);
151  friend double m2(const Vec4& v1);
152  friend double m2(const Vec4& v1, const Vec4& v2);
153  friend double m2(const Vec4& v1, const Vec4& v2, const Vec4& v3);
154  friend double m2(const Vec4& v1, const Vec4& v2, const Vec4& v3,
155  const Vec4& v4);
156 
157  // Scalar and cross product of 3-vector parts.
158  friend double dot3(const Vec4& v1, const Vec4& v2);
159  friend Vec4 cross3(const Vec4& v1, const Vec4& v2);
160 
161  // Cross-product of three 4-vectors ( p_i = epsilon_{iabc} p_a p_b p_c).
162  friend Vec4 cross4(const Vec4& a, const Vec4& b, const Vec4& c);
163 
164  // theta is the opening angle (on the unit sphere) between v1 and v2.
165  friend double theta(const Vec4& v1, const Vec4& v2);
166  friend double costheta(const Vec4& v1, const Vec4& v2);
167  friend double sintheta(const Vec4& v1, const Vec4& v2);
168 
169  // phi is azimuthal angle between v1 and v2 around z axis.
170  friend double phi(const Vec4& v1, const Vec4& v2);
171  friend double cosphi(const Vec4& v1, const Vec4& v2);
172 
173  // phi is azimuthal angle between v1 and v2 around n axis.
174  friend double phi(const Vec4& v1, const Vec4& v2, const Vec4& n);
175  friend double cosphi(const Vec4& v1, const Vec4& v2, const Vec4& n);
176 
177  // R is distance in cylindrical (y/eta, phi) coordinates.
178  friend double RRapPhi(const Vec4& v1, const Vec4& v2);
179  friend double REtaPhi(const Vec4& v1, const Vec4& v2);
180 
181  // Shift four-momenta within pair from old to new masses.
182  friend bool pShift( Vec4& p1Move, Vec4& p2Move, double m1New, double m2New);
183 
184  // Create two vectors that are perpendicular to both input vectors.
185  friend pair<Vec4,Vec4> getTwoPerpendicular(const Vec4& v1, const Vec4& v2);
186 
187 private:
188 
189  // Constants: could only be changed in the code itself.
190  static const double TINY;
191 
192  // The four-vector data members.
193  double xx, yy, zz, tt;
194 
195 };
196 
197 //--------------------------------------------------------------------------
198 
199 // Namespace function declarations; friends of Vec4 class.
200 
201 // Implementation of operator overloading with friends.
202 inline Vec4 operator*(double f, const Vec4& v1)
203  {Vec4 v = v1; return v *= f;}
204 
205 // Invariant mass and its square.
206 double m(const Vec4& v1);
207 double m(const Vec4& v1, const Vec4& v2);
208 double m2(const Vec4& v1);
209 double m2(const Vec4& v1, const Vec4& v2);
210 double m2(const Vec4& v1, const Vec4& v2, const Vec4& v3);
211 double m2(const Vec4& v1, const Vec4& v2, const Vec4& v3,
212  const Vec4& v4);
213 
214 // Scalar and cross product of 3-vector parts.
215 double dot3(const Vec4& v1, const Vec4& v2);
216 Vec4 cross3(const Vec4& v1, const Vec4& v2);
217 
218 // Cross-product of three 4-vectors ( p_i = epsilon_{iabc} p_a p_b p_c).
219 Vec4 cross4(const Vec4& a, const Vec4& b, const Vec4& c);
220 
221 // theta is polar angle between v1 and v2.
222 double theta(const Vec4& v1, const Vec4& v2);
223 double costheta(const Vec4& v1, const Vec4& v2);
224 double sintheta(const Vec4& v1, const Vec4& v2);
225 double costheta(double e1, double e2, double m1, double m2, double s12);
226 
227 // phi is azimuthal angle between v1 and v2 around z axis.
228 double phi(const Vec4& v1, const Vec4& v2);
229 double cosphi(const Vec4& v1, const Vec4& v2);
230 
231 // phi is azimuthal angle between v1 and v2 around n axis.
232 double phi(const Vec4& v1, const Vec4& v2, const Vec4& n);
233 double cosphi(const Vec4& v1, const Vec4& v2, const Vec4& n);
234 
235 // R is distance in cylindrical (y/eta, phi) coordinates.
236 double RRapPhi(const Vec4& v1, const Vec4& v2);
237 double REtaPhi(const Vec4& v1, const Vec4& v2);
238 
239 // Print a four-vector.
240 ostream& operator<<(ostream&, const Vec4& v) ;
241 
242 // Shift four-momenta within pair from old to new masses.
243 bool pShift( Vec4& p1Move, Vec4& p2Move, double m1New, double m2New);
244 
245 // Create two vectors that are perpendicular to both input vectors.
246 pair<Vec4,Vec4> getTwoPerpendicular(const Vec4& v1, const Vec4& v2);
247 
248 //==========================================================================
249 
250 // RotBstMatrix class.
251 // This class implements 4 * 4 matrices that encode an arbitrary combination
252 // of rotations and boosts, that can be applied to Vec4 four-vectors.
253 
255 
256 public:
257 
258  // Constructors.
259  RotBstMatrix() : M() {for (int i = 0; i < 4; ++i) {
260  for (int j = 0; j < 4; ++j) { M[i][j] = (i==j) ? 1. : 0.; } } }
261  RotBstMatrix(const RotBstMatrix& Min) : M() {
262  for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) {
263  M[i][j] = Min.M[i][j]; } } }
264  RotBstMatrix& operator=(const RotBstMatrix& Min) {if (this != &Min) {
265  for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) {
266  M[i][j] = Min.M[i][j]; } } } return *this; }
267 
268  // Member functions.
269  void rot(double = 0., double = 0.);
270  void rot(const Vec4& p);
271  void bst(double = 0., double = 0., double = 0., double = 0.);
272  void bst(const Vec4&);
273  void bstback(const Vec4&);
274  void bst(const Vec4&, const Vec4&);
275  void toCMframe(const Vec4&, const Vec4&);
276  void fromCMframe(const Vec4&, const Vec4&, bool flip = false);
277  void toSameVframe(const Vec4&, const Vec4&);
278  void fromSameVframe(const Vec4&, const Vec4&);
279  void rotbst(const RotBstMatrix&);
280  void invert();
281  RotBstMatrix inverse() const { RotBstMatrix tmp = *this;
282  tmp.invert(); return tmp; }
283  void reset();
284 
285  // Return value of matrix element.
286  double value(int i, int j) { return M[i][j];}
287 
288  // Crude estimate deviation from unit matrix.
289  double deviation() const;
290 
291  // Print a transformation matrix.
292  friend ostream& operator<<(ostream&, const RotBstMatrix&) ;
293 
294  // Private members to be accessible from Vec4.
295  friend class Vec4;
296 
297  // Multiplication.
298  Vec4 operator*(Vec4 p) const { p.rotbst(*this); return p; }
299  RotBstMatrix operator*(RotBstMatrix R) const { R.rotbst(*this); return R; }
300 
301 private:
302 
303  // Constants: could only be changed in the code itself.
304  static const double TINY;
305 
306  // The rotation-and-boost matrix data members.
307  double M[4][4];
308 
309 };
310 
311 //--------------------------------------------------------------------------
312 
313 // Namespace function declaration; friend of RotBstMatrix class.
314 
315 // Print a transformation matrix.
316 ostream& operator<<(ostream&, const RotBstMatrix&) ;
317 
318 // Get a RotBstMatrix to rest frame of p.
319 inline RotBstMatrix toCMframe(const Vec4& p) {
320  RotBstMatrix tmp; tmp.bstback(p); return tmp; }
321 
322 // Get a RotBstMatrix from rest frame of p.
323 inline RotBstMatrix fromCMframe(const Vec4& p) {
324  RotBstMatrix tmp; tmp.bst(p); return tmp; }
325 
326 // Get a RotBstMatrix to rest frame of p1 and p2, where p1 is along
327 // the z-axis.
328 inline RotBstMatrix toCMframe(const Vec4& p1, const Vec4& p2) {
329  RotBstMatrix tmp; tmp.toCMframe(p1, p2); return tmp; }
330 
331 // Get a RotBstMatrix from rest frame of p1 and p2, where p1 is
332 // assumed by default to be along the z-axis. The flip option
333 // handles the case when p1 is along the negative z-axis.
334 inline RotBstMatrix fromCMframe(const Vec4& p1, const Vec4& p2,
335  bool flip = false) {
336  RotBstMatrix tmp; tmp.fromCMframe(p1, p2, flip); return tmp; }
337 
338 // Get a RotBstMatrix to rest frame of ptot where pz is along the
339 // z-axis and pxz is in the xz-plane with positive x.
340 inline RotBstMatrix toCMframe(const Vec4& ptot, const Vec4& pz,
341  const Vec4 & pxz) { RotBstMatrix tmp = toCMframe(ptot);
342  Vec4 pzp = tmp*pz; tmp.rot(0.0, -pzp.phi()); tmp.rot(-pzp.theta());
343  tmp.rot(0.0, -(tmp*pxz).phi()); return tmp; }
344 
345 // Get a RotBstMatrix from rest frame of ptot where pz is along the
346 // z-axis and pxz is in the xz-plane with positive x.
347 inline RotBstMatrix fromCMframe(const Vec4& ptot, const Vec4& pz,
348  const Vec4 & pxz) { return toCMframe(ptot, pz, pxz).inverse(); }
349 
350 //==========================================================================
351 
352 // RndmEngine is the base class for external random number generators.
353 // There is only one pure virtual method, that should do the generation.
354 
355 class RndmEngine {
356 
357 public:
358 
359  // Destructor.
360  virtual ~RndmEngine() {}
361 
362  // A virtual method, wherein the derived class method
363  // generates a random number uniformly distributed between 0 and 1.
364  virtual double flat() {return 1;}
365 
366 };
367 
368 //==========================================================================
369 
370 // RndmState class.
371 // This class describes the configuration of a Rndm object.
372 
373 struct RndmState {
374  int i97{}, j97{}, seed{0};
375  long sequence{0};
376  double u[97]{}, c{}, cd{}, cm{};
377 
378  // Test whether two random states would generate the same random sequence.
379  bool operator==(const RndmState& other) const;
380 };
381 
382 //==========================================================================
383 
384 // Rndm class.
385 // This class handles random number generation according to the
386 // Marsaglia-Zaman-Tsang algorithm.
387 
388 class Rndm {
389 
390 public:
391 
392  // Constructors.
393  Rndm() : initRndm(false), stateSave(), useExternalRndm(false) { }
394  Rndm(int seedIn) : initRndm(false), stateSave(), useExternalRndm(false) {
395  init(seedIn);}
396 
397  // Possibility to pass in pointer for external random number generation.
398  bool rndmEnginePtr( RndmEnginePtr rndmEngPtrIn);
399 
400  // Initialize, normally at construction or in first call.
401  void init(int seedIn = 0) ;
402 
403  // Generate next random number uniformly between 0 and 1.
404  double flat() ;
405 
406  // Generate random numbers according to exp(-x).
407  double exp() ;
408 
409  // Generate random numbers according to x * exp(-x).
410  double xexp() { return -log(flat() * flat()) ;}
411 
412  // Generate random numbers according to exp(-x^2/2).
413  double gauss() {return sqrt(-2. * log(flat())) * cos(M_PI * flat());}
414 
415  // Generate two random numbers according to exp(-x^2/2-y^2/2).
416  pair<double, double> gauss2() {double r = sqrt(-2. * log(flat()));
417  double phi = 2. * M_PI * flat();
418  return { r * sin(phi), r * cos(phi) };}
419 
420  // Generate a random number according to a Gamma-distribution.
421  double gamma(double k0, double r0);
422 
423  // Generate two random vectors according to the phase space distribution
424  pair<Vec4, Vec4> phaseSpace2(double eCM, double m1, double m2);
425 
426  // Pick one option among vector of (positive) probabilities.
427  int pick(const vector<double>& prob) ;
428 
429  // Randomly shuffle a vector, standard Fisher-Yates algorithm.
430  template<typename T> void shuffle(vector<T>& vec);
431 
432  // Peek at the next random number in sequence without updating
433  // the generator state.
434  double peekFlat() {if (useExternalRndm) return -1;
435  RndmState oldState = stateSave;
436  double f = this->flat();
437  stateSave = oldState;
438  return f;
439  }
440 
441  // Save or read current state to or from a binary file.
442  bool dumpState(string fileName);
443  bool readState(string fileName);
444 
445  // Get or set the state of the random number generator.
446  RndmState getState() const {return stateSave;}
447  void setState(const RndmState& state) {stateSave = state;}
448 
449  // The default seed, i.e. the Marsaglia-Zaman random number sequence.
450  static constexpr int DEFAULTSEED = 19780503;
451 
452 #ifdef RNGDEBUG
453  // Random number methods used for debugging only.
454  double flatDebug(string loc, string file, int line);
455  double xexpDebug(string loc, string file, int line);
456  double gaussDebug(string loc, string file, int line);
457  pair<double, double> gauss2Debug(string loc, string file, int line);
458  double gammaDebug(string loc, string file, int line, double k0, double r0);
459  pair<Vec4, Vec4> phaseSpace2Debug(string loc, string file, int line,
460  double eCM, double m1, double m2);
461 
462  // Static members for debugging to print call file location or filter.
463  static bool debugNow, debugLocation, debugIndex;
464  static int debugPrecision, debugCall;
465  static set<string> debugStarts, debugEnds, debugContains, debugMatches;
466 #endif
467 
468 private:
469 
470  // State of the random number generator.
471  bool initRndm;
472  RndmState stateSave;
473 
474  // Pointer for external random number generation.
475  bool useExternalRndm;
476  RndmEnginePtr rndmEngPtr{};
477 
478 };
479 
480 //==========================================================================
481 
482 // Hist class.
483 // This class handles a single histogram at a time.
484 
485 class Hist {
486 
487 public:
488 
489  // Constructors, including copy constructors.
490  Hist() : titleSave(""), nBin(), nFill(), nNonFinite(), xMin(),
491  xMax(), linX(), doStats(), dx(), under(), inside(), over(), sumxNw()
492  { }
493  Hist(string titleIn, int nBinIn = 100, double xMinIn = 0.,
494  double xMaxIn = 1., bool logXIn = false, bool doStatsIn = false) :
495  nBin(), nFill(), nNonFinite(), xMin(), xMax(), linX(), doStats(), dx(),
496  under(), inside(), over(), sumxNw()
497  { book(titleIn, nBinIn, xMinIn, xMaxIn, logXIn, doStatsIn); }
498  Hist(const Hist& h)
499  : titleSave(h.titleSave), nBin(h.nBin), nFill(h.nFill),
500  nNonFinite(h.nNonFinite), xMin(h.xMin), xMax(h.xMax), linX(h.linX),
501  doStats(h.doStats), dx(h.dx), under(h.under), inside(h.inside),
502  over(h.over), res(h.res), res2(h.res2), sumxNw() {
503  for (int i = 0; i < nMoments; ++i) sumxNw[i] = h.sumxNw[i];
504  }
505  Hist(string titleIn, const Hist& h)
506  : titleSave(titleIn), nBin(h.nBin), nFill(h.nFill),
507  nNonFinite(h.nNonFinite), xMin(h.xMin), xMax(h.xMax), linX(h.linX),
508  doStats(h.doStats), dx(h.dx), under(h.under), inside(h.inside),
509  over(h.over), res(h.res), res2(h.res2), sumxNw() {
510  for (int i = 0; i < nMoments; ++i) sumxNw[i] = h.sumxNw[i];
511  }
512  Hist& operator=(const Hist& h) { if(this != &h) {
513  nBin = h.nBin; nFill = h.nFill; nNonFinite = h.nNonFinite; xMin = h.xMin;
514  xMax = h.xMax; linX = h.linX; doStats = h.doStats; dx = h.dx;
515  under = h.under; inside = h.inside; over = h.over;
516  for (int i = 0; i < nMoments; ++i) sumxNw[i] = h.sumxNw[i];
517  res = h.res; res2 = h.res2; } return *this; }
518 
519  // Create a histogram that is the plot of the given function.
520  static Hist plotFunc(function<double(double)> f, string titleIn,
521  int nBinIn, double xMinIn, double xMaxIn, bool logXIn = false);
522 
523  // Book a histogram.
524  void book(string titleIn = " ", int nBinIn = 100, double xMinIn = 0.,
525  double xMaxIn = 1., bool logXIn = false, bool doStatsIn = false) ;
526 
527  // Set title of a histogram.
528  void title(string titleIn = " ") {titleSave = titleIn; }
529 
530  // Reset bin contents.
531  void null() ;
532 
533  // Fill bin with weight.
534  void fill(double x, double w = 1.) ;
535 
536  // Print a histogram with overloaded << operator.
537  friend ostream& operator<<(ostream& os, const Hist& h) ;
538 
539  // Print histogram contents as a table (e.g. for Gnuplot, Rivet or Pyplot),
540  // optionally with statistical errors.
541  void table(ostream& os = cout, bool printOverUnder = false,
542  bool xMidBin = true, bool printError = false) const ;
543  void table(string fileName, bool printOverUnder = false,
544  bool xMidBin = true, bool printError = false) const {
545  ofstream streamName(fileName.c_str());
546  table(streamName, printOverUnder, xMidBin, printError);}
547  void rivetTable(ostream& os = cout, bool printError = true) const ;
548  void rivetTable(string fileName, bool printError = true) const {
549  ofstream streamName(fileName.c_str()); rivetTable(streamName, printError);}
550  void pyplotTable(ostream& os = cout, bool isHist = true,
551  bool printError = false) const ;
552  void pyplotTable(string fileName, bool isHist = true,
553  bool printError = false) const {ofstream streamName(fileName.c_str());
554  pyplotTable(streamName, isHist, printError);}
555 
556  // Fill contents of a two-column (x,y) table, e.g. written by table() above.
557  void fillTable(istream& is = cin);
558  void fillTable(string fileName) { ifstream streamName(fileName.c_str());
559  fillTable(streamName);}
560 
561  // Print a table out of two histograms with same x axis (no errors printed).
562  friend void table(const Hist& h1, const Hist& h2, ostream& os,
563  bool printOverUnder, bool xMidBin) ;
564  friend void table(const Hist& h1, const Hist& h2, string fileName,
565  bool printOverUnder, bool xMidBin) ;
566 
567  // Return title and size of histogram. Also if logarithmic x scale.
568  string getTitle() const {return titleSave;}
569  int getBinNumber() const {return nBin;}
570  int getNonFinite() const {return nNonFinite;}
571  bool getLinX() const {return linX;}
572 
573  // Return min and max in x and y directions.
574  double getXMin() const {return xMin;}
575  double getXMax() const {return xMax;}
576  double getYMin() const { if (nBin == 0) return 0.;
577  double yMin = res[0];
578  for (int ix = 1; ix < nBin; ++ix)
579  if (res[ix] < yMin ) yMin = res[ix];
580  return yMin;}
581  double getYMax() const { if (nBin == 0) return 0.;
582  double yMax = res[0];
583  for (int ix = 1; ix < nBin; ++ix)
584  if (res[ix] > yMax ) yMax = res[ix];
585  return yMax;}
586  double getYAbsMin() const { double yAbsMin = 1e20; double yAbs;
587  for (int ix = 0; ix < nBin; ++ix) { yAbs = abs(res[ix]);
588  if (yAbs > 1e-20 && yAbs < yAbsMin) yAbsMin = yAbs; }
589  return yAbsMin;}
590 
591  // Return <X> and error on <X>, unbinned from saved weight sums (default)
592  // or directly from the histogram bins (unbinned = false). In the latter
593  // case, the error estimate includes the difference between the binned and
594  // unbinned value summed in quadrature with the statistical error, as a
595  // measure of bin granularity error.
596  double getXMean(bool unbinned=true) const;
597  double getXMeanErr(bool unbinned=true) const;
598 
599  // Return Median in X and its statistical error, ignoring underflow and
600  // overflow (default) or including them (includeOverUnder = true). By
601  // default, error includes granularity estimate obtained by comparing binned
602  // vs unbinned mean value, but this can be switched off (unbinned = false).
603  double getXMedian(bool includeOverUnder=false) const;
604  double getXMedianErr(bool unbinned=true) const;
605 
606  // Return average <Y> value.
607  double getYMean() const { return inside / nFill; }
608 
609  // Return RMS and equivalent n'th roots of n'th moments about the mean,
610  // and their error estimates. Up to n = 6, both unbinned and binned moments
611  // can be calculated. For n >= 7, and for all error estimates, only
612  // binned values are available. Note that (unlike ROOT), the error estimates
613  // do not assume normal distributions.
614  // RMN(2) = RMS is the standard root-mean-square deviation from the mean.
615  // RMN(3) is the cube root of the mean-cube deviation,
616  // cbrt(<(x - <x>)^3>). It is sensitive to single-sided tails,
617  // as are characteristic of many particle-physics distributions.
618  // RMN(4) adds sensitivity to double-sided long tails (eg BW vs
619  // Gaussian), and further sensitivity to long single-sided ones.
620  // Etc.
621  double getXRMN(int n=2, bool unbinned=true) const;
622  double getXRMS(bool unbinned=true) const {return getXRMN(2, unbinned);}
623 
624  double getXRMNErr(int n=2, bool unbinned=true) const;
625  double getXRMSErr(bool unbinned=true) const {
626  return getXRMNErr(2, unbinned);}
627 
628  // Return content of specific bin: 0 gives underflow and nBin+1 overflow.
629  double getBinContent(int iBin) const;
630 
631  // Return the lower edge of the bin.
632  double getBinEdge(int iBin) const;
633 
634  // Return the width of the bin.
635  double getBinWidth(int iBin=1) const;
636 
637  // Return bin contents.
638  vector<double> getBinContents() const;
639 
640  // Return bin edges.
641  vector<double> getBinEdges() const;
642 
643  // Return number of entries.
644  int getEntries(bool alsoNonFinite = true) const {
645  return alsoNonFinite ? nNonFinite + nFill : nFill; }
646 
647  // Return sum of weights.
648  double getWeightSum(bool alsoOverUnder = true) const {
649  return alsoOverUnder ? inside + over + under : inside; }
650 
651  // Return effective entries (for weighted histograms = number
652  // of equivalent unweighted events for same statistical power).
653  double getNEffective() const {
654  double sumw2 = 0.;
655  for (int ix = 0; ix < nBin; ++ix) sumw2 += res2[ix];
656  if (sumw2 <= Hist::TINY) return 0.;
657  else return pow2(sumxNw[0]) / sumw2;
658  }
659 
660  // Check whether another histogram has same size and limits.
661  bool sameSize(const Hist& h) const ;
662 
663  // Take an arbitrary function of bin contents.
664  void takeFunc(function<double(double)> func);
665 
666  // Take logarithm (base 10 or e) of bin contents.
667  void takeLog(bool tenLog = true);
668 
669  // Take square root of bin contents.
670  void takeSqrt();
671 
672  // Normalize sum of bin contents to value, with or without overflow bins.
673  void normalize(double f = 1, bool overflow = true) ;
674 
675  // Normalize sum of bin areas to value, with or without overflow bins.
676  void normalizeIntegral(double f = 1, bool overflow = true);
677 
678  // Scale each bin content by 1 / (wtSum * bin width).
679  void normalizeSpectrum(double wtSum);
680 
681  // Operator overloading with member functions
682  Hist& operator+=(const Hist& h) ;
683  Hist& operator-=(const Hist& h) ;
684  Hist& operator*=(const Hist& h) ;
685  Hist& operator/=(const Hist& h) ;
686  Hist& operator+=(double f) ;
687  Hist& operator-=(double f) ;
688  Hist& operator*=(double f) ;
689  Hist& operator/=(double f) ;
690  Hist operator+(double f) const;
691  Hist operator+(const Hist& h2) const;
692  Hist operator-(double f) const;
693  Hist operator-(const Hist& h2) const;
694  Hist operator*(double f) const;
695  Hist operator*(const Hist& h2) const;
696  Hist operator/(double f) const;
697  Hist operator/(const Hist& h2) const;
698 
699  // Operator overloading with friends
700  friend Hist operator+(double f, const Hist& h1);
701  friend Hist operator-(double f, const Hist& h1);
702  friend Hist operator*(double f, const Hist& h1);
703  friend Hist operator/(double f, const Hist& h1);
704 
705 private:
706 
707  // Constants: could only be changed in the code itself.
708  static const int NBINMAX, NCOLMAX, NLINES;
709  static const double TOLERANCE, TINY, LARGE, SMALLFRAC, DYAC[];
710  static const char NUMBER[];
711 
712  // Properties and contents of a histogram.
713  string titleSave;
714  int nBin, nFill, nNonFinite;
715  double xMin, xMax;
716  bool linX, doStats;
717  double dx, under, inside, over;
718  vector<double> res, res2;
719 
720  // Sum x^N w, for different powers N, for calculation of unbinned moments.
721  static constexpr int nMoments = 7;
722  double sumxNw[nMoments];
723 
724 };
725 
726 //--------------------------------------------------------------------------
727 
728 // Namespace function declarations; friends of Hist class.
729 
730 // Print a histogram with overloaded << operator.
731 ostream& operator<<(ostream& os, const Hist& h) ;
732 
733 // Print a table out of two histograms with same x axis.
734 void table(const Hist& h1, const Hist& h2, ostream& os = cout,
735  bool printOverUnder = false, bool xMidBin = true) ;
736 void table(const Hist& h1, const Hist& h2, string fileName,
737  bool printOverUnder = false, bool xMidBin = true) ;
738 
739 // Operator overloading with friends
740 Hist operator+(double f, const Hist& h1);
741 Hist operator-(double f, const Hist& h1);
742 Hist operator*(double f, const Hist& h1);
743 Hist operator/(double f, const Hist& h1);
744 
745 //==========================================================================
746 
747 // HistPlot class.
748 // Writes a Python program that can generate PDF plots from Hist histograms.
749 
750 class HistPlot {
751 
752 public:
753 
754  // Constructor requires name of Python program (and adds .py).
755  HistPlot(string pythonName, bool useLegacyIn = false)
756  : nFrame(), nTable(), useLegacy(useLegacyIn) {
757  toPython.open( (pythonName + ".py").c_str() );
758  toPython << "from matplotlib import pyplot as plt" << endl
759  << "from matplotlib.backends.backend_pdf import PdfPages" << endl;
760  nPDF = 0; }
761 
762  // Destructor should do final close.
763  ~HistPlot() { toPython << "pp.close()" << endl; }
764 
765  // New plot frame, with title, x and y labels, x and y sizes.
766  void frame( string frameIn, string titleIn = "", string xLabIn = "",
767  string yLabIn = "", double xSizeIn = 8., double ySizeIn = 6.) {
768  framePrevious = frameName; frameName = frameIn; title = titleIn;
769  xLabel = xLabIn; yLabel = yLabIn; xSize = xSizeIn; ySize = ySizeIn;
770  histos.clear(); styles.clear(); legends.clear(); files.clear();
771  fileStyles.clear(); fileLegends.clear(); filexyerr.clear();}
772 
773  // Add a histogram to the current plot, with optional style and legend.
774  void add( const Hist& histIn, string styleIn = "h",
775  string legendIn = "void") {
776  if (histIn.getBinNumber() == 0) {
777  cout << " Error: histogram is not booked" << endl;
778  return;
779  }
780  histos.push_back(histIn);
781  styles.push_back(styleIn); legends.push_back(legendIn); }
782 
783  // Add a file of (x, y) values not from a histogram, e.g. data points.
784  void addFile( string fileIn, string styleIn = "o",
785  string legendIn = "void", string xyerrIn="") { files.push_back(fileIn);
786  fileStyles.push_back(styleIn); fileLegends.push_back(legendIn);
787  filexyerr.push_back(xyerrIn);}
788 
789  // Plot a frame given the information from the new and add calls.
790  void plot( bool logY = false, bool logX = false, bool userBorders = false);
791  void plot( double xMinUserIn, double xMaxUserIn, double yMinUserIn,
792  double yMaxUserIn, bool logY = false, bool logX = false) {
793  xMinUser = xMinUserIn; xMaxUser = xMaxUserIn; yMinUser = yMinUserIn;
794  yMaxUser = yMaxUserIn; plot( logY, logX, true);}
795 
796  // Omnibus single call when only one histogram in the frame.
797  void plotFrame( string frameIn, const Hist& histIn, string titleIn = "",
798  string xLabIn = "", string yLabIn = "", string styleIn = "h",
799  string legendIn = "void", bool logY = false) {
800  frame( frameIn, titleIn, xLabIn, yLabIn);
801  add( histIn, styleIn, legendIn); plot( logY); }
802 
803 private:
804 
805  // Initialization code.
806  void init( string pythonName);
807 
808  // Stored quantities.
809  ofstream toPython;
810  int nPDF, nFrame, nTable;
811  double xSize, ySize, xMinUser, xMaxUser, yMinUser, yMaxUser;
812  string frameName, framePrevious, title, xLabel, yLabel, fileName, tmpFig;
813  vector<Hist> histos;
814  vector<string> styles, legends, files, fileStyles, fileLegends, filexyerr;
815 
816  // If true, use old linthreshy matplotlib parameter (removed in 3.5.0)
817  bool useLegacy;
818 
819 };
820 
821 //==========================================================================
822 
823 // Methods used for debugging random number sequences.
824 
825 #ifdef RNGDEBUG
826 #define flat() flatDebug(__METHOD_NAME__, __FILE__, __LINE__)
827 #define xexp() xexpDebug(__METHOD_NAME__, __FILE__, __LINE__)
828 #define gauss() gaussDebug(__METHOD_NAME__, __FILE__, __LINE__)
829 #define gamma(...) gammaDebug(__METHOD_NAME__, __FILE__, __LINE__, __VA_ARGS__)
830 #define phaseSpace2(...) phaseSpace2Debug(__METHOD_NAME__, __FILE__, __LINE__,\
831  __VA_ARGS__)
832 #endif
833 
834 //==========================================================================
835 
836 // Randomly shuffle a vector, standard Fisher-Yates algorithm.
837 // This must be defined after possible RNG debugging.
838 
839 template<typename T> void Rndm::shuffle(vector<T>& vec) {
840  for (int i = vec.size() - 1; i > 0; --i)
841  swap(vec[i], vec[floor(flat() * (i + 1))]);
842 }
843 
844 //==========================================================================
845 
846 } // end namespace Pythia8
847 
848 #endif // Pythia8_Basics_H
RndmState getState() const
Get or set the state of the random number generator.
Definition: Basics.h:446
double px() const
Member functions for output.
Definition: Basics.h:56
constexpr double pow2(const double &x)
Powers of small integers - for balance speed/code clarity.
Definition: PythiaStdlib.h:182
double value(int i, int j)
Return value of matrix element.
Definition: Basics.h:286
pair< double, double > gauss2()
Generate two random numbers according to exp(-x^2/2-y^2/2).
Definition: Basics.h:416
friend double m2(const Vec4 &v1)
The squared invariant mass of one or more four-vectors.
Definition: Basics.cc:598
void rot(double=0., double=0.)
Member functions.
Definition: Basics.cc:853
Hist()
Constructors, including copy constructors.
Definition: Basics.h:490
void bstback(const Vec4 &pIn)
Boost given by a Vec4 p; boost in opposite direction.
Definition: Basics.cc:509
void invert()
Invert the rotation and boost.
Definition: Basics.cc:1073
void rotbst(const RotBstMatrix &)
Combine existing rotation/boost matrix with another one.
Definition: Basics.cc:1058
void rescale3(double fac)
Member functions that perform operations.
Definition: Basics.h:95
void frame(string frameIn, string titleIn="", string xLabIn="", string yLabIn="", double xSizeIn=8., double ySizeIn=6.)
New plot frame, with title, x and y labels, x and y sizes.
Definition: Basics.h:766
friend bool isnan(const Vec4 &v)
Check if NaN, INF, or finite.
Definition: Basics.h:140
Vec4 operator*(double f, const Vec4 &v1)
Namespace function declarations; friends of Vec4 class.
Definition: Basics.h:202
void rotaxis(double phiIn, double nx, double ny, double nz)
Azimuthal rotation phi around an arbitrary axis (nz, ny, nz).
Definition: Basics.cc:387
friend double sintheta(const Vec4 &v1, const Vec4 &v2)
Sine of opening angle between two three-vectors, using cross product.
Definition: Basics.cc:682
RotBstMatrix()
Constructors.
Definition: Basics.h:259
RotBstMatrix toCMframe(const Vec4 &p)
Get a RotBstMatrix to rest frame of p.
Definition: Basics.h:319
void title(string titleIn=" ")
Set title of a histogram.
Definition: Basics.h:528
void addFile(string fileIn, string styleIn="o", string legendIn="void", string xyerrIn="")
Add a file of (x, y) values not from a histogram, e.g. data points.
Definition: Basics.h:784
double peekFlat()
Definition: Basics.h:434
Vec4 operator*(Vec4 p) const
Multiplication.
Definition: Basics.h:298
void rotbst(const RotBstMatrix &M)
Arbitrary combination of rotations and boosts defined by 4 * 4 matrix.
Definition: Basics.cc:551
virtual ~RndmEngine()
Destructor.
Definition: Basics.h:360
RotBstMatrix fromCMframe(const Vec4 &p)
Get a RotBstMatrix from rest frame of p.
Definition: Basics.h:323
void plotFrame(string frameIn, const Hist &histIn, string titleIn="", string xLabIn="", string yLabIn="", string styleIn="h", string legendIn="void", bool logY=false)
Omnibus single call when only one histogram in the frame.
Definition: Basics.h:797
const double DYAC[]
Constants for printout: fixed steps on y scale; filling characters.
Definition: Basics.cc:1149
friend double costheta(const Vec4 &v1, const Vec4 &v2)
Cosine of the opening angle between two three-vectors.
Definition: Basics.cc:669
void bstback(const Vec4 &)
Boost so vector originally with same velocity as p is brought to rest.
Definition: Basics.cc:935
Rndm()
Constructors.
Definition: Basics.h:393
Definition: Basics.h:388
void rot(double thetaIn, double phiIn)
Rotation (simple).
Definition: Basics.cc:368
double getNEffective() const
Definition: Basics.h:653
friend ostream & operator<<(ostream &, const Vec4 &v)
Print a four-vector.
Definition: Basics.cc:577
bool operator==(const DireTimesEnd &dip1, const DireTimesEnd &dip2)
The DireTimesEnd class.
Definition: DireTimes.cc:19
friend double m(const Vec4 &v1)
Invariant mass and its square.
Definition: Basics.cc:588
friend Vec4 cross3(const Vec4 &v1, const Vec4 &v2)
The cross product of two three-vectors.
Definition: Basics.cc:626
string getTitle() const
Return title and size of histogram. Also if logarithmic x scale.
Definition: Basics.h:568
A derived class for nuclear PDFs. Needs a pointer for (free) proton PDFs.
Definition: PartonDistributions.h:1060
Vec4(double xIn=0., double yIn=0., double zIn=0., double tIn=0.)
Constructors.
Definition: Basics.h:37
friend bool pShift(Vec4 &p1Move, Vec4 &p2Move, double m1New, double m2New)
Shift four-momenta within pair from old to new masses.
Definition: Basics.cc:787
double getYMean() const
Return average <Y> value.
Definition: Basics.h:607
friend double dot3(const Vec4 &v1, const Vec4 &v2)
Scalar and cross product of 3-vector parts.
Definition: Basics.cc:618
~HistPlot()
Destructor should do final close.
Definition: Basics.h:763
int getEntries(bool alsoNonFinite=true) const
Return number of entries.
Definition: Basics.h:644
friend Vec4 cross4(const Vec4 &a, const Vec4 &b, const Vec4 &c)
Cross-product of three 4-vectors ( p_i = epsilon_{iabc} p_a p_b p_c).
Definition: Basics.cc:638
void table(const Hist &h1, const Hist &h2, ostream &os=cout, bool printOverUnder=false, bool xMidBin=true)
Print a table out of two histograms with same x axis.
Definition: Basics.cc:1612
void bst(double betaX, double betaY, double betaZ)
Boost (simple).
Definition: Basics.cc:434
Definition: Basics.h:355
Definition: Basics.h:485
void bst(double=0., double=0., double=0., double=0.)
Boost with velocity vector (betaX, betaY, betaZ).
Definition: Basics.cc:895
void add(const Hist &histIn, string styleIn="h", string legendIn="void")
Add a histogram to the current plot, with optional style and legend.
Definition: Basics.h:774
Definition: Basics.h:254
Definition: Basics.h:750
Definition: Basics.h:373
friend double cosphi(const Vec4 &v1, const Vec4 &v2)
Cosine of the azimuthal angle between two three-vectors.
Definition: Basics.cc:715
virtual double flat()
Definition: Basics.h:364
double gauss()
Generate random numbers according to exp(-x^2/2).
Definition: Basics.h:413
friend double RRapPhi(const Vec4 &v1, const Vec4 &v2)
R is distance in cylindrical (y/eta, phi) coordinates.
Definition: Basics.cc:764
friend pair< Vec4, Vec4 > getTwoPerpendicular(const Vec4 &v1, const Vec4 &v2)
Create two vectors that are perpendicular to both input vectors.
Definition: Basics.cc:815
HistPlot(string pythonName, bool useLegacyIn=false)
Constructor requires name of Python program (and adds .py).
Definition: Basics.h:755
void shuffle(vector< T > &vec)
Randomly shuffle a vector, standard Fisher-Yates algorithm.
Definition: Basics.h:839
Header for classes to set beam momentum and interaction vertex spread.
Definition: Analysis.h:20
void toCMframe(const Vec4 &, const Vec4 &)
Definition: Basics.cc:961
double eInFrame(const Vec4 &pIn) const
Definition: Basics.cc:566
friend double REtaPhi(const Vec4 &v1, const Vec4 &v2)
Distance in cylindrical (eta, phi) coordinates.
Definition: Basics.cc:775
Hist operator+(double f, const Hist &h1)
Operator overloading with friends.
Definition: Basics.cc:2244
void reset()
Member functions for input.
Definition: Basics.h:46
Vec4 operator-() const
Operator overloading with member functions.
Definition: Basics.h:112
Definition: Basics.h:32
double getWeightSum(bool alsoOverUnder=true) const
Return sum of weights.
Definition: Basics.h:648
double xexp()
Generate random numbers according to x * exp(-x).
Definition: Basics.h:410
double getXMin() const
Return min and max in x and y directions.
Definition: Basics.h:574
void fromCMframe(const Vec4 &, const Vec4 &, bool flip=false)
Definition: Basics.cc:980