Commit ab538013fbb7276c87bb44970dc3008d24cf9dce

Authored by mimayer
1 parent 891a9709

ctbin and ctmodel now use GEbounds::is_in_range() to verify each bin is containe…

…d in observation ebounds. Also added some improvements to csspec.
cscripts/csspec.py
@@ -122,6 +122,7 @@ class csspec(ctools.cscript): @@ -122,6 +122,7 @@ class csspec(ctools.cscript):
122 pars.append(gammalib.GApplicationPar("nxpix","i","a","200","","","Size of the X axis in pixels")) 122 pars.append(gammalib.GApplicationPar("nxpix","i","a","200","","","Size of the X axis in pixels"))
123 pars.append(gammalib.GApplicationPar("nypix","i","a","200","","","Size of the Y axis in pixels")) 123 pars.append(gammalib.GApplicationPar("nypix","i","a","200","","","Size of the Y axis in pixels"))
124 pars.append(gammalib.GApplicationPar("binsz","r","a","0.02","","","Pixel size (deg/pixel)")) 124 pars.append(gammalib.GApplicationPar("binsz","r","a","0.02","","","Pixel size (deg/pixel)"))
  125 + pars.append(gammalib.GApplicationPar("anumbins","i","h","20","","","Angular separation bins for PSF cube in binned mode"))
125 pars.append(gammalib.GApplicationPar("calc_ts","b","h","yes","yes|no","","Compute TS value in each bin")) 126 pars.append(gammalib.GApplicationPar("calc_ts","b","h","yes","yes|no","","Compute TS value in each bin"))
126 pars.append(gammalib.GApplicationPar("calc_ulim","b","h","yes","yes|no","","Compute upper limit in each bin")) 127 pars.append(gammalib.GApplicationPar("calc_ulim","b","h","yes","yes|no","","Compute upper limit in each bin"))
127 pars.append(gammalib.GApplicationPar("fix_srcs","b","h","yes","yes|no","","Fix other skymodel parameters")) 128 pars.append(gammalib.GApplicationPar("fix_srcs","b","h","yes","yes|no","","Fix other skymodel parameters"))
@@ -167,6 +168,7 @@ class csspec(ctools.cscript): @@ -167,6 +168,7 @@ class csspec(ctools.cscript):
167 self.m_coordsys = self["coordsys"].string() 168 self.m_coordsys = self["coordsys"].string()
168 self.m_proj = self["proj"].string() 169 self.m_proj = self["proj"].string()
169 self.m_ebins = self["nebins"].integer() 170 self.m_ebins = self["nebins"].integer()
  171 + self.m_anumbins = self["anumbins"].integer()
170 172
171 # Read other parameters 173 # Read other parameters
172 self.m_outfile = self["outfile"].filename() 174 self.m_outfile = self["outfile"].filename()
@@ -254,9 +256,10 @@ class csspec(ctools.cscript): @@ -254,9 +256,10 @@ class csspec(ctools.cscript):
254 if par.is_free() and self.logExplicit(): 256 if par.is_free() and self.logExplicit():
255 self.log(" Fixing \""+par.name()+"\"\n") 257 self.log(" Fixing \""+par.name()+"\"\n")
256 par.fix() 258 par.fix()
257 - if par.is_fixed() and self.logExplicit():  
258 - self.log(" Freeing \""+par.name()+"\"\n")  
259 - model.spectral()[0].free() 259 + normpar = model.spectral()[0]
  260 + if normpar.is_fixed() and self.logExplicit():
  261 + self.log(" Freeing \""+normpar.name()+"\"\n")
  262 + normpar.free()
260 if self.m_calc_ts: 263 if self.m_calc_ts:
261 model.tscalc(True) 264 model.tscalc(True)
262 265
@@ -308,7 +311,7 @@ class csspec(ctools.cscript): @@ -308,7 +311,7 @@ class csspec(ctools.cscript):
308 # Log information 311 # Log information
309 if self.logTerse(): 312 if self.logTerse():
310 self.log("\n") 313 self.log("\n")
311 - self.log.header2("Energy bin "+str(i)) 314 + self.log.header2("Energy bin "+str(i+1))
312 315
313 # Get energy boundaries 316 # Get energy boundaries
314 emin = self.m_ebounds.emin(i) 317 emin = self.m_ebounds.emin(i)
@@ -323,33 +326,36 @@ class csspec(ctools.cscript): @@ -323,33 +326,36 @@ class csspec(ctools.cscript):
323 energy_low[i] = (elogmean - emin).TeV() 326 energy_low[i] = (elogmean - emin).TeV()
324 energy_high[i] = (emax - elogmean).TeV() 327 energy_high[i] = (emax - elogmean).TeV()
325 328
326 - # Log information  
327 - if self.logExplicit():  
328 - self.log.header3("Selecting events")  
329 -  
330 - # Select events  
331 - select = ctools.ctselect(self.obs)  
332 - select["emin"] = emin.TeV()  
333 - select["emax"] = emax.TeV()  
334 - select["tmin"] = "UNDEFINED"  
335 - select["tmax"] = "UNDEFINED"  
336 - select["rad"] = "UNDEFINED"  
337 - select["ra"] = "UNDEFINED"  
338 - select["dec"] = "UNDEFINED"  
339 - select.run()  
340 -  
341 - # Retrieve observation  
342 - obs = select.obs() 329 + # unbinned analysis
  330 + if not self.m_binned:
  331 +
  332 + # Log information
  333 + if self.logExplicit():
  334 + self.log.header3("Selecting events")
  335 +
  336 + # Select events
  337 + select = ctools.ctselect(self.obs)
  338 + select["emin"] = emin.TeV()
  339 + select["emax"] = emax.TeV()
  340 + select["tmin"] = "UNDEFINED"
  341 + select["tmax"] = "UNDEFINED"
  342 + select["rad"] = "UNDEFINED"
  343 + select["ra"] = "UNDEFINED"
  344 + select["dec"] = "UNDEFINED"
  345 + select.run()
  346 +
  347 + # Retrieve observation
  348 + obs = select.obs()
