Belle II Software development
NNWaveFitTool Class Reference

The class holds arrays of bins and bin centers, and a wave generator object containing information on the waveform function. More...

#include <NNWaveFitTool.h>

Public Member Functions

 NNWaveFitTool (const nnFitterBins &bins, const nnFitterBinData &binCenters, const WaveGenerator &wave)
 Constructor takes arrays of signals and bin probabilities.
 
const nnFitterBinsgetBins () const
 Get edges of time shift bins.
 
const nnFitterBinDatagetBinCenters () const
 Get mean bin time shifts.
 
void shiftInTime (nnFitterBinData &p, double timeShift)
 Shift the probability array in time.
 
void multiply (nnFitterBinData &p, const nnFitterBinData &p1)
 Multiply probabilities.
 
std::shared_ptr< nnFitterBinDatapFromInterval (double left, double right)
 Convert a uniform distribution to time-shift-like pdf.
 
std::tuple< double, double > getTimeShift (const nnFitterBinData &p)
 Return std::tuple containing time shift and its error.
 
std::tuple< double, double, double > getAmplitudeChi2 (const apvSamples &samples, double timeShift, double tau)
 Return std::tuple with signal amplitude, its error, and chi2 of the fit.
 
double pLessThan (nnFitterBinData p1, nnFitterBinData p2)
 Return the probability X < Y, where X and Y are random variables with binned pdfs p1 and p2.
 

Protected Member Functions

void normalize (nnFitterBinData &p)
 Normalize the probability distribution.
 

Private Attributes

const nnFitterBinsm_bins
 Edges of bins.
 
const nnFitterBinDatam_binCenters
 Centers of bins.
 
nnFitterBinData m_altBinData
 Bin data array for re-use.
 
apvSamples m_altSamples
 Array of 6 apv samples for re-use.
 
WaveGenerator m_waveGenerator
 APV signal generator.
 

Detailed Description

The class holds arrays of bins and bin centers, and a wave generator object containing information on the waveform function.

It can compute probability distribution updates in clustering, or the parameters of waveform fit, such as the time shift, amplitude and their errors, and it can do compatibility tests.

Definition at line 91 of file NNWaveFitTool.h.

Constructor & Destructor Documentation

◆ NNWaveFitTool()

NNWaveFitTool ( const nnFitterBins bins,
const nnFitterBinData binCenters,
const WaveGenerator wave 
)
inline

Constructor takes arrays of signals and bin probabilities.

Parameters
binsPointer to array of time bin edges
binCentersPointer to array of mean bin times
waveWave generator to generate waveform samples.

Definition at line 98 of file NNWaveFitTool.h.

99 :
100 m_bins(bins), m_binCenters(binCenters), m_waveGenerator(wave)
101 {
102 m_altBinData.resize(m_binCenters.size());
103 }
const nnFitterBinData & m_binCenters
Centers of bins.
WaveGenerator m_waveGenerator
APV signal generator.
nnFitterBinData m_altBinData
Bin data array for re-use.
const nnFitterBins & m_bins
Edges of bins.

Member Function Documentation

◆ getAmplitudeChi2()

tuple< double, double, double > getAmplitudeChi2 ( const apvSamples samples,
double  timeShift,
double  tau 
)

Return std::tuple with signal amplitude, its error, and chi2 of the fit.

The amplitude is calculated using linear regression through origin, its error as the error of the regression coefficient.

Parameters
samplesarray of 6 samples to fit
timeShiftfitted time shift
tauwidth of the waveform
Returns
std::tuple of amplitude, its error, and chi2

Definition at line 27 of file NNWaveFitTool.cc.

29{
30 // Amplitude
31 auto tw = m_waveGenerator(timeShift, tau);
32 double waveNorm = inner_product(
33 tw.begin(), tw.end(), tw.begin(), 0.0);
34 double amplitude = 0.0;
35 double amplitudeError = 100.0;
36 if (waveNorm > 0.0) {
37 amplitude = inner_product(samples.begin(), samples.end(),
38 tw.begin(), 0.0) / waveNorm;
39 amplitudeError = 1.0 / sqrt(waveNorm);
40 }
41 // Chi2
42 transform(samples.begin(), samples.end(), tw.begin(), m_altSamples.begin(),
43 [amplitude](double s, double w)->double { return s - w* amplitude;});
44 size_t ndf = accumulate(samples.begin(), samples.end(), size_t(0),
45 [](size_t sum, double x)->size_t { return ((x > 3) ? sum + 1 : sum); }
46 ) - 2;
47 double chi2 = sqrt(1.0 / ndf * inner_product(m_altSamples.begin(), m_altSamples.end(),
48 m_altSamples.begin(), 0.0));
49 return make_tuple(amplitude, amplitudeError, chi2);
50}
apvSamples m_altSamples
Array of 6 apv samples for re-use.
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28

◆ getBinCenters()

const nnFitterBinData & getBinCenters ( ) const
inline

Get mean bin time shifts.

Returns
pointer to array of bin time shifts

Definition at line 115 of file NNWaveFitTool.h.

115{ return m_binCenters; }

◆ getBins()

const nnFitterBins & getBins ( ) const
inline

Get edges of time shift bins.

Returns
pointer to array of bin edges

Definition at line 110 of file NNWaveFitTool.h.

110{ return m_bins; }

◆ getTimeShift()

tuple< double, double > getTimeShift ( const nnFitterBinData p)

