rivet is hosted by Hepforge, IPPP Durham
Rivet 4.0.0
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 const double eta = std::log((mod() + fabs(z())) / perp());
215 return std::copysign(eta, z());
216 }
217
219 double eta() const {
220 return pseudorapidity();
221 }
222
224 double abseta() const {
225 return fabs(eta());
226 }
227
228
229 public:
230
232 Vector3& operator *= (const double a) {
233 _vec = multiply(a, *this)._vec;
234 return *this;
235 }
236
238 Vector3& operator /= (const double a) {
239 _vec = multiply(1.0/a, *this)._vec;
240 return *this;
241 }
242
245 _vec = add(*this, v)._vec;
246 return *this;
247 }
248
251 _vec = subtract(*this, v)._vec;
252 return *this;
253 }
254
257 Vector3 rtn;
258 rtn._vec = -_vec;
259 return rtn;
260 }
261
262 };
263
264
265
267 inline double dot(const Vector3& a, const Vector3& b) {
268 return a.dot(b);
269 }
270
272 inline Vector3 cross(const Vector3& a, const Vector3& b) {
273 return a.cross(b);
274 }
275
277 inline Vector3 multiply(const double a, const Vector3& v) {
278 Vector3 result;
279 result._vec = a * v._vec;
280 return result;
281 }
282
284 inline Vector3 multiply(const Vector3& v, const double a) {
285 return multiply(a, v);
286 }
287
289 inline Vector3 operator * (const double a, const Vector3& v) {
290 return multiply(a, v);
291 }
292
294 inline Vector3 operator * (const Vector3& v, const double a) {
295 return multiply(a, v);
296 }
297
299 inline Vector3 operator / (const Vector3& v, const double a) {
300 return multiply(1.0/a, v);
301 }
302
304 inline Vector3 add(const Vector3& a, const Vector3& b) {
305 Vector3 result;
306 result._vec = a._vec + b._vec;
307 return result;
308 }
309
311 inline Vector3 subtract(const Vector3& a, const Vector3& b) {
312 Vector3 result;
313 result._vec = a._vec - b._vec;
314 return result;
315 }
316
318 inline Vector3 operator + (const Vector3& a, const Vector3& b) {
319 return add(a, b);
320 }
321
323 inline Vector3 operator - (const Vector3& a, const Vector3& b) {
324 return subtract(a, b);
325 }
326
327 // More physicsy coordinates etc.
328
330 inline double angle(const Vector3& a, const Vector3& b) {
331 return a.angle(b);
332 }
333
334
336
337
339 class ThreeMomentum : public ThreeVector {
340 public:
341 ThreeMomentum() { }
342
344 ThreeMomentum(const V3TYPE& other) {
345 this->setPx(other.x());
346 this->setPy(other.y());
347 this->setPz(other.z());
348 }
349
351 : ThreeVector(other) { }
352
353 ThreeMomentum(const double px, const double py, const double pz) {
354 this->setPx(px);
355 this->setPy(py);
356 this->setPz(pz);
357 }
358
359 ~ThreeMomentum() {}
360
361 public:
362
363
366
369 setX(px);
370 return *this;
371 }
372
375 setY(py);
376 return *this;
377 }
378
381 setZ(pz);
382 return *this;
383 }
384
386
387
390
392 double px() const { return x(); }
394 double px2() const { return x2(); }
395
397 double py() const { return y(); }
399 double py2() const { return y2(); }
400
402 double pz() const { return z(); }
404 double pz2() const { return z2(); }
405
406
408 double p() const { return mod(); }
410 double p2() const { return mod2(); }
411
412
415 return polarVec();
416 }
419 return pTvec();
420 }
421
423 double pT2() const {
424 return polarRadius2();
425 }
427 double pt2() const {
428 return polarRadius2();
429 }
430
432 double pT() const {
433 return sqrt(pT2());
434 }
436 double pt() const {
437 return sqrt(pT2());
438 }
439
441
442
444
445
448
451 _vec = multiply(a, *this)._vec;
452 return *this;
453 }
454
457 _vec = multiply(1.0/a, *this)._vec;
458 return *this;
459 }
460
463 _vec = add(*this, v)._vec;
464 return *this;
465 }
466
469 _vec = add(*this, -v)._vec;
470 return *this;
471 }
472
476 result._vec = -_vec;
477 return result;
478 }
479
480 // /// Multiply space (i.e. all!) components by -1.
481 // ThreeMomentum reverse() const {
482 // return -*this;
483 // }
484
486
487 };
488
489
490 inline ThreeMomentum multiply(const double a, const ThreeMomentum& v) {
491 ThreeMomentum result;
492 result._vec = a * v._vec;
493 return result;
494 }
495
496 inline ThreeMomentum multiply(const ThreeMomentum& v, const double a) {
497 return multiply(a, v);
498 }
499
500 inline ThreeMomentum operator*(const double a, const ThreeMomentum& v) {
501 return multiply(a, v);
502 }
503
504 inline ThreeMomentum operator*(const ThreeMomentum& v, const double a) {
505 return multiply(a, v);
506 }
507
508 inline ThreeMomentum operator/(const ThreeMomentum& v, const double a) {
509 return multiply(1.0/a, v);
510 }
511
512 inline ThreeMomentum add(const ThreeMomentum& a, const ThreeMomentum& b) {
513 ThreeMomentum result;
514 result._vec = a._vec + b._vec;
515 return result;
516 }
517
518 inline ThreeMomentum operator+(const ThreeMomentum& a, const ThreeMomentum& b) {
519 return add(a, b);
520 }
521
522 inline ThreeMomentum operator-(const ThreeMomentum& a, const ThreeMomentum& b) {
523 return add(a, -b);
524 }
525
526
529 inline Vector3 operator+(const ThreeMomentum& a, const Vector3& b) {
530 return add(static_cast<const Vector3&>(a), b);
531 }
532 inline Vector3 operator+(const Vector3& a, const ThreeMomentum& b) {
533 return add(a, static_cast<const Vector3&>(b));
534 }
535
536 inline Vector3 operator-(const ThreeMomentum& a, const Vector3& b) {
537 return add(static_cast<const Vector3&>(a), -b);
538 }
539 inline Vector3 operator-(const Vector3& a, const ThreeMomentum& b) {
540 return add(a, -static_cast<const Vector3&>(b));
541 }
542
543
544
546
547
550
552 inline double deltaEta(const Vector3& a, const Vector3& b, bool sign=false) {
553 return deltaEta(a.pseudorapidity(), b.pseudorapidity(), sign);
554 }
555
557 inline double deltaEta(const Vector3& v, double eta2, bool sign=false) {
558 return deltaEta(v.pseudorapidity(), eta2, sign);
559 }
560
562 inline double deltaEta(double eta1, const Vector3& v, bool sign=false) {
563 return deltaEta(eta1, v.pseudorapidity(), sign);
564 }
565
567
568
571
573 inline double deltaPhi(const Vector3& a, const Vector3& b, bool sign=false) {
574 return deltaPhi(a.azimuthalAngle(), b.azimuthalAngle(), sign);
575 }
576
578 inline double deltaPhi(const Vector3& v, double phi2, bool sign=false) {
579 return deltaPhi(v.azimuthalAngle(), phi2, sign);
580 }
581
583 inline double deltaPhi(double phi1, const Vector3& v, bool sign=false) {
584 return deltaPhi(phi1, v.azimuthalAngle(), sign);
585 }
586
588
589
592
594 inline double deltaR2(const Vector3& a, const Vector3& b) {
595 return deltaR2(a.pseudorapidity(), a.azimuthalAngle(),
597 }
598
600 inline double deltaR(const Vector3& a, const Vector3& b) {
601 return sqrt(deltaR2(a,b));
602 }
603
605 inline double deltaR2(const Vector3& v, double eta2, double phi2) {
606 return deltaR2(v.pseudorapidity(), v.azimuthalAngle(), eta2, phi2);
607 }
608
610 inline double deltaR(const Vector3& v, double eta2, double phi2) {
611 return sqrt(deltaR2(v, eta2, phi2));
612 }
613
615 inline double deltaR2(double eta1, double phi1, const Vector3& v) {
616 return deltaR2(eta1, phi1, v.pseudorapidity(), v.azimuthalAngle());
617 }
618
620 inline double deltaR(double eta1, double phi1, const Vector3& v) {
621 return sqrt(deltaR2(eta1, phi1, v));
622 }
623
625
626
629
633 inline double mT(const Vector3& vis, const Vector3& invis) {
634 // return sqrt(2*vis.perp()*invis.perp() * (1 - cos(deltaPhi(vis, invis))) );
635 return mT(vis.perp(), invis.perp(), deltaPhi(vis, invis));
636 }
637
639 inline double pT(const Vector3& a, const Vector3& b) {
640 return (a+b).perp();
641 }
642
644
645
646}
647
648#endif
Specialisation of MatrixN to aid 3 dimensional rotations.
Definition Matrix3.hh:13
Specialized version of the ThreeVector with momentum functionality.
Definition Vector3.hh:339
double pt() const
Calculate the transverse momentum .
Definition Vector3.hh:436
double pz() const
Get z-component of momentum .
Definition Vector3.hh:402
double pz2() const
Get z-squared .
Definition Vector3.hh:404
double pT() const
Calculate the transverse momentum .
Definition Vector3.hh:432
double p() const
Get the modulus of the 3-momentum.
Definition Vector3.hh:408
ThreeMomentum pTvec() const
Calculate the transverse momentum vector .
Definition Vector3.hh:414
double px() const
Get x-component of momentum .
Definition Vector3.hh:392
double py() const
Get y-component of momentum .
Definition Vector3.hh:397
ThreeMomentum & setPy(double py)
Set y-component of momentum .
Definition Vector3.hh:374
ThreeMomentum & operator-=(const ThreeMomentum &v)
Subtract two 3-momenta.
Definition Vector3.hh:468
double pT2() const
Calculate the squared transverse momentum .
Definition Vector3.hh:423
double px2() const
Get x-squared .
Definition Vector3.hh:394
ThreeMomentum & setPz(double pz)
Set z-component of momentum .
Definition Vector3.hh:380
double pt2() const
Calculate the squared transverse momentum .
Definition Vector3.hh:427
ThreeMomentum & operator/=(double a)
Divide by a scalar.
Definition Vector3.hh:456
ThreeMomentum & operator+=(const ThreeMomentum &v)
Add two 3-momenta.
Definition Vector3.hh:462
ThreeMomentum & setPx(double px)
Set x-component of momentum .
Definition Vector3.hh:368
ThreeMomentum operator-() const
Multiply all components by -1.
Definition Vector3.hh:474
ThreeMomentum & operator*=(double a)
Multiply by a scalar.
Definition Vector3.hh:450
ThreeMomentum ptvec() const
Synonym for pTvec.
Definition Vector3.hh:418
double py2() const
Get y-squared .
Definition Vector3.hh:399
double p2() const
Get the modulus-squared of the 3-momentum.
Definition Vector3.hh:410
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:219
double perp() const
Synonym for polarRadius.
Definition Vector3.hh:162
Vector3 & operator-=(const Vector3 &v)
In-place subtraction operator.
Definition Vector3.hh:250
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:224
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:244
friend Vector3 subtract(const Vector3 &, const Vector3 &)
Unbound vector subtraction function.
Definition Vector3.hh:311
Vector3 & operator*=(const double a)
In-place scalar multiplication operator.
Definition Vector3.hh:232
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:304
double rho2() const
Synonym for polarRadius2.
Definition Vector3.hh:153
Vector3 & operator/=(const double a)
In-place scalar division operator.
Definition Vector3.hh:238
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:256
friend Vector3 multiply(const double, const Vector3 &)
Unbound scalar-product function.
Definition Vector3.hh:277
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:639
Definition MC_CENT_PPB_Projections.hh:10
double deltaR(double rap1, double phi1, double rap2, double phi2)
Definition MathUtils.hh:698
double deltaPhi(double phi1, double phi2, bool sign=false)
Calculate the difference between two angles in radians.
Definition MathUtils.hh:668
double deltaEta(double eta1, double eta2, bool sign=false)
Definition MathUtils.hh:676
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:691
double mT(double pT1, double pT2, double dphi)
Definition MathUtils.hh:720
constexpr std::enable_if< std::is_arithmetic< NUM >::value, int >::type sign(NUM val)
Find the sign of a number.
Definition MathUtils.hh:265
Vector3 cross(const Vector3 &a, const Vector3 &b)
Unbound cross-product function.
Definition Vector3.hh:272
double mapAngle(double angle, PhiMapping mapping)
Map an angle into the enum-specified range.
Definition MathUtils.hh:646
double mapAngle0ToPi(double angle)
Map an angle into the range [0, PI].
Definition MathUtils.hh:638
std::enable_if< std::is_arithmetic< NUM >::value, NUM >::type sqr(NUM a)
Named number-type squaring operation.
Definition MathUtils.hh:219
double angle(const Vector2 &a, const Vector2 &b)
Angle (in radians) between two 2-vectors.
Definition Vector2.hh:177