Commit e2f4606774eab4b8b17636851f978e7c74b202e2

Authored by Jürgen Knödlseder
1 parent b7130ada

Fix csspec

cscripts/csspec.py
... ... @@ -84,6 +84,9 @@ class csspec(ctools.cscript):
84 84 # Query source name
85 85 self['srcname'].string()
86 86  
  87 + # Get spectrum generation method
  88 + self._method = self['method'].string()
  89 +
87 90 # Collect number of unbinned, binned and On/Off observations in
88 91 # observation container
89 92 n_unbinned = 0
... ... @@ -100,28 +103,31 @@ class csspec(ctools.cscript):
100 103 n_cta = n_unbinned + n_binned + n_onoff
101 104 n_other = self._obs.size() - n_cta
102 105  
103   - # Set script mode according to CTA observations
104   - if n_unbinned == 0 and n_binned != 0 and n_onoff == 0:
105   - self._binned_mode = True
106   - elif n_unbinned == 0 and n_binned == 0 and n_onoff != 0:
107   - self._onoff_mode = True
108   - elif n_unbinned == 0 and n_binned != 0 and n_onoff != 0:
109   - msg = 'Mix of binned and On/Off CTA observations found in ' \
110   - 'observation container. csscript does not support this ' \
111   - 'mix.'
112   - raise RuntimeError(msg)
113   - elif n_unbinned != 0 and (n_binned != 0 or n_onoff != 0):
114   - msg = 'Mix of unbinned and binned or On/Off CTA observations ' \
115   - 'found in observation container. csscript does not support ' \
116   - 'this mix.'
117   - raise RuntimeError(msg)
  106 + # If spectrum method is not "NODES" then set spectrum method and
  107 + # script mode according to type of CTA observations
  108 + if self._method != 'NODES':
  109 + if n_unbinned == 0 and n_binned != 0 and n_onoff == 0:
  110 + self._binned_mode = True
  111 + self._method = 'SLICE'
  112 + elif n_unbinned == 0 and n_binned == 0 and n_onoff != 0:
  113 + self._onoff_mode = True
  114 + self._method = 'SLICE'
  115 + elif n_unbinned == 0 and n_binned != 0 and n_onoff != 0:
  116 + msg = 'Mix of binned and On/Off CTA observations found in ' \
  117 + 'observation container. csscript does not support this ' \
  118 + 'mix.'
  119 + raise RuntimeError(msg)
  120 + elif n_unbinned != 0 and (n_binned != 0 or n_onoff != 0):
  121 + msg = 'Mix of unbinned and binned or On/Off CTA observations ' \
  122 + 'found in observation container. csscript does not support ' \
  123 + 'this mix.'
  124 + raise RuntimeError(msg)
  125 + elif n_unbinned != 0:
  126 + self._method = 'SLICE'
118 127  
119 128 # Set ebounds
120 129 self._set_ebounds()
121 130  
122   - # Get method
123   - self._method = self['method'].string()
124   -
125 131 # Query other parameeters
126 132 self['edisp'].boolean()
127 133 self['calc_ulim'].boolean()
... ...
doc/source/users/tutorials/1dc/first_fitting.rst
... ... @@ -151,10 +151,11 @@ You create the spectrum for the first source using
151 151 Input background cube file (only needed for stacked analysis) [NONE] bkgcube.fits
152 152 Input model definition XML file [$CTOOLS/share/models/crab.xml] stacked_results.xml
153 153 Source name [Crab] Src001
  154 + Spectrum generation method (SLICE|NODES|AUTO) [AUTO]
154 155 Binning algorithm (LIN|LOG|FILE) [LOG]
155 156 Lower energy limit (TeV) [0.1]
156 157 Upper energy limit (TeV) [100.0]
157   - Number of energy bins (0=unbinned) [20] 10
  158 + Number of energy bins [20] 10
158 159 Output spectrum file [spectrum.fits] spectrum_src001.fits
159 160  
160 161 and for the second source using
... ... @@ -168,10 +169,11 @@ and for the second source using
168 169 Input background cube file (only needed for stacked analysis) [bkgcube.fits]
169 170 Input model definition XML file [stacked_results.xml]
170 171 Source name [Src001] Src002
  172 + Spectrum generation method (SLICE|NODES|AUTO) [AUTO]
171 173 Binning algorithm (LIN|LOG|FILE) [LOG]
172 174 Lower energy limit (TeV) [0.1]
173 175 Upper energy limit (TeV) [100.0]
174   - Number of energy bins (0=unbinned) [10]
  176 + Number of energy bins [10]
