Commit 3c6ae30b9fb1ca74ef8599065ae0a0b47d06b20d

Authored by Jürgen Knödlseder
2 parents d975bd1a 6b06b2a7

Merge branch '1051-create-ctbutterfly' into integration

doc/source/fhelp/ctbutterfly.html 0 → 100644
  1 +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2 +<html lang="en"><head>
  3 +<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>ctbutterfly</title>
  4 +
  5 +<link rel="stylesheet" type="text/css" media="screen,print" href="ctools.css">
  6 +<meta content="Michael Mayer" name="author">
  7 +<meta content="Spectral butterfly (confidence band) computation tools" name="description">
  8 +</head>
  9 +<body>
  10 +<p class="Header">ctbutterfly</p>
  11 +<p class="Body">Butterfly calculation tool.</p>
  12 +<br>
  13 +<p class="Title">Synopsis</p>
  14 +<p style="padding-top: 0pt; text-align: left;" class="Body">Calculates
  15 +the confidence band of a spectral model, taking into account the
  16 +covariance matrix of a likelihood fit.
  17 +</p>
  18 +<br>
  19 +<p class="Title">Links</p>
  20 +<ul class="Body">
  21 +<li><a href="ftools_usage.html">Basic FTOOL
  22 +Parameter Usage</a></li>
  23 +</ul>
  24 +<p style="padding-top: 0pt; text-align: left;" class="Body"></p>
  25 +<br>
  26 +<p class="Title">General parameters</p>
  27 +<table id="table1" class="Body" border="0" cellpadding="0" cellspacing="0" width="600">
  28 +<tbody>
  29 +<tr>
  30 +<td width="50">&nbsp;</td>
  31 +<td colspan="2"><span class="irafpar"><strong>infile
  32 +[file]</strong></span></td>
  33 +</tr>
  34 +<tr>
  35 +<td width="50">&nbsp;</td>
  36 +<td width="25">&nbsp;</td>
  37 +<td width="525">XML or fits file that defines the observation</td>
  38 +</tr>
  39 +<tr>
  40 +<td width="50">&nbsp;</td>
  41 +<td>&nbsp;</td>
  42 +<td width="525">&nbsp;</td>
  43 +</tr>
  44 +<tr>
  45 +<td width="50">&nbsp;</td>
  46 +<td colspan="2"><span class="irafpar"><strong>srcmdl
  47 +[file]</strong></span></td>
  48 +</tr>
  49 +<tr>
  50 +<td width="50">&nbsp;</td>
  51 +<td width="25">&nbsp;</td>
  52 +<td width="525">Model XML file containing the source and background definitions</td>
  53 +</tr>
  54 +<tr>
  55 +<td width="50">&nbsp;</td>
  56 +<td>&nbsp;</td>
  57 +<td width="525">&nbsp;</td>
  58 +</tr>
  59 +<td width="50">&nbsp;</td>
  60 +<td colspan="2"><span class="irafpar"><strong>srcname
  61 + [file]</strong></span></td>
  62 +</tr>
  63 +<tr>
  64 + <td width="50">&nbsp;</td>
  65 + <td width="25">&nbsp;</td>
  66 + <td width="525">Name of model for which butterfly should be computed</td>
  67 +</tr>
  68 +<tr>
  69 + <td width="50">&nbsp;</td>
  70 + <td>&nbsp;</td>
  71 + <td width="525">&nbsp;</td>
  72 +</tr>
  73 +<tr>
  74 + <td width="50">&nbsp;</td>
  75 + <td colspan="2"><span class="irafpar"><strong>caldb
  76 + [string]</strong></span></td>
  77 +</tr>
  78 +<tr>
  79 + <td width="50">&nbsp;</td>
  80 + <td width="25">&nbsp;</td>
  81 + <td width="525">Calibration database repository.</td>
  82 +</tr>
  83 +<tr>
  84 + <td width="50">&nbsp;</td>
  85 + <td>&nbsp;</td>
  86 + <td width="525">&nbsp;</td>
  87 +</tr>
  88 +<tr>
  89 + <td width="50">&nbsp;</td>
  90 + <td colspan="2"><span class="irafpar"><strong>irf
  91 + [string]</strong></span></td>
  92 +</tr>
  93 +<tr>
  94 + <td width="50">&nbsp;</td>
  95 + <td width="25">&nbsp;</td>
  96 + <td width="525">Instrumental response function that
  97 + should be used for event generation.</td>
  98 +</tr>
  99 +<tr>
  100 + <td width="50">&nbsp;</td>
  101 + <td>&nbsp;</td>
  102 + <td width="525">&nbsp;</td>
  103 +</tr>
  104 +<tr>
  105 + <td width="50">&nbsp;</td>
  106 + <td colspan="2"><span class="irafpar"><strong>outfile
  107 + [file]</strong></span></td>
  108 +</tr>
  109 +<tr>
  110 + <td width="50">&nbsp;</td>
  111 + <td width="25">&nbsp;</td>
  112 + <td width="525">Output butterfly file name</td>
  113 +</tr>
  114 +<tr>
  115 + <td width="50">&nbsp;</td>
  116 + <td>&nbsp;</td>
  117 + <td width="525">&nbsp;</td>
  118 +</tr>
  119 +<tr>
  120 + <td width="50">&nbsp;</td>
  121 + <td colspan="2"><span class="irafpar"><strong>(matrix = "NONE")
  122 + [file]</strong></span></td>
  123 +</tr>
  124 +<tr>
  125 + <td width="50">&nbsp;</td>
  126 + <td width="25">&nbsp;</td>
  127 + <td width="525">Input covariance matrix file (not used)</td>
  128 +</tr>
  129 +<tr>
  130 + <td width="50">&nbsp;</td>
  131 + <td>&nbsp;</td>
  132 + <td width="525">&nbsp;</td>
  133 +</tr>
  134 +<td width="50">&nbsp;</td>
  135 +<td colspan="2"><span class="irafpar"><strong>emin
  136 + [double]</strong></span></td>
  137 +</tr>
  138 +<tr>
  139 + <td width="50">&nbsp;</td>
  140 + <td width="25">&nbsp;</td>
  141 + <td width="525">Minimum energy of butterfly (in
  142 + TeV).</td>
  143 +</tr>
  144 +<tr>
  145 + <td width="50">&nbsp;</td>
  146 + <td>&nbsp;</td>
  147 + <td width="525">&nbsp;</td>
  148 +</tr>
  149 +<tr>
  150 + <td width="50">&nbsp;</td>
  151 + <td colspan="2"><span class="irafpar"><strong>emax
  152 + [double]</strong></span></td>
  153 +</tr>
  154 +<tr>
  155 + <td width="50">&nbsp;</td>
  156 + <td width="25">&nbsp;</td>
  157 + <td width="525">Maximum energy of butterfly (in
  158 + TeV).</td>
  159 +</tr>
  160 +<tr>
  161 + <td width="50">&nbsp;</td>
  162 + <td>&nbsp;</td>
  163 + <td width="525">&nbsp;</td>
  164 +</tr>
  165 +<tr>
  166 + <td width="50">&nbsp;</td>
  167 + <td colspan="2"><span class="irafpar"><strong>(ebinalg
  168 + = "LOG") &lt;FILE|LIN|LOG&gt;
  169 + [string]</strong></span></td>
  170 +</tr>
  171 +<tr>
  172 + <td width="50">&nbsp;</td>
  173 + <td width="25">&nbsp;</td>
  174 + <td width="525">Butterfly binning algorithm</td>
  175 +</tr>
  176 +<tr>
  177 + <td width="50">&nbsp;</td>
  178 + <td>&nbsp;</td>
  179 + <td width="525">&nbsp;</td>
  180 +</tr>
  181 +<tr>
  182 + <td width="50">&nbsp;</td>
  183 + <td colspan="2"><span class="irafpar"><strong>(enumbins
  184 + = 100)
  185 + [string]</strong></span></td>
  186 +</tr>
  187 +<tr>
  188 + <td width="50">&nbsp;</td>
  189 + <td width="25">&nbsp;</td>
  190 + <td width="525">Number of energy bins of butterfly</td>
  191 +</tr>
  192 +<tr>
  193 + <td width="50">&nbsp;</td>
  194 + <td>&nbsp;</td>
  195 + <td width="525">&nbsp;</td>
  196 +</tr>
  197 +<tr>
  198 + <td width="50">&nbsp;</td>
  199 + <td colspan="2"><span class="irafpar"><strong>(ebinfile
  200 + = "NONE")
  201 + [string]</strong></span></td>
  202 +</tr>
  203 +<tr>
  204 + <td width="50">&nbsp;</td>
  205 + <td width="25">&nbsp;</td>
  206 + <td width="525">File for energy binning algorithm</td>
  207 +</tr>
  208 +<tr>
  209 + <td width="50">&nbsp;</td>
  210 + <td>&nbsp;</td>
  211 + <td width="525">&nbsp;</td>
  212 +</tr>
  213 +</tbody>
  214 +</table>
  215 +<p class="Title">Standard parameters</p>
  216 +<table id="table1" class="Body" border="0" cellpadding="0" cellspacing="0" width="600">
  217 +<tbody>
  218 +<tr>
  219 +<td width="50">&nbsp;</td>
  220 +<td colspan="2"><span class="irafpar"><strong>(chatter
  221 += 2) [integer]</strong></span></td>
  222 +</tr>
  223 +<tr>
  224 +<td width="50">&nbsp;</td>
  225 +<td width="25">&nbsp;</td>
  226 +<td width="525">Verbosity of the executable:<br>
  227 +<code>chatter = 0</code>: no information will be
  228 +logged<br>
  229 +<code>chatter = 1</code>: only errors will be logged<br>
  230 +<code>chatter = 2</code>: errors and actions will be
  231 +logged<br>
  232 +<code>chatter = 3</code>: report about the task
  233 +execution<br>
  234 +<code>chatter = 4</code>: detailed report about the
  235 +task execution</td>
  236 +</tr>
  237 +<tr>
  238 +<td width="50">&nbsp;</td>
  239 +<td>&nbsp;</td>
  240 +<td width="525">&nbsp;</td>
  241 +</tr>
  242 +<tr>
  243 +<td width="50">&nbsp;</td>
  244 +<td colspan="2"><span class="irafpar"><strong>(clobber
  245 += yes) [boolean]</strong></span></td>
  246 +</tr>
  247 +<tr>
  248 +<td width="50">&nbsp;</td>
  249 +<td width="25">&nbsp;</td>
  250 +<td width="525">Specifies whether an existing output
  251 +event list should be overwritten.</td>
  252 +</tr>
  253 +<tr>
  254 +<td width="50">&nbsp;</td>
  255 +<td>&nbsp;</td>
  256 +<td width="525">&nbsp;</td>
  257 +</tr>
  258 +<tr>
  259 +<td width="50">&nbsp;</td>
  260 +<td colspan="2"><span class="irafpar"><strong>(debug
  261 += no) [boolean]</strong></span></td>
  262 +</tr>
  263 +<tr>
  264 +<td width="50">&nbsp;</td>
  265 +<td width="25">&nbsp;</td>
  266 +<td width="525">Enables debug mode. In debug mode
  267 +the executable will dump any log file output in the console.</td>
  268 +</tr>
  269 +<tr>
  270 +<td width="50">&nbsp;</td>
  271 +<td>&nbsp;</td>
  272 +<td width="525">&nbsp;</td>
  273 +</tr>
  274 +<tr>
  275 +<td width="50">&nbsp;</td>
  276 +<td colspan="2"><span class="irafpar"><strong>(mode
  277 += ql) [string]</strong></span></td>
  278 +</tr>
  279 +<tr>
  280 +<td width="50">&nbsp;</td>
  281 +<td width="25">&nbsp;</td>
  282 +<td width="525">Mode of automatic parameters.</td>
  283 +</tr>
  284 +<tr>
  285 +<td width="50">&nbsp;</td>
  286 +<td>&nbsp;</td>
  287 +<td width="525">&nbsp;</td>
  288 +</tr>
  289 +</tbody>
  290 +</table>
  291 +<br>
  292 +<p class="Title">Related FTOOLS</p>
  293 +<br>
  294 +<hr size="1">
  295 +<p class="Body">Last updated by: Michael
  296 +Mayer 30 October
  297 +2014
  298 +</p>
  299 +<br>
  300 +</body></html>
