From e2f4606774eab4b8b17636851f978e7c74b202e2 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Wed, 18 Oct 2017 21:33:23 +0200 Subject: [PATCH] Fix csspec --- cscripts/csspec.py | 42 ++++++++++++++++++++++++------------------ doc/source/users/tutorials/1dc/first_fitting.rst | 6 ++++-- examples/make_spectrum.py | 5 +++-- test/test_csspec.py | 28 ++++++++++++++++++++++++++-- 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/cscripts/csspec.py b/cscripts/csspec.py index 014ad6a..acb7d61 100755 --- a/cscripts/csspec.py +++ b/cscripts/csspec.py @@ -84,6 +84,9 @@ class csspec(ctools.cscript): # Query source name self['srcname'].string() + # Get spectrum generation method + self._method = self['method'].string() + # Collect number of unbinned, binned and On/Off observations in # observation container n_unbinned = 0 @@ -100,28 +103,31 @@ class csspec(ctools.cscript): n_cta = n_unbinned + n_binned + n_onoff n_other = self._obs.size() - n_cta - # Set script mode according to CTA observations - if n_unbinned == 0 and n_binned != 0 and n_onoff == 0: - self._binned_mode = True - elif n_unbinned == 0 and n_binned == 0 and n_onoff != 0: - self._onoff_mode = True - elif n_unbinned == 0 and n_binned != 0 and n_onoff != 0: - msg = 'Mix of binned and On/Off CTA observations found in ' \ - 'observation container. csscript does not support this ' \ - 'mix.' - raise RuntimeError(msg) - elif n_unbinned != 0 and (n_binned != 0 or n_onoff != 0): - msg = 'Mix of unbinned and binned or On/Off CTA observations ' \ - 'found in observation container. csscript does not support ' \ - 'this mix.' - raise RuntimeError(msg) + # If spectrum method is not "NODES" then set spectrum method and + # script mode according to type of CTA observations + if self._method != 'NODES': + if n_unbinned == 0 and n_binned != 0 and n_onoff == 0: + self._binned_mode = True + self._method = 'SLICE' + elif n_unbinned == 0 and n_binned == 0 and n_onoff != 0: + self._onoff_mode = True + self._method = 'SLICE' + elif n_unbinned == 0 and n_binned != 0 and n_onoff != 0: + msg = 'Mix of binned and On/Off CTA observations found in ' \ + 'observation container. csscript does not support this ' \ + 'mix.' + raise RuntimeError(msg) + elif n_unbinned != 0 and (n_binned != 0 or n_onoff != 0): + msg = 'Mix of unbinned and binned or On/Off CTA observations ' \ + 'found in observation container. csscript does not support ' \ + 'this mix.' + raise RuntimeError(msg) + elif n_unbinned != 0: + self._method = 'SLICE' # Set ebounds self._set_ebounds() - # Get method - self._method = self['method'].string() - # Query other parameeters self['edisp'].boolean() self['calc_ulim'].boolean() diff --git a/doc/source/users/tutorials/1dc/first_fitting.rst b/doc/source/users/tutorials/1dc/first_fitting.rst index 0ac982a..e29fcb6 100644 --- a/doc/source/users/tutorials/1dc/first_fitting.rst +++ b/doc/source/users/tutorials/1dc/first_fitting.rst @@ -151,10 +151,11 @@ You create the spectrum for the first source using Input background cube file (only needed for stacked analysis) [NONE] bkgcube.fits Input model definition XML file [$CTOOLS/share/models/crab.xml] stacked_results.xml Source name [Crab] Src001 + Spectrum generation method (SLICE|NODES|AUTO) [AUTO] Binning algorithm (LIN|LOG|FILE) [LOG] Lower energy limit (TeV) [0.1] Upper energy limit (TeV) [100.0] - Number of energy bins (0=unbinned) [20] 10 + Number of energy bins [20] 10 Output spectrum file [spectrum.fits] spectrum_src001.fits and for the second source using @@ -168,10 +169,11 @@ and for the second source using Input background cube file (only needed for stacked analysis) [bkgcube.fits] Input model definition XML file [stacked_results.xml] Source name [Src001] Src002 + Spectrum generation method (SLICE|NODES|AUTO) [AUTO] Binning algorithm (LIN|LOG|FILE) [LOG] Lower energy limit (TeV) [0.1] Upper energy limit (TeV) [100.0] - Number of energy bins (0=unbinned) [10] + Number of energy bins [10] Output spectrum file [spectrum_src001.fits] spectrum_src002.fits The :ref:`csspec` script divided here the data into ten logarithmically diff --git a/examples/make_spectrum.py b/examples/make_spectrum.py index 29f8dca..759602f 100755 --- a/examples/make_spectrum.py +++ b/examples/make_spectrum.py @@ -1,8 +1,8 @@ #! /usr/bin/env python # ========================================================================== -# Makes spectrum and optionally plot it +# Makes and optionally displays spectrum # -# Copyright (C) 2014-2016 Juergen Knoedlseder +# Copyright (C) 2014-2017 Juergen Knoedlseder # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -72,6 +72,7 @@ def create_spectrum(datadir): # Create spectrum spec = cscripts.csspec(sim.obs()) spec['srcname'] = 'Crab' + spec['method'] = 'AUTO' spec['outfile'] = 'example_spectrum.fits' spec['expcube'] = 'NONE' spec['psfcube'] = 'NONE' diff --git a/test/test_csspec.py b/test/test_csspec.py index d1dd4ae..435b161 100755 --- a/test/test_csspec.py +++ b/test/test_csspec.py @@ -80,7 +80,7 @@ class Test(test): # Setup csspec command cmd = csspec+' inobs="'+self._events+'"'+ \ ' inmodel="'+self._model+'"'+ \ - ' srcname="Crab"'+ \ + ' srcname="Crab" method="AUTO"'+ \ ' caldb="'+self._caldb+'" irf="'+self._irf+'"'+ \ ' ebinalg="LOG" enumbins=5 emin=0.1 emax=100.0'+ \ ' outfile="csspec_cmd1.fits"'+ \ @@ -100,7 +100,7 @@ class Test(test): # Setup csspec command cmd = csspec+' inobs="input_file_that_does_not_exist.fits"'+ \ ' inmodel="'+self._model+'"'+ \ - ' srcname="Crab"'+ \ + ' srcname="Crab" method="AUTO"'+ \ ' caldb="'+self._caldb+'" irf="'+self._irf+'"'+ \ ' ebinalg="LOG" enumbins=5 emin=0.1 emax=100.0'+ \ ' outfile="csspec_cmd2.fits"'+ \ @@ -129,6 +129,7 @@ class Test(test): spec['caldb'] = self._caldb spec['irf'] = self._irf spec['outfile'] = 'csspec_py1.fits' + spec['method'] = 'AUTO' spec['ebinalg'] = 'LOG' spec['enumbins'] = 5 spec['emin'] = 0.1 @@ -155,6 +156,7 @@ class Test(test): spec['inmodel'] = self._model spec['srcname'] = 'Crab' spec['outfile'] = 'csspec_py2.fits' + spec['method'] = 'AUTO' spec['ebinalg'] = 'LOG' spec['enumbins'] = 2 spec['emin'] = 0.1 @@ -175,6 +177,7 @@ class Test(test): spec['inmodel'] = self._onoff_model spec['srcname'] = 'Crab' spec['outfile'] = 'csspec_py3.fits' + spec['method'] = 'AUTO' spec['ebinalg'] = 'LOG' spec['enumbins'] = 10 spec['emin'] = 0.1 @@ -189,6 +192,27 @@ class Test(test): # Check result file self._check_result_file('csspec_py3.fits', 10) + # Set-up On/Off csspec with NODES method + spec = cscripts.csspec() + spec['inobs'] = self._inonoff + spec['inmodel'] = self._onoff_model + spec['srcname'] = 'Crab' + spec['outfile'] = 'csspec_py4.fits' + spec['method'] = 'NODES' + spec['ebinalg'] = 'LOG' + spec['enumbins'] = 10 + spec['emin'] = 0.1 + spec['emax'] = 10.0 + spec['logfile'] = 'csspec_py4.log' + spec['chatter'] = 4 + spec['publish'] = False + + # Execute csspec script + spec.execute() + + # Check result file + self._check_result_file('csspec_py4.fits', 10) + # Return return -- libgit2 0.21.4