Belle II Software  release-05-02-19
Angle.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Markus Prim *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <TMath.h>
14 
15 #include <utility>
16 
17 namespace Belle2 {
26  class BaseAngle {
27  public:
32  BaseAngle(double angle, double error) : m_angle(angle), m_error(error) {};
33 
35  virtual ~BaseAngle() {};
36 
39  double getAngle() const { return m_angle; }
40 
43  double getError() const { return m_error; }
44 
47  double getAngleInDeg() const { return m_angle * TMath::RadToDeg(); }
48 
51  double getErrorInDeg() const { return m_error * TMath::RadToDeg(); }
52 
57  double getLowerIntervalBoundary(double sigma = 1) const { return m_angle - sigma * m_error; }
58 
63  double getUpperIntervalBoundary(double sigma = 1) const { return m_angle + sigma * m_error; }
64 
65  protected:
66  typedef std::pair<double, double> Interval;
68  double m_angle;
69  double m_error;
78  bool intervalsCompatible(const Interval& x_interval, const Interval& y_interval) const
79  {
80  const double xCenter = (x_interval.second + x_interval.first) / 2.;
81  const double yCenter = (y_interval.second + y_interval.first) / 2.;
82  const double delta = std::fabs(yCenter - xCenter);
83 
84  if (delta >= 0) {
85  const double xArm = x_interval.second - xCenter;
86  const double yArm = yCenter - y_interval.first;
87  const double sigmaSum = xArm + yArm;
88  if (delta < sigmaSum) return true;
89  return false;
90  } else {
91  const double xArm = xCenter - x_interval.first;
92  const double yArm = y_interval.second - yCenter;
93  const double sigmaSum = xArm + yArm;
94  if (std::fabs(delta) < sigmaSum) return true;
95  return false;
96  }
97 
98  }
99 
100  };
101 
102 
107  class ThetaAngle : public BaseAngle {
108  public:
115  ThetaAngle(double angle, double error) : BaseAngle(angle, error)
116  {
117  m_angle = std::fabs(std::fmod(m_angle, TMath::Pi()));
118  }
119 
124  bool contains(const ThetaAngle& angle) const
125  {
126  return containsIn(angle, 1);
127  }
128 
134  bool containsIn(const ThetaAngle& angle, double sigma) const
135  {
137  const Interval y(angle.getLowerIntervalBoundary(sigma), angle.getUpperIntervalBoundary(sigma));
138 
139  if (intervalsCompatible(x, y)) return true;
140 
141  return false;
142  }
143  };
144 
145 
150  class PhiAngle : public BaseAngle {
151  public:
158  PhiAngle(double angle, double error) : BaseAngle(angle, error)
159  {
160  m_angle = std::fabs(std::fmod(m_angle, TMath::TwoPi()));
161  }
162 
167  bool contains(const PhiAngle& angle) const
168  {
169  return containsIn(angle, 1);
170  }
171 
177  bool containsIn(const PhiAngle& angle, double sigma) const
178  {
179  const double twoPi = TMath::TwoPi();
181  const Interval y(angle.getLowerIntervalBoundary(sigma), angle.getUpperIntervalBoundary(sigma));
182 
183  //Transform intervals
184  const double shift = x.first;
185  const Interval xShifted(x.first - shift, x.second - shift);
186  const Interval yShifted(y.first - shift, y.second - shift);
187 
188  if (twoPi < xShifted.second) return true; // x covers [0, 2Pi)
189 
190  if ((yShifted.first <= 0) and (twoPi < yShifted.second)) return true; // y covers [0, 2Pi)
191 
192  if ((0. <= yShifted.first) and (yShifted.second < twoPi)) {
193  return intervalsCompatible(xShifted, yShifted);
194  }
195 
196  if (!(0. <= yShifted.first) and (yShifted.second < twoPi)) {
197  const Interval y1(y.first + twoPi, twoPi);
198  const Interval y2(0., y.second);
199 
200  if (intervalsCompatible(x, y1)) return true;
201  if (intervalsCompatible(x, y2)) return true;
202  return false;
203  }
204 
205  if ((0 <= yShifted.first) and !(yShifted.second < twoPi)) {
206  const Interval y1(y.first, twoPi);
207  const Interval y2(0., y.second - twoPi);
208 
209  if (intervalsCompatible(x, y1)) return true;
210  if (intervalsCompatible(x, y2)) return true;
211  return false;
212  }
213 
214  return false;
215  }
216  };
218 }
Belle2::BaseAngle::intervalsCompatible
bool intervalsCompatible(const Interval &x_interval, const Interval &y_interval) const
Checks if the intervals overlap at some point.
Definition: Angle.h:86
Belle2::BaseAngle::getUpperIntervalBoundary
double getUpperIntervalBoundary(double sigma=1) const
Getter for the upper interval bound: angle+sigma*error.
Definition: Angle.h:71
Belle2::BaseAngle::getError
double getError() const
Getter for the error of the angle.
Definition: Angle.h:51
Belle2::BaseAngle::m_error
double m_error
Error in rad.
Definition: Angle.h:77
Belle2::PhiAngle::PhiAngle
PhiAngle(double angle, double error)
Constructor using radian units.
Definition: Angle.h:166
Belle2::ThetaAngle
Definition: Angle.h:115
Belle2::PhiAngle
Definition: Angle.h:158
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::BaseAngle::getAngleInDeg
double getAngleInDeg() const
Getter for angle in degrees.
Definition: Angle.h:55
Belle2::ThetaAngle::contains
bool contains(const ThetaAngle &angle) const
Check if two angles are compatible.
Definition: Angle.h:132
Belle2::PhiAngle::containsIn
bool containsIn(const PhiAngle &angle, double sigma) const
Check if two angles are compatible.
Definition: Angle.h:185
Belle2::BaseAngle::BaseAngle
BaseAngle(double angle, double error)
Constructor.
Definition: Angle.h:40
Belle2::BaseAngle::getErrorInDeg
double getErrorInDeg() const
Getter for the error of the angle in degrees.
Definition: Angle.h:59
Belle2::BaseAngle::getAngle
double getAngle() const
Getter for the angle.
Definition: Angle.h:47
Belle2::PhiAngle::contains
bool contains(const PhiAngle &angle) const
Check if two angles are compatible.
Definition: Angle.h:175
Belle2::BaseAngle::m_angle
double m_angle
Angle in rad.
Definition: Angle.h:76
Belle2::ThetaAngle::ThetaAngle
ThetaAngle(double angle, double error)
Constructor using radian units.
Definition: Angle.h:123
Belle2::BaseAngle::~BaseAngle
virtual ~BaseAngle()
Destructor.
Definition: Angle.h:43
Belle2::BaseAngle::getLowerIntervalBoundary
double getLowerIntervalBoundary(double sigma=1) const
Getter for the lower interval bound: angle-sigma*error.
Definition: Angle.h:65
Belle2::BaseAngle::Interval
std::pair< double, double > Interval
Shortcut for std::pair used as interval.
Definition: Angle.h:74
Belle2::ThetaAngle::containsIn
bool containsIn(const ThetaAngle &angle, double sigma) const
Check if two angles are compatible.
Definition: Angle.h:142
Belle2::BaseAngle
Class to compare if two angles are compatible withing a given error range.
Definition: Angle.h:34