CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/DataFormats/GeometryVector/interface/Phi.h

Go to the documentation of this file.
00001 #ifndef GeometryVector_Geom_Phi_h
00002 #define GeometryVector_Geom_Phi_h
00003 
00004 #include "DataFormats/GeometryVector/interface/Pi.h"
00005 #include <cmath>
00006 
00007 namespace Geom {
00008 
00019   template <class T>
00020   class Phi {
00021   public:
00022 
00024     Phi() {}
00025 
00029     explicit Phi( const T& val) : theValue(val) { normalize();}
00030 
00032     operator T() const { return theValue;}
00033 
00035     template <class T1> operator Phi<T1>() { return Phi<T1>(theValue);}
00036 
00038     T value() const { return theValue;}
00039 
00040     // so that template classes expecting phi() works! (deltaPhi)
00041     T phi() const { return theValue;}
00042 
00044     Phi& operator+=(const T& a) {theValue+=a; normalize(); return *this;}
00045     Phi& operator+=(const Phi& a) {return operator+=(a.value());}
00046 
00047     Phi& operator-=(const T& a) {theValue-=a; normalize(); return *this;}
00048     Phi& operator-=(const Phi& a) {return operator-=(a.value());}
00049 
00050     Phi& operator*=(const T& a) {theValue*=a; normalize(); return *this;}
00051 
00052     Phi& operator/=(const T& a) {theValue/=a; normalize(); return *this;}
00053 
00054     T degrees() const { return theValue*180./pi();}
00055 
00056   private:
00057 
00058     T theValue;
00059 
00060     void normalize() { 
00061       if( theValue > twoPi() || theValue < -twoPi()) {
00062         theValue = fmod( theValue, (T) twoPi());
00063       }
00064       if (theValue <= -pi()) theValue += twoPi();
00065       if (theValue >  pi()) theValue -= twoPi();
00066     }
00067 
00068   };
00069 
00071   template <class T>
00072   inline Phi<T> operator-(const Phi<T>& a) {return Phi<T>(-a.value());}
00073 
00074 
00076   template <class T>
00077   inline Phi<T> operator+(const Phi<T>& a, const Phi<T>& b) {
00078     return Phi<T>(a) += b;
00079   }
00081   template <class T, class Scalar>
00082   inline Phi<T> operator+(const Phi<T>& a, const Scalar& b) {
00083     return Phi<T>(a) += b;
00084   }
00086   template <class T, class Scalar>
00087   inline Phi<T> operator+(const Scalar& a, const Phi<T>& b) {
00088     return Phi<T>(b) += a;
00089   }
00090 
00091 
00093   template <class T>
00094   inline Phi<T> operator-(const Phi<T>& a, const Phi<T>& b) { 
00095     return Phi<T>(a) -= b;
00096   }
00098   template <class T, class Scalar>
00099   inline Phi<T> operator-(const Phi<T>& a, const Scalar& b) { 
00100     return Phi<T>(a) -= b;
00101   }
00103   template <class T, class Scalar>
00104   inline Phi<T> operator-(const Scalar& a, const Phi<T>& b) { 
00105     return Phi<T>(a - b.value());  // use of unary operators would normalize twice
00106   }
00107 
00108 
00110   template <class T, class Scalar>
00111   inline Phi<T> operator*(const Phi<T>& a, const Scalar& b) {
00112     return Phi<T>(a) *= b;
00113   }
00115   template <class T>
00116   inline Phi<T> operator*(double a, const Phi<T>& b) {
00117     return Phi<T>(b) *= a;
00118   }
00119 
00120 
00122   template <class T>
00123   inline T operator/(const Phi<T>& a, const Phi<T>& b) { 
00124     return a.value() / b.value();
00125   }
00127   template <class T>
00128   inline Phi<T> operator/(const Phi<T>& a, double b) {
00129     return Phi<T>(a) /= b;
00130   }
00131 
00132 
00133 }
00134 
00135 /*
00136 // this a full mess wiht the above that is a mess in itself
00137 #include "DataFormats/Math/interface/deltaPhi.h"
00138 namespace reco {
00139   template <class T1,class T2>
00140   inline double deltaPhi(const Geom::Phi<T1> phi1, const Geom::Phi<T2> phi2) {
00141     return deltaPhi(static_cast<double>(phi1.value()), static_cast<double>(phi2.value()));
00142   }
00143  
00144   template <class T>
00145   inline double deltaPhi(const Geom::Phi<T> phi1, double phi2) {
00146     return deltaPhi(static_cast<double>(phi1.value()), phi2);
00147   }
00148   template <class T>
00149   inline double deltaPhi(const Geom::Phi<T> phi1, float phi2) {
00150     return deltaPhi(static_cast<double>(phi1.value()), static_cast<double>(phi2));
00151   }
00152   template <class T>
00153   inline double deltaPhi(double phi1, const Geom::Phi<T>  phi2) {
00154     return deltaPhi(phi1, static_cast<double>(phi2.value()) );
00155   }
00156   template <class T>
00157   inline double deltaPhi(float phi1, const Geom::Phi<T>  phi2) {
00158     return deltaPhi(static_cast<double>(phi1),static_cast<double>(phi2.value()) );
00159   }
00160 }
00161 */
00162 
00163 #endif
00164 
00165 
00166 
00167 
00168 
00169 
00170 
00171 
00172 
00173 
00174