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