Jpp 20.0.0-rc.9-4-g539b3ce77
the software that should make you happy
Loading...
Searching...
No Matches
JPseudoExperiment.cc
Go to the documentation of this file.
1#include <string>
2#include <iostream>
3#include <iomanip>
4#include <chrono>
5#include <vector>
6#include <map>
7#include <exception>
8#include <algorithm>
9
10#include "TROOT.h"
11#include "TFile.h"
12#include "TH1D.h"
13#include "TH2D.h"
14#include "TH3D.h"
15#include "TRandom3.h"
16
18#include "JAstronomy/JGarage.hh"
20
21#include "JLang/JVectorize.hh"
24#include "JROOT/JRandom.hh"
26
27#include "Jeep/JContainer.hh"
28#include "Jeep/JParser.hh"
29
30
31namespace {
32
33 /**
34 * Auxiliary data structure for experiment.
35 */
36 struct experiment_type {
37
38 JGIZMO::JRootObjectID Hs; //!< signal
39 JGIZMO::JRootObjectID Hb; //!< background
40
41 /**
42 * Read experiment from input stream.
43 *
44 * \param in input stream
45 * \param experiment experiment
46 * \return input stream
47 */
48 friend inline std::istream& operator>>(std::istream& in, experiment_type& experiment)
49 {
50 return in >> experiment.Hs
51 >> experiment.Hb;
52 }
53
54
55 /**
56 * Write experiment to output stream.
57 *
58 * \param out output stream
59 * \param experiment experiment
60 * \return output stream
61 */
62 friend inline std::ostream& operator<<(std::ostream& out, const experiment_type& experiment)
63 {
64 return out << experiment.Hs << ' '
65 << experiment.Hb;
66 }
67 };
68
69
70 /**
71 * Auxiliary data structure for printing.
72 */
73 struct printer {
74 /**
75 * Constructor.
76 *
77 * \param title title
78 * \param ps pointer to object
79 */
80 printer(const char* const title,
81 const TObject* ps) :
82 title(title),
83 ps(ps)
84 {}
85
86 /**
87 * Write printer to output stream.
88 *
89 * \param out output stream
90 * \param printer printer
91 * \return output stream
92 */
93 friend inline std::ostream& operator<<(std::ostream& out, const printer& printer)
94 {
95 using namespace std;
96 using namespace JPP;
97
98 out << setw(16) << left << printer.title << right;
99
100 if (printer.ps != NULL) {
101
102 out << ' ' << setw(16) << left << printer.ps->GetName() << right;
103
104 const TH1* h1 = dynamic_cast<const TH1*>(printer.ps);
105
106 if (h1 != NULL) {
107 out << ' ' << FIXED(10,3) << h1->GetSumOfWeights();
108 }
109 }
110
111 return out;
112 }
113
114 private:
115 const char* const title;
116 const TObject* ps;
117 };
118}
119
120
121/**
122 * \file
123 * Test application for pseudo experiment generation and likelihood ratio evaluations.
124 *
125 * \author mdejong
126 */
127int main(int argc, char **argv)
128{
129 using namespace std;
130 using namespace JPP;
131
132 typedef JAbstractHistogram<double> histogram_type;
133 typedef float data_type;
134
136
138 string outputFile;
139 size_t numberOfTests;
140 double Fs;
141 double Fb;
142 double boost;
143 size_t M;
144 double SNR;
145 bool add;
146 histogram_type X;
147 data_type Q;
149 JRandom seed;
150 int debug;
151
152 try {
153
154 JParser<> zap;
155
156 zap['E'] = make_field(setup,
157 "douplets of signal and background histograms, "
158 << "each of which defined by <file name>:<histogram name>");
159 zap['o'] = make_field(outputFile, "output file with histograms") = "benchmark.root";
160 zap['n'] = make_field(numberOfTests, "number of tests");
161 zap['s'] = make_field(Fs, "signal strength");
162 zap['b'] = make_field(Fb, "background strength") = 1.0;
163 zap['B'] = make_field(boost, "boost livetime of experiment") = 1.0;
164 zap['M'] = make_field(M, "lookup table for CDFs") = 0;
165 zap['R'] = make_field(SNR, "signal-to-noise ratio") = 0.0;
166 zap['A'] = make_field(add, "add remnant signal and noise");
167 zap['N'] = make_field(px.nuisance,
168 "signal and background nuisances, "
169 << "each of which defined by <type> (values), \n"
170 << "\twhere <type> can be:" << get_keys(nuisance_helper)) = JPARSER::initialised();
171 zap['x'] = make_field(X, "x-axis likelihood histogram") = histogram_type(110, -10.0, +100.0);
172 zap['Q'] = make_field(Q, "minimal likelihood ratio") = 0.0;
173 zap['P'] = make_field(P, "maximal probability") = JPARSER::initialised();
174 zap['S'] = make_field(seed) = 0;
175 zap['d'] = make_field(debug) = 1;
176
177 zap(argc, argv);
178 }
179 catch(const exception& error) {
180 FATAL(error.what() << endl);
181 }
182
183
184 seed.set(gRandom);
185
186 JExperiment::setSNR(SNR);
187
188 for (const auto& i : setup) {
189
190 TObject* ps = getObject(i.Hs);
191 TObject* pb = getObject(i.Hb);
192
193 dynamic_cast<TH1*>(ps)->Scale(boost);
194 dynamic_cast<TH1*>(pb)->Scale(boost);
195
196 STATUS(printer("Signal:", ps) << endl);
197 STATUS(printer("Background:", pb) << endl);
198
199 px.add(ps, pb);
200 }
201
202 px.set(Fs, Fb);
203
204 if (M != 0) {
205 px.configure(M);
206 }
207
208 if (add) {
209 px.add();
210 }
211
212 STATUS("Signal: " << FIXED(10,3) << px.getSignal() << endl);
213 STATUS("Background: " << FIXED(10,3) << px.getBackground() << endl);
214 STATUS("Number of tests: " << setw(10) << numberOfTests << endl);
215
216
217 TFile out(outputFile.c_str(), "recreate");
218
219 out << *gRandom;
220
221 TH1D hl("hl", NULL, X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
222 TH1D hn("hn", NULL, 100, -15.0, +15.0);
223
224
225 if (P.size() > 1) { // special case
226
228
229 double Ps = 0.0; // total probability
230 size_t nb = px.getBackground() + 1; // start value for number of background events
231 size_t mb = 0;
232
233 {
234 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
235
236 for ( ; ; ++nb) {
237
238 const double p = poisson(nb, px.getBackground());
239 const size_t n = p * numberOfTests;
240
241 if (p > P[1]) {
242 continue;
243 }
244
245 if (n == 0) {
246 break;
247 }
248
249 if (mb == 0) {
250 mb = nb;
251
252 }
253 for (size_t i = 0; i != n; ++i) {
254 storage.put(px(nb).likelihood);
255 }
256
257 Ps += p;
258 }
259
260 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
261
262 STATUS("Total time: " << setw(8) << (t1 - t0) / chrono::milliseconds(1) << " [ms]" << endl);
263 STATUS("Average time: " << setw(6) << (t1 - t0) / chrono::nanoseconds(numberOfTests) << " [ns]" << endl);
264 }
265
266 STATUS("Poisson: " << setw(3) << mb << ' ' << SCIENTIFIC(12,3) << Ps << ' ' << setw(8) << storage.size() << endl);
267
268 storage([&hl](const data_type x) { hl.Fill(x); });
269
270 double x = 0.0;
271 double p = 0.0;
272
273 {
274 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
275
276 x = storage.getValue(P[0]/Ps);
277 p = storage.getProbability(x) * Ps;
278
279 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
280
281 STATUS("Time to result: " << setw(6) << (t1 - t0) / chrono::nanoseconds(1) << " [ns]" << endl);
282 }
283
284 cout << "Minimal likelihood ratio: " << FIXED(9,5) << x << ' ' << SCIENTIFIC(12,3) << p << endl;
285
286 } else if (P.size() > 0 && Q > 0.0) {
287
288 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
289
290 const double signal = discovery<float, 100>(px, Q, P[0], numberOfTests);
291
292 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
293
294 STATUS("Total time: " << setw(8) << (t1 - t0) / chrono::milliseconds(1) << " [ms]" << endl);
295
296 cout << "Signal strength: " << FIXED(9,5) << signal << endl;
297
298 } else if (P.size() > 0 || Q > 0.0) {
299
301
302 {
303 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
304
305 for (size_t i = 0; i != numberOfTests; ++i) {
306 storage.put(px().likelihood);
307 }
308
309 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
310
311 STATUS("Total time: " << setw(8) << (t1 - t0) / chrono::milliseconds(1) << " [ms]" << endl);
312 STATUS("Average time: " << setw(6) << (t1 - t0) / chrono::nanoseconds(numberOfTests) << " [ns]" << endl);
313 }
314
315 storage([&hl](const data_type x) { hl.Fill(x); });
316
317 double x = 0.0;
318 double p = 0.0;
319
320 {
321 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
322
323 if (P.size() == 1) { // determine minimal likelihood ratio given maximal probability
324 x = storage.getValue(P[0]);
325 p = storage.getProbability(x);
326 }
327
328 if (Q > 0.0) { // determine maximal probability given minimal likelihood ratio
329 p = storage.getProbability(Q);
330 x = storage.getValue(p);
331 }
332
333 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
334
335 STATUS("Time to result: " << setw(6) << (t1 - t0) / chrono::microseconds(1) << " [us]" << endl);
336 }
337
338 if (P.size() == 1) { cout << "Minimal likelihood ratio: " << FIXED(9,5) << x << ' ' << SCIENTIFIC(12,3) << p << endl; }
339 if (Q > 0.0) { cout << "Maximal probability: " << SCIENTIFIC(12,3) << p << ' ' << FIXED(9,5) << x << endl; }
340
341 } else {
342
343 std::vector<JPseudoExperiment::result_type> storage(numberOfTests);
344
345 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
346
347 px(storage);
348
349 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
350
351 STATUS("Average time: " << setw(6) << (t1 - t0) / chrono::nanoseconds(storage.size()) << " [ns]" << endl);
352
353 for (const auto& result : storage) {
354
355 DEBUG("result: "
356 << setw(3) << result.ns << ' '
357 << setw(3) << result.nb << ' '
358 << FIXED(12,5) << result.likelihood << ' '
359 << FIXED(12,5) << result.signal << endl);
360
361 hl.Fill(result.likelihood);
362 hn.Fill(result.signal - result.ns);
363 }
364 }
365
366 out.Write();
367 out.Close();
368}
369
Container I/O.
string outputFile
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition JHead.hh:1850
Auxiliary methods for mathematics.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define STATUS(A)
Definition JMessage.hh:63
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
int main(int argc, char **argv)
Pseudo experiment.
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
Auxiliary class to handle file name, ROOT directory and object name.
Utility class to parse command line options.
Definition JParser.hh:1698
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
std::ostream & operator<<(std::ostream &out, const morphology_type &object)
Write morphology to output stream.
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
Auxiliary container for statistical analysis of a large number of positive (or zero) values.
Definition JGarage.hh:30
double getProbability(const T value) const
Get maximal probability corresponding given minimal value.
Definition JGarage.hh:196
T getValue(const double P) const
Get minimal value corresponding given maximal probability.
Definition JGarage.hh:154
void put(const T value)
Add value to container.
Definition JGarage.hh:105
Pseudo experiment using CDF for combined generation and likelihood evaluation.
void add(const TObject *ps, const TObject *pb)
Add objects with PDFs of signal and background.
struct JASTRONOMY::JPseudoExperiment::parameters_type nuisance
double getSignal() const
Get total signal.
virtual void set(const double fS, const double fB=1.0) override
Set scaling factors of signal and background strengths.
double getBackground() const
Get total background.
void configure(size_t N)
Configure lookup tables.
Data structure for measured coincidence rates of all pairs of PMTs in optical module.
Definition JFitK40.hh:103
Auxiliary wrapper for I/O of container with optional comment (see JComment).
Definition JContainer.hh:42
Template definition of random value generator.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:68
Simple data structure for histogram binning.
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488