Rivet  1.8.3
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  public:
59  static Vector3 mkX() { return Vector3(1,0,0); }
60  static Vector3 mkY() { return Vector3(0,1,0); }
61  static Vector3 mkZ() { return Vector3(0,0,1); }
62 
63  public:
64  double x() const { return get(0); }
65  double y() const { return get(1); }
66  double z() const { return get(2); }
67  Vector3& setX(double x) { set(0, x); return *this; }
68  Vector3& setY(double y) { set(1, y); return *this; }
69  Vector3& setZ(double z) { set(2, z); return *this; }
70 
71  double dot(const Vector3& v) const {
72  return _vec.dot(v._vec);
73  }
74 
75  Vector3 cross(const Vector3& v) const {
76  Vector3 result;
77  result._vec = _vec.cross(v._vec);
78  return result;
79  }
80 
81  double angle(const Vector3& v) const {
82  const double localDotOther = unit().dot(v.unit());
83  if (fuzzyEquals(localDotOther, 1.0)) return 0.0;
84  else if (fuzzyEquals(localDotOther, -1.0)) return M_PI;
85  return acos(localDotOther);
86  }
87 
88  Vector3 unit() const {
90  if (isZero()) return *this;
91  else return *this * 1.0/this->mod();
92  }
93 
94  double polarRadius2() const {
95  return x()*x() + y()*y();
96  }
97 
99  double perp2() const {
100  return polarRadius2();
101  }
102 
104  double rho2() const {
105  return polarRadius2();
106  }
107 
108  double polarRadius() const {
109  return sqrt(polarRadius2());
110  }
111 
113  double perp() const {
114  return polarRadius();
115  }
116 
118  double rho() const {
119  return polarRadius();
120  }
121 
123  double azimuthalAngle(const PhiMapping mapping = ZERO_2PI) const {
124  // If this is a null vector, return zero rather than let atan2 set an error state
125  if (Rivet::isZero(mod2())) return 0.0;
126 
127  // Calculate the arctan and return in the requested range
128  const double value = atan2( y(), x() );
129  return mapAngle(value, mapping);
130  }
131 
133  double phi(const PhiMapping mapping = ZERO_2PI) const {
134  return azimuthalAngle(mapping);
135  }
136 
138  double polarAngle() const {
139  // Get number beween [0,PI]
140  const double polarangle = atan2(polarRadius(), z());
141  return mapAngle0ToPi(polarangle);
142  }
143 
145  double theta() const {
146  return polarAngle();
147  }
148 
151  double pseudorapidity() const {
152  return -std::log(tan( 0.5 * polarAngle() ));
153  }
154 
156  double eta() const {
157  return pseudorapidity();
158  }
159 
160  public:
161  Vector3& operator*=(const double a) {
162  _vec = multiply(a, *this)._vec;
163  return *this;
164  }
165 
166  Vector3& operator/=(const double a) {
167  _vec = multiply(1.0/a, *this)._vec;
168  return *this;
169  }
170 
171  Vector3& operator+=(const Vector3& v) {
172  _vec = add(*this, v)._vec;
173  return *this;
174  }
175 
176  Vector3& operator-=(const Vector3& v) {
177  _vec = subtract(*this, v)._vec;
178  return *this;
179  }
180 
181  Vector3 operator-() const {
182  Vector3 rtn;
183  rtn._vec = -_vec;
184  return rtn;
185  }
186 
187  };
188 
189 
190 
191  inline double dot(const Vector3& a, const Vector3& b) {
192  return a.dot(b);
193  }
194 
195  inline Vector3 cross(const Vector3& a, const Vector3& b) {
196  return a.cross(b);
197  }
198 
199  inline Vector3 multiply(const double a, const Vector3& v) {
200  Vector3 result;
201  result._vec = a * v._vec;
202  return result;
203  }
204 
205  inline Vector3 multiply(const Vector3& v, const double a) {
206  return multiply(a, v);
207  }
208 
209  inline Vector3 operator*(const double a, const Vector3& v) {
210  return multiply(a, v);
211  }
212 
213  inline Vector3 operator*(const Vector3& v, const double a) {
214  return multiply(a, v);
215  }
216 
217  inline Vector3 operator/(const Vector3& v, const double a) {
218  return multiply(1.0/a, v);
219  }
220 
221  inline Vector3 add(const Vector3& a, const Vector3& b) {
222  Vector3 result;
223  result._vec = a._vec + b._vec;
224  return result;
225  }
226 
227  inline Vector3 subtract(const Vector3& a, const Vector3& b) {
228  Vector3 result;
229  result._vec = a._vec - b._vec;
230  return result;
231  }
232 
233  inline Vector3 operator+(const Vector3& a, const Vector3& b) {
234  return add(a, b);
235  }
236 
237  inline Vector3 operator-(const Vector3& a, const Vector3& b) {
238  return subtract(a, b);
239  }
240 
241  // More physicsy coordinates etc.
242 
244  inline double angle(const Vector3& a, const Vector3& b) {
245  return a.angle(b);
246  }
247 
249  inline double polarRadius2(const Vector3& v) {
250  return v.polarRadius2();
251  }
253  inline double perp2(const Vector3& v) {
254  return v.perp2();
255  }
257  inline double rho2(const Vector3& v) {
258  return v.rho2();
259  }
260 
262  inline double polarRadius(const Vector3& v) {
263  return v.polarRadius();
264  }
266  inline double perp(const Vector3& v) {
267  return v.perp();
268  }
270  inline double rho(const Vector3& v) {
271  return v.rho();
272  }
273 
274 
277  inline double azimuthalAngle(const Vector3& v, const PhiMapping mapping = ZERO_2PI) {
278  return v.azimuthalAngle(mapping);
279  }
281  inline double phi(const Vector3& v, const PhiMapping mapping = ZERO_2PI) {
282  return v.phi(mapping);
283  }
284 
286  inline double polarAngle(const Vector3& v) {
287  return v.polarAngle();
288  }
290  inline double theta(const Vector3& v) {
291  return v.theta();
292  }
293 
295  inline double pseudorapidity(const Vector3& v) {
296  return v.pseudorapidity();
297  }
299  inline double eta(const Vector3& v) {
300  return v.eta();
301  }
302 
303 
305 
307 
308 
310  inline double deltaEta(const Vector3& a, const Vector3& b) {
311  return deltaEta(a.pseudorapidity(), b.pseudorapidity());
312  }
313 
315  inline double deltaEta(const Vector3& v, double eta2) {
316  return deltaEta(v.pseudorapidity(), eta2);
317  }
318 
320  inline double deltaEta(double eta1, const Vector3& v) {
321  return deltaEta(eta1, v.pseudorapidity());
322  }
323 
325 
326 
328 
329 
331  inline double deltaPhi(const Vector3& a, const Vector3& b) {
332  return deltaPhi(a.azimuthalAngle(), b.azimuthalAngle());
333  }
334 
336  inline double deltaPhi(const Vector3& v, double phi2) {
337  return deltaPhi(v.azimuthalAngle(), phi2);
338  }
339 
341  inline double deltaPhi(double phi1, const Vector3& v) {
342  return deltaPhi(phi1, v.azimuthalAngle());
343  }
344 
346 
347 
349 
350 
352  inline double deltaR(const Vector3& a, const Vector3& b) {
353  return deltaR(a.pseudorapidity(), a.azimuthalAngle(),
354  b.pseudorapidity(), b.azimuthalAngle());
355  }
356 
358  inline double deltaR(const Vector3& v, double eta2, double phi2) {
359  return deltaR(v.pseudorapidity(), v.azimuthalAngle(), eta2, phi2);
360  }
361 
363  inline double deltaR(double eta1, double phi1, const Vector3& v) {
364  return deltaR(eta1, phi1, v.pseudorapidity(), v.azimuthalAngle());
365  }
366 
368 
369 }
370 
371 #endif
Definition: MC_JetAnalysis.hh:9
bool isZero(double tolerance=1E-5) const
Check for nullness, allowing for numerical precision.
Definition: VectorN.hh:67
double mod2() const
Calculate the modulus-squared of a vector. .
Definition: VectorN.hh:76
double rho() const
Synonym for polarRadius.
Definition: Vector3.hh:118
double perp() const
Synonym for polarRadius.
Definition: Vector3.hh:113
double mapAngle(double angle, PhiMapping mapping)
Map an angle into the enum-specified range.
Definition: MathUtils.hh:441
Specialisation of MatrixN to aid 3 dimensional rotations.
Definition: Matrix3.hh:13
double polarAngle() const
Angle subtended by the vector and the z-axis.
Definition: Vector3.hh:138
PhiMapping
Enum for range of to be mapped into.
Definition: MathHeader.hh:63
double pseudorapidity() const
Definition: Vector3.hh:151
double rho2() const
Synonym for polarRadius2.
Definition: Vector3.hh:104
bool isZero(double val, double tolerance=1E-8)
Definition: MathUtils.hh:17
A minimal base class for -dimensional vectors.
Definition: VectorN.hh:13
double mapAngle0ToPi(double angle)
Map an angle into the range [0, PI].
Definition: MathUtils.hh:433
Vector3 unit() const
Definition: Vector3.hh:88
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:123
double theta() const
Synonym for polarAngle.
Definition: Vector3.hh:145
double phi(const PhiMapping mapping=ZERO_2PI) const
Synonym for azimuthalAngle.
Definition: Vector3.hh:133
Three-dimensional specialisation of Vector.
Definition: Vector3.hh:26
double eta() const
Synonym for pseudorapidity.
Definition: Vector3.hh:156
bool fuzzyEquals(double a, double b, double tolerance=1E-5)
Compare two floating point numbers for equality with a degree of fuzziness.
Definition: MathUtils.hh:34
double mod() const
Calculate the modulus of a vector. .
Definition: VectorN.hh:87
double perp2() const
Synonym for polarRadius2.
Definition: Vector3.hh:99