GCOMResponse.cpp
23.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
/***************************************************************************
* GCOMResponse.cpp - COMPTEL Response class *
* ----------------------------------------------------------------------- *
* copyright (C) 2012-2015 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 GCOMResponse.cpp
* @brief COMPTEL response class implementation
* @author Juergen Knoedlseder
*/
/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include "GTools.hpp"
#include "GMath.hpp"
#include "GFits.hpp"
#include "GCaldb.hpp"
#include "GEvent.hpp"
#include "GPhoton.hpp"
#include "GSource.hpp"
#include "GEnergy.hpp"
#include "GTime.hpp"
#include "GObservation.hpp"
#include "GFitsImage.hpp"
#include "GModelSky.hpp"
#include "GModelSpatialPointSource.hpp"
#include "GCOMResponse.hpp"
#include "GCOMObservation.hpp"
#include "GCOMEventBin.hpp"
#include "GCOMInstDir.hpp"
#include "GCOMException.hpp"
/* __ Method name definitions ____________________________________________ */
#define G_IRF "GCOMResponse::irf(GEvent&, GSource&, GObservation&)"
#define G_NROI "GCOMResponse::nroi(GModelSky&, GEnergy&, GTime&, "\
"GObservation&)"
#define G_EBOUNDS "GCOMResponse::ebounds(GEnergy&)"
/* __ Macros _____________________________________________________________ */
/* __ Coding definitions _________________________________________________ */
/* __ Debug definitions __________________________________________________ */
/* __ Constants __________________________________________________________ */
/*==========================================================================
= =
= Constructors/destructors =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Void constructor
*
* Creates an empty COMPTEL response.
***************************************************************************/
GCOMResponse::GCOMResponse(void) : GResponse()
{
// Initialise members
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Copy constructor
*
* @param[in] rsp COM response.
**************************************************************************/
GCOMResponse::GCOMResponse(const GCOMResponse& rsp) : GResponse(rsp)
{
// Initialise members
init_members();
// Copy members
copy_members(rsp);
// Return
return;
}
/***********************************************************************//**
* @brief Response constructor
*
* @param[in] iaqname IAQ file name.
* @param[in] caldb Calibration database.
*
* Create COMPTEL response by loading an IAQ file from a calibration
* database.
***************************************************************************/
GCOMResponse::GCOMResponse(const std::string& iaqname,
const GCaldb& caldb) : GResponse()
{
// Initialise members
init_members();
// Set calibration database
this->caldb(caldb);
// Load IRF
this->load(iaqname);
// Return
return;
}
/***********************************************************************//**
* @brief Destructor
*
* Destroys instance of COMPTEL response object.
***************************************************************************/
GCOMResponse::~GCOMResponse(void)
{
// Free members
free_members();
// Return
return;
}
/*==========================================================================
= =
= Operators =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Assignment operator
*
* @param[in] rsp COMPTEL response.
* @return COMPTEL response.
*
* Assigns COMPTEL response object to another COMPTEL response object. The
* assignment performs a deep copy of all information, hence the original
* object from which the assignment has been performed can be destroyed after
* this operation without any loss of information.
***************************************************************************/
GCOMResponse& GCOMResponse::operator=(const GCOMResponse& rsp)
{
// Execute only if object is not identical
if (this != &rsp) {
// Copy base class members
this->GResponse::operator=(rsp);
// Free members
free_members();
// Initialise members
init_members();
// Copy members
copy_members(rsp);
} // endif: object was not identical
// Return this object
return *this;
}
/*==========================================================================
= =
= Public methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Clear instance
*
* Clears COMPTEL response object by resetting all members to an initial
* state. Any information that was present in the object before will be lost.
***************************************************************************/
void GCOMResponse::clear(void)
{
// Free class members (base and derived classes, derived class first)
free_members();
this->GResponse::free_members();
// Initialise members
this->GResponse::init_members();
init_members();
// Return
return;
}
/***********************************************************************//**
* @brief Clone instance
*
* @return Pointer to deep copy of COMPTEL response.
***************************************************************************/
GCOMResponse* GCOMResponse::clone(void) const
{
return new GCOMResponse(*this);
}
/***********************************************************************//**
* @brief Return value of instrument response function
*
* @param[in] event Observed event.
* @param[in] photon Incident photon.
* @param[in] obs Observation.
* @return Instrument response function (cm2 sr-1)
*
* @exception GCOMException::bad_observation_type
* Observation is not a COMPTEL observation.
* @exception GCOMException::bad_instdir_type
* Instrument direction is not a COMPTEL instrument direction.
*
* Returns the instrument response function for a given observed photon
* direction as function of the assumed true photon direction. The result
* is given by
* \f[IRF = \frac{IAQ \times DRG \times DRX}{ontime \times ewidth}\f]
* where
* \f$IRF\f$ is the instrument response function,
* \f$IAQ\f$ is the COMPTEL response matrix (sr-1),
* \f$DRG\f$ is the geometry factor (cm2),
* \f$DRX\f$ is the exposure (s),
* \f$ontime\f$ is the ontime (s), and
* \f$ewidth\f$ is the energy width (MeV).
*
* The observed photon direction is spanned by the 3 values (Chi,Psi,Phibar).
* (Chi,Psi) is the scatter direction of the event, given in sky coordinates.
* Phibar is the Compton scatter angle, computed from the energy deposits.
***************************************************************************/
double GCOMResponse::irf(const GEvent& event,
const GPhoton& photon,
const GObservation& obs) const
{
// Extract COMPTEL observation
const GCOMObservation* observation = dynamic_cast<const GCOMObservation*>(&obs);
if (observation == NULL) {
throw GCOMException::bad_observation_type(G_IRF);
}
// Extract COMPTEL event bin
const GCOMEventBin* bin = dynamic_cast<const GCOMEventBin*>(&event);
if (bin == NULL) {
throw GCOMException::bad_event_type(G_IRF);
}
// Extract event parameters
const GCOMInstDir& obsDir = bin->dir();
// Extract photon parameters
const GSkyDir& srcDir = photon.dir();
const GTime& srcTime = photon.time();
// Compute angle between true photon arrival direction and scatter
// direction (Chi,Psi)
double phigeo = srcDir.dist_deg(obsDir.dir());
// Compute scatter angle index
int iphibar = int(obsDir.phibar() / m_phibar_bin_size);
// Extract IAQ value by linear inter/extrapolation in Phigeo
double iaq = 0.0;
if (iphibar < m_phibar_bins) {
double phirat = phigeo / m_phigeo_bin_size; // 0.5 at bin centre
int iphigeo = int(phirat); // index into which Phigeo falls
double eps = phirat - iphigeo - 0.5; // 0.0 at bin centre
if (iphigeo < m_phigeo_bins) {
int i = iphibar * m_phigeo_bins + iphigeo;
if (eps < 0.0) { // interpolate towards left
if (iphigeo > 0) {
iaq = (1.0 + eps) * m_iaq[i] - eps * m_iaq[i-1];
}
else {
iaq = (1.0 - eps) * m_iaq[i] + eps * m_iaq[i+1];
}
}
else { // interpolate towards right
if (iphigeo < m_phigeo_bins-1) {
iaq = (1.0 - eps) * m_iaq[i] + eps * m_iaq[i+1];
}
else {
iaq = (1.0 + eps) * m_iaq[i] - eps * m_iaq[i-1];
}
}
}
}
// Get DRG value (units: cm2)
double drg = observation->drg()(obsDir.dir(), iphibar);
// Get DRX value (units: sec)
double drx = observation->drx()(srcDir);
// Get ontime
double ontime = observation->ontime(); // sec
// Compute IRF value
double irf = iaq * drg * drx / ontime;
// Apply deadtime correction
irf *= obs.deadc(srcTime);
// Compile option: Check for NaN/Inf
#if defined(G_NAN_CHECK)
if (gammalib::is_notanumber(irf) || gammalib::is_infinite(irf)) {
std::cout << "*** ERROR: GCOMResponse::irf:";
std::cout << " NaN/Inf encountered";
std::cout << " (irf=" << irf;
std::cout << ", iaq=" << iaq;
std::cout << ", drg=" << drg;
std::cout << ", drx=" << drx;
std::cout << ")";
std::cout << std::endl;
}
#endif
// Return IRF value
return irf;
}
/***********************************************************************//**
* @brief Return instrument response
*
* @param[in] event Event.
* @param[in] source Source.
* @param[in] obs Observation.
* @return Instrument response.
*
* Returns the instrument response for a given event, source and observation.
***************************************************************************/
double GCOMResponse::irf(const GEvent& event,
const GSource& source,
const GObservation& obs) const
{
// Initialise IRF value
double irf = 0.0;
// Select IRF depending on the spatial model type
switch (source.model()->code()) {
case GMODEL_SPATIAL_POINT_SOURCE:
{
const GModelSpatialPointSource* src =
static_cast<const GModelSpatialPointSource*>(source.model());
GPhoton photon(src->dir(), source.energy(), source.time());
irf = this->irf(event, photon, obs);
}
break;
case GMODEL_SPATIAL_RADIAL:
case GMODEL_SPATIAL_ELLIPTICAL:
case GMODEL_SPATIAL_DIFFUSE:
{
std::string msg = "Response computation not yet implemented for "
"spatial model type \""+source.model()->type()+"\".";
throw GException::feature_not_implemented(G_IRF, msg);
}
break;
default:
break;
}
// Return IRF value
return irf;
}
/***********************************************************************//**
* @brief Return integral of event probability for a given sky model over ROI
*
* @param[in] model Sky model.
* @param[in] obsEng Observed photon energy.
* @param[in] obsTime Observed photon arrival time.
* @param[in] obs Observation.
* @return 0.0
*
* @exception GException::feature_not_implemented
* Method is not implemented.
***************************************************************************/
double GCOMResponse::nroi(const GModelSky& model,
const GEnergy& obsEng,
const GTime& obsTime,
const GObservation& obs) const
{
// Method is not implemented
std::string msg = "Spatial integration of sky model over the data space "
"is not implemented.";
throw GException::feature_not_implemented(G_NROI, msg);
// Return Npred
return (0.0);
}
/***********************************************************************//**
* @brief Return true energy boundaries for a specific observed energy
*
* @param[in] obsEnergy Observed Energy.
* @return True energy boundaries for given observed energy.
*
* @exception GException::feature_not_implemented
* Method is not implemented.
***************************************************************************/
GEbounds GCOMResponse::ebounds(const GEnergy& obsEnergy) const
{
// Initialise an empty boundary object
GEbounds ebounds;
// Throw an exception
std::string msg = "Energy dispersion not implemented.";
throw GException::feature_not_implemented(G_EBOUNDS, msg);
// Return energy boundaries
return ebounds;
}
/***********************************************************************//**
* @brief Load COMPTEL response.
*
* @param[in] rspname COMPTEL response name.
*
* Loads the COMPTEL response with specified name @p rspname. The method first
* searchs for an appropriate response in the calibration database. If no
* appropriate response is found, the method takes the database root path
* and response name to build the full path to the response file, and tries
* to load the response from these paths.
***************************************************************************/
void GCOMResponse::load(const std::string& rspname)
{
// Clear instance but conserve calibration database
GCaldb caldb = m_caldb;
clear();
m_caldb = caldb;
// Save response name
m_rspname = rspname;
// First attempt reading the response using the GCaldb interface
std::string filename = m_caldb.filename("","","IAQ","","",rspname);
// If filename is empty then build filename from CALDB root path and
// response name
if (filename.empty()) {
filename = gammalib::filepath(m_caldb.rootdir(), rspname);
if (!gammalib::file_exists(filename)) {
std::string testname = filename + ".fits";
if (gammalib::file_exists(testname)) {
filename = testname;
}
else {
std::string testname = filename + ".fits.gz";
if (gammalib::file_exists(testname)) {
filename = testname;
}
}
}
}
// Open FITS file
GFits file(filename);
// Get IAQ image
const GFitsImage& iaq = *file.image(0);
// Read IAQ
read(iaq);
// Close ARF FITS file
file.close();
// Return
return;
}
/***********************************************************************//**
* @brief Read COMPTEL response from FITS image.
*
* @param[in] image FITS image.
*
* Read the COMPTEL response from IAQ FITS file and convert the IAQ values
* into a probability per steradian.
***************************************************************************/
void GCOMResponse::read(const GFitsImage& image)
{
// Store IAQ dimensions
m_phigeo_bins = image.naxes(0);
m_phibar_bins = image.naxes(1);
// Store IAQ axes definitions
m_phigeo_ref_value = image.real("CRVAL1");
m_phigeo_ref_pixel = image.real("CRPIX1");
m_phigeo_bin_size = image.real("CDELT1");
m_phibar_ref_value = image.real("CRVAL2");
m_phibar_ref_pixel = image.real("CRPIX2");
m_phibar_bin_size = image.real("CDELT2");
// Get axes minima (values of first bin)
m_phigeo_min = m_phigeo_ref_value + (1.0-m_phigeo_ref_pixel) * m_phigeo_bin_size;
m_phibar_min = m_phibar_ref_value + (1.0-m_phibar_ref_pixel) * m_phibar_bin_size;
// Compute IAQ size. Continue only if size is positive
int size = m_phigeo_bins * m_phibar_bins;
if (size > 0) {
// Allocate memory for IAQ
m_iaq.assign(size, 0.0);
// Copy over IAQ values
for (int i = 0; i < size; ++i) {
m_iaq[i] = image.pixel(i);
}
} // endif: size was positive
// Convert IAQ matrix from probability per Phigeo bin into a
// probability per steradian
double omega0 = gammalib::fourpi *
std::sin(0.5 * m_phigeo_bin_size * gammalib::deg2rad);
for (int iphigeo = 0; iphigeo < m_phigeo_bins; ++iphigeo) {
double phigeo = iphigeo * m_phigeo_bin_size + m_phigeo_min;
double omega = omega0 * std::sin(phigeo * gammalib::deg2rad);
for (int iphibar = 0; iphibar < m_phibar_bins; ++iphibar) {
m_iaq[iphigeo+iphibar*m_phigeo_bins] /= omega;
}
}
// Return
return;
}
/***********************************************************************//**
* @brief Print COMPTEL response information
*
* @param[in] chatter Chattiness (defaults to NORMAL).
* @return String containing COMPTEL response information.
***************************************************************************/
std::string GCOMResponse::print(const GChatter& chatter) const
{
// Initialise result string
std::string result;
// Continue only if chatter is not silent
if (chatter != SILENT) {
// Append header
result.append("=== GCOMResponse ===");
// Append response name
result.append("\n"+gammalib::parformat("Response name")+m_rspname);
// Append calibration database
result.append("\n"+m_caldb.print(chatter));
// Append information
result.append("\n"+gammalib::parformat("Number of Phigeo bins"));
result.append(gammalib::str(m_phigeo_bins));
result.append("\n"+gammalib::parformat("Number of Phibar bins"));
result.append(gammalib::str(m_phibar_bins));
result.append("\n"+gammalib::parformat("Phigeo reference value"));
result.append(gammalib::str(m_phigeo_ref_value)+" deg");
result.append("\n"+gammalib::parformat("Phigeo reference pixel"));
result.append(gammalib::str(m_phigeo_ref_pixel));
result.append("\n"+gammalib::parformat("Phigeo bin size"));
result.append(gammalib::str(m_phigeo_bin_size)+" deg");
result.append("\n"+gammalib::parformat("Phigeo first bin value"));
result.append(gammalib::str(m_phigeo_min)+" deg");
result.append("\n"+gammalib::parformat("Phibar reference value"));
result.append(gammalib::str(m_phibar_ref_value)+" deg");
result.append("\n"+gammalib::parformat("Phibar reference pixel"));
result.append(gammalib::str(m_phibar_ref_pixel));
result.append("\n"+gammalib::parformat("Phibar bin size"));
result.append(gammalib::str(m_phibar_bin_size)+" deg");
result.append("\n"+gammalib::parformat("Phibar first bin value"));
result.append(gammalib::str(m_phibar_min)+" deg");
} // endif: chatter was not silent
// Return result
return result;
}
/*==========================================================================
= =
= Private methods =
= =
==========================================================================*/
/***********************************************************************//**
* @brief Initialise class members
***************************************************************************/
void GCOMResponse::init_members(void)
{
// Initialise members
m_caldb.clear();
m_rspname.clear();
m_iaq.clear();
m_phigeo_bins = 0;
m_phibar_bins = 0;
m_phigeo_ref_value = 0.0;
m_phigeo_ref_pixel = 0.0;
m_phigeo_bin_size = 0.0;
m_phigeo_min = 0.0;
m_phibar_ref_value = 0.0;
m_phibar_ref_pixel = 0.0;
m_phibar_bin_size = 0.0;
m_phibar_min = 0.0;
// Return
return;
}
/***********************************************************************//**
* @brief Copy class members
*
* @param[in] rsp COMPTEL response.
***************************************************************************/
void GCOMResponse::copy_members(const GCOMResponse& rsp)
{
// Copy attributes
m_caldb = rsp.m_caldb;
m_rspname = rsp.m_rspname;
m_iaq = rsp.m_iaq;
m_phigeo_bins = rsp.m_phigeo_bins;
m_phibar_bins = rsp.m_phibar_bins;
m_phigeo_ref_value = rsp.m_phigeo_ref_value;
m_phigeo_ref_pixel = rsp.m_phigeo_ref_pixel;
m_phigeo_bin_size = rsp.m_phigeo_bin_size;
m_phigeo_min = rsp.m_phigeo_min;
m_phibar_ref_value = rsp.m_phibar_ref_value;
m_phibar_ref_pixel = rsp.m_phibar_ref_pixel;
m_phibar_bin_size = rsp.m_phibar_bin_size;
m_phibar_min = rsp.m_phibar_min;
// Return
return;
}
/***********************************************************************//**
* @brief Delete class members
***************************************************************************/
void GCOMResponse::free_members(void)
{
// Return
return;
}