Rivet  3.1.5
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 
8 namespace Rivet {
9 
10 
11  class Vector3;
12  typedef Vector3 ThreeVector;
13  typedef Vector3 V3;
14 
15  class Matrix3;
16 
17  Vector3 multiply(const double, const Vector3&);
18  Vector3 multiply(const Vector3&, const double);
19  Vector3 add(const Vector3&, const Vector3&);
20  Vector3 operator*(const double, const Vector3&);
21  Vector3 operator*(const Vector3&, const double);
22  Vector3 operator/(const Vector3&, const double);
23  Vector3 operator+(const Vector3&, const Vector3&);
24  Vector3 operator-(const Vector3&, const Vector3&);
25 
26 
28  class Vector3 : public Vector<3> {
29 
30  friend class Matrix3;
31  friend Vector3 multiply(const double, const Vector3&);
32  friend Vector3 multiply(const Vector3&, const double);
33  friend Vector3 add(const Vector3&, const Vector3&);
34  friend Vector3 subtract(const Vector3&, const Vector3&);
35 
36  public:
37  Vector3() : Vector<3>() { }
38 
39  template<typename V3TYPE>
40  Vector3(const V3TYPE& other) {
41  this->setX(other.x());
42  this->setY(other.y());
43  this->setZ(other.z());
44  }
45 
46  Vector3(const Vector<3>& other) {
47  this->setX(other.get(0));
48  this->setY(other.get(1));
49  this->setZ(other.get(2));
50  }
51 
52  Vector3(double x, double y, double z) {
53  this->setX(x);
54  this->setY(y);
55  this->setZ(z);
56  }
57 
58  ~Vector3() { }
59 
60 
61  public:
62 
63  static Vector3 mkX() { return Vector3(1,0,0); }
64  static Vector3 mkY() { return Vector3(0,1,0); }
65  static Vector3 mkZ() { return Vector3(0,0,1); }
66 
67 
68  public:
69 
70  double x() const { return get(0); }
71  double y() const { return get(1); }
72  double z() const { return get(2); }
73  Vector3& setX(double x) { set(0, x); return *this; }
74  Vector3& setY(double y) { set(1, y); return *this; }
75  Vector3& setZ(double z) { set(2, z); return *this; }
76 
77 
79  double dot(const Vector3& v) const {
80  return _vec.dot(v._vec);
81  }
82 
84  Vector3 cross(const Vector3& v) const {
85  Vector3 result;
86  result._vec = _vec.cross(v._vec);
87  return result;
88  }
89 
91  double angle(const Vector3& v) const {
92  const double localDotOther = unit().dot(v.unit());
93  if (localDotOther > 1.0) return 0.0;
94  if (localDotOther < -1.0) return M_PI;
95  return acos(localDotOther);
96  }
97 
98 
100  Vector3 unitVec() const {
101  double md = mod();
102  if ( md <= 0.0 ) return Vector3();
103  else return *this * 1.0/md;
104  }
105 
107  Vector3 unit() const {
108  return unitVec();
109  }
110 
111 
113  Vector3 polarVec() const {
114  Vector3 rtn = *this;
115  rtn.setZ(0.);
116  return rtn;
117  }
119  Vector3 perpVec() const {
120  return polarVec();
121  }
123  Vector3 rhoVec() const {
124  return polarVec();
125  }
126 
128  double polarRadius2() const {
129  return x()*x() + y()*y();
130  }
132  double perp2() const {
133  return polarRadius2();
134  }
136  double rho2() const {
137  return polarRadius2();
138  }
139 
141  double polarRadius() const {
142  return sqrt(polarRadius2());
143  }
145  double perp() const {
146  return polarRadius();
147  }
149  double rho() const {
150  return polarRadius();
151  }
152 
157  double azimuthalAngle(const PhiMapping mapping = ZERO_2PI) const {
158  // If this has a null perp-vector, return zero rather than let atan2 set an error state
159  // This isn't necessary if the implementation supports IEEE floating-point arithmetic (IEC 60559)... are we sure?
160  if (x() == 0 && y() == 0) return 0.0; //< Or return nan / throw an exception?
161  // Calculate the arctan and return in the requested range
162  const double value = atan2( y(), x() );
163  return mapAngle(value, mapping);
164  }
166  double phi(const PhiMapping mapping = ZERO_2PI) const {
167  return azimuthalAngle(mapping);
168  }
169 
171  double tanTheta() const {
172  return polarRadius()/z();
173  }
174 
176  double polarAngle() const {
177  // Get number beween [0,PI]
178  const double polarangle = atan2(polarRadius(), z());
179  return mapAngle0ToPi(polarangle);
180  }
181 
183  double theta() const {
184  return polarAngle();
185  }
186 
195  double pseudorapidity() const {
196  if (mod() == 0.0) return 0.0;
197  const double eta = std::log((mod() + fabs(z())) / perp());
198  return std::copysign(eta, z());
199  }
200 
202  double eta() const {
203  return pseudorapidity();
204  }
205 
207  double abseta() const {
208  return fabs(eta());
209  }
210 
211 
212  public:
213 
215  Vector3& operator *= (const double a) {
216  _vec = multiply(a, *this)._vec;
217  return *this;
218  }
219 
221  Vector3& operator /= (const double a) {
222  _vec = multiply(1.0/a, *this)._vec;
223  return *this;
224  }
225 
228  _vec = add(*this, v)._vec;
229  return *this;
230  }
231 
234  _vec = subtract(*this, v)._vec;
235  return *this;
236  }
237 
240  Vector3 rtn;
241  rtn._vec = -_vec;
242  return rtn;
243  }
244 
245  };
246 
247 
248 
250  inline double dot(const Vector3& a, const Vector3& b) {
251  return a.dot(b);
252  }
253 
255  inline Vector3 cross(const Vector3& a, const Vector3& b) {
256  return a.cross(b);
257  }
258 
260  inline Vector3 multiply(const double a, const Vector3& v) {
261  Vector3 result;
262  result._vec = a * v._vec;
263  return result;
264  }
265 
267  inline Vector3 multiply(const Vector3& v, const double a) {
268  return multiply(a, v);
269  }
270 
272  inline Vector3 operator * (const double a, const Vector3& v) {
273  return multiply(a, v);
274  }
275 
277  inline Vector3 operator * (const Vector3& v, const double a) {
278  return multiply(a, v);
279  }
280 
282  inline Vector3 operator / (const Vector3& v, const double a) {
283  return multiply(1.0/a, v);
284  }
285 
287  inline Vector3 add(const Vector3& a, const Vector3& b) {
288  Vector3 result;
289  result._vec = a._vec + b._vec;
290  return result;
291  }
292 
294  inline Vector3 subtract(const Vector3& a, const Vector3& b) {
295  Vector3 result;
296  result._vec = a._vec - b._vec;
297  return result;
298  }
299 
301  inline Vector3 operator + (const Vector3& a, const Vector3& b) {
302  return add(a, b);
303  }
304 
306  inline Vector3 operator - (const Vector3& a, const Vector3& b) {
307  return subtract(a, b);
308  }
309 
310  // More physicsy coordinates etc.
311 
313  inline double angle(const Vector3& a, const Vector3& b) {
314  return a.angle(b);
315  }
316 
317 
319 
320 
323 
325  inline double deltaEta(const Vector3& a, const Vector3& b, bool sign=false) {
326  return deltaEta(a.pseudorapidity(), b.pseudorapidity(), sign);
327  }
328 
330  inline double deltaEta(const Vector3& v, double eta2, bool sign=false) {
331  return deltaEta(v.pseudorapidity(), eta2, sign);
332  }
333 
335  inline double deltaEta(double eta1, const Vector3& v, bool sign=false) {
336  return deltaEta(eta1, v.pseudorapidity(), sign);
337  }
338 
340 
341 
344 
346  inline double deltaPhi(const Vector3& a, const Vector3& b, bool sign=false) {
347  return deltaPhi(a.azimuthalAngle(), b.azimuthalAngle(), sign);
348  }
349 
351  inline double deltaPhi(const Vector3& v, double phi2, bool sign=false) {
352  return deltaPhi(v.azimuthalAngle(), phi2, sign);
353  }
354 
356  inline double deltaPhi(double phi1, const Vector3& v, bool sign=false) {
357  return deltaPhi(phi1, v.azimuthalAngle(), sign);
358  }
359 
361 
362 
365 
367  inline double deltaR2(const Vector3& a, const Vector3& b) {
368  return deltaR2(a.pseudorapidity(), a.azimuthalAngle(),
369  b.pseudorapidity(), b.azimuthalAngle());
370  }
371 
373  inline double deltaR(const Vector3& a, const Vector3& b) {
374  return sqrt(deltaR2(a,b));
375  }
376 
378  inline double deltaR2(const Vector3& v, double eta2, double phi2) {
379  return deltaR2(v.pseudorapidity(), v.azimuthalAngle(), eta2, phi2);
380  }
381 
383  inline double deltaR(const Vector3& v, double eta2, double phi2) {
384  return sqrt(deltaR2(v, eta2, phi2));
385  }
386 
388  inline double deltaR2(double eta1, double phi1, const Vector3& v) {
389  return deltaR2(eta1, phi1, v.pseudorapidity(), v.azimuthalAngle());
390  }
391 
393  inline double deltaR(double eta1, double phi1, const Vector3& v) {
394  return sqrt(deltaR2(eta1, phi1, v));
395  }
396 
398 
399 
402 
404  inline double mT(const Vector3& vis, const Vector3& invis) {
405  // return sqrt(2*vis.perp()*invis.perp() * (1 - cos(deltaPhi(vis, invis))) );
406  return mT(vis.perp(), invis.perp(), deltaPhi(vis, invis));
407  }
408 
410 
411 
412 }
413 
414 #endif
Definition: MC_Cent_pPb.hh:10
Vector3 rhoVec() const
Synonym for polarVec.
Definition: Vector3.hh:123
double polarRadius() const
Polar radius.
Definition: Vector3.hh:141
double rho() const
Synonym for polarRadius.
Definition: Vector3.hh:149
double perp() const
Synonym for polarRadius.
Definition: Vector3.hh:145
double mapAngle(double angle, PhiMapping mapping)
Map an angle into the enum-specified range.
Definition: MathUtils.hh:607
Vector3 & operator*=(const double a)
In-place scalar multiplication operator.
Definition: Vector3.hh:215
Specialisation of MatrixN to aid 3 dimensional rotations.
Definition: Matrix3.hh:13
double tanTheta() const
Tangent of the polar angle.
Definition: Vector3.hh:171
double deltaEta(double eta1, double eta2, bool sign=false)
Definition: MathUtils.hh:637
double angle(const Vector3 &v) const
Angle in radians to another vector.
Definition: Vector3.hh:91
Vector3 unitVec() const
Unit-normalized version of this vector.
Definition: Vector3.hh:100
Vector3 & operator/=(const double a)
In-place scalar division operator.
Definition: Vector3.hh:221
Vector3 polarVec() const
Polar projection of this vector into the x-y plane.
Definition: Vector3.hh:113
double dot(const Vector3 &v) const
Dot-product with another vector.
Definition: Vector3.hh:79
string operator/(const string &a, const string &b)
Operator for joining strings a and b with filesystem separators.
Definition: Utils.hh:260
double polarAngle() const
Angle subtended by the vector and the z-axis.
Definition: Vector3.hh:176
PhiMapping
Enum for range of to be mapped into.
Definition: MathConstants.hh:49
friend Vector3 multiply(const double, const Vector3 &)
Unbound scalar-product function.
Definition: Vector3.hh:260
double pseudorapidity() const
Purely geometric approximation to rapidity.
Definition: Vector3.hh:195
double deltaPhi(double phi1, double phi2, bool sign=false)
Calculate the difference between two angles in radians.
Definition: MathUtils.hh:629
Vector3 operator-() const
In-place negation operator.
Definition: Vector3.hh:239
double rho2() const
Synonym for polarRadius2.
Definition: Vector3.hh:136
Vector3 cross(const Vector3 &v) const
Cross-product with another vector.
Definition: Vector3.hh:84
friend Vector3 subtract(const Vector3 &, const Vector3 &)
Unbound vector subtraction function.
Definition: Vector3.hh:294
Vector3 & operator+=(const Vector3 &v)
In-place addition operator.
Definition: Vector3.hh:227
double mT(double pT1, double pT2, double dphi)
Definition: MathUtils.hh:681
A minimal base class for -dimensional vectors.
Definition: VectorN.hh:13
double deltaR2(double rap1, double phi1, double rap2, double phi2)
Definition: MathUtils.hh:652
double deltaR(double rap1, double phi1, double rap2, double phi2)
Definition: MathUtils.hh:659
Vector3 perpVec() const
Synonym for polarVec.
Definition: Vector3.hh:119
double mapAngle0ToPi(double angle)
Map an angle into the range [0, PI].
Definition: MathUtils.hh:599
Vector3 unit() const
Synonym for unitVec.
Definition: Vector3.hh:107
double azimuthalAngle(const PhiMapping mapping=ZERO_2PI) const
Angle subtended by the vector&#39;s projection in x-y and the x-axis.
Definition: Vector3.hh:157
double theta() const
Synonym for polarAngle.
Definition: Vector3.hh:183
double phi(const PhiMapping mapping=ZERO_2PI) const
Synonym for azimuthalAngle.
Definition: Vector3.hh:166
double abseta() const
Convenience shortcut for fabs(eta())
Definition: Vector3.hh:207
Three-dimensional specialisation of Vector.
Definition: Vector3.hh:28
double eta() const
Synonym for pseudorapidity.
Definition: Vector3.hh:202
friend Vector3 add(const Vector3 &, const Vector3 &)
Unbound vector addition function.
Definition: Vector3.hh:287
double polarRadius2() const
Square of the polar radius (.
Definition: Vector3.hh:128
double mod() const
Calculate the modulus of a vector. .
Definition: VectorN.hh:95
double perp2() const
Synonym for polarRadius2.
Definition: Vector3.hh:132
Vector3 & operator-=(const Vector3 &v)
In-place subtraction operator.
Definition: Vector3.hh:233
std::enable_if< std::is_arithmetic< NUM >::value, int >::type sign(NUM val)
Find the sign of a number.
Definition: MathUtils.hh:266