31 Generates background model
39 self._init_csobservation(self.__class__.__name__, ctools.__version__, argv)
51 Extend ctools.csobservation getstate method to include some members
59 state = {
'base' : ctools.csobservation.__getstate__(self),
69 Extend ctools.csobservation setstate method to include some members
76 ctools.csobservation.__setstate__(self, state[
'base'])
87 Get parameters from parfile
91 self._setup_observations(self.obs(),
True,
True,
True)
97 spatial = self[
'spatial'].string()
98 if spatial ==
'GAUSS(E)':
99 snumbins = self[
'snumbins'].integer()
103 if spatial ==
'LOOKUP':
104 self[
'slufile'].filename()
105 if spatial ==
'LOOKUP' or spatial ==
'GAUSS' or \
106 spatial ==
'GAUSS(E)' or spatial ==
'PROFILE' or \
107 spatial ==
'POLYNOM':
108 self[
'gradient'].boolean()
109 spectral = self[
'spectral'].string()
110 if spectral ==
'NODES':
111 self._create_energies()
112 self[
'runwise'].boolean()
118 if self._read_ahead():
119 self[
'outmodel'].filename()
122 self._log_parameters(gammalib.TERSE)
131 Extracts the instrument name from the observation container. If there
132 are multiple instruments in the container then use the "instrument"
133 parameter to set the instrument name.
144 for obs
in self.obs():
146 instrument = obs.instrument()
147 elif instrument != obs.instrument():
148 instrument = self[
'instrument'].string()
156 Generate initial background model
160 sigma_min : float, optional
162 sigma_max : float, optional
167 model : `~gammalib.GModelData()`
171 if self[
'spatial'].string() ==
'IRF':
172 epivot = gammalib.GEnergy(1.0,
'TeV')
173 spectral = gammalib.GModelSpectralPlaw(1.0, 0.0, epivot)
174 model = gammalib.GCTAModelIrfBackground(spectral)
177 elif self[
'spatial'].string() ==
'AEFF':
178 epivot = gammalib.GEnergy(1.0,
'TeV')
179 spectral = gammalib.GModelSpectralPlaw(1.0e-13, -2.5, epivot)
180 model = gammalib.GCTAModelAeffBackground(spectral)
186 if self[
'spatial'].string() ==
'LOOKUP':
189 factor1 = gammalib.GCTAModelSpatialLookup(self[
'slufile'].filename())
192 elif self[
'spatial'].string() ==
'GAUSS':
195 factor1 = gammalib.GCTAModelRadialGauss(3.0)
196 factor1[
'Sigma'].min(sigma_min)
197 factor1[
'Sigma'].max(sigma_max)
200 elif self[
'spatial'].string() ==
'GAUSS(E)':
205 if self[
'snumbins'].integer() == 1:
206 factor1 = gammalib.GCTAModelRadialGauss(3.0)
207 factor1[
'Sigma'].min(sigma_min)
208 factor1[
'Sigma'].max(sigma_max)
210 emin = gammalib.GEnergy(self[
'smin'].real(),
'TeV')
211 emax = gammalib.GEnergy(self[
'smax'].real(),
'TeV')
212 energies = gammalib.GEnergies(self[
'snumbins'].integer(), emin, emax)
213 spectrum = gammalib.GModelSpectralConst(3.0)
214 nodes = gammalib.GModelSpectralNodes(spectrum, energies)
215 for i
in range(nodes.nodes()):
216 nodes[i*2+1].min(sigma_min)
217 nodes[i*2+1].max(sigma_max)
219 factor1 = gammalib.GCTAModelSpatialGaussSpectrum(nodes)
222 elif self[
'spatial'].string() ==
'PROFILE':
225 factor1 = gammalib.GCTAModelRadialProfile(2.0, 4.0, 5.0)
226 factor1[
'Width'].min(1.0)
227 factor1[
'Width'].max(10.0)
228 factor1[
'Core'].min(1.0)
229 factor1[
'Core'].max(10.0)
230 factor1[
'Tail'].min(1.0)
231 factor1[
'Tail'].max(10.0)
232 factor1[
'Tail'].free()
235 elif self[
'spatial'].string() ==
'POLYNOM':
238 factor1 = gammalib.GCTAModelRadialPolynom([1.0, -0.1, +0.1])
239 factor1[
'Coeff0'].min(0.1)
240 factor1[
'Coeff0'].max(10.0)
241 factor1[
'Coeff0'].fix()
242 factor1[
'Coeff1'].min(-10.0)
243 factor1[
'Coeff1'].max(10.0)
244 factor1[
'Coeff2'].min(-10.0)
245 factor1[
'Coeff2'].max(10.0)
252 if self[
'gradient'].boolean():
253 spatial = gammalib.GCTAModelSpatialMultiplicative()
254 factor2 = gammalib.GCTAModelSpatialGradient()
255 spatial.append(factor1)
256 spatial.append(factor2)
261 epivot = gammalib.GEnergy(1.0,
'TeV')
262 spectral = gammalib.GModelSpectralPlaw(3.0e-4, -1.5, epivot)
263 spectral[
'Prefactor'].min(1.0e-8)
266 model = gammalib.GCTAModelBackground(spatial, spectral)
269 if model
is not None:
270 model.name(
'Background')
278 Select events within a given RoI radius
282 obs : `~gammalib.GObservations()`
283 Observation container
287 obs : `~gammalib.GObservations()`
288 Observation container
291 select = ctools.ctselect(obs)
292 select[
'ra'] =
'UNDEF'
293 select[
'dec'] =
'UNDEF'
294 select[
'rad'] = self[
'rad'].real()
295 select[
'tmin'] =
'UNDEF'
296 select[
'tmax'] =
'UNDEF'
297 select[
'emin'] = self[
'emin'].real()
298 select[
'emax'] = self[
'emax'].real()
299 select[
'usethres'] =
'DEFAULT'
305 obs = select.obs().copy()
312 Replace spectral model by node function
316 model : `~gammalib.GModelData()`
317 Input background model
321 model : `~gammalib.GModelData()`
322 Background model with spectral node function
325 spectrum = model.spectral()
328 energies = self._create_energies()
331 nodes = gammalib.GModelSpectralNodes(spectrum, energies)
335 for i
in range(nodes.size()):
336 nodes[i].min(1.0e-10*nodes[i].value())
339 model.spectral(nodes)
346 Generate background models
350 obs : `~gammalib.GObservations()`
351 Observations container
355 model : `~gammalib.GModelData()`
356 Background model component
359 self._log_header3(gammalib.EXPLICIT,
'Select events from observation')
365 self._log_header3(gammalib.EXPLICIT,
'Generate initial background model')
371 models = gammalib.GModels()
376 self._log_header3(gammalib.EXPLICIT,
'Fit initial background model')
379 like = ctools.ctlike(obs)
386 model = like.obs().
models()[0].copy()
389 if self[
'spectral'].string() ==
'NODES':
395 models = gammalib.GModels()
400 self._log_header3(gammalib.EXPLICIT,
'Fit nodes background model')
403 like = ctools.ctlike(obs)
410 model = like.obs().
models()[0].copy()
414 spectral = model.spectral()
415 nodes = spectral.nodes()
416 for i
in range(nodes):
417 iint = 2*(nodes - i) - 1
418 if spectral[iint].error() == 0.0:
419 spectral.remove(nodes - i - 1)
422 self._log_string(gammalib.EXPLICIT, str(opt))
429 Generate background models
432 for run
in self.obs():
435 self._log_header2(gammalib.TERSE, self._get_obs_header(run))
438 obs = gammalib.GObservations()
445 model.name((
'Background_%s' % run.id()))
449 self._log_string(gammalib.NORMAL, str(model))
452 self._models.append(model)
467 self._log_observations(gammalib.NORMAL, self.obs(),
'Input observation')
471 for i
in range(obs.size()-1, -1, -1):
476 self._log_value(gammalib.NORMAL,
'Obs matching instrument', self.obs().size())
479 self._log_header1(gammalib.TERSE,
'Generate background models')
485 if self[
'runwise'].boolean():
489 self._models.append(model)
492 self._log_models(gammalib.NORMAL, self.
_models,
'Output model')
502 self._log_header1(gammalib.TERSE,
'Save models')
505 outmodel = self[
'outmodel'].filename()
508 self._log_value(gammalib.NORMAL,
'Models file', outmodel.url())
511 self._models.save(outmodel)
518 Return background models
522 ebounds : `~gammalib.GModels()`
532 if __name__ ==
'__main__':
def _generate_initial_model
def _generate_runwise_bkg