Rivet API documentation

Rivet 4.1.3
Vector3.hh
1#ifndef RIVET_MATH_VECTOR3
2#define RIVET_MATH_VECTOR3
3
4#include "Rivet/Tools/TypeTraits.hh"
5#include "Rivet/Math/MathConstants.hh"
6#include "Rivet/Math/MathUtils.hh"
7#include "Rivet/Math/VectorN.hh"
8
9namespace Rivet {
10
11
12 class Vector3;
13 typedef Vector3 ThreeVector;
14 typedef Vector3 V3;
15 Vector3 multiply(const double, const Vector3&);
16 Vector3 multiply(const Vector3&, const double);
17 Vector3 add(const Vector3&, const Vector3&);
18 Vector3 operator*(const double, const Vector3&);
19 Vector3 operator*(const Vector3&, const double);
20 Vector3 operator/(const Vector3&, const double);
21 Vector3 operator+(const Vector3&, const Vector3&);
22 Vector3 operator-(const Vector3&, const Vector3&);
23
24 class ThreeMomentum;
25 typedef ThreeMomentum P3;
26 ThreeMomentum multiply(const double, const ThreeMomentum&);
27 ThreeMomentum multiply(const ThreeMomentum&, const double);
29 ThreeMomentum operator*(const double, const ThreeMomentum&);
30 ThreeMomentum operator*(const ThreeMomentum&, const double);
31 ThreeMomentum operator/(const ThreeMomentum&, const double);
32 ThreeMomentum operator+(const ThreeMomentum&, const ThreeMomentum&);
33 ThreeMomentum operator-(const ThreeMomentum&, const ThreeMomentum&);
34
35 class Matrix3;
36
37
38
40 class Vector3 : public Vector<3> {
41
42 friend class Matrix3;
43 friend Vector3 multiply(const double, const Vector3&);
44 friend Vector3 multiply(const Vector3&, const double);
45 friend Vector3 add(const Vector3&, const Vector3&);
46 friend Vector3 subtract(const Vector3&, const Vector3&);
47
48 public:
49 Vector3() : Vector<3>() { }
50
51 template<typename V3TYPE>
52 Vector3(const V3TYPE& other) {
53 this->setX(other.x());
54 this->setY(other.y());
55 this->setZ(other.z());
56 }
57
58 Vector3(const Vector<3>& other) {
59 this->setX(other.get(0));
60 this->setY(other.get(1));
61 this->setZ(other.get(2));
62 }
63
64 Vector3(double x, double y, double z) {
65 this->setX(x);
66 this->setY(y);
67 this->setZ(z);
68 }
69
70 ~Vector3() { }
71
72
73 public:
74
75 static Vector3 mkX() { return Vector3(1,0,0); }
76 static Vector3 mkY() { return Vector3(0,1,0); }
77 static Vector3 mkZ() { return Vector3(0,0,1); }
78
79
80 public:
81
82 double x() const { return get(0); }
83 double x2() const { return sqr(x()); }
84 Vector3& setX(double x) { set(0, x); return *this; }
85
86 double y() const { return get(1); }
87 double y2() const { return sqr(y()); }
88 Vector3& setY(double y) { set(1, y); return *this; }
89
90 double z() const { return get(2); }
91 double z2() const { return sqr(z()); }
92 Vector3& setZ(double z) { set(2, z); return *this; }
93
94
96 double dot(const Vector3& v) const {
97 return _vec.dot(v._vec);
98 }
99
101 Vector3 cross(const Vector3& v) const {
102 Vector3 result;
103 result._vec = _vec.cross(v._vec);
104 return result;
105 }
106
108 double angle(const Vector3& v) const {
109 const double localDotOther = unit().dot(v.unit());
110 if (localDotOther > 1.0) return 0.0;
111 if (localDotOther < -1.0) return M_PI;
112 return acos(localDotOther);
113 }
114
115
117 Vector3 unitVec() const {
118 double md = mod();
119 if ( fuzzyLessEquals(md, 0.0) ) return Vector3();
120 else return *this * 1.0/md;
121 }
122
124 Vector3 unit() const {
125 return unitVec();
126 }
127
129 Vector3 polarVec() const {
130 Vector3 rtn = *this;
131 rtn.setZ(0.);
132 return rtn;
133 }
134
135 Vector3 perpVec() const {
136 return polarVec();
137 }
138
139 Vector3 rhoVec() const {
140 return polarVec();
141 }
142
144 double polarRadius2() const {
145 return x()*x() + y()*y();
146 }
147
148 double perp2() const {
149 return polarRadius2();
150 }
151
152 double rho2() const {
153 return polarRadius2();
154 }
155
157 double polarRadius() const {
158 return sqrt(polarRadius2());
159 }
160
161 double perp() const {
162 return polarRadius();
163 }
164
165 double rho() const {
166 return polarRadius();
167 }
168
173 double azimuthalAngle(const PhiMapping mapping = ZERO_2PI) const {
174 // If this has a null perp-vector, return zero rather than let atan2 set an error state
175 // This isn't necessary if the implementation supports IEEE floating-point arithmetic (IEC 60559)... are we sure?
176 if (x() == 0 && y() == 0) return 0.0; //< Or return nan / throw an exception?
177 // Calculate the arctan and return in the requested range
178 const double value = atan2( y(), x() );
179 return mapAngle(value, mapping);
180 }
181
182 double phi(const PhiMapping mapping = ZERO_2PI) const {
183 return azimuthalAngle(mapping);
184 }
185
187 double tanTheta() const {
188 return polarRadius()/z();
189 }
190
192 double sinTheta() const {
193 return sqrt(polarRadius2()/mod2());
194 }
195
197 double cosTheta() const {
198 return z()/mod2();
199 }
200
202 double polarAngle() const {
203 // Get number beween [0,PI]
204 const double polarangle = atan2(polarRadius(), z());
205 return mapAngle0ToPi(polarangle);
206 }
207
209 double theta() const {
210 return polarAngle();
211 }
212
221 double pseudorapidity() const {
222 if (mod() == 0.0) return 0.0;
223 if (mod() == fabs(z()) ) return std::copysign(INF, z());
224 const double eta = std::log((mod() + fabs(z())) / perp());
225 return std::copysign(eta, z());
226 }
227
229 double eta() const {
230 return pseudorapidity();
231 }
232
234 double abseta() const {
235 return fabs(eta());
236 }
237
238
239 public:
240
242 Vector3& operator *= (const double a) {
243 _vec = multiply(a, *this)._vec;
244 return *this;
245 }
246
248 Vector3& operator /= (const double a) {
249 _vec = multiply(1.0/a, *this)._vec;
250 return *this;
251 }
252
254 Vector3& operator += (const Vector3& v) {
255 _vec = add(*this, v)._vec;
256 return *this;
257 }
258
260 Vector3& operator -= (const Vector3& v) {
261 _vec = subtract(*this, v)._vec;
262 return *this;
263 }
264
266 Vector3 operator - () const {
267 Vector3 rtn;
268 rtn._vec = -_vec;
269 return rtn;
270 }
271
272 };
273
274
275
277 inline double dot(const Vector3& a, const Vector3& b) {
278 return a.dot(b);
279 }
280
282 inline Vector3 cross(const Vector3& a, const Vector3& b) {
283 return a.cross(b);
284 }
285
287 inline Vector3 multiply(const double a, const Vector3& v) {
288 Vector3 result;
289 result._vec = a * v._vec;
290 return result;
291 }
292
294 inline Vector3 multiply(const Vector3& v, const double a) {
295 return multiply(a, v);
296 }
297
299 inline Vector3 operator * (const double a, const Vector3& v) {
300 return multiply(a, v);
301 }
302
304 inline Vector3 operator * (const Vector3& v, const double a) {
305 return multiply(a, v);
306 }
307
309 inline Vector3 operator / (const Vector3& v, const double a) {
310 return multiply(1.0/a, v);
311 }
312
314 inline Vector3 add(const Vector3& a, const Vector3& b) {
315 Vector3 result;
316 result._vec = a._vec + b._vec;
317 return result;
318 }
319
321 inline Vector3 subtract(const Vector3& a, const Vector3& b) {
322 Vector3 result;
323 result._vec = a._vec - b._vec;
324 return result;
325 }
326
328 inline Vector3 operator + (const Vector3& a, const Vector3& b) {
329 return add(a, b);
330 }
331
333 inline Vector3 operator - (const Vector3& a, const Vector3& b) {
334 return subtract(a, b);
335 }
336
337 // More physicsy coordinates etc.
338
340 inline double angle(const Vector3& a, const Vector3& b) {
341 return a.angle(b);
342 }
343
344
346
347
349 class ThreeMomentum : public ThreeVector {
350 public:
351 ThreeMomentum() { }
352
353 template<typename V3TYPE, typename std::enable_if<HasXYZ<V3TYPE>::value, int>::type DUMMY=0>
354 ThreeMomentum(const V3TYPE& other) {
355 this->setPx(other.x());
356 this->setPy(other.y());
357 this->setPz(other.z());
358 }
359
360 ThreeMomentum(const Vector<3>& other)
361 : ThreeVector(other) { }
362
363 ThreeMomentum(const double px, const double py, const double pz) {
364 this->setPx(px);
365 this->setPy(py);
366 this->setPz(pz);
367 }
368
369 ~ThreeMomentum() {}
370
371 public:
372
373
376
378 ThreeMomentum& setPx(double px) {
379 setX(px);
380 return *this;
381 }
382
384 ThreeMomentum& setPy(double py) {
385 setY(py);
386 return *this;
387 }
388
390 ThreeMomentum& setPz(double pz) {
391 setZ(pz);
392 return *this;
393 }
394
396
397
400
402 double px() const { return x(); }
404 double px2() const { return x2(); }
405
407 double py() const { return y(); }
409 double py2() const { return y2(); }
410
412 double pz() const { return z(); }
414 double pz2() const { return z2(); }
415
416
418 double p() const { return mod(); }
420 double p2() const { return mod2(); }
421
422
424 ThreeMomentum pTvec() const {
425 return polarVec();
426 }
427
428 ThreeMomentum ptvec() const {
429 return pTvec();
430 }
431
433 double pT2() const {
434 return polarRadius2();
435 }
436
437 double pt2() const {
438 return polarRadius2();
439 }
440
442 double pT() const {
443 return sqrt(pT2());
444 }
445
446 double pt() const {
447 return sqrt(pT2());
448 }
449
451
452
454
455
458
460 ThreeMomentum& operator *= (double a) {
461 _vec = multiply(a, *this)._vec;
462 return *this;
463 }
464
466 ThreeMomentum& operator /= (double a) {
467 _vec = multiply(1.0/a, *this)._vec;
468 return *this;
469 }
470
472 ThreeMomentum& operator += (const ThreeMomentum& v) {
473 _vec = add(*this, v)._vec;
474 return *this;
475 }
476
478 ThreeMomentum& operator -= (const ThreeMomentum& v) {
479 _vec = add(*this, -v)._vec;
480 return *this;
481 }
482
484 ThreeMomentum operator - () const {
485 ThreeMomentum result;
486 result._vec = -_vec;
487 return result;
488 }
489
490 // /// Multiply space (i.e. all!) components by -1.
491 // ThreeMomentum reverse() const {
492 // return -*this;
493 // }
494
496
497 };
498
499
500 inline ThreeMomentum multiply(const double a, const ThreeMomentum& v) {
501 ThreeMomentum result;
502 result._vec = a * v._vec;
503 return result;
504 }
505
506 inline ThreeMomentum multiply(const ThreeMomentum& v, const double a) {
507 return multiply(a, v);
508 }
509
510 inline ThreeMomentum operator*(const double a, const ThreeMomentum& v) {
511 return multiply(a, v);
512 }
513
514 inline ThreeMomentum operator*(const ThreeMomentum& v, const double a) {
515 return multiply(a, v);
516 }
517
518 inline ThreeMomentum operator/(const ThreeMomentum& v, const double a) {
519 return multiply(1.0/a, v);
520 }
521
522 inline ThreeMomentum add(const ThreeMomentum& a, const ThreeMomentum& b) {
523 ThreeMomentum result;
524 result._vec = a._vec + b._vec;
525 return result;
526 }
527
528 inline ThreeMomentum operator+(const ThreeMomentum& a, const ThreeMomentum& b) {
529 return add(a, b);
530 }
531
532 inline ThreeMomentum operator-(const ThreeMomentum& a, const ThreeMomentum& b) {
533 return add(a, -b);
534 }
535
536
539 inline Vector3 operator+(const ThreeMomentum& a, const Vector3& b) {
540 return add(static_cast<const Vector3&>(a), b);
541 }
542 inline Vector3 operator+(const Vector3& a, const ThreeMomentum& b) {
543 return add(a, static_cast<const Vector3&>(b));
544 }
545
546 inline Vector3 operator-(const ThreeMomentum& a, const Vector3& b) {
547 return add(static_cast<const Vector3&>(a), -b);
548 }
549 inline Vector3 operator-(const Vector3& a, const ThreeMomentum& b) {
550 return add(a, -static_cast<const Vector3&>(b));
551 }
552
553
554
556
557
560
562 inline double deltaEta(const Vector3& a, const Vector3& b, bool sign=false) {
563 return deltaEta(a.pseudorapidity(), b.pseudorapidity(), sign);
564 }
565
567 inline double deltaEta(const Vector3& v, double eta2, bool sign=false) {
568 return deltaEta(v.pseudorapidity(), eta2, sign);
569 }
570
572 inline double deltaEta(double eta1, const Vector3& v, bool sign=false) {
573 return deltaEta(eta1, v.pseudorapidity(), sign);
574 }
575
577
578
581
583 inline double deltaPhi(const Vector3& a, const Vector3& b, bool sign=false) {
584 return deltaPhi(a.azimuthalAngle(), b.azimuthalAngle(), sign);
585 }
586
588 inline double deltaPhi(const Vector3& v, double phi2, bool sign=false) {
589 return deltaPhi(v.azimuthalAngle(), phi2, sign);
590 }
591
593 inline double deltaPhi(double phi1, const Vector3& v, bool sign=false) {
594 return deltaPhi(phi1, v.azimuthalAngle(), sign);
595 }
596
598
599
602
604 inline double deltaR2(const Vector3& a, const Vector3& b) {
605 return deltaR2(a.pseudorapidity(), a.azimuthalAngle(),
607 }
608
610 inline double deltaR(const Vector3& a, const Vector3& b) {
611 return sqrt(deltaR2(a,b));
612 }
613
615 inline double deltaR2(const Vector3& v, double eta2, double phi2) {
616 return deltaR2(v.pseudorapidity(), v.azimuthalAngle(), eta2, phi2);
617 }
618
620 inline double deltaR(const Vector3& v, double eta2, double phi2) {
621 return sqrt(deltaR2(v, eta2, phi2));
622 }
623
625 inline double deltaR2(double eta1, double phi1, const Vector3& v) {
626 return deltaR2(eta1, phi1, v.pseudorapidity(), v.azimuthalAngle());
627 }
628
630 inline double deltaR(double eta1, double phi1, const Vector3& v) {
631 return sqrt(deltaR2(eta1, phi1, v));
632 }
633
635
636
639
643 inline double mT(const Vector3& vis, const Vector3& invis) {
644 // return sqrt(2*vis.perp()*invis.perp() * (1 - cos(deltaPhi(vis, invis))) );
645 return mT(vis.perp(), invis.perp(), deltaPhi(vis, invis));
646 }
647
649 inline double pT(const Vector3& a, const Vector3& b) {
650 return (a+b).perp();
651 }
652
654
655
656}
657
658#endif
Specialisation of MatrixN to aid 3 dimensional rotations.
Definition Matrix3.hh:13
Specialized version of the ThreeVector with momentum functionality.
Definition Vector3.hh:349
double pt() const
Calculate the transverse momentum .
Definition Vector3.hh:446
double pz() const
Get z-component of momentum .
Definition Vector3.hh:412
double pz2() const
Get z-squared .
Definition Vector3.hh:414
double pT() const
Calculate the transverse momentum .
Definition Vector3.hh:442
double p() const
Get the modulus of the 3-momentum.
Definition Vector3.hh:418
ThreeMomentum pTvec() const
Calculate the transverse momentum vector .
Definition Vector3.hh:424
double px() const
Get x-component of momentum .
Definition Vector3.hh:402
double py() const
Get y-component of momentum .
Definition Vector3.hh:407
ThreeMomentum & setPy(double py)
Set y-component of momentum .
Definition Vector3.hh:384
ThreeMomentum & operator-=(const ThreeMomentum &v)
Subtract two 3-momenta.
Definition Vector3.hh:478
double pT2() const
Calculate the squared transverse momentum .
Definition Vector3.hh:433
double px2() const
Get x-squared .
Definition Vector3.hh:404
ThreeMomentum & setPz(double pz)
Set z-component of momentum .
Definition Vector3.hh:390
double pt2() const
Calculate the squared transverse momentum .
Definition Vector3.hh:437
ThreeMomentum & operator/=(double a)
Divide by a scalar.
Definition Vector3.hh:466
ThreeMomentum & operator+=(const ThreeMomentum &v)
Add two 3-momenta.
Definition Vector3.hh:472
ThreeMomentum & setPx(double px)
Set x-component of momentum .
Definition Vector3.hh:378
ThreeMomentum operator-() const
Multiply all components by -1.
Definition Vector3.hh:484
ThreeMomentum & operator*=(double a)
Multiply by a scalar.
Definition Vector3.hh:460
ThreeMomentum ptvec() const
Synonym for pTvec.
Definition Vector3.hh:428
double py2() const
Get y-squared .
Definition Vector3.hh:409
double p2() const
Get the modulus-squared of the 3-momentum.
Definition Vector3.hh:420
Three-dimensional specialisation of Vector.
Definition Vector3.hh:40
Vector3 rhoVec() const
Synonym for polarVec.
Definition Vector3.hh:139
double polarRadius() const
Polar radius.
Definition Vector3.hh:157
double cosTheta() const
Cosine of the polar angle.
Definition Vector3.hh:197
Vector3 cross(const Vector3 &v) const
Cross-product with another vector.
Definition Vector3.hh:101
double rho() const
Synonym for polarRadius.
Definition Vector3.hh:165
double eta() const
Synonym for pseudorapidity.
Definition Vector3.hh:229
friend Vector3 add(const Vector3 &, const Vector3 &)
Unbound vector addition function.
Definition Vector3.hh:314
double perp() const
Synonym for polarRadius.
Definition Vector3.hh:161
Vector3 & operator-=(const Vector3 &v)
In-place subtraction operator.
Definition Vector3.hh:260
double perp2() const
Synonym for polarRadius2.
Definition Vector3.hh:148
double pseudorapidity() const
Purely geometric approximation to rapidity.
Definition Vector3.hh:221
friend Vector3 multiply(const double, const Vector3 &)
Unbound scalar-product function.
Definition Vector3.hh:287
friend Vector3 subtract(const Vector3 &, const Vector3 &)
Unbound vector subtraction function.
Definition Vector3.hh:321
Vector3 unit() const
Synonym for unitVec.
Definition Vector3.hh:124
double abseta() const
Convenience shortcut for fabs(eta()).
Definition Vector3.hh:234
double tanTheta() const
Tangent of the polar angle.
Definition Vector3.hh:187
double theta() const
Synonym for polarAngle.
Definition Vector3.hh:209
double dot(const Vector3 &v) const
Dot-product with another vector.
Definition Vector3.hh:96
double phi(const PhiMapping mapping=ZERO_2PI) const
Synonym for azimuthalAngle.
Definition Vector3.hh:182
Vector3 & operator+=(const Vector3 &v)
In-place addition operator.
Definition Vector3.hh:254
Vector3 & operator*=(const double a)
In-place scalar multiplication operator.
Definition Vector3.hh:242
double polarRadius2() const
Square of the polar radius (.
Definition Vector3.hh:144
double sinTheta() const
Sine of the polar angle.
Definition Vector3.hh:192
Vector3 perpVec() const
Synonym for polarVec.
Definition Vector3.hh:135
double azimuthalAngle(const PhiMapping mapping=ZERO_2PI) const
Angle subtended by the vector's projection in x-y and the x-axis.
Definition Vector3.hh:173
double rho2() const
Synonym for polarRadius2.
Definition Vector3.hh:152
Vector3 & operator/=(const double a)
In-place scalar division operator.
Definition Vector3.hh:248
double angle(const Vector3 &v) const
Angle in radians to another vector.
Definition Vector3.hh:108
double polarAngle() const
Angle subtended by the vector and the z-axis.
Definition Vector3.hh:202
Vector3 operator-() const
In-place negation operator.
Definition Vector3.hh:266
Vector3 unitVec() const
Unit-normalized version of this vector.
Definition Vector3.hh:117
Vector3 polarVec() const
Polar projection of this vector into the x-y plane.
Definition Vector3.hh:129
A minimal base class for -dimensional vectors.
Definition VectorN.hh:23
double mod() const
Definition VectorN.hh:95
double mod2() const
Definition VectorN.hh:84
Vector< N > & set(const size_t index, const double value)
Definition VectorN.hh:60
double pT(const Vector3 &a, const Vector3 &b)
Calculate transverse momentum of pair of 3-vectors.
Definition Vector3.hh:649
Definition MC_CENT_PPB_Projections.hh:10
constexpr std::enable_if_t< std::is_arithmetic_v< NUM >, int > sign(NUM val)
Find the sign of a number.
Definition MathUtils.hh:275
double deltaR(double rap1, double phi1, double rap2, double phi2)
Definition MathUtils.hh:708
double deltaPhi(double phi1, double phi2, bool sign=false)
Calculate the difference between two angles in radians.
Definition MathUtils.hh:678
double subtract(double a, double b, double tolerance=1e-5)
Subtract two numbers with FP fuzziness.
Definition MathUtils.hh:223
double deltaEta(double eta1, double eta2, bool sign=false)
Definition MathUtils.hh:686
PhiMapping
Enum for range of to be mapped into.
Definition MathConstants.hh:49
double deltaR2(double rap1, double phi1, double rap2, double phi2)
Definition MathUtils.hh:701
double mT(double pT1, double pT2, double dphi)
Definition MathUtils.hh:731
std::enable_if_t< std::is_arithmetic_v< NUM >, NUM > sqr(NUM a)
Named number-type squaring operation.
Definition MathUtils.hh:218
double add(double a, double b, double tolerance=1e-5)
Add two numbers with FP fuzziness.
Definition MathUtils.hh:229
Vector3 cross(const Vector3 &a, const Vector3 &b)
Unbound cross-product function.
Definition Vector3.hh:282
double mapAngle(double angle, PhiMapping mapping)
Map an angle into the enum-specified range.
Definition MathUtils.hh:656
double mapAngle0ToPi(double angle)
Map an angle into the range [0, PI].
Definition MathUtils.hh:648
double angle(const Vector2 &a, const Vector2 &b)
Angle (in radians) between two 2-vectors.
Definition Vector2.hh:177
std::enable_if_t< std::is_arithmetic_v< N1 > &&std::is_arithmetic_v< N2 >, bool > fuzzyLessEquals(N1 a, N2 b, double tolerance=1e-5)
Compare two floating point numbers for <= with a degree of fuzziness.
Definition MathUtils.hh:94