0 \ No newline at end of file 301 \ No newline at end of file
doc/source/tools_reference.rst
@@ -14,6 +14,7 @@ ctools @@ -14,6 +14,7 @@ ctools
14 * `ctobssim <fhelp/ctobssim.html>`__ --- Observation simulator. 14 * `ctobssim <fhelp/ctobssim.html>`__ --- Observation simulator.
15 * `ctselect <fhelp/ctselect.html>`__ --- Performs sub-selections of event data which are needed for unbinned maximum likelihood analysis. 15 * `ctselect <fhelp/ctselect.html>`__ --- Performs sub-selections of event data which are needed for unbinned maximum likelihood analysis.
16 * `ctskymap <fhelp/ctskymap.html>`__ --- Computes sky map from CTA data. 16 * `ctskymap <fhelp/ctskymap.html>`__ --- Computes sky map from CTA data.
  17 +* `ctbutterfly <fhelp/ctbutterfly.html>`__ --- Computes a butterfly from a spectral model.
17 18
18 cscripts 19 cscripts
19 -------- 20 --------
doc/source/user_manual/getting_started/butterfly.jpg 0 → 100644

58.9 KB

doc/source/user_manual/getting_started/quickstart.rst
@@ -19,6 +19,8 @@ The example consists of the following analysis steps: @@ -19,6 +19,8 @@ The example consists of the following analysis steps:
19 19
20 - :ref:`sec_unbinned_cta` 20 - :ref:`sec_unbinned_cta`
21 21
  22 +- :ref:`sec_butterfly_cta`
  23 +
