Commit e99f033eecada40bbb9455b84052797400988376

Authored by Jürgen Knödlseder
1 parent ad7ffe86

Make TS computation optional.

src/ctlike/ctlike.cpp
@@ -267,39 +267,46 @@ void ctlike::run(void) @@ -267,39 +267,46 @@ void ctlike::run(void)
267 267
268 // Optimize model parameters using LM optimizer 268 // Optimize model parameters using LM optimizer
269 optimize_lm(); 269 optimize_lm();
  270 +
  271 + // Optionally perform TS computation
  272 + if (m_tscalc) {
270 273
271 - // Store original models and max. likelihood  
272 - double logL_src=m_logL;  
273 - GModels* models_orig = m_obs.models().clone(); 274 + // Store original models and max. likelihood
  275 + double logL_src = m_logL;
  276 + GModels* models_orig = m_obs.models().clone();
274 277
275 - //Store models whith free parameters  
276 - std::vector<std::string> free_srcs;  
277 - GModels models = m_obs.models();  
278 - for (int ii=0; ii < models.size();ii++){  
279 - GModel* model = models[ii];  
280 - for(int jj=0; jj < model->size();jj++){  
281 - GModelPar par = model->at(jj);  
282 - if (par.is_free()){  
283 - free_srcs.push_back(model->name());  
284 - break; 278 + // Store models whith free parameters
  279 + std::vector<std::string> free_srcs;
  280 + GModels models = m_obs.models();
  281 + for (int i = 0; i < models.size(); ++i) {
  282 + GModel* model = models[i];
  283 + for(int j = 0; j < model->size(); ++j) {
  284 + GModelPar par = model->at(j);
  285 + if (par.is_free()){
  286 + free_srcs.push_back(model->name());
  287 + break;
  288 + }
285 } 289 }
286 } 290 }
287 - }  
288 - //Loop over stored models, remove source and refit  
289 - for (int ii=0; ii < free_srcs.size();ii++){  
290 - models.remove(free_srcs[ii]);  
291 - m_obs.models(models); 291 +
  292 + // Loop over stored models, remove source and refit
  293 + for (int i = 0; i < free_srcs.size(); ++i) {
  294 + models.remove(free_srcs[i]);
  295 + m_obs.models(models);
  296 + optimize_lm();
  297 + double logL_nosrc = m_logL;
  298 + double ts = 2.0 * (logL_src-logL_nosrc);
  299 + std::string name = free_srcs[i];
  300 + (*models_orig)[name]->ts(ts);
  301 + models = *models_orig;
  302 + }
  303 +
  304 + // Restore best fit values
  305 + // TODO: store fit results before and avoid refitting
  306 + m_obs.models(*models_orig);
292 optimize_lm(); 307 optimize_lm();
293 - double logL_nosrc=m_logL;  
294 - double TS = 2* (logL_src-logL_nosrc);  
295 - std::string name = free_srcs[ii];  
296 - (*models_orig)[name]->ts(TS);  
297 - models = *models_orig;  
298 - }  
299 - // Restore best fit values  
300 - // TODO: store fit results before and avoid refitting  
301 - m_obs.models(*models_orig);  
302 - optimize_lm(); 308 +
  309 + } // endif: did optional TS computation
