Jpp 20.0.0-rc.8
the software that should make you happy
Loading...
Searching...
No Matches
JGen2.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
17#include "JAstronomy/JGen2.hh"
18#include "JAstronomy/JGarage.hh"
20
23#include "JROOT/JRandom.hh"
24
25#include "Jeep/JContainer.hh"
26#include "Jeep/JParser.hh"
27
28
29namespace {
30
31 /**
32 * Auxiliary data structure for experiment.
33 */
34 struct experiment_type {
35
36 JGIZMO::JRootObjectID HS; //!< signal for generation
37 JGIZMO::JRootObjectID HB; //!< background for generation
38 JGIZMO::JRootObjectID Hs; //!< signal for evaluation
39 JGIZMO::JRootObjectID Hb; //!< background for evaluation
40
42
43 /**
44 * Read experiment from input stream.
45 *
46 * \param in input stream
47 * \param experiment experiment
48 * \return input stream
49 */
50 friend inline std::istream& operator>>(std::istream& in, experiment_type& experiment)
51 {
52 return in >> experiment.HS
53 >> experiment.HB
54 >> experiment.Hs
55 >> experiment.Hb
56 >> experiment.nuisance;
57 }
58
59
60 /**
61 * Write experiment to output stream.
62 *
63 * \param out output stream
64 * \param experiment experiment
65 * \return output stream
66 */
67 friend inline std::ostream& operator<<(std::ostream& out, const experiment_type& experiment)
68 {
69 return out << experiment.HS << ' '
70 << experiment.HB << ' '
71 << experiment.Hs << ' '
72 << experiment.Hb << ' '
73 << experiment.nuisance;
74 }
75 };
76
77
78 /**
79 * Auxiliary data structure for printing.
80 */
81 struct printer {
82 /**
83 * Constructor.
84 *
85 * \param title title
86 * \param ps pointer to object
87 */
88 printer(const char* const title,
89 const TObject* ps) :
90 title(title),
91 ps(ps)
92 {}
93
94 /**
95 * Write printer to output stream.
96 *
97 * \param out output stream
98 * \param printer printer
99 * \return output stream
100 */
101 friend inline std::ostream& operator<<(std::ostream& out, const printer& printer)
102 {
103 using namespace std;
104 using namespace JPP;
105
106 out << setw(16) << left << printer.title << right;
107
108 if (printer.ps != NULL) {
109
110 out << ' ' << setw(16) << left << printer.ps->GetName() << right;
111
112 const TH1* h1 = dynamic_cast<const TH1*>(printer.ps);
113
114 if (h1 != NULL) {
115 out << ' ' << FIXED(10,3) << h1->GetSumOfWeights();
116 }
117 }
118
119 return out;
120 }
121
122 private:
123 const char* const title;
124 const TObject* ps;
125 };
126}
127
128
129/**
130 * \file
131 * Test application for pseudo experiment generation and likelihood ratio evaluations.
132 *
133 * \author mdejong
134 */
135int main(int argc, char **argv)
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 histogram_type X;
151 data_type Q;
152 double P;
153 JRandom seed;
154 int debug;
155
156 try {
157
158 JParser<> zap;
159
161
162 zap['E'] = make_field(setup,
163 "douplets of signal and background histograms and doublets of signal and background nuisances, \n"
164 << "\teach of which defined by <file name>:<histogram name> and <type> (values), respectively, \n"
165 << "\twhere <type> can be:" << get_keys(px.nuisance.signal.getDictionary()));
166 zap['o'] = make_field(outputFile, "output file with histograms") = "benchmark.root";
167 zap['s'] = make_field(Fs, "signal strength");
168 zap['b'] = make_field(Fb, "background strength") = 1.0;
169 zap['n'] = make_field(numberOfTests, "number of tests");
170 zap['M'] = make_field(M, "lookup table for CDFs") = 0;
171 zap['R'] = make_field(SNR, "signal-to-noise ratio") = 0.0;
172 zap['x'] = make_field(X, "x-axis likelihood histogram") = histogram_type(110, -10.0, +100.0);
173 zap['Q'] = make_field(Q, "minimal likelihood ratio") = 0.0;
174 zap['P'] = make_field(P, "maximal probability") = 0.0;
175 zap['S'] = make_field(seed) = 0;
176 zap['d'] = make_field(debug) = 1;
177
178 zap(argc, argv);
179 }
180 catch(const exception& error) {
181 FATAL(error.what() << endl);
182 }
183
184
185 seed.set(gRandom);
186
187 JExperiment::setSNR(SNR);
188
189
190 JGen2 px;
191
192 for (const auto& i : setup) {
193
194 const TObject* pS = getObject(i.HS);
195 const TObject* pB = getObject(i.HB);
196 const TObject* ps = getObject(i.Hs);
197 const TObject* pb = getObject(i.Hb);
198
199 STATUS(printer("Signal for generation:", pS) << endl);
200 STATUS(printer("Background for generation:", pB) << endl);
201 STATUS(printer("Signal for evaluation:", ps) << endl);
202 STATUS(printer("Background for evaluation:", pb) << endl);
203
204 JPseudoExperiment pi(pS, pB, ps, pb);
205
206 pi.nuisance = i.nuisance;
207
208 px.push_back(pi);
209 }
210
211 px.set(Fs, Fb);
212
213 if (M != 0) {
214 px.configure(M);
215 }
216
217 STATUS("Signal: " << FIXED(10,3) << px.getSignal() << endl);
218 STATUS("Background: " << FIXED(10,3) << px.getBackground() << endl);
219 STATUS("Number of tests: " << setw(10) << numberOfTests << endl);
220
221
222 TFile out(outputFile.c_str(), "recreate");
223
224 out << *gRandom;
225
226 TH1D hl("hl", NULL, X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
227 TH1D hn("hn", NULL, 100, -15.0, +15.0);
228
229
230 if (P > 0.0 && Q > 0.0) {
231
232 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
233
234 const double signal = discovery<float, 100>(px, Q, P, numberOfTests);
235
236 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
237
238 STATUS("Total time: " << setw(8) << (t1 - t0) / chrono::milliseconds(1) << " [ms]" << endl);
239
240 cout << "Signal strength: " << FIXED(9,5) << signal << endl;
241
242 } else if (P > 0.0 || Q > 0.0) {
243
245
246 {
247 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
248
249 for (size_t i = 0; i != numberOfTests; ++i) {
250 storage.put(px().likelihood);
251 }
252
253 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
254
255 STATUS("Average time: " << setw(6) << (t1 - t0) / chrono::nanoseconds(numberOfTests) << " [ns]" << endl);
256 }
257
258 storage([&hl](const data_type x) { hl.Fill(x); });
259
260 double x = 0.0;
261 double p = 0.0;
262
263 {
264 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
265
266 if (P > 0.0) { // determine minimal likelihood ratio given maximal probability
267 x = storage.getValue(P);
268 p = storage.getProbability(x);
269 }
270
271 if (Q > 0.0) { // determine maximal probability given minimal likelihood ratio
272 p = storage.getProbability(Q);
273 x = storage.getValue(p);
274 }
275
276 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
277
278 STATUS("Time to result: " << setw(6) << (t1 - t0) / chrono::microseconds(1) << " [us]" << endl);
279 }
280
281 if (P > 0.0) { cout << "Minimal likelihood ratio: " << FIXED(9,5) << x << ' ' << SCIENTIFIC(12,3) << p << endl; }
282 if (Q > 0.0) { cout << "Maximal probability: " << SCIENTIFIC(12,3) << p << ' ' << FIXED(9,5) << x << endl; }
283
284 } else {
285
286 std::vector<JPseudoExperiment::result_type> storage(numberOfTests);
287
288 const chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
289
290 px(storage);
291
292 const chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
293
294 STATUS("Average time: " << setw(6) << (t1 - t0) / chrono::nanoseconds(storage.size()) << " [ns]" << endl);
295
296 for (const auto& result : storage) {
297
298 DEBUG("result: "
299 << setw(3) << result.ns << ' '
300 << setw(3) << result.nb << ' '
301 << FIXED(12,5) << result.likelihood << ' '
302 << FIXED(12,5) << result.signal << endl);
303
304 hl.Fill(result.likelihood);
305 hn.Fill(result.signal - result.ns);
306 }
307 }
308
309 out.Write();
310 out.Close();
311}
312
Container I/O.
string outputFile
int main(int argc, char **argv)
Definition JGen2.cc:135
Set of pseudo experments.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition JHead.hh:1850
#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
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)
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
JWriter & operator<<(JWriter &out, const JDAQChronometer &chronometer)
Write DAQ chronometer to output.
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
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:47
virtual void set(const double fS, const double fB=1.0) override
Set scaling factors of signal and background strengths.
Definition JGen2.hh:70
void configure(size_t N)
Configure lookup tables.
Definition JGen2.hh:34
double getBackground() const
Get total background.
Definition JGen2.hh:58
dictionary_type & getDictionary()
Get dictionary.
Definition JNuisance.hh:372
Pseudo experiment using CDF for combined generation and likelihood evaluation.
struct JASTRONOMY::JPseudoExperiment::parameters_type nuisance
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.
Simple data structure for histogram binning.
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488