CMS 3D CMS Logo

OnePiRange.h
Go to the documentation of this file.
1 #ifndef GeometryVector_Geom_OnePiRange_h
2 #define GeometryVector_Geom_OnePiRange_h
3 
5 #include <cmath>
6 
7 namespace Geom {
8 
19  template <class T>
20  class OnePiRange {
21  public:
22 
25 
27  explicit OnePiRange( const T& val) : theValue(val) { normalize(); }
28 
30  operator T() const { return theValue;}
31 
33  template <class T1> operator OnePiRange<T1>() { return OnePiRange<T1>(theValue);}
34 
36  T value() const { return theValue;}
37 
38  // Standard arithmetics
39  OnePiRange& operator+=(const T& a) {theValue+=a; normalize(); return *this;}
40  OnePiRange& operator+=(const OnePiRange& a) {return operator+=(a.value());}
41 
42  OnePiRange& operator-=(const T& a) {theValue-=a; normalize(); return *this;}
43  OnePiRange& operator-=(const OnePiRange& a) {return operator-=(a.value());}
44 
45  OnePiRange& operator*=(const T& a) {theValue*=a; normalize(); return *this;}
46 
47  OnePiRange& operator/=(const T& a) {theValue/=a; normalize(); return *this;}
48 
49  T degrees() const { return theValue*180./pi();}
50 
52  // No need to handle 0 or pi; in this case "inf" is returned.
53  T eta() const { return -log(tan(theValue/2.)); }
54 
55 
56  private:
57 
59 
60  void normalize() {
61  if( theValue > (T) pi() || theValue < 0) {
62  theValue = fmod( theValue, (T) pi());
63  }
64  if (theValue < 0.) theValue += pi();
65  }
66 
67  };
68 
70  template <class T>
71  inline OnePiRange<T> operator-(const OnePiRange<T>& a) {return OnePiRange<T>(-a.value());}
72 
73 
75  template <class T>
77  return OnePiRange<T>(a) += b;
78  }
80  template <class T, class Scalar>
81  inline OnePiRange<T> operator+(const OnePiRange<T>& a, const Scalar& b) {
82  return OnePiRange<T>(a) += b;
83  }
85  template <class T, class Scalar>
86  inline OnePiRange<T> operator+(const Scalar& a, const OnePiRange<T>& b) {
87  return OnePiRange<T>(b) += a;
88  }
89 
91  template <class T>
93  return OnePiRange<T>(a) -= b;
94  }
95 
97  template <class T, class Scalar>
98  inline OnePiRange<T> operator-(const OnePiRange<T>& a, const Scalar& b) {
99  return OnePiRange<T>(a) -= b;
100  }
101 
103  template <class T, class Scalar>
104  inline OnePiRange<T> operator-(const Scalar& a, const OnePiRange<T>& b) {
105  return OnePiRange<T>(a - b.value()); // use of unary operators would normalize twice
106  }
107 
109  template <class T, class Scalar>
110  inline OnePiRange<T> operator*(const OnePiRange<T>& a, const Scalar& b) {
111  return OnePiRange<T>(a) *= b;
112  }
113 
115  template <class T>
116  inline OnePiRange<T> operator*(double a, const OnePiRange<T>& b) {
117  return OnePiRange<T>(b) *= a;
118  }
119 
121  template <class T>
122  inline T operator/(const OnePiRange<T>& a, const OnePiRange<T>& b) {
123  return a.value() / b.value();
124  }
125 
127  template <class T>
128  inline OnePiRange<T> operator/(const OnePiRange<T>& a, double b) {
129  return OnePiRange<T>(a) /= b;
130  }
131 
132 }
133 #endif
Geom::OnePiRange::operator+=
OnePiRange & operator+=(const T &a)
Definition: OnePiRange.h:39
Geom::operator+
OnePiRange< T > operator+(const OnePiRange< T > &a, const OnePiRange< T > &b)
Addition.
Definition: OnePiRange.h:76
Geom::OnePiRange::theValue
T theValue
Definition: OnePiRange.h:58
align::Scalar
double Scalar
Definition: Definitions.h:25
Geom::OnePiRange::operator/=
OnePiRange & operator/=(const T &a)
Definition: OnePiRange.h:47
Geom::pi
constexpr double pi()
Definition: Pi.h:31
Geom::OnePiRange::degrees
T degrees() const
Definition: OnePiRange.h:49
Geom::OnePiRange::normalize
void normalize()
Definition: OnePiRange.h:60
Geom::OnePiRange::operator-=
OnePiRange & operator-=(const T &a)
Definition: OnePiRange.h:42
b
double b
Definition: hdecay.h:118
Geom::OnePiRange::operator*=
OnePiRange & operator*=(const T &a)
Definition: OnePiRange.h:45
Geom::operator*
OnePiRange< T > operator*(const OnePiRange< T > &a, const Scalar &b)
Multiplication with scalar, does not change the precision.
Definition: OnePiRange.h:110
a
double a
Definition: hdecay.h:119
Geom::operator-
OnePiRange< T > operator-(const OnePiRange< T > &a)
Definition: OnePiRange.h:71
Pi.h
funct::tan
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Geom
Definition: CoordinateSets.h:6
Geom::OnePiRange::eta
T eta() const
Return the pseudorapidity.
Definition: OnePiRange.h:53
Geom::OnePiRange::OnePiRange
OnePiRange()
Default constructor does not initialise - just as double.
Definition: OnePiRange.h:24
heppy_batch.val
val
Definition: heppy_batch.py:351
Geom::OnePiRange::value
T value() const
Explicit access to value in case implicit conversion not OK.
Definition: OnePiRange.h:36
T
long double T
Definition: Basic3DVectorLD.h:48
Geom::operator/
T operator/(const OnePiRange< T > &a, const OnePiRange< T > &b)
Division.
Definition: OnePiRange.h:122
Geom::OnePiRange::operator-=
OnePiRange & operator-=(const OnePiRange &a)
Definition: OnePiRange.h:43
dqm-mbProfile.log
log
Definition: dqm-mbProfile.py:17
Geom::OnePiRange::OnePiRange
OnePiRange(const T &val)
Constructor from T, does not provide automatic conversion.
Definition: OnePiRange.h:27
Geom::OnePiRange::operator+=
OnePiRange & operator+=(const OnePiRange &a)
Definition: OnePiRange.h:40
Geom::OnePiRange
Definition: OnePiRange.h:20