Rivet API documentation

Rivet 4.1.3
Vector3.hh
1#ifndef RIVET_MATH_VECTOR3
2#define RIVET_MATH_VECTOR3
3
4#include "Rivet/Math/MathConstants.hh"
5#include "Rivet/Math/MathUtils.hh"
6#include "Rivet/Math/VectorN.hh"
7#include "Rivet/Tools/TypeTraits.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
39 class Vector3 : public Vector<3> {
40
41 friend class Matrix3;
42 friend Vector3 multiply(const double, const Vector3&);
43 friend Vector3 multiply(const Vector3&, const double);
44 friend Vector3 add(const Vector3&, const Vector3&);
45 friend Vector3 subtract(const Vector3&, const Vector3&);
46
47 public:
48
49 Vector3()
50 : Vector<3>() { }
51
52 template <typename V3TYPE>
53 Vector3(const V3TYPE& other) {
54 this->setX(other.x());
55 this->setY(other.y());
56 this->setZ(other.z());
57 }
58
59 Vector3(const Vector<3>& other) {
60 this->setX(other.get(0));
61 this->setY(other.get(1));
62 this->setZ(other.get(2));
63 }
64
65 Vector3(double x, double y, double z) {
66 this->setX(x);
67 this->setY(y);
68 this->setZ(z);
69 }
70
71 ~Vector3() { }
72
73
74 public:
75
76 static Vector3 mkX() {
77 return Vector3(1, 0, 0);
78 }
79 static Vector3 mkY() {
80 return Vector3(0, 1, 0);
81 }
82 static Vector3 mkZ() {
83 return Vector3(0, 0, 1);
84 }
85
86
87 public:
88
89 double x() const {
90 return get(0);
91 }
92 double x2() const {
93 return sqr(x());
94 }
95 Vector3& setX(double x) {
96 set(0, x);
97 return *this;
98 }
99
100 double y() const {
101 return get(1);
102 }
103 double y2() const {
104 return sqr(y());
105 }
106 Vector3& setY(double y) {
107 set(1, y);
108 return *this;
109 }
110
111 double z() const {
112 return get(2);
113 }
114 double z2() const {
115 return sqr(z());
116 }
117 Vector3& setZ(double z) {
118 set(2, z);
119 return *this;
120 }
121
122
124 double dot(const Vector3& v) const {
125 return _vec.dot(v._vec);
126 }
127
129 Vector3 cross(const Vector3& v) const {
130 Vector3 result;
131 result._vec = _vec.cross(v._vec);
132 return result;
133 }
134
136 double angle(const Vector3& v) const {
137 const double localDotOther = unit().dot(v.unit());
138 if (localDotOther > 1.0) return 0.0;
139 if (localDotOther < -1.0) return M_PI;
140 return acos(localDotOther);
141 }
142
143
145 Vector3 unitVec() const {
146 double md = mod();
147 if (fuzzyLessEquals(md, 0.0))
148 return Vector3();
149 else
150 return *this * 1.0 / md;
151 }
152
154 Vector3 unit() const {
155 return unitVec();
156 }
157
159 Vector3 polarVec() const {
160 Vector3 rtn = *this;
161 rtn.setZ(0.);
162 return rtn;
163 }
164
165 Vector3 perpVec() const {
166 return polarVec();
167 }
168
169 Vector3 rhoVec() const {
170 return polarVec();
171 }
172
174 double polarRadius2() const {
175 return x() * x() + y() * y();
176 }
177
178 double perp2() const {
179 return polarRadius2();
180 }
181
182 double rho2() const {
183 return polarRadius2();
184 }
185
187 double polarRadius() const {
188 return sqrt(polarRadius2());
189 }
190
191 double perp() const {
192 return polarRadius();
193 }
194
195 double rho() const {
196 return polarRadius();
197 }
198
203 double azimuthalAngle(const PhiMapping mapping = ZERO_2PI) const {
204 // If this has a null perp-vector, return zero rather than let atan2 set an error state
205 // This isn't necessary if the implementation supports IEEE floating-point arithmetic (IEC 60559)... are we sure?
206 if (x() == 0 && y() == 0) return 0.0; //< Or return nan / throw an exception?
207 // Calculate the arctan and return in the requested range
208 const double value = atan2(y(), x());
209 return mapAngle(value, mapping);
210 }
211
212 double phi(const PhiMapping mapping = ZERO_2PI) const {
213 return azimuthalAngle(mapping);
214 }
215
217 double tanTheta() const {
218 return polarRadius() / z();
219 }
220
222 double sinTheta() const {
223 return sqrt(polarRadius2() / mod2());
224 }
225
227 double cosTheta() const {
228 return z() / mod2();
229 }
230
232 double polarAngle() const {
233 // Get number beween [0,PI]
234 const double polarangle = atan2(polarRadius(), z());
235 return mapAngle0ToPi(polarangle);
236 }
237
239 double theta() const {
240 return polarAngle();
241 }
242
251 double pseudorapidity() const {
252 if (mod() == 0.0) return 0.0;
253 if (mod() == fabs(z())) return std::copysign(INF, z());
254 const double eta = std::log((mod() + fabs(z())) / perp());
255 return std::copysign(eta, z());
256 }
257
259 double eta() const {
260 return pseudorapidity();
261 }
262
264 double abseta() const {
265 return fabs(eta());
266 }
267
268
269 public:
270
272 Vector3& operator*=(const double a) {
273 _vec = multiply(a, *this)._vec;
274 return *this;
275 }
276
278 Vector3& operator/=(const double a) {
279 _vec = multiply(1.0 / a, *this)._vec;
280 return *this;
281 }
282
284 Vector3& operator+=(const Vector3& v) {
285 _vec = add(*this, v)._vec;
286 return *this;
287 }
288
290 Vector3& operator-=(const Vector3& v) {
291 _vec = subtract(*this, v)._vec;
292 return *this;
293 }
294
296 Vector3 operator-() const {
297 Vector3 rtn;
298 rtn._vec = -_vec;
299 return rtn;
300 }
301 };
302
303
305 inline double dot(const Vector3& a, const Vector3& b) {
306 return a.dot(b);
307 }
308
310 inline Vector3 cross(const Vector3& a, const Vector3& b) {
311 return a.cross(b);
312 }
313
315 inline Vector3 multiply(const double a, const Vector3& v) {
316 Vector3 result;
317 result._vec = a * v._vec;
318 return result;
319 }
320
322 inline Vector3 multiply(const Vector3& v, const double a) {
323 return multiply(a, v);
324 }
325
327 inline Vector3 operator*(const double a, const Vector3& v) {
328 return multiply(a, v);
329 }
330
332 inline Vector3 operator*(const Vector3& v, const double a) {
333 return multiply(a, v);
334 }
335
337 inline Vector3 operator/(const Vector3& v, const double a) {
338 return multiply(1.0 / a, v);
339 }
340
342 inline Vector3 add(const Vector3& a, const Vector3& b) {
343 Vector3 result;
344 result._vec = a._vec + b._vec;
345 return result;
346 }
347
349 inline Vector3 subtract(const Vector3& a, const Vector3& b) {
350 Vector3 result;
351 result._vec = a._vec - b._vec;
352 return result;
353 }
354
356 inline Vector3 operator+(const Vector3& a, const Vector3& b) {
357 return add(a, b);
358 }
359
361 inline Vector3 operator-(const Vector3& a, const Vector3& b) {
362 return subtract(a, b);
363 }
364
365 // More physicsy coordinates etc.
366
368 inline double angle(const Vector3& a, const Vector3& b) {
369 return a.angle(b);
370 }
371
372
374
375
377 class ThreeMomentum : public ThreeVector {
378 public:
379
380 ThreeMomentum() { }
381
382 template <typename V3TYPE, typename std::enable_if<HasXYZ<V3TYPE>::value, int>::type DUMMY = 0>
383 ThreeMomentum(const V3TYPE& other) {
384 this->setPx(other.x());
385 this->setPy(other.y());
386 this->setPz(other.z());
387 }
388
389 ThreeMomentum(const Vector<3>& other)
390 : ThreeVector(other) { }
391
392 ThreeMomentum(const double px, const double py, const double pz) {
393 this->setPx(px);
394 this->setPy(py);
395 this->setPz(pz);
396 }
397
398 ~ThreeMomentum() { }
399
400 public:
401
402
405
407 ThreeMomentum& setPx(double px) {
408 setX(px);
409 return *this;
410 }
411
413 ThreeMomentum& setPy(double py) {
414 setY(py);
415 return *this;
416 }
417
419 ThreeMomentum& setPz(double pz) {
420 setZ(pz);
421 return *this;
422 }
423
425
426
429
431 double px() const {
432 return x();
433 }
434
435 double px2() const {
436 return x2();
437 }
438
440 double py() const {
441 return y();
442 }
443
444 double py2() const {
445 return y2();
446 }
447
449 double pz() const {
450 return z();
451 }
452
453 double pz2() const {
454 return z2();
455 }
456
457
459 double p() const {
460 return mod();
461 }
462
463 double p2() const {
464 return mod2();
465 }
466
467
469 ThreeMomentum pTvec() const {
470 return polarVec();
471 }
472
473 ThreeMomentum ptvec() const {
474 return pTvec();
475 }
476
478 double pT2() const {
479 return polarRadius2();
480 }
481
482 double pt2() const {
483 return polarRadius2();
484 }
485
487 double pT() const {
488 return sqrt(pT2());
489 }
490
491 double pt() const {
492 return sqrt(pT2());
493 }
494
496
497
499
500
503
505 ThreeMomentum& operator*=(double a) {
506 _vec = multiply(a, *this)._vec;
507 return *this;
508 }
509
511 ThreeMomentum& operator/=(double a) {
512 _vec = multiply(1.0 / a, *this)._vec;
513 return *this;
514 }
515
517 ThreeMomentum& operator+=(const ThreeMomentum& v) {
518 _vec = add(*this, v)._vec;
519 return *this;
520 }
521
523 ThreeMomentum& operator-=(const ThreeMomentum& v) {
524 _vec = add(*this, -v)._vec;
525 return *this;
526 }
527
529 ThreeMomentum operator-() const {
530 ThreeMomentum result;
531 result._vec = -_vec;
532 return result;
533 }
534
535 // /// Multiply space (i.e. all!) components by -1.
536 // ThreeMomentum reverse() const {
537 // return -*this;
538 // }
539
541 };
542
543
544 inline ThreeMomentum multiply(const double a, const ThreeMomentum& v) {
545 ThreeMomentum result;
546 result._vec = a * v._vec;
547 return result;
548 }
549
550 inline ThreeMomentum multiply(const ThreeMomentum& v, const double a) {
551 return multiply(a, v);
552 }
553
554 inline ThreeMomentum operator*(const double a, const ThreeMomentum& v) {
555 return multiply(a, v);
556 }
557
558 inline ThreeMomentum operator*(const ThreeMomentum& v, const double a) {
559 return multiply(a, v);
560 }
561
562 inline ThreeMomentum operator/(const ThreeMomentum& v, const double a) {
563 return multiply(1.0 / a, v);
564 }
565
566 inline ThreeMomentum add(const ThreeMomentum& a, const ThreeMomentum& b) {
567 ThreeMomentum result;
568 result._vec = a._vec + b._vec;
569 return result;
570 }
571
572 inline ThreeMomentum operator+(const ThreeMomentum& a, const ThreeMomentum& b) {
573 return add(a, b);
574 }
575
576 inline ThreeMomentum operator-(const ThreeMomentum& a, const ThreeMomentum& b) {
577 return add(a, -b);
578 }
579
580
583 inline Vector3 operator+(const ThreeMomentum& a, const Vector3& b) {
584 return add(static_cast<const Vector3&>(a), b);
585 }
586 inline Vector3 operator+(const Vector3& a, const ThreeMomentum& b) {
587 return add(a, static_cast<const Vector3&>(b));
588 }
589
590 inline Vector3 operator-(const ThreeMomentum& a, const Vector3& b) {
591 return add(static_cast<const Vector3&>(a), -b);
592 }
593 inline Vector3 operator-(const Vector3& a, const ThreeMomentum& b) {
594 return add(a, -static_cast<const Vector3&>(b));
595 }
596
597
599
600
603
605 inline double deltaEta(const Vector3& a, const Vector3& b, bool sign = false) {
606 return deltaEta(a.pseudorapidity(), b.pseudorapidity(), sign);
607 }
608
610 inline double deltaEta(const Vector3& v, double eta2, bool sign = false) {
611 return deltaEta(v.pseudorapidity(), eta2, sign);
612 }
613
615 inline double deltaEta(double eta1, const Vector3& v, bool sign = false) {
616 return deltaEta(eta1, v.pseudorapidity(), sign);
617 }
618
620
621
624
626 inline double deltaPhi(const Vector3& a, const Vector3& b, bool sign = false) {
627 return deltaPhi(a.azimuthalAngle(), b.azimuthalAngle(), sign);
628 }
629
631 inline double deltaPhi(const Vector3& v, double phi2, bool sign = false) {
632 return deltaPhi(v.azimuthalAngle(), phi2, sign);
633 }
634
636 inline double deltaPhi(double phi1, const Vector3& v, bool sign = false) {
637 return deltaPhi(phi1, v.azimuthalAngle(), sign);
638 }
639
641
642
645
647 inline double deltaR2(const Vector3& a, const Vector3& b) {
649 }
650
652 inline double deltaR(const Vector3& a, const Vector3& b) {
653 return sqrt(deltaR2(a, b));
654 }
655
657 inline double deltaR2(const Vector3& v, double eta2, double phi2) {
658 return deltaR2(v.pseudorapidity(), v.azimuthalAngle(), eta2, phi2);
659 }
660
662 inline double deltaR(const Vector3& v, double eta2, double phi2) {
663 return sqrt(deltaR2(v, eta2, phi2));
664 }
665
667 inline double deltaR2(double eta1, double phi1, const Vector3& v) {
668 return deltaR2(eta1, phi1, v.pseudorapidity(), v.azimuthalAngle());
669 }
670
672 inline double deltaR(double eta1, double phi1, const Vector3& v) {
673 return sqrt(deltaR2(eta1, phi1, v));
674 }
675
677
678
681
685 inline double mT(const Vector3& vis, const Vector3& invis) {
686 // return sqrt(2*vis.perp()*invis.perp() * (1 - cos(deltaPhi(vis, invis))) );
687 return mT(vis.perp(), invis.perp(), deltaPhi(vis, invis));
688 }
689
691 inline double pT(const Vector3& a, const Vector3& b) {
692 return (a + b).perp();
693 }
694
696
697
698}
699
700#endif
Specialisation of MatrixN to aid 3 dimensional rotations.
Definition Matrix3.hh:13
Specialized version of the ThreeVector with momentum functionality.
Definition Vector3.hh:377
double pt() const
Calculate the transverse momentum $p_T$.
Definition Vector3.hh:491
double pz() const
Get z-component of momentum $p_z$.
Definition Vector3.hh:449
double pz2() const
Get z-squared $p_z^2$.
Definition Vector3.hh:453
double pT() const
Calculate the transverse momentum $p_T$.
Definition Vector3.hh:487
double p() const
Get the modulus of the 3-momentum.
Definition Vector3.hh:459
ThreeMomentum pTvec() const
Calculate the transverse momentum vector $\vec{p}_T$.
Definition Vector3.hh:469
double px() const
Get x-component of momentum $p_x$.
Definition Vector3.hh:431
double py() const
Get y-component of momentum $p_y$.
Definition Vector3.hh:440
ThreeMomentum & setPy(double py)
Set y-component of momentum $p_y$.
Definition Vector3.hh:413
ThreeMomentum & operator-=(const ThreeMomentum &v)
Subtract two 3-momenta.
Definition Vector3.hh:523
double pT2() const
Calculate the squared transverse momentum $p_T^2$.
Definition Vector3.hh:478
double px2() const
Get x-squared $p_x^2$.
Definition Vector3.hh:435
ThreeMomentum & setPz(double pz)
Set z-component of momentum $p_z$.
Definition Vector3.hh:419
double pt2() const
Calculate the squared transverse momentum $p_T^2$.
Definition Vector3.hh:482
ThreeMomentum & operator/=(double a)
Divide by a scalar.
Definition Vector3.hh:511
ThreeMomentum & operator+=(const ThreeMomentum &v)
Add two 3-momenta.
Definition Vector3.hh:517
ThreeMomentum & setPx(double px)
Set x-component of momentum $p_x$.
Definition Vector3.hh:407
ThreeMomentum operator-() const
Multiply all components by -1.
Definition Vector3.hh:529
ThreeMomentum & operator*=(double a)
Multiply by a scalar.
Definition Vector3.hh:505
ThreeMomentum ptvec() const
Synonym for pTvec.
Definition Vector3.hh:473
double py2() const
Get y-squared $p_y^2$.
Definition Vector3.hh:444
double p2() const
Get the modulus-squared of the 3-momentum.
Definition Vector3.hh:463
Three-dimensional specialisation of Vector.
Definition Vector3.hh:39
Vector3 rhoVec() const
Synonym for polarVec.
Definition Vector3.hh:169
double polarRadius() const
Polar radius.
Definition Vector3.hh:187
double cosTheta() const
Cosine of the polar angle.
Definition Vector3.hh:227
Vector3 cross(const Vector3 &v) const
Cross-product with another vector.
Definition Vector3.hh:129
double rho() const
Synonym for polarRadius.
Definition Vector3.hh:195
double eta() const
Synonym for pseudorapidity.
Definition Vector3.hh:259
friend Vector3 add(const Vector3 &, const Vector3 &)
Unbound vector addition function.
Definition Vector3.hh:342
double perp() const
Synonym for polarRadius.
Definition Vector3.hh:191
Vector3 & operator-=(const Vector3 &v)
In-place subtraction operator.
Definition Vector3.hh:290
double perp2() const
Synonym for polarRadius2.
Definition Vector3.hh:178
double pseudorapidity() const
Purely geometric approximation to rapidity.
Definition Vector3.hh:251
friend Vector3 multiply(const double, const Vector3 &)
Unbound scalar-product function.
Definition Vector3.hh:315
friend Vector3 subtract(const Vector3 &, const Vector3 &)
Unbound vector subtraction function.
Definition Vector3.hh:349
Vector3 unit() const
Synonym for unitVec.
Definition Vector3.hh:154
double abseta() const
Convenience shortcut for fabs(eta()).
Definition Vector3.hh:264
double tanTheta() const
Tangent of the polar angle.
Definition Vector3.hh:217
double theta() const
Synonym for polarAngle.
Definition Vector3.hh:239
double dot(const Vector3 &v) const
Dot-product with another vector.
Definition Vector3.hh:124
double phi(const PhiMapping mapping=ZERO_2PI) const
Synonym for azimuthalAngle.
Definition Vector3.hh:212
Vector3 & operator+=(const Vector3 &v)
In-place addition operator.
Definition Vector3.hh:284
Vector3 & operator*=(const double a)
In-place scalar multiplication operator.
Definition Vector3.hh:272
double polarRadius2() const
Square of the polar radius (.
Definition Vector3.hh:174
double sinTheta() const
Sine of the polar angle.
Definition Vector3.hh:222
Vector3 perpVec() const
Synonym for polarVec.
Definition Vector3.hh:165
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:203
double rho2() const
Synonym for polarRadius2.
Definition Vector3.hh:182
Vector3 & operator/=(const double a)
In-place scalar division operator.
Definition Vector3.hh:278
double angle(const Vector3 &v) const
Angle in radians to another vector.
Definition Vector3.hh:136
double polarAngle() const
Angle subtended by the vector and the z-axis.
Definition Vector3.hh:232
Vector3 operator-() const
In-place negation operator.
Definition Vector3.hh:296
Vector3 unitVec() const
Unit-normalized version of this vector.
Definition Vector3.hh:145
Vector3 polarVec() const
Polar projection of this vector into the x-y plane.
Definition Vector3.hh:159
A minimal base class for -dimensional vectors.
Definition VectorN.hh:23
double mod() const
Definition VectorN.hh:99
double mod2() const
Definition VectorN.hh:88
Vector< N > & set(const size_t index, const double value)
Definition VectorN.hh:63
double pT(const Vector3 &a, const Vector3 &b)
Calculate transverse momentum of pair of 3-vectors.
Definition Vector3.hh:691
Definition LHCbCommon.hh:9
constexpr std::enable_if_t< std::is_arithmetic_v< NUM >, int > sign(NUM val)
Find the sign of a number.
Definition MathUtils.hh:295
double deltaR(double rap1, double phi1, double rap2, double phi2)
Definition MathUtils.hh:754
double deltaPhi(double phi1, double phi2, bool sign=false)
Calculate the difference between two angles in radians.
Definition MathUtils.hh:724
double subtract(double a, double b, double tolerance=1e-5)
Subtract two numbers with FP fuzziness.
Definition MathUtils.hh:242
double deltaEta(double eta1, double eta2, bool sign=false)
Definition MathUtils.hh:732
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:747
double mT(double pT1, double pT2, double dphi)
Definition MathUtils.hh:777
std::enable_if_t< std::is_arithmetic_v< NUM >, NUM > sqr(NUM a)
Named number-type squaring operation.
Definition MathUtils.hh:237
double add(double a, double b, double tolerance=1e-5)
Add two numbers with FP fuzziness.
Definition MathUtils.hh:248
Vector3 cross(const Vector3 &a, const Vector3 &b)
Unbound cross-product function.
Definition Vector3.hh:310
double mapAngle(double angle, PhiMapping mapping)
Map an angle into the enum-specified range.
Definition MathUtils.hh:706
double mapAngle0ToPi(double angle)
Map an angle into the range [0, PI].
Definition MathUtils.hh:698
double angle(const Vector2 &a, const Vector2 &b)
Angle (in radians) between two 2-vectors.
Definition Vector2.hh:194
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:103