Rivet  3.1.4
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  class Matrix3;
14 
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 
26  class Vector3 : public Vector<3> {
27 
28  friend class Matrix3;
29  friend Vector3 multiply(const double, const Vector3&);
30  friend Vector3 multiply(const Vector3&, const double);
31  friend Vector3 add(const Vector3&, const Vector3&);
32  friend Vector3 subtract(const Vector3&, const Vector3&);
33 
34  public:
35  Vector3() : Vector<3>() { }
36 
37  template<typename V3>
38  Vector3(const V3& other) {
39  this->setX(other.x());
40  this->setY(other.y());
41  this->setZ(other.z());
42  }
43 
44  Vector3(const Vector<3>& other) {
45  this->setX(other.get(0));
46  this->setY(other.get(1));
47  this->setZ(other.get(2));
48  }
49 
50  Vector3(double x, double y, double z) {
51  this->setX(x);
52  this->setY(y);
53  this->setZ(z);
54  }
55 
56  ~Vector3() { }
57 
58 
59  public:
60 
61  static Vector3 mkX() { return Vector3(1,0,0); }
62  static Vector3 mkY() { return Vector3(0,1,0); }
63  static Vector3 mkZ() { return Vector3(0,0,1); }
64 
65 
66  public:
67 
68  double x() const { return get(0); }
69  double y() const { return get(1); }
70  double z() const { return get(2); }
71  Vector3& setX(double x) { set(0, x); return *this; }
72  Vector3& setY(double y) { set(1, y); return *this; }
73  Vector3& setZ(double z) { set(2, z); return *this; }
74 
75 
77  double dot(const Vector3& v) const {
78  return _vec.dot(v._vec);
79  }
80 
82  Vector3 cross(const Vector3& v) const {
83  Vector3 result;
84  result._vec = _vec.cross(v._vec);
85  return result;
86  }
87 
89  double angle(const Vector3& v) const {
90  const double localDotOther = unit().dot(v.unit());
91  if (localDotOther > 1.0) return 0.0;
92  if (localDotOther < -1.0) return M_PI;
93  return acos(localDotOther);
94  }
95 
96 
98  Vector3 unitVec() const {
99  double md = mod();
100  if ( md <= 0.0 ) return Vector3();
101  else return *this * 1.0/md;
102  }
103 
105  Vector3 unit() const {
106  return unitVec();
107  }
108 
109 
111  Vector3 polarVec() const {
112  Vector3 rtn = *this;
113  rtn.setZ(0.);
114  return rtn;
115  }
117  Vector3 perpVec() const {
118  return polarVec();
119  }
121  Vector3 rhoVec() const {
122  return polarVec();
123  }
124 
126  double polarRadius2() const {
127  return x()*x() + y()*y();
128  }
130  double perp2() const {
131  return polarRadius2();
132  }
134  double rho2() const {
135  return polarRadius2();
136  }
137 
139  double polarRadius() const {
140  return sqrt(polarRadius2());
141  }
143  double perp() const {
144  return polarRadius();
145  }
147  double rho() const {
148  return polarRadius();
149  }
150 
155  double azimuthalAngle(const PhiMapping mapping = ZERO_2PI) const {
156  // If this has a null perp-vector, return zero rather than let atan2 set an error state
157  // This isn't necessary if the implementation supports IEEE floating-point arithmetic (IEC 60559)... are we sure?
158  if (x() == 0 && y() == 0) return 0.0; //< Or return nan / throw an exception?
159  // Calculate the arctan and return in the requested range
160  const double value = atan2( y(), x() );
161  return mapAngle(value, mapping);
162  }
164  double phi(const PhiMapping mapping = ZERO_2PI) const {
165  return azimuthalAngle(mapping);
166  }
167 
169  double tanTheta() const {
170  return polarRadius()/z();
171  }
172 
174  double polarAngle() const {
175  // Get number beween [0,PI]
176  const double polarangle = atan2(polarRadius(), z());
177  return mapAngle0ToPi(polarangle);
178  }
179 
181  double theta() const {
182  return polarAngle();
183  }
184 
190  double pseudorapidity() const {
191  const double epsilon = DBL_EPSILON;
192  double m = mod();
193  if ( m == 0.0 ) return 0.0;
194  double pt = max(epsilon*m, perp());
195  double rap = std::log((m + fabs(z()))/pt);
196  return z() > 0.0 ? rap: -rap;
197  }
198 
200  double eta() const {
201  return pseudorapidity();
202  }
203 
205  double abseta() const {
206  return fabs(eta());
207  }
208 
209 
210  public:
211 
213  Vector3& operator *= (const double a) {
214  _vec = multiply(a, *this)._vec;
215  return *this;
216  }
217 
219  Vector3& operator /= (const double a) {
220  _vec = multiply(1.0/a, *this)._vec;
221  return *this;
222  }
223 
226  _vec = add(*this, v)._vec;
227  return *this;
228  }
229 
232  _vec = subtract(*this, v)._vec;
233  return *this;
234  }
235 
238  Vector3 rtn;
239  rtn._vec = -_vec;
240  return rtn;
241  }
242 
243  };
244 
245 
247  inline double dot(const Vector3& a, const Vector3& b) {
248  return a.dot(b);
249  }
250 
252  inline Vector3 cross(const Vector3& a, const Vector3& b) {
253  return a.cross(b);
254  }
255 
257  inline Vector3 multiply(const double a, const Vector3& v) {
258  Vector3 result;
259  result._vec = a * v._vec;
260  return result;
261  }
262 
264  inline Vector3 multiply(const Vector3& v, const double a) {
265  return multiply(a, v);
266  }
267 
269  inline Vector3 operator * (const double a, const Vector3& v) {
270  return multiply(a, v);
271  }
272 
274  inline Vector3 operator * (const Vector3& v, const double a) {
275  return multiply(a, v);
276  }
277 
279  inline Vector3 operator / (const Vector3& v, const double a) {
280  return multiply(1.0/a, v);
281  }
282 
284  inline Vector3 add(const Vector3& a, const Vector3& b) {
285  Vector3 result;
286  result._vec = a._vec + b._vec;
287  return result;
288  }
289 
291  inline Vector3 subtract(const Vector3& a, const Vector3& b) {
292  Vector3 result;
293  result._vec = a._vec - b._vec;
294  return result;
295  }
296 
298  inline Vector3 operator + (const Vector3& a, const Vector3& b) {
299  return add(a, b);
300  }
301 
303  inline Vector3 operator - (const Vector3& a, const Vector3& b) {
304  return subtract(a, b);
305  }
306 
307  // More physicsy coordinates etc.
308 
310  inline double angle(const Vector3& a, const Vector3& b) {
311  return a.angle(b);
312  }
313 
314 
316 
317 
320 
322  inline double deltaEta(const Vector3& a, const Vector3& b, bool sign=false) {
323  return deltaEta(a.pseudorapidity(), b.pseudorapidity(), sign);
324  }
325 
327  inline double deltaEta(const Vector3& v, double eta2, bool sign=false) {
328  return deltaEta(v.pseudorapidity(), eta2, sign);
329  }
330 
332  inline double deltaEta(double eta1, const Vector3& v, bool sign=false) {
333  return deltaEta(eta1, v.pseudorapidity(), sign);
334  }
335 
337 
338 
341 
343  inline double deltaPhi(const Vector3& a, const Vector3& b, bool sign=false) {
344  return deltaPhi(a.azimuthalAngle(), b.azimuthalAngle(), sign);
345  }
346 
348  inline double deltaPhi(const Vector3& v, double phi2, bool sign=false) {
349  return deltaPhi(v.azimuthalAngle(), phi2, sign);
350  }
351 
353  inline double deltaPhi(double phi1, const Vector3& v, bool sign=false) {
354  return deltaPhi(phi1, v.azimuthalAngle(), sign);
355  }
356 
358 
359 
362 
364  inline double deltaR2(const Vector3& a, const Vector3& b) {
365  return deltaR2(a.pseudorapidity(), a.azimuthalAngle(),
366  b.pseudorapidity(), b.azimuthalAngle());
367  }
368 
370  inline double deltaR(const Vector3& a, const Vector3& b) {
371  return sqrt(deltaR2(a,b));
372  }
373 
375  inline double deltaR2(const Vector3& v, double eta2, double phi2) {
376  return deltaR2(v.pseudorapidity(), v.azimuthalAngle(), eta2, phi2);
377  }
378 
380  inline double deltaR(const Vector3& v, double eta2, double phi2) {
381  return sqrt(deltaR2(v, eta2, phi2));
382  }
383 
385  inline double deltaR2(double eta1, double phi1, const Vector3& v) {
386  return deltaR2(eta1, phi1, v.pseudorapidity(), v.azimuthalAngle());
387  }
388 
390  inline double deltaR(double eta1, double phi1, const Vector3& v) {
391  return sqrt(deltaR2(eta1, phi1, v));
392  }
393 
395 
396 
399 
401  inline double mT(const Vector3& vis, const Vector3& invis) {
402  // return sqrt(2*vis.perp()*invis.perp() * (1 - cos(deltaPhi(vis, invis))) );
403  return mT(vis.perp(), invis.perp(), deltaPhi(vis, invis));
404  }
405 
407 
408 
409 }
410 
411 #endif
Definition: MC_Cent_pPb.hh:10
Vector3 rhoVec() const
Synonym for polarVec.
Definition: Vector3.hh:121
double polarRadius() const
Polar radius.
Definition: Vector3.hh:139
double rho() const
Synonym for polarRadius.
Definition: Vector3.hh:147
double perp() const
Synonym for polarRadius.
Definition: Vector3.hh:143
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:213
Specialisation of MatrixN to aid 3 dimensional rotations.
Definition: Matrix3.hh:13
double tanTheta() const
Tangent of the polar angle.
Definition: Vector3.hh:169
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:89
std::enable_if< std::is_arithmetic< N1 >::value &&std::is_arithmetic< N2 >::value, typename std::common_type< N1, N2 >::type >::type max(N1 a, N2 b)
Get the maximum of two numbers.
Definition: MathUtils.hh:111
Vector3 unitVec() const
Unit-normalized version of this vector.
Definition: Vector3.hh:98
Vector3 & operator/=(const double a)
In-place scalar division operator.
Definition: Vector3.hh:219
Vector3 polarVec() const
Polar projection of this vector into the x-y plane.
Definition: Vector3.hh:111
double dot(const Vector3 &v) const
Dot-product with another vector.
Definition: Vector3.hh:77
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:174
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:257
double pseudorapidity() const
Purely geometric approximation to rapidity.
Definition: Vector3.hh:190
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:237
double rho2() const
Synonym for polarRadius2.
Definition: Vector3.hh:134
Vector3 cross(const Vector3 &v) const
Cross-product with another vector.
Definition: Vector3.hh:82
friend Vector3 subtract(const Vector3 &, const Vector3 &)
Unbound vector subtraction function.
Definition: Vector3.hh:291
Vector3 & operator+=(const Vector3 &v)
In-place addition operator.
Definition: Vector3.hh:225
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:117
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:105
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:155
double theta() const
Synonym for polarAngle.
Definition: Vector3.hh:181
double phi(const PhiMapping mapping=ZERO_2PI) const
Synonym for azimuthalAngle.
Definition: Vector3.hh:164
double abseta() const
Convenience shortcut for fabs(eta())
Definition: Vector3.hh:205
Three-dimensional specialisation of Vector.
Definition: Vector3.hh:26
double eta() const
Synonym for pseudorapidity.
Definition: Vector3.hh:200
friend Vector3 add(const Vector3 &, const Vector3 &)
Unbound vector addition function.
Definition: Vector3.hh:284
double polarRadius2() const
Square of the polar radius (.
Definition: Vector3.hh:126
double mod() const
Calculate the modulus of a vector. .
Definition: VectorN.hh:95
double perp2() const
Synonym for polarRadius2.
Definition: Vector3.hh:130
Vector3 & operator-=(const Vector3 &v)
In-place subtraction operator.
Definition: Vector3.hh:231
std::enable_if< std::is_arithmetic< NUM >::value, int >::type sign(NUM val)
Find the sign of a number.
Definition: MathUtils.hh:266