ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ThreeVector.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ThreeVector.h
1 // -*- C++ -*-
2 // CLASSDOC OFF
3 // ---------------------------------------------------------------------------
4 // CLASSDOC ON
5 //
6 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
7 //
8 // Hep3Vector is a general 3-vector class defining vectors in three
9 // dimension using double components. Rotations of these vectors are
10 // performed by multiplying with an object of the HepRotation class.
11 //
12 // .SS See Also
13 // LorentzVector.h, Rotation.h, LorentzRotation.h
14 //
15 // .SS Authors
16 // Leif Lonnblad and Anders Nilsson; Modified by Evgueni Tcherniaev;
17 // ZOOM additions by Mark Fischler
18 //
19 
20 #ifndef HEP_THREEVECTOR_H
21 #define HEP_THREEVECTOR_H
22 
23 #ifdef GNUPRAGMA
24 #pragma interface
25 #endif
26 
27 #include <iostream>
28 #include "CLHEP/Utility/defs.h"
29 
30 namespace CLHEP {
31 
32 class HepRotation;
33 class HepEulerAngles;
34 class HepAxisAngle;
35 
40 class Hep3Vector {
41 
42 public:
43 
44 // Basic properties and operations on 3-vectors:
45 
46  enum { X=0, Y=1, Z=2, NUM_COORDINATES=3, SIZE=NUM_COORDINATES };
47  // Safe indexing of the coordinates when using with matrices, arrays, etc.
48  // (BaBar)
49 
50  Hep3Vector();
51  explicit Hep3Vector(double x);
52  Hep3Vector(double x, double y);
53  Hep3Vector(double x, double y, double z);
54  // The constructor.
55 
56  inline Hep3Vector(const Hep3Vector &);
57  inline Hep3Vector(Hep3Vector &&) = default;
58  // The copy and move constructors.
59 
60  inline ~Hep3Vector();
61  // The destructor. Not virtual - inheritance from this class is dangerous.
62 
63  inline double operator () (int) const;
64  // Get components by index -- 0-based (Geant4)
65 
66  inline double operator [] (int) const;
67  // Get components by index -- 0-based (Geant4)
68 
69  inline double & operator () (int);
70  // Set components by index. 0-based.
71 
72  inline double & operator [] (int);
73  // Set components by index. 0-based.
74 
75  inline double x() const;
76  inline double y() const;
77  inline double z() const;
78  // The components in cartesian coordinate system. Same as getX() etc.
79 
80  inline void setX(double);
81  inline void setY(double);
82  inline void setZ(double);
83  // Set the components in cartesian coordinate system.
84 
85  inline void set( double x, double y, double z);
86  // Set all three components in cartesian coordinate system.
87 
88  inline double phi() const;
89  // The azimuth angle.
90 
91  inline double theta() const;
92  // The polar angle.
93 
94  inline double cosTheta() const;
95  // Cosine of the polar angle.
96 
97  inline double cos2Theta() const;
98  // Cosine squared of the polar angle - faster than cosTheta(). (ZOOM)
99 
100  inline double mag2() const;
101  // The magnitude squared (r^2 in spherical coordinate system).
102 
103  inline double mag() const;
104  // The magnitude (r in spherical coordinate system).
105 
106  inline void setPhi(double);
107  // Set phi keeping mag and theta constant (BaBar).
108 
109  inline void setTheta(double);
110  // Set theta keeping mag and phi constant (BaBar).
111 
112  void setMag(double);
113  // Set magnitude keeping theta and phi constant (BaBar).
114 
115  inline double perp2() const;
116  // The transverse component squared (rho^2 in cylindrical coordinate system).
117 
118  inline double perp() const;
119  // The transverse component (rho in cylindrical coordinate system).
120 
121  inline void setPerp(double);
122  // Set the transverse component keeping phi and z constant.
123 
124  void setCylTheta(double);
125  // Set theta while keeping transvers component and phi fixed
126 
127  inline double perp2(const Hep3Vector &) const;
128  // The transverse component w.r.t. given axis squared.
129 
130  inline double perp(const Hep3Vector &) const;
131  // The transverse component w.r.t. given axis.
132 
133  inline Hep3Vector & operator = (const Hep3Vector &);
134  inline Hep3Vector & operator = (Hep3Vector &&) = default;
135  // The copy and move assignment operators.
136 
137  inline bool operator == (const Hep3Vector &) const;
138  inline bool operator != (const Hep3Vector &) const;
139  // Comparisons (Geant4).
140 
141  bool isNear (const Hep3Vector &, double epsilon=tolerance) const;
142  // Check for equality within RELATIVE tolerance (default 2.2E-14). (ZOOM)
143  // |v1 - v2|**2 <= epsilon**2 * |v1.dot(v2)|
144 
145  double howNear(const Hep3Vector & v ) const;
146  // sqrt ( |v1-v2|**2 / v1.dot(v2) ) with a maximum of 1.
147  // If v1.dot(v2) is negative, will return 1.
148 
149  double deltaR(const Hep3Vector & v) const;
150  // sqrt( pseudorapity_difference**2 + deltaPhi **2 )
151 
152  inline Hep3Vector & operator += (const Hep3Vector &);
153  // Addition.
154 
155  inline Hep3Vector & operator -= (const Hep3Vector &);
156  // Subtraction.
157 
158  inline Hep3Vector operator - () const;
159  // Unary minus.
160 
161  inline Hep3Vector & operator *= (double);
162  // Scaling with real numbers.
163 
164  Hep3Vector & operator /= (double);
165  // Division by (non-zero) real number.
166 
167  inline Hep3Vector unit() const;
168  // Vector parallel to this, but of length 1.
169 
170  inline Hep3Vector orthogonal() const;
171  // Vector orthogonal to this (Geant4).
172 
173  inline double dot(const Hep3Vector &) const;
174  // double product.
175 
176  inline Hep3Vector cross(const Hep3Vector &) const;
177  // Cross product.
178 
179  double angle(const Hep3Vector &) const;
180  // The angle w.r.t. another 3-vector.
181 
182  double pseudoRapidity() const;
183  // Returns the pseudo-rapidity, i.e. -ln(tan(theta/2))
184 
185  void setEta ( double p );
186  // Set pseudo-rapidity, keeping magnitude and phi fixed. (ZOOM)
187 
188  void setCylEta ( double p );
189  // Set pseudo-rapidity, keeping transverse component and phi fixed. (ZOOM)
190 
191  Hep3Vector & rotateX(double);
192  // Rotates the Hep3Vector around the x-axis.
193 
194  Hep3Vector & rotateY(double);
195  // Rotates the Hep3Vector around the y-axis.
196 
197  Hep3Vector & rotateZ(double);
198  // Rotates the Hep3Vector around the z-axis.
199 
200  Hep3Vector & rotateUz(const Hep3Vector&);
201  // Rotates reference frame from Uz to newUz (unit vector) (Geant4).
202 
203  Hep3Vector & rotate(double, const Hep3Vector &);
204  // Rotates around the axis specified by another Hep3Vector.
205  // (Uses methods of HepRotation, forcing linking in of Rotation.cc.)
206 
208  Hep3Vector & transform(const HepRotation &);
209  // Transformation with a Rotation matrix.
210 
211 // = = = = = = = = = = = = = = = = = = = = = = = =
212 //
213 // Esoteric properties and operations on 3-vectors:
214 //
215 // 1 - Set vectors in various coordinate systems
216 // 2 - Synonyms for accessing coordinates and properties
217 // 3 - Comparisions (dictionary, near-ness, and geometric)
218 // 4 - Intrinsic properties
219 // 5 - Properties releative to z axis and arbitrary directions
220 // 6 - Polar and azimuthal angle decomposition and deltaPhi
221 // 7 - Rotations
222 //
223 // = = = = = = = = = = = = = = = = = = = = = = = =
224 
225 // 1 - Set vectors in various coordinate systems
226 
227  inline void setRThetaPhi (double r, double theta, double phi);
228  // Set in spherical coordinates: Angles are measured in RADIANS
229 
230  inline void setREtaPhi ( double r, double eta, double phi );
231  // Set in spherical coordinates, but specify peudorapidiy to determine theta.
232 
233  inline void setRhoPhiZ (double rho, double phi, double z);
234  // Set in cylindrical coordinates: Phi angle is measured in RADIANS
235 
236  void setRhoPhiTheta ( double rho, double phi, double theta);
237  // Set in cylindrical coordinates, but specify theta to determine z.
238 
239  void setRhoPhiEta ( double rho, double phi, double eta);
240  // Set in cylindrical coordinates, but specify pseudorapidity to determine z.
241 
242 // 2 - Synonyms for accessing coordinates and properties
243 
244  inline double getX() const;
245  inline double getY() const;
246  inline double getZ() const;
247  // x(), y(), and z()
248 
249  inline double getR () const;
250  inline double getTheta() const;
251  inline double getPhi () const;
252  // mag(), theta(), and phi()
253 
254  inline double r () const;
255  // mag()
256 
257  inline double rho () const;
258  inline double getRho () const;
259  // perp()
260 
261  double eta () const;
262  double getEta () const;
263  // pseudoRapidity()
264 
265  inline void setR ( double s );
266  // setMag()
267 
268  inline void setRho ( double s );
269  // setPerp()
270 
271 // 3 - Comparisions (dictionary, near-ness, and geometric)
272 
273  int compare (const Hep3Vector & v) const;
274  bool operator > (const Hep3Vector & v) const;
275  bool operator < (const Hep3Vector & v) const;
276  bool operator>= (const Hep3Vector & v) const;
277  bool operator<= (const Hep3Vector & v) const;
278  // dictionary ordering according to z, then y, then x component
279 
280  inline double diff2 (const Hep3Vector & v) const;
281  // |v1-v2|**2
282 
283  static double setTolerance (double tol);
284  static inline double getTolerance ();
285  // Set the tolerance used in isNear() for Hep3Vectors
286 
287  bool isParallel (const Hep3Vector & v, double epsilon=tolerance) const;
288  // Are the vectors parallel, within the given tolerance?
289 
290  bool isOrthogonal (const Hep3Vector & v, double epsilon=tolerance) const;
291  // Are the vectors orthogonal, within the given tolerance?
292 
293  double howParallel (const Hep3Vector & v) const;
294  // | v1.cross(v2) / v1.dot(v2) |, to a maximum of 1.
295 
296  double howOrthogonal (const Hep3Vector & v) const;
297  // | v1.dot(v2) / v1.cross(v2) |, to a maximum of 1.
298 
299  enum { ToleranceTicks = 100 };
300 
301 // 4 - Intrinsic properties
302 
303  double beta () const;
304  // relativistic beta (considering v as a velocity vector with c=1)
305  // Same as mag() but will object if >= 1
306 
307  double gamma() const;
308  // relativistic gamma (considering v as a velocity vector with c=1)
309 
310  double coLinearRapidity() const;
311  // inverse tanh (beta)
312 
313 // 5 - Properties relative to Z axis and to an arbitrary direction
314 
315  // Note that the non-esoteric CLHEP provides
316  // theta(), cosTheta(), cos2Theta, and angle(const Hep3Vector&)
317 
318  inline double angle() const;
319  // angle against the Z axis -- synonym for theta()
320 
321  inline double theta(const Hep3Vector & v2) const;
322  // synonym for angle(v2)
323 
324  double cosTheta (const Hep3Vector & v2) const;
325  double cos2Theta(const Hep3Vector & v2) const;
326  // cos and cos^2 of the angle between two vectors
327 
328  inline Hep3Vector project () const;
329  Hep3Vector project (const Hep3Vector & v2) const;
330  // projection of a vector along a direction.
331 
332  inline Hep3Vector perpPart() const;
333  inline Hep3Vector perpPart (const Hep3Vector & v2) const;
334  // vector minus its projection along a direction.
335 
336  double rapidity () const;
337  // inverse tanh(v.z())
338 
339  double rapidity (const Hep3Vector & v2) const;
340  // rapidity with respect to specified direction:
341  // inverse tanh (v.dot(u)) where u is a unit in the direction of v2
342 
343  double eta(const Hep3Vector & v2) const;
344  // - ln tan of the angle beween the vector and the ref direction.
345 
346 // 6 - Polar and azimuthal angle decomposition and deltaPhi
347 
348  // Decomposition of an angle within reference defined by a direction:
349 
350  double polarAngle (const Hep3Vector & v2) const;
351  // The reference direction is Z: the polarAngle is abs(v.theta()-v2.theta()).
352 
353  double deltaPhi (const Hep3Vector & v2) const;
354  // v.phi()-v2.phi(), brought into the range (-PI,PI]
355 
356  double azimAngle (const Hep3Vector & v2) const;
357  // The reference direction is Z: the azimAngle is the same as deltaPhi
358 
359  double polarAngle (const Hep3Vector & v2,
360  const Hep3Vector & ref) const;
361  // For arbitrary reference direction,
362  // polarAngle is abs(v.angle(ref) - v2.angle(ref)).
363 
364  double azimAngle (const Hep3Vector & v2,
365  const Hep3Vector & ref) const;
366  // To compute azimangle, project v and v2 into the plane normal to
367  // the reference direction. Then in that plane take the angle going
368  // clockwise around the direction from projection of v to that of v2.
369 
370 // 7 - Rotations
371 
372 // These mehtods **DO NOT** use anything in the HepRotation class.
373 // Thus, use of v.rotate(axis,delta) does not force linking in Rotation.cc.
374 
375  Hep3Vector & rotate (const Hep3Vector & axis, double delta);
376  // Synonym for rotate (delta, axis)
377 
378  Hep3Vector & rotate (const HepAxisAngle & ax);
379  // HepAxisAngle is a struct holding an axis direction and an angle.
380 
381  Hep3Vector & rotate (const HepEulerAngles & e);
382  Hep3Vector & rotate (double phi,
383  double theta,
384  double psi);
385  // Rotate via Euler Angles. Our Euler Angles conventions are
386  // those of Goldstein Classical Mechanics page 107.
387 
388 protected:
389  void setSpherical (double r, double theta, double phi);
390  void setCylindrical (double r, double phi, double z);
391  double negativeInfinity() const;
392 
393 protected:
394 
395  double dx;
396  double dy;
397  double dz;
398  // The components.
399 
400  DLL_API static double tolerance;
401  // default tolerance criterion for isNear() to return true.
402 }; // Hep3Vector
403 
404 // Global Methods
405 
406 Hep3Vector rotationXOf (const Hep3Vector & vec, double delta);
407 Hep3Vector rotationYOf (const Hep3Vector & vec, double delta);
408 Hep3Vector rotationZOf (const Hep3Vector & vec, double delta);
409 
410 Hep3Vector rotationOf (const Hep3Vector & vec,
411  const Hep3Vector & axis, double delta);
412 Hep3Vector rotationOf (const Hep3Vector & vec, const HepAxisAngle & ax);
413 
414 Hep3Vector rotationOf (const Hep3Vector & vec,
415  double phi, double theta, double psi);
416 Hep3Vector rotationOf (const Hep3Vector & vec, const HepEulerAngles & e);
417 // Return a new vector based on a rotation of the supplied vector
418 
419 std::ostream & operator << (std::ostream &, const Hep3Vector &);
420 // Output to a stream.
421 
422 std::istream & operator >> (std::istream &, Hep3Vector &);
423 // Input from a stream.
424 
425 extern DLL_API const Hep3Vector HepXHat, HepYHat, HepZHat;
426 
429 
430 Hep3Vector operator / (const Hep3Vector &, double a);
431 // Division of 3-vectors by non-zero real number
432 
433 inline Hep3Vector operator + (const Hep3Vector &, const Hep3Vector &);
434 // Addition of 3-vectors.
435 
436 inline Hep3Vector operator - (const Hep3Vector &, const Hep3Vector &);
437 // Subtraction of 3-vectors.
438 
439 inline double operator * (const Hep3Vector &, const Hep3Vector &);
440 // double product of 3-vectors.
441 
442 inline Hep3Vector operator * (const Hep3Vector &, double a);
443 inline Hep3Vector operator * (double a, const Hep3Vector &);
444 // Scaling of 3-vectors with a real number
445 
446 } // namespace CLHEP
447 
448 #include "CLHEP/Vector/ThreeVector.icc"
449 
450 #endif /* HEP_THREEVECTOR_H */