Jpp 20.0.0-rc.9-22-g74b57fa79
the software that should make you happy
Loading...
Searching...
No Matches
JNuisance.hh
Go to the documentation of this file.
1#ifndef __JASTRONOMY__JNUISANCE__
2#define __JASTRONOMY__JNUISANCE__
3
4#include <string>
5#include <istream>
6#include <ostream>
7#include <iomanip>
8#include <memory>
9#include <map>
10
11#include "TRandom3.h"
12#include "TROOT.h"
13#include "TFile.h"
14#include "TH1D.h"
15#include "TF1.h"
16#include "TString.h"
17
18#include "JLang/JClonable.hh"
19#include "JLang/JTitle.hh"
20#include "JLang/JException.hh"
21
22
23/**
24 * \file
25 *
26 * Nuisance parameter.
27 * \author mdejong
28 */
29namespace JASTRONOMY {}
30namespace JPP { using namespace JASTRONOMY; }
31
32namespace JASTRONOMY {
33
34 using JLANG::JClonable;
35 using JLANG::JTitle;
39
40
41 /**
42 * Interface for nuisance parameter.
43 */
44 struct JNuisance :
45 public JClonable<JNuisance>,
46 public JTitle
47 {
48 /**
49 * Central value.
50 */
51 static constexpr double FACTOR = 1.0;
52
53 /**
54 * Virtual destructor.
55 */
56 virtual ~JNuisance()
57 {}
58
59
60 /**
61 * Read nuisance from input stream.
62 *
63 * \param in input stream
64 * \return input stream
65 */
66 virtual std::istream& read(std::istream& in) = 0;
67
68
69 /**
70 * Write nuisance to output stream.
71 *
72 * \param out output stream
73 * \return output stream
74 */
75 virtual std::ostream& write(std::ostream& out) const = 0;
76
77
78 /**
79 * Get value.
80 *
81 * \return value
82 */
83 virtual double get() const = 0;
84
85
86 /**
87 * Read nuisance from input stream.
88 *
89 * \param in input stream
90 * \param nuisance nuisance
91 * \return input stream
92 */
93 friend inline std::istream& operator>>(std::istream& in, JNuisance& nuisance)
94 {
95 return nuisance.read(in);
96 }
97
98
99 /**
100 * Write nuisance to output stream.
101 *
102 * \param out output stream
103 * \param nuisance nuisance
104 * \return output stream
105 */
106 friend inline std::ostream& operator<<(std::ostream& out, const JNuisance& nuisance)
107 {
108 return nuisance.write(out);
109 }
110 };
111
112
113 /**
114 * Implementation of fixed nuisance.
115 */
117 public JClonable<JNuisance, JNuisanceFixed>
118 {
119 /**
120 * Read nuisance from input stream.
121 *
122 * \param in input stream
123 * \return input stream
124 */
125 virtual std::istream& read(std::istream& in) override
126 {
127 return in;
128 }
129
130
131 /**
132 * Write nuisance to output stream.
133 *
134 * \param out output stream
135 * \return output stream
136 */
137 virtual std::ostream& write(std::ostream& out) const override
138 {
139 return out;
140 }
141
142
143 /**
144 * Get value.
145 *
146 * \return value
147 */
148 double get() const override
149 {
150 return FACTOR;
151 }
152 };
153
154
155 /**
156 * Implementation of Gaussian nuisance.
157 */
159 public JClonable<JNuisance, JNuisanceGauss>
160 {
161 /**
162 * Read nuisance from input stream.
163 *
164 * \param in input stream
165 * \return input stream
166 */
167 virtual std::istream& read(std::istream& in) override
168 {
169 return in >> this->sigma;
170 }
171
172
173 /**
174 * Write nuisance to output stream.
175 *
176 * \param out output stream
177 * \return output stream
178 */
179 virtual std::ostream& write(std::ostream& out) const override
180 {
181 return out << this->sigma;
182 }
183
184
185 /**
186 * Get value.
187 *
188 * \return value
189 */
190 double get() const override
191 {
192 return gRandom->Gaus(FACTOR, sigma);
193 }
194
195
196 double sigma;
197 };
198
199
200 /**
201 * Implementation of nuisance based on a formula.
202 */
204 public JClonable<JNuisance, JNuisanceTF1>
205 {
206 /**
207 * Read nuisance from input stream.
208 *
209 * \param in input stream
210 * \return input stream
211 */
212 virtual std::istream& read(std::istream& in) override
213 {
214 TString buffer;
215
216 if (buffer.ReadLine(in)) {
217
218 f1.reset(new TF1("f1", buffer));
219
220 if (!f1->IsValid()) {
221 THROW(JParseError, "Invalid function " << buffer);
222 }
223 }
224
225 return in;
226 }
227
228
229 /**
230 * Write nuisance to output stream.
231 *
232 * \param out output stream
233 * \return output stream
234 */
235 virtual std::ostream& write(std::ostream& out) const override
236 {
237 if (f1)
238 return out << f1->GetExpFormula();
239 else
240 return out;
241 }
242
243
244 /**
245 * Get value.
246 *
247 * \return value
248 */
249 double get() const override
250 {
251 return f1->GetRandom(gRandom);
252 }
253
254
255 std::shared_ptr<TF1> f1;
256 };
257
258
259 /**
260 * Implementation of nuisance based on histogram.
261 */
263 public JClonable<JNuisance, JNuisanceTH1>
264 {
265 /**
266 * Virtual destructor.
267 */
269 {
270 if (in != NULL) {
271 in->Close();
272 }
273 }
274
275
276 /**
277 * Read nuisance from input stream.
278 *
279 * \param in input stream
280 * \return input stream
281 */
282 virtual std::istream& read(std::istream& in) override
283 {
284 in >> this->filename
285 >> this->histname;
286
287 load();
288
289 return in;
290 }
291
292
293 /**
294 * Write nuisance to output stream.
295 *
296 * \param out output stream
297 * \return output stream
298 */
299 virtual std::ostream& write(std::ostream& out) const override
300 {
301 return out << this->filename << ' '
302 << this->histname;
303 }
304
305
306 /**
307 * Get value.
308 *
309 * \return value
310 */
311 double get() const override
312 {
313 return h1->GetRandom(gRandom);
314 }
315
316
317 std::string filename;
318 std::string histname;
319
320 protected:
321 /**
322 * Load histogram from file.
323 */
324 void load()
325 {
326 in = TFile::Open(filename.c_str(), "exist");
327
328 if (in == NULL || !in->IsOpen()) {
329 THROW(JFileOpenException, "File: " << filename << " not opened.");
330 }
331
332 h1 = dynamic_cast<TH1D*>(in->Get(histname.c_str()));
333
334 if (h1 == NULL) {
335 THROW(JValueOutOfRange, "Histogram: " << histname << " not found.");
336 }
337 }
338
339 mutable TFile* in = NULL;
340 mutable TH1D* h1 = NULL;
341 };
342
343
344 /**
345 * Type definition of generic nuisance.
346 */
347 typedef std::shared_ptr<JNuisance> nuisance_type;
348
349
350 /**
351 * Helper data structure for nuisance I/O.
352 */
354 public std::map<std::string, nuisance_type>
355 {
356 /**
357 * Default constructor.
358 */
360 {
361 (*this)["fixed"] .reset(new JNuisanceFixed());
362 (*this)["Gauss"] .reset(new JNuisanceGauss());
363 (*this)["Formula"] .reset(new JNuisanceTF1());
364 (*this)["Histogram"].reset(new JNuisanceTH1());
365 }
366
367
368 /**
369 * Parse nuisance from input stream.
370 *
371 * \param in input stream
372 * \param object nuisance
373 * \return input stream
374 */
375 std::istream& operator()(std::istream& in, nuisance_type& object) const
376 {
377 std::string key;
378
379 if (in >> key) {
380
381 const_iterator p = this->find(key);
382
383 if (p != this->end()) {
384
385 object.reset(p->second->clone());
386
387 object->setTitle(key);
388 object->read(in);
389
390 } else {
391
392 THROW(JParseError, "Invalid key " << key);
393 }
394 }
395
396 return in;
397 }
398 };
399
400
401 /**
402 * Helper object for nuisance I/O.
403 */
405
406
407 /**
408 * Read nuisance from input stream.
409 *
410 * \param in input stream
411 * \param object nuisance
412 * \return input stream
413 */
414 inline std::istream& operator>>(std::istream& in, nuisance_type& object)
415 {
416 return nuisance_helper(in, object);
417 }
418
419
420 /**
421 * Write nuisance to output stream.
422 *
423 * \param out output stream
424 * \param object nuisance
425 * \return output stream
426 */
427 inline std::ostream& operator<<(std::ostream& out, const nuisance_type& object)
428 {
429 if (object) {
430 out << object->getTitle() << ' ' << *object.get();
431 }
432
433 return out;
434 }
435}
436
437#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Exception for opening of file.
Exception for parsing value.
Auxiliary class for title.
Definition JTitle.hh:19
Exception for accessing a value in a collection that is outside of its range.
std::ostream & operator<<(std::ostream &out, const morphology_type &object)
Write morphology to output stream.
std::shared_ptr< JNuisance > nuisance_type
Type definition of generic nuisance.
Definition JNuisance.hh:347
static const JNuisanceHelper nuisance_helper
Helper object for nuisance I/O.
Definition JNuisance.hh:404
std::istream & operator>>(std::istream &in, morphology_type &object)
Read morphology from input stream.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Implementation of fixed nuisance.
Definition JNuisance.hh:118
double get() const override
Get value.
Definition JNuisance.hh:148
virtual std::istream & read(std::istream &in) override
Read nuisance from input stream.
Definition JNuisance.hh:125
virtual std::ostream & write(std::ostream &out) const override
Write nuisance to output stream.
Definition JNuisance.hh:137
Implementation of Gaussian nuisance.
Definition JNuisance.hh:160
virtual std::istream & read(std::istream &in) override
Read nuisance from input stream.
Definition JNuisance.hh:167
double get() const override
Get value.
Definition JNuisance.hh:190
virtual std::ostream & write(std::ostream &out) const override
Write nuisance to output stream.
Definition JNuisance.hh:179
Helper data structure for nuisance I/O.
Definition JNuisance.hh:355
JNuisanceHelper()
Default constructor.
Definition JNuisance.hh:359
std::istream & operator()(std::istream &in, nuisance_type &object) const
Parse nuisance from input stream.
Definition JNuisance.hh:375
Implementation of nuisance based on a formula.
Definition JNuisance.hh:205
double get() const override
Get value.
Definition JNuisance.hh:249
std::shared_ptr< TF1 > f1
Definition JNuisance.hh:255
virtual std::ostream & write(std::ostream &out) const override
Write nuisance to output stream.
Definition JNuisance.hh:235
virtual std::istream & read(std::istream &in) override
Read nuisance from input stream.
Definition JNuisance.hh:212
Implementation of nuisance based on histogram.
Definition JNuisance.hh:264
virtual std::istream & read(std::istream &in) override
Read nuisance from input stream.
Definition JNuisance.hh:282
virtual ~JNuisanceTH1()
Virtual destructor.
Definition JNuisance.hh:268
virtual std::ostream & write(std::ostream &out) const override
Write nuisance to output stream.
Definition JNuisance.hh:299
void load()
Load histogram from file.
Definition JNuisance.hh:324
double get() const override
Get value.
Definition JNuisance.hh:311
Interface for nuisance parameter.
Definition JNuisance.hh:47
friend std::istream & operator>>(std::istream &in, JNuisance &nuisance)
Read nuisance from input stream.
Definition JNuisance.hh:93
virtual double get() const =0
Get value.
virtual std::istream & read(std::istream &in)=0
Read nuisance from input stream.
virtual ~JNuisance()
Virtual destructor.
Definition JNuisance.hh:56
virtual std::ostream & write(std::ostream &out) const =0
Write nuisance to output stream.
friend std::ostream & operator<<(std::ostream &out, const JNuisance &nuisance)
Write nuisance to output stream.
Definition JNuisance.hh:106
static constexpr double FACTOR
Central value.
Definition JNuisance.hh:51
Template class for object cloning.
Definition JClonable.hh:59