Jpp 20.0.0-rc.8
the software that should make you happy
Loading...
Searching...
No Matches
JPseudoExperimentUpperLimit.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
18#include "JAstronomy/JGarage.hh"
20
21#include "JLang/JVectorize.hh"
24#include "JROOT/JRandom.hh"
26
27#include "Jeep/JContainer.hh"
28#include "Jeep/JParser.hh"
29
30
31namespace {
32
33 /**
34 * Auxiliary data structure for experiment.
35 */
36 struct experiment_type {
37
38 JGIZMO::JRootObjectID Hs; //!< signal
39 JGIZMO::JRootObjectID Hb; //!< background
40
41 /**
42 * Read experiment from input stream.
43 *
44 * \param in input stream
45 * \param experiment experiment
46 * \return input stream
47 */
48 friend inline std::istream& operator>>(std::istream& in, experiment_type& experiment)
49 {
50 return in >> experiment.Hs
51 >> experiment.Hb;
52 }
53
54
55 /**
56 * Write experiment to output stream.
57 *
58 * \param out output stream
59 * \param experiment experiment
60 * \return output stream
61 */
62 friend inline std::ostream& operator<<(std::ostream& out, const experiment_type& experiment)
63 {
64 return out << experiment.Hs << ' '
65 << experiment.Hb;
66 }
67 };
68
69
70 /**
71 * Auxiliary data structure for printing.
72 */
73 struct printer {
74 /**
75 * Constructor.
76 *
77 * \param title title
78 * \param ps pointer to object
79 */
80 printer(const char* const title,
81 const TObject* ps) :
82 title(title),
83 ps(ps)
84 {}
85
86 /**
87 * Write printer to output stream.
88 *
89 * \param out output stream
90 * \param printer printer
91 * \return output stream
92 */
93 friend inline std::ostream& operator<<(std::ostream& out, const printer& printer)
94 {
95 using namespace std;
96 using namespace JPP;
97
98 out << setw(16) << left << printer.title << right;
99
100 if (printer.ps != NULL) {
101
102 out << ' ' << setw(16) << left << printer.ps->GetName() << right;
103
104 const TH1* h1 = dynamic_cast<const TH1*>(printer.ps);
105
106 if (h1 != NULL) {
107 out << ' ' << FIXED(10,3) << h1->GetSumOfWeights();
108 }
109 }
110
111 return out;
112 }
113
114 private:
115 const char* const title;
116 const TObject* ps;
117 };
118}
119
120
121/**
122 * \file
123 * Test application for pseudo experiment generation and upper limit evaluations.
124 *
125 * \author mdejong
126 */
127int main(int argc, char **argv)
128{
129 using namespace std;
130 using namespace JPP;
131
132 typedef JAbstractHistogram<double> histogram_type;
133
135 string outputFile;
136 size_t numberOfTests;
137 double boost;
138 size_t M;
139 double SNR;
140 histogram_type X;
141 JRandom seed;
142 double precision;
143 int debug;
144
145 try {
146
147 JParser<> zap;
148
149 zap['E'] = make_field(setup,
150 "douplets of signal and background histograms, "
151 << "each of which defined by <file name>:<histogram name>");
152 zap['o'] = make_field(outputFile, "output file with histograms") = "benchmark.root";
153 zap['n'] = make_field(numberOfTests, "number of tests");
154 zap['B'] = make_field(boost, "boost livetime of experiment") = 1.0;
155 zap['M'] = make_field(M, "lookup table for CDFs") = 0;
156 zap['R'] = make_field(SNR, "signal-to-noise ratio") = 0.0;
157 zap['x'] = make_field(X, "x-axis signal histogram") = histogram_type(100, -1.0e3, +1.0e3);
158 zap['p'] = make_field(precision, "precision") = 1.0e-4;
159 zap['S'] = make_field(seed) = 0;
160 zap['d'] = make_field(debug) = 1;
161
162 zap(argc, argv);
163 }
164 catch(const exception& error) {
165 FATAL(error.what() << endl);
166 }
167
168
169 seed.set(gRandom);
170
171 JExperiment::setSNR(SNR);
172
173 const double Q = 0.9; // probability limit
174
176
177 for (const auto& i : setup) {
178
179 TObject* ps = getObject(i.Hs);
180 TObject* pb = getObject(i.Hb);
181
182 dynamic_cast<TH1*>(ps)->Scale(boost);
183 dynamic_cast<TH1*>(pb)->Scale(boost);
184
185 STATUS(printer("Signal:", ps) << endl);
186 STATUS(printer("Background:", pb) << endl);
187
188 px.add(ps, pb);
189 }
190
191 if (M != 0) {
192 px.configure(M);
193 }
194
195 STATUS("Signal: " << FIXED(10,3) << px.getSignal() << endl);
196 STATUS("Background: " << FIXED(10,3) << px.getBackground() << endl);
197 STATUS("Number of tests: " << setw(10) << numberOfTests << endl);
198
199
200 TFile out(outputFile.c_str(), "recreate");
201
202 out << *gRandom;
203
204 TH1D h0("h0", NULL, X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
205 TH1D h1("h1", NULL, X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
206
207
208 // generate background only pseudo experiments and store fitted signals
209
210 px.set(0.0);
211
212 for (size_t i = 0; i != numberOfTests; ++i) {
213
214 JAspera aspera;
215
216 px.run(aspera);
217
218 h0.Fill(aspera(true).signal);
219 }
220
221
222 vector<double> mu_up;
223
224 for (size_t i = 0; i != numberOfTests; ++i) {
225
226 if ((i*10) < numberOfTests || (i*10)%numberOfTests == 0) {
227 STATUS(setw(6) << i << '\r'); DEBUG(endl);
228 }
229
230 JAspera aspera;
231
232 px.set(0.0);
233
234 px.run(aspera);
235
236 mu_up.push_back(px.getSignalStrengthForUpperLimit(aspera, Q, numberOfTests));
237 }
238 STATUS(endl);
239
240 for (const double x : mu_up) {
241 h1.Fill(x);
242 }
243
244 sort(mu_up.begin(), mu_up.end());
245
246 cout << "<mu> " << FIXED(7,3) << mu_up[mu_up.size() / 2] << endl;
247
248 out.Write();
249 out.Close();
250}
251
Container I/O.
string outputFile
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition JHead.hh:1850
Auxiliary methods for mathematics.
#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
Auxiliary data structure to fit signal strength using likelihood ratio.
Definition JAspera.hh:24
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.
void add(const TObject *ps, const TObject *pb)
Add objects with PDFs of signal and background.
virtual stats_type run(JAspera &out) const
Generate pseudo experiment and transfer values to fit method.
double getSignal() const
Get total signal.
virtual void set(const double fS, const double fB=1.0) override
Set scaling factors of signal and background strengths.
double getBackground() const
Get total background.
void configure(size_t N)
Configure lookup tables.
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.