Commit 44919bfd3dd89486447db23b32fd3663ff421b4b

Authored by Andreas Specovius
1 parent 667d8eb7

Implemented test case for csphagen which reads src and bkg regions from region f…

…iles. As output regions of test_cmd() and input regions of this test case are (should be!) the same a comparison of the results obtained with CUSTOM and with the REFLECTED method is made.
test/data/crab_bkg_reg.reg 0 → 100644
  1 +# Region file format: DS9 version 4.1
  2 +global color=green dashlist=8 3 width=1
  3 +fk5;circle(82.2412081162728,22.7975361111674,0.2)
  4 +fk5;circle(82.5360705305998,23.1293202741537,0.2)
  5 +fk5;circle(82.980058098929,23.260640435668,0.2)
  6 +fk5;circle(83.4304728956696,23.1492236356079,0.2)
  7 +fk5;circle(83.7424610541293,22.8309432073396,0.2)
... ...
test/data/crab_src_reg.reg 0 → 100644
  1 +# Region file format: DS9 version 4.1
  2 +global color=green dashlist=8 3 width=1
  3 +fk5;circle(83.633,22.0145,0.2)
... ...
test/test_csphagen.py
... ... @@ -52,6 +52,9 @@ class Test(test):
52 52 self._nbins = 10
53 53 # number of expected background regions with/wo exclusion,
54 54 # and for two different runs
  55 + self._regfile_src = self._datadir + '/crab_src_reg.reg'
  56 + self._regfile_bkg = self._datadir + '/crab_bkg_reg.reg'
  57 + self._counts_reflected = 0
55 58  
56 59 # Return
57 60 return
... ... @@ -67,6 +70,7 @@ class Test(test):
67 70 # Append tests
68 71 self.append(self._test_cmd, 'Test csphagen on command line')
69 72 self.append(self._test_python, 'Test csphagen from Python')
  73 + self.append(self._test_regfiles_cmd, 'Test csphagen input region files on command line')
70 74  
71 75 # Return
72 76 return
... ... @@ -103,6 +107,9 @@ class Test(test):
103 107 self._check_output('csphagen_cmd1', self._nbins, self._nreg_with_excl)
104 108 self._check_outobs('csphagen_cmd1', 1)
105 109  
  110 + # store on-counts for later testing bkgmethod CUSTOM
  111 + self._counts_reflected = self._read_pha_counts('csphagen_cmd1_pha_on.fits')
  112 +
106 113 # Setup csphagen command
107 114 cmd = csphagen + ' inobs="events_that_do_not_exist.fits" ' + \
108 115 'caldb="' + self._caldb + '" irf="' + self._irf + \
... ... @@ -259,6 +266,57 @@ class Test(test):
259 266 # Return
260 267 return
261 268  
  269 + # Test csphagen input region files (command line)
  270 + def _test_regfiles_cmd(self):
  271 + """
  272 + Test csphagen input region files (command line).
  273 + This is done by redoing the test performed in _test_cmd()
  274 + but this time bkgmethod is CUSTOM and the regions are read
  275 + from ds9 region files. Exactly the same output of csphagen
  276 + compared to _test_cmd() is expected.
  277 + Hint: The output region files of _test_cmd() could be used
  278 + directly, but to make the execution of this test case standalone
  279 + the output once created by _test_cmd() was copied and added
  280 + staticly to the repository.
  281 + """
  282 + # Set script name
  283 + csphagen = self._script('csphagen')
  284 +
  285 + # Setup csphagen command
  286 + # TODO: remove rad from cmd line arguments. currently it is needed (ctool::set_obs_bounds needs rad!)
  287 + cmd = csphagen + ' inobs="' + self._myevents1 + \
  288 + '" caldb="' + self._caldb + '" irf="' + self._irf + \
  289 + '" ebinalg="LOG" emin=0.1 emax=100. enumbins=' + \
  290 + str(self._nbins) + \
  291 + ' etruemin=0.05 etruemax=150 etruebins=5 '+ \
  292 + 'stack="no" ' + \
  293 + 'inexclusion="' + self._exclusion + '" ' + \
  294 + 'bkgmethod="CUSTOM" ' + \
  295 + 'rad=0.2 ' + \
  296 + 'srcreg="' + self._regfile_src + '" ' + \
  297 + 'bkgreg="' + self._regfile_bkg + '" ' + \
  298 + 'outobs="csphagen_regfiles_cmd1.xml" prefix="csphagen_regfiles_cmd1" ' + \
  299 + 'logfile="csphagen_regfiles_cmd1.log" chatter=2 ' + \
  300 + 'coordsys="CEL" ra=83.633 dec=22.0145'
  301 +
  302 + # Check if execution was successful
  303 + self.test_assert(self._execute(cmd) == 0,
  304 + 'Check successful execution from command line')
  305 +
  306 + # Check output files
  307 + self._check_output('csphagen_regfiles_cmd1', self._nbins, self._nreg_with_excl)
  308 + self._check_outobs('csphagen_regfiles_cmd1', 1)
  309 +
  310 + # Check resulting number of counts, which should be equal to those obtained in _test_cmd()
  311 + counts_custom = self._read_pha_counts('csphagen_regfiles_cmd1_pha_on.fits')
  312 +
  313 + self.test_assert(counts_custom == self._counts_reflected,
  314 + 'Check bkgmethods CUSTOM and REFLECTED: number of on-counts should be equal')
  315 +
  316 + # Return
  317 + return
  318 +
  319 +
262 320 def _check_ebounds(self, table, bins):
263 321 """
264 322 Check EBOUNDS table
... ... @@ -309,7 +367,7 @@ class Test(test):
309 367 self.test_assert(table.contains(col),
310 368 'FITS file contains "' + col + '" column')
311 369  
312   - # Check SPECTRUM table
  370 + # Check EBOUNDS table
313 371 table = fits['EBOUNDS']
314 372 self._check_ebounds(table, bins)
315 373  
... ... @@ -433,3 +491,26 @@ class Test(test):
433 491  
434 492 # Return
435 493 return
  494 +
  495 + def _read_pha_counts(self, filename):
  496 + """
  497 + Read and integrate the counts in a pha file.
  498 + Pha file structure already tested in _check_pha()
  499 + """
  500 + # Open FITS file
  501 + fits = gammalib.GFits(filename)
  502 +
  503 + # Get SPECTRUM table
  504 + table = fits['SPECTRUM']
  505 +
  506 + # Integrate counts
  507 + counts_col = table['COUNTS']
  508 + counts = 0
  509 + for channel in range( counts_col.nrows() ):
  510 + counts += counts_col[channel]
  511 +
  512 + # Close FITS file
  513 + fits.close()
  514 +
  515 + # Return
  516 + return counts
... ...