Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JMultiPDF.cc
Go to the documentation of this file.
1
2#include <string>
3#include <iostream>
4#include <iomanip>
5
6#include "TMath.h"
7#include "TRandom.h"
8
14#include "JTools/JMultiPDF.hh"
16#include "JTools/JQuadrature.hh"
17#include "JTools/JQuantile.hh"
18#include "JIO/JFileStreamIO.hh"
19#include "JLang/JObjectIO.hh"
20
21#include "Jeep/JPrint.hh"
22#include "Jeep/JTimer.hh"
23#include "Jeep/JParser.hh"
24#include "Jeep/JMessage.hh"
25
26
27namespace {
28
29 /**
30 * Normalised function in 2D.
31 *
32 * \param x x value
33 * \param y y value
34 * \return function value
35 */
36 inline Double_t g2(const Double_t x,
37 const Double_t y)
38 {
39 return (TMath::Gaus(x, 0.0, 1.0, kTRUE) *
40 TMath::Gaus(y, 0.0, 1.0, kTRUE));
41 }
42
43
44 /**
45 * Function in 4D.
46 *
47 * \param x0 x0 value
48 * \param x1 x1 value
49 * \param x2 x2 value
50 * \param x3 x3 value
51 * \return function value
52 */
53 inline Double_t g4(const Double_t x0,
54 const Double_t x1,
55 const Double_t x2,
56 const Double_t x3)
57 {
58 return g2(x2 - x0, x3 - x1);
59 }
60}
61
62
63/**
64 * \file
65 *
66 * Example program to test conditional probability density function \f$P(x_0,x_1\|x_2,x_3)\f$ using JTOOLS::JMultiPDF and JTOOLS::JMultiFunction.
67 * \author mdejong
68 */
69int main(int argc, char **argv)
70{
71 using namespace std;
72
73 string inputFile;
74 string outputFile;
75 int numberOfEvents;
76 int numberOfBins;
77 int debug;
78
79 try {
80
81 JParser<> zap("Example program to test conditional probability density function.");
82
83 zap['f'] = make_field(inputFile) = "";
84 zap['o'] = make_field(outputFile) = "";
85 zap['n'] = make_field(numberOfEvents);
86 zap['N'] = make_field(numberOfBins) = 15;
87 zap['d'] = make_field(debug) = 2;
88
89 zap(argc, argv);
90 }
91 catch(const exception &error) {
92 FATAL(error.what() << endl);
93 }
94
95
96 if (numberOfEvents <= 0) {
97 FATAL("No events." << endl);
98 }
99
100 using namespace JPP;
101
102
103 const double xmin = -10.0;
104 const double xmax = +10.0;
105 const double dx = (xmax - xmin) / (numberOfBins - 1);
106
107
108 if (outputFile != "") {
109
113 JHistogramMap_t>::maplist> JMultiHistogram_t;
114
115 JMultiHistogram_t histogram;
116
117 const JGaussHermite bounds(numberOfBins);
118
119 for (double x0 = xmin; x0 <= xmax + 0.5*dx; x0 += dx) {
120 for (double x1 = xmin; x1 <= xmax + 0.5*dx; x1 += dx) {
121 for (JGaussHermite::const_iterator x2 = bounds.begin(); x2 != bounds.end(); ++x2) {
122 for (JGaussHermite::const_iterator x3 = bounds.begin(); x3 != bounds.end(); ++x3) {
123 histogram[x0][x1][x0 + x2->getX()][x1 + x3->getX()] = 0.0;
124 }
125 }
126 }
127 }
128
129 // fill
130
131 JTimer timer("histogram");
132
133 for (int i = 0; i != numberOfEvents; ++i) {
134
135 if (i%1000 == 0) {
136 STATUS("event: " << setw(10) << i << '\r'); DEBUG(endl);
137 }
138
139 const double x0 = gRandom->Uniform(xmin, xmax);
140 const double x1 = gRandom->Uniform(xmin, xmax);
141 const double x2 = x0 + gRandom->Gaus(0.0, 1.0);
142 const double x3 = x1 + gRandom->Gaus(0.0, 1.0);
143 const double w = 1.0;
144
145 timer.start();
146
147 histogram.fill(x0, x1, x2, x3, w);
148
149 timer.stop();
150 }
151 STATUS(endl);
152
153 if (debug >= status_t) {
154 timer.print(cout, true, micro_t);
155 }
156
157 try {
158
159 NOTICE("storing output to file " << outputFile << "... " << flush);
160
162
163 NOTICE("OK" << endl);
164 }
165 catch(const JException& error) {
166 FATAL(error.what() << endl);
167 }
168 }
169
170
171 if (inputFile != "") {
172
175
176 typedef JMultiHistogram<JHistogram2D_t,
178 JHistogramGridMap_t>::maplist> JMultiHistogram_t;
179
180 JMultiHistogram_t histogram;
181
182 try {
183
184 NOTICE("loading input to file " << inputFile << "... " << flush);
185
186 JLANG::load<JIO::JFileStreamReader>(inputFile.c_str(), histogram);
187
188 NOTICE("OK" << endl);
189 }
190 catch(const JException& error) {
191 FATAL(error.what() << endl);
192 }
193
194
196 JMAPLIST<JPolint1FunctionalMap>::maplist> JFunction2D_t; // PDF
197
198 typedef JMultiFunction<JFunction2D_t,
200 JPolint1FunctionalGridMap>::maplist> JMultiFunction_t; // interpolation
201
202
203 JMultiFunction_t pdf(histogram);
204
205
206 // test
207
208 JQuantile Q;
209
210 for (int i = 0; i != numberOfEvents; ++i) {
211
212 const double x0 = gRandom->Uniform(xmin, xmax);
213 const double x1 = gRandom->Uniform(xmin, xmax);
214 const double x2 = x0 + gRandom->Gaus(0.0, 1.0);
215 const double x3 = x1 + gRandom->Gaus(0.0, 1.0);
216
217 try {
218
219 const double u = g4 (x0, x1, x2, x3);
220 const double v = pdf(x0, x1, x2, x3);
221
222 Q.put(u - v);
223 }
224 catch(const std::exception& error) {}
225 }
226
227
228 JQuantile V;
229
230 for (JMultiFunction_t::super_const_iterator i = pdf.super_begin(); i != pdf.super_end(); ++i) {
231 V.put(getIntegral((*i).getValue().getMultiFunction()));
232 }
233
234 cout << "normalisation "
235 << FIXED(6,4) << V.getMean() << " +/- "
236 << FIXED(6,4) << V.getSTDev() << endl;
237 cout << "efficiency "
238 << FIXED(5,3) << (double) Q.getCount() / (double) numberOfEvents << endl;
239
240 cout << "mean " << SCIENTIFIC(10,2) << Q.getMean() << endl;
241 cout << "RMS " << SCIENTIFIC(10,2) << Q.getSTDev() << endl;
242 }
243}
string outputFile
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define STATUS(A)
Definition JMessage.hh:63
#define NOTICE(A)
Definition JMessage.hh:64
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
int main(int argc, char **argv)
Definition JMultiPDF.cc:69
General methods for loading and storing a single object from and to a file, respectively.
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
I/O formatting auxiliaries.
Auxiliary classes for numerical integration.
int numberOfBins
number of bins for average CDF integral of optical module
Definition JSirene.cc:73
This include file contains various recursive methods to operate on multi-dimensional collections.
Auxiliary class for CPU timing and usage.
Definition JTimer.hh:33
void print(std::ostream &out, const JScale_t scale=milli_t) const
Print timer data.
Definition JTimer.hh:172
void stop()
Stop timer.
Definition JTimer.hh:127
void start()
Start timer.
Definition JTimer.hh:106
General exception.
Definition JException.hh:24
virtual const char * what() const override
Get error message.
Definition JException.hh:64
Utility class to parse command line options.
Definition JParser.hh:1698
Numerical integrator for .
Histogram in 1D.
Multidimensional interpolation method.
Multidimensional histogram.
General purpose class for multi-dimensional probability density function (PDF).
Definition JMultiPDF.hh:37
void store(const std::string &file_name, const T &object)
Store object to output file.
Definition JObjectIO.hh:68
void load(const std::string &file_name, T &object)
Load object from input file.
Definition JObjectIO.hh:55
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Type definition of a JHistogramMap based on JGridMap implementation.
Type definition of a JHistogramMap based on JMap implementation.
Auxiliary class for recursive map list generation.
Definition JMapList.hh:109
Map list.
Definition JMapList.hh:25
Type definition of a 1st degree polynomial interpolation with result type double.
Type definition of a 1st degree polynomial interpolation based on a JGridMap implementation.
Auxiliary data structure for running average, standard deviation and quantiles.
Definition JQuantile.hh:46
double getSTDev() const
Get standard deviation.
Definition JQuantile.hh:281
void put(const double x, const double w=1.0)
Put value.
Definition JQuantile.hh:133
long long int getCount() const
Get total count.
Definition JQuantile.hh:186
double getMean() const
Get mean value.
Definition JQuantile.hh:252
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488