Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JMorphology.hh
Go to the documentation of this file.
1#ifndef __JASTRONOMY__JMORPHOLOGY__
2#define __JASTRONOMY__JMORPHOLOGY__
3
4#include <string>
5#include <istream>
6#include <ostream>
7#include <memory>
8#include <map>
9
10#include "TRandom3.h"
11#include "TFile.h"
12#include "TH2D.h"
13
17
18#include "JLang/JTitle.hh"
19#include "JLang/JException.hh"
20
21
22/**
23 * \file
24 *
25 * Tools for simulation of source morphology.
26 * \author mdejong
27 */
28namespace JASTRONOMY {}
29namespace JPP { using namespace JASTRONOMY; }
30
31namespace JASTRONOMY {
32
33 using JLANG::JTitle;
37
38
39 /**
40 * Inteface for source morphology simulation.
41 * Note that all input angles are in degrees.
42 */
43 struct JMorphology :
44 public JSourceLocation
45 {
46 /**
47 * Virtual destructor.
48 */
49 virtual ~JMorphology()
50 {}
51
52
53 /**
54 * Read source location in degrees from input stream.
55 *
56 * \param in input stream
57 * \param source source
58 * \return input stream
59 */
60 static std::istream& read(std::istream& in, JSourceLocation& source)
61 {
62 angle_type_deg angle;
63
64 in >> angle;
65
66 source.set(angle);
67
68 return in;
69 }
70
71
72 /**
73 * Write source location in degrees to output stream.
74 *
75 * \param out output stream
76 * \param source source
77 * \return output stream
78 */
79 static std::ostream& write(std::ostream& out, const JSourceLocation& source)
80 {
81 return out << angle_type_deg(source);
82 }
83
84
85 /**
86 * Read source morphology from input stream.
87 *
88 * \param in input stream
89 * \return input stream
90 */
91 virtual std::istream& read(std::istream& in) = 0;
92
93
94 /**
95 * Write source morphology to output stream.
96 *
97 * \param out output stream
98 * \return output stream
99 */
100 virtual std::ostream& write(std::ostream& out) const = 0;
101
102
103 /**
104 * Get location on sky.
105 *
106 * \return location on sky
107 */
108 virtual JSourceLocation get() const = 0;
109 };
110
111
112 /**
113 * Implementation of point source morphology.
114 * Note that all input angles are in degrees.
115 */
117 public JMorphology
118 {
119 /**
120 * Read source morphology 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 JMorphology::read(in, *this);
128 }
129
130
131 /**
132 * Write source morphology 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 JMorphology::write(out, *this);
140 }
141
142
143 /**
144 * Get location on sky.
145 *
146 * \return location on sky
147 */
148 virtual JSourceLocation get() const override
149 {
150 using namespace JPP;
151
152 JDirection3D u(0.0, 0.0, 1.0);
153
154 const JRotation3D Rs(this->getSourceLocation());
155
156 u.rotate_back(Rs);
157
158 return JSourceLocation(u);
159 }
160 };
161
162
163 /**
164 * Implementation of Gaussian source morphology.
165 * Note that all input angles are in degrees.
166 */
168 public JMorphology
169 {
170 /**
171 * Read source morphology from input stream.
172 *
173 * \param in input stream
174 * \return input stream
175 */
176 virtual std::istream& read(std::istream& in) override
177 {
178 return JMorphology::read(in, *this)
179 >> this->sigma_deg;
180 }
181
182
183 /**
184 * Write source morphology to output stream.
185 *
186 * \param out output stream
187 * \return output stream
188 */
189 virtual std::ostream& write(std::ostream& out) const override
190 {
191 return JMorphology::write(out, *this)
192 << ' ' << this->sigma_deg;
193 }
194
195
196 /**
197 * Get location on sky.
198 *
199 * \return location on sky
200 */
201 virtual JSourceLocation get() const override
202 {
203 using namespace JPP;
204
205 JDirection3D u(0.0, 0.0, 1.0);
206
207 if (sigma_deg > 0.0) {
208
209 const double theta = gRandom->Gaus(0.0, getRadians(sigma_deg));
210 const double phi = gRandom->Uniform(-PI, +PI);
211
212 const JRotation3D R(JAngle3D(theta, phi));
213
214 u.rotate_back(R);
215 }
216
217 const JRotation3D Rs(this->getSourceLocation());
218
219 u.rotate_back(Rs);
220
221 return JSourceLocation(u);
222 }
223
224
225 double sigma_deg;
226 };
227
228
229 /**
230 * Implementation of 2D-Gaussian source morphology.
231 * Note that all input angles are in degrees.
232 */
234 public JMorphology
235 {
236 /**
237 * Read source morphology from input stream.
238 *
239 * \param in input stream
240 * \return input stream
241 */
242 virtual std::istream& read(std::istream& in) override
243 {
244 return JMorphology::read(in, *this)
245 >> this->sigmaX_deg >> this->sigmaY_deg;
246 }
247
248
249 /**
250 * Write source morphology to output stream.
251 *
252 * \param out output stream
253 * \return output stream
254 */
255 virtual std::ostream& write(std::ostream& out) const override
256 {
257 return JMorphology::write(out, *this)
258 << ' ' << this->sigmaX_deg << ' ' << this->sigmaY_deg;
259 }
260
261
262 /**
263 * Get location on sky.
264 *
265 * \return location on sky
266 */
267 virtual JSourceLocation get() const override
268 {
269 using namespace JPP;
270
271 JDirection3D u(0.0, 0.0, 1.0);
272
273 if (sigmaX_deg > 0.0 ||
274 sigmaY_deg > 0.0) {
275
276 const double x = sin(gRandom->Gaus(0.0, getRadians(sigmaX_deg)));
277 const double y = sin(gRandom->Gaus(0.0, getRadians(sigmaY_deg)));
278
279 u = JDirection3D(x, y, sqrt(1.0 - x*x - y*y));
280 }
281
282 const JRotation3D Rs(this->getSourceLocation());
283
284 u.rotate_back(Rs);
285
286 return JSourceLocation(u);
287 }
288
289
292 };
293
294
295 /**
296 * Implementation of binary source morphology.
297 * Note that all input angles are in degrees.
298 */
300 public JMorphology
301 {
302 /**
303 * Read source morphology from input stream.
304 *
305 * \param in input stream
306 * \return input stream
307 */
308 virtual std::istream& read(std::istream& in) override
309 {
310 return JMorphology::read(in, *this)
311 >> this->x1_deg >> this->y1_deg >> this->w1 >> this->s1_deg
312 >> this->x2_deg >> this->y2_deg >> this->w2 >> this->s2_deg;
313 }
314
315
316 /**
317 * Write source morphology to output stream.
318 *
319 * \param out output stream
320 * \return output stream
321 */
322 virtual std::ostream& write(std::ostream& out) const override
323 {
324 return JMorphology::write(out, *this)
325 << ' ' << this->x1_deg << ' ' << this->y1_deg << ' ' << this->w1 << ' ' << this->s1_deg
326 << ' ' << this->x2_deg << ' ' << this->y2_deg << ' ' << this->w2 << ' ' << this->s2_deg;
327 }
328
329
330 /**
331 * Get location on sky.
332 *
333 * \return location on sky
334 */
335 virtual JSourceLocation get() const override
336 {
337 using namespace JPP;
338
339 const double w = gRandom->Uniform(0.0, w1 + w2);
340
341 const double x = sin(getRadians(w < w1 ? x1_deg : x2_deg));
342 const double y = sin(getRadians(w < w1 ? y1_deg : y2_deg));
343 const double s = getRadians(w < w1 ? s1_deg : s2_deg);
344
345 JDirection3D v(x, y, sqrt(1.0 - x*x - y*y));
346
347 JDirection3D u(0.0, 0.0, 1.0);
348
349 if (s > 0.0) {
350
351 const double theta = gRandom->Gaus(0.0, s);
352 const double phi = gRandom->Uniform(-PI, +PI);
353
354 const JRotation3D R(JAngle3D(theta, phi));
355
356 u.rotate_back(R);
357 }
358 {
359 const JRotation3D R(v);
360
361 u.rotate_back(R);
362 }
363
364 const JRotation3D Rs(this->getSourceLocation());
365
366 u.rotate_back(Rs);
367
368 return JSourceLocation(u);
369 }
370
371
374 };
375
376
377 /**
378 * Implementation of histogram source morphology.
379 * Note that all input angles are in degrees.
380 */
382 public JMorphology
383 {
384 /**
385 * Virtual destructor.
386 */
388 {
389 if (in != NULL) {
390 in->Close();
391 }
392 }
393
394
395 /**
396 * Read source morphology from input stream.
397 *
398 * \param in input stream
399 * \return input stream
400 */
401 virtual std::istream& read(std::istream& in) override
402 {
403 JMorphology::read(in, *this)
404 >> this->filename
405 >> this->histname;
406
407 load();
408
409 return in;
410 }
411
412
413 /**
414 * Write source morphology to output stream.
415 *
416 * \param out output stream
417 * \return output stream
418 */
419 virtual std::ostream& write(std::ostream& out) const override
420 {
421 return JMorphology::write(out, *this)
422 << ' ' << this->filename
423 << ' ' << this->histname;
424 }
425
426
427 /**
428 * Get location on sky.
429 *
430 * \return location on sky
431 */
432 virtual JSourceLocation get() const override
433 {
434 using namespace JPP;
435
436 Double_t x;
437 Double_t y;
438
439 h2->GetRandom2(x, y, gRandom);
440
441 x = sin(getRadians(x));
442 y = sin(getRadians(y));
443
444 JDirection3D u(x, y, sqrt(1.0 - x*x - y*y));
445
446 const JRotation3D Rs(this->getSourceLocation());
447
448 u.rotate_back(Rs);
449
450 return JSourceLocation(u);
451 }
452
453 std::string filename;
454 std::string histname;
455
456 protected:
457 /**
458 * Load histogram from file.
459 */
460 void load()
461 {
462 in = TFile::Open(filename.c_str(), "exist");
463
464 if (in == NULL || !in->IsOpen()) {
465 THROW(JFileOpenException, "File: " << filename << " not opened.");
466 }
467
468 h2 = dynamic_cast<TH2D*>(in->Get(histname.c_str()));
469
470 if (h2 == NULL) {
471 THROW(JValueOutOfRange, "Histogram: " << histname << " not found.");
472 }
473 }
474
475 mutable TFile* in = NULL;
476 mutable TH2D* h2 = NULL;
477 };
478
479
480 /**
481 * Helper for source morphology.
482 */
484 public JTitle,
485 public std::shared_ptr<JMorphology>
486 {
487 /**
488 * Default constructor.
489 */
491 {
492 dictionary["Point"] .reset(new JMorphologyPoint());
493 dictionary["Gauss"] .reset(new JMorphologyGauss());
494 dictionary["Gauss2D"] .reset(new JMorphologyGauss2D());
495 dictionary["Binary"] .reset(new JMorphologyBinary());
496 dictionary["Histogram"].reset(new JMorphologyHistogram());
497 }
498
499
500 /**
501 * Put source morphology at given key.
502 *
503 * \param key key
504 * \param morphology morphology
505 */
506 void put(const std::string& key, JMorphology* morphology)
507 {
508 return dictionary[key].reset(morphology);
509 }
510
511
512 /**
513 * Read source morphology from input stream.
514 *
515 * \param in input stream
516 * \param object source morphology
517 * \return input stream
518 */
519 friend inline std::istream& operator>>(std::istream& in, JMorphology_t& object)
520 {
521 using namespace std;
522
523 string key;
524
525 if (in >> key) {
526
527 if (object.dictionary.count(key)) {
528
529 static_cast<std::shared_ptr<JMorphology>&>(object) = object.dictionary[key];
530
531 object.setTitle(key);
532
533 object->read(in);
534
535 } else {
536
537 THROW(JParseError, "Invalid key " << key);
538 }
539 }
540
541 return in;
542 }
543
544
545 /**
546 * Write source morphology to output stream.
547 *
548 * \param out output stream
549 * \param object source morphology
550 * \return output stream
551 */
552 friend inline std::ostream& operator<<(std::ostream& out, const JMorphology_t& object)
553 {
554 out << object.getTitle() << ' ';
555
556 if (static_cast<const std::shared_ptr<JMorphology>&>(object)) {
557 object->write(out);
558 }
559
560 return out;
561 }
562
563 protected:
565 };
566}
567
568#endif
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.
double getRadians(const double angle)
Convert angle to radians.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Implementation of binary source morphology.
virtual std::ostream & write(std::ostream &out) const override
Write source morphology to output stream.
virtual JSourceLocation get() const override
Get location on sky.
virtual std::istream & read(std::istream &in) override
Read source morphology from input stream.
Implementation of 2D-Gaussian source morphology.
virtual JSourceLocation get() const override
Get location on sky.
virtual std::ostream & write(std::ostream &out) const override
Write source morphology to output stream.
virtual std::istream & read(std::istream &in) override
Read source morphology from input stream.
Implementation of Gaussian source morphology.
virtual std::ostream & write(std::ostream &out) const override
Write source morphology to output stream.
virtual std::istream & read(std::istream &in) override
Read source morphology from input stream.
virtual JSourceLocation get() const override
Get location on sky.
Implementation of histogram source morphology.
virtual std::istream & read(std::istream &in) override
Read source morphology from input stream.
virtual JSourceLocation get() const override
Get location on sky.
virtual ~JMorphologyHistogram()
Virtual destructor.
virtual std::ostream & write(std::ostream &out) const override
Write source morphology to output stream.
void load()
Load histogram from file.
Implementation of point source morphology.
virtual std::ostream & write(std::ostream &out) const override
Write source morphology to output stream.
virtual std::istream & read(std::istream &in) override
Read source morphology from input stream.
virtual JSourceLocation get() const override
Get location on sky.
Helper for source morphology.
std::map< std::string, std::shared_ptr< JMorphology > > dictionary
friend std::istream & operator>>(std::istream &in, JMorphology_t &object)
Read source morphology from input stream.
void put(const std::string &key, JMorphology *morphology)
Put source morphology at given key.
JMorphology_t()
Default constructor.
friend std::ostream & operator<<(std::ostream &out, const JMorphology_t &object)
Write source morphology to output stream.
Inteface for source morphology simulation.
static std::istream & read(std::istream &in, JSourceLocation &source)
Read source location in degrees from input stream.
virtual std::ostream & write(std::ostream &out) const =0
Write source morphology to output stream.
virtual std::istream & read(std::istream &in)=0
Read source morphology from input stream.
static std::ostream & write(std::ostream &out, const JSourceLocation &source)
Write source location in degrees to output stream.
virtual ~JMorphology()
Virtual destructor.
virtual JSourceLocation get() const =0
Get location on sky.
Location of astrophysical source.
const JSourceLocation & getSourceLocation() const
Get source location.
JSourceLocation()
Default constructor.
Auxiliary data structure for pair of angles.
void set(const angle_type_deg &angle)
Convert angle.