Commit 2ed89b08b75266829f1538b58c21f27d2ce78381

Authored by Jürgen Knödlseder
1 parent 756605e6

Add simulation and stacked in-memory analysis pipeline.

ChangeLog
1 -2014-10-26 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu> 1 +2014-10-30 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu>
2 2
3 * Version 0.8.0 released 3 * Version 0.8.0 released
4 ======================== 4 ========================
@@ -2,7 +2,7 @@ New Features and Important Changes in ctools 00-08-00 @@ -2,7 +2,7 @@ New Features and Important Changes in ctools 00-08-00
2 2
3 Author(s) : Juergen Knoedlseder 3 Author(s) : Juergen Knoedlseder
4 4
5 -26 October 2014 5 +30 October 2014
6 6
7 7
8 1. Introduction 8 1. Introduction
@@ -169,10 +169,11 @@ or may be specified by user parameters. @@ -169,10 +169,11 @@ or may be specified by user parameters.
169 The first version of the ctbkgcube tool is included in the release. 169 The first version of the ctbkgcube tool is included in the release.
170 ctbkgcube computes the background cube for an observation definition 170 ctbkgcube computes the background cube for an observation definition
171 file. The cube definition may be taken from a counts cube or may be 171 file. The cube definition may be taken from a counts cube or may be
172 -specified by user parameters.  
173 -  
174 -Memory usage has been reduced by disposing the event lists after their  
175 -usage. 172 +specified by user parameters. The tools also updates the input model
  173 +by replacing all IACT background model components by a single
  174 +background cube background model with a power law as spectral
  175 +parameter. The model container is available to Python using the
  176 +models() method or can be written into an XML file on disk.
176 177
177 178
178 15. ctcubemask - CTA cube filtering 179 15. ctcubemask - CTA cube filtering
@@ -193,7 +194,8 @@ file. @@ -193,7 +194,8 @@ file.
193 194
194 17. Examples 195 17. Examples
195 ------------ 196 ------------
196 -None 197 +Added the following example scripts:
  198 +* pipeline_stacked.py (simulation and stacked analysis)
197 199
198 200
199 18. Scripts 201 18. Scripts
examples/README
1 This folder contains Python example scripts that illustrate the 1 This folder contains Python example scripts that illustrate the
2 usage of the ctools. 2 usage of the ctools.
3 3
  4 +pipeline_stacked_mem.py
  5 + Example pipeline for simulating events and performing a stacked
  6 + analysis in memory. No files will be written to disk.
  7 +
