Commit 1898bd89f89c3219bd1a3d4a87d048be75360560

Authored by Jürgen Knödlseder
1 parent 9d76a7e9

Integrate code (#3436)

cscripts/Makefile.am
... ... @@ -182,6 +182,7 @@ testdata = $(top_srcdir)/test/data/crab.xml \
182 182 $(top_srcdir)/test/data/crab_offaxis1.fits.gz \
183 183 $(top_srcdir)/test/data/crab_offaxis2.fits.gz \
184 184 $(top_srcdir)/test/data/crab_exclusion.fits.gz \
  185 + $(top_srcdir)/test/data/background_events.fits.gz \
185 186 $(top_srcdir)/test/data/crab_src_reg.reg \
186 187 $(top_srcdir)/test/data/crab_bkg_reg.reg \
187 188 $(top_srcdir)/test/data/tsmap_0.fits \
... ...
cscripts/csphagen.py
... ... @@ -3,7 +3,7 @@
3 3 # Computes the PHA spectra for source/background and ARF/RMF files using the
4 4 # reflected region method
5 5 #
6   -# Copyright (C) 2017-2020 Luigi Tibaldo
  6 +# Copyright (C) 2017-2021 Luigi Tibaldo
7 7 #
8 8 # This program is free software: you can redistribute it and/or modify
9 9 # it under the terms of the GNU General Public License as published by
... ... @@ -308,38 +308,52 @@ class csphagen(ctools.csobservation):
308 308 Off observations must be event lists
309 309 """
310 310  
311   - # Set up Off observations
312   - # If there are no Off observations in container then load them via user parameters
  311 + # Set up Off observations. If there are no Off observations in the
  312 + # container then load them via user parameters
313 313 if self.obs_off().is_empty():
  314 +
  315 + # Get Off observation file name
314 316 filename = self['inobsoff'].filename()
315   - # If it is a FITS file load into observation and append to container
  317 +
  318 + # If Off observation is a FITS file then load observation and
  319 + # append it to the Off observation container
316 320 if gammalib.GFilename(filename).is_fits():
317 321 self._obs_off.append(gammalib.GCTAObservation(filename))
318   - # Otherwise load file into container
  322 +
  323 + # ... otherwise load XML file into Off observation container
319 324 else:
320 325 self._obs_off.load(filename)
321 326  
322   - # Check that size of On and Off observations are the same, otherwise throw error
  327 + # Check that size of On and Off observations are the same, otherwise
  328 + # raise error
323 329 if self.obs().size() != self.obs_off().size():
324 330 raise RuntimeError('On and Off observations must have the same size')
325 331  
326 332 # Loop through observations
327 333 for obs in self.obs_off():
  334 +
328 335 # Check that observation is event list, otherwise throw error
329 336 if obs.eventtype() != "EventList":
330 337 raise RuntimeError('Off observations must be event lists')
  338 +
331 339 # Check that they have response, otherwise assign based on user parameter
332 340 if obs.has_response() == False:
  341 +
  342 + # Get database and IRF
333 343 database = self["caldb"].string()
334   - irf = self["irf"].string()
335   - # xml element for response
336   - response_xml = "parameter name=\"Calibration\"" +\
  344 + irf = self["irf"].string()
  345 +
  346 + # Create an XML element for response
  347 + parameter = "parameter name=\"Calibration\"" +\
337 348 " database=\"" + database + "\"" +\
338 349 " response=\"" + irf + "\""
339 350 xml = gammalib.GXmlElement()
340   - xml.append(response_xml)
341   - # define response and assign to observation
  351 + xml.append(parameter)
  352 +
  353 + # Create CTA response
342 354 response = gammalib.GCTAResponseIrf(xml)
  355 +
  356 + # Attach response to observation
343 357 obs.response(response)
344 358  
345 359 # Add models from Off observations to model container
... ... @@ -609,8 +623,10 @@ class csphagen(ctools.csobservation):
609 623  
610 624 def _instrument_regions(self, obs, obs_off):
611 625 """
612   - Calculate background region in Off observation that corresponds to the source region
613   - in the On observation in instrument coordinates
  626 + Compute background regions for Off observation
  627 +
  628 + Calculate background region in Off observation that corresponds to the
  629 + source region in the On observation in instrument coordinates
614 630  
615 631 Parameters
616 632 ----------
... ... @@ -637,6 +653,7 @@ class csphagen(ctools.csobservation):
637 653 # If circle
638 654 if self._srcshape == 'CIRCLE':
639 655 region = gammalib.GSkyRegionCircle(off_dir, self._rad)
  656 +
640 657 # ... otherwise if rectangle
641 658 elif self._srcshape == 'RECT':
642 659 # Instrument coordinates take sky direction as reference
... ... @@ -651,11 +668,11 @@ class csphagen(ctools.csobservation):
651 668 if self._has_exclusion:
652 669 if self._excl_reg.overlaps(region):
653 670 # Signal region overlap
654   - msg = ' Background region overlaps with ' \
655   - 'exclusion region.'
  671 + msg = ' Background region overlaps with exclusion region.'
656 672 self._log_string(gammalib.EXPLICIT, msg)
657 673 is_valid = False
658   - # ... otherwise store region in container
  674 +
  675 + # If region is valid then append it to container
659 676 if is_valid:
660 677 regions.append(region)
661 678  
... ...
test/data/background_events.fits deleted
No preview for this file type
test/data/background_events.fits.gz 0 → 100644
No preview for this file type
test/test_csphagen.py
... ... @@ -2,7 +2,7 @@
2 2 # ==========================================================================
3 3 # This scripts performs unit tests for the csphagen script.
4 4 #
5   -# Copyright (C) 2017-2020 Luigi Tibaldo
  5 +# Copyright (C) 2017-2021 Luigi Tibaldo
6 6 #
7 7 # This program is free software: you can redistribute it and/or modify
8 8 # it under the terms of the GNU General Public License as published by
... ... @@ -422,11 +422,11 @@ class Test(test):
422 422 phagen['inmodel'] = 'NONE'
423 423 phagen['caldb'] = self._caldb
424 424 phagen['irf'] = self._irf
425   - phagen['srcshape'] = 'CIRCLE'
426   - phagen['coordsys'] = 'CEL'
427   - phagen['ra'] = 83.633
428   - phagen['dec'] = 22.0145
429   - phagen['rad'] = 0.2
  425 + phagen['srcshape'] = 'CIRCLE'
  426 + phagen['coordsys'] = 'CEL'
  427 + phagen['ra'] = 83.633
  428 + phagen['dec'] = 22.0145
  429 + phagen['rad'] = 0.2
430 430 phagen['ebinalg'] = 'LOG'
431 431 phagen['emin'] = 0.1
432 432 phagen['emax'] = 100.0
... ... @@ -449,8 +449,8 @@ class Test(test):
449 449 self._check_output('csphagen_py9', self._nbins, 1)
450 450 self._check_outobs('csphagen_py9_obs.xml', 1)
451 451  
452   - # Setup csphagen for test with dedicated Off observations
453   - # and exclusion region
  452 + # Setup csphagen for test with dedicated Off observations and
  453 + # exclusion region
454 454 phagen = cscripts.csphagen()
455 455 phagen['inobs'] = self._myevents1
456 456 phagen['inobsoff'] = self._offevents
... ... @@ -498,11 +498,11 @@ class Test(test):
498 498 phagen = cscripts.csphagen(obs)
499 499 phagen.obs_off(obs_off)
500 500 phagen['inmodel'] = 'NONE'
501   - phagen['srcshape'] = 'CIRCLE'
502   - phagen['coordsys'] = 'CEL'
503   - phagen['ra'] = 83.633
504   - phagen['dec'] = 22.0145
505   - phagen['rad'] = 0.2
  501 + phagen['srcshape'] = 'CIRCLE'
  502 + phagen['coordsys'] = 'CEL'
  503 + phagen['ra'] = 83.633
  504 + phagen['dec'] = 22.0145
  505 + phagen['rad'] = 0.2
506 506 phagen['ebinalg'] = 'LOG'
507 507 phagen['emin'] = 0.1
508 508 phagen['emax'] = 100.0
... ...