All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FastAndPoorGauss_test.cc
Go to the documentation of this file.
1 /**
2  * @file FastAndPoorGauss_test.cc
3  * @brief Unit test for `util::FastAndPoorGauss`.
4  * @date February 15, 2020
5  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
6  * @see `icarusalg/Utilities/FastAndPoorGauss.h
7  */
8 
9 // Boost libraries
10 #define BOOST_TEST_MODULE FastAndPoorGauss
11 #include <boost/test/unit_test.hpp>
12 #include <boost/test/tools/floating_point_comparison.hpp> // BOOST_CHECK_CLOSE()
13 
14 // ICARUS libraries
16 
17 // LArSoft libraries
19 
20 
21 // ROOT
22 #include "TFile.h"
23 #include "TH1F.h"
24 #include "TF1.h"
25 #include "TFitResult.h"
26 
27 
28 //------------------------------------------------------------------------------
29 template <std::size_t NSamples>
30 void Test(TFile* pFile = nullptr, bool statChecks = true) {
31 
32  constexpr unsigned int NPoints = 1'000'000U;
33 
34  std::string const NSamplesTag = std::to_string(NSamples);
35 
37  BOOST_TEST_MESSAGE("Testing sampling " << NSamples << " points.");
38 
39  TH1D HGaus(
40  ("HGaus" + NSamplesTag).c_str(),
41  ("Distribution from util::FastAndPoorGauss<" + NSamplesTag + ">").c_str(),
42  1024, -5.0, +5.0
43  );
44  HGaus.SetDirectory(pFile);
45 
46  util::UniformSequence<> extract { NPoints };
47  for (auto _ [[gnu::unused]]: util::counter(NPoints)) {
48  double const u = extract();
49  double const z = gauss(u);
50  HGaus.Fill(z);
51  } // for
52 
53  TF1* pGaus = new TF1(("FGaus" + NSamplesTag).c_str(), "gaus", -5.0, +5.0);
54  TFitResultPtr pFitRes = HGaus.Fit(pGaus, "QS"); // Quiet, reSult
55  BOOST_CHECK(pFitRes->IsValid());
56  if (statChecks) BOOST_CHECK_LT(pFitRes->Chi2()/pFitRes->Ndf(), 5.0);
57  pFitRes->Print();
58 
59  // mean should be smaller than twice its uncertainty
60  if (statChecks) BOOST_CHECK_SMALL(pFitRes->Parameter(1), 2.*pFitRes->Error(1));
61  if (statChecks) BOOST_CHECK_CLOSE(pFitRes->Parameter(2), 1.0, 1.0); // stddev (1% tolerance)
62 
63  if (pFile) {
64  HGaus.Write();
65  HGaus.SetDirectory(nullptr);
66  }
67 
68 } // void Test()
69 
70 
71 //------------------------------------------------------------------------------
72 //--- The tests
73 //---
74 BOOST_AUTO_TEST_CASE( TestCase ) {
75 
76  TFile F("FastAndPoorGauss_test.root", "RECREATE");
77 
78  Test< 1024U>(&F, false); // with low points the statistics is not very good;
79  Test< 4096U>(&F, false); // so we skip the statistics checks
80  Test< 8192U>(&F, false);
81  Test< 16384U>(&F);
82  Test< 32768U>(&F);
83  Test< 65536U>(&F);
84  Test< 131072U>(&F);
85  Test< 262144U>(&F);
86  Test< 524288U>(&F);
87 // Test<1048576U>(&F);
88 
89  F.Write();
90 
91 } // BOOST_AUTO_TEST_CASE( TestCase )
92 
BOOST_AUTO_TEST_CASE(AllTests)
process_name opflash particleana ie ie ie z
Fast approximate Gaussian random translator.
void Test(TFile *pFile=nullptr, bool statChecks=true)
Translates a number u uniformly distributed between 0 and 1 into a Gaussian distributed one z...
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
Definition: counter.h:285
Test of util::counter and support utilities.
std::string to_string(WindowPattern const &pattern)
Samples the interval [ 0, 1 ] in sequence, cyclically.