4 make_binned_analysis.py 8 make_binned_analysis.py
5 Example Python pipelines for binned analysis. 9 Example Python pipelines for binned analysis.
6 The first pipeline writes intermediate results to the disk while 10 The first pipeline writes intermediate results to the disk while
examples/pipeline_stacked_mem.py 0 → 100755
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This script illustrates how to perform a stacked CTA analysis from the
  4 +# simulation to the analysis. You may use and adapt this script to
  5 +# implement your own pipeline. You can run the example by typing
  6 +#
  7 +# ./pipeline_stacked_mem.py
  8 +#
  9 +# Copyright (C) 2014 Juergen Knoedlseder
  10 +#
  11 +# This program is free software: you can redistribute it and/or modify
  12 +# it under the terms of the GNU General Public License as published by
  13 +# the Free Software Foundation, either version 3 of the License, or
  14 +# (at your option) any later version.
  15 +#
  16 +# This program is distributed in the hope that it will be useful,
  17 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19 +# GNU General Public License for more details.
  20 +#
  21 +# You should have received a copy of the GNU General Public License
  22 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  23 +#
  24 +# ==========================================================================
  25 +import gammalib
  26 +import ctools
  27 +
  28 +
  29 +# ================== #
  30 +# Setup observations #
  31 +# ================== #
  32 +def setup_observations(pattern="four", ra=83.6331, dec=22.0145, offset=1.5, \
  33 + emin=0.1, emax=100.0, rad=5.0, duration=1800.0, \
  34 + deadc=0.95, \
  35 + caldb="dummy", irf="cta_dummy_irf"):
  36 + """
  37 + Returns an observation container.
  38 +
  39 + Keywords:
  40 + pattern - Pointing pattern, either "single" or "four"
  41 + ra - RA of pattern centre [deg] (default: 83.6331)
  42 + dec - DEC of pattern centre [deg] (default: 22.0145)
  43 + offset - Offset between observations of pattern [deg] (default: 1.5)
  44 + emin - Minimum energy [TeV] (default: 0.1)
  45 + emax - Maximum energy [TeV] (default: 100.0)
  46 + rad - ROI radius used for analysis [deg] (default: 5.0)
  47 + duration - Duration of one CTA observation [seconds] (default: 1800.0)
  48 + deadc - Deadtime correction factor (default: 0.95)
  49 + caldb - Calibration database path (default: "dummy")
  50 + irf - Instrument response function (default: cta_dummy_irf)
  51 + """
  52 + # Set list of observations
  53 + obs_def_list = ctools.obsutils.set_obs_patterns(pattern, \
  54 + ra=ra, \
  55 + dec=dec, \
  56 + offset=offset)
  57 +
  58 + # Get observation container
  59 + obs = ctools.obsutils.set_obs_list(obs_def_list, \
  60 + duration=duration, \
  61 + emin=emin, \
  62 + emax=emax, \
  63 + rad=rad, \
  64 + caldb=caldb, \
  65 + irf=irf)
  66 +
  67 + # Return observation container
  68 + return obs
  69 +
  70 +
  71 +# =========== #
  72 +# Setup model #
  73 +# =========== #
  74 +def setup_model(obs, model="${CTOOLS}/share/models/crab.xml"):
  75 + """
  76 + Setup model for analysis.
  77 +
  78 + Keywords:
  79 + model - Model Xml file
  80 + """
  81 + # Append model from file to observation container
  82 + obs.models(gammalib.GModels(model))
  83 +
  84 + # Return observation container
  85 + return obs
  86 +
  87 +
  88 +# ================================ #
  89 +# Simulation and analysis pipeline #
  90 +# ================================ #
  91 +def run_pipeline(obs, ra=83.6331, dec=22.0145, emin=0.1, emax=100.0, \
  92 + enumbins=20, nxpix=200, nypix=200, binsz=0.02, \
  93 + coordsys="CEL", proj="CAR", debug=False):
  94 + """
  95 + Simulation and stacked analysis pipeline.
  96 +
  97 + Keywords:
  98 + ra - RA of cube centre [deg] (default: 83.6331)
  99 + dec - DEC of cube centre [deg] (default: 22.0145)
  100 + emin - Minimum energy of cube [TeV] (default: 0.1)
  101 + emax - Maximum energy of cube [TeV] (default: 100.0)
  102 + enumbins - Number of energy bins in cube (default: 20)
  103 + nxpix - Number of RA pixels in cube (default: 200)
  104 + nypix - Number of DEC pixels in cube (default: 200)
  105 + binsz - Spatial cube bin size [deg] (default: 0.02)
  106 + coordsys - Cube coordinate system (CEL or GAL)
  107 + proj - Cube World Coordinate System (WCS) projection
  108 + """
  109 + # Simulate events
  110 + sim = ctools.ctobssim(obs)
  111 + sim["debug"].boolean(debug)
  112 + sim.run()
  113 +
  114 + # Bin events into counts map
  115 + bin = ctools.ctbin(sim.obs())
  116 + bin["ebinalg"].string("LOG")
  117 + bin["emin"].real(emin)
  118 + bin["emax"].real(emax)
  119 + bin["enumbins"].integer(enumbins)
  120 + bin["nxpix"].integer(nxpix)
  121 + bin["nypix"].integer(nypix)
  122 + bin["binsz"].real(binsz)
  123 + bin["coordsys"].string(coordsys)
  124 + bin["proj"].string(proj)
  125 + bin["xref"].real(ra)
  126 + bin["yref"].real(dec)
  127 + bin["debug"].boolean(debug)
  128 + bin.run()
  129 +
  130 + # Create exposure cube
  131 + expcube = ctools.ctexpcube(sim.obs())
  132 + expcube["cntmap"].filename("NONE")
  133 + expcube["ebinalg"].string("LOG")
  134 + expcube["emin"].real(emin)
  135 + expcube["emax"].real(emax)
  136 + expcube["enumbins"].integer(enumbins)
  137 + expcube["nxpix"].integer(nxpix)
  138 + expcube["nypix"].integer(nypix)
  139 + expcube["binsz"].real(binsz)
  140 + expcube["coordsys"].string(coordsys)
  141 + expcube["proj"].string(proj)
  142 + expcube["xref"].real(ra)
  143 + expcube["yref"].real(dec)
  144 + expcube["debug"].boolean(debug)
  145 + expcube.run()
  146 +
  147 + # Create PSF cube
  148 + psfcube = ctools.ctpsfcube(sim.obs())
  149 + psfcube["cntmap"].filename("NONE")
  150 + psfcube["ebinalg"].string("LOG")
  151 + psfcube["emin"].real(emin)
  152 + psfcube["emax"].real(emax)
  153 + psfcube["enumbins"].integer(enumbins)
  154 + psfcube["nxpix"].integer(10)
  155 + psfcube["nypix"].integer(10)
  156 + psfcube["binsz"].real(1.0)
  157 + psfcube["coordsys"].string(coordsys)
  158 + psfcube["proj"].string(proj)
  159 + psfcube["xref"].real(ra)
  160 + psfcube["yref"].real(dec)
  161 + psfcube["debug"].boolean(debug)
  162 + psfcube.run()
  163 +
  164 + # Create background cube
  165 + bkgcube = ctools.ctbkgcube(sim.obs())
  166 + bkgcube["cntmap"].filename("NONE")
  167 + bkgcube["ebinalg"].string("LOG")
  168 + bkgcube["emin"].real(emin)
  169 + bkgcube["emax"].real(emax)
  170 + bkgcube["enumbins"].integer(enumbins)
  171 + bkgcube["nxpix"].integer(10)
  172 + bkgcube["nypix"].integer(10)
  173 + bkgcube["binsz"].real(1.0)
  174 + bkgcube["coordsys"].string(coordsys)
  175 + bkgcube["proj"].string(proj)
  176 + bkgcube["xref"].real(ra)
  177 + bkgcube["yref"].real(dec)
  178 + bkgcube["debug"].boolean(debug)
  179 + bkgcube.run()
  180 +
  181 + # Attach background model to observation container
  182 + bin.obs().models(bkgcube.models())
  183 +
  184 + # Set Exposure and Psf cube for first CTA observation
  185 + # (ctbin will create an observation with a single container)
  186 + bin.obs()[0].response(expcube.expcube(), psfcube.psfcube())
  187 +
  188 + # Perform maximum likelihood fitting
  189 + like = ctools.ctlike(bin.obs())
  190 + like["debug"].boolean(True) # Switch this always on for results in console
  191 + like.run()
  192 +
  193 + # Return
  194 + return
  195 +
  196 +
  197 +# ======================== #
  198 +# Main routine entry point #
  199 +# ======================== #
  200 +if __name__ == '__main__':
  201 + """
  202 + """
  203 + # Dump header
  204 + print("********************************************")
  205 + print("* CTA stacked analysis pipeline *")
  206 + print("********************************************")
  207 +
  208 + # Setup observations
  209 + obs = setup_observations()
  210 +
  211 + # Setup model
  212 + obs = setup_model(obs)
  213 +
  214 + # Run analysis pipeline
  215 + run_pipeline(obs)