Line data Source code
1 : // Basics.h is a part of the PYTHIA event generator.
2 : // Copyright (C) 2015 Torbjorn Sjostrand.
3 : // PYTHIA is licenced under the GNU GPL version 2, 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 :
18 : namespace Pythia8 {
19 :
20 : //==========================================================================
21 :
22 : // RndmEngine is the base class for external random number generators.
23 : // There is only one pure virtual method, that should do the generation.
24 :
25 0 : class RndmEngine {
26 :
27 : public:
28 :
29 : // Destructor.
30 0 : virtual ~RndmEngine() {}
31 :
32 : // A pure virtual method, wherein the derived class method
33 : // generates a random number uniformly distributed between 1 and 1.
34 : virtual double flat() = 0;
35 :
36 : };
37 :
38 : //==========================================================================
39 :
40 : // Rndm class.
41 : // This class handles random number generation according to the
42 : // Marsaglia-Zaman-Tsang algorithm.
43 :
44 : class Rndm {
45 :
46 : public:
47 :
48 : // Constructors.
49 : Rndm() : initRndm(false), seedSave(0), sequence(0),
50 : useExternalRndm(false), rndmEngPtr(0) { }
51 : Rndm(int seedIn) : initRndm(false), seedSave(0), sequence(0),
52 : useExternalRndm(false), rndmEngPtr(0) { init(seedIn);}
53 :
54 : // Possibility to pass in pointer for external random number generation.
55 : bool rndmEnginePtr( RndmEngine* rndmEngPtrIn);
56 :
57 : // Initialize, normally at construction or in first call.
58 : void init(int seedIn = 0) ;
59 :
60 : // Generate next random number uniformly between 0 and 1.
61 : double flat() ;
62 :
63 : // Generate random numbers according to exp(-x).
64 : double exp() { return -log(flat()) ;}
65 :
66 : // Generate random numbers according to x * exp(-x).
67 : double xexp() { return -log(flat() * flat()) ;}
68 :
69 : // Generate random numbers according to exp(-x^2/2).
70 : double gauss() {return sqrt(-2. * log(flat())) * cos(M_PI * flat());}
71 :
72 : // Generate two random numbers according to exp(-x^2/2-y^2/2).
73 : pair<double, double> gauss2() {double r = sqrt(-2. * log(flat()));
74 : double phi = 2. * M_PI * flat();
75 : return pair<double, double>(r * sin(phi), r * cos(phi));}
76 :
77 : // Pick one option among vector of (positive) probabilities.
78 : int pick(const vector<double>& prob) ;
79 :
80 : // Save or read current state to or from a binary file.
81 : bool dumpState(string fileName);
82 : bool readState(string fileName);
83 :
84 : private:
85 :
86 : // Default random number sequence.
87 : static const int DEFAULTSEED;
88 :
89 : // State of the random number generator.
90 : bool initRndm;
91 : int i97, j97, seedSave;
92 : long sequence;
93 : double u[97], c, cd, cm;
94 :
95 : // Pointer for external random number generation.
96 : bool useExternalRndm;
97 : RndmEngine* rndmEngPtr;
98 :
99 : };
100 :
101 : //==========================================================================
102 :
103 : // Forward reference to RotBstMatrix class; needed in Vec4 class.
104 : class RotBstMatrix;
105 :
106 : //--------------------------------------------------------------------------
107 :
108 : // Vec4 class.
109 : // This class implements four-vectors, in energy-momentum space.
110 : // (But can equally well be used to hold space-time four-vectors.)
111 :
112 : class Vec4 {
113 :
114 : public:
115 :
116 : // Constructors.
117 : Vec4(double xIn = 0., double yIn = 0., double zIn = 0., double tIn = 0.)
118 0 : : xx(xIn), yy(yIn), zz(zIn), tt(tIn) { }
119 0 : Vec4(const Vec4& v) : xx(v.xx), yy(v.yy), zz(v.zz), tt(v.tt) { }
120 : Vec4& operator=(const Vec4& v) { if (this != &v) { xx = v.xx; yy = v.yy;
121 : zz = v.zz; tt = v.tt; } return *this; }
122 : Vec4& operator=(double value) { xx = value; yy = value; zz = value;
123 : tt = value; return *this; }
124 :
125 : // Member functions for input.
126 : void reset() {xx = 0.; yy = 0.; zz = 0.; tt = 0.;}
127 : void p(double xIn, double yIn, double zIn, double tIn)
128 : {xx = xIn; yy = yIn; zz = zIn; tt = tIn;}
129 : void p(Vec4 pIn) {xx = pIn.xx; yy = pIn.yy; zz = pIn.zz; tt = pIn.tt;}
130 : void px(double xIn) {xx = xIn;}
131 : void py(double yIn) {yy = yIn;}
132 : void pz(double zIn) {zz = zIn;}
133 : void e(double tIn) {tt = tIn;}
134 :
135 : // Member functions for output.
136 0 : double px() const {return xx;}
137 0 : double py() const {return yy;}
138 0 : double pz() const {return zz;}
139 0 : double e() const {return tt;}
140 : double& operator[](int i) {
141 : if (i == 1) return xx;
142 : else if (i == 2) return yy;
143 : else if (i == 3) return zz;
144 : else return tt;
145 : }
146 : double mCalc() const {double temp = tt*tt - xx*xx - yy*yy - zz*zz;
147 : return (temp >= 0.) ? sqrt(temp) : -sqrt(-temp);}
148 : double m2Calc() const {return tt*tt - xx*xx - yy*yy - zz*zz;}
149 : double pT() const {return sqrt(xx*xx + yy*yy);}
150 : double pT2() const {return xx*xx + yy*yy;}
151 : double pAbs() const {return sqrt(xx*xx + yy*yy + zz*zz);}
152 : double pAbs2() const {return xx*xx + yy*yy + zz*zz;}
153 : double eT() const {double temp = xx*xx + yy*yy;
154 : return tt * sqrt( temp / (temp + zz*zz) );}
155 : double eT2() const {double temp = xx*xx + yy*yy;
156 : return tt*tt * temp / (temp + zz*zz);}
157 : double theta() const {return atan2(sqrt(xx*xx + yy*yy), zz);}
158 : double phi() const {return atan2(yy,xx);}
159 : double thetaXZ() const {return atan2(xx,zz);}
160 : double pPos() const {return tt + zz;}
161 : double pNeg() const {return tt - zz;}
162 : double rap() const {return 0.5 * log( (tt + zz) / (tt - zz) );}
163 : double eta() const {double xyz = sqrt(xx*xx + yy*yy + zz*zz);
164 : return 0.5 * log( (xyz + zz) / (xyz - zz) );}
165 :
166 : // Member functions that perform operations.
167 : void rescale3(double fac) {xx *= fac; yy *= fac; zz *= fac;}
168 : void rescale4(double fac) {xx *= fac; yy *= fac; zz *= fac; tt *= fac;}
169 : void flip3() {xx = -xx; yy = -yy; zz = -zz;}
170 : void flip4() {xx = -xx; yy = -yy; zz = -zz; tt = -tt;}
171 : void rot(double thetaIn, double phiIn);
172 : void rotaxis(double phiIn, double nx, double ny, double nz);
173 : void rotaxis(double phiIn, const Vec4& n);
174 : void bst(double betaX, double betaY, double betaZ);
175 : void bst(double betaX, double betaY, double betaZ, double gamma);
176 : void bst(const Vec4& pIn);
177 : void bst(const Vec4& pIn, double mIn);
178 : void bstback(const Vec4& pIn);
179 : void bstback(const Vec4& pIn, double mIn);
180 : void rotbst(const RotBstMatrix& M);
181 :
182 : // Operator overloading with member functions
183 : Vec4 operator-() {Vec4 tmp; tmp.xx = -xx; tmp.yy = -yy; tmp.zz = -zz;
184 : tmp.tt = -tt; return tmp;}
185 : Vec4& operator+=(const Vec4& v) {xx += v.xx; yy += v.yy; zz += v.zz;
186 : tt += v.tt; return *this;}
187 : Vec4& operator-=(const Vec4& v) {xx -= v.xx; yy -= v.yy; zz -= v.zz;
188 : tt -= v.tt; return *this;}
189 : Vec4& operator*=(double f) {xx *= f; yy *= f; zz *= f;
190 : tt *= f; return *this;}
191 : Vec4& operator/=(double f) {xx /= f; yy /= f; zz /= f;
192 : tt /= f; return *this;}
193 :
194 : // Operator overloading with friends
195 : friend Vec4 operator+(const Vec4& v1, const Vec4& v2);
196 : friend Vec4 operator-(const Vec4& v1, const Vec4& v2);
197 : friend Vec4 operator*(double f, const Vec4& v1);
198 : friend Vec4 operator*(const Vec4& v1, double f);
199 : friend Vec4 operator/(const Vec4& v1, double f);
200 : friend double operator*(const Vec4& v1, const Vec4& v2);
201 :
202 : // Invariant mass of a pair and its square.
203 : friend double m(const Vec4& v1, const Vec4& v2);
204 : friend double m2(const Vec4& v1, const Vec4& v2);
205 :
206 : // Scalar and cross product of 3-vector parts.
207 : friend double dot3(const Vec4& v1, const Vec4& v2);
208 : friend Vec4 cross3(const Vec4& v1, const Vec4& v2);
209 :
210 : // theta is polar angle between v1 and v2.
211 : friend double theta(const Vec4& v1, const Vec4& v2);
212 : friend double costheta(const Vec4& v1, const Vec4& v2);
213 :
214 : // phi is azimuthal angle between v1 and v2 around z axis.
215 : friend double phi(const Vec4& v1, const Vec4& v2);
216 : friend double cosphi(const Vec4& v1, const Vec4& v2);
217 :
218 : // phi is azimuthal angle between v1 and v2 around n axis.
219 : friend double phi(const Vec4& v1, const Vec4& v2, const Vec4& n);
220 : friend double cosphi(const Vec4& v1, const Vec4& v2, const Vec4& n);
221 :
222 : // R is distance in cylindrical (y/eta, phi) coordinates.
223 : friend double RRapPhi(const Vec4& v1, const Vec4& v2);
224 : friend double REtaPhi(const Vec4& v1, const Vec4& v2);
225 :
226 : // Print a four-vector.
227 : friend ostream& operator<<(ostream&, const Vec4& v) ;
228 :
229 : private:
230 :
231 : // Constants: could only be changed in the code itself.
232 : static const double TINY;
233 :
234 : // The four-vector data members.
235 : double xx, yy, zz, tt;
236 :
237 : };
238 :
239 : //--------------------------------------------------------------------------
240 :
241 : // Namespace function declarations; friends of Vec4 class.
242 :
243 : // Implementation of operator overloading with friends.
244 :
245 : inline Vec4 operator+(const Vec4& v1, const Vec4& v2)
246 : {Vec4 v = v1 ; return v += v2;}
247 :
248 : inline Vec4 operator-(const Vec4& v1, const Vec4& v2)
249 : {Vec4 v = v1 ; return v -= v2;}
250 :
251 : inline Vec4 operator*(double f, const Vec4& v1)
252 : {Vec4 v = v1; return v *= f;}
253 :
254 : inline Vec4 operator*(const Vec4& v1, double f)
255 : {Vec4 v = v1; return v *= f;}
256 :
257 : inline Vec4 operator/(const Vec4& v1, double f)
258 : {Vec4 v = v1; return v /= f;}
259 :
260 : inline double operator*(const Vec4& v1, const Vec4& v2)
261 : {return v1.tt*v2.tt - v1.xx*v2.xx - v1.yy*v2.yy - v1.zz*v2.zz;}
262 :
263 : // Invariant mass of a pair and its square.
264 : double m(const Vec4& v1, const Vec4& v2);
265 : double m2(const Vec4& v1, const Vec4& v2);
266 :
267 : // Scalar and cross product of 3-vector parts.
268 : double dot3(const Vec4& v1, const Vec4& v2);
269 : Vec4 cross3(const Vec4& v1, const Vec4& v2);
270 :
271 : // theta is polar angle between v1 and v2.
272 : double theta(const Vec4& v1, const Vec4& v2);
273 : double costheta(const Vec4& v1, const Vec4& v2);
274 :
275 : // phi is azimuthal angle between v1 and v2 around z axis.
276 : double phi(const Vec4& v1, const Vec4& v2);
277 : double cosphi(const Vec4& v1, const Vec4& v2);
278 :
279 : // phi is azimuthal angle between v1 and v2 around n axis.
280 : double phi(const Vec4& v1, const Vec4& v2, const Vec4& n);
281 : double cosphi(const Vec4& v1, const Vec4& v2, const Vec4& n);
282 :
283 : // R is distance in cylindrical (y/eta, phi) coordinates.
284 : double RRapPhi(const Vec4& v1, const Vec4& v2);
285 : double REtaPhi(const Vec4& v1, const Vec4& v2);
286 :
287 : // Print a four-vector.
288 : ostream& operator<<(ostream&, const Vec4& v) ;
289 :
290 : //==========================================================================
291 :
292 : // RotBstMatrix class.
293 : // This class implements 4 * 4 matrices that encode an arbitrary combination
294 : // of rotations and boosts, that can be applied to Vec4 four-vectors.
295 :
296 : class RotBstMatrix {
297 :
298 : public:
299 :
300 : // Constructors.
301 : RotBstMatrix() {for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j)
302 : { M[i][j] = (i==j) ? 1. : 0.; } } }
303 : RotBstMatrix(const RotBstMatrix& Min) {
304 : for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) {
305 : M[i][j] = Min.M[i][j]; } } }
306 : RotBstMatrix& operator=(const RotBstMatrix& Min) {if (this != &Min) {
307 : for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) {
308 : M[i][j] = Min.M[i][j]; } } } return *this; }
309 :
310 : // Member functions.
311 : void rot(double = 0., double = 0.);
312 : void rot(const Vec4& p);
313 : void bst(double = 0., double = 0., double = 0.);
314 : void bst(const Vec4&);
315 : void bstback(const Vec4&);
316 : void bst(const Vec4&, const Vec4&);
317 : void toCMframe(const Vec4&, const Vec4&);
318 : void fromCMframe(const Vec4&, const Vec4&);
319 : void rotbst(const RotBstMatrix&);
320 : void invert();
321 : void reset();
322 :
323 : // Crude estimate deviation from unit matrix.
324 : double deviation() const;
325 :
326 : // Print a transformation matrix.
327 : friend ostream& operator<<(ostream&, const RotBstMatrix&) ;
328 :
329 : // Private members to be accessible from Vec4.
330 : friend class Vec4;
331 :
332 : private:
333 :
334 : // Constants: could only be changed in the code itself.
335 : static const double TINY;
336 :
337 : // The rotation-and-boost matrix data members.
338 : double M[4][4];
339 :
340 : };
341 :
342 : //--------------------------------------------------------------------------
343 :
344 : // Namespace function declaration; friend of RotBstMatrix class.
345 :
346 : // Print a transformation matrix.
347 : ostream& operator<<(ostream&, const RotBstMatrix&) ;
348 :
349 : //==========================================================================
350 :
351 : // Hist class.
352 : // This class handles a single histogram at a time.
353 :
354 : class Hist{
355 :
356 : public:
357 :
358 : // Constructors, including copy constructors.
359 : Hist() {;}
360 : Hist(string titleIn, int nBinIn = 100, double xMinIn = 0.,
361 : double xMaxIn = 1.) {
362 : book(titleIn, nBinIn, xMinIn, xMaxIn);}
363 : Hist(const Hist& h)
364 : : title(h.title), nBin(h.nBin), nFill(h.nFill), xMin(h.xMin),
365 : xMax(h.xMax), dx(h.dx), under(h.under), inside(h.inside),
366 : over(h.over), res(h.res) { }
367 : Hist(string titleIn, const Hist& h)
368 : : title(titleIn), nBin(h.nBin), nFill(h.nFill), xMin(h.xMin),
369 : xMax(h.xMax), dx(h.dx), under(h.under), inside(h.inside),
370 : over(h.over), res(h.res) { }
371 : Hist& operator=(const Hist& h) { if(this != &h) {
372 : nBin = h.nBin; nFill = h.nFill; xMin = h.xMin; xMax = h.xMax;
373 : dx = h.dx; under = h.under; inside = h.inside; over = h.over;
374 : res = h.res; } return *this; }
375 :
376 : // Book a histogram.
377 : void book(string titleIn = " ", int nBinIn = 100, double xMinIn = 0.,
378 : double xMaxIn = 1.) ;
379 :
380 : // Set title of a histogram.
381 : void name(string titleIn = " ") {title = titleIn; }
382 :
383 : // Reset bin contents.
384 : void null() ;
385 :
386 : // Fill bin with weight.
387 : void fill(double x, double w = 1.) ;
388 :
389 : // Print a histogram with overloaded << operator.
390 : friend ostream& operator<<(ostream& os, const Hist& h) ;
391 :
392 : // Print histogram contents as a table (e.g. for Gnuplot).
393 : void table(ostream& os = cout, bool printOverUnder = false,
394 : bool xMidBin = true) const ;
395 : void table(string fileName, bool printOverUnder = false,
396 : bool xMidBin = true) const { ofstream streamName(fileName.c_str());
397 : table(streamName, printOverUnder, xMidBin);}
398 :
399 : // Print a table out of two histograms with same x axis.
400 : friend void table(const Hist& h1, const Hist& h2, ostream& os,
401 : bool printOverUnder, bool xMidBin) ;
402 : friend void table(const Hist& h1, const Hist& h2, string fileName,
403 : bool printOverUnder, bool xMidBin) ;
404 :
405 : // Return content of specific bin: 0 gives underflow and nBin+1 overflow.
406 : double getBinContent(int iBin) const;
407 :
408 : // Return number of entries.
409 : int getEntries() const {return nFill; }
410 :
411 : // Check whether another histogram has same size and limits.
412 : bool sameSize(const Hist& h) const ;
413 :
414 : // Take logarithm (base 10 or e) of bin contents.
415 : void takeLog(bool tenLog = true) ;
416 :
417 : // Take square root of bin contents.
418 : void takeSqrt() ;
419 :
420 : // Operator overloading with member functions
421 : Hist& operator+=(const Hist& h) ;
422 : Hist& operator-=(const Hist& h) ;
423 : Hist& operator*=(const Hist& h) ;
424 : Hist& operator/=(const Hist& h) ;
425 : Hist& operator+=(double f) ;
426 : Hist& operator-=(double f) ;
427 : Hist& operator*=(double f) ;
428 : Hist& operator/=(double f) ;
429 :
430 : // Operator overloading with friends
431 : friend Hist operator+(double f, const Hist& h1);
432 : friend Hist operator+(const Hist& h1, double f);
433 : friend Hist operator+(const Hist& h1, const Hist& h2);
434 : friend Hist operator-(double f, const Hist& h1);
435 : friend Hist operator-(const Hist& h1, double f);
436 : friend Hist operator-(const Hist& h1, const Hist& h2);
437 : friend Hist operator*(double f, const Hist& h1);
438 : friend Hist operator*(const Hist& h1, double f);
439 : friend Hist operator*(const Hist& h1, const Hist& h2);
440 : friend Hist operator/(double f, const Hist& h1);
441 : friend Hist operator/(const Hist& h1, double f);
442 : friend Hist operator/(const Hist& h1, const Hist& h2);
443 :
444 : private:
445 :
446 : // Constants: could only be changed in the code itself.
447 : static const int NBINMAX, NCOLMAX, NLINES;
448 : static const double TOLERANCE, TINY, LARGE, SMALLFRAC, DYAC[];
449 : static const char NUMBER[];
450 :
451 : // Properties and contents of a histogram.
452 : string title;
453 : int nBin, nFill;
454 : double xMin, xMax, dx, under, inside, over;
455 : vector<double> res;
456 :
457 : };
458 :
459 : //--------------------------------------------------------------------------
460 :
461 : // Namespace function declarations; friends of Hist class.
462 :
463 : // Print a histogram with overloaded << operator.
464 : ostream& operator<<(ostream& os, const Hist& h) ;
465 :
466 : // Print a table out of two histograms with same x axis.
467 : void table(const Hist& h1, const Hist& h2, ostream& os = cout,
468 : bool printOverUnder = false, bool xMidBin = true) ;
469 : void table(const Hist& h1, const Hist& h2, string fileName,
470 : bool printOverUnder = false, bool xMidBin = true) ;
471 :
472 : // Operator overloading with friends
473 : Hist operator+(double f, const Hist& h1);
474 : Hist operator+(const Hist& h1, double f);
475 : Hist operator+(const Hist& h1, const Hist& h2);
476 : Hist operator-(double f, const Hist& h1);
477 : Hist operator-(const Hist& h1, double f);
478 : Hist operator-(const Hist& h1, const Hist& h2);
479 : Hist operator*(double f, const Hist& h1);
480 : Hist operator*(const Hist& h1, double f);
481 : Hist operator*(const Hist& h1, const Hist& h2);
482 : Hist operator/(double f, const Hist& h1);
483 : Hist operator/(const Hist& h1, double f);
484 : Hist operator/(const Hist& h1, const Hist& h2);
485 :
486 : //==========================================================================
487 :
488 : } // end namespace Pythia8
489 :
490 : #endif // end Pythia8_Basics_H
|