ROOT logo
// @(#)root/mathcore:$Id: Polar3D.h 31938 2009-12-18 14:49:38Z moneta $
// Authors: W. Brown, M. Fischler, L. Moneta    2005  

 /**********************************************************************
  *                                                                    *
  * Copyright (c) 2005 , LCG ROOT MathLib Team  and                    *
  *                      FNAL LCG ROOT MathLib Team                    *
  *                                                                    *
  *                                                                    *
  **********************************************************************/

// Header file for class Polar3D
// 
// Created by: Lorenzo Moneta  at Mon May 30 11:40:03 2005
// Major revamp:  M. Fischler  at Wed Jun  8 2005
// 
// Last update: $Id: Polar3D.h 31938 2009-12-18 14:49:38Z moneta $
// 
#ifndef ROOT_Math_GenVector_Polar3D 
#define ROOT_Math_GenVector_Polar3D  1

#ifndef ROOT_Math_Math
#include "Math/Math.h"
#endif

#ifndef ROOT_Math_GenVector_eta
#include "Math/GenVector/eta.h"
#endif

 
namespace ROOT { 

namespace Math { 


//__________________________________________________________________________________________
   /** 
       Class describing a polar coordinate system based on r, theta and phi
       Phi is restricted to be in the range [-PI,PI)
       
       @ingroup GenVector
   */ 


template <class T> 
class Polar3D { 

public : 

   typedef T Scalar;

   /**
      Default constructor with r=theta=phi=0
   */
   Polar3D() : fR(0), fTheta(0), fPhi(0) {  }

   /**
      Construct from the polar coordinates:  r, theta and phi
   */
   Polar3D(T r,T theta,T phi) : fR(r), fTheta(theta), fPhi(phi) { Restrict(); }

   /**
      Construct from any Vector or coordinate system implementing 
      R(), Theta() and Phi()
   */ 
   template <class CoordSystem > 
   explicit Polar3D( const CoordSystem & v ) : 
      fR(v.R() ),  fTheta(v.Theta() ),  fPhi(v.Phi() )  { Restrict(); } 

   // for g++  3.2 and 3.4 on 32 bits found that the compiler generated copy ctor and assignment are much slower 
   // re-implement them ( there is no no need to have them with g++4)

   /**
      copy constructor
    */
   Polar3D(const Polar3D & v) :
      fR(v.R() ),  fTheta(v.Theta() ),  fPhi(v.Phi() )  {   } 

   /**
      assignment operator 
    */
   Polar3D & operator= (const Polar3D & v) { 
      fR     = v.R();  
      fTheta = v.Theta(); 
      fPhi   = v.Phi(); 
      return *this;
   } 

   /**
      Set internal data based on an array of 3 Scalar numbers
   */ 
   void SetCoordinates( const Scalar src[] ) 
   { fR=src[0]; fTheta=src[1]; fPhi=src[2]; Restrict(); }

   /**
      get internal data into an array of 3 Scalar numbers
   */ 
   void GetCoordinates( Scalar dest[] ) const 
   { dest[0] = fR; dest[1] = fTheta; dest[2] = fPhi; }

   /**
      Set internal data based on 3 Scalar numbers
   */ 
   void SetCoordinates(Scalar r, Scalar  theta, Scalar  phi) 
   { fR=r; fTheta=theta; fPhi=phi; Restrict(); }

   /**
      get internal data into 3 Scalar numbers
   */ 
   void GetCoordinates(Scalar& r, Scalar& theta, Scalar& phi) const {r=fR; theta=fTheta; phi=fPhi;}  				

   
   Scalar R()     const { return fR;}
   Scalar Phi()   const { return fPhi; }
   Scalar Theta() const { return fTheta; } 
   Scalar Rho()   const { return fR*std::sin(fTheta); }
   Scalar X()     const { return Rho()*std::cos(fPhi);}
   Scalar Y()     const { return Rho()*std::sin(fPhi);}
   Scalar Z()     const { return fR*std::cos(fTheta); } 
   Scalar Mag2()  const { return fR*fR;}
   Scalar Perp2() const { return Rho()*Rho(); }

   // pseudorapidity
   Scalar Eta() const 
   { 
      return Impl::Eta_FromTheta(fTheta, fR);
   }

   // setters (only for data members) 

  
   /** 
       set the r coordinate value keeping theta and phi constant
   */ 
   void SetR(const T & r) { 
      fR = r;      
   }

   /** 
       set the theta coordinate value keeping r and phi constant
   */ 
   void SetTheta(const T & theta) { 
      fTheta = theta;      
   }

