Commit 397573014d6e547d8a7ab6f91cb189f25c612a75
1 parent
1432fd7e
Added observation container test to ctobssim unit test.
Showing
1 changed file
with
115 additions
and
0 deletions
test/test_ctobssim.py
| @@ -56,6 +56,7 @@ class Test(gammalib.GPythonTestSuite): | @@ -56,6 +56,7 @@ class Test(gammalib.GPythonTestSuite): | ||
| 56 | 56 | ||
| 57 | # Append tests | 57 | # Append tests |
| 58 | self.append(self.test_functional, "Test ctobssim functionality") | 58 | self.append(self.test_functional, "Test ctobssim functionality") |
| 59 | + self.append(self.test_container, "Test ctobssim on observation container") | ||
| 59 | 60 | ||
| 60 | # Return | 61 | # Return |
| 61 | return | 62 | return |
| @@ -106,3 +107,117 @@ class Test(gammalib.GPythonTestSuite): | @@ -106,3 +107,117 @@ class Test(gammalib.GPythonTestSuite): | ||
| 106 | self.test_try_success() | 107 | self.test_try_success() |
| 107 | except: | 108 | except: |
| 108 | self.test_try_failure("Exception occured in saving events.") | 109 | self.test_try_failure("Exception occured in saving events.") |
| 110 | + | ||
| 111 | + # Test ctobssim on observation container | ||
| 112 | + def test_container(self): | ||
| 113 | + """ | ||
| 114 | + Test ctobssim on observation container. | ||
| 115 | + """ | ||
| 116 | + # Set-up observation container | ||
| 117 | + obs = self.set_obs(4) | ||
| 118 | + | ||
| 119 | + # Set-up ctobssim | ||
| 120 | + sim = ctools.ctobssim(obs) | ||
| 121 | + sim["outfile"].filename("sim_events.xml") | ||
| 122 | + | ||
| 123 | + # Run tool | ||
| 124 | + self.test_try("Run ctobssim") | ||
| 125 | + try: | ||
| 126 | + sim.run() | ||
| 127 | + self.test_try_success() | ||
| 128 | + except: | ||
| 129 | + self.test_try_failure("Exception occured in ctobssim.") | ||
| 130 | + | ||
| 131 | + # Retrieve observation and check content | ||
| 132 | + self.test_value(sim.obs().size(), 4, "There are not 4 observations") | ||
| 133 | + | ||
| 134 | + # Save events | ||
| 135 | + self.test_try("Save events") | ||
| 136 | + try: | ||
| 137 | + sim.save() | ||
| 138 | + self.test_try_success() | ||
| 139 | + except: | ||
| 140 | + self.test_try_failure("Exception occured in saving events.") | ||
| 141 | + | ||
| 142 | + # Setup observation container | ||
| 143 | + def set_obs(self, number): | ||
| 144 | + """ | ||
| 145 | + Setup observation container. | ||
| 146 | + """ | ||
| 147 | + # Initialise empty observation container | ||
| 148 | + obs = gammalib.GObservations() | ||
| 149 | + | ||
| 150 | + # Initialise first time and identifier | ||
| 151 | + tstart = 0.0 | ||
| 152 | + offset = -float(number)/2.0 | ||
| 153 | + | ||
| 154 | + # Loop over number of observations | ||
| 155 | + for i in range(number): | ||
| 156 | + id = "%6.6d" % i | ||
| 157 | + obs_cta = self.set_one_obs(id, offset) | ||
| 158 | + offset += 1.0 | ||
| 159 | + tstart += 1830.0 | ||
| 160 | + obs.append(obs_cta) | ||
| 161 | + | ||
| 162 | + # Return observation container | ||
| 163 | + return obs | ||
| 164 | + | ||
| 165 | + # Setup one observation | ||
| 166 | + def set_one_obs(self, id, offset, \ | ||
| 167 | + tstart=0.0, duration=1800.0, deadc=0.95, \ | ||
| 168 | + emin=0.1, emax=100.0, rad=5.0, \ | ||
| 169 | + irf="cta_dummy_irf", caldb="dummy"): | ||
| 170 | + """ | ||
| 171 | + Setup one observation for test purposes. | ||
| 172 | + """ | ||
| 173 | + # Allocate CTA observation | ||
| 174 | + obs_cta = gammalib.GCTAObservation() | ||
| 175 | + | ||
| 176 | + # Set calibration database | ||
| 177 | + db = gammalib.GCaldb() | ||
| 178 | + if (gammalib.dir_exists(self.caldb)): | ||
| 179 | + db.rootdir(self.caldb) | ||
| 180 | + else: | ||
| 181 | + db.open("cta", self.caldb) | ||
| 182 | + | ||
| 183 | + # Set pointing direction | ||
| 184 | + pntdir = gammalib.GSkyDir() | ||
| 185 | + pntdir.radec_deg(83.63, 22.01+offset) | ||
| 186 | + pnt = gammalib.GCTAPointing() | ||
| 187 | + pnt.dir(pntdir) | ||
| 188 | + obs_cta.pointing(pnt) | ||
| 189 | + | ||
| 190 | + # Set ROI | ||
| 191 | + roi = gammalib.GCTARoi() | ||
| 192 | + instdir = gammalib.GCTAInstDir() | ||
| 193 | + instdir.dir(pntdir) | ||
| 194 | + roi.centre(instdir) | ||
| 195 | + roi.radius(rad) | ||
| 196 | + | ||
| 197 | + # Set GTI | ||
| 198 | + gti = gammalib.GGti() | ||
| 199 | + gti.append(gammalib.GTime(tstart), gammalib.GTime(tstart+duration)) | ||
| 200 | + | ||
| 201 | + # Set energy boundaries | ||
| 202 | + ebounds = gammalib.GEbounds(gammalib.GEnergy(emin, "TeV"), \ | ||
| 203 | + gammalib.GEnergy(emax, "TeV")) | ||
| 204 | + | ||
| 205 | + # Allocate event list | ||
| 206 | + events = gammalib.GCTAEventList() | ||
| 207 | + events.roi(roi) | ||
| 208 | + events.gti(gti) | ||
| 209 | + events.ebounds(ebounds) | ||
| 210 | + obs_cta.events(events) | ||
| 211 | + | ||
| 212 | + # Set instrument response | ||
| 213 | + obs_cta.response(self.irf, db) | ||
| 214 | + | ||
| 215 | + # Set ontime, livetime, and deadtime correction factor | ||
| 216 | + obs_cta.ontime(duration) | ||
| 217 | + obs_cta.livetime(duration*deadc) | ||
| 218 | + obs_cta.deadc(deadc) | ||
| 219 | + obs_cta.id(id) | ||
| 220 | + | ||
| 221 | + # Return CTA observation | ||
| 222 | + return obs_cta | ||
| 223 | + | ||
| 109 | \ No newline at end of file | 224 | \ No newline at end of file |