GResponse.cpp
27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/***************************************************************************
* GResponse.cpp - Abstract response base class *
* ----------------------------------------------------------------------- *
* copyright (C) 2008-2013 by Juergen Knoedlseder *
* ----------------------------------------------------------------------- *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
/**
* @file GResponse.hpp
* @brief Abstract response base class interface definition
* @author Juergen Knoedlseder
*/
/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include <unistd.h> // access() function
#include "GResponse.hpp"
#include "GObservation.hpp"
#include "GIntegral.hpp"
#include "GVector.hpp"
#include "GSkyDir.hpp"
#include "GException.hpp"
#include "GTools.hpp"
#include "GMath.hpp"
#include "GModelSpatialPointSource.hpp"
#include "GModelSpatialRadial.hpp"
#include "GModelSpatialElliptical.hpp"
#include "GModelSpatialDiffuse.hpp"
/* __ Method name definitions ____________________________________________ */
#define G_IRF_RADIAL "GResponse::irf_radial(GEvent&, GSource&,"\
" GObservation&)"
#define G_IRF_ELLIPTICAL "GResponse::irf_elliptical(GEvent&, GSource&,"\
" GObservation&)"
#define G_IRF_DIFFUSE "GResponse::irf_diffuse(GEvent&, GSource&,"\
" GObservation&)"
#define G_NPRED_RADIAL "GResponse::npred_radial(GSource&, GObservation&)"
#define G_NPRED_ELLIPTICAL "GResponse::npred_elliptical(GSource&,"\
" GObservation&)"
#define G_NPRED_DIFFUSE "GResponse::npred_diffuse(GSource&, GObservation&)"
/* __ Macros _____________________________________________________________ */
/* __ Coding definitions _________________________________________________ */
/* __ Debug definitions __________________________________________________ */
//#define G_DEBUG_NPRED_RADIAL //!< Debug npred_radial
/*==========================================================================
= =
= Constructors/destructors =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Void constructor
***************************************************************************/
GResponse::GResponse(void)
{
// Initialise class members for clean destruction
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Copy constructor
*
* @param[in] rsp Response.
***************************************************************************/
GResponse::GResponse(const GResponse& rsp)
{
// Initialise class members for clean destruction
init_members();
// Copy members
copy_members(rsp);
// Return
return;
}
/***********************************************************************//**
* @brief Destructor
***************************************************************************/
GResponse::~GResponse(void)
{
// Free members
free_members();
// Return
return;
}
/*==========================================================================
= =
= Operators =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Assignment operator
*
* @param[in] rsp Response.
* @return Response.
***************************************************************************/
GResponse& GResponse::operator= (const GResponse& rsp)
{
// Execute only if object is not identical
if (this != &rsp) {
// Free members
free_members();
// Initialise private members for clean destruction
init_members();
// Copy members
copy_members(rsp);
} // endif: object was not identical
// Return this object
return *this;
}
/*==========================================================================
= =
= Public methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Return value of instrument response function
*
* @param[in] event Event.
* @param[in] source Source.
* @param[in] obs Observation.
*
* Returns the instrument response function for a given event, source and
* observation.
*
* The method applies the deadtime correction, so that the response function
* can be directly multiplied by the exposure time (also known as ontime).
***************************************************************************/
double GResponse::irf(const GEvent& event,
const GSource& source,
const GObservation& obs) const
{
// Initialise IRF value
double irf = 0.0;
// Is spatial model a point source?
if (dynamic_cast<const GModelSpatialPointSource*>(source.model()) != NULL) {
irf = irf_ptsrc(event, source, obs);
}
// Is spatial model a radial source?
else if (dynamic_cast<const GModelSpatialRadial*>(source.model()) != NULL) {
irf = irf_radial(event, source, obs);
}
// Is spatial model an elliptical source?
else if (dynamic_cast<const GModelSpatialElliptical*>(source.model()) != NULL) {
irf = irf_elliptical(event, source, obs);
}
// Is spatial model a diffuse source?
else if (dynamic_cast<const GModelSpatialDiffuse*>(source.model()) != NULL) {
irf = irf_diffuse(event, source, obs);
}
// Return IRF value
return irf;
}
/***********************************************************************//**
* @brief Return value of point source instrument response function
*
* @param[in] event Observed event.
* @param[in] source Source.
* @param[in] obs Observation.
* @return Value of instrument response function for a point source.
*
* This method returns the value of the instrument response function for a
* point source. If the source is not a point source, the method returns 0.
***************************************************************************/
double GResponse::irf_ptsrc(const GEvent& event,
const GSource& source,
const GObservation& obs) const
{
// Initialise IRF
double irf = 0.0;
// Get point source spatial model
const GModelSpatialPointSource* src =
dynamic_cast<const GModelSpatialPointSource*>(source.model());
// Continue only if model is valid
if (src != NULL) {
// Set Photon
GPhoton photon(src->dir(), source.energy(), source.time());
// Compute IRF
irf = this->irf(event, photon, obs);
}
// Return IRF
return irf;
}
/***********************************************************************//**
* @brief Return IRF value for radial source model
*
* @param[in] event Observed event.
* @param[in] source Source.
* @param[in] obs Observation.
*
* @exception GException::feature_not_implemented
* Method is not implemented.
***************************************************************************/
double GResponse::irf_radial(const GEvent& event,
const GSource& source,
const GObservation& obs) const
{
// Feature not yet implemented
throw GException::feature_not_implemented(G_IRF_RADIAL,
"IRF computation not implemented for radial models.");
// Return IRF
return 0.0;
}
/***********************************************************************//**
* @brief Return IRF value for elliptical source model
*
* @param[in] event Observed event.
* @param[in] source Source.
* @param[in] obs Observation.
*
* @exception GException::feature_not_implemented
* Method is not implemented.
***************************************************************************/
double GResponse::irf_elliptical(const GEvent& event,
const GSource& source,
const GObservation& obs) const
{
// Feature not yet implemented
throw GException::feature_not_implemented(G_IRF_ELLIPTICAL,
"IRF computation not implemented for elliptical models.");
// Return IRF
return 0.0;
}
/***********************************************************************//**
* @brief Return value of diffuse source instrument response function
*
* @param[in] event Observed event.
* @param[in] source Source.
* @param[in] obs Observation.
*
* @exception GException::feature_not_implemented
* Method is not implemented.
***************************************************************************/
double GResponse::irf_diffuse(const GEvent& event,
const GSource& source,
const GObservation& obs) const
{
// Feature not yet implemented
throw GException::feature_not_implemented(G_IRF_DIFFUSE,
"IRF computation not implemented for diffuse models.");
// Return IRF
return 0.0;
}
/***********************************************************************//**
* @brief Return data space integral of instrument response function
*
* @param[in] source Source.
* @param[in] obs Observation.
*
* Returns the data space integral of the instrument response function for
* a given source and a particular observation. This method is needed for
* an unbinned maximum likelihood analysis.
*
* The method applies the deadtime correction, so that the result can be
* directly multiplied by the exposure time (also known as ontime).
***************************************************************************/
double GResponse::npred(const GSource& source, const GObservation& obs) const
{
// Initialise Npred value
double npred = 0.0;
// Is spatial model a point source?
if (dynamic_cast<const GModelSpatialPointSource*>(source.model()) != NULL) {
npred = npred_ptsrc(source, obs);
}
// Is spatial model a radial source?
else if (dynamic_cast<const GModelSpatialRadial*>(source.model()) != NULL) {
npred = npred_radial(source, obs);
}
// Is spatial model an elliptical source?
else if (dynamic_cast<const GModelSpatialElliptical*>(source.model()) != NULL) {
npred = npred_elliptical(source, obs);
}
// Is spatial model a diffuse source?
else if (dynamic_cast<const GModelSpatialDiffuse*>(source.model()) != NULL) {
npred = npred_diffuse(source, obs);
}
// Return response value
return npred;
}
/***********************************************************************//**
* @brief Return ROI integral of point source model
*
* @param[in] source Source.
* @param[in] obs Observation.
* @return Integral of point source model over ROI.
*
* This method returns the spatial integral of a point source model over the
* region of interest. If the source is not a point source, the method
* returns 0.
***************************************************************************/
double GResponse::npred_ptsrc(const GSource& source,
const GObservation& obs) const
{
// Initialise Npred
double npred = 0.0;
// Get point source spatial model
const GModelSpatialPointSource* src =
dynamic_cast<const GModelSpatialPointSource*>(source.model());
// Continue only if model is valid
if (src != NULL) {
// Set Photon
GPhoton photon(src->dir(), source.energy(), source.time());
// Compute Npred
npred = this->npred(photon, obs);
}
// Return Npred
return npred;
}
/***********************************************************************//**
* @brief Return spatial integral of radial source model
*
* @param[in] source Source.
* @param[in] obs Observation.
* @return Integral of radial source model over ROI.
*
* This method returns the spatial integral of a radial source model over
* the region of interest. If the model is not a radial model, the method
* returns 0.
***************************************************************************/
double GResponse::npred_radial(const GSource& source,
const GObservation& obs) const
{
// Initialise Npred value
double npred = 0.0;
// Get radial spatial model
const GModelSpatialRadial* spatial =
dynamic_cast<const GModelSpatialRadial*>(source.model());
// Continue only if spatial model is valid
if (spatial != NULL) {
// Compute rotation matrix to convert from native coordinates given
// by (theta,phi) into celestial coordinates.
GMatrix ry;
GMatrix rz;
ry.eulery(spatial->dec() - 90.0);
rz.eulerz(-spatial->ra());
GMatrix rot = (ry * rz).transpose();
// Set offset angle integration range
double theta_min = 0.0;
double theta_max = spatial->theta_max();
// Perform offset angle integration if interval is valid
if (theta_max > theta_min) {
// Setup integration kernel
GResponse::npred_radial_kern_theta integrand(*this,
*spatial,
source.energy(),
source.time(),
obs,
rot);
// Integrate over theta
GIntegral integral(&integrand);
npred = integral.romb(theta_min, theta_max);
// Compile option: Show integration results
#if defined(G_DEBUG_NPRED_RADIAL)
std::cout << "GResponse::npred_radial:";
std::cout << " theta_min=" << theta_min;
std::cout << " theta_max=" << theta_max;
std::cout << " npred=" << npred << std::endl;
#endif
} // endif: offset angle range was valid
// Debug: Check for NaN
#if defined(G_NAN_CHECK)
if (gammalib::is_notanumber(npred) || gammalib::is_infinite(npred)) {
std::cout << "*** ERROR: GResponse::npred_radial:";
std::cout << " NaN/Inf encountered";
std::cout << " (npred=" << npred;
std::cout << ", theta_min=" << theta_min;
std::cout << ", theta_max=" << theta_max;
std::cout << ")" << std::endl;
}
#endif
} // endif: radial model was valid
// Return Npred
return npred;
}
/***********************************************************************//**
* @brief Return spatial integral of elliptical source model
*
* @param[in] source Source.
* @param[in] obs Observation.
***************************************************************************/
double GResponse::npred_elliptical(const GSource& source,
const GObservation& obs) const
{
// Initialise Npred value
double npred = 0.0;
// Get elliptical spatial model
const GModelSpatialElliptical* spatial =
dynamic_cast<const GModelSpatialElliptical*>(source.model());
// Continue only if spatial model is valid
if (spatial != NULL) {
// Compute rotation matrix to convert from native coordinates given
// by (theta,phi) into celestial coordinates.
GMatrix ry;
GMatrix rz;
ry.eulery(spatial->dec() - 90.0);
rz.eulerz(-spatial->ra());
GMatrix rot = (ry * rz).transpose();
// Set offset angle integration range
double theta_min = 0.0;
double theta_max = spatial->theta_max();
// Perform offset angle integration if interval is valid
if (theta_max > theta_min) {
// Setup integration kernel
GResponse::npred_elliptical_kern_theta integrand(*this,
*spatial,
source.energy(),
source.time(),
obs,
rot);
// Integrate over theta
GIntegral integral(&integrand);
npred = integral.romb(theta_min, theta_max);
// Compile option: Show integration results
#if defined(G_DEBUG_NPRED_ELLIPTICAL)
std::cout << "GResponse::npred_elliptical:";
std::cout << " theta_min=" << theta_min;
std::cout << " theta_max=" << theta_max;
std::cout << " npred=" << npred << std::endl;
#endif
} // endif: offset angle range was valid
// Debug: Check for NaN
#if defined(G_NAN_CHECK)
if (gammalib::is_notanumber(npred) || gammalib::is_infinite(npred)) {
std::cout << "*** ERROR: GResponse::npred_elliptical:";
std::cout << " NaN/Inf encountered";
std::cout << " (npred=" << npred;
std::cout << ", theta_min=" << theta_min;
std::cout << ", theta_max=" << theta_max;
std::cout << ")" << std::endl;
}
#endif
} // endif: elliptical model was valid
// Return Npred
return npred;
}
/***********************************************************************//**
* @brief Return spatial integral of diffuse source model
*
* @param[in] source Source.
* @param[in] obs Observation.
*
* @exception GException::feature_not_implemented
* Method not yet implemented.
***************************************************************************/
double GResponse::npred_diffuse(const GSource& source,
const GObservation& obs) const
{
// Feature not yet implemented
throw GException::feature_not_implemented(G_NPRED_DIFFUSE,
"Npred computation not implemented for diffuse models.");
// Return Npred
return 0.0;
}
/*==========================================================================
= =
= Protected methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Initialise class members
***************************************************************************/
void GResponse::init_members(void)
{
// Return
return;
}
/***********************************************************************//**
* @brief Copy class members
*
* @param[in] rsp Response.
***************************************************************************/
void GResponse::copy_members(const GResponse& rsp)
{
// Return
return;
}
/***********************************************************************//**
* @brief Delete class members
***************************************************************************/
void GResponse::free_members(void)
{
// Return
return;
}
/***********************************************************************//**
* @brief Kernel for offset angle Npred integration of radial model
*
* @param[in] theta Radial model offset angle (radians).
***************************************************************************/
double GResponse::npred_radial_kern_theta::eval(const double& theta)
{
// Initialise Npred value
double npred = 0.0;
// Get radial model value
double model = m_spatial.eval(theta, m_srcEng, m_srcTime);
// Compute sine of offset angle
double sin_theta = std::sin(theta);
// Setup phi integration kernel
GResponse::npred_radial_kern_phi integrand(m_rsp,
m_srcEng,
m_srcTime,
m_obs,
m_rot,
theta,
sin_theta);
// Integrate over phi
GIntegral integral(&integrand);
npred = integral.romb(0.0, gammalib::twopi) * sin_theta * model;
// Debug: Check for NaN
#if defined(G_NAN_CHECK)
if (gammalib::is_notanumber(npred) || gammalib::is_infinite(npred)) {
std::cout << "*** ERROR: GResponse::npred_radial_kern_theta::eval";
std::cout << "(theta=" << theta << "):";
std::cout << " NaN/Inf encountered";
std::cout << " (npred=" << npred;
std::cout << ", model=" << model;
std::cout << ", sin_theta=" << sin_theta;
std::cout << ")" << std::endl;
}
#endif
// Return Npred
return npred;
}
/***********************************************************************//**
* @brief Kernel for azimuth angle Npred integration of radial model
*
* @param[in] phi Azimuth angle (radians).
***************************************************************************/
double GResponse::npred_radial_kern_phi::eval(const double& phi)
{
// Compute sky direction vector in native coordinates
double cos_phi = std::cos(phi);
double sin_phi = std::sin(phi);
GVector native(-cos_phi*m_sin_theta, sin_phi*m_sin_theta, m_cos_theta);
// Rotate from native into celestial system
GVector cel = m_rot * native;
// Set sky direction
GSkyDir srcDir;
srcDir.celvector(cel);
// Set photon
GPhoton photon(srcDir, m_srcEng, m_srcTime);
// Compute Npred for this sky direction
double npred = m_rsp.npred(photon, m_obs);
// Debug: Check for NaN
#if defined(G_NAN_CHECK)
if (gammalib::is_notanumber(npred) || gammalib::is_infinite(npred)) {
std::cout << "*** ERROR: GResponse::npred_radial_kern_phi::eval";
std::cout << "(phi=" << phi << "):";
std::cout << " NaN/Inf encountered";
std::cout << " (npred=" << npred;
std::cout << ", cos_phi=" << cos_phi;
std::cout << ", sin_phi=" << sin_phi;
std::cout << ")" << std::endl;
}
#endif
// Return Npred
return npred;
}
/***********************************************************************//**
* @brief Kernel for offset angle Npred integration of elliptical model
*
* @param[in] theta Radial model offset angle (radians).
***************************************************************************/
double GResponse::npred_elliptical_kern_theta::eval(const double& theta)
{
// Initialise Npred value
double npred = 0.0;
// Compute sine of offset angle
double sin_theta = std::sin(theta);
// Setup phi integration kernel
GResponse::npred_elliptical_kern_phi integrand(m_rsp,
m_spatial,
m_srcEng,
m_srcTime,
m_obs,
m_rot,
theta,
sin_theta);
// Integrate over phi
GIntegral integral(&integrand);
npred = integral.romb(0.0, gammalib::twopi) * sin_theta;
// Debug: Check for NaN
#if defined(G_NAN_CHECK)
if (gammalib::is_notanumber(npred) || gammalib::is_infinite(npred)) {
std::cout << "*** ERROR: GResponse::npred_radial_kern_theta::eval";
std::cout << "(theta=" << theta << "):";
std::cout << " NaN/Inf encountered";
std::cout << " (npred=" << npred;
std::cout << ", sin_theta=" << sin_theta;
std::cout << ")" << std::endl;
}
#endif
// Return Npred
return npred;
}
/***********************************************************************//**
* @brief Kernel for azimuth angle Npred integration of elliptical model
*
* @param[in] phi Azimuth angle (radians).
***************************************************************************/
double GResponse::npred_elliptical_kern_phi::eval(const double& phi)
{
// Compute sky direction vector in native coordinates
double cos_phi = std::cos(phi);
double sin_phi = std::sin(phi);
GVector native(-cos_phi*m_sin_theta, sin_phi*m_sin_theta, m_cos_theta);
// Rotate from native into celestial system
GVector cel = m_rot * native;
// Set sky direction
GSkyDir srcDir;
srcDir.celvector(cel);
// Set photon
GPhoton photon(srcDir, m_srcEng, m_srcTime);
// Get elliptical model value
double model = m_spatial.eval(m_theta, phi, m_srcEng, m_srcTime);
// Compute Npred for this sky direction
double npred = m_rsp.npred(photon, m_obs) * model;
// Debug: Check for NaN
#if defined(G_NAN_CHECK)
if (gammalib::is_notanumber(npred) || gammalib::is_infinite(npred)) {
std::cout << "*** ERROR: GResponse::npred_radial_kern_phi::eval";
std::cout << "(phi=" << phi << "):";
std::cout << " NaN/Inf encountered";
std::cout << " (npred=" << npred;
std::cout << ", model=" << model;
std::cout << ", cos_phi=" << cos_phi;
std::cout << ", sin_phi=" << sin_phi;
std::cout << ")" << std::endl;
}
#endif
// Return Npred
return npred;
}