Belle II Software  release-08-01-10
SinEqLine.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <tracking/trackFindingCDC/numerics/Modulo.h>
11 
12 #include <tracking/trackFindingCDC/geometry/Line2D.h>
13 #include <tracking/trackFindingCDC/geometry/Vector2D.h>
14 
15 #include <tracking/trackFindingCDC/numerics/EIncDec.h>
16 
17 #include <cmath>
18 
19 namespace Belle2 {
25  namespace TrackFindingCDC {
26 
36  class SinEqLine {
37 
38  public:
41  m_slope(0.0),
42  m_intercept(0.0)
43  {}
44 
45 
47  explicit SinEqLine(const Line2D& line2D) :
48  m_slope(line2D.slope()),
49  m_intercept(line2D.intercept())
50  {}
51 
53  SinEqLine(const double slope, const double intercept) :
54  m_slope(slope),
55  m_intercept(intercept)
56  {}
57 
58 
60  double map(const double x) const
61  { return sin(x) - getSlope() * x - getIntercept(); }
62 
64  double gradient(const double x) const
65  { return cos(x) - getSlope(); }
66 
68  int getIHalfPeriod(const double x) const
69  { return floor(x / M_PI); }
70 
71  /* Computes the positive solution that has the smallest value of x.
72  The additional parameter serves as a criterion to abbort the search if the solutions is further away than the specified half period.
73  */
74  double computeSmallestPositiveRoot(int maxIHalfPeriod = 5) const;
75 
76 
78  double computeRootLargerThanExtemumInHalfPeriod(int iHalfPeriod) const;
79 
81  double computeRootForLargeSlope() const;
82 
84  double computeRootInInterval(double lowerX, double upperX) const;
85 
86  private:
88  double newtonX(const Vector2D& pos) const;
89 
91  static double secantX(const Vector2D& lower, const Vector2D& upper);
92 
94  static double middleX(const Vector2D& lower, const Vector2D& upper);
95 
97  static bool updateBounds(Vector2D& lower, Vector2D& upper, const Vector2D& next);
98 
100  static bool isBetween(const Vector2D& lower, const Vector2D& next, const Vector2D& upper)
101  { return lower.x() < next.x() and next.x() < upper.x(); }
102 
104  static bool isConverged(const Vector2D& lower, const Vector2D& upper)
105  {
106  return fabs(lower.y()) < 10e-7 or fabs(upper.y()) < 10e-7;
107  }
108 
110  static double getConvergedBound(const Vector2D& lower, const Vector2D& upper)
111  {
112  if (not std::isfinite(lower.y()) or not std::isfinite(upper.y())) {
113  return NAN;
114  }
115 
116  if (fabs(lower.y()) <= fabs(upper.y())) {
117  return lower.x();
118  }
119 
120  if (fabs(lower.y()) > fabs(upper.y())) {
121  return upper.x();
122  }
123 
124  return NAN;
125  }
126 
127  public:
129  static bool changesSign(const Vector2D& lower, const Vector2D& upper)
130  { return (lower.y() > 0 and upper.y() < 0) or (lower.y() < 0 and upper.y() > 0); }
131 
133  static EIncDec getEIncDec(const Vector2D& lower, const Vector2D& upper)
134  {
135  if (lower.y() < upper.y()) {
136  return EIncDec::c_Increasing;
137  } else if (lower.y() > upper.y()) {
138  return EIncDec::c_Decreasing;
139  } else if (lower.y() == upper.y()) {
140  return EIncDec::c_Constant;
141  } else {
142  return EIncDec::c_Invalid;
143  }
144  }
145 
146  public:
148  double computeExtremumXInHalfPeriod(int iHalfPeriod) const;
149 
151  static int getIPeriodFromIHalfPeriod(int iHalfPeriod)
152  { return isEven(iHalfPeriod) ? iHalfPeriod / 2 : (iHalfPeriod - 1) / 2; }
153 
154  public:
156  bool hasLargeSlope() const
157  { return fabs(getSlope()) >= 1; }
158 
160  double getSlope() const
161  { return m_slope; }
162 
164  double getIntercept() const
165  { return m_intercept; }
166 
167  private:
169  double m_slope;
170 
172  double m_intercept;
173 
174 
175  };
176 
177  }
179 }
180 
A two dimensional normal line.
Definition: Line2D.h:37
Helper class to calculate roots for the function f(x) = sin x - slope * x - intercept.
Definition: SinEqLine.h:36
static bool isBetween(const Vector2D &lower, const Vector2D &next, const Vector2D &upper)
Check is next position is within the intervall given by lower and upper.
Definition: SinEqLine.h:100
static EIncDec getEIncDec(const Vector2D &lower, const Vector2D &upper)
Determines if the function is increasing or decreasing in the intervall.
Definition: SinEqLine.h:133
double getIntercept() const
Getter for the intercept.
Definition: SinEqLine.h:164
double computeExtremumXInHalfPeriod(int iHalfPeriod) const
Get the local extermum that is located in the half period indicated by the given index.
Definition: SinEqLine.cc:185
double m_intercept
Memory for the intercept.
Definition: SinEqLine.h:172
static bool updateBounds(Vector2D &lower, Vector2D &upper, const Vector2D &next)
Replaces the lower or upper bound inplace if the next candidate position is valid and within the inte...
Definition: SinEqLine.cc:149
SinEqLine(const double slope, const double intercept)
Constructor taking the slope and intercept of the line that shall be superimposed with the sin curve.
Definition: SinEqLine.h:53
bool hasLargeSlope() const
Indicates that the slope is so large such that the function has no local exterma.
Definition: SinEqLine.h:156
double computeRootInInterval(double lowerX, double upperX) const
Computes the solution in between the given x values. The x values are generally choosen consecutive l...
Definition: SinEqLine.cc:67
double getSlope() const
Getter for the slope.
Definition: SinEqLine.h:160
double computeSmallestPositiveRoot(int maxIHalfPeriod=5) const
Definition: SinEqLine.cc:17
static double secantX(const Vector2D &lower, const Vector2D &upper)
Fall back shrinking method to the secant algorithm.
Definition: SinEqLine.cc:139
double computeRootForLargeSlope() const
Compute single solution in the case that fabs(slope) >= 1.
Definition: SinEqLine.cc:50
double newtonX(const Vector2D &pos) const
Shrinking method of the newton algorithm return the next candidate root.
Definition: SinEqLine.cc:144
double computeRootLargerThanExtemumInHalfPeriod(int iHalfPeriod) const
Computes the solution that is addressed by the given half period index.
Definition: SinEqLine.cc:38
double m_slope
Memory for the slope.
Definition: SinEqLine.h:169
static bool isConverged(const Vector2D &lower, const Vector2D &upper)
Check if the intervall has shrunk close enough to the solution.
Definition: SinEqLine.h:104
SinEqLine()
Default constructor initializing slope and intercept to zero.
Definition: SinEqLine.h:40
int getIHalfPeriod(const double x) const
Returns the half period index in which the x position is located.
Definition: SinEqLine.h:68
static int getIPeriodFromIHalfPeriod(int iHalfPeriod)
Helper function to translate the index of the half period to index of the containing period.
Definition: SinEqLine.h:151
double gradient(const double x) const
Interpreting as the function f this method calculates the gradient as need in Newtons algorithms.
Definition: SinEqLine.h:64
static double middleX(const Vector2D &lower, const Vector2D &upper)
Simple fall back shrinking method using trivial devision of the intervall.
Definition: SinEqLine.cc:134
double map(const double x) const
Interpreting as the function f this method carries out the translation from x to y coordinates.
Definition: SinEqLine.h:60
static double getConvergedBound(const Vector2D &lower, const Vector2D &upper)
Returns the better solution x from the bounds of the intervall.
Definition: SinEqLine.h:110
SinEqLine(const Line2D &line2D)
Constructor taking the line that shall be superimposed with the sin curve.
Definition: SinEqLine.h:47
static bool changesSign(const Vector2D &lower, const Vector2D &upper)
Checks if the function changes sign in the intervall.
Definition: SinEqLine.h:129
A two dimensional vector which is equipped with functions for correct handeling of orientation relat...
Definition: Vector2D.h:35
double x() const
Getter for the x coordinate.
Definition: Vector2D.h:607
double y() const
Getter for the y coordinate.
Definition: Vector2D.h:617
EIncDec
Enumeration to represent the distinct possibilities of the right left passage information.
Definition: EIncDec.h:25
Abstract base class for different kinds of events.