ROOT   6.14/05 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 { return atan2(Rho(), Z()); }
115  Scalar Phi() const { return atan2(fY, fX); }
116
117  // pseudorapidity
118  Scalar Eta() const {
119  return Impl::Eta_FromRhoZ( Rho(), fZ );
120  }
121
122  /**
123  set the x coordinate value keeping y and z constant
124  */
125  void SetX(Scalar xx) { fX = xx; }
126
127  /**
128  set the y coordinate value keeping x and z constant
129  */
130  void SetY(Scalar yy) { fY = yy; }
131
132  /**
133  set the z coordinate value keeping x and y constant
134  */
135  void SetZ(Scalar zz) { fZ = zz; }
136
137  /**
138  set all values using cartesian coordinates
139  */
140  void SetXYZ(Scalar xx, Scalar yy, Scalar zz) {
141  fX=xx;
142  fY=yy;
143  fZ=zz;
144  }
145
146  /**
147  scale the vector by a scalar quantity a
148  */
149  void Scale(Scalar a)
150  {
151  fX *= a;
152  fY *= a;
153  fZ *= a;
154  }
155
156  /**
157  negate the vector
158  */
159  void Negate() { fX = -fX; fY = -fY; fZ = -fZ; }
160
161  /**
162  Assignment from any class implementing x(),y() and z()
163  (can assign from any coordinate system)
164  */
165  template <class CoordSystem>
166  Cartesian3D & operator = (const CoordSystem & v) {
167  fX = v.x();
168  fY = v.y();
169  fZ = v.z();
170  return *this;
171  }
172
173  /**
174  Exact equality
175  */
176  bool operator == (const Cartesian3D & rhs) const {
177  return fX == rhs.fX && fY == rhs.fY && fZ == rhs.fZ;
178  }
179  bool operator != (const Cartesian3D & rhs) const {return !(operator==(rhs));}
180
181
182  // ============= Compatibility section ==================
183
184  // The following make this coordinate system look enough like a CLHEP
185  // vector that an assignment member template can work with either
186  T x() const { return X();}
187  T y() const { return Y();}
188  T z() const { return Z(); }
189
190  // ============= Overloads for improved speed ==================
191
192  template <class T2>
193  explicit Cartesian3D( const Polar3D<T2> & v ) : fZ (v.Z())
194  {
195  const T rho = v.Rho();
196  // re-using this instead of calling v.X() and v.Y()
197  // is the speed improvement
198  fX = rho * std::cos(v.Phi());
199  fY = rho * std::sin(v.Phi());
200  }
201  // Technical note: This works even though only Polar3Dfwd.h is
202  // included (and in fact, including Polar3D.h would cause circularity
203  // problems). It works because any program **using** this ctor must itself
204  // be including Polar3D.h.
205
206  template <class T2>
208  {
209  const T rho = v.Rho();
210  fX = rho * cos(v.Phi());
211  fY = rho * sin(v.Phi());
212  fZ = v.Z();
213  return *this;
214  }
215
216
217
218 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
219
220  // ====== Set member functions for coordinates in other systems =======
221
222  void SetR(Scalar r);
223
224  void SetTheta(Scalar theta);
225
226  void SetPhi(Scalar phi);
227
228  void SetRho(Scalar rho);
229
230  void SetEta(Scalar eta);
231
232 #endif
233
234
235 private:
236
237  T fX; // x coordinate
238  T fY; // y coordinate
239  T fZ; // z coordinate
240 };
241
242
243  } // end namespace Math
244
245 } // end namespace ROOT
246
247
248 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
249 // need to put here setter methods to resolve nasty cyclical dependencies
250 // I need to include other coordinate systems only when Cartesian is already defined
251 // since they depend on it
254 #include "Math/GenVector/Polar3D.h"
255
256  // ====== Set member functions for coordinates in other systems =======
257
258 namespace ROOT {
259
260  namespace Math {
261
262 template <class T>
264  GenVector_exception e("Cartesian3D::SetR() is not supposed to be called");
265  throw e;
266  Polar3D<Scalar> v(*this); v.SetR(r); *this = Cartesian3D<Scalar>(v);
267 }
268
269 template <class T>
270 void Cartesian3D<T>::SetTheta(Scalar theta) {
271  GenVector_exception e("Cartesian3D::SetTheta() is not supposed to be called");
272  throw e;
273  Polar3D<Scalar> v(*this); v.SetTheta(theta); *this = Cartesian3D<Scalar>(v);
274 }
275
276 template <class T>
277 void Cartesian3D<T>::SetPhi(Scalar phi) {
278  GenVector_exception e("Cartesian3D::SetPhi() is not supposed to be called");
279  throw e;
280  Polar3D<Scalar> v(*this); v.SetPhi(phi); *this = Cartesian3D<Scalar>(v);
281 }
282
283 template <class T>
284 void Cartesian3D<T>::SetRho(Scalar rho) {
285  GenVector_exception e("Cartesian3D::SetRho() is not supposed to be called");
286  throw e;
287  CylindricalEta3D<Scalar> v(*this); v.SetRho(rho);
288  *this = Cartesian3D<Scalar>(v);
289 }
290
291 template <class T>
292 void Cartesian3D<T>::SetEta(Scalar eta) {
293  GenVector_exception e("Cartesian3D::SetEta() is not supposed to be called");
294  throw e;
295  CylindricalEta3D<Scalar> v(*this); v.SetEta(eta);
296  *this = Cartesian3D<Scalar>(v);
297 }
298
299
300
301  } // end namespace Math
302
303 } // end namespace ROOT
304
305 #endif
306
307
308
309
310 #endif /* ROOT_Math_GenVector_Cartesian3D */
Scalar Phi() const
Definition: Polar3D.h:111
bool operator==(const Cartesian3D &rhs) const
Exact equality.
Definition: Cartesian3D.h:176
void SetX(Scalar xx)
set the x coordinate value keeping y and z constant
Definition: Cartesian3D.h:125
Scalar Theta() const
Definition: Cartesian3D.h:114
void Scale(Scalar a)
scale the vector by a scalar quantity a
Definition: Cartesian3D.h:149
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
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:140
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:130
Cartesian3D()
Default constructor with x=y=z=0.
Definition: Cartesian3D.h:53
void Negate()
negate the vector
Definition: Cartesian3D.h:159
Scalar Phi() const
Definition: Cartesian3D.h:115
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:135
Scalar Z() const
Definition: Polar3D.h:116
ROOT::R::TRInterface & r
Definition: Object.C:4
SVector< double, 2 > v
Definition: Dict.h:5
auto * a
Definition: textangle.C:12
void SetRho(T rho)
set the rho coordinate value keeping eta and phi constant
Scalar Eta() const
Definition: Cartesian3D.h:118
bool operator!=(const Cartesian3D &rhs) const
Definition: Cartesian3D.h:179
Cartesian3D(const Polar3D< T2 > &v)
Definition: Cartesian3D.h:193
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