343 349
344 # Binned analysis 350 # Binned analysis
345 - if self.m_binned: 351 + else:
346 352
347 # Header 353 # Header
348 if self.logTerse(): 354 if self.logTerse():
349 self.log.header3("Binning events") 355 self.log.header3("Binning events")
350 356
351 # Bin events 357 # Bin events
352 - bin = ctools.ctbin(select.obs()) 358 + bin = ctools.ctbin(self.obs)
353 bin["usepnt"] = False 359 bin["usepnt"] = False
354 bin["ebinalg"] = "LOG" 360 bin["ebinalg"] = "LOG"
355 bin["xref"] = self.m_xref 361 bin["xref"] = self.m_xref
@@ -369,7 +375,7 @@ class csspec(ctools.cscript): @@ -369,7 +375,7 @@ class csspec(ctools.cscript):
369 self.log.header3("Creating exposure cube") 375 self.log.header3("Creating exposure cube")
370 376
371 # Create exposure cube 377 # Create exposure cube
372 - expcube = ctools.ctexpcube(select.obs()) 378 + expcube = ctools.ctexpcube(self.obs)
373 expcube["incube"] = "NONE" 379 expcube["incube"] = "NONE"
374 expcube["usepnt"] = False 380 expcube["usepnt"] = False
375 expcube["ebinalg"] = "LOG" 381 expcube["ebinalg"] = "LOG"
@@ -383,7 +389,7 @@ class csspec(ctools.cscript): @@ -383,7 +389,7 @@ class csspec(ctools.cscript):
383 expcube["emin"] = emin.TeV() 389 expcube["emin"] = emin.TeV()
384 expcube["emax"] = emax.TeV() 390 expcube["emax"] = emax.TeV()
385 expcube["coordsys"] = self.m_coordsys 391 expcube["coordsys"] = self.m_coordsys
386 - expcube["proj"] = self.m_proj 392 + expcube["proj"] = self.m_proj
387 expcube.run() 393 expcube.run()
388 394
389 # Header 395 # Header
@@ -391,7 +397,7 @@ class csspec(ctools.cscript): @@ -391,7 +397,7 @@ class csspec(ctools.cscript):
391 self.log.header3("Creating PSF cube") 397 self.log.header3("Creating PSF cube")
392 398
393 # Create psf cube 399 # Create psf cube
394 - psfcube = ctools.ctpsfcube(select.obs()) 400 + psfcube = ctools.ctpsfcube(self.obs)
395 psfcube["incube"] = "NONE" 401 psfcube["incube"] = "NONE"
396 psfcube["usepnt"] = False 402 psfcube["usepnt"] = False
397 psfcube["ebinalg"] = "LOG" 403 psfcube["ebinalg"] = "LOG"
@@ -402,11 +408,11 @@ class csspec(ctools.cscript): @@ -402,11 +408,11 @@ class csspec(ctools.cscript):
402 psfcube["nxpix"] = self.m_nxpix 408 psfcube["nxpix"] = self.m_nxpix
403 psfcube["nypix"] = self.m_nypix 409 psfcube["nypix"] = self.m_nypix
404 psfcube["enumbins"] = self.m_ebins 410 psfcube["enumbins"] = self.m_ebins
405 - psfcube["anumbins"] = 20 411 + psfcube["anumbins"] = self.m_anumbins
406 psfcube["emin"] = emin.TeV() 412 psfcube["emin"] = emin.TeV()
407 psfcube["emax"] = emax.TeV() 413 psfcube["emax"] = emax.TeV()
408 psfcube["coordsys"] = self.m_coordsys 414 psfcube["coordsys"] = self.m_coordsys
409 - psfcube["proj"] = self.m_proj 415 + psfcube["proj"] = self.m_proj
410 psfcube.run() 416 psfcube.run()
411 417
412 # Header 418 # Header
@@ -414,7 +420,7 @@ class csspec(ctools.cscript): @@ -414,7 +420,7 @@ class csspec(ctools.cscript):
414 self.log.header3("Creating background cube") 420 self.log.header3("Creating background cube")
415 421
416 # Create background cube 422 # Create background cube
417 - bkgcube = ctools.ctbkgcube(select.obs()) 423 + bkgcube = ctools.ctbkgcube(self.obs)
418 bkgcube["incube"] = "NONE" 424 bkgcube["incube"] = "NONE"
419 bkgcube["usepnt"] = False 425 bkgcube["usepnt"] = False
420 bkgcube["ebinalg"] = "LOG" 426 bkgcube["ebinalg"] = "LOG"
@@ -427,7 +433,7 @@ class csspec(ctools.cscript): @@ -427,7 +433,7 @@ class csspec(ctools.cscript):
427 bkgcube["emin"] = emin.TeV() 433 bkgcube["emin"] = emin.TeV()
428 bkgcube["emax"] = emax.TeV() 434 bkgcube["emax"] = emax.TeV()
429 bkgcube["coordsys"] = self.m_coordsys 435 bkgcube["coordsys"] = self.m_coordsys
430 - bkgcube["proj"] = self.m_proj 436 + bkgcube["proj"] = self.m_proj
431 bkgcube.run() 437 bkgcube.run()
432 438
433 # Set new binned observation 439 # Set new binned observation
@@ -463,7 +469,7 @@ class csspec(ctools.cscript): @@ -463,7 +469,7 @@ class csspec(ctools.cscript):
463 469
464 # Log information 470 # Log information
465 if self.logTerse(): 471 if self.logTerse():
466 - self.log("No event in this bin. Bin is skipped\n") 472 + self.log("No event in this bin. Likelihood is zero. Bin is skipped\n")
467 473
468 # Set all values to 0 474 # Set all values to 0
469 flux[i] = 0.0 475 flux[i] = 0.0
src/ctbin/ctbin.cpp
@@ -482,12 +482,12 @@ void ctbin::fill_cube(GCTAObservation* obs) @@ -482,12 +482,12 @@ void ctbin::fill_cube(GCTAObservation* obs)
482 throw GException::invalid_value(G_FILL_CUBE, msg); 482 throw GException::invalid_value(G_FILL_CUBE, msg);
483 } 483 }
484 484
485 - if (events->size() == 0) {  
486 - return;  
487 - }  
488 // Get the RoI 485 // Get the RoI
489 const GCTARoi& roi = events->roi(); 486 const GCTARoi& roi = events->roi();
490 487
  488 + // Get the ebounds
  489 + const GEbounds& obs_ebounds = events->ebounds();
  490 +
