Belle II Software development
NNWaveFitTool.h
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#pragma once
10#ifndef _SVD_RECONSTRUCTION_NNWAVEFITTOOL_H
11#define _SVD_RECONSTRUCTION_NNWAVEFITTOOL_H
12
13#include <vector>
14#include <memory>
15#include <tuple>
16#include <algorithm>
17#include <numeric>
18#include <functional>
19#include <svd/simulation/SVDSimulationTools.h>
20
21namespace Belle2 {
26 namespace SVD {
27
30 typedef std::vector<double> nnFitterBinData;
31
33 typedef std::vector<double> nnFitterBins;
34
40 public:
46 m_bins(bins)
47 {
48 // Create table of probabiilities for EmpiricalDistributionFunction calculation
49 m_pTable.resize(m_bins.size());
50 auto ibin = m_bins.begin();
51 auto ipt = m_pTable.begin();
52 double cp = 0.0;
53 *ipt++ = std::make_pair(*ibin++, cp);
54 for (auto prob : p) {
55 cp += prob;
56 *ipt++ = std::make_pair(*ibin++, cp);
57 }
58 }
59
64 double operator()(double t)
65 {
66 // pTable is sorted by .first
67 if (t > m_pTable.back().first) return 1.0;
68 if (t < m_pTable[0].first) return 0.0;
69 auto it = lower_bound(m_pTable.begin(), m_pTable.end(), std::make_pair(t, 0.0));
70 if (it == m_pTable.begin()) return it->second;
71 auto it2 = it;
72 --it2;
73 double result = it2->second + (it->second - it2->second) * (t - it2->first) / (it->first - it2->first);
74 return result;
75 }
76
77 private:
79 std::vector<std::pair<double, double> > m_pTable;
81 }; // class EmpiricalDistributionFunction
82
83 // ========================================================================
84 // NNWaveFitTool - object to handle computations with neural network fit.
85 // ------------------------------------------------------------------------
92 public:
98 NNWaveFitTool(const nnFitterBins& bins, const nnFitterBinData& binCenters,
99 const WaveGenerator& wave):
100 m_bins(bins), m_binCenters(binCenters), m_waveGenerator(wave)
101 {
102 m_altBinData.resize(m_binCenters.size());
103 }
104
105 //------- Operations on probability arrays -------//
106
110 const nnFitterBins& getBins() const { return m_bins; }
111
115 const nnFitterBinData& getBinCenters() const { return m_binCenters; }
116
123 void shiftInTime(nnFitterBinData& p, double timeShift);
124
130 {
131 std::transform(p.begin(), p.end(),
132 p1.begin(), p.begin(), std::multiplies<double>());
133 normalize(p);
134 }
135
142 std::shared_ptr<nnFitterBinData> pFromInterval(double left, double right);
143
144 //------- Waveform parameters ------------------------------------//
145
150 std::tuple<double, double> getTimeShift(const nnFitterBinData& p);
151
160 std::tuple<double, double, double>
161 getAmplitudeChi2(const apvSamples& samples, double timeShift, double tau);
162
169
170 protected:
171
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 }
191
192 private:
198 }; // class NNWaveFitTool
199
200 } // SVD namespace
202} // Belle2 namespace
203
204#endif
Empirical distribution function object is basic for mainpulation of probabilities.
Definition: NNWaveFitTool.h:39
double operator()(double t)
EmpiricalDistributionFunction(t) gives edf value at time t, linearly interpolated from cummulative bi...
Definition: NNWaveFitTool.h:64
std::vector< std::pair< double, double > > m_pTable
(bin, prob) pairs
Definition: NNWaveFitTool.h:79
EmpiricalDistributionFunction(const nnFitterBinData &p, const nnFitterBins &bins)
Constructor constructs edf object from a probability distribution.
Definition: NNWaveFitTool.h:45
const nnFitterBins & m_bins
array of bin boundaries.
Definition: NNWaveFitTool.h:78
The class holds arrays of bins and bin centers, and a wave generator object containing information on...
Definition: NNWaveFitTool.h:91
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.
const nnFitterBins & getBins() const
Get edges of time shift bins.
NNWaveFitTool(const nnFitterBins &bins, const nnFitterBinData &binCenters, const WaveGenerator &wave)
Constructor takes arrays of signals and bin probabilities.
Definition: NNWaveFitTool.h:98
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.
const nnFitterBinData & getBinCenters() const
Get mean bin time shifts.
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.
void multiply(nnFitterBinData &p, const nnFitterBinData &p1)
Multiply probabilities.
Waveform generator This is a functor to calculate APV samples from waveform.
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.