Commit 2940dac5b2abb2bdf4a053ff2889f1e467cac4b4

Authored by Jürgen Knödlseder
1 parent 25952ebe

Integrate code

cscripts/csbkgmodel.par
... ... @@ -29,7 +29,7 @@ spectral, s, a, NODES,PLAW|NODES,, "Spectral model component"
29 29 ebinalg, s, a, LOG,FILE|LIN|LOG,, "Algorithm for defining energy nodes"
30 30 emin, r, a, 0.1,,, "Lower energy limit (TeV)"
31 31 emax, r, a, 100.0,,, "Upper energy limit (TeV)"
32   -enumbins, i, a, 10,1,200, "Number of energy nodes"
  32 +enumbins, i, a, 8,1,200, "Number of energy nodes"
33 33 ebinfile, f, a, NONE,,, "Name of the file containing the energy node definition"
34 34 runwise, b, a, yes,,, "Generate runwise background model?"
35 35 rad, r, h, 2.0,,, "Radius for event selection (degrees)"
... ...
cscripts/csresspec.py
... ... @@ -43,9 +43,9 @@ class csresspec(ctools.csobservation):
43 43  
44 44 # Initialise class members
45 45 self._use_maps = False
46   - self._stack = False
47   - self._mask = False
48   - self._fits = gammalib.GFits()
  46 + self._stack = False
  47 + self._mask = False
  48 + self._fits = gammalib.GFits()
49 49  
50 50 # Return
51 51 return
... ... @@ -65,8 +65,8 @@ class csresspec(ctools.csobservation):
65 65 # Collect number of unbinned, binned and On/Off observations in
66 66 # observation container
67 67 n_unbinned = 0
68   - n_binned = 0
69   - n_onoff = 0
  68 + n_binned = 0
  69 + n_onoff = 0
70 70 for obs in self.obs():
71 71 if obs.classname() == 'GCTAObservation':
72 72 if obs.eventtype() == 'CountsCube':
... ... @@ -75,7 +75,7 @@ class csresspec(ctools.csobservation):
75 75 n_unbinned += 1
76 76 elif obs.classname() == 'GCTAOnOffObservation':
77 77 n_onoff += 1
78   - n_cta = n_unbinned + n_binned + n_onoff
  78 + n_cta = n_unbinned + n_binned + n_onoff
79 79 n_other = self.obs().size() - n_cta
80 80  
81 81 # Query whether to compute model for individual components
... ... @@ -185,18 +185,18 @@ class csresspec(ctools.csobservation):
185 185  
186 186 Parameters
187 187 ----------
188   - obs : `~gammalib.GObservations()`
  188 + obs : `~gammalib.GObservations`
189 189 Observation container with single event list
190 190  
191 191 Returns
192 192 -------
193   - obs : `~gammalib.GObservations()`
194   - Binned observation container dict with event list ROI and energy
195   - range information
  193 + obs, info : `~gammalib.GObservations`, dict
  194 + Binned observation container and dictionary with event list ROI
  195 + and energy range information
