Jpp 20.0.0-rc.8
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
18
20
21#include "Jeep/JContainer.hh"
22#include "Jeep/JParser.hh"
23
24
25namespace {
26
27 /**
28 * Auxiliary data structure for experiment.
29 */
30 struct experiment_type {
31
32 JGIZMO::JRootObjectID Hd; //!< data
33 JGIZMO::JRootObjectID Hs; //!< signal
34 JGIZMO::JRootObjectID Hb; //!< background
35
36 /**
37 * Read experiment from input stream.
38 *
39 * \param in input stream
40 * \param experiment experiment
41 * \return input stream
42 */
43 friend inline std::istream& operator>>(std::istream& in, experiment_type& experiment)
44 {
45 return in >> experiment.Hd
46 >> experiment.Hs
47 >> experiment.Hb;
48 }
49
50
51 /**
52 * Write experiment to output stream.
53 *
54 * \param out output stream
55 * \param experiment experiment
56 * \return output stream
57 */
58 friend inline std::ostream& operator<<(std::ostream& out, const experiment_type& experiment)
59 {
60 return out << experiment.Hd << ' '
61 << experiment.Hs << ' '
62 << experiment.Hb;
63 }
64 };
65
66
67 /**
68 * Auxiliary data structure for printing.
69 */
70 struct printer {
71 /**
72 * Constructor.
73 *
74 * \param title title
75 * \param ps pointer to object
76 */
77 printer(const char* const title,
78 const TObject* ps) :
79 title(title),
80 ps(ps)
81 {}
82
83 /**
84 * Write printer to output stream.
85 *
86 * \param out output stream
87 * \param printer printer
88 * \return output stream
89 */
90 friend inline std::ostream& operator<<(std::ostream& out, const printer& printer)
91 {
92 using namespace std;
93 using namespace JPP;
94
95 out << setw(16) << left << printer.title << right;
96
97 if (printer.ps != NULL) {
98
99 out << ' ' << setw(16) << left << printer.ps->GetName() << right;
100
101 const TH1* h1 = dynamic_cast<const TH1*>(printer.ps);
102
103 if (h1 != NULL) {
104 out << ' ' << FIXED(10,3) << h1->GetSumOfWeights();
105 }
106 }
107
108 return out;
109 }
110
111 private:
112 const char* const title;
113 const TObject* ps;
114 };
115}
116
117
118/**
119 * \file
120 * Test application for pseudo experiment generation and likelihood ratio evaluations.
121 *
122 * \author mdejong
123 */
124int main(int argc, char **argv)
125{
126 using namespace std;
127 using namespace JPP;
128
130 size_t numberOfTests;
131 double SNR;
132 int debug;
133
134 try {
135
136 JParser<> zap;
137
138 zap['E'] = make_field(setup,
139 "triplets of data, signal and background histograms, "\
140 "each of which defined by <file name>:<histogram name>");
141 zap['n'] = make_field(numberOfTests, "number of tests for upper limit calculation") = 0;
142 zap['R'] = make_field(SNR, "signal-to-noise ratio") = 0.0;
143 zap['d'] = make_field(debug) = 1;
144
145 zap(argc, argv);
146 }
147 catch(const exception& error) {
148 FATAL(error.what() << endl);
149 }
150
151
152 JExperiment::setSNR(SNR);
153
156
157 for (const auto& i : setup) {
158
159 const TObject* pd = getObject(i.Hd);
160 const TObject* ps = getObject(i.Hs);
161 const TObject* pb = getObject(i.Hb);
162
163 STATUS(printer("Data:", pd) << endl);
164 STATUS(printer("Signal:", ps) << endl);
165 STATUS(printer("Background:", pb) << endl);
166
167 rx.add(pd, ps, pb);
168 px.add(ps, pb);
169 }
170
171
172 const JRealExperiment::fit_type result = rx();
173
174
175 cout << "signal: " << FIXED(9,3) << rx.getSignal() << "/" << setw(6) << rx.size() << endl;
176 cout << "derivative: " << FIXED(9,3) << rx.getDerivative(0.0) << endl;
177
178 if (debug >= debug_t) {
179
180 size_t n = 0;
181
182 for (double x : rx) {
183
184 cout << SCIENTIFIC(12,3) << 1.0/x << ((n + 1)%10 == 0 ? "\n" : " ");
185
186 n += 1;
187 }
188 cout << endl;
189 }
190
191 cout << "result: "
192 << FIXED(12,5) << result.likelihood << ' '
193 << FIXED(12,5) << result.signal << endl;
194
195 if (numberOfTests != 0) {
196
197 const double Q = 0.9; // probability limit
198
199 const double mu = px.getSignalStrengthForUpperLimit(rx, Q, numberOfTests);
200
201 cout << "upper limit: " << FIXED(12,5) << mu << endl;
202 }
203}
204
Container I/O.
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
Pseudo experiment.
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)
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
double getSignal() const
Get total signal strength.
Definition JAspera.hh:236
double getDerivative(const double p) const
Get derivative of likelihood for given signal strength.
Definition JAspera.hh:96
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.
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