303 310
304 // Compute number of observed events in all observations 311 // Compute number of observed events in all observations
305 double num_events = 0.0; 312 double num_events = 0.0;
@@ -392,25 +399,6 @@ void ctlike::get_parameters(void) @@ -392,25 +399,6 @@ void ctlike::get_parameters(void)
392 // Try first to open as FITS file 399 // Try first to open as FITS file
393 try { 400 try {
394 401
395 - // Determine whether FITS file has EVENTS extension  
396 - /*  
397 - GFits fits(filename);  
398 - bool is_unbinned = fits.contains("EVENTS");  
399 - fits.close();  
400 - */  
401 -  
402 - // If FITS file has an events header then load as unbinned  
403 - /*  
404 - if (is_unbinned) {  
405 - obs.load_unbinned(filename);  
406 - }  
407 -  
408 - // ... otherwise load as binned  
409 - else {  
410 - obs.load_binned(filename);  
411 - }  
412 - */  
413 -  
414 // Load data 402 // Load data
415 obs.load(filename); 403 obs.load(filename);
416 404
@@ -504,6 +492,7 @@ void ctlike::get_parameters(void) @@ -504,6 +492,7 @@ void ctlike::get_parameters(void)
504 492
505 // Get other parameters 493 // Get other parameters
506 m_refit = (*this)["refit"].boolean(); 494 m_refit = (*this)["refit"].boolean();
  495 + m_tscalc = (*this)["tscalc"].boolean();
507 m_apply_edisp = (*this)["edisp"].boolean(); 496 m_apply_edisp = (*this)["edisp"].boolean();
508 497
509 // Optionally read ahead parameters so that they get correctly 498 // Optionally read ahead parameters so that they get correctly
@@ -587,6 +576,7 @@ void ctlike::init_members(void) @@ -587,6 +576,7 @@ void ctlike::init_members(void)
587 m_outmdl.clear(); 576 m_outmdl.clear();
588 m_obs.clear(); 577 m_obs.clear();
589 m_refit = false; 578 m_refit = false;
  579 + m_tscalc = false;
590 m_max_iter = 100; // Set maximum number of iterations 580 m_max_iter = 100; // Set maximum number of iterations
591 m_max_stall = 10; // Set maximum number of stalls 581 m_max_stall = 10; // Set maximum number of stalls
592 m_logL = 0.0; 582 m_logL = 0.0;
@@ -612,6 +602,7 @@ void ctlike::copy_members(const ctlike&amp; app) @@ -612,6 +602,7 @@ void ctlike::copy_members(const ctlike&amp; app)
612 // Copy attributes 602 // Copy attributes
613 m_stat = app.m_stat; 603 m_stat = app.m_stat;
614 m_refit = app.m_refit; 604 m_refit = app.m_refit;
  605 + m_tscalc = app.m_tscalc;
615 m_caldb = app.m_caldb; 606 m_caldb = app.m_caldb;
616 m_irf = app.m_irf; 607 m_irf = app.m_irf;
617 m_outmdl = app.m_outmdl; 608 m_outmdl = app.m_outmdl;
src/ctlike/ctlike.hpp
@@ -73,6 +73,7 @@ protected: @@ -73,6 +73,7 @@ protected:
73 // User parameters 73 // User parameters
74 std::string m_stat; //!< Optimisation statistics (poisson/gaussian) 74 std::string m_stat; //!< Optimisation statistics (poisson/gaussian)
75 bool m_refit; //!< Refitting 75 bool m_refit; //!< Refitting
  76 + bool m_tscalc; //!< TS computation
76 std::string m_caldb; //!< Calibration database 77 std::string m_caldb; //!< Calibration database
77 std::string m_irf; //!< Instrument response functions 78 std::string m_irf; //!< Instrument response functions
78 std::string m_outmdl; //!< Source model output XML file 79 std::string m_outmdl; //!< Source model output XML file
src/ctlike/ctlike.par
@@ -21,6 +21,7 @@ infile, f, a, &quot;events.fits&quot;,,, &quot;Event list, counts map or observation definition @@ -21,6 +21,7 @@ infile, f, a, &quot;events.fits&quot;,,, &quot;Event list, counts map or observation definition
21 stat, s, h, "POISSON",,, "Optimization statistics" 21 stat, s, h, "POISSON",,, "Optimization statistics"
22 edisp, b, h, no,,, "Apply energy dispersion?" 22 edisp, b, h, no,,, "Apply energy dispersion?"
23 refit, b, h, no,,, "Do refitting?" 23 refit, b, h, no,,, "Do refitting?"
  24 +tscalc, b, h, no,,, "Compute TS values?"
24 caldb, s, a, "dummy",,, "Calibration database" 25 caldb, s, a, "dummy",,, "Calibration database"
25 irf, s, a, "cta_dummy_irf",,, "Instrument response function" 26 irf, s, a, "cta_dummy_irf",,, "Instrument response function"
26 srcmdl, f, a, "$CTOOLS/share/models/crab.xml",,, "Source model" 27 srcmdl, f, a, "$CTOOLS/share/models/crab.xml",,, "Source model"