Jpp  18.2.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JQuantile.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 #include <cmath>
6 #include <vector>
7 #include <random>
8 
9 #include "TRandom.h"
10 
11 #include "JTools/JQuantile.hh"
12 
13 #include "Jeep/JParser.hh"
14 #include "Jeep/JMessage.hh"
15 
16 
17 /**
18  * \file
19  *
20  * Example program to test JTOOLS::JQuantile calculation.
21  * \author mdejong
22  */
23 int main(int argc, char **argv)
24 {
25  using namespace std;
26  using namespace JPP;
27 
28  int numberOfEvents;
29  double x;
30  double sigma;
31  UInt_t seed;
32  double precision;
33  int debug;
34 
35  try {
36 
37  JParser<> zap("Example program to test quantile calculation.");
38 
39  zap['n'] = make_field(numberOfEvents);
40  zap['x'] = make_field(x) = 0.0;
41  zap['s'] = make_field(sigma) = 1.0;
42  zap['S'] = make_field(seed) = 0;
43  zap['e'] = make_field(precision) = 1.0e-2;
44  zap['d'] = make_field(debug) = 3;
45 
46  zap(argc, argv);
47  }
48  catch(const exception &error) {
49  FATAL(error.what() << endl);
50  }
51 
52  if (numberOfEvents < 2) {
53  FATAL("Fatal error: number of events " << numberOfEvents << endl);
54  }
55 
56  gRandom->SetSeed(seed);
57 
58  {
59  vector<double> buffer;
60 
61  JQuantile Q;
62 
63  for (int i = 0; i != numberOfEvents; ++i) {
64 
65  double x = gRandom->Uniform(-1.0e6, +1.0e6);
66 
67  buffer.push_back(x);
68 
69  Q.put(x);
70  }
71 
72  random_device rd;
73  mt19937 g(rd());
74 
75  for (int i = 0; i != 100; ++i) {
76 
77  std::shuffle(buffer.begin(), buffer.end(), g);
78 
79  DEBUG("buffer[0] " << FIXED(15,6) << buffer[0] << endl);
80 
81  JQuantile R;
82 
83  for (const double x : buffer) {
84  R.put(x);
85  }
86 
87  ASSERT( Q.getCount() == R.getCount(), "Test shuffle counts.");
88  ASSERT(fabs(Q.getMean() - R.getMean()) <= precision, "Test shuffle means.");
89  ASSERT(fabs(Q.getSTDev() - R.getSTDev()) <= precision, "Test shuffle sigmas.");
90  }
91  }
92  {
93  JQuantile Q;
94 
95  for (int i = 0; i != numberOfEvents; ++i) {
96  Q.put(gRandom->Gaus(x, sigma));
97  }
98 
99  NOTICE("quantity " << CENTER(10) << "calculated" << " | " << CENTER(10) << "true" << endl);
100  NOTICE("mean " << FIXED(10,3) << Q.getMean() << " | " << FIXED(10,3) << x << endl);
101  NOTICE("RMS " << FIXED(10,3) << Q.getSTDev() << " | " << FIXED(10,3) << sigma << endl);
102 
103  ASSERT(numberOfEvents > 0, "Test number of events.");
104  ASSERT(fabs(Q.getMean() - x) <= precision, "Test mean.");
105  ASSERT(fabs(Q.getSTDev() - sigma) <= precision, "Test sigma.");
106  }
107  {
108  JQuantile Q("", true);
109 
110  for (int i = 0; i != numberOfEvents; ++i) {
111  Q.put(gRandom->Uniform(0.0, 1.0));
112  }
113 
114  for (const double x : { 0.1, 0.5, 0.9}) {
115 
116  NOTICE("forward quantile " << FIXED(6,3) << x << ' ' << FIXED(6,3) << Q.getQuantile(x, JQuantile::forward_t) << endl);
117  NOTICE("symmetric quantile " << FIXED(6,3) << x << ' ' << FIXED(6,3) << Q.getQuantile(x, JQuantile::symmetric_t) << endl);
118  NOTICE("backward quantile " << FIXED(6,3) << x << ' ' << FIXED(6,3) << Q.getQuantile(x, JQuantile::backward_t) << endl);
119 
120  ASSERT(fabs(Q.getQuantile(x, JQuantile::forward_t) - ( x )) <= precision, "Test forward quantile ");
121  ASSERT(fabs(Q.getQuantile(x, JQuantile::symmetric_t) - ( x )) <= precision, "Test symmetric quantile ");
122  ASSERT(fabs(Q.getQuantile(x, JQuantile::backward_t) - (1.0 - x)) <= precision, "Test backward quantile ");
123  }
124  }
125 
126  return 0;
127 }
128 
Utility class to parse command line options.
Definition: JParser.hh:1514
Q(UTCMax_s-UTCMin_s)-livetime_s
int main(int argc, char *argv[])
Definition: Main.cc:15
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
const double sigma[]
Definition: JQuadrature.cc:74
Auxiliary data structure for alignment of data.
Definition: JManip.hh:366
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
#define NOTICE(A)
Definition: JMessage.hh:64
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
then JCookie sh JDataQuality D $DETECTOR_ID R
Definition: JDataQuality.sh:41
Utility class to parse command line options.
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62