Jpp 20.0.0-195-g190c9e876
the software that should make you happy
Loading...
Searching...
No Matches
JPseudoExperimentCondNLLR.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 "TH2D.h"
13#include "TRandom3.h"
14
16#include "JAstronomy/JAspera.hh"
18
21#include "JROOT/JRandom.hh"
22
23#include "Jeep/JContainer.hh"
24#include "Jeep/JParser.hh"
25
26
27namespace {
28
29 int debug = 2;
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 double boost; //!< boosting factor for signal and background
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.boost
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.boost << ' '
76 << experiment.nuisance;
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 * Auxiliary data structure for grouping experiments.
133 */
134 struct experiment_group {
136 double signal_weight;
137
138 experiment_group(const std::vector<experiment_type>& setup_vec,
139 double Fs,
140 double Fb,
141 size_t M)
142 {
143 signal_weight = 0;
144
145 for ( const auto& setup : setup_vec) {
146 TObject* pS = getObject(setup.HS);
147 TObject* pB = getObject(setup.HB);
148 TObject* ps = getObject(setup.Hs);
149 TObject* pb = getObject(setup.Hb);
150
151 if (setup.boost != 1.) {
152 std::cout << "Boosting background and signal for following dataset by factor " << setup.boost << std::endl;
153 dynamic_cast<TH1*>(pS)->Scale(setup.boost);
154 dynamic_cast<TH1*>(pB)->Scale(setup.boost);
155 dynamic_cast<TH1*>(ps)->Scale(setup.boost);
156 dynamic_cast<TH1*>(pb)->Scale(setup.boost);
157 }
158
159 signal_weight += dynamic_cast<const TH1*>(ps)->GetSumOfWeights();
160
161 STATUS(printer("Signal for generation:", pS) << std::endl);
162 STATUS(printer("Background for generation:", pB) << std::endl);
163
164 JASTRONOMY::JPseudoExperiment pi(pS, pB, ps, pb);
165
166 pi.nuisance = setup.nuisance;
167 pi.set(Fs, Fb); // Set the strength of signal and background for generation
168
169 if (M != 0) {
170 pi.configure(M);
171 }
172
173 px_vec.push_back(pi);
174 }
175 }
176 };
177}
178
179
180/**
181 * \file
182 * Application for generating conditional Likelihood histograms
183 * of part of a dataset compared to the complete dataset.
184 * Used to generate trial factors with JTrialFactorsFromCondNLLR.cc
185 *
186 * \author mdejong fvazquezdesola
187 */
188int main(int argc, char **argv)
189{
190 using namespace std;
191 using namespace JPP;
192
193 typedef JAbstractHistogram<double> histogram_type;
194
197 string outputFile;
198 size_t numberOfTests;
199 double Fs;
200 double Fb;
201 size_t M;
202 double SNR;
203 histogram_type X;
204 JRandom seed;
205 int debug;
206
207 try {
208
209 JParser<> zap;
211
212 zap['E'] = make_field(setup_vec1,
213 "inputs for 1st data period: signal and background histograms for generation and evaluation,\n"
214 << "\texposure boost, and signal and background nuisances, provided as\n"
215 << "\t'<file>:<hgen_s name> <file>:<hgen_b name> <file>:<heval_s name> <file>:<heval_b name> <boost> <type_s> (values) <type_b> (values)' \n"
216 << "\twhere <type> can be:" << get_keys(nuisance_helper) <<"\n."
217 << "\t Can be called repeatedly to add multiple datasets to this period");
218 zap['F'] = make_field(setup_vec2,
219 "inputs for 2nd data period: signal and background histograms for generation and evaluation,\n"
220 << "\texposure boost, and signal and background nuisances, provided as\n"
221 << "\t'<file>:<hgen_s name> <file>:<hgen_b name> <file>:<heval_s name> <file>:<heval_b name> <boost> <type_s> (values) <type_b> (values)' \n"
222 << "\twhere <type> can be:" << get_keys(nuisance_helper) <<"\n."
223 << "\t Can be called repeatedly to add multiple datasets to this period");
224 zap['o'] = make_field(outputFile, "output file with histograms") = "CondNLLR.root";
225 zap['s'] = make_field(Fs, "signal strength");
226 zap['b'] = make_field(Fb, "background strength") = 1.0;
227 zap['M'] = make_field(M, "lookup table for CDFs") = 0;
228 zap['R'] = make_field(SNR, "signal-to-noise ratio") = 0.0;
229 zap['n'] = make_field(numberOfTests, "number of tests / PEs");
230 zap['x'] = make_field(X, "x-axis for likelihood histogram") = histogram_type(200, 0, +20.0);
231 zap['S'] = make_field(seed) = 0;
232 zap['d'] = make_field(debug) = 1;
233
234 zap(argc, argv);
235 }
236 catch(const exception& error) {
237 FATAL(error.what() << endl);
238 }
239
240
241 seed.set(gRandom);
242
243 JExperiment::setSNR(SNR);
244
245 cout << "**Setting up 1st period**" << endl;
246 experiment_group pxg_1(setup_vec1, Fs, Fb, M);
247 cout << "**Setting up 2nd period**" << endl;
248 experiment_group pxg_2(setup_vec2, Fs, Fb, M);
249
250 TFile out(outputFile.c_str(), "recreate");
251
252 TH2D h2d_nllr("h2d_nllr", "Negative Log-Likelihood Ratio; NLLR 1st period; NLLR total",
253 X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit(),
254 X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
255 TH2D h2d_nllr_constr("h2d_nllr_constr", "Negative Log-Likelihood Ratio (filtered); NLLR 1st period; NLLR total",
256 X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit(),
257 X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
258
259 size_t nexp_passfilter = 0;
260
261 for (size_t nexp =0; nexp != numberOfTests; ++nexp) {
262 if (nexp%100000 == 0) {
263 cout << "Done " << nexp << " pseudo-experiments out of " << numberOfTests << endl;
264 }
265
266 JAspera aspera;
267 JAspera::fit_type result_1;
268 JAspera::fit_type result_A;
269
270 for (const auto& px : pxg_1.px_vec) { px.run(aspera); }
271 result_1 = aspera();
272
273 for (const auto& px : pxg_2.px_vec) { px.run(aspera); }
274 result_A = aspera();
275
276 auto NLLR_1 = result_1.likelihood;
277 auto NLLR_A = result_A.likelihood;
278
279 if (NLLR_1 < 0) {
280 NLLR_1 = 0;
281 }
282
283 if (NLLR_A < 0) {
284 NLLR_A = 0;
285 }
286
287 h2d_nllr.Fill(NLLR_1, NLLR_A, 1./numberOfTests);
288
289 if( result_1.signal * pxg_1.signal_weight > 1) {
290 nexp_passfilter++;
291 h2d_nllr_constr.Fill(NLLR_1, NLLR_A, 1./numberOfTests);
292 }
293 }
294
295 cout << nexp_passfilter << " PEs pass the first period filter, out of " << numberOfTests << endl;
296
297 h2d_nllr.Write();
298 h2d_nllr_constr.Write();
299
300 out.Write();
301 out.Close();
302}
303
Per aspera ad astra.
Container I/O.
string outputFile
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:74
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 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.
TObject * getObject(const JRootObjectID &id)
Get first TObject with given identifier.
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 signal
signal strength
Definition JAspera.hh:34
double likelihood
likelihood
Definition JAspera.hh:33
Auxiliary data structure to fit signal strength using likelihood ratio.
Definition JAspera.hh:24
Pseudo experiment using CDF for combined generation and likelihood evaluation.
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.