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.
Start by importing the gammalib, ctools, and cscripts Python modules.
In [1]:
import gammalib
import ctools
import cscripts
In [2]:
f = open('pnt.def', 'wb')
f.write('name,ra,dec,duration\n')
f.write('Crab,83.63,21.51,1800.0\n')
f.write('Crab,83.63,22.51,1800.0\n')
f.close()
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.
In [3]:
def peek(filename):
f = open(gammalib.expand_env(filename), 'r')
for line in f:
print(line.rstrip())
f.close()
peek('pnt.def')
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.
Now transform the pointing definition file into an observation definition XML file using the csobsdef script.
In [4]:
obsdef = cscripts.csobsdef()
obsdef['inpnt'] = 'pnt.def'
obsdef['outobs'] = 'obs.xml'
obsdef['caldb'] = 'prod2'
obsdef['irf'] = 'South_0.5h'
obsdef.execute()
Let's peek the resulting observation definition XML file
In [5]:
peek('obs.xml')
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.
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.
Feed now the observation definition XML file into the ctobssim tool to simulate the event data.
In [6]:
obssim = ctools.ctobssim()
obssim['inobs'] = 'obs.xml'
obssim['rad'] = 5.0
obssim['emin'] = 0.1
obssim['emax'] = 100.0
obssim['inmodel'] = '$CTOOLS/share/models/crab_2bkg.xml'
obssim['outevents'] = 'obs_2bkg.xml'
obssim.execute()
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
.
In [7]:
peek('$CTOOLS/share/models/crab_2bkg.xml')
In [8]:
like = ctools.ctlike()
like['inobs'] = 'obs_2bkg.xml'
like['inmodel'] = '$CTOOLS/share/models/crab_2bkg.xml'
like['outmodel'] = 'crab_results.xml'
like.run()
and inspect the model fitting results
In [9]:
print(like.obs().models())
Obviously the two background models have different best fitting spectral parameters which correspond to the simulated values (see above).