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