491 // Check for RoI sanity 491 // Check for RoI sanity
492 if (!roi.is_valid()) { 492 if (!roi.is_valid()) {
493 std::string msg = "No RoI information found in input observation " 493 std::string msg = "No RoI information found in input observation "
@@ -529,7 +529,7 @@ void ctbin::fill_cube(GCTAObservation* obs) @@ -529,7 +529,7 @@ void ctbin::fill_cube(GCTAObservation* obs)
529 529
530 // Determine energy bin. Skip if we are outside the energy range 530 // Determine energy bin. Skip if we are outside the energy range
531 int index = m_ebounds.index(event->energy()); 531 int index = m_ebounds.index(event->energy());
532 - if (index == -1) { 532 + if (index == -1 || !obs_ebounds.is_in_range(m_ebounds.emin(index), m_ebounds.emax(index))) {
533 num_outside_ebds++; 533 num_outside_ebds++;
534 continue; 534 continue;
535 } 535 }
src/ctmodel/ctmodel.cpp
@@ -589,6 +589,10 @@ void ctmodel::fill_cube(const GCTAObservation* obs) @@ -589,6 +589,10 @@ void ctmodel::fill_cube(const GCTAObservation* obs)
589 const GEbounds& ebounds = obs->events()->ebounds(); 589 const GEbounds& ebounds = obs->events()->ebounds();
590 const GGti& gti = obs->events()->gti(); 590 const GGti& gti = obs->events()->gti();
591 591
  592 + // Get the ebounds
  593 + const GEbounds& obs_ebounds = obs->ebounds();
  594 + const GEbounds& cube_ebounds = m_cube.ebounds();
  595 +
592 // Initialise statistics 596 // Initialise statistics
593 double sum = 0.0; 597 double sum = 0.0;
594 int num_outside_ebds = 0; 598 int num_outside_ebds = 0;
@@ -603,7 +607,8 @@ void ctmodel::fill_cube(const GCTAObservation* obs) @@ -603,7 +607,8 @@ void ctmodel::fill_cube(const GCTAObservation* obs)
603 GCTAEventBin* bin = m_cube[i]; 607 GCTAEventBin* bin = m_cube[i];
604 608
605 // Skip bin if it is outside the energy range of the observation 609 // Skip bin if it is outside the energy range of the observation
606 - if (!ebounds.contains(bin->energy())) { 610 + int index = cube_ebounds.index(bin->energy());
  611 + if (index == -1 || !obs_ebounds.is_in_range(cube_ebounds.emin(index), cube_ebounds.emax(index))) {
607 num_outside_ebds++; 612 num_outside_ebds++;
608 continue; 613 continue;
609 } 614 }