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 122 pars.append(gammalib.GApplicationPar("nxpix","i","a","200","","","Size of the X axis in pixels"))
123 123 pars.append(gammalib.GApplicationPar("nypix","i","a","200","","","Size of the Y axis in pixels"))
124 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 126 pars.append(gammalib.GApplicationPar("calc_ts","b","h","yes","yes|no","","Compute TS value in each bin"))
126 127 pars.append(gammalib.GApplicationPar("calc_ulim","b","h","yes","yes|no","","Compute upper limit in each bin"))
127 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 168 self.m_coordsys = self["coordsys"].string()
168 169 self.m_proj = self["proj"].string()
169 170 self.m_ebins = self["nebins"].integer()
  171 + self.m_anumbins = self["anumbins"].integer()
170 172  
171 173 # Read other parameters
172 174 self.m_outfile = self["outfile"].filename()
... ... @@ -254,9 +256,10 @@ class csspec(ctools.cscript):
254 256 if par.is_free() and self.logExplicit():
255 257 self.log(" Fixing \""+par.name()+"\"\n")
256 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 263 if self.m_calc_ts:
261 264 model.tscalc(True)
262 265  
... ... @@ -308,7 +311,7 @@ class csspec(ctools.cscript):
308 311 # Log information
309 312 if self.logTerse():
310 313 self.log("\n")
311   - self.log.header2("Energy bin "+str(i))
  314 + self.log.header2("Energy bin "+str(i+1))
312 315  
313 316 # Get energy boundaries
314 317 emin = self.m_ebounds.emin(i)
... ... @@ -323,33 +326,36 @@ class csspec(ctools.cscript):
323 326 energy_low[i] = (elogmean - emin).TeV()
324 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 350 # Binned analysis
345   - if self.m_binned:
  351 + else:
346 352  
347 353 # Header
348 354 if self.logTerse():
349 355 self.log.header3("Binning events")
350 356  
351 357 # Bin events
352   - bin = ctools.ctbin(select.obs())
  358 + bin = ctools.ctbin(self.obs)
353 359 bin["usepnt"] = False
354 360 bin["ebinalg"] = "LOG"
355 361 bin["xref"] = self.m_xref
... ... @@ -369,7 +375,7 @@ class csspec(ctools.cscript):
369 375 self.log.header3("Creating exposure cube")
370 376  
371 377 # Create exposure cube
372   - expcube = ctools.ctexpcube(select.obs())
  378 + expcube = ctools.ctexpcube(self.obs)
373 379 expcube["incube"] = "NONE"
374 380 expcube["usepnt"] = False
375 381 expcube["ebinalg"] = "LOG"
... ... @@ -383,7 +389,7 @@ class csspec(ctools.cscript):
383 389 expcube["emin"] = emin.TeV()
384 390 expcube["emax"] = emax.TeV()
385 391 expcube["coordsys"] = self.m_coordsys
386   - expcube["proj"] = self.m_proj
  392 + expcube["proj"] = self.m_proj
387 393 expcube.run()
388 394  
389 395 # Header
... ... @@ -391,7 +397,7 @@ class csspec(ctools.cscript):
391 397 self.log.header3("Creating PSF cube")
392 398  
393 399 # Create psf cube
394   - psfcube = ctools.ctpsfcube(select.obs())
  400 + psfcube = ctools.ctpsfcube(self.obs)
395 401 psfcube["incube"] = "NONE"
396 402 psfcube["usepnt"] = False
397 403 psfcube["ebinalg"] = "LOG"
... ... @@ -402,11 +408,11 @@ class csspec(ctools.cscript):
402 408 psfcube["nxpix"] = self.m_nxpix
403 409 psfcube["nypix"] = self.m_nypix
404 410 psfcube["enumbins"] = self.m_ebins
405   - psfcube["anumbins"] = 20
  411 + psfcube["anumbins"] = self.m_anumbins
406 412 psfcube["emin"] = emin.TeV()
407 413 psfcube["emax"] = emax.TeV()
408 414 psfcube["coordsys"] = self.m_coordsys
409   - psfcube["proj"] = self.m_proj
  415 + psfcube["proj"] = self.m_proj
410 416 psfcube.run()
411 417  
412 418 # Header
... ... @@ -414,7 +420,7 @@ class csspec(ctools.cscript):
414 420 self.log.header3("Creating background cube")
415 421  
416 422 # Create background cube
417   - bkgcube = ctools.ctbkgcube(select.obs())
  423 + bkgcube = ctools.ctbkgcube(self.obs)
418 424 bkgcube["incube"] = "NONE"
419 425 bkgcube["usepnt"] = False
420 426 bkgcube["ebinalg"] = "LOG"
... ... @@ -427,7 +433,7 @@ class csspec(ctools.cscript):
427 433 bkgcube["emin"] = emin.TeV()
428 434 bkgcube["emax"] = emax.TeV()
429 435 bkgcube["coordsys"] = self.m_coordsys
430   - bkgcube["proj"] = self.m_proj
  436 + bkgcube["proj"] = self.m_proj
431 437 bkgcube.run()
432 438  
433 439 # Set new binned observation
... ... @@ -463,7 +469,7 @@ class csspec(ctools.cscript):
463 469  
464 470 # Log information
465 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 474 # Set all values to 0
469 475 flux[i] = 0.0
... ...
src/ctbin/ctbin.cpp
... ... @@ -482,12 +482,12 @@ void ctbin::fill_cube(GCTAObservation* obs)
482 482 throw GException::invalid_value(G_FILL_CUBE, msg);
483 483 }
484 484  
485   - if (events->size() == 0) {
486   - return;
487   - }
488 485 // Get the RoI
489 486 const GCTARoi& roi = events->roi();
490 487  
  488 + // Get the ebounds
  489 + const GEbounds& obs_ebounds = events->ebounds();
  490 +
491 491 // Check for RoI sanity
492 492 if (!roi.is_valid()) {
493 493 std::string msg = "No RoI information found in input observation "
... ... @@ -529,7 +529,7 @@ void ctbin::fill_cube(GCTAObservation* obs)
529 529  
530 530 // Determine energy bin. Skip if we are outside the energy range
531 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 533 num_outside_ebds++;
534 534 continue;
535 535 }
... ...
src/ctmodel/ctmodel.cpp
... ... @@ -589,6 +589,10 @@ void ctmodel::fill_cube(const GCTAObservation* obs)
589 589 const GEbounds& ebounds = obs->events()->ebounds();
590 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 596 // Initialise statistics
593 597 double sum = 0.0;
594 598 int num_outside_ebds = 0;
... ... @@ -603,7 +607,8 @@ void ctmodel::fill_cube(const GCTAObservation* obs)
603 607 GCTAEventBin* bin = m_cube[i];
604 608  
605 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 612 num_outside_ebds++;
608 613 continue;
609 614 }
... ...