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