196 196 """
197 197 # Retrieve information about ROI in event list
198 198 roi = obs[0].roi()
199   - ra = roi.centre().dir().ra_deg()
  199 + ra = roi.centre().dir().ra_deg()
200 200 dec = roi.centre().dir().dec_deg()
201 201 rad = roi.radius()
202 202  
... ... @@ -208,20 +208,20 @@ class csresspec(ctools.csobservation):
208 208  
209 209 # Bin events
210 210 cntcube = ctools.ctbin(obs)
211   - cntcube['xref'] = ra
212   - cntcube['yref'] = dec
213   - cntcube['binsz'] = 0.02
214   - cntcube['nxpix'] = npix
215   - cntcube['nypix'] = npix
216   - cntcube['proj'] = 'TAN'
  211 + cntcube['xref'] = ra
  212 + cntcube['yref'] = dec
  213 + cntcube['binsz'] = 0.02
  214 + cntcube['nxpix'] = npix
  215 + cntcube['nypix'] = npix
  216 + cntcube['proj'] = 'TAN'
217 217 cntcube['coordsys'] = 'CEL'
218   - cntcube['ebinalg'] = self['ebinalg'].string()
  218 + cntcube['ebinalg'] = self['ebinalg'].string()
219 219 if self['ebinalg'].string() == 'FILE':
220 220 cntcube['ebinfile'] = self['ebinfile'].filename()
221 221 else:
222 222 cntcube['enumbins'] = self['enumbins'].integer()
223   - cntcube['emin'] = self['emin'].real()
224   - cntcube['emax'] = self['emax'].real()
  223 + cntcube['emin'] = self['emin'].real()
  224 + cntcube['emax'] = self['emax'].real()
225 225 cntcube.run()
226 226  
227 227 # Retrieve the binned observation container
... ... @@ -256,7 +256,7 @@ class csresspec(ctools.csobservation):
256 256  
257 257 Parameters
258 258 ----------
259   - cube : `~gammalib.GCTAEventCube()`
  259 + cube : `~gammalib.GCTAEventCube`
260 260 Event cube
261 261 ra : float (str 'INDEF' for no selection on direction)
262 262 Right Ascension (deg)
... ... @@ -271,7 +271,7 @@ class csresspec(ctools.csobservation):
271 271  
272 272 Returns
273 273 -------
274   - cube : `~gammalib.GCTAEventCube()`
  274 + cube : `~gammalib.GCTAEventCube`
275 275 Event cube
276 276 """
277 277 # Turn cube into observation container to feed to ctcubemask
... ... @@ -282,11 +282,11 @@ class csresspec(ctools.csobservation):
282 282  
283 283 # Use ctcubemask to mask event cube pixels
284 284 cubemask = ctools.ctcubemask(obs_cont)
285   - cubemask['ra'] = ra
286   - cubemask['dec'] = dec
287   - cubemask['rad'] = rad
288   - cubemask['emin'] = emin
289   - cubemask['emax'] = emax
  285 + cubemask['ra'] = ra
  286 + cubemask['dec'] = dec
  287 + cubemask['rad'] = rad
  288 + cubemask['emin'] = emin
  289 + cubemask['emax'] = emax
290 290 cubemask['regfile'] = regfile
291 291 cubemask.run()
292 292  
... ... @@ -306,14 +306,14 @@ class csresspec(ctools.csobservation):
306 306  
307 307 Parameters
308 308 ----------
309   - cube : `~gammalib.GCTAEventCube()`
  309 + cube : `~gammalib.GCTAEventCube`
310 310 Event cube
311 311 evlist_info : dict
312 312 Dictionary with information on original event list
313 313  
314 314 Returns
315 315 -------
316   - array : `~gammalib.GNdarray()'
  316 + array : `~gammalib.GNdarray'
317 317 Counts spectrum
318 318 """
319 319 # If we started from event list mask the ROI only
... ... @@ -357,13 +357,13 @@ class csresspec(ctools.csobservation):
357 357 ----------
358 358 obs_id : str
359 359 Observation id
360   - ebounds : `~gammalib.GEbounds()'
  360 + ebounds : `~gammalib.GEbounds'
361 361 Energy boundaries
362   - counts : `~gammalib.GNdarray()'
  362 + counts : `~gammalib.GNdarray'
363 363 Counts spectrum
364   - model : `~gammalib.GNdarray()'
  364 + model : `~gammalib.GNdarray'
365 365 Model spectrum
366   - residuals : `~gammalib.GNdarray()'
  366 + residuals : `~gammalib.GNdarray'
367 367 Residual spectrum
368 368  
369 369 Returns
... ... @@ -373,21 +373,21 @@ class csresspec(ctools.csobservation):
373 373 """
374 374 # Create FITS table columns
375 375 nrows = ebounds.size()
376   - energy_low = gammalib.GFitsTableDoubleCol('Emin', nrows)
  376 + energy_low = gammalib.GFitsTableDoubleCol('Emin', nrows)
