rivet is hosted by Hepforge, IPPP Durham
Rivet  2.7.0
Vector3.hh
1 #ifndef RIVET_MATH_VECTOR3
2 #define RIVET_MATH_VECTOR3
3 
4 #include "Rivet/Math/MathHeader.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 {
100  if (isZero()) return *this;
101  else return *this * 1.0/this->mod();
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 
152  double azimuthalAngle(const PhiMapping mapping = ZERO_2PI) const {
153  // If this is a null vector, return zero rather than let atan2 set an error state
154  if (Rivet::isZero(mod2())) return 0.0;
155 
156  // Calculate the arctan and return in the requested range
157  const double value = atan2( y(), x() );
158  return mapAngle(value, mapping);
159  }
161  double phi(const PhiMapping mapping = ZERO_2PI) const {
162  return azimuthalAngle(mapping);
163  }
164 
166  double polarAngle() const {
167  // Get number beween [0,PI]
168  const double polarangle = atan2(polarRadius(), z());
169  return mapAngle0ToPi(polarangle);
170  }
171 
173  double theta() const {
174  return polarAngle();
175  }
176 
182  double pseudorapidity() const {
183  const double epsilon = DBL_EPSILON;
184  double m = mod();
185  if ( m == 0.0 ) return 0.0;
186  double pt = max(epsilon*m, perp());
187  double rap = std::log((m + fabs(z()))/pt);
188  return z() > 0.0 ? rap: -rap;
189  }
190 
192  double eta() const {
193  return pseudorapidity();
194  }
195 
197  double abseta() const {
198  return fabs(eta());
199  }
200 
201 
202  public:
203  Vector3& operator*=(const double a) {
204  _vec = multiply(a, *this)._vec;
205  return *this;
206  }
207 
208  Vector3& operator/=(const double a) {
209  _vec = multiply(1.0/a, *this)._vec;
210  return *this;
211  }
212 
213  Vector3& operator+=(const Vector3& v) {
214  _vec = add(*this, v)._vec;
215  return *this;
216  }
217 
218  Vector3& operator-=(const Vector3& v) {
219  _vec = subtract(*this, v)._vec;
220  return *this;
221  }
222 
223  Vector3 operator-() const {
224  Vector3 rtn;
225  rtn._vec = -_vec;
226  return rtn;
227  }
228 
229  };
230 
231 
232 
233  inline double dot(const Vector3& a, const Vector3& b) {
234  return a.dot(b);
235  }
236 
237  inline Vector3 cross(const Vector3& a, const Vector3& b) {
238  return a.cross(b);
239  }
240 
241  inline Vector3 multiply(const double a, const Vector3& v) {
242  Vector3 result;
243  result._vec = a * v._vec;
244  return result;
245  }
246 
247  inline Vector3 multiply(const Vector3& v, const double a) {
248  return multiply(a, v);
249  }
250 
251  inline Vector3 operator*(const double a, const Vector3& v) {
252  return multiply(a, v);
253  }
254 
255  inline Vector3 operator*(const Vector3& v, const double a) {
256  return multiply(a, v);
257  }
258 
259  inline Vector3 operator/(const Vector3& v, const double a) {
260  return multiply(1.0/a, v);
261  }
262 
263  inline Vector3 add(const Vector3& a, const Vector3& b) {
264  Vector3 result;
265  result._vec = a._vec + b._vec;
266  return result;
267  }
268 
269  inline Vector3 subtract(const Vector3& a, const Vector3& b) {
270  Vector3 result;
271  result._vec = a._vec - b._vec;
272  return result;
273  }
274 
275  inline Vector3 operator+(const Vector3& a, const Vector3& b) {
276  return add(a, b);
277  }
278 
279  inline Vector3 operator-(const Vector3& a, const Vector3& b) {
280  return subtract(a, b);
281  }
282 
283  // More physicsy coordinates etc.
284 
286  inline double angle(const Vector3& a, const Vector3& b) {
287  return a.angle(b);
288  }
289 
291 
293 
294 
296  inline double deltaEta(const Vector3& a, const Vector3& b) {
297  return deltaEta(a.pseudorapidity(), b.pseudorapidity());
298  }
299 
301  inline double deltaEta(const Vector3& v, double eta2) {
302  return deltaEta(v.pseudorapidity(), eta2);
303  }
304 
306  inline double deltaEta(double eta1, const Vector3& v) {
307  return deltaEta(eta1, v.pseudorapidity());
308  }
309 
311 
312 
314 
315 
317  inline double deltaPhi(const Vector3& a, const Vector3& b, bool sign=false) {
318  return deltaPhi(a.azimuthalAngle(), b.azimuthalAngle(), sign);
319  }
320 
322  inline double deltaPhi(const Vector3& v, double phi2, bool sign=false) {
323  return deltaPhi(v.azimuthalAngle(), phi2, sign);
324  }
325 
327  inline double deltaPhi(double phi1, const Vector3& v, bool sign=false) {
328  return deltaPhi(phi1, v.azimuthalAngle(), sign);
329  }
330 
332 
333 
335 
336 
338  inline double deltaR2(const Vector3& a, const Vector3& b) {
339  return deltaR2(a.pseudorapidity(), a.azimuthalAngle(),
340  b.pseudorapidity(), b.azimuthalAngle());
341  }
342 
344  inline double deltaR(const Vector3& a, const Vector3& b) {
345  return sqrt(deltaR2(a,b));
346  }
347 
349  inline double deltaR2(const Vector3& v, double eta2, double phi2) {
350  return deltaR2(v.pseudorapidity(), v.azimuthalAngle(), eta2, phi2);
351  }
352 
354  inline double deltaR(const Vector3& v, double eta2, double phi2) {
355  return sqrt(deltaR2(v, eta2, phi2));
356  }
357 
359  inline double deltaR2(double eta1, double phi1, const Vector3& v) {
360  return deltaR2(eta1, phi1, v.pseudorapidity(), v.azimuthalAngle());
361  }
362 
364  inline double deltaR(double eta1, double phi1, const Vector3& v) {
365  return sqrt(deltaR2(eta1, phi1, v));
366  }
367 
369 
370 
373 
374  //typedef Vector3 V3; //< generic
375  typedef Vector3 X3; //< spatial
377 
378 
379 }
380 
381 #endif
Definition: ALICE_2010_I880049.cc:13
Vector3 rhoVec() const
Synonym for polarVec.
Definition: Vector3.hh:121
bool isZero(double tolerance=1E-5) const
Check for nullness, allowing for numerical precision.
Definition: VectorN.hh:75
double mod2() const
Calculate the modulus-squared of a vector. .
Definition: VectorN.hh:84
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:547
Specialisation of MatrixN to aid 3 dimensional rotations.
Definition: Matrix3.hh:13
double angle(const Vector3 &v) const
Angle in radians to another vector.
Definition: Vector3.hh:89
Vector3 unitVec() const
Unit-normalized version of this vector.
Definition: Vector3.hh:98
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
double polarAngle() const
Angle subtended by the vector and the z-axis.
Definition: Vector3.hh:166
PhiMapping
Enum for range of to be mapped into.
Definition: MathHeader.hh:31
double pseudorapidity() const
Definition: Vector3.hh:182
double deltaPhi(double phi1, double phi2, bool sign=false)
Calculate the difference between two angles in radians.
Definition: MathUtils.hh:569
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
double deltaEta(double eta1, double eta2)
Definition: MathUtils.hh:577
A minimal base class for -dimensional vectors.
Definition: VectorN.hh:13
double deltaR2(double rap1, double phi1, double rap2, double phi2)
Definition: MathUtils.hh:590
double deltaR(double rap1, double phi1, double rap2, double phi2)
Definition: MathUtils.hh:597
double max(const vector< double > &in, double errval=DBL_NAN)
Find the maximum value in the vector.
Definition: Utils.hh:465
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:539
Vector3 unit() const
Synonym for unitVec.
Definition: Vector3.hh:105
std::enable_if< std::is_floating_point< NUM >::value, bool >::type isZero(NUM val, double tolerance=1e-8)
Compare a number to zero.
Definition: MathUtils.hh:21
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:152
double theta() const
Synonym for polarAngle.
Definition: Vector3.hh:173
double phi(const PhiMapping mapping=ZERO_2PI) const
Synonym for azimuthalAngle.
Definition: Vector3.hh:161
double abseta() const
Convenience shortcut for fabs(eta())
Definition: Vector3.hh:197
Three-dimensional specialisation of Vector.
Definition: Vector3.hh:26
double eta() const
Synonym for pseudorapidity.
Definition: Vector3.hh:192
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
std::enable_if< std::is_arithmetic< NUM >::value, int >::type sign(NUM val)
Find the sign of a number.
Definition: MathUtils.hh:236