Commit ad0d426286f0d14000d8e8c5c8cd1e52d21f0d6c

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

Add draft of classical analysis H.E.S.S. tutorial

doc/source/users/tutorials/hess_dr1/classical.rst 0 → 100644
  1 +.. _hess_dr1_classical:
  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 On/Off analysis of the
  9 + data**.
  10 +
  11 +
  12 +.. code-block:: bash
  13 +
  14 + $ csphagen use_irf_bkg=no
  15 + Input event list or observation definition XML file [obs.xml] obs_crab_selected.xml
  16 + Input model definition XML file (if NONE, use point source) [NONE] crab_models.xml
  17 + Source name [Crab]
  18 + Algorithm for defining energy bins (FILE|LIN|LOG) [LOG]
  19 + Start value for first energy bin in TeV [0.1] 0.66
  20 + Stop value for last energy bin in TeV [100.0]
  21 + Number of energy bins [120] 40
  22 + Stack multiple observations into single PHA, ARF and RMF files? [no]
  23 + Output observation definition XML file [onoff_obs.xml] onoff_obs_crab.xml
  24 + Method for background estimation (REFLECTED|CUSTOM) [REFLECTED]
  25 + Coordinate system (CEL - celestial, GAL - galactic) (CEL|GAL) [CEL]
  26 + Right Ascension of source region centre (deg) (0-360) [83.63]
  27 + Declination of source region centre (deg) (-90-90) [22.01]
  28 + Radius of source region circle (deg) (0-180) [0.2]
  29 +
  30 +.. code-block:: xml
  31 +
  32 + <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  33 + <source_library title="source library">
  34 + <source name="Crab" type="PointSource">
  35 + <spectrum type="PowerLaw">
  36 + <parameter name="Prefactor" value="1" scale="5.7e-18" min="0" free="1" />
  37 + <parameter name="Index" value="1" scale="-2.48" min="-4" max="4" free="1" />
  38 + <parameter name="PivotEnergy" value="1" scale="1000000" free="0" />
  39 + </spectrum>
  40 + <spatialModel type="PointSource">
  41 + <parameter name="RA" value="83.633" scale="1" free="0" />
  42 + <parameter name="DEC" value="22.015" scale="1" free="0" />
  43 + </spatialModel>
  44 + </source>
  45 + </source_library>
  46 +
  47 +.. code-block:: bash
  48 +
  49 + $ ctlike statistic=wstat
  50 + Input event list, counts cube or observation definition XML file [events.fits] onoff_obs_crab.xml
  51 + Input model definition XML file [$CTOOLS/share/models/crab.xml] crab_models_onoff.xml
  52 + Output model definition XML file [crab_results.xml]
  53 +
  54 +.. code-block:: none
  55 +
  56 + 2018-09-13T11:49:06: +=========================================+
  57 + 2018-09-13T11:49:06: | Maximum likelihood optimisation results |
  58 + 2018-09-13T11:49:06: +=========================================+
  59 + 2018-09-13T11:49:06: === GOptimizerLM ===
  60 + 2018-09-13T11:49:06: Optimized function value ..: 60.048
  61 + 2018-09-13T11:49:06: Absolute precision ........: 0.005
  62 + 2018-09-13T11:49:06: Acceptable value decrease .: 2
  63 + 2018-09-13T11:49:06: Optimization status .......: converged
  64 + 2018-09-13T11:49:06: Number of parameters ......: 6
  65 + 2018-09-13T11:49:06: Number of free parameters .: 2
  66 + 2018-09-13T11:49:06: Number of iterations ......: 5
  67 + 2018-09-13T11:49:06: Lambda ....................: 1e-08
  68 + 2018-09-13T11:49:06: Maximum log likelihood ....: -60.048
  69 + 2018-09-13T11:49:06: Observed events (Nobs) ...: 605.000
  70 + 2018-09-13T11:49:06: Predicted events (Npred) ..: 604.191 (Nobs - Npred = 0.808986318662733)
  71 + 2018-09-13T11:49:06: === GModels ===
  72 + 2018-09-13T11:49:06: Number of models ..........: 1
  73 + 2018-09-13T11:49:06: Number of parameters ......: 6
  74 + 2018-09-13T11:49:06: === GModelSky ===
  75 + 2018-09-13T11:49:06: Name ......................: Crab
  76 + 2018-09-13T11:49:06: Instruments ...............: all
  77 + 2018-09-13T11:49:06: Instrument scale factors ..: unity
  78 + 2018-09-13T11:49:06: Observation identifiers ...: all
  79 + 2018-09-13T11:49:06: Model type ................: PointSource
  80 + 2018-09-13T11:49:06: Model components ..........: "PointSource" * "PowerLaw" * "Constant"
  81 + 2018-09-13T11:49:06: Number of parameters ......: 6
  82 + 2018-09-13T11:49:06: Number of spatial par's ...: 2
  83 + 2018-09-13T11:49:06: RA .......................: 83.633 deg (fixed,scale=1)
  84 + 2018-09-13T11:49:06: DEC ......................: 22.015 deg (fixed,scale=1)
  85 + 2018-09-13T11:49:06: Number of spectral par's ..: 3
  86 + 2018-09-13T11:49:06: Prefactor ................: 4.77687464316323e-17 +/- 3.12433509735449e-18 [0,infty[ ph/cm2/s/MeV (free,scale=5.7e-18,gradient)
  87 + 2018-09-13T11:49:06: Index ....................: -2.60709757251519 +/- 0.0767624316833204 [9.92,-9.92] (free,scale=-2.48,gradient)
  88 + 2018-09-13T11:49:06: PivotEnergy ..............: 1000000 MeV (fixed,scale=1000000,gradient)
  89 + 2018-09-13T11:49:06: Number of temporal par's ..: 1
  90 + 2018-09-13T11:49:06: Normalization ............: 1 (relative value) (fixed,scale=1,gradient)
  91 +
  92 +The intensity at 1 TeV of the Crab was fitted to
  93 +:math:`(4.78 \pm 0.31) \times 10^{-11}\,{\rm photons}\,{\rm cm}^{-2}\,{\rm s}^{-1}\,{\rm TeV}^{-1}`
  94 +the spectral index of the power law is
  95 +:math:`-2.61 \pm 0.08`.
  96 +This can be compared to the values
  97 +:math:`(4.64 \pm 0.26) \times 10^{-11}\,{\rm photons}\,{\rm cm}^{-2}\,{\rm s}^{-1}\,{\rm TeV}^{-1}`
  98 +and
  99 +:math:`-2.61 \pm 0.07`
  100 +that were obtained before with the unbinned maximum likelihood fit using the
  101 +energy dispersion.
  102 +
  103 +.. code-block:: bash
  104 +
  105 + $ csresspec components=yes
  106 + Input event list, counts cube, or observation definition XML file [events.fits] onoff_obs_crab.xml
  107 + Stack observations? [no] yes
  108 + Input model definition XML file [$CTOOLS/share/models/crab.xml] crab_results_onoff.xml
  109 + Residuals computation algorithm (SUB|SUBDIV|SUBDIVSQRT|SIGNIFICANCE) [SIGNIFICANCE]
  110 + Output residual spectrum file [resspec.fits] resspec_onoff.fits
  111 +
  112 +The script will produce the FITS file ``resspec.fits`` that contains the
  113 +spectral residuals for each of the model components.
  114 +To display the residuals you can use the ``show_residuals.py`` script in the
  115 +example folder as follows:
  116 +
  117 +.. code-block:: bash
  118 +
  119 + $CTOOLS/share/examples/python/show_residuals.py resspec.fits
  120 +
  121 +The figure below shows the resulting residual spectrum.
  122 +Although the model provides a reasonable first order approximation of the
  123 +spectral distribution of the data, there are still significant spectral
  124 +residuals that cannot be explained by the model.
  125 +
  126 +.. figure:: classical_resspec.png
  127 + :width: 600px
  128 + :align: center
  129 +
  130 + *Residual counts spectrum after model fitting*
doc/source/users/tutorials/hess_dr1/classical_resspec.png 0 → 100644

19.9 KB

doc/source/users/tutorials/hess_dr1/index.rst
@@ -10,3 +10,4 @@ Analysing public H.E.S.S. data @@ -10,3 +10,4 @@ Analysing public H.E.S.S. data
10 select_events 10 select_events
11 skymap 11 skymap
12 fitting 12 fitting
  13 + classical
examples/show_resmap.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# Shows histogram of residual map
  4 +#
  5 +# Copyright (C) 2018 Juergen Knoedlseder
  6 +#
  7 +# This program is free software: you can redistribute it and/or modify
  8 +# it under the terms of the GNU General Public License as published by
  9 +# the Free Software Foundation, either version 3 of the License, or
  10 +# (at your option) any later version.
  11 +#
  12 +# This program is distributed in the hope that it will be useful,
  13 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15 +# GNU General Public License for more details.
  16 +#
  17 +# You should have received a copy of the GNU General Public License
  18 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  19 +#
  20 +# ==========================================================================
  21 +import sys
  22 +import gammalib
  23 +import cscripts
  24 +try:
  25 + import matplotlib.pyplot as plt
  26 + import matplotlib.mlab as mlab
  27 + plt.figure()
  28 + plt.close()
  29 +except (ImportError, RuntimeError):
  30 + print('This script needs the "matplotlib" module')
  31 + sys.exit()
  32 +try:
  33 + import numpy as np
  34 +except ImportError:
  35 + print('This script needs the "numpy" module')
  36 + sys.exit()
  37 +
  38 +
  39 +# ================= #
  40 +# Plot residual map #
  41 +# ================= #
  42 +def plot_resmap(filename, nbins, plotfile):
  43 + """
  44 + Plot pull histogram
  45 +
  46 + Parameters
  47 + ----------
  48 + filename : str
  49 + Residual map filename
  50 + nbins : int
  51 + Number of hisogram bins
  52 + plotfile : str
  53 + Plot filename
  54 + """
  55 + # Open residual map
  56 + map = gammalib.GSkyMap(filename)
  57 +
  58 + # Create array of values
  59 + values = map.array()
  60 +
  61 + # Create histogram
  62 + _, bins, _ = plt.hist(values, nbins, range=[-4.0,4.0],
  63 + normed=True, facecolor='green')
  64 +
  65 + # Create expected distribution
  66 + y = mlab.normpdf(bins, 0.0, 1.0)
  67 + plt.plot(bins, y, 'r-', linewidth=2)
  68 +
  69 + # Set plot
  70 + plt.xlabel('Pull')
  71 + plt.ylabel('Arbitrary units')
  72 + #plt.title(parname)
  73 + plt.grid(True)
  74 +
  75 + # Show figure
  76 + if len(plotfile) > 0:
  77 + plt.savefig(plotfile)
  78 + else:
  79 + plt.show()
  80 +
  81 + # Return
  82 + return
  83 +
  84 +
  85 +# ================= #
  86 +# Show residual map #
  87 +# ================= #
  88 +def show_resmap():
  89 + """
  90 + Show residual map
  91 + """
  92 + # Set usage string
  93 + usage = 'show_resmap.py [-n bins] [-p plotfile] file'
  94 +
  95 + # Set default options
  96 + options = [{'option': '-n', 'value': '50'},
  97 + {'option': '-p', 'value': ''}]
  98 +
  99 + # Get arguments and options from command line arguments
  100 + args, options = cscripts.ioutils.get_args_options(options, usage)
  101 +
  102 + # Extract script parameters from options
  103 + nbins = int(options[0]['value'])
  104 + plotfile = options[1]['value']
  105 +
  106 + # Plot residual map
  107 + plot_resmap(args[0], nbins, plotfile)
  108 +
  109 + # Return
  110 + return
  111 +
  112 +
  113 +# ======================== #
  114 +# Main routine entry point #
  115 +# ======================== #
  116 +if __name__ == '__main__':
  117 +
  118 + # Show residual map
  119 + show_resmap()