Commit 2fb9c07dbf9617f4228b8e1c7cb2efc1e8fbe864

Authored by Jürgen Knödlseder
1 parent 49d67ac3

Adapt to new ctools interface

test/example_models.py
... ... @@ -3,7 +3,7 @@
3 3 # This script performs an unbinned maximum likelihood analysis for a
4 4 # variety of models. This allows checking the models.
5 5 #
6   -# Copyright (C) 2011-2012 Juergen Knoedlseder
  6 +# Copyright (C) 2011-2015 Juergen Knoedlseder
7 7 #
8 8 # This program is free software: you can redistribute it and/or modify
9 9 # it under the terms of the GNU General Public License as published by
... ... @@ -19,11 +19,8 @@
19 19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20 #
21 21 # ==========================================================================
22   -from ctools import *
23   -from gammalib import *
24   -from math import *
25   -import os
26   -import glob
  22 +import gammalib
  23 +import ctools
27 24 import sys
28 25  
29 26  
... ... @@ -39,7 +36,7 @@ def pipeline(model_name):
39 36 FITS files on disk. All data is only hold in memory.
40 37 """
41 38 # Set script parameters
42   - caldb = "irf"
  39 + caldb = "dummy"
43 40 irf = "cta_dummy_irf"
44 41 ra = 83.63
45 42 dec = 22.01
... ... @@ -51,15 +48,15 @@ def pipeline(model_name):
51 48 rad_select = 3.0
52 49  
53 50 # Write model name
54   - print("Model: "+model_name)
  51 + print("*** Model: "+model_name+" ************************************")
55 52  
56 53 # Initialise timing
57 54 wall_seconds = 0.0
58 55 cpu_seconds = 0.0
59 56  
60 57 # Simulate events
61   - sim = ctobssim()
62   - sim["infile"].filename(model_name)
  58 + sim = ctools.ctobssim()
  59 + sim["inmodel"].filename(model_name)
63 60 sim["caldb"].string(caldb)
64 61 sim["irf"].string(irf)
65 62 sim["ra"].real(ra)
... ... @@ -72,7 +69,7 @@ def pipeline(model_name):
72 69 sim.run()
73 70  
74 71 # Select events
75   - select = ctselect(sim.obs())
  72 + select = ctools.ctselect(sim.obs())
76 73 select["ra"].real(ra)
77 74 select["dec"].real(dec)
78 75 select["rad"].real(rad_select)
... ... @@ -83,7 +80,7 @@ def pipeline(model_name):
83 80 select.run()
84 81  
85 82 # Perform maximum likelihood fitting
86   - like = ctlike(select.obs())
  83 + like = ctools.ctlike(select.obs())
87 84 like.run()
88 85  
89 86 # Show model fitting results
... ...
test/example_survey.py
... ... @@ -3,7 +3,7 @@
3 3 # This script simulates CTA surveys. It can be considered as the master
4 4 # script for CTA science simulations. Please modify as needed.
5 5 #
6   -# Copyright (C) 2011-2014 Juergen Knoedlseder
  6 +# Copyright (C) 2011-2015 Juergen Knoedlseder
7 7 #
8 8 # This program is free software: you can redistribute it and/or modify
9 9 # it under the terms of the GNU General Public License as published by
... ... @@ -19,13 +19,11 @@
19 19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20 #
21 21 # ==========================================================================
22   -from ctools import *
23   -from gammalib import *
24   -from math import *
  22 +import gammalib
  23 +from ctools import obsutils
25 24 import os
26 25 import glob
27 26 import sys
28   -from ctools import obsutils
29 27  
30 28  
31 29 # =========== #
... ... @@ -67,11 +65,11 @@ def plot_counts(obs):
67 65 list = run.events()
68 66  
69 67 # Create energy axis
70   - ebounds = GEbounds()
71   - emin = GEnergy()
72   - emax = GEnergy()
73   - emin.TeV(0.1)
74   - emax.TeV(100.0)
  68 + ebounds = gammalib.GEbounds()
  69 + emin = gammalib.GEnergy(0.1, "TeV")
  70 + emax = gammalib.GEnergy(100.0, "TeV")
  71 + #emin.TeV(0.1)
  72 + #emax.TeV(100.0)
75 73 ebounds.setlog(emin, emax, 10)
76 74 energy = [ebounds.elogmean(i).TeV() for i in range(ebounds.size())]
77 75  
... ... @@ -115,7 +113,7 @@ def plot_counts(obs):
115 113 plt.title("Offset (summed over all energies)")
116 114  
117 115 # Set Crab direction
118   - crab = GSkyDir()
  116 + crab = gammalib.GSkyDir()
119 117 crab.radec_deg(83.63, 22.01)
120 118  
121 119 # Loop over observations
... ... @@ -191,9 +189,9 @@ def add_background_model(obs):
191 189 models = obs.models()
192 190  
193 191 # Define background model
194   - bgd_radial = GCTAModelRadialGauss(3.0)
195   - bgd_spectrum = GModelSpectralPlaw(61.8e-6, -1.85, GEnergy(1.0, "TeV"))
196   - bgd_model = GCTAModelRadialAcceptance(bgd_radial, bgd_spectrum)
  192 + bgd_radial = gammalib.GCTAModelRadialGauss(3.0)
  193 + bgd_spectrum = gammalib.GModelSpectralPlaw(61.8e-6, -1.85, gammalib.GEnergy(1.0, "TeV"))
  194 + bgd_model = gammalib.GCTAModelRadialAcceptance(bgd_radial, bgd_spectrum)
197 195 bgd_model.name("Background")
198 196 bgd_model.instruments("CTA")
199 197  
... ... @@ -216,7 +214,7 @@ def crab_spec():
216 214 (Albert et al. 2008, ApJ, 674, 1037)
217 215 """
218 216 # Set parameters
219   - spectrum = GModelSpectralPlaw(5.7e-16, -2.48, GEnergy(0.3, "TeV"))
  217 + spectrum = gammalib.GModelSpectralPlaw(5.7e-16, -2.48, gammalib.GEnergy(0.3, "TeV"))