22 24
23 .. _sec_simulating_cta: 25 .. _sec_simulating_cta:
24 26
@@ -638,3 +640,64 @@ the same events for both analyses. @@ -638,3 +640,64 @@ the same events for both analyses.
638 2014-01-09T21:47:54: +==============+ 640 2014-01-09T21:47:54: +==============+
639 2014-01-09T21:47:54: 641 2014-01-09T21:47:54:
640 2014-01-09T21:47:54: Application "ctlike" terminated after 6 wall clock seconds, consuming 0.163384 seconds of CPU time. 642 2014-01-09T21:47:54: Application "ctlike" terminated after 6 wall clock seconds, consuming 0.163384 seconds of CPU time.
  643 +
  644 +
  645 + .. _sec_butterfly_cta:
  646 +
  647 +Calculate and visualise butterfly
  648 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  649 +
  650 +To visualise the analysis results retrieved above, one can calculate the
  651 +confidence band of the spectral fit. The tool ctbutterfly takes the optimised
  652 +source model as input. It takes the covariance matrix from the fit to conduct a
  653 +Gaussian error propagation for each energy value. It will write the
  654 +butterfly information into an ASCII file.
  655 +The following example shows how to compute such a butterfly from the command
  656 +line.
  657 +
  658 +.. code-block:: bash
  659 +
  660 + $ ctbutterfly
  661 + Input event list, cube or observation definition file [$CTOOLS/test/data/crab_events.fits.gz]
  662 + Calibration database [share/caldb/data/cta/dummy//cta_dummy_irf] share/caldb/data/cta/dummy/bcf/
  663 + Instrument response function [cta_dummy_irf]
  664 + Source model [$CTOOLS/test/data/crab.xml]
  665 + Source of interest [CrabNebula] Crab
  666 + Start value for first energy bin in TeV [0.1]
  667 + Stop value for last energy bin in TeV [100.]
  668 + Output ascii file [butterfly.txt]
  669 +
  670 +Below some lines of the ``ctbutterfly.log``:
  671 +
  672 +.. code-block:: xml
  673 +
  674 + 2014-10-30T17:51:39: +===========================+
  675 + 2014-10-30T17:51:39: | Compute covariance matrix |
  676 + 2014-10-30T17:51:39: +===========================+
  677 + 2014-10-30T17:51:39:
  678 + 2014-10-30T17:51:39: +====================+
  679 + 2014-10-30T17:51:39: | Generate butterfly |
  680 + 2014-10-30T17:51:39: +====================+
  681 + 2014-10-30T17:51:39:
  682 + 2014-10-30T17:51:39: +========================+
  683 + 2014-10-30T17:51:39: | Save Butterfly to file |
  684 + 2014-10-30T17:51:39: +========================+
  685 + 2014-10-30T17:51:39:
  686 + 2014-10-30T17:51:39: Application "ctbutterfly" terminated after 15 wall clock seconds, consuming 0.051253 seconds of CPU time.
  687 +
  688 +Now that you have computed the confidence band of the spectral fit and
  689 +that you will have an ASCII file named ``butterfly.txt'' on disk you can
  690 +visualise the butterfly using the script ``show_butterfly.py'' that is
  691 +in the ctools ``example'' folder. You will need matplotlib on your system
  692 +to make this work. To launch the script, type:
  693 +
  694 +.. code-block:: bash
  695 + python $CTOOLS/examples/python/show_butterfly.py butterfly.txt
  696 +
  697 +This will result in a canvas which should look like the following:
  698 +
  699 +.. figure:: butterfly.jpg
  700 + :width: 60%
  701 +
  702 + *Confidence band of the fit*
  703 +
641 \ No newline at end of file 704 \ No newline at end of file
examples/Makefile.am
@@ -7,7 +7,8 @@ scripts = README \ @@ -7,7 +7,8 @@ scripts = README \
7 make_ts_distributions.py \ 7 make_ts_distributions.py \
8 make_unbinned_analysis.py \ 8 make_unbinned_analysis.py \
9 show_pull_histogram.py \ 9 show_pull_histogram.py \
10 - show_ts_distribution.py 10 + show_ts_distribution.py \
  11 + show_butterfly
11 12
12 # Set directories 13 # Set directories
13 scriptsdir = $(datadir)/examples/python 14 scriptsdir = $(datadir)/examples/python
examples/show_butterfly.py 0 → 100644
  1 +#! /usr/bin/env python
  2 +# ==========================================================================
  3 +# This script shows how to plot a butterfly created with ctbutterfly
  4 +#
  5 +# Copyright (C) 2014 Michael Mayer
  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 +
  22 +import gammalib
  23 +import sys
  24 +try:
  25 + import matplotlib.pyplot as plt
  26 +except:
  27 + sys.exit("This script needs matplotlib")
  28 +
  29 +# =============== #
  30 +# Script entry #
  31 +# =============== #
  32 +if __name__ == "__main__":
  33 +
  34 + # check for given butterfly file
  35 + if not len(sys.argv) == 2:
  36 + sys.exit("Usage: show_butterfly.py butterfly.txt")
  37 +
  38 + # Read given butterfly file
  39 + filename = sys.argv[1]
  40 + csv = gammalib.GCsv(filename)
  41 +
  42 + # initialise arrays to be filled
  43 + butterfly_x = []
  44 + butterfly_y = []
  45 +
  46 + line_x = []
  47 + line_y = []
  48 +
  49 + # loop over rows of the file
  50 + nrows = csv.nrows()
  51 + for row in range(nrows):
  52 + # Compute upper edge of confidence band
  53 + butterfly_x.append(csv.real(row,0))
  54 + butterfly_y.append(csv.real(row,1)+csv.real(row,2))
  55 +
  56 + # Set line values
  57 + line_x.append(csv.real(row,0))
  58 + line_y.append(csv.real(row,1))
  59 +
  60 + # Loop over the rows backwards to compute the lower edge
  61 + # of the confidence band
  62 + for row in range(nrows):
  63 + index = nrows - 1 - row
  64 +
  65 + butterfly_x.append(csv.real(index,0))
  66 + butterfly_y.append(csv.real(index,1)-csv.real(index,2))
  67 +
  68 + # plot the butterfly and spectral line
  69 + plt.figure()
  70 + plt.loglog()
  71 + plt.grid()
  72 + plt.plot(line_x,line_y,color='black',ls='-')
  73 + plt.fill(butterfly_x,butterfly_y,color='green',alpha=0.5)
  74 + plt.xlabel("Energy [MeV]")
  75 + plt.ylabel(r"dN/dE [MeV$^{-1}$ s$^{-1}$ cm$^{-2}$]")
  76 + plt.show()
0 \ No newline at end of file 77 \ No newline at end of file