csresspec.py
4.74 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
#! /usr/bin/env python
# ==========================================================================
# Generates a residual spectrum.
#
# Copyright (C) 2017- Luigi Tibaldo
#
# 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/>.
#
# ==========================================================================
import gammalib
import ctools
# =============== #
# csresspec class #
# =============== #
class csresspec(ctools.csobservation):
"""
Generates a residual map
"""
# Constructor
def __init__(self, *argv):
"""
Constructor
"""
# Initialise application by calling the appropriate class constructor
self._init_csobservation(self.__class__.__name__, ctools.__version__,
argv)
# Initialise class members
self._components = False
self._use_maps = False
self._stack = False
# Return
return
# Private methods
def _get_parameters(self):
"""
Get parameters from parfile and setup the observation
"""
# Set observation if not done before
if self.obs().is_empty():
self._require_inobs('csresspec._get_parameters()')
self.obs(self._get_observations())
# Set observation statistic
self._set_obs_statistic(gammalib.toupper(self['statistic'].string()))
# Collect number of unbinned, binned and On/Off observations in
# observation container
n_unbinned = 0
n_binned = 0
n_onoff = 0
for obs in self.obs():
if obs.classname() == 'GCTAObservation':
if obs.eventtype() == 'CountsCube':
n_binned += 1
else:
n_unbinned += 1
elif obs.classname() == 'GCTAOnOffObservation':
n_onoff += 1
n_cta = n_unbinned + n_binned + n_onoff
n_other = self.obs().size() - n_cta
# Query wether to compute model for individual components
self._components = self['components'].boolean()
# If there is only one binned observation and no model for individual
# components is required, query for precomputed model file and set
# use_maps to True
if self.obs().size() == 1 and n_binned == 1 and not self._components:
modcube = self['modcube'].filename()
if modcube != 'NONE':
self._use_maps = True
# If we are not using a precomputed model query input XML model file
if not self._use_maps:
modxml = self['inmodel'].filename()
# if None check whether models are provided in observation
# container, otherwise throw exception and stop here
if modxml == 'NONE' and self.obs().models().is_empty():
msg = 'No model provided. Please specify an input XML model file'
raise RuntimeError(msg)
# If there are unbinned observations query the energy binning parameters
if n_unbinned != 0:
self['ebinalg'].string()
if self['ebinalg'].string() == 'FILE':
self['ebinfile'].filename()
else:
self['emin'].real()
self['emax'].real()
self['enumbins'].integer()
# If there is more than one observation, and observations are unbinned
# or onoff query user to know if they wish stacked results
if self.obs().size() > 1 and (
n_unbinned == self.obs().size() or n_onoff == self.obs().size()):
self['stack'].boolean()
# Apply energy dispersion
self['edisp'].boolean()
# Read ahead output parameters
if self._read_ahead():
self['outfile'].filename()
# Write input parameters into logger
self._log_parameters(gammalib.TERSE)
# Log census of input observations
self._log_value(gammalib.TERSE, 'Unbinned CTA observations', n_unbinned)
self._log_value(gammalib.TERSE, 'Binned CTA observations', n_binned)
self._log_value(gammalib.TERSE, 'On/off CTA observations', n_onoff)
self._log_value(gammalib.TERSE, 'Other observations', n_other)
# Return
return