Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
JGen2.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/JGen2.hh"
#include "JAstronomy/JGarage.hh"
#include "JAstronomy/JAstronomyToolkit.hh"
#include "JGizmo/JGizmoToolkit.hh"
#include "JTools/JAbstractHistogram.hh"
#include "JROOT/JRandom.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 JGen2.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 135 of file JGen2.cc.

136{
137 using namespace std;
138 using namespace JPP;
139
140 typedef JAbstractHistogram<double> histogram_type;
141 typedef float data_type;
142
144 string outputFile;
145 size_t numberOfTests;
146 double Fs;
147 double Fb;
148 size_t M;
149 double SNR;
150 bool add;
151 histogram_type X;
152 data_type Q;
153 double P;
154 JRandom seed;
155 int debug;
156
157 try {
158
159 JParser<> zap;
160
162
163 zap['E'] = make_field(setup,
164 "douplets of signal and background histograms and doublets of signal and background nuisances, \n"
165 << "\teach of which defined by <file name>:<histogram name> and <type> (values), respectively, \n"
166 << "\twhere <type> can be:" << get_keys(nuisance_helper));
167 zap['o'] = make_field(outputFile, "output file with histograms") = "benchmark.root";
168 zap['s'] = make_field(Fs, "signal strength");
169 zap['b'] = make_field(Fb, "background strength") = 1.0;
170 zap['n'] = make_field(numberOfTests, "number of tests");
171 zap['M'] = make_field(M, "lookup table for CDFs") = 0;
172 zap['R'] = make_field(SNR, "signal-to-noise ratio") = 0.0;
173 zap['A'] = make_field(add, "add remnant signal and noise");
174 zap['x'] = make_field(X, "x-axis likelihood histogram") = histogram_type(110, -10.0, +100.0);
175 zap['Q'] = make_field(Q, "minimal likelihood ratio") = 0.0;
176 zap['P'] = make_field(P, "maximal probability") = 0.0;
177 zap['S'] = make_field(seed) = 0;
178 zap['d'] = make_field(debug) = 1;
179
180 zap(argc, argv);
181 }
182 catch(const exception& error) {
183 FATAL(error.what() << endl);
184 }
185
186
187 seed.set(gRandom);
188
189 JExperiment::setSNR(SNR);
190
191
192 JGen2 px;
193
194 for (const auto& i : setup) {
195
196 const TObject* pS = getObject(i.HS);
197 const TObject* pB = getObject(i.HB);
198 const TObject* ps = getObject(i.Hs);
199 const TObject* pb = getObject(i.Hb);
200
201 JPseudoExperiment pi(pS, pB, ps, pb);
202 pi.nuisance = i.nuisance;
203 px.push_back(pi);
204
205 STATUS(printer("Signal for generation: ", pS) << endl);
206 STATUS(printer("Background for generation:", pB) << endl);
207 STATUS(printer("Signal for evaluation: ", ps) << endl);
208 STATUS(printer("Background for evaluation:", pb) << endl);
209 STATUS( "Nuisance: "<< pi.nuisance << endl);
210
211 }
212
213 px.set(Fs, Fb);
214
215 if (M != 0) {
216 px.configure(M);
217 }
218
219 if (add) {
220 px.add();
221 }
222
223 STATUS("Signal: " << FIXED(10,3) << px.getSignal() << endl);
224 STATUS("Background: " << FIXED(10,3) << px.getBackground() << endl);
225 STATUS("Number of tests: " << setw(10) << numberOfTests << endl);
226
227 if (px.getBackground() <= 0.0) {
228 FATAL("No background." << endl);
229 }
230
231 if (debug >= debug_t) {
232 for (size_t i = 0; i != px.size(); ++i) {
233 cout << setw(2) << i << ' ' << px[i].nuisance.signal << ' ' << px[i].nuisance.background << endl;
234 }
235 }
236
237 TFile out(outputFile.c_str(), "recreate");
238
239 out << *gRandom;
240
241 TH1D hl("hl", NULL, X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
242 TH1D hn("hn", NULL, 100, -15.0, +15.0);
243
244
245 if (P > 0.0 && Q > 0.0) {
246
247 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
248
249 const double signal = discovery<float, 100>(px, Q, P, numberOfTests);
250
251 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
252
253 STATUS("Total time: " << setw(8) << (t1 - t0) / chrono::milliseconds(1) << " [ms]" << endl);
254
255 cout << "Signal strength: " << FIXED(9,5) << signal << endl;
256
257 } else if (P > 0.0 || Q > 0.0) {
258
260
261 {
262 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
263
264 for (size_t i = 0; i != numberOfTests; ++i) {
265 storage.put(px().likelihood);
266 }
267
268 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
269
270 STATUS("Average time: " << setw(6) << (t1 - t0) / chrono::nanoseconds(numberOfTests) << " [ns]" << endl);
271 }
272
273 storage([&hl](const data_type x) { hl.Fill(x); });
274
275 double x = 0.0;
276 double p = 0.0;
277
278 {
279 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
280
281 if (P > 0.0) { // determine minimal likelihood ratio given maximal probability
282 x = storage.getValue(P);
283 p = storage.getProbability(x);
284 }
285
286 if (Q > 0.0) { // determine maximal probability given minimal likelihood ratio
287 p = storage.getProbability(Q);
288 x = storage.getValue(p);
289 }
290
291 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
292
293 STATUS("Time to result: " << setw(6) << (t1 - t0) / chrono::microseconds(1) << " [us]" << endl);
294 }
295
296 if (P > 0.0) { cout << "Minimal likelihood ratio: " << FIXED(9,5) << x << ' ' << SCIENTIFIC(12,3) << p << endl; }
297 if (Q > 0.0) { cout << "Maximal probability: " << SCIENTIFIC(12,3) << p << ' ' << FIXED(9,5) << x << endl; }
298
299 } else if (numberOfTests != 0) {
300
301 std::vector<JPseudoExperiment::result_type> storage(numberOfTests);
302
303 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
304
305 px(storage);
306
307 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
308
309 STATUS("Average time: " << setw(6) << (t1 - t0) / chrono::nanoseconds(storage.size()) << " [ns]" << endl);
310
311 for (const auto& result : storage) {
312
313 DEBUG("result: "
314 << setw(3) << result.ns << ' '
315 << setw(3) << result.nb << ' '
316 << FIXED(12,5) << result.likelihood << ' '
317 << FIXED(12,5) << result.signal << endl);
318
319 hl.Fill(result.likelihood);
320 hn.Fill(result.signal - result.ns);
321 }
322 }
323
324 out.Write();
325 out.Close();
326}
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:2107
Utility class to parse command line options.
Definition JParser.hh:1664
TObject * getObject(const JRootObjectID &id)
Get first TObject with given identifier.
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).
Definition setup.py:1
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
Auxiliary data structure to fit signal strength using likelihood ratio for multiple pseudo experiment...
Definition JGen2.hh:28
double getSignal() const
Get total signal.
Definition JGen2.hh:57
void add()
Add remnant signal and background.
Definition JGen2.hh:32
virtual void set(const double fS, const double fB=1.0) override
Set scaling factors of signal and background strengths.
Definition JGen2.hh:80
void configure(size_t N)
Configure lookup tables.
Definition JGen2.hh:44
double getBackground() const
Get total background.
Definition JGen2.hh:68
Pseudo experiment using CDF for combined generation and likelihood evaluation.
Data structure for measured coincidence rates of all pairs of PMTs in optical module.
Definition JFitK40.hh:110
Auxiliary wrapper for I/O of container with optional comment (see JComment).
Definition JContainer.hh:41
Template definition of random value generator.
Simple data structure for histogram binning.
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488