{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Generating a background model from the data" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "
\"Download
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**In this tutorial you will learn to generate a background model from the data**\n", "\n", "The public H.E.S.S. data do not contain a model for the instrumental background. We can generate one from the data using the csbkgmodel script.\n", "\n", "We start with the usual imports." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import gammalib\n", "import ctools\n", "import cscripts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will use the same events selected in a previous tutorial. For the spatial part we will model the background as a radial Gaussian modified by a bi-linear gradient. For the spectral part we will use a piece-wise power law with 8 nodes between 0.7 TeV and 30 TeV. Note that for a reasonable background fit the nodes should be within the energy range covered by the observations and where there are enough photon counts to constrain the model, hence the upper energy bound at 30 TeV." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "obsfile = 'obs_crab_selected.xml'\n", "outmodel = 'bkgmodel.xml'\n", "bkgmodel = cscripts.csbkgmodel()\n", "bkgmodel['inobs'] = obsfile\n", "bkgmodel['outmodel'] = outmodel\n", "bkgmodel['instrument'] = 'HESS'\n", "bkgmodel['spatial'] = 'LOOKUP'\n", "bkgmodel['slufile'] = '$CTOOLS/share/models/hess_bkg_lookup.fits'\n", "bkgmodel['gradient'] = True\n", "bkgmodel['spectral'] = 'NODES'\n", "bkgmodel['ebinalg'] = 'LOG'\n", "bkgmodel['emin'] = 0.7\n", "bkgmodel['emax'] = 30.0\n", "bkgmodel['enumbins'] = 8\n", "bkgmodel['runwise'] = True\n", "bkgmodel.execute()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's look at the resulting background model." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "=== GModels ===\n", " Number of models ..........: 4\n", " Number of parameters ......: 80\n", "=== GCTAModelBackground ===\n", " Name ......................: Background_023523\n", " Instruments ...............: HESS\n", " Observation identifiers ...: 023523\n", " Model type ................: CTABackground\n", " Model components ..........: \"Multiplicative\" * \"NodeFunction\" * \"Constant\"\n", " Number of parameters ......: 20\n", " Number of spatial par's ...: 3\n", " 1:Normalization ..........: 1 [0.001,1000] (fixed,scale=1,gradient)\n", " 2:Grad_DETX ..............: 0.0881748792818854 +/- 0.0222611841905954 deg^-1 (free,scale=1,gradient)\n", " 2:Grad_DETY ..............: -0.000701218995455618 +/- 0.0227202811076142 deg^-1 (free,scale=1,gradient)\n", " Number of spectral par's ..: 16\n", " Energy0 ..................: 700000 [7e-05,infty[ MeV (fixed,scale=700000)\n", " Intensity0 ...............: 0.00167503068442416 +/- 0.000204995398027476 [1.00182541969977e-13,infty[ ph/cm2/s/MeV (free,scale=0.00100182541969977,gradient)\n", " Energy1 ..................: 1197413.67463685 [0.000119741367463685,infty[ MeV (fixed,scale=1197413.67463685)\n", " Intensity1 ...............: 0.000275120588592054 +/- 1.41610992132859e-05 [3.21240099713853e-14,infty[ ph/cm2/s/MeV (free,scale=0.000321240099713853,gradient)\n", " Energy2 ..................: 2048285.01172474 [0.000204828501172474,infty[ MeV (fixed,scale=2048285.01172474)\n", " Intensity2 ...............: 0.000102369982797547 +/- 5.87189405970882e-06 [1.03007170346199e-14,infty[ ph/cm2/s/MeV (free,scale=0.000103007170346199,gradient)\n", " Energy3 ..................: 3503777.83227556 [0.000350377783227556,infty[ MeV (fixed,scale=3503777.83227556)\n", " Intensity3 ...............: 3.43310169853447e-05 +/- 2.52595458342912e-06 [3.30297405342054e-15,infty[ ph/cm2/s/MeV (free,scale=3.30297405342054e-05,gradient)\n", " Energy4 ..................: 5993530.69893743 [0.000599353069893743,infty[ MeV (fixed,scale=5993530.69893743)\n", " Intensity4 ...............: 1.04184963555313e-05 +/- 1.01064863096553e-06 [1.05911438600856e-15,infty[ ph/cm2/s/MeV (free,scale=1.05911438600856e-05,gradient)\n", " Energy5 ..................: 10252479.454662 [0.0010252479454662,infty[ MeV (fixed,scale=10252479.454662)\n", " Intensity5 ...............: 4.1837003769505e-06 +/- 5.25942224806527e-07 [3.39610080039421e-16,infty[ ph/cm2/s/MeV (free,scale=3.39610080039421e-06,gradient)\n", " Energy6 ..................: 17537798.7113509 [0.00175377987113509,infty[ MeV (fixed,scale=17537798.7113509)\n", " Intensity6 ...............: 8.6658971981008e-07 +/- 1.76584928413557e-07 [1.08897592165697e-16,infty[ ph/cm2/s/MeV (free,scale=1.08897592165697e-06,gradient)\n", " Energy7 ..................: 30000000 [0.003,infty[ MeV (fixed,scale=30000000)\n", " Intensity7 ...............: 3.41087754372141e-07 +/- 1.23894301613722e-07 [3.49185323889972e-17,infty[ ph/cm2/s/MeV (free,scale=3.49185323889972e-07,gradient)\n", " Number of temporal par's ..: 1\n", " Normalization ............: 1 (relative value) (fixed,scale=1,gradient)\n", "=== GCTAModelBackground ===\n", " Name ......................: Background_023526\n", " Instruments ...............: HESS\n", " Observation identifiers ...: 023526\n", " Model type ................: CTABackground\n", " Model components ..........: \"Multiplicative\" * \"NodeFunction\" * \"Constant\"\n", " Number of parameters ......: 20\n", " Number of spatial par's ...: 3\n", " 1:Normalization ..........: 1 [0.001,1000] (fixed,scale=1,gradient)\n", " 2:Grad_DETX ..............: 0.0153166143265463 +/- 0.0215144304488992 deg^-1 (free,scale=1,gradient)\n", " 2:Grad_DETY ..............: -0.00908019181469828 +/- 0.0213167666197536 deg^-1 (free,scale=1,gradient)\n", " Number of spectral par's ..: 16\n", " Energy0 ..................: 700000 [7e-05,infty[ MeV (fixed,scale=700000)\n", " Intensity0 ...............: 0.00126074399446221 +/- 6.18948752992e-05 [9.62519069451052e-14,infty[ ph/cm2/s/MeV (free,scale=0.000962519069451052,gradient)\n", " Energy1 ..................: 1197413.67463685 [0.000119741367463685,infty[ MeV (fixed,scale=1197413.67463685)\n", " Intensity1 ...............: 0.000220109488746193 +/- 1.10099106908638e-05 [2.95441986174209e-14,infty[ ph/cm2/s/MeV (free,scale=0.000295441986174209,gradient)\n", " Energy2 ..................: 2048285.01172474 [0.000204828501172474,infty[ MeV (fixed,scale=2048285.01172474)\n", " Intensity2 ...............: 0.000104377151461511 +/- 6.26567390995899e-06 [9.068492247571e-15,infty[ ph/cm2/s/MeV (free,scale=9.068492247571e-05,gradient)\n", " Energy3 ..................: 3503777.83227556 [0.000350377783227556,infty[ MeV (fixed,scale=3503777.83227556)\n", " Intensity3 ...............: 2.68164289254463e-05 +/- 2.2601490434718e-06 [2.78354314866283e-15,infty[ ph/cm2/s/MeV (free,scale=2.78354314866283e-05,gradient)\n", " Energy4 ..................: 5993530.69893743 [0.000599353069893743,infty[ MeV (fixed,scale=5993530.69893743)\n", " Intensity4 ...............: 8.96128027251196e-06 +/- 9.61601396204211e-07 [8.54399193266454e-16,infty[ ph/cm2/s/MeV (free,scale=8.54399193266454e-06,gradient)\n", " Energy5 ..................: 10252479.454662 [0.0010252479454662,infty[ MeV (fixed,scale=10252479.454662)\n", " Intensity5 ...............: 3.14829249162658e-06 +/- 4.37081753413707e-07 [2.62254954375342e-16,infty[ ph/cm2/s/MeV (free,scale=2.62254954375342e-06,gradient)\n", " Energy6 ..................: 17537798.7113509 [0.00175377987113509,infty[ MeV (fixed,scale=17537798.7113509)\n", " Intensity6 ...............: 6.09624701513041e-07 +/- 1.43872314987655e-07 [8.04982748537824e-17,infty[ ph/cm2/s/MeV (free,scale=8.04982748537824e-07,gradient)\n", " Energy7 ..................: 30000000 [0.003,infty[ MeV (fixed,scale=30000000)\n", " Intensity7 ...............: 3.44982419809933e-07 +/- 1.26076763285202e-07 [2.4708674312253e-17,infty[ ph/cm2/s/MeV (free,scale=2.4708674312253e-07,gradient)\n", " Number of temporal par's ..: 1\n", " Normalization ............: 1 (relative value) (fixed,scale=1,gradient)\n", "=== GCTAModelBackground ===\n", " Name ......................: Background_023559\n", " Instruments ...............: HESS\n", " Observation identifiers ...: 023559\n", " Model type ................: CTABackground\n", " Model components ..........: \"Multiplicative\" * \"NodeFunction\" * \"Constant\"\n", " Number of parameters ......: 20\n", " Number of spatial par's ...: 3\n", " 1:Normalization ..........: 1 [0.001,1000] (fixed,scale=1,gradient)\n", " 2:Grad_DETX ..............: 0.018749400342 +/- 0.0217067733288596 deg^-1 (free,scale=1,gradient)\n", " 2:Grad_DETY ..............: -0.126640342510319 +/- 0.020455126417247 deg^-1 (free,scale=1,gradient)\n", " Number of spectral par's ..: 16\n", " Energy0 ..................: 700000 [7e-05,infty[ MeV (fixed,scale=700000)\n", " Intensity0 ...............: 0.00109294743570479 +/- 5.37668986080239e-05 [8.7418891142236e-14,infty[ ph/cm2/s/MeV (free,scale=0.00087418891142236,gradient)\n", " Energy1 ..................: 1197413.67463685 [0.000119741367463685,infty[ MeV (fixed,scale=1197413.67463685)\n", " Intensity1 ...............: 0.000220103906688583 +/- 1.10283166290448e-05 [2.69181956755491e-14,infty[ ph/cm2/s/MeV (free,scale=0.000269181956755491,gradient)\n", " Energy2 ..................: 2048285.01172474 [0.000204828501172474,infty[ MeV (fixed,scale=2048285.01172474)\n", " Intensity2 ...............: 8.06623852687504e-05 +/- 5.23872750218462e-06 [8.28870337932099e-15,infty[ ph/cm2/s/MeV (free,scale=8.28870337932099e-05,gradient)\n", " Energy3 ..................: 3503777.83227556 [0.000350377783227556,infty[ MeV (fixed,scale=3503777.83227556)\n", " Intensity3 ...............: 2.85428972580395e-05 +/- 2.37266748207121e-06 [2.55227373106486e-15,infty[ ph/cm2/s/MeV (free,scale=2.55227373106486e-05,gradient)\n", " Energy4 ..................: 5993530.69893743 [0.000599353069893743,infty[ MeV (fixed,scale=5993530.69893743)\n", " Intensity4 ...............: 6.46456658892385e-06 +/- 7.36558401693409e-07 [7.85901111449516e-16,infty[ ph/cm2/s/MeV (free,scale=7.85901111449516e-06,gradient)\n", " Energy5 ..................: 10252479.454662 [0.0010252479454662,infty[ MeV (fixed,scale=10252479.454662)\n", " Intensity5 ...............: 3.20506991372933e-06 +/- 4.41036712274814e-07 [2.4199620497598e-16,infty[ ph/cm2/s/MeV (free,scale=2.4199620497598e-06,gradient)\n", " Energy6 ..................: 17537798.7113509 [0.00175377987113509,infty[ MeV (fixed,scale=17537798.7113509)\n", " Intensity6 ...............: 9.17351299053318e-07 +/- 2.02856047440946e-07 [7.45159440158635e-17,infty[ ph/cm2/s/MeV (free,scale=7.45159440158635e-07,gradient)\n", " Energy7 ..................: 30000000 [0.003,infty[ MeV (fixed,scale=30000000)\n", " Intensity7 ...............: 1.93348643577948e-07 +/- 9.48901838749967e-08 [2.29450949990163e-17,infty[ ph/cm2/s/MeV (free,scale=2.29450949990163e-07,gradient)\n", " Number of temporal par's ..: 1\n", " Normalization ............: 1 (relative value) (fixed,scale=1,gradient)\n", "=== GCTAModelBackground ===\n", " Name ......................: Background_023592\n", " Instruments ...............: HESS\n", " Observation identifiers ...: 023592\n", " Model type ................: CTABackground\n", " Model components ..........: \"Multiplicative\" * \"NodeFunction\" * \"Constant\"\n", " Number of parameters ......: 20\n", " Number of spatial par's ...: 3\n", " 1:Normalization ..........: 1 [0.001,1000] (fixed,scale=1,gradient)\n", " 2:Grad_DETX ..............: 0.0290076925442388 +/- 0.0231627042594752 deg^-1 (free,scale=1,gradient)\n", " 2:Grad_DETY ..............: 0.165459453396994 +/- 0.0212165299830029 deg^-1 (free,scale=1,gradient)\n", " Number of spectral par's ..: 16\n", " Energy0 ..................: 700000 [7e-05,infty[ MeV (fixed,scale=700000)\n", " Intensity0 ...............: 0.00173543806725313 +/- 0.000221842434989178 [9.64800822522881e-14,infty[ ph/cm2/s/MeV (free,scale=0.000964800822522881,gradient)\n", " Energy1 ..................: 1197413.67463685 [0.000119741367463685,infty[ MeV (fixed,scale=1197413.67463685)\n", " Intensity1 ...............: 0.000247432426542881 +/- 1.35075275068334e-05 [3.05783271724063e-14,infty[ ph/cm2/s/MeV (free,scale=0.000305783271724063,gradient)\n", " Energy2 ..................: 2048285.01172474 [0.000204828501172474,infty[ MeV (fixed,scale=2048285.01172474)\n", " Intensity2 ...............: 0.000102349095137416 +/- 6.00669028431716e-06 [9.6914727976462e-15,infty[ ph/cm2/s/MeV (free,scale=9.6914727976462e-05,gradient)\n", " Energy3 ..................: 3503777.83227556 [0.000350377783227556,infty[ MeV (fixed,scale=3503777.83227556)\n", " Intensity3 ...............: 3.39523135381865e-05 +/- 2.54888013407308e-06 [3.07160834724385e-15,infty[ ph/cm2/s/MeV (free,scale=3.07160834724385e-05,gradient)\n", " Energy4 ..................: 5993530.69893743 [0.000599353069893743,infty[ MeV (fixed,scale=5993530.69893743)\n", " Intensity4 ...............: 9.09839000917866e-06 +/- 9.25509371718351e-07 [9.735133179293e-16,infty[ ph/cm2/s/MeV (free,scale=9.735133179293e-06,gradient)\n", " Energy5 ..................: 10252479.454662 [0.0010252479454662,infty[ MeV (fixed,scale=10252479.454662)\n", " Intensity5 ...............: 3.01613230297962e-06 +/- 4.11382625961797e-07 [3.08544603688196e-16,infty[ ph/cm2/s/MeV (free,scale=3.08544603688196e-06,gradient)\n", " Energy6 ..................: 17537798.7113509 [0.00175377987113509,infty[ MeV (fixed,scale=17537798.7113509)\n", " Intensity6 ...............: 9.30049222211062e-07 +/- 1.86843120695054e-07 [9.77899025229569e-17,infty[ ph/cm2/s/MeV (free,scale=9.77899025229569e-07,gradient)\n", " Energy7 ..................: 30000000 [0.003,infty[ MeV (fixed,scale=30000000)\n", " Intensity7 ...............: 3.98006363733031e-07 +/- 1.41569683967725e-07 [3.09934606573553e-17,infty[ ph/cm2/s/MeV (free,scale=3.09934606573553e-07,gradient)\n", " Number of temporal par's ..: 1\n", " Normalization ............: 1 (relative value) (fixed,scale=1,gradient)\n" ] } ], "source": [ "print(gammalib.GModels(outmodel))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }