test_ctobssim.py
6.86 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
#! /usr/bin/env python
# ==========================================================================
# This scripts performs unit tests for the ctobssim tool.
#
# Copyright (C) 2014 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/>.
#
# ==========================================================================
import gammalib
import ctools
# ============================ #
# Test class for ctobssim tool #
# ============================ #
class Test(gammalib.GPythonTestSuite):
"""
Test class for ctobssim tool.
"""
# Constructor
def __init__(self):
"""
Constructor.
"""
# Call base class constructor
gammalib.GPythonTestSuite.__init__(self)
# Set members
self.model_name = "data/crab.xml"
self.caldb = "irf"
self.irf = "cta_dummy_irf"
# Return
return
# Set test functions
def set(self):
"""
Set all test functions.
"""
# Set test name
self.name("ctobssim")
# Append tests
self.append(self.test_functional, "Test ctobssim functionality")
self.append(self.test_container, "Test ctobssim on observation container")
# Return
return
# Test ctobssim functionnality
def test_functional(self):
"""
Test ctobssim functionnality.
"""
# Set-up ctobssim
sim = ctools.ctobssim()
sim["inmodel"].filename(self.model_name)
sim["outevents"].filename("events.fits")
sim["caldb"].string(self.caldb)
sim["irf"].string(self.irf)
sim["ra"].real(83.63)
sim["dec"].real(22.01)
sim["rad"].real(10.0)
sim["tmin"].real(0.0)
sim["tmax"].real(1800.0)
sim["emin"].real(0.1)
sim["emax"].real(100.0)
# Run tool
self.test_try("Run ctobssim")
try:
sim.run()
self.test_try_success()
except:
self.test_try_failure("Exception occured in ctobssim.")
# Retrieve observation and check content
obs = gammalib.GCTAObservation(sim.obs()[0])
pnt = obs.pointing()
evt = obs.events()
self.test_assert(obs.instrument() == "CTA", "Observation not a CTA observation")
self.test_value(sim.obs().size(), 1, "There is not a single observation")
self.test_value(obs.ontime(), 1800.0, 1.0e-6, "Ontime is not 1800 sec")
self.test_value(obs.livetime(), 1710.0, 1.0e-6, "Livetime is not 1710 sec")
self.test_value(pnt.dir().ra_deg(), 83.63, 1.0e-6, "ROI Right Ascension is not 83.63 deg")
self.test_value(pnt.dir().dec_deg(), 22.01, 1.0e-6, "ROI Declination is not 22.01 deg")
self.test_value(evt.size(), 4134, "Number of events is not 4134")
# Save events
self.test_try("Save events")
try:
sim.save()
self.test_try_success()
except:
self.test_try_failure("Exception occured in saving events.")
# Test ctobssim on observation container
def test_container(self):
"""
Test ctobssim on observation container.
"""
# Set-up observation container
obs = self.set_obs(4)
# Set-up ctobssim
sim = ctools.ctobssim(obs)
sim["outevents"].filename("sim_events.xml")
sim["inmodel"].filename(self.model_name)
# Run tool
self.test_try("Run ctobssim")
try:
sim.run()
self.test_try_success()
except:
self.test_try_failure("Exception occured in ctobssim.")
# Retrieve observation and check content
self.test_value(sim.obs().size(), 4, "There are not 4 observations")
# Save events
self.test_try("Save events")
try:
sim.save()
self.test_try_success()
except:
self.test_try_failure("Exception occured in saving events.")
# Setup observation container
def set_obs(self, number):
"""
Setup observation container.
"""
# Initialise empty observation container
obs = gammalib.GObservations()
# Initialise first time and identifier
tstart = 0.0
offset = -float(number)/2.0
# Loop over number of observations
for i in range(number):
id = "%6.6d" % i
obs_cta = self.set_one_obs(id, offset)
offset += 1.0
tstart += 1830.0
obs.append(obs_cta)
# Return observation container
return obs
# Setup one observation
def set_one_obs(self, id, offset, \
tstart=0.0, duration=1800.0, deadc=0.95, \
emin=0.1, emax=100.0, rad=5.0, \
irf="cta_dummy_irf", caldb="dummy"):
"""
Setup one observation for test purposes.
"""
# Allocate CTA observation
obs_cta = gammalib.GCTAObservation()
# Set calibration database
db = gammalib.GCaldb()
if (gammalib.dir_exists(self.caldb)):
db.rootdir(self.caldb)
else:
db.open("cta", self.caldb)
# Set pointing direction
pntdir = gammalib.GSkyDir()
pntdir.radec_deg(83.63, 22.01+offset)
pnt = gammalib.GCTAPointing()
pnt.dir(pntdir)
obs_cta.pointing(pnt)
# Set ROI
roi = gammalib.GCTARoi()
instdir = gammalib.GCTAInstDir()
instdir.dir(pntdir)
roi.centre(instdir)
roi.radius(rad)
# Set GTI
gti = gammalib.GGti()
gti.append(gammalib.GTime(tstart), gammalib.GTime(tstart+duration))
# Set energy boundaries
ebounds = gammalib.GEbounds(gammalib.GEnergy(emin, "TeV"), \
gammalib.GEnergy(emax, "TeV"))
# Allocate event list
events = gammalib.GCTAEventList()
events.roi(roi)
events.gti(gti)
events.ebounds(ebounds)
obs_cta.events(events)
# Set instrument response
obs_cta.response(self.irf, db)
# Set ontime, livetime, and deadtime correction factor
obs_cta.ontime(duration)
obs_cta.livetime(duration*deadc)
obs_cta.deadc(deadc)
obs_cta.id(id)
# Return CTA observation
return obs_cta