Logo ROOT   6.13/01
Reference Guide
Cartesian3D.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id: 2fd203872f434b1e4e74933903abb3429494ea6f $
2 // Authors: W. Brown, M. Fischler, L. Moneta 2005
3 
4  /**********************************************************************
5  * *
6  * Copyright (c) 2005 , LCG ROOT MathLib Team *
7  * & FNAL LCG ROOT Mathlib Team *
8  * *
9  * *
10  **********************************************************************/
11 
12 // Header file for class Cartesian3D
13 //
14 // Created by: Lorenzo Moneta at Mon May 30 11:16:56 2005
15 // Major revamp: M. FIschler at Wed Jun 8 2005
16 //
17 // Last update: $ID: $
18 //
19 #ifndef ROOT_Math_GenVector_Cartesian3D
20 #define ROOT_Math_GenVector_Cartesian3D 1
21 
23 
24 #include "Math/Math.h"
25 
26 #include <limits>
27 #include <cmath>
28 
29 #include "Math/GenVector/eta.h"
30 
31 namespace ROOT {
32 
33 namespace Math {
34 
35 //__________________________________________________________________________________________
36  /**
37  Class describing a 3D cartesian coordinate system
38  (x, y, z coordinates)
39 
40  @ingroup GenVector
41  */
42 
43 template <class T = double>
44 class Cartesian3D {
45 
46 public :
47 
48  typedef T Scalar;
49 
50  /**
51  Default constructor with x=y=z=0
52  */
53  Cartesian3D() : fX(0.0), fY(0.0), fZ(0.0) { }
54 
55  /**
56  Constructor from x,y,z coordinates
57  */
58  Cartesian3D(Scalar xx, Scalar yy, Scalar zz) : fX(xx), fY(yy), fZ(zz) { }
59 
60  /**
61  Construct from any Vector or coordinate system implementing
62  X(), Y() and Z()
63  */
64  template <class CoordSystem>
65  explicit Cartesian3D(const CoordSystem & v)
66  : fX(v.X()), fY(v.Y()), fZ(v.Z()) { }
67 
68  // for g++ 3.2 and 3.4 on 32 bits found that the compiler generated copy ctor and assignment are much slower
69  // re-implement them ( there is no no need to have them with g++4)
70  /**
71  copy constructor
72  */
74  fX(v.X()), fY(v.Y()), fZ(v.Z()) { }
75 
76  /**
77  assignment operator
78  */
80  fX = v.x();
81  fY = v.y();
82  fZ = v.z();
83  return *this;
84  }
85 
86  /**
87  Set internal data based on an array of 3 Scalar numbers
88  */
89  void SetCoordinates( const Scalar src[] ) { fX=src[0]; fY=src[1]; fZ=src[2]; }
90 
91  /**
92  get internal data into an array of 3 Scalar numbers
93  */
94  void GetCoordinates( Scalar dest[] ) const
95  { dest[0] = fX; dest[1] = fY; dest[2] = fZ; }
96 
97  /**
98  Set internal data based on 3 Scalar numbers
99  */
100  void SetCoordinates(Scalar xx, Scalar yy, Scalar zz) { fX=xx; fY=yy; fZ=zz; }
101 
102  /**
103  get internal data into 3 Scalar numbers
104  */
105  void GetCoordinates(Scalar& xx, Scalar& yy, Scalar& zz) const {xx=fX; yy=fY; zz=fZ;}
106 
107  Scalar X() const { return fX;}
108  Scalar Y() const { return fY;}
109  Scalar Z() const { return fZ;}
110  Scalar Mag2() const { return fX*fX + fY*fY + fZ*fZ;}
111  Scalar Perp2() const { return fX*fX + fY*fY ;}
112  Scalar Rho() const { return sqrt(Perp2()); }
113  Scalar R() const { return sqrt(Mag2()); }
114  Scalar Theta() const
115  {
116  return (fX == Scalar(0) && fY == Scalar(0) && fZ == Scalar(0)) ? Scalar(0) : atan2(Rho(), Z());
117  }
118  Scalar Phi() const { return (fX == Scalar(0) && fY == Scalar(0)) ? Scalar(0) : atan2(fY, fX); }
119 
120  // pseudorapidity
121  Scalar Eta() const {
122  return Impl::Eta_FromRhoZ( Rho(), fZ );
123  }
124 
125  /**
126  set the x coordinate value keeping y and z constant
127  */
128  void SetX(Scalar xx) { fX = xx; }
129 
130  /**
131  set the y coordinate value keeping x and z constant
132  */
133  void SetY(Scalar yy) { fY = yy; }
134 
135  /**
136  set the z coordinate value keeping x and y constant
137  */
138  void SetZ(Scalar zz) { fZ = zz; }
139 
140  /**
141  set all values using cartesian coordinates
142  */
143  void SetXYZ(Scalar xx, Scalar yy, Scalar zz) {
144  fX=xx;
145  fY=yy;
146  fZ=zz;
147  }
148 
149  /**
150  scale the vector by a scalar quantity a
151  */
152  void Scale(Scalar a)
153  {
154  fX *= a;
155  fY *= a;
156  fZ *= a;
157  }
158 
159  /**
160  negate the vector
161  */
162  void Negate() { fX = -fX; fY = -fY; fZ = -fZ; }
163 
164  /**
165  Assignment from any class implementing x(),y() and z()
166  (can assign from any coordinate system)
167  */
168  template <class CoordSystem>
169  Cartesian3D & operator = (const CoordSystem & v) {
170  fX = v.x();
171  fY = v.y();
172  fZ = v.z();
173  return *this;
174  }
175 
176  /**
177  Exact equality
178  */
179  bool operator == (const Cartesian3D & rhs) const {
180  return fX == rhs.fX && fY == rhs.fY && fZ == rhs.fZ;
181  }
182  bool operator != (const Cartesian3D & rhs) const {return !(operator==(rhs));}
183 
184 
185  // ============= Compatibility section ==================
186 
187  // The following make this coordinate system look enough like a CLHEP
188  // vector that an assignment member template can work with either
189  T x() const { return X();}
190  T y() const { return Y();}
191  T z() const { return Z(); }
192 
193  // ============= Overloads for improved speed ==================
194 
195  template <class T2>
196  explicit Cartesian3D( const Polar3D<T2> & v ) : fZ (v.Z())
197  {
198  const T rho = v.Rho();
199  // re-using this instead of calling v.X() and v.Y()
200  // is the speed improvement
201  fX = rho * std::cos(v.Phi());
202  fY = rho * std::sin(v.Phi());
203  }
204  // Technical note: This works even though only Polar3Dfwd.h is
205  // included (and in fact, including Polar3D.h would cause circularity
206  // problems). It works because any program **using** this ctor must itself
207  // be including Polar3D.h.
208 
209  template <class T2>
211  {
212  const T rho = v.Rho();
213  fX = rho * cos(v.Phi());
214  fY = rho * sin(v.Phi());
215  fZ = v.Z();
216  return *this;
217  }
218 
219 
220 
221 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
222 
223  // ====== Set member functions for coordinates in other systems =======
224 
225  void SetR(Scalar r);
226 
227  void SetTheta(Scalar theta);
228 
229  void SetPhi(Scalar phi);
230 
231  void SetRho(Scalar rho);
232 
233  void SetEta(Scalar eta);
234 
235 #endif
236 
237 
238 private:
239 
240  T fX; // x coordinate
241  T fY; // y coordinate
242  T fZ; // z coordinate
243 };
244 
245 
246  } // end namespace Math
247 
248 } // end namespace ROOT
249 
250 
251 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
252 // need to put here setter methods to resolve nasty cyclical dependencies
253 // I need to include other coordinate systems only when Cartesian is already defined
254 // since they depend on it
257 #include "Math/GenVector/Polar3D.h"
258 
259  // ====== Set member functions for coordinates in other systems =======
260 
261 namespace ROOT {
262 
263  namespace Math {
264 
265 template <class T>
267  GenVector_exception e("Cartesian3D::SetR() is not supposed to be called");
268  throw e;
269  Polar3D<Scalar> v(*this); v.SetR(r); *this = Cartesian3D<Scalar>(v);
270 }
271 
272 template <class T>
273 void Cartesian3D<T>::SetTheta(Scalar theta) {
274  GenVector_exception e("Cartesian3D::SetTheta() is not supposed to be called");
275  throw e;
276  Polar3D<Scalar> v(*this); v.SetTheta(theta); *this = Cartesian3D<Scalar>(v);
277 }
278 
279 template <class T>
280 void Cartesian3D<T>::SetPhi(Scalar phi) {
281  GenVector_exception e("Cartesian3D::SetPhi() is not supposed to be called");
282  throw e;
283  Polar3D<Scalar> v(*this); v.SetPhi(phi); *this = Cartesian3D<Scalar>(v);
284 }
285 
286 template <class T>
287 void Cartesian3D<T>::SetRho(Scalar rho) {
288  GenVector_exception e("Cartesian3D::SetRho() is not supposed to be called");
289  throw e;
290  CylindricalEta3D<Scalar> v(*this); v.SetRho(rho);
291  *this = Cartesian3D<Scalar>(v);
292 }
293 
294 template <class T>
295 void Cartesian3D<T>::SetEta(Scalar eta) {
296  GenVector_exception e("Cartesian3D::SetEta() is not supposed to be called");
297  throw e;
298  CylindricalEta3D<Scalar> v(*this); v.SetEta(eta);
299  *this = Cartesian3D<Scalar>(v);
300 }
301 
302 
303 
304  } // end namespace Math
305 
306 } // end namespace ROOT
307 
308 #endif
309 
310 
311 
312 
313 #endif /* ROOT_Math_GenVector_Cartesian3D */
Scalar Phi() const
Definition: Polar3D.h:111
bool operator==(const Cartesian3D &rhs) const
Exact equality.
Definition: Cartesian3D.h:179
void SetX(Scalar xx)
set the x coordinate value keeping y and z constant
Definition: Cartesian3D.h:128
Scalar Theta() const
Definition: Cartesian3D.h:114
void Scale(Scalar a)
scale the vector by a scalar quantity a
Definition: Cartesian3D.h:152
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
Cartesian3D(Scalar xx, Scalar yy, Scalar zz)
Constructor from x,y,z coordinates.
Definition: Cartesian3D.h:58
double T(double x)
Definition: ChebyshevPol.h:34
Scalar Mag2() const
Definition: Cartesian3D.h:110
Class describing a cylindrical coordinate system based on eta (pseudorapidity) instead of z...
Class describing a 3D cartesian coordinate system (x, y, z coordinates)
Definition: Cartesian3D.h:44
Cartesian3D & operator=(const Cartesian3D &v)
assignment operator
Definition: Cartesian3D.h:79
double cos(double)
void SetXYZ(Scalar xx, Scalar yy, Scalar zz)
set all values using cartesian coordinates
Definition: Cartesian3D.h:143
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
void SetR(const T &r)
set the r coordinate value keeping theta and phi constant
Definition: Polar3D.h:132
Scalar Perp2() const
Definition: Cartesian3D.h:111
void SetY(Scalar yy)
set the y coordinate value keeping x and z constant
Definition: Cartesian3D.h:133
Cartesian3D()
Default constructor with x=y=z=0.
Definition: Cartesian3D.h:53
void Negate()
negate the vector
Definition: Cartesian3D.h:162
Scalar Phi() const
Definition: Cartesian3D.h:118
double sin(double)
void SetCoordinates(Scalar xx, Scalar yy, Scalar zz)
Set internal data based on 3 Scalar numbers.
Definition: Cartesian3D.h:100
void SetTheta(const T &theta)
set the theta coordinate value keeping r and phi constant
Definition: Polar3D.h:139
void SetCoordinates(const Scalar src[])
Set internal data based on an array of 3 Scalar numbers.
Definition: Cartesian3D.h:89
void SetZ(Scalar zz)
set the z coordinate value keeping x and y constant
Definition: Cartesian3D.h:138
Scalar Z() const
Definition: Polar3D.h:116
SVector< double, 2 > v
Definition: Dict.h:5
void SetRho(T rho)
set the rho coordinate value keeping eta and phi constant
Scalar Eta() const
Definition: Cartesian3D.h:121
bool operator!=(const Cartesian3D &rhs) const
Definition: Cartesian3D.h:182
Cartesian3D(const Polar3D< T2 > &v)
Definition: Cartesian3D.h:196
Scalar Eta_FromRhoZ(Scalar rho, Scalar z)
Calculate eta given rho and zeta.
Definition: eta.h:48
void SetEta(T eta)
set the eta coordinate value keeping rho and phi constant
Cartesian3D(const CoordSystem &v)
Construct from any Vector or coordinate system implementing X(), Y() and Z()
Definition: Cartesian3D.h:65
double atan2(double, double)
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
void GetCoordinates(Scalar dest[]) const
get internal data into an array of 3 Scalar numbers
Definition: Cartesian3D.h:94
Namespace for new Math classes and functions.
void SetPhi(const T &phi)
set the phi coordinate value keeping r and theta constant
Definition: Polar3D.h:146
#define dest(otri, vertexptr)
Definition: triangle.c:1040
Cartesian3D(const Cartesian3D &v)
copy constructor
Definition: Cartesian3D.h:73
Scalar Rho() const
Definition: Cartesian3D.h:112
Scalar Rho() const
Definition: Polar3D.h:113
Rotation3D::Scalar Scalar
Class describing a polar coordinate system based on r, theta and phi Phi is restricted to be in the r...
Definition: Polar3D.h:43
void GetCoordinates(Scalar &xx, Scalar &yy, Scalar &zz) const
get internal data into 3 Scalar numbers
Definition: Cartesian3D.h:105