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