Belle II Software development
Chi2MinimumFinder1D Class Reference

Minimum finder using tabulated chi^2 values in one dimension. More...

#include <Chi2MinimumFinder1D.h>

Classes

struct  Minimum
 Result of minimum finder. More...
 

Public Member Functions

 Chi2MinimumFinder1D ()
 Default constructor.
 
 Chi2MinimumFinder1D (int nbins, double xmin, double xmax)
 Class constructor, similar to 1D histogram.
 
 Chi2MinimumFinder1D (const std::shared_ptr< TH1D > h)
 Constructor from a histogram.
 
void clear ()
 Set chi^2 values to zero.
 
void add (unsigned i, double chi2)
 Add chi^2 value to bin i.
 
Chi2MinimumFinder1Dadd (const Chi2MinimumFinder1D &other)
 Add data from another finder Finders must be defined with the same range and binning.
 
double getXmin () const
 Returns lower limit of search region.
 
double getXmax () const
 Returns upper limit of search region.
 
int getNbins () const
 Returns number of bins.
 
double getBinSize () const
 Returns bin (or step) size.
 
const std::vector< double > & getBinCenters () const
 Returns vector of bin centers.
 
const std::vector< double > & getChi2Values () const
 Returns vector of chi^2.
 
double getMinChi2 () const
 Returns minimum chi2 value.
 
int getEntries () const
 Returns number of entries (counted for bin 0)
 
const MinimumgetMinimum ()
 Returns parabolic minimum.
 
TH1F getHistogram (std::string name, std::string title) const
 Returns chi^2 packed into 1D histogram.
 

Private Member Functions

void findMinimum ()
 Finds minimum.
 
Minimum getMinimum (double yLeft, double yCenter, double yRight) const
 Calculates minimum using parabolic interpolation.
 

Private Attributes

double m_xmin = 0
 lower limit of search region
 
double m_xmax = 0
 upper limit of search region
 
double m_dx = 0
 bin size
 
std::vector< double > m_x
 bin centers
 
std::vector< double > m_chi2
 chi^2 values at bin centers
 
int m_entries = 0
 number of entries (counted for bin 0)
 
bool m_searched = false
 internal flag
 
Minimum m_minimum
 result: global minimum
 

Detailed Description

Minimum finder using tabulated chi^2 values in one dimension.

Definition at line 28 of file Chi2MinimumFinder1D.h.

Constructor & Destructor Documentation

◆ Chi2MinimumFinder1D() [1/3]

Chi2MinimumFinder1D ( )
inline

Default constructor.

Definition at line 58 of file Chi2MinimumFinder1D.h.

59 {}

◆ Chi2MinimumFinder1D() [2/3]

Chi2MinimumFinder1D ( int  nbins,
double  xmin,
double  xmax 
)

Class constructor, similar to 1D histogram.

Parameters
nbinsnumber of points the search region is divided to
xminlower limit of the search region
xmaxupper limit of the search region

Definition at line 22 of file Chi2MinimumFinder1D.cc.

22 :
23 m_xmin(xmin), m_xmax(xmax)
24 {
25 if (nbins <= 0) {
26 B2ERROR("Chi2MinimumFinder1D: nbins must be positive integer");
27 return;
28 }
29 if (m_xmax <= m_xmin) {
30 B2ERROR("Chi2MinimumFinder1D: search range max < min");
31 return;
32 }
33 m_dx = (m_xmax - m_xmin) / nbins;
34 double x = m_xmin + m_dx / 2;
35 while (x < m_xmax) {
36 m_x.push_back(x);
37 x += m_dx;
38 }
39 m_chi2.resize(m_x.size(), 0);
40 }
double m_xmin
lower limit of search region
double m_xmax
upper limit of search region
std::vector< double > m_chi2
chi^2 values at bin centers
std::vector< double > m_x
bin centers

◆ Chi2MinimumFinder1D() [3/3]

Chi2MinimumFinder1D ( const std::shared_ptr< TH1D >  h)
explicit

Constructor from a histogram.

