{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to connect observations to specific models?" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "
\"Download
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the previous examples there was always a single background model component to describe the residual particle background in the various dataset. This implies that the spatial and spectral shape of the background distribution is assumed to be identical for all observations. This is fine in a simulation, but for a real life situation this assumption will probably not hold.\n", "\n", "Start by importing the gammalib, ctools, and cscripts Python modules." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import gammalib\n", "import ctools\n", "import cscripts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simulating the dataset\n", "\n", "Let’s start with creating a pointing definition ASCII file for two 30 min pointings near the Crab." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "f = open('pnt.def', 'wb')\n", "f.write('name,ra,dec,duration\\n')\n", "f.write('Crab,83.63,21.51,1800.0\\n')\n", "f.write('Crab,83.63,22.51,1800.0\\n')\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inspect the file that you just created. For that purpose let's create a `peek()` function that will also be used later to display XML files." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name,ra,dec,duration\n", "Crab,83.63,21.51,1800.0\n", "Crab,83.63,22.51,1800.0\n" ] } ], "source": [ "def peek(filename):\n", " f = open(gammalib.expand_env(filename), 'r')\n", " for line in f:\n", " print(line.rstrip())\n", " f.close()\n", "peek('pnt.def')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A pointing definition file is an ASCII file in Comma Separated Values (CSV) format that specifies one pointing per row. The file provides the `name` of the observation, the Right Ascension `ra` and Declination `dec` of the pointing, and its `duration`. Additional optional columns are possible (defining for example the energy range or the Instrument Response Function), but for this simulation the provided information is sufficient.\n", "\n", "Now transform the pointing definition file into an observation definition XML file using the csobsdef script." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "obsdef = cscripts.csobsdef()\n", "obsdef['inpnt'] = 'pnt.def'\n", "obsdef['outobs'] = 'obs.xml'\n", "obsdef['caldb'] = 'prod2'\n", "obsdef['irf'] = 'South_0.5h'\n", "obsdef.execute()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's peek the resulting observation definition XML file" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ] } ], "source": [ "peek('obs.xml')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The file contains two observations which distinguish by the `id` attributes `000001` and `000002`, which are **unique to each observation for a given instrument**. The `id` attribute can therefore be used to uniquely identify a CTA observation.\n", "\n", "The value of the `id` attributes can be controlled by adding specific values to the pointing definition file, but if the values are missing - which is the case in the example - they simply count from `000001` upwards.\n", "\n", "Feed now the observation definition XML file into the ctobssim tool to simulate the event data." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "obssim = ctools.ctobssim()\n", "obssim['inobs'] = 'obs.xml'\n", "obssim['rad'] = 5.0\n", "obssim['emin'] = 0.1\n", "obssim['emax'] = 100.0\n", "obssim['inmodel'] = '$CTOOLS/share/models/crab_2bkg.xml'\n", "obssim['outevents'] = 'obs_2bkg.xml'\n", "obssim.execute()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This will produce the two event files `sim_events_000001.fits` and `sim_events_000002.fits` on disk. Note that a specific model was used to simulate the data and peeking that model definiton file shows that it contains two different background components with a different power law `Prefactor` and `Index`. Both background components also have an `id` attribute which is used to tie them to the two observations. In other words, `Background_000001` will be used for the observation with identifier `000001` and `Background_000002` will be used for the observation with identifier `000002`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ] } ], "source": [ "peek('$CTOOLS/share/models/crab_2bkg.xml')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Analysing the data\n", "\n", "Now run a maximum likelihood fit of the model to the simulated data" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "like = ctools.ctlike()\n", "like['inobs'] = 'obs_2bkg.xml'\n", "like['inmodel'] = '$CTOOLS/share/models/crab_2bkg.xml'\n", "like['outmodel'] = 'crab_results.xml'\n", "like.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and inspect the model fitting results" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "=== GModels ===\n", " Number of models ..........: 3\n", " Number of parameters ......: 14\n", "=== GModelSky ===\n", " Name ......................: Crab\n", " Instruments ...............: all\n", " Observation identifiers ...: all\n", " Model type ................: PointSource\n", " Model components ..........: \"PointSource\" * \"PowerLaw\" * \"Constant\"\n", " Number of parameters ......: 6\n", " Number of spatial par's ...: 2\n", " RA .......................: 83.6331 [-360,360] deg (fixed,scale=1)\n", " DEC ......................: 22.0145 [-90,90] deg (fixed,scale=1)\n", " Number of spectral par's ..: 3\n", " Prefactor ................: 5.76556140039476e-16 +/- 7.29118825882603e-18 [1e-23,1e-13] ph/cm2/s/MeV (free,scale=1e-16,gradient)\n", " Index ....................: -2.46505796935392 +/- 0.0107196310388945 [-0,-5] (free,scale=-1,gradient)\n", " PivotEnergy ..............: 300000 [10000,1000000000] MeV (fixed,scale=1000000,gradient)\n", " Number of temporal par's ..: 1\n", " Normalization ............: 1 (relative value) (fixed,scale=1,gradient)\n", " Number of scale par's .....: 0\n", "=== GCTAModelIrfBackground ===\n", " Name ......................: Background_000001\n", " Instruments ...............: CTA\n", " Observation identifiers ...: 000001\n", " Model type ................: \"PowerLaw\" * \"Constant\"\n", " Number of parameters ......: 4\n", " Number of spectral par's ..: 3\n", " Prefactor ................: 0.504862658259721 +/- 0.0077214563116661 [0.001,1000] ph/cm2/s/MeV (free,scale=1,gradient)\n", " Index ....................: 0.197685073818273 +/- 0.00982058375963094 [-5,5] (free,scale=1,gradient)\n", " PivotEnergy ..............: 1000000 [10000,1000000000] MeV (fixed,scale=1000000,gradient)\n", " Number of temporal par's ..: 1\n", " Normalization ............: 1 (relative value) (fixed,scale=1,gradient)\n", "=== GCTAModelIrfBackground ===\n", " Name ......................: Background_000002\n", " Instruments ...............: CTA\n", " Observation identifiers ...: 000002\n", " Model type ................: \"PowerLaw\" * \"Constant\"\n", " Number of parameters ......: 4\n", " Number of spectral par's ..: 3\n", " Prefactor ................: 2.01944300996949 +/- 0.0173357023683058 [0.001,1000] ph/cm2/s/MeV (free,scale=1,gradient)\n", " Index ....................: -0.19787756674158 +/- 0.00509621480931608 [-5,5] (free,scale=1,gradient)\n", " PivotEnergy ..............: 1000000 [10000,1000000000] MeV (fixed,scale=1000000,gradient)\n", " Number of temporal par's ..: 1\n", " Normalization ............: 1 (relative value) (fixed,scale=1,gradient)\n" ] } ], "source": [ "print(like.obs().models())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Obviously the two background models have different best fitting spectral parameters which correspond to the simulated values (see above)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 2 }