CMS 3D CMS Logo

OnePiRange.h

Go to the documentation of this file.
00001 #ifndef GeometryVector_Geom_OnePiRange_h
00002 #define GeometryVector_Geom_OnePiRange_h
00003 
00004 #include <DataFormats/GeometryVector/interface/Pi.h>
00005 #include <cmath>
00006 
00007 namespace Geom {
00008 
00019   template <class T>
00020   class OnePiRange {
00021   public:
00022 
00024     OnePiRange() {}
00025 
00027     explicit OnePiRange( const T& val) : theValue(val) { normalize(); }
00028 
00030     operator T() const { return theValue;}
00031 
00033     template <class T1> operator OnePiRange<T1>() { return OnePiRange<T1>(theValue);}
00034 
00036     T value() const { return theValue;}
00037 
00038     // Standard arithmetics 
00039     OnePiRange& operator+=(const T& a) {theValue+=a; normalize(); return *this;}
00040     OnePiRange& operator+=(const OnePiRange& a) {return operator+=(a.value());}
00041 
00042     OnePiRange& operator-=(const T& a) {theValue-=a; normalize(); return *this;}
00043     OnePiRange& operator-=(const OnePiRange& a) {return operator-=(a.value());}
00044 
00045     OnePiRange& operator*=(const T& a) {theValue*=a; normalize(); return *this;}
00046 
00047     OnePiRange& operator/=(const T& a) {theValue/=a; normalize(); return *this;}
00048 
00049     T degrees() const { return theValue*180./pi();}
00050 
00052     // No need to handle 0 or pi; in this case "inf" is returned.
00053     T eta() const { return -log(tan(theValue/2.)); } 
00054     
00055 
00056   private:
00057 
00058     T theValue;
00059 
00060     void normalize() {
00061       if( theValue > (T) pi() || theValue < 0) {
00062         theValue = fmod( theValue, (T) pi());   
00063       }
00064       if (theValue <  0.)   theValue += pi();
00065     }
00066 
00067   };
00068 
00070   template <class T>
00071   inline OnePiRange<T> operator-(const OnePiRange<T>& a) {return OnePiRange<T>(-a.value());}
00072 
00073 
00075   template <class T>
00076   inline OnePiRange<T> operator+(const OnePiRange<T>& a, const OnePiRange<T>& b) {
00077     return OnePiRange<T>(a) += b; 
00078   }
00080   template <class T, class Scalar>
00081   inline OnePiRange<T> operator+(const OnePiRange<T>& a, const Scalar& b) {
00082     return OnePiRange<T>(a) += b;
00083   }
00085   template <class T, class Scalar>
00086   inline OnePiRange<T> operator+(const Scalar& a, const OnePiRange<T>& b) {
00087     return OnePiRange<T>(b) += a;
00088   }
00089 
00091   template <class T>
00092   inline OnePiRange<T> operator-(const OnePiRange<T>& a, const OnePiRange<T>& b) { 
00093     return OnePiRange<T>(a) -= b;
00094   }
00095 
00097   template <class T, class Scalar>
00098   inline OnePiRange<T> operator-(const OnePiRange<T>& a, const Scalar& b) { 
00099     return OnePiRange<T>(a) -= b;
00100   }
00101 
00103   template <class T, class Scalar>
00104   inline OnePiRange<T> operator-(const Scalar& a, const OnePiRange<T>& b) { 
00105     return OnePiRange<T>(a - b.value());  // use of unary operators would normalize twice
00106   }
00107 
00109   template <class T, class Scalar>
00110   inline OnePiRange<T> operator*(const OnePiRange<T>& a, const Scalar& b) {
00111     return OnePiRange<T>(a) *= b;
00112   }
00113 
00115   template <class T>
00116   inline OnePiRange<T> operator*(double a, const OnePiRange<T>& b) {
00117     return OnePiRange<T>(b) *= a;
00118   }
00119 
00121   template <class T>
00122   inline T operator/(const OnePiRange<T>& a, const OnePiRange<T>& b) { 
00123     return a.value() / b.value();
00124   }
00125 
00127   template <class T>
00128   inline OnePiRange<T> operator/(const OnePiRange<T>& a, double b) {
00129     return OnePiRange<T>(a) /= b;
00130   }
00131 
00132 }
00133 #endif

Generated on Tue Jun 9 17:30:52 2009 for CMSSW by  doxygen 1.5.4