Definition at line 42 of file Chi2MinimumFinder1D.cc.

43 {
44 m_xmin = h->GetXaxis()->GetXmin();
45 m_xmax = h->GetXaxis()->GetXmax();
46 m_dx = h->GetBinWidth(1);
47 for (int i = 0; i < h->GetNbinsX(); i++) {
48 m_x.push_back(h->GetBinCenter(i + 1));
49 m_chi2.push_back(h->GetBinContent(i + 1));
50 }
51 m_entries = h->GetEntries() / h->GetNbinsX();
52 }
int m_entries
number of entries (counted for bin 0)

Member Function Documentation

◆ add() [1/2]

Chi2MinimumFinder1D & add ( const Chi2MinimumFinder1D other)

Add data from another finder Finders must be defined with the same range and binning.

Returns
a reference to this finder

Definition at line 74 of file Chi2MinimumFinder1D.cc.

75 {
76 if (m_x.empty()) {
77 *this = other;
78 } else if (other.getBinCenters().size() == m_x.size() and other.getXmin() == m_xmin and other.getXmax() == m_xmax) {
79 const auto& chi2 = other.getChi2Values();
80 for (unsigned i = 0; i < m_chi2.size(); i++) {
81 m_chi2[i] += chi2[i];
82 }
83 m_entries += other.getEntries();
84 m_searched = false;
85 } else {
86 B2ERROR("Chi2MinimumFinder1D::add: finders with different ranges or binning can't be added");
87 }
88
89 return *this;
90 }

◆ add() [2/2]

void add ( unsigned  i,
double  chi2 
)

Add chi^2 value to bin i.

Parameters
ibin index (0-based)
chi2value to be added

Definition at line 62 of file Chi2MinimumFinder1D.cc.

63 {
64 if (i < m_chi2.size()) {
65 m_chi2[i] += chi2;
66 m_searched = false;
67 if (i == 0) m_entries++;
68 } else {
69 B2WARNING("Chi2MinimumFinder1D::add: index out of range");
70 }
71 }

◆ clear()

void clear ( )

Set chi^2 values to zero.

Definition at line 54 of file Chi2MinimumFinder1D.cc.

55 {
56 for (auto& chi2 : m_chi2) chi2 = 0;
57 m_entries = 0;
58 m_searched = false;
59 }

◆ findMinimum()

void findMinimum ( )
private

Finds minimum.

Definition at line 93 of file Chi2MinimumFinder1D.cc.

94 {
95 if (m_chi2.size() < 3) {
97 m_minimum.error = (m_xmax - m_xmin) / 2;
98 return;
99 }
100
101 unsigned i0 = 1;
102 for (unsigned i = i0; i < m_chi2.size() - 1; i++) {
103 if (m_chi2[i] < m_chi2[i0]) i0 = i;
104 }
105 auto x = getMinimum(m_chi2[i0 - 1], m_chi2[i0], m_chi2[i0 + 1]);
106 m_minimum = Minimum(m_x[i0] + x.position * m_dx, x.error * m_dx, x.chi2, x.valid);
107 }
Minimum m_minimum
result: global minimum
const Minimum & getMinimum()
Returns parabolic minimum.
double position
position of the minimum

◆ getBinCenters()

const std::vector< double > & getBinCenters ( ) const
inline

Returns vector of bin centers.

Returns
bin centers

Definition at line 121 of file Chi2MinimumFinder1D.h.

121{return m_x;}

◆ getBinSize()

double getBinSize ( ) const
inline

Returns bin (or step) size.

Returns
bin size

Definition at line 115 of file Chi2MinimumFinder1D.h.

115{return m_dx;}

◆ getChi2Values()

const std::vector< double > & getChi2Values ( ) const
inline

Returns vector of chi^2.

Returns
chi^2 values

Definition at line 127 of file Chi2MinimumFinder1D.h.

127{return m_chi2;}

◆ getEntries()

int getEntries ( ) const
inline

Returns number of entries (counted for bin 0)

Returns
number of entries