   /** 
       set the phi coordinate value keeping r and theta constant
   */ 
   void SetPhi(const T & phi) { 
      fPhi = phi;      
      Restrict();
   }

   /** 
       set all values using cartesian coordinates  
   */
   void SetXYZ(Scalar x, Scalar y, Scalar z); 


private:
   inline static Scalar pi()  { return M_PI; } 
   inline void Restrict() {
      if ( fPhi <= -pi() || fPhi > pi() ) 
         fPhi = fPhi - std::floor( fPhi/(2*pi()) +.5 ) * 2*pi();
      return;
   } 

public:   

   /** 
       scale by a scalar quantity - for polar coordinates r changes
   */
   void Scale (T a) { 
      if (a < 0) {
         Negate();
         a = -a;
      }
      // angles do not change when scaling by a positive quantity
      fR *= a;     
   }

   /**
      negate the vector
   */ 
   void Negate ( ) { 
      fPhi = ( fPhi > 0 ? fPhi - pi() : fPhi + pi() );
      fTheta = pi() - fTheta;
   }

   // assignment operators
   /**
      generic assignment operator from any coordinate system 
   */ 
   template <class CoordSystem > 
   Polar3D & operator= ( const CoordSystem & c ) { 
      fR     = c.R();  
      fTheta = c.Theta(); 
      fPhi   = c.Phi(); 
      return *this;
   } 

   /**
      Exact equality
   */  
   bool operator==(const Polar3D & rhs) const {
      return fR == rhs.fR && fTheta == rhs.fTheta && fPhi == rhs.fPhi;
   }
   bool operator!= (const Polar3D & rhs) const {return !(operator==(rhs));}
  

   // ============= Compatibility section ==================
  
   // The following make this coordinate system look enough like a CLHEP
   // vector that an assignment member template can work with either
   T x() const { return X();}
   T y() const { return Y();}
   T z() const { return Z(); } 
  
   // ============= Specializations for improved speed ==================

   // (none)

#if defined(__MAKECINT__) || defined(G__DICTIONARY) 

   // ====== Set member functions for coordinates in other systems =======

   void SetX(Scalar x);

   void SetY(Scalar y); 

   void SetZ(Scalar z);

   void SetRho(Scalar rho);  

   void SetEta(Scalar eta); 

#endif

private:
   T fR;
   T fTheta;
   T fPhi;
};



  } // end namespace Math

} // end namespace ROOT

// move implementations here to avoid circle dependencies

#ifndef ROOT_Math_GenVector_Cartesian3D
#include "Math/GenVector/Cartesian3D.h"
#endif

#if defined(__MAKECINT__) || defined(G__DICTIONARY) 
#ifndef ROOT_Math_GenVector_GenVector_exception 
#include "Math/GenVector/GenVector_exception.h"
#endif
#ifndef ROOT_Math_GenVector_CylindricalEta3D 
#include "Math/GenVector/CylindricalEta3D.h"
#endif
#endif


namespace ROOT { 

  namespace Math { 

template <class T>  
void Polar3D<T>::SetXYZ(Scalar xx, Scalar yy, Scalar zz) {  
   *this = Cartesian3D<Scalar>(xx, yy, zz);
}

#if defined(__MAKECINT__) || defined(G__DICTIONARY) 

  // ====== Set member functions for coordinates in other systems =======


template <class T>  
void Polar3D<T>::SetX(Scalar xx) {  
   GenVector_exception e("Polar3D::SetX() is not supposed to be called");
   throw e;
   Cartesian3D<Scalar> v(*this); v.SetX(xx); *this = Polar3D<Scalar>(v);
}
template <class T>  
void Polar3D<T>::SetY(Scalar yy) {  
   GenVector_exception e("Polar3D::SetY() is not supposed to be called");
   throw e;
   Cartesian3D<Scalar> v(*this); v.SetY(yy); *this = Polar3D<Scalar>(v);
}
template <class T>  
void Polar3D<T>::SetZ(Scalar zz) {  
   GenVector_exception e("Polar3D::SetZ() is not supposed to be called");
   throw e;
   Cartesian3D<Scalar> v(*this); v.SetZ(zz); *this = Polar3D<Scalar>(v);
}
template <class T>  
void Polar3D<T>::SetRho(Scalar rho) {  
   GenVector_exception e("Polar3D::SetRho() is not supposed to be called");
   throw e;
   CylindricalEta3D<Scalar> v(*this); v.SetRho(rho); 
   *this = Polar3D<Scalar>(v);
}
template <class T>  
void Polar3D<T>::SetEta(Scalar eta) {  
   GenVector_exception e("Polar3D::SetEta() is not supposed to be called");
   throw e;
   CylindricalEta3D<Scalar> v(*this); v.SetEta(eta); 
   *this = Polar3D<Scalar>(v);
}

#endif  


  } // end namespace Math

} // end namespace ROOT



#endif /* ROOT_Math_GenVector_Polar3D  */
 Polar3D.h:1
 Polar3D.h:2
 Polar3D.h:3
 Polar3D.h:4
 Polar3D.h:5
 Polar3D.h:6
 Polar3D.h:7
 Polar3D.h:8
 Polar3D.h:9
 Polar3D.h:10
 Polar3D.h:11
 Polar3D.h:12
 Polar3D.h:13
 Polar3D.h:14
 Polar3D.h:15
 Polar3D.h:16
 Polar3D.h:17
 Polar3D.h:18
 Polar3D.h:19
 Polar3D.h:20
 Polar3D.h:21
 Polar3D.h:22
 Polar3D.h:23
 Polar3D.h:24
 Polar3D.h:25
 Polar3D.h:26
 Polar3D.h:27
 Polar3D.h:28
 Polar3D.h:29
 Polar3D.h:30
 Polar3D.h:31
 Polar3D.h:32
 Polar3D.h:33
 Polar3D.h:34
 Polar3D.h:35
 Polar3D.h:36
 Polar3D.h:37
 Polar3D.h:38
 Polar3D.h:39
 Polar3D.h:40
 Polar3D.h:41
 Polar3D.h:42
 Polar3D.h:43
 Polar3D.h:44
 Polar3D.h:45
 Polar3D.h:46
 Polar3D.h:47
 Polar3D.h:48
 Polar3D.h:49
 Polar3D.h:50
 Polar3D.h:51
 Polar3D.h:52
 Polar3D.h:53
 Polar3D.h:54
 Polar3D.h:55
 Polar3D.h:56
 Polar3D.h:57
 Polar3D.h:58
 Polar3D.h:59
 Polar3D.h:60
 Polar3D.h:61
 Polar3D.h:62
 Polar3D.h:63
 Polar3D.h:64
 Polar3D.h:65
 Polar3D.h:66
 Polar3D.h:67
 Polar3D.h:68
 Polar3D.h:69
 Polar3D.h:70
 Polar3D.h:71
 Polar3D.h:72
 Polar3D.h:73
 Polar3D.h:74
 Polar3D.h:75
 Polar3D.h:76
 Polar3D.h:77
 Polar3D.h:78
 Polar3D.h:79
 Polar3D.h:80
 Polar3D.h:81
 Polar3D.h:82
 Polar3D.h:83
 Polar3D.h:84
 Polar3D.h:85
 Polar3D.h:86
 Polar3D.h:87
 Polar3D.h:88
 Polar3D.h:89
 Polar3D.h:90
 Polar3D.h:91
 Polar3D.h:92
 Polar3D.h:93
 Polar3D.h:94
 Polar3D.h:95
 Polar3D.h:96
 Polar3D.h:97
 Polar3D.h:98
 Polar3D.h:99
 Polar3D.h:100
 Polar3D.h:101
 Polar3D.h:102
 Polar3D.h:103
 Polar3D.h:104
 Polar3D.h:105
 Polar3D.h:106
 Polar3D.h:107
 Polar3D.h:108
 Polar3D.h:109
 Polar3D.h:110
 Polar3D.h:111
 Polar3D.h:112
 Polar3D.h:113
 Polar3D.h:114
 Polar3D.h:115
 Polar3D.h:116
 Polar3D.h:117
 Polar3D.h:118
 Polar3D.h:119
 Polar3D.h:120
 Polar3D.h:121
 Polar3D.h:122
 Polar3D.h:123
 Polar3D.h:124
 Polar3D.h:125
 Polar3D.h:126
 Polar3D.h:127
 Polar3D.h:128
 Polar3D.h:129
 Polar3D.h:130
 Polar3D.h:131
 Polar3D.h:132
 Polar3D.h:133
 Polar3D.h:134
 Polar3D.h:135
 Polar3D.h:136
 Polar3D.h:137
 Polar3D.h:138
 Polar3D.h:139
 Polar3D.h:140
 Polar3D.h:141
 Polar3D.h:142
 Polar3D.h:143
 Polar3D.h:144
 Polar3D.h:145
 Polar3D.h:146
 Polar3D.h:147
 Polar3D.h:148
 Polar3D.h:149
 Polar3D.h:150
 Polar3D.h:151
 Polar3D.h:152
 Polar3D.h:153
 Polar3D.h:154
 Polar3D.h:155
 Polar3D.h:156
 Polar3D.h:157
 Polar3D.h:158
 Polar3D.h:159
 Polar3D.h:160
 Polar3D.h:161
 Polar3D.h:162
 Polar3D.h:163
 Polar3D.h:164
 Polar3D.h:165
 Polar3D.h:166
 Polar3D.h:167
 Polar3D.h:168
 Polar3D.h:169
 Polar3D.h:170
 Polar3D.h:171
 Polar3D.h:172
 Polar3D.h:173
 Polar3D.h:174
 Polar3D.h:175
 Polar3D.h:176
 Polar3D.h:177
 Polar3D.h:178
 Polar3D.h:179
 Polar3D.h:180
 Polar3D.h:181
 Polar3D.h:182
 Polar3D.h:183
 Polar3D.h:184
 Polar3D.h:185
 Polar3D.h:186
 Polar3D.h:187
 Polar3D.h:188
 Polar3D.h:189
 Polar3D.h:190
 Polar3D.h:191
 Polar3D.h:192
 Polar3D.h:193
 Polar3D.h:194
 Polar3D.h:195
 Polar3D.h:196
 Polar3D.h:197
 Polar3D.h:198
 Polar3D.h:199
 Polar3D.h:200
 Polar3D.h:201
 Polar3D.h:202
 Polar3D.h:203
 Polar3D.h:204
 Polar3D.h:205
 Polar3D.h:206
 Polar3D.h:207
 Polar3D.h:208
 Polar3D.h:209
 Polar3D.h:210
 Polar3D.h:211
 Polar3D.h:212
 Polar3D.h:213
 Polar3D.h:214
 Polar3D.h:215
 Polar3D.h:216
 Polar3D.h:217
 Polar3D.h:218
 Polar3D.h:219
 Polar3D.h:220
 Polar3D.h:221
 Polar3D.h:222
 Polar3D.h:223
 Polar3D.h:224
 Polar3D.h:225
 Polar3D.h:226
 Polar3D.h:227
 Polar3D.h:228
 Polar3D.h:229
 Polar3D.h:230
 Polar3D.h:231
 Polar3D.h:232
 Polar3D.h:233
 Polar3D.h:234
 Polar3D.h:235
 Polar3D.h:236
 Polar3D.h:237
 Polar3D.h:238
 Polar3D.h:239
 Polar3D.h:240
 Polar3D.h:241
 Polar3D.h:242
 Polar3D.h:243
 Polar3D.h:244
 Polar3D.h:245
 Polar3D.h:246
 Polar3D.h:247
 Polar3D.h:248
 Polar3D.h:249
 Polar3D.h:250
 Polar3D.h:251
 Polar3D.h:252
 Polar3D.h:253
 Polar3D.h:254
 Polar3D.h:255
 Polar3D.h:256
 Polar3D.h:257
 Polar3D.h:258
 Polar3D.h:259
 Polar3D.h:260
 Polar3D.h:261
 Polar3D.h:262
 Polar3D.h:263
 Polar3D.h:264
 Polar3D.h:265
 Polar3D.h:266
 Polar3D.h:267
 Polar3D.h:268
 Polar3D.h:269
 Polar3D.h:270
 Polar3D.h:271
 Polar3D.h:272
 Polar3D.h:273
 Polar3D.h:274
 Polar3D.h:275
 Polar3D.h:276
 Polar3D.h:277
 Polar3D.h:278
 Polar3D.h:279
 Polar3D.h:280
 Polar3D.h:281
 Polar3D.h:282
 Polar3D.h:283
 Polar3D.h:284
 Polar3D.h:285
 Polar3D.h:286
 Polar3D.h:287
 Polar3D.h:288
 Polar3D.h:289
 Polar3D.h:290
 Polar3D.h:291
 Polar3D.h:292
 Polar3D.h:293
 Polar3D.h:294
 Polar3D.h:295
 Polar3D.h:296
 Polar3D.h:297
 Polar3D.h:298
 Polar3D.h:299
 Polar3D.h:300
 Polar3D.h:301
 Polar3D.h:302
 Polar3D.h:303
 Polar3D.h:304
 Polar3D.h:305
 Polar3D.h:306
 Polar3D.h:307
 Polar3D.h:308
 Polar3D.h:309
 Polar3D.h:310
 Polar3D.h:311
 Polar3D.h:312
 Polar3D.h:313
 Polar3D.h:314
 Polar3D.h:315
 Polar3D.h:316
 Polar3D.h:317
 Polar3D.h:318
 Polar3D.h:319
 Polar3D.h:320
 Polar3D.h:321
 Polar3D.h:322
 Polar3D.h:323