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