175 177 Output spectrum file [spectrum_src001.fits] spectrum_src002.fits
176 178  
177 179 The :ref:`csspec` script divided here the data into ten logarithmically
... ...
examples/make_spectrum.py
1 1 #! /usr/bin/env python
2 2 # ==========================================================================
3   -# Makes spectrum and optionally plot it
  3 +# Makes and optionally displays spectrum
4 4 #
5   -# Copyright (C) 2014-2016 Juergen Knoedlseder
  5 +# Copyright (C) 2014-2017 Juergen Knoedlseder
6 6 #
7 7 # This program is free software: you can redistribute it and/or modify
8 8 # it under the terms of the GNU General Public License as published by
... ... @@ -72,6 +72,7 @@ def create_spectrum(datadir):
72 72 # Create spectrum
73 73 spec = cscripts.csspec(sim.obs())
74 74 spec['srcname'] = 'Crab'
  75 + spec['method'] = 'AUTO'
75 76 spec['outfile'] = 'example_spectrum.fits'
76 77 spec['expcube'] = 'NONE'
77 78 spec['psfcube'] = 'NONE'
... ...
test/test_csspec.py
... ... @@ -80,7 +80,7 @@ class Test(test):
80 80 # Setup csspec command
81 81 cmd = csspec+' inobs="'+self._events+'"'+ \
82 82 ' inmodel="'+self._model+'"'+ \
83   - ' srcname="Crab"'+ \
  83 + ' srcname="Crab" method="AUTO"'+ \
84 84 ' caldb="'+self._caldb+'" irf="'+self._irf+'"'+ \
85 85 ' ebinalg="LOG" enumbins=5 emin=0.1 emax=100.0'+ \
86 86 ' outfile="csspec_cmd1.fits"'+ \
... ... @@ -100,7 +100,7 @@ class Test(test):
100 100 # Setup csspec command
101 101 cmd = csspec+' inobs="input_file_that_does_not_exist.fits"'+ \
102 102 ' inmodel="'+self._model+'"'+ \
103   - ' srcname="Crab"'+ \
  103 + ' srcname="Crab" method="AUTO"'+ \
104 104 ' caldb="'+self._caldb+'" irf="'+self._irf+'"'+ \
105 105 ' ebinalg="LOG" enumbins=5 emin=0.1 emax=100.0'+ \
106 106 ' outfile="csspec_cmd2.fits"'+ \
... ... @@ -129,6 +129,7 @@ class Test(test):
129 129 spec['caldb'] = self._caldb
130 130 spec['irf'] = self._irf
131 131 spec['outfile'] = 'csspec_py1.fits'
  132 + spec['method'] = 'AUTO'
132 133 spec['ebinalg'] = 'LOG'
133 134 spec['enumbins'] = 5
134 135 spec['emin'] = 0.1
... ... @@ -155,6 +156,7 @@ class Test(test):
155 156 spec['inmodel'] = self._model
156 157 spec['srcname'] = 'Crab'
157 158 spec['outfile'] = 'csspec_py2.fits'
  159 + spec['method'] = 'AUTO'
158 160 spec['ebinalg'] = 'LOG'
159 161 spec['enumbins'] = 2
160 162 spec['emin'] = 0.1
... ... @@ -175,6 +177,7 @@ class Test(test):
175 177 spec['inmodel'] = self._onoff_model
176 178 spec['srcname'] = 'Crab'
177 179 spec['outfile'] = 'csspec_py3.fits'
  180 + spec['method'] = 'AUTO'
178 181 spec['ebinalg'] = 'LOG'
179 182 spec['enumbins'] = 10
180 183 spec['emin'] = 0.1
... ... @@ -189,6 +192,27 @@ class Test(test):
189 192 # Check result file
190 193 self._check_result_file('csspec_py3.fits', 10)
191 194  
  195 + # Set-up On/Off csspec with NODES method
  196 + spec = cscripts.csspec()
  197 + spec['inobs'] = self._inonoff
  198 + spec['inmodel'] = self._onoff_model
  199 + spec['srcname'] = 'Crab'
  200 + spec['outfile'] = 'csspec_py4.fits'
  201 + spec['method'] = 'NODES'
  202 + spec['ebinalg'] = 'LOG'
  203 + spec['enumbins'] = 10
  204 + spec['emin'] = 0.1
  205 + spec['emax'] = 10.0
  206 + spec['logfile'] = 'csspec_py4.log'
  207 + spec['chatter'] = 4
  208 + spec['publish'] = False
  209 +
  210 + # Execute csspec script
  211 + spec.execute()
  212 +
  213 + # Check result file
  214 + self._check_result_file('csspec_py4.fits', 10)
  215 +
192 216 # Return
193 217 return
194 218  
... ...