test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Phi.h
Go to the documentation of this file.
1 #ifndef GeometryVector_Geom_Phi_h
2 #define GeometryVector_Geom_Phi_h
3 
5 #include <cmath>
6 
7 namespace Geom {
8 
19  template <class T>
20  class Phi {
21  public:
22 
24  Phi() {}
25 
29  explicit Phi( const T& val) : theValue(val) { normalize();}
30 
32  operator T() const { return theValue;}
33 
35  template <class T1> operator Phi<T1>() { return Phi<T1>(theValue);}
36 
38  T value() const { return theValue;}
39 
40  // so that template classes expecting phi() works! (deltaPhi)
41  T phi() const { return theValue;}
42 
44  Phi& operator+=(const T& a) {theValue+=a; normalize(); return *this;}
45  Phi& operator+=(const Phi& a) {return operator+=(a.value());}
46 
47  Phi& operator-=(const T& a) {theValue-=a; normalize(); return *this;}
48  Phi& operator-=(const Phi& a) {return operator-=(a.value());}
49 
50  Phi& operator*=(const T& a) {theValue*=a; normalize(); return *this;}
51 
52  Phi& operator/=(const T& a) {theValue/=a; normalize(); return *this;}
53 
54  T degrees() const { return theValue*T(180.)/pi();}
55 
56  private:
57 
59 
60  void normalize() {
61  if( theValue > twoPi() || theValue < -twoPi()) {
62  theValue = std::fmod( theValue, (T) twoPi());
63  }
64  if (theValue <= -pi()) theValue += twoPi();
65  if (theValue > pi()) theValue -= twoPi();
66  }
67 
68  };
69 
71  template <class T>
72  inline Phi<T> operator-(const Phi<T>& a) {return Phi<T>(-a.value());}
73 
74 
76  template <class T>
77  inline Phi<T> operator+(const Phi<T>& a, const Phi<T>& b) {
78  return Phi<T>(a) += b;
79  }
81  template <class T, class Scalar>
82  inline Phi<T> operator+(const Phi<T>& a, const Scalar& b) {
83  return Phi<T>(a) += b;
84  }
86  template <class T, class Scalar>
87  inline Phi<T> operator+(const Scalar& a, const Phi<T>& b) {
88  return Phi<T>(b) += a;
89  }
90 
91 
93  template <class T>
94  inline Phi<T> operator-(const Phi<T>& a, const Phi<T>& b) {
95  return Phi<T>(a) -= b;
96  }
98  template <class T, class Scalar>
99  inline Phi<T> operator-(const Phi<T>& a, const Scalar& b) {
100  return Phi<T>(a) -= b;
101  }
103  template <class T, class Scalar>
104  inline Phi<T> operator-(const Scalar& a, const Phi<T>& b) {
105  return Phi<T>(a - b.value()); // use of unary operators would normalize twice
106  }
107 
108 
110  template <class T, class Scalar>
111  inline Phi<T> operator*(const Phi<T>& a, const Scalar& b) {
112  return Phi<T>(a) *= b;
113  }
115  template <class T>
116  inline Phi<T> operator*(double a, const Phi<T>& b) {
117  return Phi<T>(b) *= a;
118  }
119 
120 
122  template <class T>
123  inline T operator/(const Phi<T>& a, const Phi<T>& b) {
124  return a.value() / b.value();
125  }
127  template <class T>
128  inline Phi<T> operator/(const Phi<T>& a, double b) {
129  return Phi<T>(a) /= b;
130  }
131 
132 
133 }
134 
135 /*
136 // this a full mess wiht the above that is a mess in itself
137 #include "DataFormats/Math/interface/deltaPhi.h"
138 namespace reco {
139  template <class T1,class T2>
140  inline double deltaPhi(const Geom::Phi<T1> phi1, const Geom::Phi<T2> phi2) {
141  return deltaPhi(static_cast<double>(phi1.value()), static_cast<double>(phi2.value()));
142  }
143 
144  template <class T>
145  inline double deltaPhi(const Geom::Phi<T> phi1, double phi2) {
146  return deltaPhi(static_cast<double>(phi1.value()), phi2);
147  }
148  template <class T>
149  inline double deltaPhi(const Geom::Phi<T> phi1, float phi2) {
150  return deltaPhi(static_cast<double>(phi1.value()), static_cast<double>(phi2));
151  }
152  template <class T>
153  inline double deltaPhi(double phi1, const Geom::Phi<T> phi2) {
154  return deltaPhi(phi1, static_cast<double>(phi2.value()) );
155  }
156  template <class T>
157  inline double deltaPhi(float phi1, const Geom::Phi<T> phi2) {
158  return deltaPhi(static_cast<double>(phi1),static_cast<double>(phi2.value()) );
159  }
160 }
161 */
162 
163 #endif
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
T operator/(const OnePiRange< T > &a, const OnePiRange< T > &b)
Division.
Definition: OnePiRange.h:122
OnePiRange< T > operator-(const OnePiRange< T > &a)
Definition: OnePiRange.h:71
Phi & operator-=(const Phi &a)
Definition: Phi.h:48
void normalize()
Definition: Phi.h:60
Phi & operator-=(const T &a)
Definition: Phi.h:47
double Scalar
Definition: Definitions.h:27
Phi & operator/=(const T &a)
Definition: Phi.h:52
Phi()
Default constructor does not initialise - just as double.
Definition: Phi.h:24
Phi & operator+=(const Phi &a)
Definition: Phi.h:45
T phi() const
Definition: Phi.h:41
OnePiRange< T > operator+(const OnePiRange< T > &a, const OnePiRange< T > &b)
Addition.
Definition: OnePiRange.h:76
T theValue
Definition: Phi.h:58
Phi(const T &val)
Definition: Phi.h:29
OnePiRange< T > operator*(const OnePiRange< T > &a, const Scalar &b)
Multiplication with scalar, does not change the precision.
Definition: OnePiRange.h:110
T value() const
Explicit access to value in case implicit conversion not OK.
Definition: Phi.h:38
Phi & operator+=(const T &a)
Standard arithmetics.
Definition: Phi.h:44
double b
Definition: hdecay.h:120
double a
Definition: hdecay.h:121
Phi & operator*=(const T &a)
Definition: Phi.h:50
T degrees() const
Definition: Phi.h:54
constexpr double pi()
Definition: Pi.h:31
long double T
constexpr double twoPi()
Definition: Pi.h:32
Definition: Phi.h:20