Belle II Software  release-05-01-25
Chi2MinimumFinder1D.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marko Staric *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <top/utilities/Chi2MinimumFinder1D.h>
12 
13 #include <framework/logging/Logger.h>
14 
15 #include <math.h>
16 
17 namespace Belle2 {
22  namespace TOP {
23 
24  Chi2MinimumFinder1D::Chi2MinimumFinder1D(int nbins, double xmin, double xmax):
25  m_xmin(xmin), m_xmax(xmax)
26  {
27  if (nbins <= 0) {
28  B2ERROR("Chi2MinimumFinder1D: nbins must be positive integer");
29  return;
30  }
31  if (m_xmax <= m_xmin) {
32  B2ERROR("Chi2MinimumFinder1D: search range max < min");
33  return;
34  }
35  m_dx = (m_xmax - m_xmin) / nbins;
36  double x = m_xmin + m_dx / 2;
37  while (x < m_xmax) {
38  m_x.push_back(x);
39  x += m_dx;
40  }
41  m_chi2.resize(m_x.size(), 0);
42  }
43 
44  Chi2MinimumFinder1D::Chi2MinimumFinder1D(const std::shared_ptr<TH1D> h)
45  {
46  m_xmin = h->GetXaxis()->GetXmin();
47  m_xmax = h->GetXaxis()->GetXmax();
48  m_dx = h->GetBinWidth(1);
49  for (int i = 0; i < h->GetNbinsX(); i++) {
50  m_x.push_back(h->GetBinCenter(i + 1));
51  m_chi2.push_back(h->GetBinContent(i + 1));
52  }
53  m_entries = h->GetEntries() / h->GetNbinsX();
54  }
55 
57  {
58  for (auto& chi2 : m_chi2) chi2 = 0;
59  m_entries = 0;
60  m_searched = false;
61  }
62 
63 
64  void Chi2MinimumFinder1D::add(unsigned i, double chi2)
65  {
66  if (i < m_chi2.size()) {
67  m_chi2[i] += chi2;
68  m_searched = false;
69  if (i == 0) m_entries++;
70  } else {
71  B2WARNING("Chi2MinimumFinder1D::add: index out of range");
72  }
73  }
74 
75 
77  {
78  if (other.getXmin() != m_xmin or other.getXmax() != m_xmax or
79  other.getBinCenters().size() != m_x.size()) {
80  B2ERROR("Chi2MinimumFinder1D::add: finders with different ranges or binning "
81  "can't be added");
82  return *this;
83  }
84  const auto& chi2 = other.getChi2Values();
85  for (unsigned i = 0; i < m_chi2.size(); i++) {
86  m_chi2[i] += chi2[i];
87  }
88  m_entries += other.getEntries();
89  m_searched = false;
90  return *this;
91  }
92 
93 
95  {
96  if (m_chi2.size() < 3) {
97  m_minimum.position = (m_xmin + m_xmax) / 2;
98  m_minimum.error = (m_xmax - m_xmin) / 2;
99  return;
100  }
101 
102  unsigned i0 = 1;
103  for (unsigned i = i0; i < m_chi2.size() - 1; i++) {
104  if (m_chi2[i] < m_chi2[i0]) i0 = i;
105  }
106  auto x = getMinimum(m_chi2[i0 - 1], m_chi2[i0], m_chi2[i0 + 1]);
107  m_minimum = Minimum(m_x[i0] + x.position * m_dx, x.error * m_dx, x.chi2, x.valid);
108  }
109 
110 
112  double yCenter,
113  double yRight) const
114  {
115  // parabola: y = ax^2 + bx + c
116  double DL = yLeft - yCenter;
117  double DR = yRight - yCenter;
118  double A = (DR + DL) / 2;
119  if (A <= 0) return Minimum(0, 0, yCenter, false);
120  double B = (DR - DL) / 2;
121  double x = - B / 2 / A;
122  double chi2_min = A * x * x + B * x + yCenter;
123  return Minimum(x, sqrt(1 / A), chi2_min, true);
124  }
125 
126 
127  TH1F Chi2MinimumFinder1D::getHistogram(std::string name, std::string title) const
128  {
129  TH1F h(name.c_str(), title.c_str(), m_x.size(), m_xmin, m_xmax);
130  for (unsigned i = 0; i < m_chi2.size(); i++) {
131  h.SetBinContent(i + 1, m_chi2[i] - m_minimum.chi2);
132  }
133  return h;
134  }
135 
136  } // namespace TOP
138 } // namespace Belle2
Belle2::TOP::Chi2MinimumFinder1D::m_minimum
Minimum m_minimum
result: global minimum
Definition: Chi2MinimumFinder1D.h:188
Belle2::TOP::Chi2MinimumFinder1D::findMinimum
void findMinimum()
Finds minimum.
Definition: Chi2MinimumFinder1D.cc:102
Belle2::TOP::Chi2MinimumFinder1D::getHistogram
TH1F getHistogram(std::string name, std::string title) const
Returns chi^2 packed into 1D histogram.
Definition: Chi2MinimumFinder1D.cc:135
Belle2::TOP::Chi2MinimumFinder1D::m_dx
double m_dx
bin size
Definition: Chi2MinimumFinder1D.h:182
Belle2::TOP::Chi2MinimumFinder1D::m_chi2
std::vector< double > m_chi2
chi^2 values at bin centers
Definition: Chi2MinimumFinder1D.h:184
Belle2::TOP::Chi2MinimumFinder1D::getEntries
int getEntries() const
Returns number of entries (counted for bin 0)
Definition: Chi2MinimumFinder1D.h:142
Belle2::TOP::Chi2MinimumFinder1D::getBinCenters
const std::vector< double > & getBinCenters() const
Returns vector of bin centers.
Definition: Chi2MinimumFinder1D.h:130
Belle2::TOP::Chi2MinimumFinder1D::Minimum::position
double position
position of the minimum
Definition: Chi2MinimumFinder1D.h:45
Belle2::TOP::Chi2MinimumFinder1D::getXmax
double getXmax() const
Returns upper limit of search region.
Definition: Chi2MinimumFinder1D.h:112
Belle2::TOP::Chi2MinimumFinder1D
Minimum finder using tabulated chi^2 values in one dimension.
Definition: Chi2MinimumFinder1D.h:37
Belle2::TOP::Chi2MinimumFinder1D::getChi2Values
const std::vector< double > & getChi2Values() const
Returns vector of chi^2.
Definition: Chi2MinimumFinder1D.h:136
Belle2::TOP::Chi2MinimumFinder1D::Minimum
Result of minimum finder.
Definition: Chi2MinimumFinder1D.h:44
Belle2::TOP::Chi2MinimumFinder1D::getMinimum
const Minimum & getMinimum()
Returns parabolic minimum.
Definition: Chi2MinimumFinder1D.h:147
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TOP::Chi2MinimumFinder1D::m_entries
int m_entries
number of entries (counted for bin 0)
Definition: Chi2MinimumFinder1D.h:185
Belle2::TOP::Chi2MinimumFinder1D::Chi2MinimumFinder1D
Chi2MinimumFinder1D()
Default constructor.
Definition: Chi2MinimumFinder1D.h:67
Belle2::TOP::Chi2MinimumFinder1D::m_x
std::vector< double > m_x
bin centers
Definition: Chi2MinimumFinder1D.h:183
Belle2::TOP::Chi2MinimumFinder1D::Minimum::error
double error
error on the position
Definition: Chi2MinimumFinder1D.h:46
Belle2::TOP::Chi2MinimumFinder1D::m_xmax
double m_xmax
upper limit of search region
Definition: Chi2MinimumFinder1D.h:180
Belle2::TOP::Chi2MinimumFinder1D::add
void add(unsigned i, double chi2)
Add chi^2 value to bin i.
Definition: Chi2MinimumFinder1D.cc:72
Belle2::TOP::Chi2MinimumFinder1D::m_xmin
double m_xmin
lower limit of search region
Definition: Chi2MinimumFinder1D.h:179
Belle2::TOP::Chi2MinimumFinder1D::clear
void clear()
Set chi^2 values to zero.
Definition: Chi2MinimumFinder1D.cc:64
Belle2::TOP::Chi2MinimumFinder1D::getXmin
double getXmin() const
Returns lower limit of search region.
Definition: Chi2MinimumFinder1D.h:106
Belle2::TOP::Chi2MinimumFinder1D::Minimum::chi2
double chi2
chi2 at minimum
Definition: Chi2MinimumFinder1D.h:47
Belle2::TOP::Chi2MinimumFinder1D::m_searched
bool m_searched
internal flag
Definition: Chi2MinimumFinder1D.h:187