Commit aa443cc0124bc21707d6c897fb281f7a643cb6ed

Authored by Jürgen Knödlseder
1 parent ed0d4b4b

Add COMPTEL DRI tutorial (#2187)

doc/source/users/tutorials/howto/comptel/howto_comptel_1.8MeV_tsmap.png 0 → 100644

87.6 KB

doc/source/users/tutorials/howto/comptel/howto_comptel_1.8MeV_tsmap_5vp.png 0 → 100644

59.1 KB

doc/source/users/tutorials/howto/comptel/howto_comptel_dri.rst 0 → 100644
  1 +.. _howto_comptel_dri:
  2 +
  3 +Generating your own event selection
  4 +-----------------------------------
  5 +
  6 + .. admonition:: What you will learn
  7 +
  8 + You will learn how to use the GammaLib classes to generate your own
  9 + data cubes and response functions for a COMPTEL analysis.
  10 +
  11 +The data in the
  12 +`HEASARC archive <https://heasarc.gsfc.nasa.gov/xamin/>`_
  13 +comprises counts cubes for the standard COMPTEL energy ranges (0.75-1 MeV,
  14 +1-3 MeV, 3-10 MeV and 10-30 MeV) per viewing period. In case that you want
  15 +to analyse a different energy range, or that you want to perform a different
  16 +event selection, you need to create ``DRE``, ``DRG``, ``DRB`` and ``DRX``
  17 +data and the corresponding ``IAQ`` response function yourself. You can do
  18 +this with GammaLib.
  19 +
  20 +Let's assume you want to make a 1.8 MeV line map of the Galactic centre.
  21 +You can use for this the data of the COMPTEL viewing period 16 in phase 1 of
  22 +the mission. Go to the
  23 +`HEASARC archive <https://heasarc.gsfc.nasa.gov/xamin/>`_
  24 +and download the ``EVP``, ``TIM`` and ``OAD`` datasets. Once this is done,
  25 +create the following
  26 +:ref:`observation definition file <glossary_obsdef>`:
  27 +
  28 +.. code-block:: xml
  29 +
  30 + <?xml version="1.0" standalone="no"?>
  31 + <observation_list title="observation library">
  32 + <observation name="GC" id="000160" instrument="COM">
  33 + <parameter name="EVP" file="$COMDATA/phase01/vp0016_0/m28846_evp.fits"/>
  34 + <parameter name="TIM" file="$COMDATA/phase01/vp0016_0/m11765_tim.fits"/>
  35 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20745_oad.fits"/>
  36 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20750_oad.fits"/>
  37 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20753_oad.fits"/>
  38 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20759_oad.fits"/>
  39 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20760_oad.fits"/>
  40 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20761_oad.fits"/>
  41 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20762_oad.fits"/>
  42 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20764_oad.fits"/>
  43 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20770_oad.fits"/>
  44 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20771_oad.fits"/>
  45 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20772_oad.fits"/>
  46 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20785_oad.fits"/>
  47 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20786_oad.fits"/>
  48 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20787_oad.fits"/>
  49 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20788_oad.fits"/>
  50 + <parameter name="OAD" file="$COMDATA/phase01/vp0016_0/m20789_oad.fits"/>
  51 + </observation>
  52 + </observation_list>
  53 +..
  54 +
  55 + .. note::
  56 +
  57 + There exist two formats for the
  58 + :ref:`observation definition file <glossary_obsdef>`
  59 + for COMPTEL data, one that describes the locations of the COMPTEL
  60 + data space cubes and response functions, and one that desribes
  61 + the location of an ``EVP`` event list, a ``TIM`` Good Time
  62 + Interval file, and an arbitrary number of ``OAD`` Orbit Aspect
  63 + Data files. In general there exists one ``OAD`` per day, while
  64 + there is one ``EVP`` event file and one ``TIM`` Good Time Interval
  65 + file per viewing period.
  66 +
  67 + In the example above, the environment variable ``$COMDATA`` was set
  68 + to the root directory of the COMPTEL archive.
  69 +
  70 +Now you are ready to generate the ``DRE``, ``DRG``, ``DRB``, ``DRX`` and
  71 +``IAQ`` datasets that you need for a ctools analysis of COMPTEL data.
  72 +This will take a few minutes. Start
  73 +`Python <https://www.python.org>`_
  74 +in your terminal and type the following:
  75 +
  76 +.. code-block:: python
  77 +
  78 + # You need GammaLib, of course ;)
  79 + import gammalib
  80 +
  81 + # Create sky map for the definition of the DRE and DRG data cubes
  82 + cube = gammalib.GSkyMap('TAN', 'GAL', 0.0, 20.0, 1.0, 1.0, 80, 80)
  83 +
  84 + # Create sky map for the definition of the DRX exposure map
  85 + expo = gammalib.GSkyMap('CAR', 'GAL', 0.0, 0.0, 1.0, 1.0, 360, 180)
  86 +
  87 + # Load observation definition XML file 'obs.xml' (the file you created above)
  88 + obsdef = gammalib.GObservations('obs.xml')
  89 +
  90 + # Extract COMPTEL observation from that file (it's the first and unique observation)
  91 + obs = obsdef[0]
  92 +
  93 + # Allocate DRE, DRG and DRX. DRE and DRG have a 3rd Phibar dimension, starting at 0.0 deg,
  94 + # with a binsize of 2.0 deg, and 25 Phibar layers.
  95 + dre = gammalib.GCOMDri(cube, 0.0, 2.0, 25)
  96 + drg = gammalib.GCOMDri(cube, 0.0, 2.0, 25)
  97 + drx = gammalib.GCOMDri(expo)
  98 +
  99 + # Set DRE energy range to 1.7-1.9 MeV, DRG and DRX are energy independent
  100 + dre.ebounds(gammalib.GEbounds(gammalib.GEnergy(1.7, 'MeV'),
  101 + gammalib.GEnergy(1.9, 'MeV')))
  102 +
  103 + # Compute DRE, DRG and DRX (here is where the real magic happens)
  104 + dre.compute_dre(obs.events(), obs.oads(), obs.tim())
  105 + drg.compute_drg(obs.oads(), obs.tim())
  106 + drx.compute_drx(obs.oads(), obs.tim())
  107 +
  108 + # Save DRE, DRG and DRX ('True' indicates to overwrite any existing file)
  109 + dre.save('dre.fits', True)
  110 + drg.save('drg.fits', True)
  111 + drx.save('drx.fits', True)
  112 +
  113 + # Load DRE and DRG to generate a DRB background model cube
  114 + dre = gammalib.GCOMDri('dre.fits')
  115 + drb = gammalib.GCOMDri('drg.fits')
  116 +
  117 + # Normalise DRB on the Phibar distribution of the DRE cube
  118 + npix = dre.nchi() * dre.npsi()
  119 + for k in range(dre.nphibar()):
  120 + sum_dre = 0.0
  121 + sum_drb = 0.0
  122 + for i in range(npix):
  123 + index = i + k * npix
  124 + sum_dre += dre[index]
  125 + sum_drb += drb[index]
  126 + if sum_drb > 0:
  127 + for i in range(npix):
  128 + index = i + k * npix
  129 + drb[index] *= sum_dre / sum_drb
  130 +
  131 + # Save DRB
  132 + drb.save('drb.fits', True)
  133 +
  134 + # Initialise IAQ that will hold the response function
  135 + iaq = gammalib.GCOMIaq(55.0, 1.0, 50.0, 2.0)
  136 +
  137 + # Compute IAQ for a line energy of 1.809 MeV and an energy band of 1.7-1.9 MeV
  138 + iaq.set(gammalib.GEnergy(1.809, 'MeV'),
  139 + gammalib.GEbounds(gammalib.GEnergy(1.7, 'MeV'),
  140 + gammalib.GEnergy(1.9, 'MeV')))
  141 +
  142 + # Save IAQ and you are done
  143 + iaq.save('iaq.fits', True)
  144 +
  145 +Now you have everything at hand to perform a COMPTEL maximum likelihood
  146 +analysis. For that purpoe you need to gather all the datasets that you
  147 +just created in a new
  148 +:ref:`observation definition file <glossary_obsdef>`
  149 +that should look as follows:
  150 +
  151 +.. code-block:: xml
  152 +
  153 + <?xml version="1.0" standalone="no"?>
  154 + <observation_list title="observation library">
  155 + <observation name="GC" id="000160" instrument="COM">
  156 + <parameter name="DRE" file="dre.fits"/>
  157 + <parameter name="DRB" file="drb.fits"/>
  158 + <parameter name="DRG" file="drg.fits"/>
  159 + <parameter name="DRX" file="drx.fits"/>
  160 + <parameter name="IAQ" value="iaq.fits"/>
  161 + </observation>
  162 + </observation_list>
  163 +..
  164 +
  165 + .. warning::
  166 +
  167 + Be aware that the attribute for the ``IAQ`` parameter is ``value`` and
  168 + not ``file`` since the ``IAQ`` parameter is not necessarily a file
  169 + but can be also a response name of the calibration database.
  170 +
  171 +Before doing a model fit you need a model. Create the following
  172 +:ref:`model definition file <glossary_moddef>`:
  173 +
  174 +.. code-block:: xml
  175 +
  176 + <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  177 + <source_library title="source library">
  178 + <source name="GC" type="PointSource" tscalc="1">
  179 + <spectrum type="Constant">
  180 + <parameter name="Normalization" scale="1.0e-5" value="1.0" min="-100.0" max="100.0" free="1"/>
  181 + </spectrum>
  182 + <spatialModel type="PointSource">
  183 + <parameter name="RA" scale="1.0" value="266.40" min="-360" max="360" free="0"/>
  184 + <parameter name="DEC" scale="1.0" value="-28.94" min="-90" max="90" free="0"/>
  185 + </spatialModel>
  186 + </source>
  187 + <source name="Background" type="DRBFitting" instrument="COM">
  188 + <node>
  189 + <parameter name="Phibar" value="9" scale="1" min="0" max="50" free="0" />
  190 + <parameter name="Normalization" value="1.0" scale="1" min="0" max="1000" free="1" />
  191 + </node>
  192 + <node>
  193 + <parameter name="Phibar" value="11" scale="1" min="0" max="50" free="0" />
  194 + <parameter name="Normalization" value="1.0" scale="1" min="0" max="1000" free="1" />
  195 + </node>
  196 + ...
  197 + <node>
  198 + <parameter name="Phibar" value="49" scale="1" min="0" max="50" free="0" />
  199 + <parameter name="Normalization" value="1.0" scale="1" min="0" max="1000" free="1" />
  200 + </node>
  201 + </source>
  202 + </source_library>
  203 +
  204 +This file contains a single point source at the position of the Galactic
  205 +Centre. The spectral model is a simple constant normalisation that will
  206 +return the gamma-ray line flux in units of
  207 +:math:`{\rm photons}\,{\rm cm}^{-2}\,{\rm s}^{-1}`.
  208 +For the background we do a Phibar fitting of the ``DRB`` cube. Since the
  209 +first four layers of the ``DRE`` cube are empty we start the nodes at the
  210 +fifth layer which corresponds to a Phibar value of 9 degrees. There are
  211 +subsequent nodes spaced by 2 degrees (not shown) up to a Phibar value of
  212 +49 degrees.
  213 +
  214 +Now it's time for model fitting. You can produce for example a Test Statistic
  215 +map of the region around the Galactic centre as follows:
  216 +
  217 +.. code-block:: bash
  218 +
  219 + $ cttsmap
  220 + Input event list, counts cube or observation definition XML file [events.fits] obs_dri.xml
  221 + Test source name [Crab] GC
  222 + Input model definition XML file [$CTOOLS/share/models/crab.xml] models.xml
  223 + First coordinate of image center in degrees (RA or galactic l) (0-360) [83.63] 0.0
  224 + Second coordinate of image center in degrees (DEC or galactic b) (-90-90) [22.01] 0.0
  225 + Projection method (AIT|AZP|CAR|GLS|MER|MOL|SFL|SIN|STG|TAN) [CAR]
  226 + Coordinate system (CEL - celestial, GAL - galactic) (CEL|GAL) [CEL] GAL
  227 + Image scale (in degrees/pixel) [0.02] 1.0
  228 + Size of the X axis in pixels [200] 50
  229 + Size of the Y axis in pixels [200] 30
  230 + Output Test Statistic map file [tsmap.fits] tsmap.fits
  231 +
  232 +Below is the resulting Test Statistic map that shows 1.8 MeV emission
  233 +following the Galactic plane and peaking near the Galactic centre.
  234 +
  235 +.. figure:: howto_comptel_1.8MeV_tsmap.png
  236 + :width: 500px
  237 + :align: center
  238 +
  239 + *1.8 MeV Test Statistic map for COMPTEL viewing period 16 of the Galactic Centre*
  240 +
  241 +It is left to you as an exercise to extend this example. To get a better
  242 +statistics you may for example combine observations. For that purpose you
  243 +can add the ``DRI`` datasets of multiple observations into single ``DRE``,
  244 +``DRG``, ``DRB`` and ``DRX`` datasets,
  245 +that's the way how the COMPTEL analysis was done. You can however also leave
  246 +the ``DRI`` datasets separate and combine their description in the
  247 +:ref:`observation definition file <glossary_obsdef>`.
  248 +To combine for example the data of viewing periods 5, 7.5, 13, 16 and 27
  249 +the
  250 +:ref:`observation definition file <glossary_obsdef>`
  251 +should look like this:
  252 +
  253 +.. code-block:: xml
  254 +
  255 + <?xml version="1.0" standalone="no"?>
  256 + <observation_list title="observation library">
  257 + <observation name="GC" id="000050" instrument="COM">
  258 + <parameter name="DRE" file="dre_0050.fits"/>
  259 + <parameter name="DRB" file="drb_0050.fits"/>
  260 + <parameter name="DRG" file="drg_0050.fits"/>
  261 + <parameter name="DRX" file="drx_0050.fits"/>
  262 + <parameter name="IAQ" value="iaq.fits"/>
  263 + </observation>
  264 + <observation name="GC" id="000075" instrument="COM">
  265 + <parameter name="DRE" file="dre_0075.fits"/>
  266 + <parameter name="DRB" file="drb_0075.fits"/>
  267 + <parameter name="DRG" file="drg_0075.fits"/>
  268 + <parameter name="DRX" file="drx_0075.fits"/>
  269 + <parameter name="IAQ" value="iaq.fits"/>
  270 + </observation>
  271 + <observation name="GC" id="000130" instrument="COM">
  272 + <parameter name="DRE" file="dre_0130.fits"/>
  273 + <parameter name="DRB" file="drb_0130.fits"/>
  274 + <parameter name="DRG" file="drg_0130.fits"/>
  275 + <parameter name="DRX" file="drx_0130.fits"/>
  276 + <parameter name="IAQ" value="iaq.fits"/>
  277 + </observation>
  278 + <observation name="GC" id="000160" instrument="COM">
  279 + <parameter name="DRE" file="dre_0160.fits"/>
  280 + <parameter name="DRB" file="drb_0160.fits"/>
  281 + <parameter name="DRG" file="drg_0160.fits"/>
  282 + <parameter name="DRX" file="drx_0160.fits"/>
  283 + <parameter name="IAQ" value="iaq.fits"/>
  284 + </observation>
  285 + <observation name="GC" id="000270" instrument="COM">
  286 + <parameter name="DRE" file="dre_0270.fits"/>
  287 + <parameter name="DRB" file="drb_0270.fits"/>
  288 + <parameter name="DRG" file="drg_0270.fits"/>
  289 + <parameter name="DRX" file="drx_0270.fits"/>
  290 + <parameter name="IAQ" value="iaq.fits"/>
  291 + </observation>
  292 + </observation_list>
  293 +
  294 +The corresponding Test Statistic map is shown below.
  295 +
  296 +.. figure:: howto_comptel_1.8MeV_tsmap_5vp.png
  297 + :width: 500px
  298 + :align: center
  299 +
  300 + *1.8 MeV Test Statistic map for COMPTEL viewing periods 5, 7.5, 13, 16 and 27 of the Galactic Centre*
doc/source/users/tutorials/howto/comptel/index.rst
@@ -22,3 +22,4 @@ In this tutorial you will learn how to do that with a simple analysis. @@ -22,3 +22,4 @@ In this tutorial you will learn how to do that with a simple analysis.
22 howto_comptel_spectrum 22 howto_comptel_spectrum
23 howto_comptel_tsmap 23 howto_comptel_tsmap
24 howto_comptel_ulimit 24 howto_comptel_ulimit
  25 + howto_comptel_dri