Jpp 20.0.0-rc.7
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 int debug = 2; //Does this get overwritten properly in main?
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 for evaluation
38 JGIZMO::JRootObjectID Hb; //!< background for evaluation
39 double boost; //!< boosting factor for signal and background
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.boost
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.boost << ' '
75 << experiment.nuisance;
76 }
77 };
78
79
80 /**
81 * Auxiliary data structure for printing.
82 */
83 struct printer {
84 /**
85 * Constructor.
86 *
87 * \param title title
88 * \param ps pointer to object
89 */
90 printer(const char* const title,
91 const TObject* ps) :
92 title(title),
93 ps(ps)
94 {}
95
96 /**
97 * Write printer to output stream.
98 *
99 * \param out output stream
100 * \param printer printer
101 * \return output stream
102 */
103 friend inline std::ostream& operator<<(std::ostream& out, const printer& printer)
104 {
105 using namespace std;
106 using namespace JPP;
107
108 out << setw(16) << left << printer.title << right;
109
110 if (printer.ps != NULL) {
111
112 out << ' ' << setw(16) << left << printer.ps->GetName() << right;
113
114 const TH1* h1 = dynamic_cast<const TH1*>(printer.ps);
115
116 if (h1 != NULL) {
117 out << ' ' << FIXED(10,3) << h1->GetSumOfWeights();
118 }
119 }
120
121 return out;
122 }
123
124 private:
125 const char* const title;
126 const TObject* ps;
127 };
128
129
130 /**
131 * Auxiliary data structure for grouping experiments.
132 */
133 struct experiment_group {
135 double signal_weight;
136
137 experiment_group( std::vector<experiment_type> setup_vec, double Fs, double Fb, size_t M) {
138 signal_weight = 0;
139 for ( const auto& setup : setup_vec) {
140 TObject* pS = getObject(setup.HS);
141 TObject* pB = getObject(setup.HB);
142 TObject* ps = getObject(setup.Hs);
143 TObject* pb = getObject(setup.Hb);
144
145 if (setup.boost != 1.) {
146 std::cout << "Boosting background and signal for following dataset by factor " << setup.boost << std::endl;
147 dynamic_cast<TH1*>(pS)->Scale(setup.boost);
148 dynamic_cast<TH1*>(pB)->Scale(setup.boost);
149 dynamic_cast<TH1*>(ps)->Scale(setup.boost);
150 dynamic_cast<TH1*>(pb)->Scale(setup.boost);
151 }
152
153 signal_weight += dynamic_cast<const TH1*>(ps)->GetSumOfWeights();
154 STATUS(printer("Signal for generation:", pS) << std::endl);
155 STATUS(printer("Background for generation:", pB) << std::endl);
156
157 JASTRONOMY::JPseudoExperiment pi(pS, pB, ps, pb);
158 pi.nuisance = setup.nuisance;
159 pi.set(Fs, Fb); // Set the strength of signal and background for generation
160 if (M != 0) {
161 pi.configure(M);
162 }
163 px_vec.push_back(pi);
164 }
165 }
166 };
167
168}
169
170
171/**
172 * \file
173 * Application for generating conditional Likelihood histograms
174 * of part of a dataset compared to the complete dataset.
175 * Used to generate trial factors with JTrialFactorsFromCondNLLR.cc
176 *
177 * \author mdejong fvazquezdesola
178 */
179int main(int argc, char **argv)
180{
181 using namespace std;
182 using namespace JPP;
183
184 typedef JAbstractHistogram<double> histogram_type;
185
188 string outputFile;
189 size_t numberOfTests;
190 double Fs;
191 double Fb;
192 size_t M;
193 double SNR;
194 histogram_type X;
195 JRandom seed;
196 int debug;
197
198 try {
199
200 JParser<> zap;
202
203 zap['E'] = make_field(setup_vec1,
204 "inputs for 1st data period: signal and background histograms for generation and evaluation,\n"
205 << "\texposure boost, and signal and background nuisances, provided as\n"
206 << "\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"
207 << "\twhere <type> can be:" << get_keys(px.nuisance.signal.getDictionary()) <<"\n."
208 << "\t Can be called repeatedly to add multiple datasets to this period");
209 zap['F'] = make_field(setup_vec2,
210 "inputs for 2nd data period: signal and background histograms for generation and evaluation,\n"
211 << "\texposure boost, and signal and background nuisances, provided as\n"
212 << "\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"
213 << "\twhere <type> can be:" << get_keys(px.nuisance.signal.getDictionary()) <<"\n."
214 << "\t Can be called repeatedly to add multiple datasets to this period");
215 zap['o'] = make_field(outputFile, "output file with histograms") = "CondNLLR.root";
216 zap['s'] = make_field(Fs, "signal strength");
217 zap['b'] = make_field(Fb, "background strength") = 1.0;
218 zap['M'] = make_field(M, "lookup table for CDFs") = 0;
219 zap['R'] = make_field(SNR, "signal-to-noise ratio") = 0.0;
220 zap['n'] = make_field(numberOfTests, "number of tests / PEs");
221 zap['x'] = make_field(X, "x-axis for likelihood histogram") = histogram_type(200, 0, +20.0);
222 zap['S'] = make_field(seed) = 0;
223 zap['d'] = make_field(debug) = 1;
224
225 zap(argc, argv);
226 }
227 catch(const exception& error) {
228 FATAL(error.what() << endl);
229 }
230
231
232 seed.set(gRandom);
233 JExperiment::setSNR(SNR);
234
235
236 cout << "**Setting up 1st period**" << endl;
237 experiment_group pxg_1(setup_vec1, Fs, Fb, M);
238 cout << "**Setting up 2nd period**" << endl;
239 experiment_group pxg_2(setup_vec2, Fs, Fb, M);
240
241 TFile out(outputFile.c_str(), "recreate");
242
243 TH2D h2d_nllr("h2d_nllr", "Negative Log-Likelihood Ratio; NLLR 1st period; NLLR total",
244 X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit(),
245 X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
246 TH2D h2d_nllr_constr("h2d_nllr_constr", "Negative Log-Likelihood Ratio (filtered); NLLR 1st period; NLLR total",
247 X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit(),
248 X.getNumberOfBins(), X.getLowerLimit(), X.getUpperLimit());
249
250 size_t nexp_passfilter = 0;
251 for (size_t nexp =0; nexp != numberOfTests; ++nexp) {
252 if (nexp%100000 == 0) {
253 cout << "Done " << nexp << " pseudo-experiments out of " << numberOfTests << endl;
254 }
255
256 JAspera aspera;
257 JAspera::fit_type result_1;
258 JAspera::fit_type result_A;
259
260 for (const auto& px : pxg_1.px_vec) { px.run(aspera); }
261 result_1 = aspera();
262
263 for (const auto& px : pxg_2.px_vec) { px.run(aspera); }
264 result_A = aspera();
265
266 auto NLLR_1 = result_1.likelihood;
267 auto NLLR_A = result_A.likelihood;
268 if (NLLR_1 < 0) {
269 NLLR_1 = 0;
270 }
271 if (NLLR_A < 0) {
272 NLLR_A = 0;
273 }
274
275 h2d_nllr.Fill(NLLR_1, NLLR_A, 1./numberOfTests);
276 if( result_1.signal * pxg_1.signal_weight > 1) {
277 nexp_passfilter++;
278 h2d_nllr_constr.Fill(NLLR_1, NLLR_A, 1./numberOfTests);
279 }
280
281 }
282
283 cout << nexp_passfilter << " PEs pass the first period filter, out of " << numberOfTests << endl;
284
285 h2d_nllr.Write();
286 h2d_nllr_constr.Write();
287 out.Write();
288 out.Close();
289}
290
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: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 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)
TObject * getObject(const JRootObjectID &id)
Get first TObject with given identifier.
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 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
dictionary_type & getDictionary()
Get dictionary.
Definition JNuisance.hh:372
Pseudo experiment using CDF for combined generation and likelihood evaluation.
struct JASTRONOMY::JPseudoExperiment::parameters_type nuisance
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.