377 377 energy_high = gammalib.GFitsTableDoubleCol('Emax', nrows)
378   - counts_col = gammalib.GFitsTableDoubleCol('Counts', nrows)
379   - model_col = gammalib.GFitsTableDoubleCol('Model', nrows)
380   - resid_col = gammalib.GFitsTableDoubleCol('Residuals', nrows)
  378 + counts_col = gammalib.GFitsTableDoubleCol('Counts', nrows)
  379 + model_col = gammalib.GFitsTableDoubleCol('Model', nrows)
  380 + resid_col = gammalib.GFitsTableDoubleCol('Residuals', nrows)
381 381 energy_low.unit('TeV')
382 382 energy_high.unit('TeV')
383 383  
384 384 # Fill FITS table columns
385 385 for i in range(nrows):
386   - energy_low[i] = ebounds.emin(i).TeV()
  386 + energy_low[i] = ebounds.emin(i).TeV()
387 387 energy_high[i] = ebounds.emax(i).TeV()
388   - counts_col[i] = counts[i]
389   - model_col[i] = model[i]
390   - resid_col[i] = residuals[i]
  388 + counts_col[i] = counts[i]
  389 + model_col[i] = model[i]
  390 + resid_col[i] = residuals[i]
391 391  
392 392 # Initialise FITS Table with extension set to obs id
393 393 table = gammalib.GFitsBinTable(nrows)
... ... @@ -413,16 +413,16 @@ class csresspec(ctools.csobservation):
413 413  
414 414 Parameters
415 415 ----------
416   - table : `~gammalib.GFitsBinTable()'
  416 + table : `~gammalib.GFitsBinTable'
417 417 FITS binary table
418 418 name : str
419 419 Column name
420   - data : `~gammalib.GEbounds()'
  420 + data : `~gammalib.GEbounds'
421 421 Data to be filled into new column
422 422  
423 423 Returns
424 424 -------
425   - table : `~gammalib.GFitsBinTable()'
  425 + table : `~gammalib.GFitsBinTable'
426 426 FITS binary table
427 427 """
428 428 # Check size compatibility
... ... @@ -451,11 +451,13 @@ class csresspec(ctools.csobservation):
451 451 """
452 452 Calculate residuals for 3D observation
453 453  
454   - :param obs: `~gammalib.GObservations`
  454 + Parameters
  455 + ----------
  456 + obs : `~gammalib.GObservations`
455 457 Observation container with a single observations of type GCTAObservation
456   - :param obs_id: str
  458 + obs_id : str
457 459 Observation ID
458   - :param ccube: `gammalib.GCTAEventCube'
  460 + ccube : `~gammalib.GCTAEventCube'
459 461 Count cube with stacked events lists
460 462  
461 463 Returns
... ... @@ -487,14 +489,14 @@ class csresspec(ctools.csobservation):
487 489 msg = 'Computing model cube'
488 490 self._log_string(gammalib.NORMAL, msg)
489 491 modelcube = ctools.ctmodel(obs)
490   - if ccube!='NONE':
  492 + if ccube != 'NONE':
491 493 modelcube.cube(ccube)
492 494 modelcube['edisp'] = self['edisp'].boolean()
493 495 modelcube.run()
494 496 modcube = modelcube.cube().copy()
495 497  
496 498 # Extract cntcube for residual computation
497   - if ccube!='NONE':
  499 + if ccube != 'NONE':
498 500 cntcube = ccube
499 501 else:
500 502 cntcube = obs[0].events().copy()
... ... @@ -503,7 +505,7 @@ class csresspec(ctools.csobservation):
503 505 msg = 'Computing counts, model, and residual spectra'
504 506 self._log_string(gammalib.NORMAL, msg)
505 507 counts = self._cube_to_spectrum(cntcube, evlist_info)
506   - model = self._cube_to_spectrum(modcube, evlist_info)
  508 + model = self._cube_to_spectrum(modcube, evlist_info)
507 509  
508 510 # Calculate residuals
509 511 residuals = obsutils.residuals(self, counts, model)
... ... @@ -530,11 +532,12 @@ class csresspec(ctools.csobservation):
530 532 model_cont.append(component)
531 533 modelcube.obs().models(model_cont)
532 534  
533   - # Run model cube
534   - modelcube['edisp'] = self['edisp'].boolean()
535 535 # Reset base cube that was modified internally by ctmodel
536 536 if ccube != 'NONE':
537 537 modelcube.cube(ccube)
  538 +
  539 + # Run model cube
  540 + modelcube['edisp'] = self['edisp'].boolean()