220 218  
221 219 # Return spectrum
222 220 return spectrum
... ... @@ -230,24 +228,24 @@ def survey_single():
230 228 Creates a single observation survey for test purposes.
231 229 """
232 230 # Allocate observation container
233   - obs = GObservations()
  231 + obs = gammalib.GObservations()
234 232  
235 233 # Set single pointing at galactic centre
236   - pntdir = GSkyDir()
  234 + pntdir = gammalib.GSkyDir()
237 235 pntdir.lb_deg(0.0, 0.0)
238 236 run = obsutils.set_obs(pntdir)
239 237 obs.append(run)
240 238  
241 239 # Define single point source with Crab flux at galactic centre
242   - center = GSkyDir()
  240 + center = gammalib.GSkyDir()
243 241 center.lb_deg(0.0, 0.0)
244   - point_spatial = GModelSpatialPointSource(center)
  242 + point_spatial = gammalib.GModelSpatialPointSource(center)
245 243 point_spectrum = crab_spec()
246   - point = GModelSky(point_spatial, point_spectrum)
  244 + point = gammalib.GModelSky(point_spatial, point_spectrum)
247 245 point.name('GC source')
248 246  
249 247 # Create model container
250   - models = GModels()
  248 + models = gammalib.GModels()
251 249 models.append(point)
252 250 obs.models(models)
253 251  
... ... @@ -267,28 +265,28 @@ def survey_gplane(lrange=10, lstep=2):
267 265 lstep - Longitude step size (integer deg)
268 266 """
269 267 # Allocate observation container
270   - obs = GObservations()
  268 + obs = gammalib.GObservations()
271 269  
272 270 # Loop over longitudes
273 271 for l in range(-lrange,lrange+lstep,lstep):
274 272  
275 273 # Set pointing
276   - pntdir = GSkyDir()
  274 + pntdir = gammalib.GSkyDir()
277 275 pntdir.lb_deg(l, 0.0)
278 276 run = obsutils.set_obs(pntdir)
279 277 run.id(str(l))
280 278 obs.append(run)
281 279  
282 280 # Define single point source with Crab flux at galactic centre
283   - center = GSkyDir()
  281 + center = gammalib.GSkyDir()
284 282 center.lb_deg(0.0, 0.0)
285   - point_spatial = GModelSpatialPointSource(center)
  283 + point_spatial = gammalib.GModelSpatialPointSource(center)
286 284 point_spectrum = crab_spec()
287   - point = GModelSky(point_spatial, point_spectrum)
  285 + point = gammalib.GModelSky(point_spatial, point_spectrum)
288 286 point.name('GC source')
289 287  
290 288 # Create model container
291   - models = GModels()
  289 + models = gammalib.GModels()
292 290 models.append(point)
293 291 obs.models(models)
294 292  
... ...