Jpp 20.0.0-rc.7
the software that should make you happy
Loading...
Searching...
No Matches
JGen2UpperLimit.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
143 string outputFile;
144 size_t numberOfTests;
145 size_t M;
146 double SNR;
147 histogram_type X;
148 JRandom seed;
149 double precision;
150 int debug;
151
152 try {
153
154 JParser<> zap;
155
157
158 zap['E'] = make_field(setup,
159 "douplets of signal and background histograms and doublets of signal and background nuisances, \n"
160 << "\teach of which defined by <file name>:<histogram name> and <type> (values), respectively, \n"
161 << "\twhere <type> can be:" << get_keys(px.nuisance.signal.getDictionary()));
162 zap['o'] = make_field(outputFile, "output file with histograms") = "benchmark.root";
163 zap['n'] = make_field(numberOfTests, "number of tests");
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['x'] = make_field(X, "x-axis likelihood histogram") = histogram_type(100, -1.0e3, +1.0e3);
167 zap['p'] = make_field(precision, "precision") = 1.0e-4;
168 zap['S'] = make_field(seed) = 0;
169 zap['d'] = make_field(debug) = 1;
170
171 zap(argc, argv);
172 }
173 catch(const exception& error) {
174 FATAL(error.what() << endl);
175 }
176
177
178 seed.set(gRandom);
179
180 JExperiment::setSNR(SNR);
181
182 const double Q = 0.9; // probability limit
183
184 JGen2 px;
185
186 for (const auto& i : setup) {
187
188 const TObject* pS = getObject(i.HS);
189 const TObject* pB = getObject(i.HB);
190 const TObject* ps = getObject(i.Hs);
191 const TObject* pb = getObject(i.Hb);
192
193 STATUS(printer("Signal for generation:", pS) << endl);
194 STATUS(printer("Background for generation:", pB) << endl);
195 STATUS(printer("Signal for evaluation:", ps) << endl);
196 STATUS(printer("Background for evaluation:", pb) << endl);
197
198 JPseudoExperiment pi(pS, pB, ps, pb);
199
200 pi.nuisance = i.nuisance;
201
202 px.push_back(pi);
203 }
204
205 if (M != 0) {
206 px.configure(M);
207 }
208
209 STATUS("Signal: " << FIXED(10,3) << px.getSignal() << endl);
210 STATUS("Background: " << FIXED(10,3) << px.getBackground() << endl);
211 STATUS("Number of tests: " << setw(10) << numberOfTests << endl);
212
213
214 TFile out(outputFile.c_str(), "recreate");
215
216 out << *gRandom;
217
218 TH1D h0("h0", NULL, X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
219 TH1D h1("h1", NULL, X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
220
221
222 // generate background only pseudo experiments and store fitted signals
223
224 px.set(0.0);
225
226 for (size_t i = 0; i != numberOfTests; ++i) {
227
228 JAspera aspera;
229
230 px.run(aspera);
231
232 h0.Fill(aspera(true).signal);
233 }
234
235
236 vector<double> mu_up;
237
238 for (size_t i = 0; i != numberOfTests; ++i) {
239
240 if ((i*10) < numberOfTests || (i*10)%numberOfTests == 0) {
241 STATUS(setw(6) << i << '\r'); DEBUG(endl);
242 }
243
244 JAspera aspera;
245
246 px.set(0.0);
247
248 px.run(aspera);
249
250 mu_up.push_back(px.getSignalStrengthForUpperLimit(aspera, Q, numberOfTests));
251 }
252 STATUS(endl);
253
254 for (const double x : mu_up) {
255 h1.Fill(x);
256 }
257
258 sort(mu_up.begin(), mu_up.end());
259
260 cout << "<mu> " << FIXED(7,3) << mu_up[mu_up.size() / 2] << endl;
261
262 out.Write();
263 out.Close();
264}
265
Container I/O.
string outputFile
int main(int argc, char **argv)
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.
Definition JAspera.hh:24
Auxiliary data structure to fit signal strength using likelihood ratio for multiple pseudo experiment...
Definition JGen2.hh:28
virtual stats_type run(JAspera &out) const
Generate pseudo experiment and transfer S/N values to fit method.
Definition JGen2.hh:95
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
double getSignalStrengthForUpperLimit(const JAspera &aspera, const double Q, const size_t numberOfTests, const double precision=1.0e-4)
Get signal strength given result of experiment and probability of upper limit.
Pseudo experiment using CDF for combined generation and likelihood evaluation.
struct JASTRONOMY::JPseudoExperiment::parameters_type nuisance
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.