Commit 31a1247f0013bd2a7fafcf4cd6a8e82aa4200a77

Authored by Jürgen Knödlseder
1 parent bbe311d4

Update example scripts

examples/benchmark_cta_analysis.py
@@ -299,8 +299,7 @@ def stacked_pipeline(model_name, duration): @@ -299,8 +299,7 @@ def stacked_pipeline(model_name, duration):
299 # Main routine entry point # 299 # Main routine entry point #
300 #==========================# 300 #==========================#
301 if __name__ == '__main__': 301 if __name__ == '__main__':
302 - """  
303 - """ 302 +
304 # Initialise parameters 303 # Initialise parameters
305 title = "CTA analysis benchmark" 304 title = "CTA analysis benchmark"
306 model_name = "${CTOOLS}/share/models/crab.xml" 305 model_name = "${CTOOLS}/share/models/crab.xml"
examples/make_pointings.py
@@ -336,9 +336,7 @@ def write_obsdef(filename, obsdef): @@ -336,9 +336,7 @@ def write_obsdef(filename, obsdef):
336 # Main routine entry point # 336 # Main routine entry point #
337 # ======================== # 337 # ======================== #
338 if __name__ == '__main__': 338 if __name__ == '__main__':
339 - """  
340 - CTA pointing pattern generation.  
341 - """ 339 +
342 # Initialise flags 340 # Initialise flags
343 need_help = False 341 need_help = False
344 342
examples/make_pull_at_sensitivity_limit.py
@@ -135,11 +135,7 @@ def create_model(flux, index=-2.48, fitidx=False): @@ -135,11 +135,7 @@ def create_model(flux, index=-2.48, fitidx=False):
135 # Main routine entry point # 135 # Main routine entry point #
136 # ======================== # 136 # ======================== #
137 if __name__ == '__main__': 137 if __name__ == '__main__':
138 - """  
139 - Create pull distributions for the energy bands and fluxes given in the  
140 - file specified in the argument. The file needs to be created using the  
141 - cssens script.  
142 - """ 138 +
143 # Get input arguments 139 # Get input arguments
144 usage = """ 140 usage = """
145 make_pull_at_sensitivity_limit filename [max_threads] 141 make_pull_at_sensitivity_limit filename [max_threads]
examples/make_spectrum.py
@@ -167,9 +167,7 @@ def plot_spectrum(spectrum): @@ -167,9 +167,7 @@ def plot_spectrum(spectrum):
167 # Main routine entry point # 167 # Main routine entry point #
168 #==========================# 168 #==========================#
169 if __name__ == '__main__': 169 if __name__ == '__main__':
170 - """  
171 - Generate a Crab spectrum.  
172 - """ 170 +
173 # Generate spectrum 171 # Generate spectrum
174 spectrum = make_spectrum() 172 spectrum = make_spectrum()
175 173
examples/make_survey.py
@@ -303,9 +303,7 @@ def survey_gplane(lrange=10, lstep=2): @@ -303,9 +303,7 @@ def survey_gplane(lrange=10, lstep=2):
303 # Main routine entry point # 303 # Main routine entry point #
304 #==========================# 304 #==========================#
305 if __name__ == '__main__': 305 if __name__ == '__main__':
306 - """  
307 - CTA survey simulation and analysis script.  
308 - """ 306 +
309 # Initialise flags 307 # Initialise flags
310 need_help = False 308 need_help = False
311 309
examples/make_ts_distributions.py
@@ -94,9 +94,7 @@ def create_ts(loge, emin, emax, ntrials=100, duration=180000.0, @@ -94,9 +94,7 @@ def create_ts(loge, emin, emax, ntrials=100, duration=180000.0,
94 # Main routine entry point # 94 # Main routine entry point #
95 # ======================== # 95 # ======================== #
96 if __name__ == '__main__': 96 if __name__ == '__main__':
97 - """  
98 - Create TS distribution in a number of energy bands.  
99 - """ 97 +
100 # Get input arguments 98 # Get input arguments
101 usage = "make_ts_distributions [-n ntrials] [-e enumbins] [-m max_threads]" 99 usage = "make_ts_distributions [-n ntrials] [-e enumbins] [-m max_threads]"
102 if len(sys.argv) < 1: 100 if len(sys.argv) < 1:
examples/pipeline_binned_disk.py
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 # ========================================================================== 2 # ==========================================================================
3 -# This script illustrates how to perform a binned CTA analysis based on  
4 -# simulated CTA data. You may use and adapt this script to implement your  
5 -# own pipeline. 3 +# Perform binned CTA analysis based of simulated CTA data.
6 # 4 #
7 -# Usage:  
8 -# ./pipeline_binned_disk.py  
9 -#  
10 -# ==========================================================================  
11 -#  
12 -# Copyright (C) 2015 Juergen Knoedlseder 5 +# Copyright (C) 2015-2016 Juergen Knoedlseder
13 # 6 #
14 # 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
15 # 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
@@ -33,25 +26,41 @@ from cscripts import obsutils @@ -33,25 +26,41 @@ from cscripts import obsutils
33 # ================== # 26 # ================== #
34 # Setup observations # 27 # Setup observations #
35 # ================== # 28 # ================== #
36 -def setup_observations(pattern="four", ra=83.63, dec=22.01, offset=1.5, 29 +def setup_observations(pattern='four', ra=83.63, dec=22.01, offset=1.5,
37 emin=0.1, emax=100.0, rad=5.0, duration=1800.0, 30 emin=0.1, emax=100.0, rad=5.0, duration=1800.0,
38 - deadc=0.95,  
39 - caldb="prod2", irf="South_50h"): 31 + deadc=0.95, caldb='prod2', irf='South_0.5h'):
40 """ 32 """
41 Returns an observation container. 33 Returns an observation container.
42 34
43 - Keywords:  
44 - pattern - Pointing pattern, either "single" or "four"  
45 - ra - RA of pattern centre [deg] (default: 83.6331)  
46 - dec - DEC of pattern centre [deg] (default: 22.0145)  
47 - offset - Offset between observations of pattern [deg] (default: 1.5)  
48 - emin - Minimum energy [TeV] (default: 0.1)  
49 - emax - Maximum energy [TeV] (default: 100.0)  
50 - rad - ROI radius used for analysis [deg] (default: 5.0)  
51 - duration - Duration of one CTA observation [seconds] (default: 1800.0)  
52 - deadc - Deadtime correction factor (default: 0.95)  
53 - caldb - Calibration database path (default: "dummy")  
54 - irf - Instrument response function (default: cta_dummy_irf) 35 + Parameters
  36 + ----------
  37 + pattern : str, optional
  38 + Pointing pattern, either 'single' or 'four'
  39 + ra : float, optional
  40 + Right Ascension of pattern centre [deg]
  41 + dec : float, optional
  42 + Delication of pattern centre [deg]
  43 + offset : float, optional
  44 + Offset between observations of pattern [deg]
  45 + emin : float, optional
  46 + Minimum energy [TeV]
  47 + emax : float, optional
  48 + Maximum energy [TeV]
  49 + rad : float, optional
  50 + ROI radius used for analysis [deg]
  51 + duration : float, optional
  52 + Duration of one CTA observation [seconds]
  53 + deadc : float, optional
  54 + Deadtime correction factor
  55 + caldb : str, optional
  56 + Calibration database path
  57 + irf : str, optional
  58 + Instrument response function
  59 +
  60 + Returns
  61 + -------
  62 + obs : `~gammalib.GObservations`
  63 + Observation container
55 """ 64 """
56 # Set list of observations 65 # Set list of observations
57 obs_def_list = obsutils.set_obs_patterns(pattern, 66 obs_def_list = obsutils.set_obs_patterns(pattern,
@@ -75,12 +84,21 @@ def setup_observations(pattern=&quot;four&quot;, ra=83.63, dec=22.01, offset=1.5, @@ -75,12 +84,21 @@ def setup_observations(pattern=&quot;four&quot;, ra=83.63, dec=22.01, offset=1.5,
75 # =========== # 84 # =========== #
76 # Setup model # 85 # Setup model #
77 # =========== # 86 # =========== #
78 -def setup_model(obs, model="${CTOOLS}/share/models/crab.xml"): 87 +def setup_model(obs, model='${CTOOLS}/share/models/crab.xml'):
79 """ 88 """
80 Setup model for analysis. 89 Setup model for analysis.
81 90
82 - Keywords:  
83 - model - Model Xml file 91 + Parameters
  92 + ----------
  93 + obs : `~gammalib.GObservations`
  94 + Observation container
  95 + model : str, optional
  96 + Model definition XML file
  97 +
  98 + Returns
  99 + -------
  100 + obs : `~gammalib.GObservations`
  101 + Observation container
84 """ 102 """
85 # Append model from file to observation container 103 # Append model from file to observation container
86 obs.models(gammalib.GModels(model)) 104 obs.models(gammalib.GModels(model))
@@ -94,56 +112,71 @@ def setup_model(obs, model=&quot;${CTOOLS}/share/models/crab.xml&quot;): @@ -94,56 +112,71 @@ def setup_model(obs, model=&quot;${CTOOLS}/share/models/crab.xml&quot;):
94 # ================================ # 112 # ================================ #
95 def run_pipeline(obs, emin=0.1, emax=100.0, 113 def run_pipeline(obs, emin=0.1, emax=100.0,
96 enumbins=20, nxpix=200, nypix=200, binsz=0.02, 114 enumbins=20, nxpix=200, nypix=200, binsz=0.02,
97 - coordsys="CEL", proj="CAR",  
98 - model="${CTOOLS}/share/models/crab.xml",  
99 - caldb="prod2", irf="South_50h", 115 + coordsys='CEL', proj='CAR',
  116 + model='${CTOOLS}/share/models/crab.xml',
  117 + caldb='prod2', irf='South_0.5h',
100 debug=False): 118 debug=False):
101 """ 119 """
102 Simulation and binned analysis pipeline. 120 Simulation and binned analysis pipeline.
103 121
104 - Keywords:  
105 - emin - Minimum energy of cube [TeV] (default: 0.1)  
106 - emax - Maximum energy of cube [TeV] (default: 100.0)  
107 - enumbins - Number of energy bins in cube (default: 20)  
108 - nxpix - Number of RA pixels in cube (default: 200)  
109 - nypix - Number of DEC pixels in cube (default: 200)  
110 - binsz - Spatial cube bin size [deg] (default: 0.02)  
111 - coordsys - Cube coordinate system (CEL or GAL)  
112 - proj - Cube World Coordinate System (WCS) projection  
113 - model - Model Xml file  
114 - caldb - Calibration database path (default: "dummy")  
115 - irf - Instrument response function (default: cta_dummy_irf)  
116 - debug - Enable debugging (default: False) 122 + Parameters
  123 + ----------
  124 + obs : `~gammalib.GObservations`
  125 + Observation container
  126 + emin : float, optional
  127 + Minimum energy [TeV]
  128 + emax : float, optional
  129 + Maximum energy [TeV]
  130 + enumbins : int, optional
  131 + Number of energy bins
  132 + nxpix : int, optional
  133 + Number of pixels in X axis
  134 + nypix : int, optional
  135 + Number of pixels in Y axis
  136 + binsz : float, optional
  137 + Pixel size [deg/pixel]
  138 + coordsys : str, optional
  139 + Coordinate system
  140 + proj : str, optional
  141 + Coordinate projection
  142 + model : str, optional
  143 + Model definition XML file
  144 + caldb : str, optional
  145 + Calibration database path
  146 + irf : str, optional
  147 + Instrument response function
  148 + debug : bool, optional
  149 + Debug function
117 """ 150 """
118 # Simulate events 151 # Simulate events
119 sim = ctools.ctobssim(obs) 152 sim = ctools.ctobssim(obs)
120 - sim["debug"] = debug  
121 - sim["outevents"] = "obs.xml" 153 + sim['debug'] = debug
  154 + sim['outevents'] = 'obs.xml'
122 sim.execute() 155 sim.execute()
123 156
124 # Bin events by looping over all observations in the container 157 # Bin events by looping over all observations in the container
125 - sim_obs = gammalib.GObservations("obs.xml") 158 + sim_obs = gammalib.GObservations('obs.xml')
126 obs = gammalib.GObservations() 159 obs = gammalib.GObservations()
127 for run in sim_obs: 160 for run in sim_obs:
128 161
129 # Get event filename and set counts cube filename 162 # Get event filename and set counts cube filename
130 - eventfile = run.eventfile()  
131 - cubefile = "cube_"+eventfile 163 + eventfile = run.eventfile().url()
  164 + cubefile = 'cube_'+eventfile
132 165
133 # Bin events for that observation 166 # Bin events for that observation
134 bin = ctools.ctbin() 167 bin = ctools.ctbin()
135 - bin["inobs"] = eventfile  
136 - bin["outcube"] = cubefile  
137 - bin["ebinalg"] = "LOG"  
138 - bin["emin"] = emin  
139 - bin["emax"] = emax  
140 - bin["enumbins"] = enumbins  
141 - bin["nxpix"] = nxpix  
142 - bin["nypix"] = nypix  
143 - bin["binsz"] = binsz  
144 - bin["coordsys"] = coordsys  
145 - bin["usepnt"] = True  
146 - bin["proj"] = proj 168 + bin['inobs'] = eventfile
  169 + bin['outcube'] = cubefile
  170 + bin['ebinalg'] = 'LOG'
  171 + bin['emin'] = emin
  172 + bin['emax'] = emax
  173 + bin['enumbins'] = enumbins
  174 + bin['nxpix'] = nxpix
  175 + bin['nypix'] = nypix
  176 + bin['binsz'] = binsz
  177 + bin['coordsys'] = coordsys
  178 + bin['usepnt'] = True
  179 + bin['proj'] = proj
147 bin.execute() 180 bin.execute()
148 181
149 # Set observation ID 182 # Set observation ID
@@ -156,19 +189,19 @@ def run_pipeline(obs, emin=0.1, emax=100.0, @@ -156,19 +189,19 @@ def run_pipeline(obs, emin=0.1, emax=100.0,
156 # Save XML file 189 # Save XML file
157 xml = gammalib.GXml() 190 xml = gammalib.GXml()
158 obs.write(xml) 191 obs.write(xml)
159 - xml.save("obs_cube.xml") 192 + xml.save('obs_cube.xml')
160 193
161 # Perform maximum likelihood fitting 194 # Perform maximum likelihood fitting
162 like = ctools.ctlike() 195 like = ctools.ctlike()
163 - like["inobs"] = "obs_cube.xml"  
164 - like["inmodel"] = model  
165 - like["outmodel"] = "fit_results.xml"  
166 - like["expcube"] = "NONE"  
167 - like["psfcube"] = "NONE"  
168 - like["bkgcube"] = "NONE"  
169 - like["caldb"] = caldb  
170 - like["irf"] = irf  
171 - like["debug"] = True # Switch this always on for results in console 196 + like['inobs'] = 'obs_cube.xml'
  197 + like['inmodel'] = model
  198 + like['outmodel'] = 'fit_results.xml'
  199 + like['expcube'] = 'NONE'
  200 + like['psfcube'] = 'NONE'
  201 + like['bkgcube'] = 'NONE'
  202 + like['caldb'] = caldb
  203 + like['irf'] = irf
  204 + like['debug'] = True # Switch this always on for results in console
172 like.execute() 205 like.execute()
173 206
174 # Return 207 # Return
@@ -181,9 +214,9 @@ def run_pipeline(obs, emin=0.1, emax=100.0, @@ -181,9 +214,9 @@ def run_pipeline(obs, emin=0.1, emax=100.0,
181 if __name__ == '__main__': 214 if __name__ == '__main__':
182 215
183 # Dump header 216 # Dump header
184 - print("********************************************")  
185 - print("* CTA binned analysis pipeline *")  
186 - print("********************************************") 217 + print('********************************************')
  218 + print('* CTA binned analysis pipeline *')
  219 + print('********************************************')
187 220
188 # Setup observations 221 # Setup observations
189 obs = setup_observations() 222 obs = setup_observations()
examples/show_butterfly.py
@@ -30,8 +30,7 @@ except: @@ -30,8 +30,7 @@ except:
30 # Script entry # 30 # Script entry #
31 # ============ # 31 # ============ #
32 if __name__ == "__main__": 32 if __name__ == "__main__":
33 - """  
34 - """ 33 +
35 # Check for given butterfly file 34 # Check for given butterfly file
36 if not len(sys.argv) == 2: 35 if not len(sys.argv) == 2:
37 sys.exit("Usage: show_butterfly.py butterfly.txt") 36 sys.exit("Usage: show_butterfly.py butterfly.txt")
examples/show_model.py
@@ -82,8 +82,7 @@ def plot_spectrum(model, emin=0.01, emax=100.0, enumbins=100): @@ -82,8 +82,7 @@ def plot_spectrum(model, emin=0.01, emax=100.0, enumbins=100):
82 # Script entry # 82 # Script entry #
83 # ============= # 83 # ============= #
84 if __name__ == "__main__": 84 if __name__ == "__main__":
85 - """  
86 - """ 85 +
87 # Check for model filename 86 # Check for model filename
88 if len(sys.argv) < 2: 87 if len(sys.argv) < 2:
89 sys.exit("Usage: show_model.py model.xml [index]") 88 sys.exit("Usage: show_model.py model.xml [index]")
examples/show_pha.py
@@ -79,9 +79,7 @@ def show_pha(pha): @@ -79,9 +79,7 @@ def show_pha(pha):
79 # Main routine entry point # 79 # Main routine entry point #
80 # ======================== # 80 # ======================== #
81 if __name__ == '__main__': 81 if __name__ == '__main__':
82 - """  
83 - Display spectrum in PHA format.  
84 - """ 82 +
85 # Print usage information 83 # Print usage information
86 usage = "Usage: show_pha filename" \ 84 usage = "Usage: show_pha filename" \
87 " [-n bins] [-c column] [-t title] [-p plot]" 85 " [-n bins] [-c column] [-t title] [-p plot]"
examples/show_pull_evolution.py
@@ -81,9 +81,7 @@ def read_pull(filename, parname): @@ -81,9 +81,7 @@ def read_pull(filename, parname):
81 # Main routine entry point # 81 # Main routine entry point #
82 # ======================== # 82 # ======================== #
83 if __name__ == '__main__': 83 if __name__ == '__main__':
84 - """  
85 - Display pull evolution generated by cspull using matplotlib.  
86 - """ 84 +
87 # Print usage information 85 # Print usage information
88 usage = "Usage: show_pull_evolution filename parname" 86 usage = "Usage: show_pull_evolution filename parname"
89 if len(sys.argv) < 3: 87 if len(sys.argv) < 3:
examples/show_pull_histogram.py
@@ -81,9 +81,7 @@ def read_pull(filename, parname): @@ -81,9 +81,7 @@ def read_pull(filename, parname):
81 # Main routine entry point # 81 # Main routine entry point #
82 # ======================== # 82 # ======================== #
83 if __name__ == '__main__': 83 if __name__ == '__main__':
84 - """  
85 - Display pull histogram generated by cspull using matplotlib.  
86 - """ 84 +
87 # Print usage information 85 # Print usage information
88 usage = "Usage: show_pull_histogram filename parname [bins]" 86 usage = "Usage: show_pull_histogram filename parname [bins]"
89 if len(sys.argv) < 3: 87 if len(sys.argv) < 3:
examples/show_response.py
@@ -252,9 +252,7 @@ def show_one_response(rspname, dbname, name, rootdir=None, color=&quot;r&quot;): @@ -252,9 +252,7 @@ def show_one_response(rspname, dbname, name, rootdir=None, color=&quot;r&quot;):
252 # Main routine entry point # 252 # Main routine entry point #
253 # ======================== # 253 # ======================== #
254 if __name__ == '__main__': 254 if __name__ == '__main__':
255 - """  
256 - Display response information.  
257 - """ 255 +
258 # Create figures 256 # Create figures
259 plt.figure(1) 257 plt.figure(1)
260 plt.title("Effective area") 258 plt.title("Effective area")
examples/show_sensitivity.py
@@ -119,8 +119,7 @@ def show_sensitivity(sensitivity, filename): @@ -119,8 +119,7 @@ def show_sensitivity(sensitivity, filename):
119 # Main routine entry point # 119 # Main routine entry point #
120 # ======================== # 120 # ======================== #
121 if __name__ == '__main__': 121 if __name__ == '__main__':
122 - """  
123 - """ 122 +
124 # Print usage information 123 # Print usage information
125 usage = "Usage: show_sensitivity [filename]" 124 usage = "Usage: show_sensitivity [filename]"
126 if len(sys.argv) < 1: 125 if len(sys.argv) < 1:
examples/show_spectrum.py
@@ -31,8 +31,6 @@ except: @@ -31,8 +31,6 @@ except:
31 # Script entry # 31 # Script entry #
32 # ============ # 32 # ============ #
33 if __name__ == "__main__": 33 if __name__ == "__main__":
34 - """  
35 - """  
36 34
37 # Check for given spectrum file 35 # Check for given spectrum file
38 if not len(sys.argv) == 2: 36 if not len(sys.argv) == 2:
examples/show_ts_distribution.py
@@ -219,9 +219,7 @@ def dist_pdf(values, nbins, title): @@ -219,9 +219,7 @@ def dist_pdf(values, nbins, title):
219 # Main routine entry point # 219 # Main routine entry point #
220 # ======================== # 220 # ======================== #
221 if __name__ == '__main__': 221 if __name__ == '__main__':
222 - """  
223 - Display TS distribution generated using cstsdist using matplotlib.  
224 - """ 222 +
225 # Print usage information 223 # Print usage information
226 usage = "Usage: show_ts_distribution filename" \ 224 usage = "Usage: show_ts_distribution filename" \
227 " [-n bins] [-c column] [-t title] [-p plot]" 225 " [-n bins] [-c column] [-t title] [-p plot]"
examples/simulate_events.py
@@ -239,17 +239,15 @@ def show_events(events, xmlname, duration, emin, emax, ebins=30): @@ -239,17 +239,15 @@ def show_events(events, xmlname, duration, emin, emax, ebins=30):
239 # Main routine entry point # 239 # Main routine entry point #
240 #==========================# 240 #==========================#
241 if __name__ == '__main__': 241 if __name__ == '__main__':
242 - """  
243 - Simulate events.  
244 - """ 242 +
245 # Dump header 243 # Dump header
246 - print("")  
247 - print("*******************")  
248 - print("* Simulate events *")  
249 - print("*******************") 244 + print('')
  245 + print('*******************')
  246 + print('* Simulate events *')
  247 + print('*******************')
250 248
251 # Set XML names 249 # Set XML names
252 - xmlname = "model_bgd.xml" 250 + xmlname = 'model_bgd.xml'
253 251
254 # Set simulation parameters 252 # Set simulation parameters
255 duration = 360000.0 # Observation duration (s) 253 duration = 360000.0 # Observation duration (s)