538 541 modelcube.run()
539 542  
540 543 # Extract spectrum of individual component
... ... @@ -551,13 +554,16 @@ class csresspec(ctools.csobservation):
551 554 """
552 555 Calculate residual for OnOff observation
553 556  
554   - :param obs: `~gammalib.GObservations`
  557 + Parameters
  558 + ----------
  559 + obs : `~gammalib.GObservations`
555 560 Observation container with a single observations of type GCTAOnOffObservation
556   - :param obs_id: str
  561 + obs_id : str
557 562 Observation ID
  563 +
558 564 Returns
559 565 -------
560   - table : `~gammalib.GFitsBinTable()'
  566 + table : `~gammalib.GFitsBinTable'
561 567 Residual spectrum as FITS binary table
562 568 """
563 569  
... ... @@ -572,10 +578,10 @@ class csresspec(ctools.csobservation):
572 578  
573 579 # Model for On region
574 580 background = onoff.model_background(obs.models()).counts_spectrum()
575   - alpha = onoff.on_spec().backscal_spectrum()
576   - model = background.copy()
577   - model *= alpha
578   - model += onoff.model_gamma(obs.models()).counts_spectrum()
  581 + alpha = onoff.on_spec().backscal_spectrum()
  582 + model = background.copy()
  583 + model *= alpha
  584 + model += onoff.model_gamma(obs.models()).counts_spectrum()
579 585  
580 586 # On Residuals
581 587 residuals = obsutils.residuals(self, counts, model)
... ... @@ -672,25 +678,25 @@ class csresspec(ctools.csobservation):
672 678 msg = 'Computing count cube from multiple unbinned observations'
673 679 self._log_string(gammalib.NORMAL, msg)
674 680  
675   - #build count cube
  681 + # Build count cube
676 682 binning = ctools.ctbin(self.obs())
677   - binning['xref'] = self['xref'].real()
678   - binning['yref'] = self['yref'].real()
679   - binning['proj'] = self['proj'].string()
  683 + binning['xref'] = self['xref'].real()
  684 + binning['yref'] = self['yref'].real()
  685 + binning['proj'] = self['proj'].string()
680 686 binning['coordsys'] = self['coordsys'].string()
681   - binning['ebinalg'] = self['ebinalg'].string()
682   - binning['nxpix'] = self['nxpix'].integer()
683   - binning['nypix'] = self['nypix'].integer()
684   - binning['binsz'] = self['binsz'].real()
  687 + binning['ebinalg'] = self['ebinalg'].string()
  688 + binning['nxpix'] = self['nxpix'].integer()
  689 + binning['nypix'] = self['nypix'].integer()
  690 + binning['binsz'] = self['binsz'].real()
685 691 if self['ebinalg'].string() == 'FILE':
686 692 binning['ebinfile'] = self['ebinfile'].filename().file()
687 693 else:
688 694 binning['enumbins'] = self['enumbins'].integer()
689   - binning['emin'] = self['emin'].real()
690   - binning['emax'] = self['emax'].real()
  695 + binning['emin'] = self['emin'].real()
  696 + binning['emax'] = self['emax'].real()
691 697 binning['chatter'] = self['chatter'].integer()
692 698 binning['clobber'] = self['clobber'].boolean()
693   - binning['debug'] = self['debug'].boolean()
  699 + binning['debug'] = self['debug'].boolean()
694 700 binning.run()
695 701  
696 702 #compute residuals using cube
... ... @@ -735,7 +741,6 @@ class csresspec(ctools.csobservation):
735 741 # Return
736 742 return
737 743  
738   -
739 744 def save(self):
740 745 """
741 746 Save residuals
... ... @@ -755,7 +760,6 @@ class csresspec(ctools.csobservation):
755 760 # Return
756 761 return
757 762  
758   -
759 763 def resspec(self):
760 764 """
761 765 Return residual FITS file
... ... @@ -768,10 +772,12 @@ class csresspec(ctools.csobservation):
768 772 # Return
769 773 return self._fits
770 774  
  775 +
