Commit 4e90453a8d21cc5ea149e3c83b25473ac2fee3d8

Authored by Jürgen Knödlseder
1 parent c8621c30

Extend cspull to handle also stacked observations.

1 1 Johan Bregeon (johan.bregeon@lupm.in2p3.fr)
  2 +Rolf Buehler (rolf.buehler@desy.de)
2 3 Jean-Baptiste Cayrou (cayrou@etud.insa-toulouse.fr)
3 4 Christoph Deil (Christoph.Deil@mpi-hd.mpg.de)
4 5 Florent Forest (florent.forest@isae.fr)
... ...
ChangeLog
... ... @@ -3,6 +3,7 @@
3 3 * Version 0.10.0 released
4 4 =========================
5 5  
  6 + Extend cspull to stacked analysis (#1503)
6 7 Create cscripts Python module, move obsutils in that module (#1470)
7 8 Add csinfo script (#1489)
8 9 Removed obsutils.spectrum() function (replaced by csspec)
... ...
... ... @@ -119,6 +119,10 @@ None
119 119 Added csinfo script to provide some information about the GammaLib and ctools
120 120 installation. To be run in case of problems to help diagnostics.
121 121  
  122 +The cspull script was extended to now also handle stacked analysis in case
  123 +that an observation definition XML file is specified with multiple observations
  124 +by the inobs parameter.
  125 +
122 126  
123 127 21. caldb
124 128 ---------
... ...
cscripts/cspull.py
... ... @@ -44,11 +44,15 @@ class cspull(ctools.cscript):
44 44 self.version = "1.0.0"
45 45  
46 46 # Initialise some members
47   - self.obs = None
48   - self.model = None
49   - self.m_inmodel = None
50   - self.m_edisp = False
51   - self.m_profile = False
  47 + self.obs = None
  48 + self.model = None
  49 + self.m_inmodel = None
  50 + self.m_edisp = False
  51 + self.m_profile = False
  52 + self.m_exposure = None
  53 + self.m_psfcube = None
  54 + self.m_bckcube = None
  55 + self.m_stackmodels = None
52 56  
53 57 # Make sure that parfile exists
54 58 file = self.parfile()
... ... @@ -195,7 +199,66 @@ class cspull(ctools.cscript):
195 199 # Return
196 200 return
197 201  
198   -
  202 +
  203 + def set_stacked_irf(self):
  204 + """
  205 + For stacked analysis prepare stacked irfs
  206 + """
  207 + # Get stacked exposure
  208 + expcube = ctools.ctexpcube(self.obs)
  209 + expcube["incube"] = "NONE"
  210 + expcube["usepnt"] = True
  211 + expcube["ebinalg"] = "LOG"
  212 + expcube["binsz"] = self.m_binsz
  213 + expcube["nxpix"] = self.m_npix
  214 + expcube["nypix"] = self.m_npix
  215 + expcube["enumbins"] = self.m_enumbins
  216 + expcube["emin"] = self["emin"].real()
  217 + expcube["emax"] = self["emax"].real()
  218 + expcube["coordsys"] = "GAL"
  219 + expcube["proj"] = "TAN"
  220 + expcube.run()
  221 +
  222 + # Get stacked Psf
  223 + psfcube = ctools.ctpsfcube(self.obs)
  224 + psfcube["incube"] = "NONE"
  225 + psfcube["usepnt"] = True
  226 + psfcube["ebinalg"] = "LOG"
  227 + psfcube["binsz"] = self.m_binsz
  228 + psfcube["nxpix"] = self.m_npix
  229 + psfcube["nypix"] = self.m_npix
  230 + psfcube["enumbins"] = self.m_enumbins
  231 + psfcube["emin"] = self["emin"].real()
  232 + psfcube["emax"] = self["emax"].real()
  233 + psfcube["coordsys"] = "GAL"
  234 + psfcube["proj"] = "TAN"
  235 + psfcube.run()
  236 +
  237 + # Get stacked background
  238 + bkgcube = ctools.ctbkgcube(self.obs)
  239 + bkgcube["incube"] = "NONE"
  240 + bkgcube["usepnt"] = True
  241 + bkgcube["ebinalg"] = "LOG"
  242 + bkgcube["binsz"] = self.m_binsz
  243 + bkgcube["nxpix"] = self.m_npix
  244 + bkgcube["nypix"] = self.m_npix
  245 + bkgcube["enumbins"] = self.m_enumbins
  246 + bkgcube["emin"] = self["emin"].real()
  247 + bkgcube["emax"] = self["emax"].real()
  248 + bkgcube["coordsys"] = "GAL"
  249 + bkgcube["proj"] = "TAN"
  250 + bkgcube.run()
  251 +
  252 + # Store results
  253 + self.m_exposure = expcube.expcube().copy()
  254 + self.m_psfcube = psfcube.psfcube().copy()
  255 + self.m_bckcube = bkgcube.bkgcube().copy()
  256 + self.m_stackmodels = bkgcube.models().copy()
  257 +
  258 + # Return
  259 + return
  260 +
  261 +
199 262 def run(self):
200 263 """
201 264 Run the script.
... ... @@ -207,6 +270,10 @@ class cspull(ctools.cscript):
207 270 # Get parameters
208 271 self.get_parameters()
209 272  
  273 + # If several observations and binned: prepare stacked irfs
  274 + if self.obs.size() > 1 and self.m_enumbins > 0:
  275 + self.set_stacked_irf()
  276 +
210 277 # Write input parameters into logger
211 278 if self.logTerse():
212 279 self.log_parameters()
... ... @@ -276,6 +343,7 @@ class cspull(ctools.cscript):
276 343 Parameters:
277 344 seed - Random number generator seed
278 345 """
  346 +
279 347 # Write header
280 348 if self.logNormal():
281 349 self.log.header2("Trial "+str(seed+1))
... ... @@ -290,7 +358,12 @@ class cspull(ctools.cscript):
290 358 log=self.m_log, \
291 359 debug=self.m_debug, \
292 360 chatter=self.m_chatter)
293   -
  361 +
  362 + #If stacked, add stacked responses and model
  363 + if self.obs.size()>0 and self.m_enumbins>0:
  364 + obs[0].response(self.m_exposure,self.m_psfcube,self.m_bckcube)
  365 + obs.models(self.m_stackmodels)
  366 +
294 367 # Determine number of events in simulation
295 368 nevents = 0.0
296 369 for run in obs:
... ... @@ -305,16 +378,13 @@ class cspull(ctools.cscript):
305 378  
306 379 # Fit model
307 380 if self.m_profile:
308   - models = obs.models()
309   - new_models = gammalib.GModels()
  381 + models = obs.models()
310 382 for i in range(models.size()):
311 383 model_name = models[i].name()
312 384 like = obsutils.cterror(obs, model_name, \
313 385 log=self.m_log, \
314 386 debug=self.m_debug,
315 387 chatter=self.m_chatter)
316   - new_models.append(like.obs().models()[i])
317   - like.obs().models(new_models)
318 388 else:
319 389 like = obsutils.fit(obs, edisp=self.m_edisp, \
320 390 log=self.m_log, \
... ...