Jpp 20.0.0-195-g190c9e876
the software that should make you happy
Loading...
Searching...
No Matches
JPseudoExperiment.cc File Reference

Test application for pseudo experiment generation and likelihood ratio evaluations. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <vector>
#include <map>
#include <exception>
#include <algorithm>
#include "TROOT.h"
#include "TFile.h"
#include "TH1D.h"
#include "TH2D.h"
#include "TH3D.h"
#include "TRandom3.h"
#include "JAstronomy/JPseudoExperiment.hh"
#include "JAstronomy/JGarage.hh"
#include "JAstronomy/JAstronomyToolkit.hh"
#include "JLang/JVectorize.hh"
#include "JGizmo/JGizmoToolkit.hh"
#include "JTools/JAbstractHistogram.hh"
#include "JROOT/JRandom.hh"
#include "JMath/JMathSupportkit.hh"
#include "Jeep/JContainer.hh"
#include "Jeep/JParser.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Test application for pseudo experiment generation and likelihood ratio evaluations.

Author
mdejong

Definition in file JPseudoExperiment.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 127 of file JPseudoExperiment.cc.

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 if (px.getBackground() <= 0.0) {
217 FATAL("No background." << endl);
218 }
219
220 TFile out(outputFile.c_str(), "recreate");
221
222 out << *gRandom;
223
224 TH1D hl("hl", NULL, X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
225 TH1D hn("hn", NULL, 100, -15.0, +15.0);
226
227
228 if (P.size() > 1) { // special case
229
231
232 double Ps = 0.0; // total probability
233 size_t nb = px.getBackground() + 1; // start value for number of background events
234 size_t mb = 0;
235
236 {
237 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
238
239 for ( ; ; ++nb) {
240
241 const double p = poisson(nb, px.getBackground());
242 const size_t n = p * numberOfTests;
243
244 if (p > P[1]) {
245 continue;
246 }
247
248 if (n == 0) {
249 break;
250 }
251
252 if (mb == 0) {
253 mb = nb;
254
255 }
256 for (size_t i = 0; i != n; ++i) {
257 storage.put(px(nb).likelihood);
258 }
259
260 Ps += p;
261 }
262
263 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
264
265 STATUS("Total time: " << setw(8) << (t1 - t0) / chrono::milliseconds(1) << " [ms]" << endl);
266 STATUS("Average time: " << setw(6) << (t1 - t0) / chrono::nanoseconds(numberOfTests) << " [ns]" << endl);
267 }
268
269 STATUS("Poisson: " << setw(3) << mb << ' ' << SCIENTIFIC(12,3) << Ps << ' ' << setw(8) << storage.size() << endl);
270
271 storage([&hl](const data_type x) { hl.Fill(x); });
272
273 double x = 0.0;
274 double p = 0.0;
275
276 {
277 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
278
279 x = storage.getValue(P[0]/Ps);
280 p = storage.getProbability(x) * Ps;
281
282 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
283
284 STATUS("Time to result: " << setw(6) << (t1 - t0) / chrono::nanoseconds(1) << " [ns]" << endl);
285 }
286
287 cout << "Minimal likelihood ratio: " << FIXED(9,5) << x << ' ' << SCIENTIFIC(12,3) << p << endl;
288
289 } else if (P.size() > 0 && Q > 0.0) {
290
291 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
292
293 const double signal = discovery<float, 100>(px, Q, P[0], numberOfTests);
294
295 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
296
297 STATUS("Total time: " << setw(8) << (t1 - t0) / chrono::milliseconds(1) << " [ms]" << endl);
298
299 cout << "Signal strength: " << FIXED(9,5) << signal << endl;
300
301 } else if (P.size() > 0 || Q > 0.0) {
302
304
305 {
306 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
307
308 for (size_t i = 0; i != numberOfTests; ++i) {
309 storage.put(px().likelihood);
310 }
311
312 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
313
314 STATUS("Total time: " << setw(8) << (t1 - t0) / chrono::milliseconds(1) << " [ms]" << endl);
315 STATUS("Average time: " << setw(6) << (t1 - t0) / chrono::nanoseconds(numberOfTests) << " [ns]" << endl);
316 }
317
318 storage([&hl](const data_type x) { hl.Fill(x); });
319
320 double x = 0.0;
321 double p = 0.0;
322
323 {
324 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
325
326 if (P.size() == 1) { // determine minimal likelihood ratio given maximal probability
327 x = storage.getValue(P[0]);
328 p = storage.getProbability(x);
329 }
330
331 if (Q > 0.0) { // determine maximal probability given minimal likelihood ratio
332 p = storage.getProbability(Q);
333 x = storage.getValue(p);
334 }
335
336 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
337
338 STATUS("Time to result: " << setw(6) << (t1 - t0) / chrono::microseconds(1) << " [us]" << endl);
339 }
340
341 if (P.size() == 1) { cout << "Minimal likelihood ratio: " << FIXED(9,5) << x << ' ' << SCIENTIFIC(12,3) << p << endl; }
342 if (Q > 0.0) { cout << "Maximal probability: " << SCIENTIFIC(12,3) << p << ' ' << FIXED(9,5) << x << endl; }
343
344 } else {
345
346 std::vector<JPseudoExperiment::result_type> storage(numberOfTests);
347
348 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
349
350 px(storage);
351
352 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
353
354 STATUS("Average time: " << setw(6) << (t1 - t0) / chrono::nanoseconds(storage.size()) << " [ns]" << endl);
355
356 for (const auto& result : storage) {
357
358 DEBUG("result: "
359 << setw(3) << result.ns << ' '
360 << setw(3) << result.nb << ' '
361 << FIXED(12,5) << result.likelihood << ' '
362 << FIXED(12,5) << result.signal << endl);
363
364 hl.Fill(result.likelihood);
365 hn.Fill(result.signal - result.ns);
366 }
367 }
368
369 out.Write();
370 out.Close();
371}
string outputFile
#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:74
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
Utility class to parse command line options.
Definition JParser.hh:1698
TObject * getObject(const JRootObjectID &id)
Get first TObject with given identifier.
const array_type< JKey_t > & get_keys(const std::map< JKey_t, JValue_t, JComparator_t, JAllocator_t > &data)
Method to create array of keys of map.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
const int n
Definition JPolint.hh:791
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:199
T getValue(const double P) const
Get minimal value corresponding given maximal probability.
Definition JGarage.hh:157
void put(const T value)
Add value to container.
Definition JGarage.hh:108
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