Commit 3647c518d2885c9ab022d68340a659818ceab8ac

Authored by Jürgen Knödlseder
1 parent 48e0057e

Correct flux computation (was still based on the old interface and now leads to …

…excessive memory usage). This fixes bug #1425.
Showing 1 changed file with 15 additions and 13 deletions
scripts/cssens.py
... ... @@ -154,7 +154,7 @@ class cssens(GApplication):
154 154  
155 155 # Set some fixed parameters
156 156 self.m_log = False # Logging in client tools
157   - self.m_debug = False # Debugging in client tools
  157 + self.m_debug = False # self["debug"].boolean() # Debugging in client tools
158 158  
159 159 # Return
160 160 return
... ... @@ -424,26 +424,28 @@ class cssens(GApplication):
424 424 diffSens_value = []
425 425 iter = 0
426 426 test_flux = 0.1 # This is the initial test flux in Crab units
427   -
  427 +
428 428 # Loop until we break
429 429 while True:
430   -
  430 +
431 431 # Update iteration counter
432 432 iter += 1
433   -
  433 +
434 434 # Write header
435 435 if self.logExplicit():
436 436 self.log.header2("Iteration "+str(iter))
437 437  
438 438 # Set test flux
439   - full_model["Test"]['Prefactor'].value(test_flux)
440   - obs.models(full_model)
441   -
  439 + src_model = full_model.copy()
  440 + crab_flux = src_model["Test"]['Prefactor'].value()
  441 + src_model["Test"]['Prefactor'].value(crab_flux*test_flux)
  442 + obs.models(src_model)
  443 +
442 444 # Simulate events
443 445 sim = obsutils.sim(obs, nbins=self.m_nbins, seed=iter, \
444 446 binsz=self.m_binsz, npix=self.m_npix, \
445 447 log=self.m_log, debug=self.m_debug)
446   -
  448 +
447 449 # Determine number of events in simulation
448 450 nevents = 0.0
449 451 for run in sim:
... ... @@ -462,7 +464,7 @@ class cssens(GApplication):
462 464 result_bgm = like.obs().models().copy()
463 465 LogL_bgm = like.opt().value()
464 466 npred_bgm = like.obs().npred()
465   -
  467 +
466 468 # Assess quality based on a comparison between Npred and Nevents
467 469 quality_bgm = npred_bgm-nevents
468 470  
... ... @@ -484,7 +486,7 @@ class cssens(GApplication):
484 486 if self.logExplicit():
485 487 self.log("Fit quality outside required range. Start over.\n")
486 488 continue
487   -
  489 +
488 490 # Write model fit results
489 491 if self.logExplicit():
490 492 for model in result_bgm:
... ... @@ -495,7 +497,7 @@ class cssens(GApplication):
495 497 self.log(str(par)+"\n")
496 498  
497 499 # Fit background and test source
498   - sim.models(full_model)
  500 + sim.models(src_model)
499 501 like = obsutils.fit(sim, log=self.m_log, debug=self.m_debug)
500 502 result_all = like.obs().models().copy()
501 503 LogL_all = like.opt().value()
... ... @@ -541,8 +543,8 @@ class cssens(GApplication):
541 543 continue
542 544  
543 545 # Get fitted Crab, photon and energy fluxes
544   - cflux = result_all["Test"]['Prefactor'].value()
545   - cflux_err = result_all["Test"]['Prefactor'].error()
  546 + cflux = result_all["Test"]['Prefactor'].value() / crab_flux
  547 + cflux_err = result_all["Test"]['Prefactor'].error() / crab_flux
546 548 pflux = result_all["Test"].spectral().flux(emin, emax)
547 549 eflux = result_all["Test"].spectral().eflux(emin, emax)
548 550  
... ...