ROOT  6.06/09
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 
22 #ifndef ROOT_Math_GenVector_Polar3Dfwd
24 #endif
25 
26 #ifndef ROOT_Math_Math
27 #include "Math/Math.h"
28 #endif
29 
30 #include <limits>
31 
32 
33 #ifndef ROOT_Math_GenVector_eta
34 #include "Math/GenVector/eta.h"
35 #endif
36 
37 
38 namespace ROOT {
39 
40 namespace Math {
41 
42 //__________________________________________________________________________________________
43  /**
44  Class describing a 3D cartesian coordinate system
45  (x, y, z coordinates)
46 
47  @ingroup GenVector
48  */
49 
50 template <class T = double>
51 class Cartesian3D {
52 
53 public :
54 
55  typedef T Scalar;
56 
57  /**
58  Default constructor with x=y=z=0
59  */
60  Cartesian3D() : fX(0.0), fY(0.0), fZ(0.0) { }
61 
62  /**
63  Constructor from x,y,z coordinates
64  */
65  Cartesian3D(Scalar xx, Scalar yy, Scalar zz) : fX(xx), fY(yy), fZ(zz) { }
66 
67  /**
68  Construct from any Vector or coordinate system implementing
69  X(), Y() and Z()
70  */
71  template <class CoordSystem>
72  explicit Cartesian3D(const CoordSystem & v)
73  : fX(v.X()), fY(v.Y()), fZ(v.Z()) { }
74 
75  // for g++ 3.2 and 3.4 on 32 bits found that the compiler generated copy ctor and assignment are much slower
76  // re-implement them ( there is no no need to have them with g++4)
77  /**
78  copy constructor
79  */
81  fX(v.X()), fY(v.Y()), fZ(v.Z()) { }
82 
83  /**
84  assignment operator
85  */
87  fX = v.x();
88  fY = v.y();
89  fZ = v.z();
90  return *this;
91  }
92 
93 
94  /**
95  Set internal data based on an array of 3 Scalar numbers
96  */
97  void SetCoordinates( const Scalar src[] ) { fX=src[0]; fY=src[1]; fZ=src[2]; }
98 
99  /**
100  get internal data into an array of 3 Scalar numbers
101  */
102  void GetCoordinates( Scalar dest[] ) const
103  { dest[0] = fX; dest[1] = fY; dest[2] = fZ; }
104 
105  /**
106  Set internal data based on 3 Scalar numbers
107  */
108  void SetCoordinates(Scalar xx, Scalar yy, Scalar zz) { fX=xx; fY=yy; fZ=zz; }
109 
110  /**
111  get internal data into 3 Scalar numbers
112  */
113  void GetCoordinates(Scalar& xx, Scalar& yy, Scalar& zz) const {xx=fX; yy=fY; zz=fZ;}
114 
115  Scalar X() const { return fX;}
116  Scalar Y() const { return fY;}
117  Scalar Z() const { return fZ;}
118  Scalar Mag2() const { return fX*fX + fY*fY + fZ*fZ;}
119  Scalar Perp2() const { return fX*fX + fY*fY ;}
120  Scalar Rho() const { return std::sqrt( Perp2());}
121  Scalar R() const { return std::sqrt( Mag2());}
122  Scalar Theta() const { return (fX==0 && fY==0 && fZ==0) ?
123  0 : atan2(Rho(),Z());}
124  Scalar Phi() const { return (fX==0 && fY==0) ? 0 : atan2(fY,fX);}
125 
126  // pseudorapidity
127  Scalar Eta() const {
128  return Impl::Eta_FromRhoZ ( Rho(),fZ);
129  }
130 
131  /**
132  set the x coordinate value keeping y and z constant
133  */
134  void SetX(Scalar xx) { fX = xx; }
135 
136  /**
137  set the y coordinate value keeping x and z constant
138  */
139  void SetY(Scalar yy) { fY = yy; }
140 
141  /**
142  set the z coordinate value keeping x and y constant
143  */
144  void SetZ(Scalar zz) { fZ = zz; }
145 
146  /**
147  set all values using cartesian coordinates
148  */
149  void SetXYZ(Scalar xx, Scalar yy, Scalar zz) {
150  fX=xx;
151  fY=yy;
152  fZ=zz;
153  }
154 
155  /**
156  scale the vector by a scalar quantity a
157  */
158  void Scale(Scalar a) { fX *= a; fY *= a; fZ *= a; }
159 
160  /**
161  negate the vector
162  */
163  void Negate() { fX = -fX; fY = -fY; fZ = -fZ; }
164 
165  /**
166  Assignment from any class implementing x(),y() and z()
167  (can assign from any coordinate system)
168  */
169  template <class CoordSystem>
170  Cartesian3D & operator = (const CoordSystem & v) {
171  fX = v.x();
172  fY = v.y();
173  fZ = v.z();
174  return *this;
175  }
176 
177  /**
178  Exact equality
179  */
180  bool operator == (const Cartesian3D & rhs) const {
181  return fX == rhs.fX && fY == rhs.fY && fZ == rhs.fZ;
182  }
183  bool operator != (const Cartesian3D & rhs) const {return !(operator==(rhs));}
184 
185 
186  // ============= Compatibility section ==================
187 
188  // The following make this coordinate system look enough like a CLHEP
189  // vector that an assignment member template can work with either
190  T x() const { return X();}
191  T y() const { return Y();}
192  T z() const { return Z(); }
193 
194  // ============= Overloads for improved speed ==================
195 
196  template <class T2>
197  explicit Cartesian3D( const Polar3D<T2> & v ) : fZ (v.Z())
198  {
199  T rho = v.Rho(); // 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  T rho = v.Rho();
213  fX = rho * std::cos(v.Phi());
214  fY = rho * std::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
255 #ifndef ROOT_Math_GenVector_GenVector_exception
257 #endif
258 #ifndef ROOT_Math_GenVector_CylindricalEta3D
260 #endif
261 #ifndef ROOT_Math_GenVector_Polar3D
262 #include "Math/GenVector/Polar3D.h"
263 #endif
264 
265  // ====== Set member functions for coordinates in other systems =======
266 
267 namespace ROOT {
268 
269  namespace Math {
270 
271 template <class T>
272 void Cartesian3D<T>::SetR(Scalar r) {
273  GenVector_exception e("Cartesian3D::SetR() is not supposed to be called");
274  throw e;
275  Polar3D<Scalar> v(*this); v.SetR(r); *this = Cartesian3D<Scalar>(v);
276 }
277 
278 template <class T>
279 void Cartesian3D<T>::SetTheta(Scalar theta) {
280  GenVector_exception e("Cartesian3D::SetTheta() is not supposed to be called");
281  throw e;
282  Polar3D<Scalar> v(*this); v.SetTheta(theta); *this = Cartesian3D<Scalar>(v);
283 }
284 
285 template <class T>
286 void Cartesian3D<T>::SetPhi(Scalar phi) {
287  GenVector_exception e("Cartesian3D::SetPhi() is not supposed to be called");
288  throw e;
289  Polar3D<Scalar> v(*this); v.SetPhi(phi); *this = Cartesian3D<Scalar>(v);
290 }
291 
292 template <class T>
293 void Cartesian3D<T>::SetRho(Scalar rho) {
294  GenVector_exception e("Cartesian3D::SetRho() is not supposed to be called");
295  throw e;
296  CylindricalEta3D<Scalar> v(*this); v.SetRho(rho);
297  *this = Cartesian3D<Scalar>(v);
298 }
299 
300 template <class T>
301 void Cartesian3D<T>::SetEta(Scalar eta) {
302  GenVector_exception e("Cartesian3D::SetEta() is not supposed to be called");
303  throw e;
304  CylindricalEta3D<Scalar> v(*this); v.SetEta(eta);
305  *this = Cartesian3D<Scalar>(v);
306 }
307 
308 
309 
310  } // end namespace Math
311 
312 } // end namespace ROOT
313 
314 #endif
315 
316 
317 
318 
319 #endif /* ROOT_Math_GenVector_Cartesian3D */
void SetX(Scalar xx)
set the x coordinate value keeping y and z constant
Definition: Cartesian3D.h:134
void Scale(Scalar a)
scale the vector by a scalar quantity a
Definition: Cartesian3D.h:158
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
Cartesian3D(Scalar xx, Scalar yy, Scalar zz)
Constructor from x,y,z coordinates.
Definition: Cartesian3D.h:65
double T(double x)
Definition: ChebyshevPol.h:34
Scalar Y() const
Definition: Cartesian3D.h:116
Scalar Rho() const
Definition: Polar3D.h:116
Class describing a 3D cartesian coordinate system (x, y, z coordinates)
Definition: Cartesian3D.h:51
void GetCoordinates(Scalar dest[]) const
get internal data into an array of 3 Scalar numbers
Definition: Cartesian3D.h:102
TArc * a
Definition: textangle.C:12
Cartesian3D & operator=(const Cartesian3D &v)
assignment operator
Definition: Cartesian3D.h:86
Scalar Perp2() const
Definition: Cartesian3D.h:119
double cos(double)
void SetXYZ(Scalar xx, Scalar yy, Scalar zz)
set all values using cartesian coordinates
Definition: Cartesian3D.h:149
Scalar Mag2() const
Definition: Cartesian3D.h:118
double sqrt(double)
void SetY(Scalar yy)
set the y coordinate value keeping x and z constant
Definition: Cartesian3D.h:139
Scalar R() const
Definition: Cartesian3D.h:121
void GetCoordinates(Scalar &xx, Scalar &yy, Scalar &zz) const
get internal data into 3 Scalar numbers
Definition: Cartesian3D.h:113
Scalar X() const
Definition: Cartesian3D.h:115
Cartesian3D()
Default constructor with x=y=z=0.
Definition: Cartesian3D.h:60
void Negate()
negate the vector
Definition: Cartesian3D.h:163
Scalar Z() const
Definition: Polar3D.h:119
double sin(double)
void SetCoordinates(Scalar xx, Scalar yy, Scalar zz)
Set internal data based on 3 Scalar numbers.
Definition: Cartesian3D.h:108
void SetCoordinates(const Scalar src[])
Set internal data based on an array of 3 Scalar numbers.
Definition: Cartesian3D.h:97
Scalar Theta() const
Definition: Cartesian3D.h:122
void SetZ(Scalar zz)
set the z coordinate value keeping x and y constant
Definition: Cartesian3D.h:144
SVector< double, 2 > v
Definition: Dict.h:5
bool operator==(const Cartesian3D &rhs) const
Exact equality.
Definition: Cartesian3D.h:180
Cartesian3D(const Polar3D< T2 > &v)
Definition: Cartesian3D.h:197
Scalar Eta_FromRhoZ(Scalar rho, Scalar z)
Calculate eta given rho and zeta.
Definition: eta.h:50
bool operator!=(const Cartesian3D &rhs) const
Definition: Cartesian3D.h:183
Scalar Phi() const
Definition: Cartesian3D.h:124
Scalar Phi() const
Definition: Polar3D.h:114
Cartesian3D(const CoordSystem &v)
Construct from any Vector or coordinate system implementing X(), Y() and Z()
Definition: Cartesian3D.h:72
Scalar Eta() const
Definition: Cartesian3D.h:127
double atan2(double, double)
Namespace for new Math classes and functions.
#define dest(otri, vertexptr)
Definition: triangle.c:1040
Scalar Rho() const
Definition: Cartesian3D.h:120
Cartesian3D(const Cartesian3D &v)
copy constructor
Definition: Cartesian3D.h:80
std::complex< float_v > Z
Definition: main.cpp:120
Scalar Z() const
Definition: Cartesian3D.h:117
Plane3D::Scalar Scalar
Definition: Plane3D.cxx:29
Class describing a polar coordinate system based on r, theta and phi Phi is restricted to be in the r...
Definition: Polar3D.h:46