Definition at line 140 of file Chi2MinimumFinder1D.h.

140{return m_entries;}

◆ getHistogram()

TH1F getHistogram ( std::string  name,
std::string  title 
) const

Returns chi^2 packed into 1D histogram.

Parameters
namehistogram name
titlehistogram title

Definition at line 126 of file Chi2MinimumFinder1D.cc.

127 {
128 TH1F h(name.c_str(), title.c_str(), m_x.size(), m_xmin, m_xmax);
129 for (unsigned i = 0; i < m_chi2.size(); i++) {
130 h.SetBinContent(i + 1, m_chi2[i] - m_minimum.chi2);
131 }
132 return h;
133 }

◆ getMinChi2()

double getMinChi2 ( ) const
inline

Returns minimum chi2 value.

Returns
minimum chi2 value

Definition at line 134 of file Chi2MinimumFinder1D.h.

134{return *std::min_element(m_chi2.begin(), m_chi2.end());}

◆ getMinimum() [1/2]

const Minimum & getMinimum ( )
inline

Returns parabolic minimum.

Definition at line 145 of file Chi2MinimumFinder1D.h.

146 {
147 if (!m_searched) {
148 findMinimum();
149 m_searched = true;
150 }
151 return m_minimum;
152 }

◆ getMinimum() [2/2]

Chi2MinimumFinder1D::Minimum getMinimum ( double  yLeft,
double  yCenter,
double  yRight 
) const
private

Calculates minimum using parabolic interpolation.

Parameters
yLeftbin content of left-to-minimal bin
yCenterbin content of minimal bin
yRightbin content of right-to-minimal bin
Returns
minimum given as a fraction of step to be added to central bin position

Definition at line 110 of file Chi2MinimumFinder1D.cc.

113 {
114 // parabola: y = ax^2 + bx + c
115 double DL = yLeft - yCenter;
116 double DR = yRight - yCenter;
117 double A = (DR + DL) / 2;
118 if (A <= 0) return Minimum(0, 0, yCenter, false);
119 double B = (DR - DL) / 2;
120 double x = - B / 2 / A;
121 double chi2_min = A * x * x + B * x + yCenter;
122 return Minimum(x, sqrt(1 / A), chi2_min, true);
123 }
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28

◆ getNbins()

int getNbins ( ) const
inline

Returns number of bins.

Returns
number of bins

Definition at line 109 of file Chi2MinimumFinder1D.h.

109{return m_x.size();}

◆ getXmax()

double getXmax ( ) const
inline

Returns upper limit of search region.

Returns
xmax

Definition at line 103 of file Chi2MinimumFinder1D.h.

103{return m_xmax;}

◆ getXmin()

double getXmin ( ) const
inline

Returns lower limit of search region.

Returns
xmin

Definition at line 97 of file Chi2MinimumFinder1D.h.

97{return m_xmin;}

Member Data Documentation

◆ m_chi2

std::vector<double> m_chi2
private

chi^2 values at bin centers

Definition at line 182 of file Chi2MinimumFinder1D.h.

◆ m_dx

double m_dx = 0
private

bin size

Definition at line 180 of file Chi2MinimumFinder1D.h.

◆ m_entries

int m_entries = 0
private

number of entries (counted for bin 0)

Definition at line 183 of file Chi2MinimumFinder1D.h.

◆ m_minimum

Minimum m_minimum
private

result: global minimum

Definition at line 186 of file Chi2MinimumFinder1D.h.

◆ m_searched

bool m_searched = false
private

internal flag

Definition at line 185 of file Chi2MinimumFinder1D.h.

◆ m_x

std::vector<double> m_x
private

bin centers

Definition at line 181 of file Chi2MinimumFinder1D.h.

◆ m_xmax

double m_xmax = 0
private

upper limit of search region

Definition at line 178 of file Chi2MinimumFinder1D.h.

◆ m_xmin

double m_xmin = 0
private

lower limit of search region

Definition at line 177 of file Chi2MinimumFinder1D.h.


The documentation for this class was generated from the following files: