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