1 // Basics.cc is a part of the PYTHIA event generator.
2 // Copyright (C) 2012 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.
6 // Function definitions (not found in the header) for the Rndm, Vec4,
7 // RotBstMatrix and Hist classes, and some related global functions.
11 // Access time information.
16 //==========================================================================
19 // This class handles random number generation according to the
20 // Marsaglia-Zaman-Tsang algorithm
22 //--------------------------------------------------------------------------
24 // Constants: could be changed here if desired, but normally should not.
25 // These are of technical nature, as described for each.
27 // The default seed, i.e. the Marsaglia-Zaman random number sequence.
28 const int Rndm::DEFAULTSEED = 19780503;
30 //--------------------------------------------------------------------------
32 // Method to pass in pointer for external random number generation.
34 bool Rndm::rndmEnginePtr( RndmEngine* rndmEngPtrIn) {
37 if (rndmEngPtrIn == 0) return false;
38 rndmEngPtr = rndmEngPtrIn;
39 useExternalRndm = true;
46 //--------------------------------------------------------------------------
48 // Initialize, normally at construction or in first call.
50 void Rndm::init(int seedIn) {
52 // Pick seed in convenient way. Assure it to be non-negative.
54 if (seedIn < 0) seed = DEFAULTSEED;
55 else if (seedIn == 0) seed = int(time(0));
56 if (seed < 0) seed = -seed;
59 int ij = (seed/30082) % 31329;
60 int kl = seed % 30082;
61 int i = (ij/177) % 177 + 2;
63 int k = (kl/169) % 178 + 1;
66 // Initialize random number array.
67 for (int ii = 0; ii < 97; ++ii) {
70 for (int jj = 0; jj < 48; ++jj) {
71 int m = (( (i*j)%179 )*k) % 179;
76 if ( (l*m) % 64 >= 32) s += t;
82 // Initialize other variables.
84 for (int i24 = 0; i24 < 24; ++i24) twom24 *= 0.5;
86 cd = 7654321. * twom24;
87 cm = 16777213. * twom24;
98 //--------------------------------------------------------------------------
100 // Generate next random number uniformly between 0 and 1.
102 double Rndm::flat() {
104 // Use external random number generator if such has been linked.
105 if (useExternalRndm) return rndmEngPtr->flat();
107 // Ensure that already initialized.
108 if (!initRndm) init(DEFAULTSEED);
110 // Find next random number and update saved state.
114 uni = u[i97] - u[j97];
115 if (uni < 0.) uni += 1.;
117 if (--i97 < 0) i97 = 96;
118 if (--j97 < 0) j97 = 96;
122 if(uni < 0.) uni += 1.;
123 } while (uni <= 0. || uni >= 1.);
128 //--------------------------------------------------------------------------
130 // Pick one option among vector of (positive) probabilities.
132 int Rndm::pick(const vector<double>& prob) {
135 for (int i = 0; i < int(prob.size()); ++i) work += prob[i];
138 do work -= prob[++index];
139 while (work > 0. && index < int(prob.size()));
144 //--------------------------------------------------------------------------
146 // Save current state of the random number generator to a binary file.
148 bool Rndm::dumpState(string fileName) {
150 // Open file as output stream.
151 const char* fn = fileName.c_str();
152 ofstream ofs(fn, ios::binary);
155 cout << " Rndm::dumpState: could not open output file" << endl;
159 // Write the state of the generator on the file.
160 ofs.write((char *) &seedSave, sizeof(int));
161 ofs.write((char *) &sequence, sizeof(long));
162 ofs.write((char *) &i97, sizeof(int));
163 ofs.write((char *) &j97, sizeof(int));
164 ofs.write((char *) &c, sizeof(double));
165 ofs.write((char *) &cd, sizeof(double));
166 ofs.write((char *) &cm, sizeof(double));
167 ofs.write((char *) &u, sizeof(double) * 97);
169 // Write confirmation on cout.
170 cout << " PYTHIA Rndm::dumpState: seed = " << seedSave
171 << ", sequence no = " << sequence << endl;
176 //--------------------------------------------------------------------------
178 // Read in the state of the random number generator from a binary file.
180 bool Rndm::readState(string fileName) {
182 // Open file as input stream.
183 const char* fn = fileName.c_str();
184 ifstream ifs(fn, ios::binary);
187 cout << " Rndm::readState: could not open input file" << endl;
191 // Read the state of the generator from the file.
192 ifs.read((char *) &seedSave, sizeof(int));
193 ifs.read((char *) &sequence, sizeof(long));
194 ifs.read((char *) &i97, sizeof(int));
195 ifs.read((char *) &j97, sizeof(int));
196 ifs.read((char *) &c, sizeof(double));
197 ifs.read((char *) &cd, sizeof(double));
198 ifs.read((char *) &cm, sizeof(double));
199 ifs.read((char *) &u, sizeof(double) *97);
201 // Write confirmation on cout.
202 cout << " PYTHIA Rndm::readState: seed " << seedSave
203 << ", sequence no = " << sequence << endl;
208 //==========================================================================
211 // This class implements four-vectors, in energy-momentum space.
212 // (But could also be used to hold space-time four-vectors.)
214 //--------------------------------------------------------------------------
216 // Constants: could be changed here if desired, but normally should not.
217 // These are of technical nature, as described for each.
219 // Small number to avoid division by zero.
220 const double Vec4::TINY = 1e-20;
222 //--------------------------------------------------------------------------
224 // Rotation (simple).
226 void Vec4::rot(double thetaIn, double phiIn) {
228 double cthe = cos(thetaIn);
229 double sthe = sin(thetaIn);
230 double cphi = cos(phiIn);
231 double sphi = sin(phiIn);
232 double tmpx = cthe * cphi * xx - sphi * yy + sthe * cphi * zz;
233 double tmpy = cthe * sphi * xx + cphi * yy + sthe * sphi * zz;
234 double tmpz = -sthe * xx + cthe * zz;
241 //--------------------------------------------------------------------------
243 // Azimuthal rotation phi around an arbitrary axis (nz, ny, nz).
245 void Vec4::rotaxis(double phiIn, double nx, double ny, double nz) {
247 double norm = 1./sqrt(nx*nx + ny*ny + nz*nz);
251 double cphi = cos(phiIn);
252 double sphi = sin(phiIn);
253 double comb = (nx * xx + ny * yy + nz * zz) * (1. - cphi);
254 double tmpx = cphi * xx + comb * nx + sphi * (ny * zz - nz * yy);
255 double tmpy = cphi * yy + comb * ny + sphi * (nz * xx - nx * zz);
256 double tmpz = cphi * zz + comb * nz + sphi * (nx * yy - ny * xx);
263 //--------------------------------------------------------------------------
265 // Azimuthal rotation phi around an arbitrary (3-vector component of) axis.
267 void Vec4::rotaxis(double phiIn, const Vec4& n) {
272 double norm = 1./sqrt(nx*nx + ny*ny + nz*nz);
276 double cphi = cos(phiIn);
277 double sphi = sin(phiIn);
278 double comb = (nx * xx + ny * yy + nz * zz) * (1. - cphi);
279 double tmpx = cphi * xx + comb * nx + sphi * (ny * zz - nz * yy);
280 double tmpy = cphi * yy + comb * ny + sphi * (nz * xx - nx * zz);
281 double tmpz = cphi * zz + comb * nz + sphi * (nx * yy - ny * xx);
288 //--------------------------------------------------------------------------
292 void Vec4::bst(double betaX, double betaY, double betaZ) {
294 double beta2 = betaX*betaX + betaY*betaY + betaZ*betaZ;
295 double gamma = 1. / sqrt(1. - beta2);
296 double prod1 = betaX * xx + betaY * yy + betaZ * zz;
297 double prod2 = gamma * (gamma * prod1 / (1. + gamma) + tt);
301 tt = gamma * (tt + prod1);
305 //--------------------------------------------------------------------------
307 // Boost (simple, given gamma).
309 void Vec4::bst(double betaX, double betaY, double betaZ, double gamma) {
311 double prod1 = betaX * xx + betaY * yy + betaZ * zz;
312 double prod2 = gamma * (gamma * prod1 / (1. + gamma) + tt);
316 tt = gamma * (tt + prod1);
320 //--------------------------------------------------------------------------
322 // Boost given by a Vec4 p.
324 void Vec4::bst(const Vec4& pIn) {
326 double betaX = pIn.xx / pIn.tt;
327 double betaY = pIn.yy / pIn.tt;
328 double betaZ = pIn.zz / pIn.tt;
329 double beta2 = betaX*betaX + betaY*betaY + betaZ*betaZ;
330 double gamma = 1. / sqrt(1. - beta2);
331 double prod1 = betaX * xx + betaY * yy + betaZ * zz;
332 double prod2 = gamma * (gamma * prod1 / (1. + gamma) + tt);
336 tt = gamma * (tt + prod1);
340 //--------------------------------------------------------------------------
342 // Boost given by a Vec4 p and double m.
344 void Vec4::bst(const Vec4& pIn, double mIn) {
346 double betaX = pIn.xx / pIn.tt;
347 double betaY = pIn.yy / pIn.tt;
348 double betaZ = pIn.zz / pIn.tt;
349 double gamma = pIn.tt / mIn;
350 double prod1 = betaX * xx + betaY * yy + betaZ * zz;
351 double prod2 = gamma * (gamma * prod1 / (1. + gamma) + tt);
355 tt = gamma * (tt + prod1);
359 //--------------------------------------------------------------------------
361 // Boost given by a Vec4 p; boost in opposite direction.
363 void Vec4::bstback(const Vec4& pIn) {
365 double betaX = -pIn.xx / pIn.tt;
366 double betaY = -pIn.yy / pIn.tt;
367 double betaZ = -pIn.zz / pIn.tt;
368 double beta2 = betaX*betaX + betaY*betaY + betaZ*betaZ;
369 double gamma = 1. / sqrt(1. - beta2);
370 double prod1 = betaX * xx + betaY * yy + betaZ * zz;
371 double prod2 = gamma * (gamma * prod1 / (1. + gamma) + tt);
375 tt = gamma * (tt + prod1);
379 //--------------------------------------------------------------------------
381 // Boost given by a Vec4 p and double m; boost in opposite direction.
383 void Vec4::bstback(const Vec4& pIn, double mIn) {
385 double betaX = -pIn.xx / pIn.tt;
386 double betaY = -pIn.yy / pIn.tt;
387 double betaZ = -pIn.zz / pIn.tt;
388 double gamma = pIn.tt / mIn;
389 double prod1 = betaX * xx + betaY * yy + betaZ * zz;
390 double prod2 = gamma * (gamma * prod1 / (1. + gamma) + tt);
394 tt = gamma * (tt + prod1);
398 //--------------------------------------------------------------------------
400 // Arbitrary combination of rotations and boosts defined by 4 * 4 matrix.
402 void Vec4::rotbst(const RotBstMatrix& M) {
404 double x = xx; double y = yy; double z = zz; double t = tt;
405 tt = M.M[0][0] * t + M.M[0][1] * x + M.M[0][2] * y + M.M[0][3] * z;
406 xx = M.M[1][0] * t + M.M[1][1] * x + M.M[1][2] * y + M.M[1][3] * z;
407 yy = M.M[2][0] * t + M.M[2][1] * x + M.M[2][2] * y + M.M[2][3] * z;
408 zz = M.M[3][0] * t + M.M[3][1] * x + M.M[3][2] * y + M.M[3][3] * z;
412 //--------------------------------------------------------------------------
414 // The invariant mass of two four-vectors.
416 double m(const Vec4& v1, const Vec4& v2) {
417 double m2 = pow2(v1.tt + v2.tt) - pow2(v1.xx + v2.xx)
418 - pow2(v1.yy + v2.yy) - pow2(v1.zz + v2.zz);
419 return (m2 > 0.) ? sqrt(m2) : 0.;
422 //--------------------------------------------------------------------------
424 // The squared invariant mass of two four-vectors.
426 double m2(const Vec4& v1, const Vec4& v2) {
427 double m2 = pow2(v1.tt + v2.tt) - pow2(v1.xx + v2.xx)
428 - pow2(v1.yy + v2.yy) - pow2(v1.zz + v2.zz);
432 //--------------------------------------------------------------------------
434 // The scalar product of two three-vectors.
436 double dot3(const Vec4& v1, const Vec4& v2) {
437 return v1.xx*v2.xx + v1.yy*v2.yy + v1.zz*v2.zz;
440 //--------------------------------------------------------------------------
442 // The cross product of two three-vectors.
444 Vec4 cross3(const Vec4& v1, const Vec4& v2) {
446 v.xx = v1.yy * v2.zz - v1.zz * v2.yy;
447 v.yy = v1.zz * v2.xx - v1.xx * v2.zz;
448 v.zz = v1.xx * v2.yy - v1.yy * v2.xx; return v;
451 //--------------------------------------------------------------------------
453 // Opening angle between two three-vectors.
455 double theta(const Vec4& v1, const Vec4& v2) {
456 double cthe = (v1.xx * v2.xx + v1.yy * v2.yy + v1.zz * v2.zz)
457 / sqrt( (v1.xx*v1.xx + v1.yy*v1.yy + v1.zz*v1.zz)
458 * (v2.xx*v2.xx + v2.yy*v2.yy + v2.zz*v2.zz) );
459 cthe = max(-1., min(1., cthe));
463 //--------------------------------------------------------------------------
465 // Cosine of the opening angle between two three-vectors.
467 double costheta(const Vec4& v1, const Vec4& v2) {
468 double cthe = (v1.xx * v2.xx + v1.yy * v2.yy + v1.zz * v2.zz)
469 / sqrt( (v1.xx*v1.xx + v1.yy*v1.yy + v1.zz*v1.zz)
470 * (v2.xx*v2.xx + v2.yy*v2.yy + v2.zz*v2.zz) );
471 cthe = max(-1., min(1., cthe));
475 //--------------------------------------------------------------------------
477 // Azimuthal angle between two three-vectors.
479 double phi(const Vec4& v1, const Vec4& v2) {
480 double cphi = (v1.xx * v2.xx + v1.yy * v2.yy) / sqrt( max( Vec4::TINY,
481 (v1.xx*v1.xx + v1.yy*v1.yy) * (v2.xx*v2.xx + v2.yy*v2.yy) ));
482 cphi = max(-1., min(1., cphi));
486 //--------------------------------------------------------------------------
488 // Cosine of the azimuthal angle between two three-vectors.
490 double cosphi(const Vec4& v1, const Vec4& v2) {
491 double cphi = (v1.xx * v2.xx + v1.yy * v2.yy) / sqrt( max( Vec4::TINY,
492 (v1.xx*v1.xx + v1.yy*v1.yy) * (v2.xx*v2.xx + v2.yy*v2.yy) ));
493 cphi = max(-1., min(1., cphi));
497 //--------------------------------------------------------------------------
499 // Azimuthal angle between two three-vectors around a third.
501 double phi(const Vec4& v1, const Vec4& v2, const Vec4& n) {
502 double nx = n.xx; double ny = n.yy; double nz = n.zz;
503 double norm = 1. / sqrt(nx*nx + ny*ny + nz*nz);
504 nx *= norm; ny *=norm; nz *=norm;
505 double v1s = v1.xx * v1.xx + v1.yy * v1.yy + v1.zz * v1.zz;
506 double v2s = v2.xx * v2.xx + v2.yy * v2.yy + v2.zz * v2.zz;
507 double v1v2 = v1.xx * v2.xx + v1.yy * v2.yy + v1.zz * v2.zz;
508 double v1n = v1.xx * nx + v1.yy * ny + v1.zz * nz;
509 double v2n = v2.xx * nx + v2.yy * ny + v2.zz * nz;
510 double cphi = (v1v2 - v1n * v2n) / sqrt( max( Vec4::TINY,
511 (v1s - v1n*v1n) * (v2s - v2n*v2n) ));
512 cphi = max(-1., min(1., cphi));
516 //--------------------------------------------------------------------------
518 // Cosine of the azimuthal angle between two three-vectors around a third.
520 double cosphi(const Vec4& v1, const Vec4& v2, const Vec4& n) {
521 double nx = n.xx; double ny = n.yy; double nz = n.zz;
522 double norm = 1. / sqrt(nx*nx + ny*ny + nz*nz);
523 nx *= norm; ny *=norm; nz *=norm;
524 double v1s = v1.xx * v1.xx + v1.yy * v1.yy + v1.zz * v1.zz;
525 double v2s = v2.xx * v2.xx + v2.yy * v2.yy + v2.zz * v2.zz;
526 double v1v2 = v1.xx * v2.xx + v1.yy * v2.yy + v1.zz * v2.zz;
527 double v1n = v1.xx * nx + v1.yy * ny + v1.zz * nz;
528 double v2n = v2.xx * nx + v2.yy * ny + v2.zz * nz;
529 double cphi = (v1v2 - v1n * v2n) / sqrt( max( Vec4::TINY,
530 (v1s - v1n*v1n) * (v2s - v2n*v2n) ));
531 cphi = max(-1., min(1., cphi));
535 //--------------------------------------------------------------------------
537 // Print a four-vector: also operator overloading with friend.
539 ostream& operator<<(ostream& os, const Vec4& v) {
540 os << fixed << setprecision(3) << " " << setw(9) << v.xx << " "
541 << setw(9) << v.yy << " " << setw(9) << v.zz << " " << setw(9)
542 << v.tt << " (" << setw(9) << v.mCalc() << ")\n";
546 //==========================================================================
548 // RotBstMatrix class.
549 // This class implements 4 * 4 matrices that encode an arbitrary combination
550 // of rotations and boosts, that can be applied to Vec4 four-vectors.
552 //--------------------------------------------------------------------------
554 // Constants: could be changed here if desired, but normally should not.
555 // These are of technical nature, as described for each.
557 // Small number to avoid division by zero.
558 const double RotBstMatrix::TINY = 1e-20;
560 //--------------------------------------------------------------------------
562 // Rotate by polar angle theta and azimuthal angle phi.
564 void RotBstMatrix::rot(double theta, double phi) {
566 // Set up rotation matrix.
567 double cthe = cos(theta);
568 double sthe = sin(theta);
569 double cphi = cos(phi);
570 double sphi = sin(phi);
571 double Mrot[4][4] = {
573 {0., cthe * cphi, - sphi, sthe * cphi},
574 {0., cthe * sphi, cphi, sthe * sphi},
575 {0., -sthe, 0., cthe } };
577 // Rotate current matrix accordingly.
579 for (int i = 0; i < 4; ++i)
580 for (int j = 0; j < 4; ++j)
581 Mtmp[i][j] = M[i][j];
582 for (int i = 0; i < 4; ++i)
583 for (int j = 0; j < 4; ++j)
584 M[i][j] = Mrot[i][0] * Mtmp[0][j] + Mrot[i][1] * Mtmp[1][j]
585 + Mrot[i][2] * Mtmp[2][j] + Mrot[i][3] * Mtmp[3][j];
589 //--------------------------------------------------------------------------
591 // Rotate so that vector originally along z axis becomes parallel with p.
593 void RotBstMatrix::rot(const Vec4& p) {
595 double theta = p.theta();
596 double phi = p.phi();
602 //--------------------------------------------------------------------------
604 // Boost with velocity vector (betaX, betaY, betaZ).
606 void RotBstMatrix::bst(double betaX, double betaY, double betaZ) {
608 // Set up boost matrix.
609 double gm = 1. / sqrt( max( TINY, 1. - betaX*betaX - betaY*betaY
611 double gf = gm*gm / (1. + gm);
612 double Mbst[4][4] = {
613 { gm, gm*betaX, gm*betaY, gm*betaZ },
614 { gm*betaX, 1. + gf*betaX*betaX, gf*betaX*betaY, gf*betaX*betaZ },
615 { gm*betaY, gf*betaY*betaX, 1. + gf*betaY*betaY, gf*betaY*betaZ },
616 { gm*betaZ, gf*betaZ*betaX, gf*betaZ*betaY, 1. + gf*betaZ*betaZ } };
618 // Boost current matrix correspondingly.
620 for (int i = 0; i < 4; ++i)
621 for (int j = 0; j < 4; ++j)
622 Mtmp[i][j] = M[i][j];
623 for (int i = 0; i < 4; ++i)
624 for (int j = 0; j < 4; ++j)
625 M[i][j] = Mbst[i][0] * Mtmp[0][j] + Mbst[i][1] * Mtmp[1][j]
626 + Mbst[i][2] * Mtmp[2][j] + Mbst[i][3] * Mtmp[3][j];
630 //--------------------------------------------------------------------------
632 // Boost so that vector originally at rest obtains same velocity as p.
634 void RotBstMatrix::bst(const Vec4& p) {
635 double betaX = p.px() / p.e();
636 double betaY = p.py() / p.e();
637 double betaZ = p.pz() / p.e();
638 bst(betaX, betaY, betaZ);
641 //--------------------------------------------------------------------------
643 // Boost so vector originally with same velocity as p is brought to rest.
645 void RotBstMatrix::bstback(const Vec4& p) {
646 double betaX = -p.px() / p.e();
647 double betaY = -p.py() / p.e();
648 double betaZ = -p.pz() / p.e();
649 bst(betaX, betaY, betaZ);
652 //--------------------------------------------------------------------------
654 // Boost that transforms p1 to p2, where p1^2 = p2^2 is assumed.
656 void RotBstMatrix::bst(const Vec4& p1, const Vec4& p2) {
657 double eSum = p1.e() + p2.e();
658 double betaX = (p2.px() - p1.px()) / eSum;
659 double betaY = (p2.py() - p1.py()) / eSum;
660 double betaZ = (p2.pz() - p1.pz()) / eSum;
661 double fac = 2. / (1. + betaX*betaX + betaY*betaY + betaZ*betaZ);
662 betaX *= fac; betaY *= fac; betaZ *= fac;
663 bst(betaX, betaY, betaZ);
666 //--------------------------------------------------------------------------
668 // Boost and rotation that transforms from p1 and p2
669 // to their rest frame with p1 along +z axis.
671 void RotBstMatrix::toCMframe(const Vec4& p1, const Vec4& p2) {
675 double theta = dir.theta();
676 double phi = dir.phi();
682 //--------------------------------------------------------------------------
684 // Rotation and boost that transforms from rest frame of p1 and p2
685 // with p1 along +z axis to actual frame of p1 and p2. (Inverse of above.)
687 void RotBstMatrix::fromCMframe(const Vec4& p1, const Vec4& p2) {
691 double theta = dir.theta();
692 double phi = dir.phi();
698 //--------------------------------------------------------------------------
700 // Combine existing rotation/boost matrix with another one.
702 void RotBstMatrix::rotbst(const RotBstMatrix& Mrb) {
704 for (int i = 0; i < 4; ++i)
705 for (int j = 0; j < 4; ++j)
706 Mtmp[i][j] = M[i][j];
707 for (int i = 0; i < 4; ++i)
708 for (int j = 0; j < 4; ++j)
709 M[i][j] = Mrb.M[i][0] * Mtmp[0][j] + Mrb.M[i][1] * Mtmp[1][j]
710 + Mrb.M[i][2] * Mtmp[2][j] + Mrb.M[i][3] * Mtmp[3][j];
713 //--------------------------------------------------------------------------
715 // Invert the rotation and boost.
717 void RotBstMatrix::invert() {
719 for (int i = 0; i < 4; ++i)
720 for (int j = 0; j < 4; ++j)
721 Mtmp[i][j] = M[i][j];
722 for (int i = 0; i < 4; ++i)
723 for (int j = 0; j < 4; ++j)
724 M[i][j] = ( (i == 0 && j > 0) || (i > 0 && j == 0) )
725 ? - Mtmp[j][i] : Mtmp[j][i];
728 //--------------------------------------------------------------------------
730 // Reset to diagonal matrix.
732 void RotBstMatrix::reset() {
733 for (int i = 0; i < 4; ++i)
734 for (int j = 0; j < 4; ++j)
735 M[i][j] = (i==j) ? 1. : 0.;
738 //--------------------------------------------------------------------------
740 // Crude estimate deviation from unit matrix.
742 double RotBstMatrix::deviation() const {
744 for (int i = 0; i < 4; ++i)
745 for (int j = 0; j < 4; ++j)
746 devSum += (i==j) ? abs(M[i][j] - 1.) : abs(M[i][j]);
750 //--------------------------------------------------------------------------
752 // Print a rotation and boost matrix: operator overloading with friend.
754 ostream& operator<<(ostream& os, const RotBstMatrix& M) {
755 os << fixed << setprecision(5) << " Rotation/boost matrix: \n";
756 for (int i = 0; i <4; ++i)
757 os << setw(10) << M.M[i][0] << setw(10) << M.M[i][1]
758 << setw(10) << M.M[i][2] << setw(10) << M.M[i][3] << "\n";
762 //==========================================================================
765 // This class handles a single histogram at a time
766 // (or a vector of histograms).
768 //--------------------------------------------------------------------------
770 // Constants: could be changed here if desired, but normally should not.
771 // These are of technical nature, as described for each.
773 // Maximum number of bins in a histogram.
774 const int Hist::NBINMAX = 1000;
776 // Maximum number of columns that can be printed for a histogram.
777 const int Hist::NCOLMAX = 100;
779 // Maximum number of lines a histogram can use at output.
780 const int Hist::NLINES = 30;
782 // Tolerance in deviation of xMin and xMax between two histograms.
783 const double Hist::TOLERANCE = 0.001;
785 // Small number to avoid division by zero.
786 const double Hist::TINY = 1e-20;
788 // When minbin/maxbin < SMALLFRAC the y scale goes down to zero.
789 const double Hist::SMALLFRAC = 0.1;
791 // Constants for printout: fixed steps on y scale; filling characters.
792 const double DYAC[] = {0.04, 0.05, 0.06, 0.08, 0.10,
793 0.12, 0.15, 0.20, 0.25, 0.30};
794 const char NUMBER[] = {'0', '1', '2', '3', '4', '5',
795 '6', '7', '8', '9', 'X' };
797 //--------------------------------------------------------------------------
801 void Hist::book(string titleIn, int nBinIn, double xMinIn,
806 if (nBinIn < 1) nBin = 1;
807 if (nBinIn > NBINMAX) nBin = NBINMAX;
810 dx = (xMax - xMin)/nBin;
816 //--------------------------------------------------------------------------
818 // Reset bin contents.
826 for (int ix = 0; ix < nBin; ++ix) res[ix] = 0.;
830 //--------------------------------------------------------------------------
832 // Fill bin with weight.
834 void Hist::fill(double x, double w) {
837 int iBin = int(floor((x - xMin)/dx));
838 if (iBin < 0) under += w;
839 else if (iBin >= nBin) over += w;
840 else {inside += w; res[iBin] += w; }
844 //--------------------------------------------------------------------------
846 // Print a histogram: also operator overloading with friend.
848 ostream& operator<<(ostream& os, const Hist& h) {
850 // Do not print empty histograms.
851 if (h.nFill <= 0) return os;
853 // Write time and title.
856 strftime(date,18,"%Y-%m-%d %H:%M",localtime(&t));
857 os << "\n\n " << date << " " << h.title << "\n\n";
859 // Group bins, where required, to make printout have fewer columns.
860 int nGroup = 1 + (h.nBin - 1) / Hist::NCOLMAX;
861 int nCol = 1 + (h.nBin - 1) / nGroup;
862 vector<double> resCol(nCol);
863 for (int iCol = 0; iCol < nCol; ++iCol) {
865 for (int ix = nGroup * iCol; ix < min( h.nBin, nGroup * (iCol + 1)); ++ix)
866 resCol[iCol] += h.res[ix];
869 // Find minimum and maximum bin content.
870 double yMin = resCol[0];
871 double yMax = resCol[0];
872 for (int iCol = 1; iCol < nCol; ++iCol) {
873 if (resCol[iCol] < yMin) yMin = resCol[iCol];
874 if (resCol[iCol] > yMax) yMax = resCol[iCol];
877 // Determine scale and step size for y axis.
878 if (yMax - yMin > Hist::NLINES * DYAC[0] * 1e-9) {
879 if (yMin > 0. && yMin < Hist::SMALLFRAC * yMax) yMin = 0.;
880 if (yMax < 0. && yMax > Hist::SMALLFRAC * yMin) yMax = 0.;
881 int iPowY = int(floor( log10(yMax - yMin) ));
882 if (yMax - yMin < Hist::NLINES * DYAC[0] * pow(10.,iPowY))
884 if (yMax - yMin > Hist::NLINES * DYAC[9] * pow(10.,iPowY))
886 double nLinePow = Hist::NLINES * pow(10.,iPowY);
887 double delY = DYAC[0];
888 for (int idel = 0; idel < 9; ++idel)
889 if (yMax - yMin >= nLinePow * DYAC[idel]) delY = DYAC[idel+1];
890 double dy = delY * pow(10.,iPowY);
892 // Convert bin contents to integer form; fractional fill in top row.
893 vector<int> row(nCol);
894 vector<int> frac(nCol);
895 for (int iCol = 0; iCol < nCol ; ++iCol) {
896 double cta = abs(resCol[iCol]) / dy;
897 row[iCol] = int(cta + 0.95);
898 if(resCol[iCol] < 0.) row[iCol] = - row[iCol];
899 frac[iCol] = int(10. * (cta + 1.05 - floor(cta + 0.95)));
901 int rowMin = int(abs(yMin)/dy + 0.95);
902 if ( yMin < 0) rowMin = - rowMin;
903 int rowMax = int(abs(yMax)/dy + 0.95);
904 if ( yMax < 0) rowMax = - rowMax;
906 // Print histogram row by row.
907 os << fixed << setprecision(2);
908 for (int iRow = rowMax; iRow >= rowMin; iRow--) if (iRow != 0) {
909 os << " " << setw(10) << iRow*delY << "*10^"
910 << setw(2) << iPowY << " ";
911 for (int iCol = 0; iCol < nCol ; ++iCol) {
912 if (iRow == row[iCol]) os << NUMBER[frac[iCol]];
913 else if (iRow * (row[iCol] - iRow) > 0) os << NUMBER[10];
918 // Print sign and value of bin contents
919 double maxim = log10(max(yMax, -yMin));
920 int iPowBin = int(floor(maxim + 0.0001));
922 for (int iCol = 0; iCol < nCol ; ++iCol) {
923 if (resCol[iCol] < - pow(10., iPowBin - 4)) os << "-";
925 row[iCol] = int(abs(resCol[iCol]) * pow(10., 3 - iPowBin) + 0.5);
927 for (int iRow = 3; iRow >= 0; iRow--) {
928 os << " *10^" << setw(2) << iPowBin + iRow - 3 << " ";
929 int mask = int( pow(10., iRow) + 0.5);
930 for (int iCol = 0; iCol < nCol ; ++iCol) {
931 os << NUMBER[(row[iCol] / mask) % 10];
935 // Print sign and value of lower bin edge.
936 maxim = log10( max( -h.xMin, h.xMax - h.dx));
937 int iPowExp = int(floor(maxim + 0.0001));
939 for (int iCol = 0; iCol < nCol ; ++iCol) {
940 if (h.xMin + iCol * nGroup * h.dx < - pow(10., iPowExp - 3)) os << "-";
942 row[iCol] = int(abs(h.xMin + iCol * nGroup * h.dx)
943 * pow(10., 2 - iPowExp) + 0.5);
945 for (int iRow = 2; iRow >= 0; iRow--) {
946 os << " *10^" << setw(2) << iPowExp + iRow - 2 << " ";
947 int mask = int( pow(10., iRow) + 0.5);
948 for (int iCol = 0; iCol < nCol ; ++iCol)
949 os << NUMBER[(row[iCol] / mask) % 10];
953 // Print explanation if histogram cannot be shown.
954 } else os << " Histogram not shown since lowest value" << scientific
955 << setprecision(4) << setw(12) << yMin << " and highest value"
956 << setw(12) << yMax << " are too close \n \n";
958 // Calculate and print statistics.
962 for (int ix = 0; ix < h.nBin ; ++ix) {
963 double cta = abs(h.res[ix]);
964 double x = h.xMin + (ix + 0.5) * h.dx;
966 cxSum = cxSum + cta * x;
967 cxxSum = cxxSum + cta * x * x;
969 double xmean = cxSum / max(cSum, Hist::TINY);
970 double rms = sqrtpos( cxxSum / max(cSum, Hist::TINY) - xmean*xmean );
971 os << scientific << setprecision(4)
972 << " Entries =" << setw(12) << h.nFill
973 << " Mean =" << setw(12) << xmean
974 << " Underflow =" << setw(12) << h.under
975 << " Low edge =" << setw(12) << h.xMin << "\n"
976 << " All chan =" << setw(12) << h.inside
977 << " Rms =" << setw(12) << rms
978 << " Overflow =" << setw(12) << h.over
979 << " High edge =" << setw(12) << h.xMax << endl;
983 //--------------------------------------------------------------------------
985 // Print histogram contents as a table (e.g. for Gnuplot).
987 void Hist::table(ostream& os) const {
989 // Print histogram vector bin by bin, with mean x as first column.
990 os << scientific << setprecision(4);
991 for (int ix = 0; ix < nBin; ++ix)
992 os << setw(12) << xMin + (ix + 0.5) * dx
993 << setw(12) << res[ix] << "\n";
997 //--------------------------------------------------------------------------
999 // Print a table out of two histograms with same x axis (e.g. for Gnuplot).
1001 void table(const Hist& h1, const Hist& h2, ostream& os) {
1003 // Require histogram x axes to agree.
1004 if (h1.nBin != h2.nBin || abs(h1.xMin - h2.xMin) > Hist::TOLERANCE * h1.dx
1005 || abs(h1.xMax - h2.xMax) > Hist::TOLERANCE * h1.dx) return;
1007 // Print histogram vectors bin by bin, with mean x as first column.
1008 os << scientific << setprecision(4);
1009 for (int ix = 0; ix < h1.nBin; ++ix)
1010 os << setw(12) << h1.xMin + (ix + 0.5) * h1.dx
1011 << setw(12) << h1.res[ix] << setw(12) << h2.res[ix] << "\n";
1015 void table(const Hist& h1, const Hist& h2, string fileName) {
1016 ofstream streamName(fileName.c_str());
1017 table( h1, h2, streamName);
1020 //--------------------------------------------------------------------------
1022 // Get content of specific bin.
1023 // Special values are bin 0 for underflow and bin nBin+1 for overflow.
1024 // All other bins outside proper histogram range return 0.
1026 double Hist::getBinContent(int iBin) {
1028 if (iBin > 0 && iBin <= nBin) return res[iBin - 1];
1029 else if (iBin == 0) return under;
1030 else if (iBin == nBin + 1) return over;
1035 //--------------------------------------------------------------------------
1037 // Check whether another histogram has same size and limits.
1039 bool Hist::sameSize(const Hist& h) const {
1041 if (nBin == h.nBin && abs(xMin - h.xMin) < TOLERANCE * dx &&
1042 abs(xMax - h.xMax) < TOLERANCE * dx) return true;
1047 //--------------------------------------------------------------------------
1049 // Take 10-logarithm or natural logarithm of contents bin by bin.
1051 void Hist::takeLog(bool tenLog) {
1053 // Find smallest positive bin content, and put min a bit below.
1055 for (int ix = 0; ix < nBin; ++ix)
1056 if (res[ix] > 1e-20 && res[ix] < yMin ) yMin = res[ix];
1059 // Take 10-logarithm bin by bin, but ensure positivity.
1061 for (int ix = 0; ix < nBin; ++ix)
1062 res[ix] = log10( max( yMin, res[ix]) );
1063 under = log10( max( yMin, under) );
1064 inside = log10( max( yMin, inside) );
1065 over = log10( max( yMin, over) );
1067 // Take natural logarithm bin by bin, but ensure positivity.
1069 for (int ix = 0; ix < nBin; ++ix)
1070 res[ix] = log( max( yMin, res[ix]) );
1071 under = log( max( yMin, under) );
1072 inside = log( max( yMin, inside) );
1073 over = log( max( yMin, over) );
1078 //--------------------------------------------------------------------------
1080 // Take square root of contents bin by bin; set 0 for negative content.
1082 void Hist::takeSqrt() {
1084 for (int ix = 0; ix < nBin; ++ix) res[ix] = sqrtpos(res[ix]);
1085 under = sqrtpos(under);
1086 inside = sqrtpos(inside);
1087 over = sqrtpos(over);
1091 //--------------------------------------------------------------------------
1093 // Add histogram to existing one.
1095 Hist& Hist::operator+=(const Hist& h) {
1096 if (!sameSize(h)) return *this;
1101 for (int ix = 0; ix < nBin; ++ix) res[ix] += h.res[ix];
1105 //--------------------------------------------------------------------------
1107 // Subtract histogram from existing one.
1109 Hist& Hist::operator-=(const Hist& h) {
1110 if (!sameSize(h)) return *this;
1115 for (int ix = 0; ix < nBin; ++ix) res[ix] -= h.res[ix];
1119 //--------------------------------------------------------------------------
1121 // Multiply existing histogram by another one.
1123 Hist& Hist::operator*=(const Hist& h) {
1124 if (!sameSize(h)) return *this;
1129 for (int ix = 0; ix < nBin; ++ix) res[ix] *= h.res[ix];
1133 //--------------------------------------------------------------------------
1135 // Divide existing histogram by another one.
1137 Hist& Hist::operator/=(const Hist& h) {
1138 if (!sameSize(h)) return *this;
1140 under = (abs(h.under) < Hist::TINY) ? 0. : under/h.under;
1141 inside = (abs(h.inside) < Hist::TINY) ? 0. : inside/h.inside;
1142 over = (abs(h.over) < Hist::TINY) ? 0. : over/h.over;
1143 for (int ix = 0; ix < nBin; ++ix)
1144 res[ix] = (abs(h.res[ix]) < Hist::TINY) ? 0. : res[ix]/h.res[ix];
1148 //--------------------------------------------------------------------------
1150 // Add constant offset to histogram.
1152 Hist& Hist::operator+=(double f) {
1156 for (int ix = 0; ix < nBin; ++ix) res[ix] += f;
1160 //--------------------------------------------------------------------------
1162 // Subtract constant offset from histogram.
1164 Hist& Hist::operator-=(double f) {
1168 for (int ix = 0; ix < nBin; ++ix) res[ix] -= f;
1172 //--------------------------------------------------------------------------
1174 // Multiply histogram by constant
1176 Hist& Hist::operator*=(double f) {
1180 for (int ix = 0; ix < nBin; ++ix) res[ix] *= f;
1184 //--------------------------------------------------------------------------
1186 // Divide histogram by constant
1188 Hist& Hist::operator/=(double f) {
1192 for (int ix = 0; ix < nBin; ++ix) res[ix] /= f;
1196 //--------------------------------------------------------------------------
1198 // Implementation of operator overloading with friends.
1200 Hist operator+(double f, const Hist& h1) {
1201 Hist h = h1; return h += f;}
1203 Hist operator+(const Hist& h1, double f) {
1204 Hist h = h1; return h += f;}
1206 Hist operator+(const Hist& h1, const Hist& h2) {
1207 Hist h = h1; return h += h2;}
1209 Hist operator-(double f, const Hist& h1) {
1211 h.under = f - h1.under;
1212 h.inside = h1.nBin * f - h1.inside;
1213 h.over = f - h1.over;
1214 for (int ix = 0; ix < h1.nBin; ++ix) h.res[ix] = f - h1.res[ix];
1217 Hist operator-(const Hist& h1, double f) {
1218 Hist h = h1; return h -= f;}
1220 Hist operator-(const Hist& h1, const Hist& h2) {
1221 Hist h = h1; return h -= h2;}
1223 Hist operator*(double f, const Hist& h1) {
1224 Hist h = h1; return h *= f;}
1226 Hist operator*(const Hist& h1, double f) {
1227 Hist h = h1; return h *= f;}
1229 Hist operator*(const Hist& h1, const Hist& h2) {
1230 Hist h = h1; return h *= h2;}
1232 Hist operator/(double f, const Hist& h1) {
1234 h.under = (abs(h1.under) < Hist::TINY) ? 0. : f/h1.under;
1235 h.inside = (abs(h1.inside) < Hist::TINY) ? 0. : f/h1.inside;
1236 h.over = (abs(h1.over) < Hist::TINY) ? 0. : f/h1.over;
1237 for (int ix = 0; ix < h1.nBin; ++ix)
1238 h.res[ix] = (abs(h1.res[ix]) < Hist::TINY) ? 0. : f/h1.res[ix];
1242 Hist operator/(const Hist& h1, double f) {
1243 Hist h = h1; return h /= f;}
1245 Hist operator/(const Hist& h1, const Hist& h2) {
1246 Hist h = h1; return h /= h2;}
1248 //==========================================================================
1250 } // end namespace Pythia8