Jpp 20.0.0-27-g39925593c-D
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
19#include "JLang/JVectorize.hh"
21#include "JTools/JAbstractHistogram.hh" //for get_keys
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
38 JGIZMO::JRootObjectID Hb; //!< background
39 JGIZMO::JRootObjectID Hd; //!< data
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.Hd
57 >> experiment.nuisance;
58 }
59
60
61 /**
62 * Write experiment to output stream.
63 *
64 * \param out output stream
65 * \param experiment experiment
66 * \return output stream
67 */
68 friend inline std::ostream& operator<<(std::ostream& out, const experiment_type& experiment)
69 {
70 return out << experiment.HS << ' '
71 << experiment.HB << ' '
72 << experiment.Hs << ' '
73 << experiment.Hb << ' '
74 << experiment.Hd << ' '
75 << experiment.nuisance;
76
77 }
78 };
79
80
81 /**
82 * Auxiliary data structure for printing.
83 */
84 struct printer {
85 /**
86 * Constructor.
87 *
88 * \param title title
89 * \param ps pointer to object
90 */
91 printer(const char* const title,
92 const TObject* ps) :
93 title(title),
94 ps(ps)
95 {}
96
97 /**
98 * Write printer to output stream.
99 *
100 * \param out output stream
101 * \param printer printer
102 * \return output stream
103 */
104 friend inline std::ostream& operator<<(std::ostream& out, const printer& printer)
105 {
106 using namespace std;
107 using namespace JPP;
108
109 out << setw(16) << left << printer.title << right;
110
111 if (printer.ps != NULL) {
112
113 out << ' ' << setw(16) << left << printer.ps->GetName() << right;
114
115 const TH1* h1 = dynamic_cast<const TH1*>(printer.ps);
116
117 if (h1 != NULL) {
118 out << ' ' << FIXED(10,3) << h1->GetSumOfWeights();
119 }
120 }
121
122 return out;
123 }
124
125 private:
126 const char* const title;
127 const TObject* ps;
128 };
129}
130
131
132/**
133 * \file
134 * Test application for pseudo experiment generation and likelihood ratio evaluations.
135 *
136 * \author mdejong
137 */
138int main(int argc, char **argv)
139{
140 using namespace std;
141 using namespace JPP;
142
144 size_t numberOfTests;
145 size_t M;
146 double SNR;
147 bool add;
148 int debug;
149 double testsignal;
150
151 try {
152
154
156
157 zap['E'] = make_field(setup,
158 "quintuplets of signal and background histograms (generation then evaluation) and data, \n"
159 << "\teach of which defined by <file name>:<histogram name> and <type> (values), respectively, \n"
160 << "\twhere <type> can be:" << get_keys(nuisance_helper));
161 zap['n'] = make_field(numberOfTests, "number of tests for upper limit calculation") = 0;
162 zap['M'] = make_field(M, "lookup table for CDFs") = 0;
163 zap['R'] = make_field(SNR, "signal-to-noise ratio") = 0.0;
164 zap['A'] = make_field(add, "add remnant signal and noise");
165 zap['s'] = make_field(testsignal, "signal to test") = -1.;
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 JExperiment::setSNR(SNR);
176
178 JGen2 px;
179
180 for (const auto& i : setup) {
181
182 const TObject* pS = getObject(i.HS);
183 const TObject* pB = getObject(i.HB);
184 const TObject* ps = getObject(i.Hs);
185 const TObject* pb = getObject(i.Hb);
186 const TObject* pd = getObject(i.Hd);
187
188 JPseudoExperiment pi(pS, pB, ps, pb);
189
190 pi.nuisance = i.nuisance;
191
192 px.push_back(pi);
193 rx.add(pd, ps, pb);
194
195 STATUS(printer("Signal for generation: ", pS) << endl);
196 STATUS(printer("Background for generation:", pB) << endl);
197 STATUS(printer("Signal for evaluation: ", ps) << endl);
198 STATUS(printer("Background for evaluation:", pb) << endl);
199 STATUS(printer("Data: ", pd) << endl);
200 STATUS( "Nuisance: " << pi.nuisance << endl);
201 }
202
203 if (M != 0) {
204 px.configure(M);
205 }
206
207 if (add) {
208 px.add();
209 rx.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 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.
Template definition of a multi-dimensional oscillation probability interpolation table.
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:247
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.
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 data structure for floating point format specification.
Definition JManip.hh:488