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