rivet is hosted by Hepforge, IPPP Durham
Rivet 4.0.2
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);
28 ThreeMomentum add(const ThreeMomentum&, const ThreeMomentum&);
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 {
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 ( md <= 0.0 ) return Vector3();
120 else return *this * 1.0/md;
121 }
122
124 Vector3 unit() const {
125 return unitVec();
126 }
127
128
131 Vector3 rtn = *this;
132 rtn.setZ(0.);
133 return rtn;
134 }
136 Vector3 perpVec() const {
137 return polarVec();
138 }
140 Vector3 rhoVec() const {
141 return polarVec();
142 }
143
145 double polarRadius2() const {
146 return x()*x() + y()*y();
147 }
149 double perp2() const {
150 return polarRadius2();
151 }
153 double rho2() const {
154 return polarRadius2();
155 }
156
158 double polarRadius() const {
159 return sqrt(polarRadius2());
160 }
162 double perp() const {
163 return polarRadius();
164 }
166 double rho() const {
167 return polarRadius();
168 }
169
174 double azimuthalAngle(const PhiMapping mapping = ZERO_2PI) const {
175 // If this has a null perp-vector, return zero rather than let atan2 set an error state
176 // This isn't necessary if the implementation supports IEEE floating-point arithmetic (IEC 60559)... are we sure?
177 if (x() == 0 && y() == 0) return 0.0; //< Or return nan / throw an exception?
178 // Calculate the arctan and return in the requested range
179 const double value = atan2( y(), x() );
180 return mapAngle(value, mapping);
181 }
183 double phi(const PhiMapping mapping = ZERO_2PI) const {
184 return azimuthalAngle(mapping);
185 }
186
188 double tanTheta() const {
189 return polarRadius()/z();
190 }
191
193 double polarAngle() const {
194 // Get number beween [0,PI]
195 const double polarangle = atan2(polarRadius(), z());
197 }
198
200 double theta() const {
201 return polarAngle();
202 }
203
212 double pseudorapidity() const {
213 if (mod() == 0.0) return 0.0;
214 if (mod() == fabs(z()) ) return std::copysign(INF, z());
215 const double eta = std::log((mod() + fabs(z())) / perp());
216 return std::copysign(eta, z());
217 }
218
220 double eta() const {
221 return pseudorapidity();
222 }
223
225 double abseta() const {
226 return fabs(eta());
227 }
228
229
230 public:
231
233 Vector3& operator *= (const double a) {
234 _vec = multiply(a, *this)._vec;
235 return *this;
236 }
237
239 Vector3& operator /= (const double a) {
240 _vec = multiply(1.0/a, *this)._vec;
241 return *this;
242 }
243
246 _vec = add(*this, v)._vec;
247 return *this;
248 }
249
252 _vec = subtract(*this, v)._vec;
253 return *this;
254 }
255
258 Vector3 rtn;
259 rtn._vec = -_vec;
260 return rtn;
261 }
262
263 };
264
265
266
268 inline double dot(const Vector3& a, const Vector3& b) {
269 return a.dot(b);
270 }
271
273 inline Vector3 cross(const Vector3& a, const Vector3& b) {
274 return a.cross(b);
275 }
276
278 inline Vector3 multiply(const double a, const Vector3& v) {
279 Vector3 result;
280 result._vec = a * v._vec;
281 return result;
282 }
283
285 inline Vector3 multiply(const Vector3& v, const double a) {
286 return multiply(a, v);
287 }
288
290 inline Vector3 operator * (const double a, const Vector3& v) {
291 return multiply(a, v);
292 }
293
295 inline Vector3 operator * (const Vector3& v, const double a) {
296 return multiply(a, v);
297 }
298
300 inline Vector3 operator / (const Vector3& v, const double a) {
301 return multiply(1.0/a, v);
302 }
303
305 inline Vector3 add(const Vector3& a, const Vector3& b) {
306 Vector3 result;
307 result._vec = a._vec + b._vec;
308 return result;
309 }
310
312 inline Vector3 subtract(const Vector3& a, const Vector3& b) {
313 Vector3 result;
314 result._vec = a._vec - b._vec;
315 return result;
316 }
317
319 inline Vector3 operator + (const Vector3& a, const Vector3& b) {
320 return add(a, b);
321 }
322
324 inline Vector3 operator - (const Vector3& a, const Vector3& b) {
325 return subtract(a, b);
326 }
327
328 // More physicsy coordinates etc.
329
331 inline double angle(const Vector3& a, const Vector3& b) {
332 return a.angle(b);
333 }
334
335
337
338
340 class ThreeMomentum : public ThreeVector {
341 public:
342 ThreeMomentum() { }
343
345 ThreeMomentum(const V3TYPE& other) {
346 this->setPx(other.x());
347 this->setPy(other.y());
348 this->setPz(other.z());
349 }
350
352 : ThreeVector(other) { }
353
354 ThreeMomentum(const double px, const double py, const double pz) {
355 this->setPx(px);
356 this->setPy(py);
357 this->setPz(pz);
358 }
359
360 ~ThreeMomentum() {}
361
362 public:
363
364
367
370 setX(px);
371 return *this;
372 }
373
376 setY(py);
377 return *this;
378 }
379
382 setZ(pz);
383 return *this;
384 }
385
387
388
391
393 double px() const { return x(); }
395 double px2() const { return x2(); }
396
398 double py() const { return y(); }
400 double py2() const { return y2(); }
401
403 double pz() const { return z(); }
405 double pz2() const { return z2(); }
406
407
409 double p() const { return mod(); }
411 double p2() const { return mod2(); }
412
413
416 return polarVec();
417 }
420 return pTvec();
421 }
422
424 double pT2() const {
425 return polarRadius2();
426 }
428 double pt2() const {
429 return polarRadius2();
430 }
431
433 double pT() const {
434 return sqrt(pT2());
435 }
437 double pt() const {
438 return sqrt(pT2());
439 }
440
442
443
445
446
449
452 _vec = multiply(a, *this)._vec;
453 return *this;
454 }
455
458 _vec = multiply(1.0/a, *this)._vec;
459 return *this;
460 }
461
464 _vec = add(*this, v)._vec;
465 return *this;
466 }
467
470 _vec = add(*this, -v)._vec;
471 return *this;
472 }
473
477 result._vec = -_vec;
478 return result;
479 }
480
481 // /// Multiply space (i.e. all!) components by -1.
482 // ThreeMomentum reverse() const {
483 // return -*this;
484 // }
485
487
488 };
489
490
491 inline ThreeMomentum multiply(const double a, const ThreeMomentum& v) {
492 ThreeMomentum result;
493 result._vec = a * v._vec;
494 return result;
495 }
496
497 inline ThreeMomentum multiply(const ThreeMomentum& v, const double a) {
498 return multiply(a, v);
499 }
500
501 inline ThreeMomentum operator*(const double a, const ThreeMomentum& v) {
502 return multiply(a, v);
503 }
504
505 inline ThreeMomentum operator*(const ThreeMomentum& v, const double a) {
506 return multiply(a, v);
507 }
508
509 inline ThreeMomentum operator/(const ThreeMomentum& v, const double a) {
510 return multiply(1.0/a, v);
511 }
512
513 inline ThreeMomentum add(const ThreeMomentum& a, const ThreeMomentum& b) {
514 ThreeMomentum result;
515 result._vec = a._vec + b._vec;
516 return result;
517 }
518
519 inline ThreeMomentum operator+(const ThreeMomentum& a, const ThreeMomentum& b) {
520 return add(a, b);
521 }
522
523 inline ThreeMomentum operator-(const ThreeMomentum& a, const ThreeMomentum& b) {
524 return add(a, -b);
525 }
526
527
530 inline Vector3 operator+(const ThreeMomentum& a, const Vector3& b) {
531 return add(static_cast<const Vector3&>(a), b);
532 }
533 inline Vector3 operator+(const Vector3& a, const ThreeMomentum& b) {
534 return add(a, static_cast<const Vector3&>(b));
535 }
536
537 inline Vector3 operator-(const ThreeMomentum& a, const Vector3& b) {
538 return add(static_cast<const Vector3&>(a), -b);
539 }
540 inline Vector3 operator-(const Vector3& a, const ThreeMomentum& b) {
541 return add(a, -static_cast<const Vector3&>(b));
542 }
543
544
545
547
548
551
553 inline double deltaEta(const Vector3& a, const Vector3& b, bool sign=false) {
554 return deltaEta(a.pseudorapidity(), b.pseudorapidity(), sign);
555 }
556
558 inline double deltaEta(const Vector3& v, double eta2, bool sign=false) {
559 return deltaEta(v.pseudorapidity(), eta2, sign);
560 }
561
563 inline double deltaEta(double eta1, const Vector3& v, bool sign=false) {
564 return deltaEta(eta1, v.pseudorapidity(), sign);
565 }
566
568
569
572
574 inline double deltaPhi(const Vector3& a, const Vector3& b, bool sign=false) {
575 return deltaPhi(a.azimuthalAngle(), b.azimuthalAngle(), sign);
576 }
577
579 inline double deltaPhi(const Vector3& v, double phi2, bool sign=false) {
580 return deltaPhi(v.azimuthalAngle(), phi2, sign);
581 }
582
584 inline double deltaPhi(double phi1, const Vector3& v, bool sign=false) {
585 return deltaPhi(phi1, v.azimuthalAngle(), sign);
586 }
587
589
590
593
595 inline double deltaR2(const Vector3& a, const Vector3& b) {
596 return deltaR2(a.pseudorapidity(), a.azimuthalAngle(),
598 }
599
601 inline double deltaR(const Vector3& a, const Vector3& b) {
602 return sqrt(deltaR2(a,b));
603 }
604
606 inline double deltaR2(const Vector3& v, double eta2, double phi2) {
607 return deltaR2(v.pseudorapidity(), v.azimuthalAngle(), eta2, phi2);
608 }
609
611 inline double deltaR(const Vector3& v, double eta2, double phi2) {
612 return sqrt(deltaR2(v, eta2, phi2));
613 }
614
616 inline double deltaR2(double eta1, double phi1, const Vector3& v) {
617 return deltaR2(eta1, phi1, v.pseudorapidity(), v.azimuthalAngle());
618 }
619
621 inline double deltaR(double eta1, double phi1, const Vector3& v) {
622 return sqrt(deltaR2(eta1, phi1, v));
623 }
624
626
627
630
634 inline double mT(const Vector3& vis, const Vector3& invis) {
635 // return sqrt(2*vis.perp()*invis.perp() * (1 - cos(deltaPhi(vis, invis))) );
636 return mT(vis.perp(), invis.perp(), deltaPhi(vis, invis));
637 }
638
640 inline double pT(const Vector3& a, const Vector3& b) {
641 return (a+b).perp();
642 }
643
645
646
647}
648
649#endif
Specialisation of MatrixN to aid 3 dimensional rotations.
Definition Matrix3.hh:13
Specialized version of the ThreeVector with momentum functionality.
Definition Vector3.hh:340
double pt() const
Calculate the transverse momentum .
Definition Vector3.hh:437
double pz() const
Get z-component of momentum .
Definition Vector3.hh:403
double pz2() const
Get z-squared .
Definition Vector3.hh:405
double pT() const
Calculate the transverse momentum .
Definition Vector3.hh:433
double p() const
Get the modulus of the 3-momentum.
Definition Vector3.hh:409
ThreeMomentum pTvec() const
Calculate the transverse momentum vector .
Definition Vector3.hh:415
double px() const
Get x-component of momentum .
Definition Vector3.hh:393
double py() const
Get y-component of momentum .
Definition Vector3.hh:398
ThreeMomentum & setPy(double py)
Set y-component of momentum .
Definition Vector3.hh:375
ThreeMomentum & operator-=(const ThreeMomentum &v)
Subtract two 3-momenta.
Definition Vector3.hh:469
double pT2() const
Calculate the squared transverse momentum .
Definition Vector3.hh:424
double px2() const
Get x-squared .
Definition Vector3.hh:395
ThreeMomentum & setPz(double pz)
Set z-component of momentum .
Definition Vector3.hh:381
double pt2() const
Calculate the squared transverse momentum .
Definition Vector3.hh:428
ThreeMomentum & operator/=(double a)
Divide by a scalar.
Definition Vector3.hh:457
ThreeMomentum & operator+=(const ThreeMomentum &v)
Add two 3-momenta.
Definition Vector3.hh:463
ThreeMomentum & setPx(double px)
Set x-component of momentum .
Definition Vector3.hh:369
ThreeMomentum operator-() const
Multiply all components by -1.
Definition Vector3.hh:475
ThreeMomentum & operator*=(double a)
Multiply by a scalar.
Definition Vector3.hh:451
ThreeMomentum ptvec() const
Synonym for pTvec.
Definition Vector3.hh:419
double py2() const
Get y-squared .
Definition Vector3.hh:400
double p2() const
Get the modulus-squared of the 3-momentum.
Definition Vector3.hh:411
Three-dimensional specialisation of Vector.
Definition Vector3.hh:40
Vector3 rhoVec() const
Synonym for polarVec.
Definition Vector3.hh:140
double polarRadius() const
Polar radius.
Definition Vector3.hh:158
Vector3 cross(const Vector3 &v) const
Cross-product with another vector.
Definition Vector3.hh:101
double rho() const
Synonym for polarRadius.
Definition Vector3.hh:166
double eta() const
Synonym for pseudorapidity.
Definition Vector3.hh:220
double perp() const
Synonym for polarRadius.
Definition Vector3.hh:162
Vector3 & operator-=(const Vector3 &v)
In-place subtraction operator.
Definition Vector3.hh:251
double perp2() const
Synonym for polarRadius2.
Definition Vector3.hh:149
double pseudorapidity() const
Purely geometric approximation to rapidity.
Definition Vector3.hh:212
Vector3 unit() const
Synonym for unitVec.
Definition Vector3.hh:124
double abseta() const
Convenience shortcut for fabs(eta())
Definition Vector3.hh:225
double tanTheta() const
Tangent of the polar angle.
Definition Vector3.hh:188
double theta() const
Synonym for polarAngle.
Definition Vector3.hh:200
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:183
Vector3 & operator+=(const Vector3 &v)
In-place addition operator.
Definition Vector3.hh:245
friend Vector3 subtract(const Vector3 &, const Vector3 &)
Unbound vector subtraction function.
Definition Vector3.hh:312
Vector3 & operator*=(const double a)
In-place scalar multiplication operator.
Definition Vector3.hh:233
double polarRadius2() const
Square of the polar radius (.
Definition Vector3.hh:145
Vector3 perpVec() const
Synonym for polarVec.
Definition Vector3.hh:136
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:174
friend Vector3 add(const Vector3 &, const Vector3 &)
Unbound vector addition function.
Definition Vector3.hh:305
double rho2() const
Synonym for polarRadius2.
Definition Vector3.hh:153
Vector3 & operator/=(const double a)
In-place scalar division operator.
Definition Vector3.hh:239
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:193
Vector3 operator-() const
In-place negation operator.
Definition Vector3.hh:257
friend Vector3 multiply(const double, const Vector3 &)
Unbound scalar-product function.
Definition Vector3.hh:278
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:130
A minimal base class for -dimensional vectors.
Definition VectorN.hh:23
double mod() const
Calculate the modulus of a vector. .
Definition VectorN.hh:95
double mod2() const
Calculate the modulus-squared of a vector. .
Definition VectorN.hh:84
Vector< N > & set(const size_t index, const double value)
Set indexed value.
Definition VectorN.hh:60
double pT(const Vector3 &a, const Vector3 &b)
Calculate transverse momentum of pair of 3-vectors.
Definition Vector3.hh:640
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:264
double deltaR(double rap1, double phi1, double rap2, double phi2)
Definition MathUtils.hh:697
double deltaPhi(double phi1, double phi2, bool sign=false)
Calculate the difference between two angles in radians.
Definition MathUtils.hh:667
double deltaEta(double eta1, double eta2, bool sign=false)
Definition MathUtils.hh:675
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:690
double mT(double pT1, double pT2, double dphi)
Definition MathUtils.hh:719
std::enable_if_t< std::is_arithmetic_v< NUM >, NUM > sqr(NUM a)
Named number-type squaring operation.
Definition MathUtils.hh:218
Vector3 cross(const Vector3 &a, const Vector3 &b)
Unbound cross-product function.
Definition Vector3.hh:273
double mapAngle(double angle, PhiMapping mapping)
Map an angle into the enum-specified range.
Definition MathUtils.hh:645
double mapAngle0ToPi(double angle)
Map an angle into the range [0, PI].
Definition MathUtils.hh:637
double angle(const Vector2 &a, const Vector2 &b)
Angle (in radians) between two 2-vectors.
Definition Vector2.hh:177