Jpp 20.0.0-rc.9
the software that should make you happy
Loading...
Searching...
No Matches
JRealExperiment.cc
Go to the documentation of this file.
1#include <string>
2#include <iostream>
3#include <iomanip>
4#include <vector>
5#include <exception>
6#include <algorithm>
7
8#include "TROOT.h"
9#include "TFile.h"
10#include "TH1D.h"
11#include "TH2D.h"
12#include "TH3D.h"
13#include "TRandom3.h"
14
16#include "JAstronomy/JGen2.hh"
18
20#include "JTools/JAbstractHistogram.hh" //for get_keys
21#include "JROOT/JRandom.hh"
22
23#include "Jeep/JContainer.hh"
24#include "Jeep/JParser.hh"
25
26//#include "JLang/JVectorize.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
39 JGIZMO::JRootObjectID Hb; //!< background
40 JGIZMO::JRootObjectID Hd; //!< data
41
43
44 /**
45 * Read experiment from input stream.
46 *
47 * \param in input stream
48 * \param experiment experiment
49 * \return input stream
50 */
51 friend inline std::istream& operator>>(std::istream& in, experiment_type& experiment)
52 {
53 return in >> experiment.HS
54 >> experiment.HB
55 >> experiment.Hs
56 >> experiment.Hb
57 >> experiment.Hd
58 >> experiment.nuisance;
59 }
60
61
62 /**
63 * Write experiment to output stream.
64 *
65 * \param out output stream
66 * \param experiment experiment
67 * \return output stream
68 */
69 friend inline std::ostream& operator<<(std::ostream& out, const experiment_type& experiment)
70 {
71 return out << experiment.HS << ' '
72 << experiment.HB << ' '
73 << experiment.Hs << ' '
74 << experiment.Hb << ' '
75 << experiment.Hd << ' '
76 << experiment.nuisance;
77
78 }
79 };
80
81
82 /**
83 * Auxiliary data structure for printing.
84 */
85 struct printer {
86 /**
87 * Constructor.
88 *
89 * \param title title
90 * \param ps pointer to object
91 */
92 printer(const char* const title,
93 const TObject* ps) :
94 title(title),
95 ps(ps)
96 {}
97
98 /**
99 * Write printer to output stream.
100 *
101 * \param out output stream
102 * \param printer printer
103 * \return output stream
104 */
105 friend inline std::ostream& operator<<(std::ostream& out, const printer& printer)
106 {
107 using namespace std;
108 using namespace JPP;
109
110 out << setw(16) << left << printer.title << right;
111
112 if (printer.ps != NULL) {
113
114 out << ' ' << setw(16) << left << printer.ps->GetName() << right;
115
116 const TH1* h1 = dynamic_cast<const TH1*>(printer.ps);
117
118 if (h1 != NULL) {
119 out << ' ' << FIXED(10,3) << h1->GetSumOfWeights();
120 }
121 }
122
123 return out;
124 }
125
126 private:
127 const char* const title;
128 const TObject* ps;
129 };
130}
131
132
133/**
134 * \file
135 * Test application for pseudo experiment generation and likelihood ratio evaluations.
136 *
137 * \author mdejong
138 */
139int main(int argc, char **argv)
140{
141 using namespace std;
142 using namespace JPP;
143
145 size_t numberOfTests;
146 size_t M;
147 double SNR;
148 bool add;
149 int debug;
150 double testsignal;
151
152 try {
153
154 JParser<> zap;
155
157
158 zap['E'] = make_field(setup,
159 "quintuplets of signal and background histograms (generation then evaluation) and data, \n"
160 << "\teach of which defined by <file name>:<histogram name> and <type> (values), respectively, \n"
161 << "\twhere <type> can be:" << get_keys(nuisance_helper));
162 zap['n'] = make_field(numberOfTests, "number of tests for upper limit calculation") = 0;
163 zap['M'] = make_field(M, "lookup table for CDFs") = 0;
164 zap['R'] = make_field(SNR, "signal-to-noise ratio") = 0.0;
165 zap['A'] = make_field(add, "add remnant signal and noise");
166 zap['s'] = make_field(testsignal, "signal to test") = -1.;
167 zap['d'] = make_field(debug) = 1;
168
169 zap(argc, argv);
170 }
171 catch(const exception& error) {
172 FATAL(error.what() << endl);
173 }
174
175
176 JExperiment::setSNR(SNR);
177
179 JGen2 px;
180
181 for (const auto& i : setup) {
182
183 const TObject* pS = getObject(i.HS);
184 const TObject* pB = getObject(i.HB);
185 const TObject* ps = getObject(i.Hs);
186 const TObject* pb = getObject(i.Hb);
187 const TObject* pd = getObject(i.Hd);
188
189 JPseudoExperiment pi(pS, pB, ps, pb);
190
191 pi.nuisance = i.nuisance;
192
193 px.push_back(pi);
194 rx.add(pd, ps, pb);
195
196 STATUS(printer("Signal for generation: ", pS) << endl);
197 STATUS(printer("Background for generation:", pB) << endl);
198 STATUS(printer("Signal for evaluation: ", ps) << endl);
199 STATUS(printer("Background for evaluation:", pb) << endl);
200 STATUS(printer("Data: ", pd) << endl);
201 STATUS( "Nuisance: " << pi.nuisance << endl);
202 }
203
204 if (M != 0) {
205 px.configure(M);
206 }
207
208 if (add) {
209 px.add();
210 }
211
212 const JRealExperiment::fit_type result = rx();
213
214 cout << "signal: " << FIXED(9,3) << rx.getSignal() << "/" << setw(6) << rx.size() << endl;
215 cout << "derivative: " << FIXED(9,3) << rx.getDerivative(0.0) << endl;
216 if (testsignal >= 0) {
217 cout << "check TS: " << FIXED(12,5) << rx.getLikelihood(testsignal) << ' ' << FIXED(12,5) << testsignal << endl;
218 }
219
220 if (debug >= debug_t) {
221
222 size_t n = 0;
223
224 for (double x : rx) {
225
226 cout << SCIENTIFIC(12,3) << 1.0/x << ((n + 1)%10 == 0 ? "\n" : " ");
227
228 n += 1;
229 }
230 cout << endl;
231 }
232
233 cout << "result: "
234 << FIXED(12,5) << result.likelihood << ' '
235 << FIXED(12,5) << result.signal << endl;
236
237 if (numberOfTests != 0) {
238
239 const double Q = 0.9; // probability limit
240
241 const double mu = px.getSignalStrengthForUpperLimit(rx, Q, numberOfTests);
242
243 cout << "upper limit: " << FIXED(12,5) << mu << endl;
244 }
245}
246
Container I/O.
Set of pseudo experments.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition JHead.hh:1850
#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)
Real experiment.
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)
std::ostream & operator<<(std::ostream &out, const morphology_type &object)
Write morphology to output stream.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
double getSignal() const
Get total signal strength.
Definition JAspera.hh:236
double getLikelihood(const double p) const
Get likelihood for given signal strength.
Definition JAspera.hh:78
double getDerivative(const double p) const
Get derivative of likelihood for given signal strength.
Definition JAspera.hh:96
Auxiliary data structure to fit signal strength using likelihood ratio for multiple pseudo experiment...
Definition JGen2.hh:28
void add()
Add remnant signal and background.
Definition JGen2.hh:32
void configure(size_t N)
Configure lookup tables.
Definition JGen2.hh:44
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
Real experiment using PDF of signal and background.
void add(const TObject *pd, const TObject *ps, const TObject *pb)
Add objects with data and PDFs of signal and background.
Auxiliary wrapper for I/O of container with optional comment (see JComment).
Definition JContainer.hh:42
Auxiliary data structure for floating point format specification.
Definition JManip.hh:488