Commit 0a50ee54974102f39bdd7ce5f7b930cd908b410a

Authored by Jürgen Knödlseder
2 parents bebc74bb bbccb39e

Merge branch 'integration' into devel

# Conflicts:
#	ChangeLog
ChangeLog
... ... @@ -3,6 +3,7 @@
3 3 * Version 1.5.0 released
4 4 ========================
5 5  
  6 + Add tutorial for classical analysis (#2267)
6 7 Fix chatter level handling in ctpsfcube (#2262)
7 8 Handle On/Off observations by cspull (#2260)
8 9 Add "statistic" parameter to all likelihood tools and scripts (#2237)
... ...
cscripts/csspec.py
... ... @@ -192,7 +192,7 @@ class csspec(ctools.csobservation):
192 192 if self._binned_mode:
193 193 cube_ebounds = self.obs()[0].events().ebounds()
194 194 else:
195   - cube_ebounds = self.obs()[0].rmf().etrue()
  195 + cube_ebounds = self.obs()[0].rmf().emeasured()
196 196  
197 197 # Loop over user energy boundaries and collect all energy bins
198 198 # that overlap
... ...
doc/source/admin/release_history/1.5.rst
... ... @@ -24,6 +24,8 @@ Bug fixes
24 24 Improvements
25 25 ------------
26 26  
  27 +* [`2267 <https://cta-redmine.irap.omp.eu/issues/2267>`_] -
  28 + Add tutorial for classical analysis
27 29 * [`2260 <https://cta-redmine.irap.omp.eu/issues/2260>`_] -
28 30 Handle On/Off observations by :ref:`cspull`
29 31 * [`2237 <https://cta-redmine.irap.omp.eu/issues/2237>`_] -
... ...
doc/source/users/reference_manual/ctskymap.rst
... ... @@ -22,7 +22,7 @@ Instrument Response Functions will be used for background subtraction.
22 22  
23 23 ctskymap generates a FITS file comprising a sky map as primary extension.
24 24 If a background subtraction method was selected, the FITS file will contain
25   -the additional extensions ``BACKGRAOUND`` and ``SIGNIFICANCE`` that contain
  25 +the additional extensions ``BACKGROUND`` and ``SIGNIFICANCE`` that contain
26 26 the background map and a significance map, respectively. For the latter, the
27 27 significance of the signal is computed for each sky map pixel.
28 28  
... ... @@ -90,7 +90,7 @@ Background subtraction configuration parameters
90 90 ``outradius [real]``
91 91 Outer ring radius for RING subtraction (in degrees).
92 92  
93   -``(regfile = NONE) [file]``
  93 +``(inexclusion = NONE) [file]``
94 94 Exclusion region file as either a FITS map or DS9 region file.
95 95  
96 96  
... ...
doc/source/users/tutorials/1dc/classic_analysis_SED.png 0 → 100644

88.2 KB

doc/source/users/tutorials/1dc/classic_analysis_skymap.png 0 → 100644

66.3 KB

doc/source/users/tutorials/1dc/classic_analysis_skymap_exclusion.png 0 → 100644

66.3 KB

doc/source/users/tutorials/1dc/classic_analysis_skymap_onoff.png 0 → 100644

172 KB

doc/source/users/tutorials/1dc/classical_analysis.rst 0 → 100644
  1 +.. _classical_analysis:
  2 +
  3 +Performing a classical (On/Off) analysis
  4 +----------------------------------------
  5 +
  6 + .. admonition:: What you will learn
  7 +
  8 + You will learn how to perform a *classical* analysis
  9 + of a source. In thys type of analysis you do not need to have a 3D (spatial
  10 + and spectral) model of the residual hadronic background. First you create a
  11 + skymap to identify a source region. Then, you extract the measured counts
  12 + from the source region and from one or more background regions, and perform
  13 + a 1D maximum likelihood analysis to derive the source's spectrum.
  14 +
  15 +We will derive the spectrum of a source with known position, i.e., the supernova
  16 +remnants Cassiopeia A. First, you want to use the :ref:`csobsselect` script to
  17 +preselect observations around our source of interest.
  18 +
  19 +.. code-block:: bash
  20 +
  21 + csobsselect
  22 + Input event list or observation definition XML file [obs.xml] $CTA_DATA/obs/obs_gps_baseline.xml
  23 + Pointing selection region shape (CIRCLE|BOX) [CIRCLE]
  24 + Coordinate system (CEL - celestial, GAL - galactic) (CEL|GAL) [CEL]
  25 + Right Ascension of selection centre (deg) (0-360) [83.63] 350.85
  26 + Declination of selection centre (deg) (-90-90) [22.01] 58.815
  27 + Radius of selection circle (deg) (0-180) [5.0] 2.5
  28 + Output observation definition XML file [outobs.xml] obs.xml
  29 +
  30 +.. note::
  31 +
  32 + We are limiting the offset between pointing and source directions to 2.5 deg
  33 + for the sake of a faster execution time for the tutorial. You can/should use
  34 + all observations for which the source is within the CTA field of view.
  35 +
  36 +Next, you want to create a skymap to identify the source
  37 +region. You can do this using the :ref:`ctskymap` tool.
  38 +
  39 +.. code-block:: bash
  40 +
  41 + $ ctskymap
  42 + Input event list or observation definition XML file [events.fits] obs.xml
  43 + First coordinate of image center in degrees (RA or galactic l) (0-360) [83.63] 350.85
  44 + Second coordinate of image center in degrees (DEC or galactic b) (-90-90) [22.01] 58.815
  45 + Projection method (AIT|AZP|CAR|GLS|MER|MOL|SFL|SIN|STG|TAN) [CAR] TAN
  46 + Coordinate system (CEL - celestial, GAL - galactic) (CEL|GAL) [CEL]
  47 + Image scale (in degrees/pixel) [0.02]
  48 + Size of the X axis in pixels [200] 250
  49 + Size of the Y axis in pixels [200] 250
  50 + Lower energy limit (TeV) [0.1]
  51 + Upper energy limit (TeV) [100.0] 50.
  52 + Background subtraction method (NONE|IRF|RING) [NONE] RING
  53 + Source region radius for estimating on-counts (degrees) [0.0] 0.2
  54 + Inner ring radius (degrees) [0.6]
  55 + Outer ring radius (degrees) [0.8]
  56 + Output skymap file [skymap.fits]
  57 +
  58 +In the spirit of the classical analysis we have chosen to derive the background
  59 +using the RING method (e.g., Berge et al. 2007 A&A 466 1219B), in which for each
  60 +point in the map (trial source position) the background is derived from a ring
  61 +centered at the same position. The RING method is fast and robust against linear
  62 +gradients of the background rates, but requires a model (from the :ref:`instrument response functions <glossary_irf>`
  63 +) of the background acceptance as a function of position in the camera. You
  64 +need to choose inner/outer radii such that you avoid emission from a source when
  65 +deriving the background. This means that the inner radius must be larger
  66 +than the source's size, or the instrument PSF for a pointlike source. This has
  67 +produced a FITS file ``skymap.fits`` that contains three images of the region
  68 +around the source. The primary image shows the excess counts, i.e., the total
  69 +number of counts minus the estimated background counts. The BACKGROUND image
  70 +shows the number of estimated background counts. Finally, the SIGNIFICANCE image
  71 +shows the significance of the excess, calculated according to Li&Ma 1983 ApJ 272
  72 +317, Eq. 17. We can visualize the resulting map using `ds9 <http://ds9.si.edu>`_.
  73 +
  74 +.. figure:: classic_analysis_skymap.png
  75 + :width: 400px
  76 + :align: center
  77 +
  78 + *Sky map of the significance of a gamma-ray excess around Cas A. The green circle shows a circular region with 0.2 deg radius centered at the source's position.*
  79 +
  80 +You can note that there is a ring with negative significance (i.e.,
  81 +a count deficit) at offsets between 0.6 deg and 0.8 deg from the source. This is
  82 +an artefact due to the fact that when computing the background for a trial
  83 +source in this area the region around Cas A was falling into the ring used for
  84 +the background estimation.
  85 +
  86 +We will recompute the skymap addressing this issue. We will provide an input
  87 +exclusion region to avoid Cas A when calculating the background for any trial
  88 +source position. To this end you can use two exclusion formats: a `ds9 <http://ds9.si.edu>`_ region
  89 +file, or a FITS WCS map. For this example we will use a circular region with
  90 +radius 0.2 dec centered on Cas A. This is what the corresponding ds9 region
  91 +file looks like.
  92 +
  93 +.. code-block:: bash
  94 +
  95 + # Region file format: DS9 version 4.1
  96 + global color=green dashlist=8 3 width=1 font="helvetica 10 normal roman" select=1 highlite=1 dash=0 fixed=0 edit=1 move=1 delete=1 include=1 source=1
  97 + fk5
  98 + circle(350.85,58.815,720.000")
  99 +
  100 +In fact we could have done this from the beginning since Cas A is a know source.
  101 +In general you will need to iterate until you have found all the significant
  102 +gamma-ray emission regions and added them to the exlusion regions/map, which is
  103 +then necessary for spectral extraction.
  104 +
  105 +We rerun :ref:`ctskymap` with the exclusion region.
  106 +
  107 +.. code-block:: bash
  108 +
  109 + $ ctskymap inexclusion=CasA-exclusion.reg
  110 + Input event list or observation definition XML file [obs.xml]
  111 + First coordinate of image center in degrees (RA or galactic l) (0-360) [350.85]
  112 + Second coordinate of image center in degrees (DEC or galactic b) (-90-90) [58.815]
  113 + Projection method (AIT|AZP|CAR|GLS|MER|MOL|SFL|SIN|STG|TAN) [TAN]
  114 + Coordinate system (CEL - celestial, GAL - galactic) (CEL|GAL) [CEL]
  115 + Image scale (in degrees/pixel) [0.02]
  116 + Size of the X axis in pixels [250]
  117 + Size of the Y axis in pixels [250]
  118 + Lower energy limit (TeV) [0.1]
  119 + Upper energy limit (TeV) [50.]
  120 + Background subtraction method (NONE|IRF|RING) [RING]
  121 + Source region radius for estimating on-counts (degrees) [0.2] 0.2
  122 + Inner ring radius (degrees) [0.6]
  123 + Outer ring radius (degrees) [0.8] 0.8
  124 + Output skymap file [skymap.fits] skymap-exclusion.fits
  125 +
  126 +Below you can see the new significance map with the source/exclusion region.
  127 +
  128 +.. figure:: classic_analysis_skymap_exclusion.png
  129 + :width: 400px
  130 + :align: center
  131 +
  132 + *Sky map of the significance of a gamma-ray excess around Cas A. The green circle shows a circular region with 0.2 deg radius centered at the source's position, that is excluded from the background estimation.*
  133 +
  134 +For a classical spectral analysis we need to derive count spectra for the source
  135 +region and for background regions. This is accomplished by the :ref:`csphagen`
  136 +script. This script saves the source (On) and background (Off) count spectra
  137 +in `OGIP format <https://heasarc.gsfc.nasa.gov/docs/heasarc/ofwg/docs/spectra/ogip_92_007/node5.html>`_,
  138 +along with the relevant information from the :ref:`instrument response functions <glossary_irf>`
  139 +refashioned according to this format conventions.
  140 +
  141 +:ref:`csphagen` calculates the background counts using the REFLECTED algorithm
  142 +(e.g., Berge et al. 2007 A&A 466 1219B), in which, for each individual
  143 +observation the background regions have the same shape as the source region, and
  144 +are rotated around the center of the camera keeping the same offset. As many
  145 +reflected regions as possible are used, excluding the area of the camera near
  146 +the source position. Since the background rates are expected to be approximately
  147 +radially symmetric in camera coordinates, this method minimizes the impact of
  148 +the background rate modeling from Monte Carlo. An optional exclusion map (in
  149 +FITS WCS format) can be provided as input through the ``inexclusion`` hidden
  150 +parameter if other regions of significant gamma-ray emission ought to be
  151 +excluded from the background computation.
  152 +
  153 +.. code-block:: bash
  154 +
  155 + $ csphagen
  156 + Input event list or observation definition XML file [obs.xml]
  157 + Binning algorithm (LIN|LOG|FILE) [LOG]
  158 + Lower energy limit (TeV) [0.1]
  159 + Upper energy limit (TeV) [100.0] 50.
  160 + Number of energy bins [120] 30
  161 + Coordinate system (CEL - celestial, GAL - galactic) (CEL|GAL) [CEL]
  162 + Right Ascension of source region centre (deg) (0-360) [83.63] 350.85
  163 + Declination of source region centre (deg) (-90-90) [22.01] 58.815
  164 + Radius of source region circle (deg) (0-180) [0.2]
  165 + Stack multiple observations into single PHA, ARF and RMF files? [no] yes
  166 + Output observation definition XML file [onoff_obs.xml]
  167 +
  168 +This has produced in output several files. An xml observations file lists
  169 +the On/Off observations. Since we have decided to stack multiple observations we
  170 +have only one output On/Off observation. Alternatively, we can keep an On/Off
  171 +observation for each of the original observations. This is what the xml file
  172 +looks like.
  173 +
  174 +.. code-block:: bash
  175 +
  176 + <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  177 + <observation_list title="observation list">
  178 + <observation name="" id="" instrument="CTAOnOff" statistic="cstat">
  179 + <parameter name="Pha_on" file="onoff_stacked_pha_on.fits" />
  180 + <parameter name="Pha_off" file="onoff_stacked_pha_off.fits" />
  181 + <parameter name="Arf" file="onoff_stacked_arf.fits" />
  182 + <parameter name="Rmf" file="onoff_stacked_rmf.fits" />
  183 + </observation>
  184 + </observation_list>
  185 +
  186 +The observation entails four FITS files. ``onoff_stacked_pha_on.fits`` and
  187 +``onoff_stacked_pha_off.fits`` contain the On and Off spectra, respectively.
  188 +These are stored in the SPECTRUM extension of the FITS file, along with ancillary
  189 +information, notably the scaling factor to be applied to the background spectrum,
  190 +BACKSCAL. The third extension, EBOUNDS, contains the boundaries of the energy
  191 +bins, as defined by the binning parameters in input to csphagen. The file
  192 +``onoff_stacked_arf.fits`` contains the spectral response of the instrument
  193 +extracted fromt the :ref:`instrument response functions <glossary_irf>`,
  194 +including effective area for gamma-ray detection and background rates, in the
  195 +SPECRESP extension. The file ``onoff_stacked_rmf.fits`` contains the remaining
  196 +part of the instrument response, i.e., an energy redistribution matrix (MATRIX),
  197 +as well as another instance of the EBOUNDS table. Note that we are performing a
  198 +1D analysis: the effect of the PSF is already folded into the effective area
  199 +computation.
  200 +
  201 +.. note::
  202 +
  203 + The parameters specified control the energy binning of the count spectra in
  204 + *reconstructed* energy. For the computation of the instrument response we
  205 + need a fine binning in *true* energy, which is controlled by the hidden
  206 + parameters ``etruemin``, ``etruemax``, and ``etruebins``.
  207 +
  208 +.. note::
  209 +
  210 + The first part of the FITS files names (and a full path to the desired
  211 + location) can be set using the hidden ``prefix`` parameter of
  212 + :ref:`csphagen`. If you decide not to stack multiple observations the string
  213 + ``stacked`` with be replaced in the file names by the observation id for
  214 + each of the original observations.
  215 +
  216 +There are also come ancillary `ds9 <http://ds9.si.edu>`_ region files, that show
  217 +the On region and the Off regions for each observation, ``onoff_on.reg`` and
  218 +``onoff_xxx_off.reg`` (with xxx observation id), respectively. Below there is
  219 +a skymap where you can see the pointing directions along with the position of
  220 +the On and Off regions for a few observations (extracted using the
  221 +:ref:`csobsinfo` script).
  222 +
  223 +.. figure:: classic_analysis_skymap_onoff.png
  224 + :width: 400px
  225 + :align: center
  226 +
  227 + *Sky map of the event counts in a larger region around Cas A (not background subtracted). The green crosses show the pointing directions, the magenta circles the Off regions, and the white circle the On region.*
  228 +
  229 +.. note::
  230 +
  231 + If you wish to limit the number of observations considered to those pointed
  232 + closer to the source, you can do this either at the observation selection
  233 + level (see :ref:`csobsselect`), or directly in :ref:`csphagen` via the
  234 + hidden ``maxoffset`` parameter.
  235 +
  236 +Next we need a model to be fit to the observations. It must contain a model for the
  237 +source, and, optionally, a model for the background. Here is what such a
  238 +model looks like.
  239 +
  240 +.. code-block:: bash
  241 +
  242 + <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  243 + <source_library title="source library">
  244 + <source name="Cassiopeia A" type="PointSource">
  245 + <spectrum type="PowerLaw">
  246 + <parameter name="Prefactor" value="144.9999950838" error="0" scale="1e-20" min="0" free="1" />
  247 + <parameter name="Index" value="2.75" error="-0" scale="-1" min="-10" max="10" free="1" />
  248 + <parameter name="PivotEnergy" value="1" scale="1000000" free="0" />
  249 + </spectrum>
  250 + <spatialModel type="PointSource">
  251 + <parameter name="RA" value="350.850006001541" error="0" scale="1" free="0" />
  252 + <parameter name="DEC" value="58.8150032295771" error="0" scale="1" free="0" />
  253 + </spatialModel>
  254 + </source>
  255 + <source name="Background model" type="CTAIrfBackground" instrument="CTAOnOff">
  256 + <spectrum type="PowerLaw">
  257 + <parameter name="Prefactor" value="1" error="0" scale="1" min="0.001" max="1000" free="1" />
  258 + <parameter name="Index" value="0" error="0" scale="1" min="-5" max="5" free="1" />
  259 + <parameter name="Scale" value="1" scale="1000000" min="0.01" max="1000" free="0" />
  260 + </spectrum>
  261 + </source>
  262 +
  263 +We can now fit the model to the data using :ref:`ctlike`.
  264 +
  265 +.. code-block:: bash
  266 +
  267 + $ ctlike
  268 + Input event list, counts cube or observation definition XML file [events.fits] onoff_obs.xml
  269 + Input model definition XML file [$CTOOLS/share/models/crab.xml] model.xml
  270 + Output model definition XML file [crab_results.xml] CasA_results.xml
  271 +
  272 +The output file ``CasA_results.xml`` contains the best fit parameter values.
  273 +
  274 +.. code-block:: bash
  275 +
  276 + <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  277 + <source_library title="source library">
  278 + <source name="Cassiopeia A" type="PointSource">
  279 + <spectrum type="PowerLaw">
  280 + <parameter name="Prefactor" value="145.578961369814" error="4.93849616822191" scale="1e-20" min="0" free="1" />
  281 + <parameter name="Index" value="2.73210299568622" error="0.0232463328680207" scale="-1" min="-10" max="10" free="1" />
  282 + <parameter name="PivotEnergy" value="1" scale="1000000" free="0" />
  283 + </spectrum>
  284 + <spatialModel type="PointSource">
  285 + <parameter name="RA" value="350.850006001541" scale="1" free="0" />
  286 + <parameter name="DEC" value="58.8150032295771" scale="1" free="0" />
  287 + </spatialModel>
  288 + </source>
  289 + <source name="Background model" type="CTAIrfBackground" instrument="CTAOnOff">
  290 + <spectrum type="PowerLaw">
  291 + <parameter name="Prefactor" value="1.01524362954036" error="0.0084255502742701" scale="1" min="0.001" max="1000" free="1" />
  292 + <parameter name="Index" value="0.00464866003911358" error="0.00484378659736716" scale="1" min="-5" max="5" free="1" />
  293 + <parameter name="Scale" value="1" scale="1000000" min="0.01" max="1000" free="0" />
  294 + </spectrum>
  295 + </source>
  296 + </source_library>
  297 +
  298 +.. note::
  299 +
  300 + Check the :ref:`ctlike` log file (by default ``ctlike.log``) to learn
  301 + about the fit convergence and investigate any issues.
  302 +
  303 +:ref:`ctlike` has a hidden parameter called ``statistic`` that sets the
  304 +statistic used for the fit.
  305 +
  306 +- The DEFAULT for OnOff osbervations is CSTAT, i.e., Poisson signal and Poisson
  307 + background. A spectral model for the signal and a spectral model for the
  308 + background are jointly fit to the On and Off spectra.
  309 +- WSTAT is a special case of CSTAT, Poisson signal with Poisson background, in
  310 + which you do not need to have a spectral model for the background and
  311 + free parameters associated with it. The number of background counts in each
  312 + energy bin is treated as a nuisance parameter, derived from the On and Off
  313 + counts by profiling the likelihood function. In this case the only assumption
  314 + is that the background rate spectrum is the same in the On and Off regions.
  315 +
  316 +.. warning::
  317 + Beware that the profiling may yield unphysical results (negative background
  318 + counts) if the number of events in the Off spectra are zero. In this case a
  319 + null number of expected background events must be enforced,
  320 + which can result in a bias on the source's parameters. You can address this
  321 + issue by stacking multiple observations, using a coarser energy binning, or
  322 + using CSTAT instead (if you have a spectral model for the background that is
  323 + good enough). See the `XSPEC manual Appendix B <https://heasarc.gsfc.nasa.gov/xanadu/xspec/manual/XSappendixStatistics.html>`_
  324 + for more information.
  325 +
  326 +- You can also use CHI2, a classical chi square, i.e., a Gaussian signal and
  327 + Gaussian background. As for CSTAT, a spectral model for the signal and a
  328 + spectral model for the background are jointly fit to the On and Off spectra.
  329 +
  330 +The :ref:`ctbutterfly` tool and :ref:`csspec` script can now be used to extract
  331 +the best-fit source spectrum.
  332 +
  333 +.. code-block:: bash
  334 +
  335 + $ ctbutterfly
  336 + Input event list, counts cube or observation definition XML file [events.fits] onoff_obs.xml
  337 + Source of interest [Crab] Cassiopeia A
  338 + Input model definition XML file [$CTOOLS/share/models/crab.xml] CasA_results.xml
  339 + Lower energy limit (TeV) [0.1]
  340 + Upper energy limit (TeV) [100.0] 50.
  341 + Output ASCII file [butterfly.txt]
  342 +
  343 +.. code-block:: bash
  344 +
  345 + $ csspec
  346 + Input event list, counts cube, or observation definition XML file [events.fits] onoff_obs.xml
  347 + Input model definition XML file [$CTOOLS/share/models/crab.xml] CasA_results.xml
  348 + Source name [Crab] Cassiopeia A
  349 + Spectrum generation method (SLICE|NODES|AUTO) [AUTO]
  350 + Binning algorithm (LIN|LOG|FILE) [LOG]
  351 + Lower energy limit (TeV) [0.1]
  352 + Upper energy limit (TeV) [100.0] 50.
  353 + Number of energy bins [20] 30
  354 + Output spectrum file [spectrum.fits] CasA-spectrum.fits
  355 +
  356 +The plot below displays the derived spectrum (using the python module
  357 +matplotlib).
  358 +
  359 +.. figure:: classic_analysis_SED.png
  360 + :width: 400px
  361 + :align: center
  362 +
  363 + *Spectral Energy Distribution of the source: the best-fit function over the whole energy range and its uncertainty range, along with the spectral points in energy bins.*
0 364 \ No newline at end of file
... ...
doc/source/users/tutorials/1dc/first.rst
... ... @@ -30,3 +30,4 @@ We also assume that you have read the section about
30 30 first_fitting
31 31 first_improving
32 32 first_unbinned
  33 + classical_analysis
... ...