Belle II Software  release-05-01-25
SinEqLine.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2014 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/numerics/Modulo.h>
13 
14 #include <tracking/trackFindingCDC/geometry/Line2D.h>
15 #include <tracking/trackFindingCDC/geometry/Vector2D.h>
16 
17 #include <tracking/trackFindingCDC/numerics/EIncDec.h>
18 
19 #include <cmath>
20 
21 namespace Belle2 {
27  namespace TrackFindingCDC {
28 
38  class SinEqLine {
39 
40  public:
42  SinEqLine() :
43  m_slope(0.0),
44  m_intercept(0.0)
45  {}
46 
47 
49  explicit SinEqLine(const Line2D& line2D) :
50  m_slope(line2D.slope()),
51  m_intercept(line2D.intercept())
52  {}
53 
55  SinEqLine(const double slope, const double intercept) :
56  m_slope(slope),
57  m_intercept(intercept)
58  {}
59 
60 
62  double map(const double x) const
63  { return sin(x) - getSlope() * x - getIntercept(); }
64 
66  double gradient(const double x) const
67  { return cos(x) - getSlope(); }
68 
70  int getIHalfPeriod(const double x) const
71  { return floor(x / M_PI); }
72 
73  /* Computes the positive solution that has the smallest value of x.
74  The additional parameter serves as a criterion to abbort the search if the solutions is further away than the specified half period.
75  */
76  double computeSmallestPositiveRoot(int maxIHalfPeriod = 5) const;
77 
78 
80  double computeRootLargerThanExtemumInHalfPeriod(int iHalfPeriod) const;
81 
83  double computeRootForLargeSlope() const;
84 
86  double computeRootInInterval(double lowerX, double upperX) const;
87 
88  private:
90  double newtonX(const Vector2D& pos) const;
91 
93  static double secantX(const Vector2D& lower, const Vector2D& upper);
94 
96  static double middleX(const Vector2D& lower, const Vector2D& upper);
97 
99  static bool updateBounds(Vector2D& lower, Vector2D& upper, const Vector2D& next);
100 
102  static bool isBetween(const Vector2D& lower, const Vector2D& next, const Vector2D& upper)
103  { return lower.x() < next.x() and next.x() < upper.x(); }
104 
106  static bool isConverged(const Vector2D& lower, const Vector2D& upper)
107  {
108  return fabs(lower.y()) < 10e-7 or fabs(upper.y()) < 10e-7;
109  }
110 
112  static double getConvergedBound(const Vector2D& lower, const Vector2D& upper)
113  {
114  if (not std::isfinite(lower.y()) or not std::isfinite(upper.y())) {
115  return NAN;
116  }
117 
118  if (fabs(lower.y()) <= fabs(upper.y())) {
119  return lower.x();
120  }
121 
122  if (fabs(lower.y()) > fabs(upper.y())) {
123  return upper.x();
124  }
125 
126  return NAN;
127  }
128 
129  public:
131  static bool changesSign(const Vector2D& lower, const Vector2D& upper)
132  { return (lower.y() > 0 and upper.y() < 0) or (lower.y() < 0 and upper.y() > 0); }
133 
135  static EIncDec getEIncDec(const Vector2D& lower, const Vector2D& upper)
136  {
137  if (lower.y() < upper.y()) {
138  return EIncDec::c_Increasing;
139  } else if (lower.y() > upper.y()) {
140  return EIncDec::c_Decreasing;
141  } else if (lower.y() == upper.y()) {
142  return EIncDec::c_Constant;
143  } else {
144  return EIncDec::c_Invalid;
145  }
146  }
147 
148  public:
150  double computeExtremumXInHalfPeriod(int iHalfPeriod) const;
151 
153  static int getIPeriodFromIHalfPeriod(int iHalfPeriod)
154  { return isEven(iHalfPeriod) ? iHalfPeriod / 2 : (iHalfPeriod - 1) / 2; }
155 
156  public:
158  bool hasLargeSlope() const
159  { return fabs(getSlope()) >= 1; }
160 
162  double getSlope() const
163  { return m_slope; }
164 
166  double getIntercept() const
167  { return m_intercept; }
168 
169  private:
171  double m_slope;
172 
174  double m_intercept;
175 
176 
177  };
178 
179  }
181 }
182 
Belle2::TrackFindingCDC::SinEqLine::getSlope
double getSlope() const
Getter for the slope.
Definition: SinEqLine.h:170
Belle2::TrackFindingCDC::SinEqLine::m_intercept
double m_intercept
Memory for the intercept.
Definition: SinEqLine.h:182
Belle2::TrackFindingCDC::SinEqLine::changesSign
static bool changesSign(const Vector2D &lower, const Vector2D &upper)
Checks if the function changes sign in the intervall.
Definition: SinEqLine.h:139
Belle2::TrackFindingCDC::SinEqLine::isConverged
static bool isConverged(const Vector2D &lower, const Vector2D &upper)
Check if the intervall has shrunk close enough to the solution.
Definition: SinEqLine.h:114
Belle2::TrackFindingCDC::SinEqLine::m_slope
double m_slope
Memory for the slope.
Definition: SinEqLine.h:179
Belle2::TrackFindingCDC::SinEqLine::newtonX
double newtonX(const Vector2D &pos) const
Shrinking method of the newton algorithm return the next candidate root.
Definition: SinEqLine.cc:147
Belle2::TrackFindingCDC::Vector2D
A two dimensional vector which is equipped with functions for correct handeling of orientation relat...
Definition: Vector2D.h:37
Belle2::TrackFindingCDC::SinEqLine::getIPeriodFromIHalfPeriod
static int getIPeriodFromIHalfPeriod(int iHalfPeriod)
Helper function to translate the index of the half period to index of the containing period.
Definition: SinEqLine.h:161
Belle2::TrackFindingCDC::NIncDec::EIncDec
EIncDec
Enumeration to represent the distinct possibilities of the right left passage information.
Definition: EIncDec.h:35
Belle2::TrackFindingCDC::Vector2D::y
double y() const
Getter for the y coordinate.
Definition: Vector2D.h:619
Belle2::TrackFindingCDC::SinEqLine::updateBounds
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:152
Belle2::TrackFindingCDC::SinEqLine::map
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:70
Belle2::TrackFindingCDC::SinEqLine::isBetween
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:110
Belle2::TrackFindingCDC::SinEqLine::gradient
double gradient(const double x) const
Interpreting as the function f this method calculates the gradient as need in Newtons algorithms.
Definition: SinEqLine.h:74
Belle2::TrackFindingCDC::SinEqLine::computeSmallestPositiveRoot
double computeSmallestPositiveRoot(int maxIHalfPeriod=5) const
Definition: SinEqLine.cc:19
Belle2::TrackFindingCDC::SinEqLine::getIntercept
double getIntercept() const
Getter for the intercept.
Definition: SinEqLine.h:174
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::SinEqLine::middleX
static double middleX(const Vector2D &lower, const Vector2D &upper)
Simple fall back shrinking method using trivial devision of the intervall.
Definition: SinEqLine.cc:137
Belle2::TrackFindingCDC::SinEqLine::secantX
static double secantX(const Vector2D &lower, const Vector2D &upper)
Fall back shrinking method to the secant algorithm.
Definition: SinEqLine.cc:142
Belle2::TrackFindingCDC::SinEqLine::computeRootLargerThanExtemumInHalfPeriod
double computeRootLargerThanExtemumInHalfPeriod(int iHalfPeriod) const
Computes the solution that is addressed by the given half period index.
Definition: SinEqLine.cc:40
Belle2::TrackFindingCDC::SinEqLine::hasLargeSlope
bool hasLargeSlope() const
Indicates that the slope is so large such that the function has no local exterma.
Definition: SinEqLine.h:166
Belle2::TrackFindingCDC::SinEqLine::computeRootInInterval
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:69
Belle2::TrackFindingCDC::Vector2D::x
double x() const
Getter for the x coordinate.
Definition: Vector2D.h:609
Belle2::TrackFindingCDC::Line2D
A two dimensional normal line.
Definition: Line2D.h:47
Belle2::TrackFindingCDC::SinEqLine::getEIncDec
static EIncDec getEIncDec(const Vector2D &lower, const Vector2D &upper)
Determines if the function is increasing or decreasing in the intervall.
Definition: SinEqLine.h:143
Belle2::TrackFindingCDC::SinEqLine::getConvergedBound
static double getConvergedBound(const Vector2D &lower, const Vector2D &upper)
Returns the better solution x from the bounds of the intervall.
Definition: SinEqLine.h:120
Belle2::TrackFindingCDC::SinEqLine::computeRootForLargeSlope
double computeRootForLargeSlope() const
Compute single solution in the case that fabs(slope) >= 1.
Definition: SinEqLine.cc:52
Belle2::TrackFindingCDC::SinEqLine::SinEqLine
SinEqLine()
Default constructor initializing slope and intercept to zero.
Definition: SinEqLine.h:50
Belle2::TrackFindingCDC::SinEqLine::computeExtremumXInHalfPeriod
double computeExtremumXInHalfPeriod(int iHalfPeriod) const
Get the local extermum that is located in the half period indicated by the given index.
Definition: SinEqLine.cc:188
Belle2::TrackFindingCDC::SinEqLine::getIHalfPeriod
int getIHalfPeriod(const double x) const
Returns the half period index in which the x position is located.
Definition: SinEqLine.h:78