Belle II Software development
NNWaveFitTool.cc
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
9#include <svd/reconstruction/NNWaveFitTool.h>
10#include <memory>
11
12using namespace std;
13using namespace Belle2;
14using namespace Belle2::SVD;
15
16tuple<double, double> NNWaveFitTool::getTimeShift(const nnFitterBinData& p)
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}
26
27tuple<double, double, double> NNWaveFitTool::getAmplitudeChi2(const apvSamples& samples,
28 double timeShift, double tau)
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}
51
52void NNWaveFitTool::shiftInTime(nnFitterBins& p, double timeShift)
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}
65
66shared_ptr<nnFitterBinData> NNWaveFitTool::pFromInterval(double left, double right)
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}
77
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
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.
std::tuple< double, double > getTimeShift(const nnFitterBinData &p)
Return std::tuple containing time shift and its error.
void shiftInTime(nnFitterBinData &p, double timeShift)
Shift the probability array in time.
void normalize(nnFitterBinData &p)
Normalize the probability distribution.
apvSamples m_altSamples
Array of 6 apv samples for re-use.
const nnFitterBinData & m_binCenters
Centers of bins.
WaveGenerator m_waveGenerator
APV signal generator.
double pLessThan(nnFitterBinData p1, nnFitterBinData p2)
Return the probability X < Y, where X and Y are random variables with binned pdfs p1 and p2.
nnFitterBinData m_altBinData
Bin data array for re-use.
std::shared_ptr< nnFitterBinData > pFromInterval(double left, double right)
Convert a uniform distribution to time-shift-like pdf.
const nnFitterBins & m_bins
Edges of bins.
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
Namespace to encapsulate code needed for simulation and reconstrucion of the SVD.
Definition: GeoSVDCreator.h:23
std::array< apvSampleBaseType, nAPVSamples > apvSamples
vector od apvSample BaseType objects
std::vector< double > nnFitterBins
Vector of bin edges, nnFitterBinData.size() + 1.
Definition: NNWaveFitTool.h:33
std::vector< double > nnFitterBinData
Vector of values defined for bins, such as bin times or bin probabilities.
Definition: NNWaveFitTool.h:30
Abstract base class for different kinds of events.
STL namespace.