\n",
"**This is a fixed-text formatted version of a Jupyter notebook.**\n",
"\n",
"- Try online [![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/gammapy/gammapy-webpage/v0.9?urlpath=lab/tree/cta_sensitivity.ipynb)\n",
"- You can contribute with your own notebooks in this\n",
"[GitHub repository](https://github.com/gammapy/gammapy/tree/master/tutorials).\n",
"- **Source files:**\n",
"[cta_sensitivity.ipynb](../_static/notebooks/cta_sensitivity.ipynb) |\n",
"[cta_sensitivity.py](../_static/notebooks/cta_sensitivity.py)\n",
"
\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Computation of the CTA sensitivity"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction\n",
"\n",
"This notebook explains how to derive the CTA sensitivity for a point-like IRF at a fixed zenith angle and fixed offset. The significativity is computed for the 1D analysis (On-OFF regions) and the LiMa formula.\n",
"\n",
"We will be using the following Gammapy classes:\n",
"\n",
"* gammapy.irf.CTAIrf\n",
"* [gammapy.spectrum.SensitivityEstimator](..\/api/gammapy.spectrum.SensitivityEstimator.rst)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"As usual, we'll start with some setup ..."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "ImportError",
"evalue": "cannot import name 'CTAPerf' from 'gammapy.irf' (/Users/deil/work/code/gammapy-docs/build/dev/gammapy/gammapy/irf/__init__.py)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mgammapy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mirf\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mCTAPerf\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mgammapy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mspectrum\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mSensitivityEstimator\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mImportError\u001b[0m: cannot import name 'CTAPerf' from 'gammapy.irf' (/Users/deil/work/code/gammapy-docs/build/dev/gammapy/gammapy/irf/__init__.py)"
]
}
],
"source": [
"from gammapy.irf import CTAPerf\n",
"from gammapy.spectrum import SensitivityEstimator"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load IRFs\n",
"\n",
"First load the CTA IRFs."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'CTAPerf' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mfilename\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"$GAMMAPY_EXTRA/datasets/cta/perf_prod2/point_like_non_smoothed/South_5h.fits.gz\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mirf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mCTAPerf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'CTAPerf' is not defined"
]
}
],
"source": [
"filename = \"$GAMMAPY_EXTRA/datasets/cta/perf_prod2/point_like_non_smoothed/South_5h.fits.gz\"\n",
"irf = CTAPerf.read(filename)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compute sensitivity\n",
"\n",
"Choose a few parameters, then run the sentitivity computation."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'SensitivityEstimator' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msensitivity_estimator\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSensitivityEstimator\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mirf\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mirf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlivetime\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"5h\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0msensitivity_estimator\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'SensitivityEstimator' is not defined"
]
}
],
"source": [
"sensitivity_estimator = SensitivityEstimator(irf=irf, livetime=\"5h\")\n",
"sensitivity_estimator.run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Results\n",
"\n",
"The results are given as an Astropy table."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'sensitivity_estimator' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Show the results table\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0msensitivity_estimator\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresults_table\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'sensitivity_estimator' is not defined"
]
}
],
"source": [
"# Show the results table\n",
"sensitivity_estimator.results_table"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Save it to file (could use e.g. format of CSV or ECSV or FITS)\n",
"# sensitivity_estimator.results_table.write('sensitivity.ecsv', format='ascii.ecsv')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'sensitivity_estimator' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Plot the sensitivity curve\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msensitivity_estimator\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresults_table\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mis_s\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"criterion\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"significance\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m plt.plot(\n",
"\u001b[0;31mNameError\u001b[0m: name 'sensitivity_estimator' is not defined"
]
}
],
"source": [
"# Plot the sensitivity curve\n",
"t = sensitivity_estimator.results_table\n",
"\n",
"is_s = t[\"criterion\"] == \"significance\"\n",
"plt.plot(\n",
" t[\"energy\"][is_s],\n",
" t[\"e2dnde\"][is_s],\n",
" \"s-\",\n",
" color=\"red\",\n",
" label=\"significance\",\n",
")\n",
"\n",
"is_g = t[\"criterion\"] == \"gamma\"\n",
"plt.plot(\n",
" t[\"energy\"][is_g], t[\"e2dnde\"][is_g], \"*-\", color=\"blue\", label=\"gamma\"\n",
")\n",
"\n",
"plt.loglog()\n",
"plt.xlabel(\"Energy ({})\".format(t[\"energy\"].unit))\n",
"plt.ylabel(\"Sensitivity ({})\".format(t[\"e2dnde\"].unit))\n",
"plt.legend();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercises\n",
"\n",
"* Also compute the sensitivity for a 20 hour observation\n",
"* Compare how the sensitivity differs between 5 and 20 hours by plotting the ratio as a function of energy."
]
},
{
"cell_type": "code",
"execution_count": 8,
"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.0"
},
"nbsphinx": {
"orphan": true
}
},
"nbformat": 4,
"nbformat_minor": 2
}