Commit c00513ceb572725464f40b10648024fd01c005d5

Authored by Jürgen Knödlseder
1 parent d8f2d169

Replace spectral model by file function in csspec (#3624)

ChangeLog
@@ -3,6 +3,8 @@ @@ -3,6 +3,8 @@
3 * Bug fix version 1.7.4 released 3 * Bug fix version 1.7.4 released
4 ================================ 4 ================================
5 5
  6 + Replace spectral model by file function in csspec (#3624)
  7 +
6 8
7 2020-12-02 9 2020-12-02
8 10
@@ -256,7 +256,10 @@ None @@ -256,7 +256,10 @@ None
256 256
257 csspec - Generates Spectral Energy Distribution 257 csspec - Generates Spectral Energy Distribution
258 ----------------------------------------------- 258 -----------------------------------------------
259 -None 259 +The script now transforms the spectral model of the source of interest into a
  260 +file function, using the model parameters specified in the input model. This
  261 +allows to determine spectral energy distributions for combined spectral models
  262 +(#3624).
260 263
261 264
262 cssrcdetect - Detects sources in sky map 265 cssrcdetect - Detects sources in sky map
configure.ac
@@ -54,7 +54,7 @@ AC_CONFIG_HEADER([config.h]) @@ -54,7 +54,7 @@ AC_CONFIG_HEADER([config.h])
54 # 5. If any interfaces have been removed or changed since the last public # 54 # 5. If any interfaces have been removed or changed since the last public #
55 # release, then set age to 0. # 55 # release, then set age to 0. #
56 ############################################################################# 56 #############################################################################
57 -CTOOLS_LT_VERSION="7:1:0" 57 +CTOOLS_LT_VERSION="7:2:0"
58 AC_SUBST(CTOOLS_LT_VERSION) 58 AC_SUBST(CTOOLS_LT_VERSION)
59 59
60 60
cscripts/csspec.py
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 # ========================================================================== 2 # ==========================================================================
3 # Generates a spectrum. 3 # Generates a spectrum.
4 # 4 #
5 -# Copyright (C) 2014-2020 Michael Mayer 5 +# Copyright (C) 2014-2021 Michael Mayer
6 # 6 #
7 # This program is free software: you can redistribute it and/or modify 7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by 8 # it under the terms of the GNU General Public License as published by
@@ -20,6 +20,7 @@ @@ -20,6 +20,7 @@
20 # ========================================================================== 20 # ==========================================================================
21 import sys 21 import sys
22 import math 22 import math
  23 +import tempfile # Kludge for file function generation
23 import gammalib 24 import gammalib
24 import ctools 25 import ctools
25 from cscripts import mputils 26 from cscripts import mputils
@@ -366,6 +367,24 @@ class csspec(ctools.csobservation): @@ -366,6 +367,24 @@ class csspec(ctools.csobservation):
366 ' Fixing "'+par.name()+'"') 367 ' Fixing "'+par.name()+'"')
367 par.fix() 368 par.fix()
368 369
  370 + # Kludge: convert spectral model into a file function
  371 + nbins = self._ebounds.size()
  372 + num = nbins * 20
  373 + energies = gammalib.GEnergies(num, self._ebounds.emin(0),
  374 + self._ebounds.emax(nbins-1))
  375 + _, filename = tempfile.mkstemp()
  376 + f = open(filename, 'wb')
  377 + spectral = model.spectral()
  378 + for energy in energies:
  379 + value = spectral.eval(energy)
  380 + f.write('%20.6f %20e\n' % (energy.MeV(), value))
  381 + f.close()
  382 + spectral = gammalib.GModelSpectralFunc(filename, 1.0)
  383 + model.spectral(spectral)
  384 + self._log_string(gammalib.EXPLICIT, ' Converting spectral model '
  385 + 'into file function via temporary file "%s"' %
  386 + filename)
  387 +
369 # Free the normalisation parameter which is assumed to be 388 # Free the normalisation parameter which is assumed to be
370 # the first spectral parameter 389 # the first spectral parameter
371 normpar = model.spectral()[0] 390 normpar = model.spectral()[0]
@@ -895,6 +914,7 @@ class csspec(ctools.csobservation): @@ -895,6 +914,7 @@ class csspec(ctools.csobservation):
895 result['dloglike'] = dlogL 914 result['dloglike'] = dlogL
896 result['logL'] = loglike 915 result['logL'] = loglike
897 916
  917 + # Compute upper flux limit
898 if self['calc_ulim'].boolean(): 918 if self['calc_ulim'].boolean():
899 919
900 # Logging information 920 # Logging information
@@ -1009,7 +1029,7 @@ class csspec(ctools.csobservation): @@ -1009,7 +1029,7 @@ class csspec(ctools.csobservation):
1009 if (self._method != 'NODES') or (not self['dll_freenodes'].boolean()): 1029 if (self._method != 'NODES') or (not self['dll_freenodes'].boolean()):
1010 for par in spectral: 1030 for par in spectral:
1011 par.fix() 1031 par.fix()
1012 - 1032 +
1013 # Re-compute the log-likelihood 1033 # Re-compute the log-likelihood
1014 like.run() 1034 like.run()
1015 loglike = like.obs().logL() 1035 loglike = like.obs().logL()
@@ -1130,7 +1150,7 @@ class csspec(ctools.csobservation): @@ -1130,7 +1150,7 @@ class csspec(ctools.csobservation):
1130 table.append(dnde) 1150 table.append(dnde)
1131 table.append(TSvalues) 1151 table.append(TSvalues)
1132 table.append(Npred_values) 1152 table.append(Npred_values)
1133 - 1153 +
1134 # Define the SED type 1154 # Define the SED type
1135 table.card('SED_TYPE', 'norm,e2dnde,dnde,npred', 'SED type') 1155 table.card('SED_TYPE', 'norm,e2dnde,dnde,npred', 'SED type')
1136 1156
doc/source/admin/release_history/1.7.4.rst
@@ -10,3 +10,5 @@ ctools 1.7.4 is a bug fix release for ctools release 1.7. @@ -10,3 +10,5 @@ ctools 1.7.4 is a bug fix release for ctools release 1.7.
10 10
11 The following bugs were fixed: 11 The following bugs were fixed:
12 12
  13 +* [`3624 <https://cta-redmine.irap.omp.eu/issues/3624>`_] -
  14 + Replace spectral model by file function in :ref:`csspec`
doc/source/users/reference_manual/csspec.rst
@@ -20,14 +20,24 @@ flux limit that is particularly useful in case that the source is not @@ -20,14 +20,24 @@ flux limit that is particularly useful in case that the source is not
20 significantly detected within a spectral bin (hidden parameter ``calc_ulim``). 20 significantly detected within a spectral bin (hidden parameter ``calc_ulim``).
21 21
22 There are two fundamental methods to run the script depending on the value of 22 There are two fundamental methods to run the script depending on the value of
23 -the ``method`` parameter. If ``method=SLICE`` the energy interval defined by the  
24 -``emin`` and ``emax`` parameters is divided into a number of energy bins and an  
25 -independent maximum likelihood fit is performed in each of the energy bins,  
26 -while if ``method=NODES`` the spectral model will be replaced by a node function 23 +the ``method`` parameter, ``SLICE`` and ``NODES``.
  24 +
  25 +If ``method=SLICE`` the energy interval defined by the ``emin`` and ``emax``
  26 +parameters is divided into a number of energy bins and an independent maximum
  27 +likelihood fit is performed in each of the energy bins. In order to cope with
  28 +all kinds of spectral models, including composite spectra, input spectrum will
  29 +be converted into a file function, with the normalisation of the file function
  30 +being the fit parameter for each energy bin. This implies that spectral
  31 +components are merged into a single function using the model parameters that
  32 +are specified in the input model.
  33 +
  34 +If ``method=NODES`` the spectral model will be replaced by a node function
27 that will be fit to all data. The latter is particularly useful if non-CTA data 35 that will be fit to all data. The latter is particularly useful if non-CTA data
28 -should be fitted such as data from Fermi/LAT or COMPTEL. By default, ``method`` is  
29 -set to ``AUTO`` which will automatically select the method based on the input data.  
30 -For CTA-only observations ``SLICE`` will be used, otherwise ``NODES`` will be used. 36 +should be fitted such as data from Fermi/LAT or COMPTEL.
  37 +
  38 +By default, ``method`` is set to ``AUTO`` which will automatically select the
  39 +method based on the input data. For CTA-only observations ``SLICE`` will be used,
  40 +otherwise ``NODES`` will be used.
31 41
32 The spectral binning is either defined by a FITS file containing the energy 42 The spectral binning is either defined by a FITS file containing the energy
33 boundaries of each bin (option ``ebinalg=FILE``) or as ``enumbins`` bins spread 43 boundaries of each bin (option ``ebinalg=FILE``) or as ``enumbins`` bins spread
@@ -54,9 +64,6 @@ the fitted flux and flux error, the Test Statistics value (option @@ -54,9 +64,6 @@ the fitted flux and flux error, the Test Statistics value (option
54 ``calc_ts=yes``), the upper flux limit (option ``calc_ulim=yes``) and the 64 ``calc_ts=yes``), the upper flux limit (option ``calc_ulim=yes``) and the
55 predicted number of events (only for unbinned data). 65 predicted number of events (only for unbinned data).
56 66
57 -.. warning::  
58 - The upper limit computation is not yet implemented for the ``NODES`` method.  
59 -  
60 67
61 General parameters 68 General parameters
62 ------------------ 69 ------------------