Jpp 20.0.0-rc.9-22-g74b57fa79
the software that should make you happy
Loading...
Searching...
No Matches
JResolution.hh
Go to the documentation of this file.
1#ifndef __JASTRONOMY__JRESOLUTION__
2#define __JASTRONOMY__JRESOLUTION__
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
20
21#include "JLang/JClonable.hh"
22#include "JLang/JTitle.hh"
23#include "JLang/JException.hh"
24
26
27
28/**
29 * \file
30 *
31 * Tools for simulation of detector resolution.
32 * \author mdejong
33 */
34namespace JASTRONOMY {}
35namespace JPP { using namespace JASTRONOMY; }
36
37namespace JASTRONOMY {
38
39 using JLANG::JClonable;
40 using JLANG::JTitle;
45
46
47 /**
48 * Interface for detector resolution simulation.
49 * Note that all input angles are in degrees.
50 */
51 struct JResolution :
52 public JClonable<JResolution>,
53 public JTitle
54 {
55 /**
56 * Virtual destructor.
57 */
58 virtual ~JResolution()
59 {}
60
61
62 /**
63 * Read detector resolution from input stream.
64 *
65 * \param in input stream
66 * \return input stream
67 */
68 virtual std::istream& read(std::istream& in) = 0;
69
70
71 /**
72 * Write detector resolution to output stream.
73 *
74 * \param out output stream
75 * \return output stream
76 */
77 virtual std::ostream& write(std::ostream& out) const = 0;
78
79
80 /**
81 * Get direction.
82 *
83 * \return direction
84 */
85 virtual JDirection3D get() const = 0;
86 };
87
88
89 /**
90 * Implementation of Gaussian detector resolution.
91 * Note that all input angles are in degrees.
92 */
94 public JClonable<JResolution, JResolutionGauss>
95 {
96 /**
97 * Read detector resolution from input stream.
98 *
99 * \param in input stream
100 * \return input stream
101 */
102 virtual std::istream& read(std::istream& in) override
103 {
104 return in >> this->sigma_deg;
105 }
106
107
108 /**
109 * Write detector resolution to output stream.
110 *
111 * \param out output stream
112 * \return output stream
113 */
114 virtual std::ostream& write(std::ostream& out) const override
115 {
116 return out << this->sigma_deg;
117 }
118
119
120 /**
121 * Get direction.
122 *
123 * \return direction
124 */
125 JDirection3D get() const override
126 {
127 using namespace JPP;
128
129 JDirection3D u(0.0, 0.0, 1.0);
130
131 if (sigma_deg > 0.0) {
132
133 const double theta = gRandom->Gaus(0.0, getRadians(sigma_deg));
134 const double phi = gRandom->Uniform(-PI, +PI);
135
136 const JRotation3D R(JAngle3D(theta, phi));
137
138 u.rotate_back(R);
139 }
140
141 return u;
142 }
143
144
145 double sigma_deg;
146 };
147
148
149 /**
150 * Implementation of detector resolution based on a formula.
151 * Note that all input angles are in degrees.
152 */
154 public JClonable<JResolution, JResolutionTF1>
155 {
156 /**
157 * Read detector resolution from input stream.
158 *
159 * \param in input stream
160 * \return input stream
161 */
162 virtual std::istream& read(std::istream& in) override
163 {
164 TString buffer;
165
166 if (buffer.ReadLine(in)) {
167
168 f1.reset(new TF1("f1", buffer));
169
170 if (!f1->IsValid()) {
171 THROW(JParseError, "Invalid function " << buffer);
172 }
173 }
174
175 return in;
176 }
177
178
179 /**
180 * Write detector resolution to output stream.
181 *
182 * \param out output stream
183 * \return output stream
184 */
185 virtual std::ostream& write(std::ostream& out) const override
186 {
187 if (f1)
188 return out << f1->GetExpFormula();
189 else
190 return out;
191 }
192
193
194 /**
195 * Get direction.
196 *
197 * \return direction
198 */
199 JDirection3D get() const override
200 {
201 using namespace JPP;
202
203 JDirection3D u(0.0, 0.0, 1.0);
204
205 if (f1) {
206
207 const double theta = getRadians(f1->GetRandom(gRandom));
208 const double phi = gRandom->Uniform(-PI, +PI);
209
210 const JRotation3D R(JAngle3D(theta, phi));
211
212 u.rotate_back(R);
213 }
214
215 return u;
216 }
217
218
219 std::shared_ptr<TF1> f1;
220 };
221
222
223 /**
224 * Implementation of detector resolution based on histogram.
225 * Note that all input angles are in degrees.
226 */
228 public JClonable<JResolution, JResolutionTH1>
229 {
230 /**
231 * Virtual destructor.
232 */
234 {
235 if (in != NULL) {
236 in->Close();
237 }
238 }
239
240
241 /**
242 * Read detector resolution from input stream.
243 *
244 * \param in input stream
245 * \return input stream
246 */
247 virtual std::istream& read(std::istream& in) override
248 {
249 in >> this->filename
250 >> this->histname;
251
252 load();
253
254 return in;
255 }
256
257
258 /**
259 * Write detector resolution to output stream.
260 *
261 * \param out output stream
262 * \return output stream
263 */
264 virtual std::ostream& write(std::ostream& out) const override
265 {
266 return out << this->filename << ' '
267 << this->histname;
268 }
269
270
271 /**
272 * Get direction.
273 *
274 * \return direction
275 */
276 JDirection3D get() const override
277 {
278 using namespace JPP;
279
280 JDirection3D u(0.0, 0.0, 1.0);
281
282 if (h1 != NULL) {
283
284 const double theta = getRadians(h1->GetRandom(gRandom));
285 const double phi = gRandom->Uniform(-PI, +PI);
286
287 const JRotation3D R(JAngle3D(theta, phi));
288
289 u.rotate_back(R);
290 }
291
292 return u;
293 }
294
295
296 std::string filename;
297 std::string histname;
298
299 protected:
300 /**
301 * Load histogram from file.
302 */
303 void load()
304 {
305 in = TFile::Open(filename.c_str(), "exist");
306
307 if (in == NULL || !in->IsOpen()) {
308 THROW(JFileOpenException, "File: " << filename << " not opened.");
309 }
310
311 h1 = dynamic_cast<TH1D*>(in->Get(histname.c_str()));
312
313 if (h1 == NULL) {
314 THROW(JValueOutOfRange, "Histogram: " << histname << " not found.");
315 }
316 }
317
318 mutable TFile* in = NULL;
319 mutable TH1D* h1 = NULL;
320 };
321
322
323 /**
324 * Implementation of detector resolution based on histogram filled according <tt>log10(alpha)</tt>.
325 * Note that all input angles are in degrees.
326 */
328 public JResolutionTH1
329 {
330 /**
331 * Get direction.
332 *
333 * \return direction
334 */
335 JDirection3D get() const override
336 {
337 using namespace JPP;
338
339 JDirection3D u(0.0, 0.0, 1.0);
340
341 if (h1 != NULL) {
342
343 const double theta = getRadians(pow(10.0,h1->GetRandom(gRandom)));
344 const double phi = gRandom->Uniform(-PI, +PI);
345
346 const JRotation3D R(JAngle3D(theta, phi));
347
348 u.rotate_back(R);
349 }
350
351 return u;
352 }
353 };
354
355
356 /**
357 * Type definition of generic resolution.
358 */
359 typedef std::shared_ptr<JResolution> resolution_type;
360
361
362 /**
363 * Helper data structure for detector resolution I/O.
364 */
366 public std::map<std::string, resolution_type>
367 {
368 /**
369 * Default constructor.
370 */
372 {
373 (*this)["Gauss"] .reset(new JResolutionGauss());
374 (*this)["Formula"] .reset(new JResolutionTF1());
375 (*this)["Histogram"] .reset(new JResolutionTH1());
376 (*this)["Logarithmic"].reset(new JResolutionLog());
377 }
378
379
380 /**
381 * Parse detector resolution from input stream.
382 *
383 * \param in input stream
384 * \param object detector resolution
385 * \return input stream
386 */
387 inline std::istream& operator()(std::istream& in, resolution_type& object) const
388 {
389 std::string key;
390
391 if (in >> key) {
392
393 const_iterator p = this->find(key);
394
395 if (p != this->end()) {
396
397 object.reset(p->second->clone());
398
399 object->setTitle(key);
400 object->read(in);
401
402 } else {
403
404 THROW(JParseError, "Invalid key " << key);
405 }
406 }
407
408 return in;
409 }
410 };
411
412
413 /**
414 * Helper object for resolution I/O.
415 */
417
418
419 /**
420 * Read resolution from input stream.
421 *
422 * \param in input stream
423 * \param object resolution
424 * \return input stream
425 */
426 inline std::istream& operator>>(std::istream& in, resolution_type& object)
427 {
428 return resolution_helper(in, object);
429 }
430
431
432 /**
433 * Write resolution to output stream.
434 *
435 * \param out output stream
436 * \param object resolution
437 * \return output stream
438 */
439 inline std::ostream& operator<<(std::ostream& out, const resolution_type& object)
440 {
441 if (object) {
442 out << object->getTitle() << ' ';
443 object->write(out);
444 }
445
446 return out;
447 }
448}
449
450#endif
Interface methods for SLALIB and auxiliary classes and methods for astronomy.
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Data structure for angles in three dimensions.
Definition JAngle3D.hh:35
Data structure for direction in three dimensions.
JDirection3D & rotate_back(const JRotation3D &R)
Rotate back.
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.
static const JResolutionHelper resolution_helper
Helper object for resolution I/O.
std::shared_ptr< JResolution > resolution_type
Type definition of generic resolution.
double getRadians(const double angle)
Convert angle to radians.
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 Gaussian detector resolution.
virtual std::istream & read(std::istream &in) override
Read detector resolution from input stream.
JDirection3D get() const override
Get direction.
virtual std::ostream & write(std::ostream &out) const override
Write detector resolution to output stream.
Helper data structure for detector resolution I/O.
JResolutionHelper()
Default constructor.
std::istream & operator()(std::istream &in, resolution_type &object) const
Parse detector resolution from input stream.
Implementation of detector resolution based on histogram filled according log10(alpha).
JDirection3D get() const override
Get direction.
Implementation of detector resolution based on a formula.
virtual std::istream & read(std::istream &in) override
Read detector resolution from input stream.
JDirection3D get() const override
Get direction.
std::shared_ptr< TF1 > f1
virtual std::ostream & write(std::ostream &out) const override
Write detector resolution to output stream.
Implementation of detector resolution based on histogram.
virtual std::istream & read(std::istream &in) override
Read detector resolution from input stream.
virtual ~JResolutionTH1()
Virtual destructor.
void load()
Load histogram from file.
JDirection3D get() const override
Get direction.
virtual std::ostream & write(std::ostream &out) const override
Write detector resolution to output stream.
Interface for detector resolution simulation.
virtual JDirection3D get() const =0
Get direction.
virtual ~JResolution()
Virtual destructor.
virtual std::istream & read(std::istream &in)=0
Read detector resolution from input stream.
virtual std::ostream & write(std::ostream &out) const =0
Write detector resolution to output stream.
Template class for object cloning.
Definition JClonable.hh:59