Belle II Software  release-08-01-10
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 
12 using namespace std;
13 using namespace Belle2;
14 using namespace Belle2::SVD;
15 
16 tuple<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 
27 tuple<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 
52 void NNWaveFitTool::shiftInTime(nnFitterBins& p, double timeShift)
53 {
54  // Calculate at bin boundaries shifted by timeShift, new p's are differences.
55  EmpiricalDistributionFunction edf(p, m_bins);
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 
66 shared_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 
78 double NNWaveFitTool::pLessThan(nnFitterBinData p1, nnFitterBinData p2)
79 {
80  // The formula is integral(P1(x)F2(x)dx.
81  // We do sum(F1[i]P[i]
82  EmpiricalDistributionFunction edf1(p1, m_bins);
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
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.