Belle II Software  release-05-01-25
NNFitterTest.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Peter Kvasnicka *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <svd/simulation/SVDSimulationTools.h>
12 #include <svd/reconstruction/NNWaveFitter.h>
13 #include <svd/reconstruction/NNWaveFitTool.h>
14 #include <gtest/gtest.h>
15 #include <iostream>
16 #include <fstream>
17 #include <sstream>
18 #include <string>
19 #include <tuple>
20 
21 using namespace std;
22 
23 namespace Belle2 {
28  namespace SVD {
29 
37  TEST(NNTimeFitter, DISABLED_CompareNetworkCoefficient)
38  {
39  // Create an instance of the NN fitter
40  NNWaveFitter fitter("svd/data/SVDTimeNet.xml");
41  EXPECT_TRUE(fitter.checkCoefficients("svd/data/classifier.txt", 1.0e-6));
42  }
43 
55  TEST(NNTimeFitter, DISABLED_CompareFits)
56  {
57  const size_t max_lines = 100; // maximum number of lines to be read
58 
59  // Create an instance of the NN fitter and the fitter tool.
60  NNWaveFitter fitter("SVDTimeNet_6samples");
61  auto fitTool = fitter.getFitTool();
62  size_t nProbs = fitTool.getBinCenters().size();
63 
64  ifstream infile("svd/data/test_sample.csv");
65 
66  // Read the rows one by one and compare results
67  string line;
68  getline(infile, line);
69 
70  for (size_t i_line = 0; i_line < max_lines; i_line++) {
71 
72  getline(infile, line);
73  if (line.size() < 10) break;
74  istringstream sline(line);
75 
76  // Parse header. We want the dimennsion of the probability array.
77  // not needed
78  string cell;
79  getline(sline, cell, ','); // index
80  getline(sline, cell, ','); // test
81 
82  // true values. Read from the file, though not used.
83  getline(sline, cell, ',');
84  [[maybe_unused]] double true_amp = stod(cell);
85  getline(sline, cell, ',');
86  [[maybe_unused]] double true_t0 = stod(cell);
87  getline(sline, cell, ',');
88  [[maybe_unused]] double width = stod(cell);
89  getline(sline, cell, ',');
90  [[maybe_unused]] double noise = stod(cell);
91 
92  // normalized samples
93  apvSamples normedSamples;
94  for (size_t iSample = 0; iSample < nAPVSamples; ++iSample) {
95  getline(sline, cell, ',');
96  normedSamples[iSample] = stod(cell);
97  }
98 
99  // not needed
100  getline(sline, cell, ',');
101  getline(sline, cell, ',');
102  getline(sline, cell, ',');
103 
104  // probabilities
105  nnFitterBinData ProbsPy(nProbs);
106  for (size_t iSample = 0; iSample < nProbs; ++iSample) {
107  getline(sline, cell, ',');
108  ProbsPy[iSample] = stod(cell);
109  }
110 
111  // fit results
112  getline(sline, cell, ',');
113  double fitPy_amp = stod(cell);
114  getline(sline, cell, ',');
115  double fitPy_ampSigma = stod(cell);
116  getline(sline, cell, ',');
117  [[maybe_unused]] double fitPy_chi2 = stod(cell);
118  getline(sline, cell, ',');
119  double fitPy_t0 = stod(cell);
120  getline(sline, cell, ',');
121  double fitPy_t0Sigma = stod(cell);
122 
123  // now do the Cpp fit
124  const shared_ptr<nnFitterBinData> ProbsCpp = fitter.getFit(normedSamples, width);
125  for (size_t iBin = 0; iBin < nProbs; ++iBin)
126  EXPECT_NEAR((*ProbsCpp)[iBin], ProbsPy[iBin], 5.0e-3);
127 
128  double t0_cpp, t0_err_cpp;
129  tie(t0_cpp, t0_err_cpp) = fitTool.getTimeShift(*ProbsCpp);
130  EXPECT_NEAR(t0_cpp, fitPy_t0, 5);
131  EXPECT_NEAR(t0_err_cpp, fitPy_t0Sigma, 2);
132 
133  double amp_cpp, amp_err_cpp, chi2_cpp;
134  tie(amp_cpp, amp_err_cpp, chi2_cpp) = fitTool.getAmplitudeChi2(normedSamples, t0_cpp, width);
135  EXPECT_NEAR(amp_cpp, fitPy_amp, 1.0);
136  EXPECT_NEAR(amp_err_cpp, fitPy_ampSigma, 0.1);
137  // FIXME: This is calculated slightly differently in Python, and it shows.
138  // EXPECT_NEAR(chi2_cpp, fitPy_chi2, 1);
139  }
140  }
141 
142  } // namespace SVD
144 } // namespace Belle2
Belle2::SVD::NNWaveFitter
The class uses a neural network to find a probability distribution of arrival times for a sextet of A...
Definition: NNWaveFitter.h:63
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::SVD::apvSamples
std::array< apvSampleBaseType, nAPVSamples > apvSamples
vector od apvSample BaseType objects
Definition: SVDSimulationTools.h:41
Belle2::SVD::nAPVSamples
const std::size_t nAPVSamples
Number of APV samples.
Definition: SVDSimulationTools.h:34
Belle2::TEST
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Definition: utilityFunctions.cc:18
Belle2::SVD::nnFitterBinData
std::vector< double > nnFitterBinData
Vector of values defined for bins, such as bin times or bin probabilities.
Definition: NNWaveFitTool.h:32