13 #ifndef Pythia8_Basics_H
14 #define Pythia8_Basics_H
16 #include "Pythia8/PythiaStdlib.h"
36 Vec4(
double xIn = 0.,
double yIn = 0.,
double zIn = 0.,
double tIn = 0.)
37 : xx(xIn), yy(yIn), zz(zIn), tt(tIn) { }
38 Vec4(
const Vec4& v) : xx(v.xx), yy(v.yy), zz(v.zz), tt(v.tt) { }
39 Vec4& operator=(
const Vec4& v) {
if (
this != &v) { xx = v.xx; yy = v.yy;
40 zz = v.zz; tt = v.tt; }
return *
this; }
41 Vec4& operator=(
double value) { xx = value; yy = value; zz = value;
42 tt = value;
return *
this; }
45 void reset() {xx = 0.; yy = 0.; zz = 0.; tt = 0.;}
46 void p(
double xIn,
double yIn,
double zIn,
double tIn)
47 {xx = xIn; yy = yIn; zz = zIn; tt = tIn;}
48 void p(Vec4 pIn) {xx = pIn.xx; yy = pIn.yy; zz = pIn.zz; tt = pIn.tt;}
49 void px(
double xIn) {xx = xIn;}
50 void py(
double yIn) {yy = yIn;}
51 void pz(
double zIn) {zz = zIn;}
52 void e(
double tIn) {tt = tIn;}
55 double px()
const {
return xx;}
56 double py()
const {
return yy;}
57 double pz()
const {
return zz;}
58 double e()
const {
return tt;}
59 double& operator[](
int i) {
60 if (i == 1)
return xx;
61 else if (i == 2)
return yy;
62 else if (i == 3)
return zz;
65 double mCalc()
const {
double temp = tt*tt - xx*xx - yy*yy - zz*zz;
66 return (temp >= 0.) ? sqrt(temp) : -sqrt(-temp);}
67 double m2Calc()
const {
return tt*tt - xx*xx - yy*yy - zz*zz;}
68 double pT()
const {
return sqrt(xx*xx + yy*yy);}
69 double pT2()
const {
return xx*xx + yy*yy;}
70 double pAbs()
const {
return sqrt(xx*xx + yy*yy + zz*zz);}
71 double pAbs2()
const {
return xx*xx + yy*yy + zz*zz;}
72 double eT()
const {
double temp = xx*xx + yy*yy;
73 return tt * sqrt( temp / (temp + zz*zz) );}
74 double eT2()
const {
double temp = xx*xx + yy*yy;
75 return tt*tt * temp / (temp + zz*zz);}
76 double theta()
const {
return atan2(sqrt(xx*xx + yy*yy), zz);}
77 double phi()
const {
return atan2(yy,xx);}
78 double thetaXZ()
const {
return atan2(xx,zz);}
79 double pPos()
const {
return tt + zz;}
80 double pNeg()
const {
return tt - zz;}
81 double rap()
const {
return 0.5 * log( (tt + zz) / (tt - zz) );}
82 double eta()
const {
double xyz = sqrt(xx*xx + yy*yy + zz*zz);
83 return 0.5 * log( (xyz + zz) / (xyz - zz) );}
86 void rescale3(
double fac) {xx *= fac; yy *= fac; zz *= fac;}
87 void rescale4(
double fac) {xx *= fac; yy *= fac; zz *= fac; tt *= fac;}
88 void flip3() {xx = -xx; yy = -yy; zz = -zz;}
89 void flip4() {xx = -xx; yy = -yy; zz = -zz; tt = -tt;}
90 void rot(
double thetaIn,
double phiIn);
91 void rotaxis(
double phiIn,
double nx,
double ny,
double nz);
92 void rotaxis(
double phiIn,
const Vec4& n);
93 void bst(
double betaX,
double betaY,
double betaZ);
94 void bst(
double betaX,
double betaY,
double betaZ,
double gamma);
95 void bst(
const Vec4& pIn);
96 void bst(
const Vec4& pIn,
double mIn);
97 void bstback(
const Vec4& pIn);
98 void bstback(
const Vec4& pIn,
double mIn);
99 void rotbst(
const RotBstMatrix& M);
102 inline Vec4 operator-()
const {Vec4 tmp; tmp.xx = -xx; tmp.yy = -yy;
103 tmp.zz = -zz; tmp.tt = -tt;
return tmp;}
104 inline Vec4& operator+=(
const Vec4& v) {xx += v.xx; yy += v.yy; zz += v.zz;
105 tt += v.tt;
return *
this;}
106 inline Vec4& operator-=(
const Vec4& v) {xx -= v.xx; yy -= v.yy; zz -= v.zz;
107 tt -= v.tt;
return *
this;}
108 inline Vec4& operator*=(
double f) {xx *= f; yy *= f; zz *= f;
109 tt *= f;
return *
this;}
110 inline Vec4& operator/=(
double f) {xx /= f; yy /= f; zz /= f;
111 tt /= f;
return *
this;}
112 inline Vec4 operator+(
const Vec4& v)
const {
113 Vec4 tmp = *
this;
return tmp += v;}
114 inline Vec4 operator-(
const Vec4& v)
const {
115 Vec4 tmp = *
this;
return tmp -= v;}
116 inline Vec4 operator*(
double f)
const {
117 Vec4 tmp = *
this;
return tmp *= f;}
118 inline Vec4 operator/(
double f)
const {
119 Vec4 tmp = *
this;
return tmp /= f;}
120 inline double operator*(
const Vec4& v)
const {
121 return tt*v.tt - xx*v.xx - yy*v.yy - zz*v.zz;}
124 friend Vec4 operator*(
double f,
const Vec4& v1);
127 friend ostream& operator<<(ostream&,
const Vec4& v) ;
130 friend inline bool isnan(
const Vec4 &v) {
131 return isnan(v.tt) || isnan(v.xx) || isnan(v.yy) || isnan(v.zz);}
132 friend inline bool isinf(
const Vec4 &v) {
133 return isinf(v.tt) || isinf(v.xx) || isinf(v.yy) || isinf(v.zz);}
134 friend inline bool isfinite(
const Vec4 &v) {
135 return isfinite(v.tt) && isfinite(v.xx) && isfinite(v.yy)
139 friend double m(
const Vec4& v1,
const Vec4& v2);
140 friend double m2(
const Vec4& v1,
const Vec4& v2);
143 friend double dot3(
const Vec4& v1,
const Vec4& v2);
144 friend Vec4 cross3(
const Vec4& v1,
const Vec4& v2);
147 friend Vec4 cross4(
const Vec4& a,
const Vec4& b,
const Vec4& c);
150 friend double theta(
const Vec4& v1,
const Vec4& v2);
151 friend double costheta(
const Vec4& v1,
const Vec4& v2);
154 friend double phi(
const Vec4& v1,
const Vec4& v2);
155 friend double cosphi(
const Vec4& v1,
const Vec4& v2);
158 friend double phi(
const Vec4& v1,
const Vec4& v2,
const Vec4& n);
159 friend double cosphi(
const Vec4& v1,
const Vec4& v2,
const Vec4& n);
162 friend double RRapPhi(
const Vec4& v1,
const Vec4& v2);
163 friend double REtaPhi(
const Vec4& v1,
const Vec4& v2);
166 friend bool pShift( Vec4& p1Move, Vec4& p2Move,
double m1New,
double m2New);
169 friend pair<Vec4,Vec4> getTwoPerpendicular(
const Vec4& v1,
const Vec4& v2);
174 static const double TINY;
177 double xx, yy, zz, tt;
186 inline Vec4 operator*(
double f,
const Vec4& v1)
187 {Vec4 v = v1;
return v *= f;}
190 double m(
const Vec4& v1,
const Vec4& v2);
191 double m2(
const Vec4& v1,
const Vec4& v2);
194 double dot3(
const Vec4& v1,
const Vec4& v2);
195 Vec4 cross3(
const Vec4& v1,
const Vec4& v2);
198 Vec4 cross4(
const Vec4& a,
const Vec4& b,
const Vec4& c);
201 double theta(
const Vec4& v1,
const Vec4& v2);
202 double costheta(
const Vec4& v1,
const Vec4& v2);
205 double phi(
const Vec4& v1,
const Vec4& v2);
206 double cosphi(
const Vec4& v1,
const Vec4& v2);
209 double phi(
const Vec4& v1,
const Vec4& v2,
const Vec4& n);
210 double cosphi(
const Vec4& v1,
const Vec4& v2,
const Vec4& n);
213 double RRapPhi(
const Vec4& v1,
const Vec4& v2);
214 double REtaPhi(
const Vec4& v1,
const Vec4& v2);
217 ostream& operator<<(ostream&,
const Vec4& v) ;
220 bool pShift( Vec4& p1Move, Vec4& p2Move,
double m1New,
double m2New);
223 pair<Vec4,Vec4> getTwoPerpendicular(
const Vec4& v1,
const Vec4& v2);
236 RotBstMatrix() : M() {
for (
int i = 0; i < 4; ++i) {
237 for (
int j = 0; j < 4; ++j) { M[i][j] = (i==j) ? 1. : 0.; } } }
238 RotBstMatrix(
const RotBstMatrix& Min) : M() {
239 for (
int i = 0; i < 4; ++i) {
for (
int j = 0; j < 4; ++j) {
240 M[i][j] = Min.M[i][j]; } } }
241 RotBstMatrix& operator=(
const RotBstMatrix& Min) {
if (
this != &Min) {
242 for (
int i = 0; i < 4; ++i) {
for (
int j = 0; j < 4; ++j) {
243 M[i][j] = Min.M[i][j]; } } }
return *
this; }
246 void rot(
double = 0.,
double = 0.);
247 void rot(
const Vec4& p);
248 void bst(
double = 0.,
double = 0.,
double = 0.);
249 void bst(
const Vec4&);
250 void bstback(
const Vec4&);
251 void bst(
const Vec4&,
const Vec4&);
252 void toCMframe(
const Vec4&,
const Vec4&);
253 void fromCMframe(
const Vec4&,
const Vec4&);
254 void toSameVframe(
const Vec4&,
const Vec4&);
255 void fromSameVframe(
const Vec4&,
const Vec4&);
256 void rotbst(
const RotBstMatrix&);
258 RotBstMatrix inverse()
const { RotBstMatrix tmp = *
this;
259 tmp.invert();
return tmp; }
263 double value(
int i,
int j) {
return M[i][j];}
266 double deviation()
const;
269 friend ostream& operator<<(ostream&,
const RotBstMatrix&) ;
275 Vec4 operator*(Vec4 p)
const { p.rotbst(*
this);
return p; }
276 RotBstMatrix operator*(RotBstMatrix R)
const { R.rotbst(*
this);
return R; }
281 static const double TINY;
293 ostream& operator<<(ostream&,
const RotBstMatrix&) ;
296 inline RotBstMatrix toCMframe(
const Vec4& p) {
297 RotBstMatrix tmp; tmp.bstback(p);
return tmp; }
300 inline RotBstMatrix fromCMframe(
const Vec4& p) {
301 RotBstMatrix tmp; tmp.bst(p);
return tmp; }
305 inline RotBstMatrix toCMframe(
const Vec4& p1,
const Vec4& p2) {
306 RotBstMatrix tmp; tmp.toCMframe(p1, p2);
return tmp; }
310 inline RotBstMatrix fromCMframe(
const Vec4& p1,
const Vec4& p2) {
311 RotBstMatrix tmp; tmp.fromCMframe(p1, p2);
return tmp; }
315 inline RotBstMatrix toCMframe(
const Vec4& ptot,
const Vec4& pz,
316 const Vec4 & pxz) { RotBstMatrix tmp = toCMframe(ptot);
317 Vec4 pzp = tmp*pz; tmp.rot(0.0, -pzp.phi()); tmp.rot(-pzp.theta());
318 tmp.rot(0.0, -(tmp*pxz).phi());
return tmp; }
322 inline RotBstMatrix fromCMframe(
const Vec4& ptot,
const Vec4& pz,
323 const Vec4 & pxz) {
return toCMframe(ptot, pz, pxz).inverse(); }
335 virtual ~RndmEngine() {}
339 virtual double flat() = 0;
354 Rndm() : initRndm(false), i97(), j97(), seedSave(0), sequence(0), u(), c(),
355 cd(), cm(), useExternalRndm(false), rndmEngPtr(0) { }
356 Rndm(
int seedIn) : initRndm(false), i97(), j97(), seedSave(0), sequence(0),
357 u(), c(), cd(), cm(), useExternalRndm(false), rndmEngPtr(0) {init(seedIn);}
360 bool rndmEnginePtr( RndmEngine* rndmEngPtrIn);
363 void init(
int seedIn = 0) ;
369 double exp() {
return -log(flat()) ;}
372 double xexp() {
return -log(flat() * flat()) ;}
375 double gauss() {
return sqrt(-2. * log(flat())) * cos(M_PI * flat());}
378 pair<double, double> gauss2() {
double r = sqrt(-2. * log(flat()));
379 double phi = 2. * M_PI * flat();
380 return { r * sin(phi), r * cos(phi) };}
383 pair<Vec4, Vec4> phaseSpace2(
double eCM,
double m1,
double m2);
386 int pick(
const vector<double>& prob) ;
389 bool dumpState(
string fileName);
390 bool readState(
string fileName);
395 static const int DEFAULTSEED;
399 int i97, j97, seedSave;
401 double u[97], c, cd, cm;
404 bool useExternalRndm;
405 RndmEngine* rndmEngPtr;
419 Hist() : titleSave(
""), nBin(), nFill(), nNonFinite(),
420 xMin(), xMax(), linX(), dx(), under(), inside(), over(), sumxw()
422 Hist(
string titleIn,
int nBinIn = 100,
double xMinIn = 0.,
423 double xMaxIn = 1.,
bool logXIn =
false) : nBin(), nFill(), nNonFinite(),
424 xMin(), xMax(), linX(), dx(), under(), inside(), over(), sumxw()
425 { book(titleIn, nBinIn, xMinIn, xMaxIn, logXIn); }
427 : titleSave(h.titleSave), nBin(h.nBin), nFill(h.nFill),
428 nNonFinite(h.nNonFinite), xMin(h.xMin), xMax(h.xMax), linX(h.linX),
429 dx(h.dx), under(h.under), inside(h.inside), over(h.over),
430 sumxw(h.sumxw), res(h.res) { }
431 Hist(
string titleIn,
const Hist& h)
432 : titleSave(titleIn), nBin(h.nBin), nFill(h.nFill),
433 nNonFinite(h.nNonFinite), xMin(h.xMin), xMax(h.xMax), linX(h.linX),
434 dx(h.dx), under(h.under), inside(h.inside), over(h.over),
435 sumxw(h.sumxw), res(h.res) { }
436 Hist& operator=(
const Hist& h) {
if(
this != &h) {
437 nBin = h.nBin; nFill = h.nFill; nNonFinite = h.nNonFinite; xMin = h.xMin;
438 xMax = h.xMax; linX = h.linX; dx = h.dx; under = h.under;
439 inside = h.inside; over = h.over; sumxw = h.sumxw;
440 res = h.res; }
return *
this; }
443 static Hist plotFunc(
function<
double(
double)> f,
string titleIn,
444 int nBinIn,
double xMinIn,
double xMaxIn,
bool logXIn =
false);
447 void book(
string titleIn =
" ",
int nBinIn = 100,
double xMinIn = 0.,
448 double xMaxIn = 1.,
bool logXIn =
false) ;
451 void title(
string titleIn =
" ") {titleSave = titleIn; }
457 void fill(
double x,
double w = 1.) ;
460 friend ostream& operator<<(ostream& os,
const Hist& h) ;
463 void table(ostream& os = cout,
bool printOverUnder =
false,
464 bool xMidBin =
true)
const ;
465 void table(
string fileName,
bool printOverUnder =
false,
466 bool xMidBin =
true)
const { ofstream streamName(fileName.c_str());
467 table(streamName, printOverUnder, xMidBin);}
468 void rivetTable(ostream& os = cout,
bool printError =
false)
const ;
469 void rivetTable(
string fileName,
bool printError =
false)
const {
470 ofstream streamName(fileName.c_str()); rivetTable(streamName, printError);}
471 void pyplotTable(ostream& os = cout,
bool isHist =
true)
const ;
472 void pyplotTable(
string fileName,
bool isHist =
true)
const {
473 ofstream streamName(fileName.c_str()); pyplotTable(streamName, isHist);}
476 friend void table(
const Hist& h1,
const Hist& h2, ostream& os,
477 bool printOverUnder,
bool xMidBin) ;
478 friend void table(
const Hist& h1,
const Hist& h2,
string fileName,
479 bool printOverUnder,
bool xMidBin) ;
482 string getTitle()
const {
return titleSave;}
483 int getBinNumber()
const {
return nBin;}
484 int getNonFinite()
const {
return nNonFinite;}
485 bool getLinX()
const {
return linX;}
488 double getXMin()
const {
return xMin;}
489 double getXMax()
const {
return xMax;}
490 double getYMin()
const {
double yMin = res[0];
491 for (
int ix = 1; ix < nBin; ++ix)
492 if (res[ix] < yMin ) yMin = res[ix];
494 double getYMax()
const {
double yMax = res[0];
495 for (
int ix = 1; ix < nBin; ++ix)
496 if (res[ix] > yMax ) yMax = res[ix];
498 double getYAbsMin()
const {
double yAbsMin = 1e20;
double yAbs;
499 for (
int ix = 0; ix < nBin; ++ix) { yAbs = abs(res[ix]);
500 if (yAbs > 1e-20 && yAbs < yAbsMin) yAbsMin = yAbs; }
503 double getXMean()
const {
return sumxw / inside; }
504 double getYMean()
const {
return inside / nFill; }
507 double getBinContent(
int iBin)
const;
510 int getEntries(
bool alsoNonFinite =
true)
const {
511 return alsoNonFinite ? nNonFinite + nFill : nFill; }
514 bool sameSize(
const Hist& h)
const ;
517 void takeLog(
bool tenLog =
true) ;
523 void normalize(
double sum = 1.,
bool alsoOverflow =
true) ;
526 Hist& operator+=(
const Hist& h) ;
527 Hist& operator-=(
const Hist& h) ;
528 Hist& operator*=(
const Hist& h) ;
529 Hist& operator/=(
const Hist& h) ;
530 Hist& operator+=(
double f) ;
531 Hist& operator-=(
double f) ;
532 Hist& operator*=(
double f) ;
533 Hist& operator/=(
double f) ;
534 Hist operator+(
double f)
const;
535 Hist operator+(
const Hist& h2)
const;
536 Hist operator-(
double f)
const;
537 Hist operator-(
const Hist& h2)
const;
538 Hist operator*(
double f)
const;
539 Hist operator*(
const Hist& h2)
const;
540 Hist operator/(
double f)
const;
541 Hist operator/(
const Hist& h2)
const;
544 friend Hist operator+(
double f,
const Hist& h1);
545 friend Hist operator-(
double f,
const Hist& h1);
546 friend Hist operator*(
double f,
const Hist& h1);
547 friend Hist operator/(
double f,
const Hist& h1);
552 static const int NBINMAX, NCOLMAX, NLINES;
553 static const double TOLERANCE, TINY, LARGE, SMALLFRAC, DYAC[];
554 static const char NUMBER[];
558 int nBin, nFill, nNonFinite;
561 double dx, under, inside, over, sumxw;
571 ostream& operator<<(ostream& os,
const Hist& h) ;
574 void table(
const Hist& h1,
const Hist& h2, ostream& os = cout,
575 bool printOverUnder =
false,
bool xMidBin =
true) ;
576 void table(
const Hist& h1,
const Hist& h2,
string fileName,
577 bool printOverUnder =
false,
bool xMidBin =
true) ;
580 Hist operator+(
double f,
const Hist& h1);
581 Hist operator-(
double f,
const Hist& h1);
582 Hist operator*(
double f,
const Hist& h1);
583 Hist operator/(
double f,
const Hist& h1);
595 HistPlot(
string pythonName) : nFrame(), nTable() {
596 toPython.open( (pythonName +
".py").c_str() );
597 toPython <<
"from matplotlib import pyplot as plt" << endl
598 <<
"from matplotlib.backends.backend_pdf import PdfPages" << endl;
602 ~HistPlot() { toPython <<
"pp.close()" << endl; }
605 void frame(
string frameIn,
string titleIn =
"",
string xLabIn =
"",
606 string yLabIn =
"",
double xSizeIn = 8.,
double ySizeIn = 6.) {
607 framePrevious = frameName; frameName = frameIn; title = titleIn;
608 xLabel = xLabIn; yLabel = yLabIn; xSize = xSizeIn; ySize = ySizeIn;
609 histos.clear(); styles.clear(); legends.clear(); files.clear();
610 fileStyles.clear(); fileLegends.clear(); filexyerr.clear();}
613 void add(
const Hist& histIn,
string styleIn =
"h",
614 string legendIn =
"void") { histos.push_back(histIn);
615 styles.push_back(styleIn); legends.push_back(legendIn); }
618 void addFile(
string fileIn,
string styleIn =
"o",
619 string legendIn =
"void",
string xyerrIn=
"") { files.push_back(fileIn);
620 fileStyles.push_back(styleIn); fileLegends.push_back(legendIn);
621 filexyerr.push_back(xyerrIn);}
624 void plot(
bool logY =
false,
bool logX =
false,
bool userBorders =
false);
625 void plot(
double xMinUserIn,
double xMaxUserIn,
double yMinUserIn,
626 double yMaxUserIn,
bool logY =
false,
bool logX =
false) {
627 xMinUser = xMinUserIn; xMaxUser = xMaxUserIn; yMinUser = yMinUserIn;
628 yMaxUser = yMaxUserIn; plot( logY, logX,
true);}
631 void plotFrame(
string frameIn,
const Hist& histIn,
string titleIn =
"",
632 string xLabIn =
"",
string yLabIn =
"",
string styleIn =
"h",
633 string legendIn =
"void",
bool logY =
false) {
634 frame( frameIn, titleIn, xLabIn, yLabIn);
635 add( histIn, styleIn, legendIn); plot( logY); }
640 void init(
string pythonName);
644 int nPDF, nFrame, nTable;
645 double xSize, ySize, xMinUser, xMaxUser, yMinUser, yMaxUser;
646 string frameName, framePrevious, title, xLabel, yLabel, fileName, tmpFig;
648 vector<string> styles, legends, files, fileStyles, fileLegends, filexyerr;
656 #endif // end Pythia8_Basics_H