Background models¶
The naming of the models in this section follows the convention from Overview.
The documentation on how to produce background models in Gammapy is available at Make background models.
BACKGROUND_3D¶
BACKGROUND_3D is a background rate 3D cube (X, Y, energy) in units of per energy, per time, per solid angle. X and Y are given in detector coordinates (DETX, DETY), a.k.a. nominal system. This is a tangential system to the instrument during observations.
Two classes are used as container for this model:
The
FOVCube
class is used as base container for cubes. It has generic methods to I/O (read/write) and operate the 3D cubes. It also has visualization methods to plot slices/bins of the cubes.The
FOVCubeBackgroundModel
class is used to contain and handle cube background models. It contains 3 cubes of typeFOVCube
:counts_cube
- counts (a.k.a. events) used to fill the model.livetime_cube
- livetime correction used for the model.background_cube
- background model (rate)
The class also defines usefull methods to produce the models, such as define binning, fill (histogram) the model or smooth.
Two test files are located in the gammapy-extra
repository as
examples and test benches of these classes:
- bg_cube_model_test1.fits is a
FOVCube
produced with an older version ofmake_test_bg_cube_model
, using a simplified background model. The current version of the mehod produces aFOVCubeBackgroundModel
object. - bg_cube_model_test2.fits.gz is a
FOVCubeBackgroundModel
produced withmake_bg_cube_model
, using dummy data produced withmake_test_dataset
.
An example script of how to read/write the cubes from file and
perform some simple plots is given in the examples
directory:
example_plot_background_model.py
"""Plot cube background model and store it in fits.
The 'image' format file can be viewed with ds9.
"""
import matplotlib.pyplot as plt
from astropy.units import Quantity
from astropy.coordinates import Angle
from gammapy.background import FOVCube
from gammapy.datasets import gammapy_extra
filename = gammapy_extra.filename('test_datasets/background/bg_cube_model_test1.fits')
bg_cube_model = FOVCube.read(filename, format='table', scheme='bg_cube')
bg_cube_model.plot_image(energy=Quantity(2., 'TeV'))
bg_cube_model.plot_spectrum(coord=Angle([0., 0.], 'degree'))
outname = 'cube_background_model'
bg_cube_model.write('{}_bin_table.fits'.format(outname), format='table', clobber=True)
bg_cube_model.write('{}_image.fits'.format(outname), format='image', clobber=True)
plt.show()
The data of the cube can be accessed via:
energy_bin = bg_cube_model.find_energy_bin(energy=Quantity(2., 'TeV'))
det_bin = bg_cube_model.find_det_bin(det=Angle([0., 0.], 'degree'))
bg_cube_model.background[energy_bin, det_bin[1], det_bin[0]]
More complex plots can be easily produced with a few lines of code:
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from astropy.units import Quantity
from astropy.coordinates import Angle
from gammapy.background import FOVCube
from gammapy.datasets import gammapy_extra
filename = gammapy_extra.filename('test_datasets/background/bg_cube_model_test1.fits')
bg_cube_model = FOVCube.read(filename, format='table', scheme='bg_cube', hdu='BACKGROUND')
fig, axes = plt.subplots(nrows=1, ncols=3)
fig.set_size_inches(16, 5., forward=True)
# plot images
bg_cube_model.plot_image(energy=Quantity(0.5, 'TeV'), ax=axes[0],
style_kwargs=dict(norm=LogNorm()))
bg_cube_model.plot_image(energy=Quantity(50., 'TeV'), ax=axes[1],
style_kwargs=dict(norm=LogNorm()))
# plot spectra
bg_cube_model.plot_spectrum(coord=Angle([0., 0.], 'degree'), ax=axes[2],
style_kwargs=dict(label='(0, 0) deg'))
bg_cube_model.plot_spectrum(coord=Angle([2., 2.], 'degree'), ax=axes[2],
style_kwargs=dict(label='(2, 2) deg'))
axes[2].set_title('')
axes[2].legend()
plt.tight_layout()
plt.show()
(Source code, png, hires.png, pdf)