771 776 # ======================== #
772 777 # Main routine entry point #
773 778 # ======================== #
774 779 if __name__ == '__main__':
  780 +
775 781 # Create instance of application
776 782 app = csresspec(sys.argv)
777 783  
... ...
doc/source/users/reference_manual/csresmap.rst
... ... @@ -18,14 +18,15 @@ binning and used for residual computation. Before residual computation,
18 18 the counts and model cubes are collapsed into maps by summing over all
19 19 energies. Four options exist then for residual computation:
20 20  
21   -- the subtraction of the model from the counts (SUB)
22   -- the subtraction and division by the model (SUBDIV)
23   -- the subtraction and division by the square root of the model (SUBDIVSQRT)
  21 +- the subtraction of the model from the counts (``SUB``)
  22 +- the subtraction and division by the model (``SUBDIV``)
  23 +- the subtraction and division by the square root of the model (``SUBDIVSQRT``)
24 24 - the computation of the significance based on the likelihood-ratio
25   - test for Poisson statistics (SIGNIFICANCE)
  25 + test for Poisson statistics (``SIGNIFICANCE``)
26 26  
27   -The SUBDIVSQRT and SIGNIFICANCE algorithms approximate the residual
  27 +The ``SUBDIVSQRT`` and ``SIGNIFICANCE`` algorithms approximate the residual
28 28 significance and may become inaccurate in the low-counting regime.
  29 +
29 30 The residual map is written into a FITS file.
30 31  
31 32  
... ...
doc/source/users/reference_manual/csresspec.rst
... ... @@ -32,13 +32,13 @@ residuals are calculated separately for each observation.
32 32  
33 33 Four options exist then for residual computation:
34 34  
35   -- the subtraction of the model from the counts (SUB)
36   -- the subtraction and division by the model (SUBDIV)
37   -- the subtraction and division by the square root of the model (SUBDIVSQRT)
  35 +- the subtraction of the model from the counts (``SUB``)
  36 +- the subtraction and division by the model (``SUBDIV``)
  37 +- the subtraction and division by the square root of the model (``SUBDIVSQRT``)
38 38 - the computation of the significance based on the likelihood-ratio
39   - test for Poisson statistics (SIGNIFICANCE)
  39 + test for Poisson statistics (``SIGNIFICANCE``)
40 40  
41   -The SUBDIVSQRT and SIGNIFICANCE algorithms approximate the residual
  41 +The ``SUBDIVSQRT`` and ``SIGNIFICANCE`` algorithms approximate the residual
42 42 significance and may become inaccurate in the low-counting regime.
43 43  
44 44 The counts, model, and residual spectra are written into a FITS file.
... ...
doc/source/users/tutorials/notebooks.rst
... ... @@ -19,9 +19,8 @@ this page.
19 19 * :download:`How to connect observations to specific models? <howto/howto_connect_models.ipynb>`
20 20 * :download:`How to perform a stacked analysis? <howto/howto_stacked_analysis.ipynb>`
21 21 * :download:`How to do advanced model manipulation and fitting in Python? <howto/howto_advanced_python_fitting.ipynb>`
22   -* :download:`Data preparation for 1DC How-tos <howto/howto_prepare_1dc.ipynb>`
23   -* :download:`Selecting events for H.E.S.S. public data
24   - <hess_dr1/selecting_events.ipynb>`
  22 +* :download:`Data preparation for 1DC How-to <howto/howto_prepare_1dc.ipynb>`
  23 +* :download:`Selecting events for H.E.S.S. public data <hess_dr1/selecting_events.ipynb>`
25 24 * :download:`Generating a sky map and finding sources for H.E.S.S. public data <hess_dr1/skymap.ipynb>`
26 25 * :download:`Generating a background model from H.E.S.S. public data <hess_dr1/bkgmodel.ipynb>`
27 26 * :download:`Performing an unbinned analysis of H.E.S.S. public data <hess_dr1/unbinned.ipynb>`
... ...