Belle II Software development
fitter_test.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#include <iostream>
9#include <iterator>
10#include <string>
11#include <vector>
12#include <tuple>
13#include <algorithm>
14#include <random>
15#include <chrono>
16
17#include <svd/reconstruction/NNWaveFitter.h>
18#include <svd/reconstruction/NNWaveFitTool.h>
19#include <svd/simulation/SVDSimulationTools.h>
20
21using namespace std;
22using namespace Belle2::SVD;
23
24int main()
25{
26 // Initialize random seed generation
27 typedef chrono::high_resolution_clock myclock;
28 myclock::time_point beginning = myclock::now();
29
30// Create fitter
31 NNWaveFitter fitter("svd/data/SVDTimeNet.xml");
32 auto fitTool = fitter.getFitTool();
33
34 // Initialize random generators
35 myclock::duration d = myclock::now() - beginning;
36 unsigned seed = d.count();
37 default_random_engine randn(seed);
38 normal_distribution<double> gaussian(0.0, 1.0);
39
40 // Generate samples
41 auto generator = WaveGenerator();
42 size_t n_amplitudes = 3;
43 vector< tuple<double, double, double> > components(n_amplitudes);
44 components = {{6, -20, 300}, {6, -20, 300}, {6, -20, 300}};
45 vector<apvSamples> samples_arr(n_amplitudes);
46 vector<shared_ptr<nnFitterBinData> > parr(n_amplitudes);
47 nnFitterBinData p(fitter.getBinCenters().size());
48 fill(p.begin(), p.end(), double(1.0));
49 double amplitude, t0, width;
50 double t0_fit, t0_err;
51 double a_fit, a_err, chi2;
52 cout << " >>>>>>>>>> SEPARRATE COMPONENTS <<<<<<<<<< " << endl;
53 for (size_t icomp = 0; icomp < n_amplitudes; ++icomp) {
54 tie(amplitude, t0, width) = components[icomp];
55 apvSamples u = generator(t0, width);
56 do {
57 transform(u.begin(), u.end(), samples_arr[icomp].begin(),
58 [&](double x)->double {
59 double w = amplitude * x + gaussian(randn);
60 return (int(w) > 3 ? int(w) : 0.0);
61 });
62 } while (!pass3Samples(samples_arr[icomp], 3));
63 copy(samples_arr[icomp].begin(), samples_arr[icomp].end(), ostream_iterator<double>(cout, " "));
64 cout << endl;
65 parr[icomp] = fitter.getFit(samples_arr[icomp], width);
66 copy(parr[icomp]->begin(), parr[icomp]->end(), ostream_iterator<double>(cout, " "));
67 cout << endl;
68 fitTool.multiply(p, *(parr[icomp]));
69 tie(t0_fit, t0_err) = fitTool.getTimeShift(*(parr[icomp]));
70 tie(a_fit, a_err, chi2) = fitTool.getAmplitudeChi2(u, t0_fit, width);
71 cout << "Component: " << icomp << endl;
72 cout << "Time: " << t0_fit << " +/- " << t0_err << endl;
73 cout << "Amplitude: " << a_fit << " +/- " << a_err << endl;
74 cout << "Chi-square " << chi2 << endl;
75 }
76 cout << " >>>>>>>>>> POOLED COMPONENTS <<<<<<<<<< " << endl;
77 tie(t0_fit, t0_err) = fitTool.getTimeShift(p);
78 cout << "Time: " << t0_fit << " +/- " << t0_err << endl;
79 for (size_t icomp = 0; icomp < n_amplitudes; ++icomp) {
80 tie(a_fit, a_err, chi2) = fitTool.getAmplitudeChi2(samples_arr[icomp], t0_fit, width);
81 cout << "Component: " << icomp << endl;
82 cout << "Amplitude: " << a_fit << " +/- " << a_err << endl;
83 cout << "Chi-square " << chi2 << endl;
84 }
85 cout << " >>>>>>>>>> SUM OF SIGNALS COMPONENTS <<<<<<<<<< " << endl;
86 width = 300;
87 apvSamples u_sum;
88 fill(u_sum.begin(), u_sum.end(), double(0.0));
89 for (size_t i = 0; i < nAPVSamples; ++i)
90 for (size_t j = 0; j < n_amplitudes; ++j)
91 u_sum[i] += samples_arr[j][i];
92 shared_ptr<nnFitterBinData> p_sum = fitter.getFit(u_sum, width);
93 tie(t0_fit, t0_err) = fitTool.getTimeShift(*p_sum);
94 cout << "Time: " << t0_fit << " +/- " << t0_err << endl;
95 for (size_t icomp = 0; icomp < n_amplitudes; ++icomp) {
96 tie(a_fit, a_err, chi2) = fitTool.getAmplitudeChi2(samples_arr[icomp], t0_fit, width);
97 cout << "Component: " << icomp << endl;
98 cout << "Amplitude: " << a_fit << " +/- " << a_err << endl;
99 cout << "Chi-square " << chi2 << endl;
100 }
101 return 0;
102}
The class uses a neural network to find a probability distribution of arrival times for a sextet of A...
Definition: NNWaveFitter.h:61
Waveform generator This is a functor to calculate APV samples from waveform.
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
const std::size_t nAPVSamples
Number of APV samples.
std::vector< double > nnFitterBinData
Vector of values defined for bins, such as bin times or bin probabilities.
Definition: NNWaveFitTool.h:30
bool pass3Samples(const T &a, double thr)
pass 3-samples
STL namespace.