Return std::tuple containing time shift and its error.

Parameters
parray of bin probabilities
Returns
std::tuple of timeShift, timeShiftError

Definition at line 16 of file NNWaveFitTool.cc.

17{
18 // calculate time estimate and error
19 double timeShift = inner_product(p.begin(), p.end(), m_binCenters.begin(), 0.0);
20 // use altBinData storage to sum squared residuals
21 transform(m_binCenters.begin(), m_binCenters.end(), m_altBinData.begin(),
22 [timeShift](double t)->double { return (t - timeShift) * (t - timeShift);});
23 double timeShiftError = sqrt(inner_product(p.begin(), p.end(), m_altBinData.begin(), 0.0));
24 return make_tuple(timeShift, timeShiftError);
25}

◆ multiply()

void multiply ( nnFitterBinData p,
const nnFitterBinData p1 
)
inline

Multiply probabilities.

Modify first probability distribution by multiplying it with the second.

Definition at line 129 of file NNWaveFitTool.h.

130 {
131 std::transform(p.begin(), p.end(),
132 p1.begin(), p.begin(), std::multiplies<double>());
133 normalize(p);
134 }
void normalize(nnFitterBinData &p)
Normalize the probability distribution.

◆ normalize()

void normalize ( nnFitterBinData p)
inlineprotected

Normalize the probability distribution.

After shifting, multiplications or other ooperations on probabilities, we need to restore the normalization and re-build the list used for calculation of EmpiricalDistributionFunction. If probabilities vanish over all bins, return uniform distribution.

Definition at line 178 of file NNWaveFitTool.h.

179 {
180 double pnorm = std::accumulate(p.begin(), p.end(), 0.0);
181 // If the norm is too small, return default distribution.
182 if (pnorm < 1.0e-10) {
183 double uniformP = 1.0 / std::distance(p.begin(), p.end());
184 std::fill(p.begin(), p.end(), uniformP);
185 } else {
186 // Normalize if the norm makes sense.
187 std::transform(p.begin(), p.end(), p.begin(),
188 std::bind(std::divides<double>(), std::placeholders::_1, pnorm));
189 }
190 }

◆ pFromInterval()

shared_ptr< nnFitterBinData > pFromInterval ( double  left,
double  right 
)

Convert a uniform distribution to time-shift-like pdf.

Parameters
leftLeft edge of the interval
rightRight edge of the interval
Returns
pointer to binned pdf representing the uniform distribution between left and right.

Definition at line 66 of file NNWaveFitTool.cc.

67{
68 auto uniCdf = [left, right](double x)->double {
69 if (x < left) return 0.0;
70 if (x > right) return 1.0;
71 return (x - left) / (right - left);
72 };
73 auto result = shared_ptr<nnFitterBinData>(new nnFitterBinData(m_binCenters.size()));
74 for (size_t i = 1; i < m_bins.size(); ++i)(*result)[i - 1] = uniCdf(m_bins[i] - m_bins[i - 1]);
75 return result;
76}
std::vector< double > nnFitterBinData
Vector of values defined for bins, such as bin times or bin probabilities.
Definition: NNWaveFitTool.h:30

◆ pLessThan()

double pLessThan ( nnFitterBinData  p1,
nnFitterBinData  p2 
)

Return the probability X < Y, where X and Y are random variables with binned pdfs p1 and p2.

Parameters
p1binned pdf for X
p2binned pdf for Y

Definition at line 78 of file NNWaveFitTool.cc.

79{
80 // The formula is integral(P1(x)F2(x)dx.
81 // We do sum(F1[i]P[i]
83 double result = 0;
84 for (size_t i2 = 1; i2 < m_binCenters.size(); ++i2)
85 result += edf1(m_binCenters[i2 - 1]) * p2[i2];
86 return result;
87}
Empirical distribution function object is basic for mainpulation of probabilities.
Definition: NNWaveFitTool.h:39

◆ shiftInTime()

void shiftInTime ( nnFitterBinData p,
double  timeShift 
)

Shift the probability array in time.

The method works by linearly interpolating the EmpiricalDistributionFunction to calculate values for a shifted set of bins.

Parameters
pthe probability array to be modified.
timeShiftthe size of the shift.

Definition at line 52 of file NNWaveFitTool.cc.

53{
54 // Calculate at bin boundaries shifted by timeShift, new p's are differences.
56 auto ibin = m_bins.begin();
57 double lowEdf = edf(-timeShift + *ibin++);
58 for (auto& prob : p) {
59 double highEdf = edf(-timeShift + *ibin++);
60 prob = highEdf - lowEdf;
61 lowEdf = highEdf;
62 }
63 normalize(p);
64}

Member Data Documentation

◆ m_altBinData

nnFitterBinData m_altBinData
private

Bin data array for re-use.

Definition at line 195 of file NNWaveFitTool.h.

◆ m_altSamples

apvSamples m_altSamples
private

Array of 6 apv samples for re-use.

Definition at line 196 of file NNWaveFitTool.h.

◆ m_binCenters

const nnFitterBinData& m_binCenters
private

Centers of bins.

Definition at line 194 of file NNWaveFitTool.h.

◆ m_bins

const nnFitterBins& m_bins
private

Edges of bins.

Definition at line 193 of file NNWaveFitTool.h.

◆ m_waveGenerator

WaveGenerator m_waveGenerator
private

APV signal generator.

Definition at line 197 of file NNWaveFitTool.h.


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