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