ROOT  6.06/09
Reference Guide
Polar2D.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 Polar2D
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_Polar2D
20 #define ROOT_Math_GenVector_Polar2D 1
21 
22 #ifndef ROOT_Math_Math
23 #include "Math/Math.h"
24 #endif
25 
26 #ifndef ROOT_Math_GenVector_etaMax
27 #include "Math/GenVector/etaMax.h"
28 #endif
29 
30 
31 
32 namespace ROOT {
33 
34 namespace Math {
35 
36 
37 //__________________________________________________________________________________________
38  /**
39  Class describing a polar 2D coordinate system based on r and phi
40  Phi is restricted to be in the range [-PI,PI)
41 
42  @ingroup GenVector
43  */
44 
45 
46 template <class T>
47 class Polar2D {
48 
49 public :
50 
51  typedef T Scalar;
52 
53  /**
54  Default constructor with r=1,phi=0
55  */
56  Polar2D() : fR(1.), fPhi(0) { }
57 
58  /**
59  Construct from the polar coordinates: r and phi
60  */
61  Polar2D(T r,T phi) : fR(r), fPhi(phi) { Restrict(); }
62 
63  /**
64  Construct from any Vector or coordinate system implementing
65  R() and Phi()
66  */
67  template <class CoordSystem >
68  explicit Polar2D( const CoordSystem & v ) :
69  fR(v.R() ), fPhi(v.Phi() ) { Restrict(); }
70 
71  // for g++ 3.2 and 3.4 on 32 bits found that the compiler generated copy ctor and assignment are much slower
72  // re-implement them ( there is no no need to have them with g++4)
73 
74  /**
75  copy constructor
76  */
77  Polar2D(const Polar2D & v) :
78  fR(v.R() ), fPhi(v.Phi() ) { }
79 
80  /**
81  assignment operator
82  */
83  Polar2D & operator= (const Polar2D & v) {
84  fR = v.R();
85  fPhi = v.Phi();
86  return *this;
87  }
88 
89 
90  /**
91  Set internal data based on 2 Scalar numbers
92  */
93  void SetCoordinates(Scalar r, Scalar phi)
94  { fR=r; fPhi=phi; Restrict(); }
95 
96  /**
97  get internal data into 2 Scalar numbers
98  */
99  void GetCoordinates(Scalar& r, Scalar& phi) const {r=fR; phi=fPhi;}
100 
101 
102  Scalar R() const { return fR;}
103  Scalar Phi() const { return fPhi; }
104  Scalar X() const { return fR*std::cos(fPhi);}
105  Scalar Y() const { return fR*std::sin(fPhi);}
106  Scalar Mag2() const { return fR*fR;}
107 
108 
109  // setters (only for data members)
110 
111 
112  /**
113  set the r coordinate value keeping phi constant
114  */
115  void SetR(const T & r) {
116  fR = r;
117  }
118 
119 
120  /**
121  set the phi coordinate value keeping r constant
122  */
123  void SetPhi(const T & phi) {
124  fPhi = phi;
125  Restrict();
126  }
127 
128  /**
129  set all values using cartesian coordinates
130  */
131  void SetXY(Scalar a, Scalar b);
132 
133 
134 private:
135  inline static double pi() { return M_PI; }
136 
137  /**
138  restrict abgle hi to be between -PI and PI
139  */
140  inline void Restrict() {
141  if ( fPhi <= -pi() || fPhi > pi() )
142  fPhi = fPhi - std::floor( fPhi/(2*pi()) +.5 ) * 2*pi();
143  return;
144  }
145 
146 public:
147 
148  /**
149  scale by a scalar quantity - for polar coordinates r changes
150  */
151  void Scale (T a) {
152  if (a < 0) {
153  Negate();
154  a = -a;
155  }
156  // angles do not change when scaling by a positive quantity
157  fR *= a;
158  }
159 
160  /**
161  negate the vector
162  */
163  void Negate ( ) {
164  fPhi = ( fPhi > 0 ? fPhi - pi() : fPhi + pi() );
165  }
166 
167  /**
168  rotate the vector
169  */
170  void Rotate(T angle) {
171  fPhi += angle;
172  Restrict();
173  }
174 
175  // assignment operators
176  /**
177  generic assignment operator from any coordinate system
178  */
179  template <class CoordSystem >
180  Polar2D & operator= ( const CoordSystem & c ) {
181  fR = c.R();
182  fPhi = c.Phi();
183  return *this;
184  }
185 
186  /**
187  Exact equality
188  */
189  bool operator==(const Polar2D & rhs) const {
190  return fR == rhs.fR && fPhi == rhs.fPhi;
191  }
192  bool operator!= (const Polar2D & rhs) const {return !(operator==(rhs));}
193 
194 
195  // ============= Compatibility section ==================
196 
197  // The following make this coordinate system look enough like a CLHEP
198  // vector that an assignment member template can work with either
199  T x() const { return X();}
200  T y() const { return Y();}
201 
202  // ============= Specializations for improved speed ==================
203 
204  // (none)
205 
206 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
207 
208  // ====== Set member functions for coordinates in other systems =======
209 
210  void SetX(Scalar a);
211 
212  void SetY(Scalar a);
213 
214 #endif
215 
216 private:
217  T fR;
219 };
220 
221 
222  } // end namespace Math
223 
224 } // end namespace ROOT
225 
226 
227 // move implementations here to avoid circle dependencies
228 
229 #ifndef ROOT_Math_GenVector_Cartesian2D
231 #endif
232 
233 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
234 #ifndef ROOT_Math_GenVector_GenVector_exception
236 #endif
237 #endif
238 
239 namespace ROOT {
240 
241  namespace Math {
242 
243 template <class T>
245  *this = Cartesian2D<Scalar>(a, b);
246 }
247 
248 
249 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
250 
251 
252 // ====== Set member functions for coordinates in other systems =======
253 
254  template <class T>
255  void Polar2D<T>::SetX(Scalar a) {
256  GenVector_exception e("Polar2D::SetX() is not supposed to be called");
257  throw e;
258  Cartesian2D<Scalar> v(*this); v.SetX(a); *this = Polar2D<Scalar>(v);
259  }
260  template <class T>
261  void Polar2D<T>::SetY(Scalar a) {
262  GenVector_exception e("Polar2D::SetY() is not supposed to be called");
263  throw e;
264  Cartesian2D<Scalar> v(*this); v.SetY(a); *this = Polar2D<Scalar>(v);
265  }
266 
267 #endif
268 
269 
270  } // end namespace Math
271 
272 } // end namespace ROOT
273 
274 
275 
276 #endif /* ROOT_Math_GenVector_Polar2D */
void GetCoordinates(Scalar &r, Scalar &phi) const
get internal data into 2 Scalar numbers
Definition: Polar2D.h:99
bool operator!=(const Polar2D &rhs) const
Definition: Polar2D.h:192
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
double T(double x)
Definition: ChebyshevPol.h:34
void SetR(const T &r)
set the r coordinate value keeping phi constant
Definition: Polar2D.h:115
Scalar R() const
Definition: Polar2D.h:102
void Rotate(T angle)
rotate the vector
Definition: Polar2D.h:170
Polar2D(const CoordSystem &v)
Construct from any Vector or coordinate system implementing R() and Phi()
Definition: Polar2D.h:68
bool operator==(const Polar2D &rhs) const
Exact equality.
Definition: Polar2D.h:189
TArc * a
Definition: textangle.C:12
void SetPhi(const T &phi)
set the phi coordinate value keeping r constant
Definition: Polar2D.h:123
Scalar Phi() const
Definition: Polar2D.h:103
double cos(double)
Polar2D(const Polar2D &v)
copy constructor
Definition: Polar2D.h:77
Class describing a polar 2D coordinate system based on r and phi Phi is restricted to be in the range...
Definition: Polar2D.h:47
double sin(double)
T y() const
Definition: Polar2D.h:200
ROOT::R::TRInterface & r
Definition: Object.C:4
#define M_PI
Definition: Rotated.cxx:105
SVector< double, 2 > v
Definition: Dict.h:5
void Restrict()
restrict abgle hi to be between -PI and PI
Definition: Polar2D.h:140
Scalar Y() const
Definition: Polar2D.h:105
Scalar X() const
Definition: Polar2D.h:104
double floor(double)
Scalar Mag2() const
Definition: Polar2D.h:106
void Negate()
negate the vector
Definition: Polar2D.h:163
static double pi()
Definition: Polar2D.h:135
Polar2D()
Default constructor with r=1,phi=0.
Definition: Polar2D.h:56
void SetCoordinates(Scalar r, Scalar phi)
Set internal data based on 2 Scalar numbers.
Definition: Polar2D.h:93
Class describing a 2D cartesian coordinate system (x, y coordinates)
Definition: Cartesian2D.h:41
void SetXY(Scalar a, Scalar b)
set all values using cartesian coordinates
Definition: Polar2D.h:244
Namespace for new Math classes and functions.
Polar2D & operator=(const Polar2D &v)
assignment operator
Definition: Polar2D.h:83
T x() const
Definition: Polar2D.h:199
void Scale(T a)
scale by a scalar quantity - for polar coordinates r changes
Definition: Polar2D.h:151
Plane3D::Scalar Scalar
Definition: Plane3D.cxx:29
Polar2D(T r, T phi)
Construct from the polar coordinates: r and phi.
Definition: Polar2D.h:61