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