Commit f8b62516ae64a94d7649802b8fcc83549d983a23
Merge branch 'integration' into devel
Showing
3 changed files
with
46 additions
and
29 deletions
pyext/ctools/obsutils.py
| ... | ... | @@ -57,7 +57,7 @@ def sim(obs, log=False, debug=False, chatter=2, edisp=False, seed=0, nbins=0, |
| 57 | 57 | sim["debug"].boolean(True) |
| 58 | 58 | |
| 59 | 59 | # Set chatter level |
| 60 | - sim["chatter"].chatter = chatter | |
| 60 | + sim["chatter"].integer(chatter) | |
| 61 | 61 | |
| 62 | 62 | # Run ctobssim application. This will loop over all observations in the |
| 63 | 63 | # container and simulation the events for each observation. Note that |
| ... | ... | @@ -105,7 +105,7 @@ def sim(obs, log=False, debug=False, chatter=2, edisp=False, seed=0, nbins=0, |
| 105 | 105 | bin["debug"].boolean(True) |
| 106 | 106 | |
| 107 | 107 | # Set chatter level |
| 108 | - bin["chatter"].chatter = chatter | |
| 108 | + bin["chatter"].integer(chatter) | |
| 109 | 109 | |
| 110 | 110 | # Run ctbin application. This will loop over all observations in |
| 111 | 111 | # the container and bin the events in counts maps |
| ... | ... | @@ -157,7 +157,7 @@ def fit(obs, log=False, debug=False, chatter=2, edisp=False): |
| 157 | 157 | like["debug"].boolean(True) |
| 158 | 158 | |
| 159 | 159 | # Set chatter level |
| 160 | - like["chatter"].chatter = chatter | |
| 160 | + like["chatter"].integer(chatter) | |
| 161 | 161 | |
| 162 | 162 | # Optionally apply energy dispersion |
| 163 | 163 | like["edisp"].boolean(edisp) |
| ... | ... | @@ -172,12 +172,13 @@ def fit(obs, log=False, debug=False, chatter=2, edisp=False): |
| 172 | 172 | # ============================================================== # |
| 173 | 173 | # Fit observations and determine errors using likelihood profile # |
| 174 | 174 | # ============================================================== # |
| 175 | -def cterror(obs, log=False, debug=False, chatter=2): | |
| 175 | +def cterror(obs, srcname, log=False, debug=False, chatter=2): | |
| 176 | 176 | """ |
| 177 | 177 | Perform maximum likelihood fitting of observations in the container. |
| 178 | 178 | |
| 179 | 179 | Parameters: |
| 180 | - obs - Observation container | |
| 180 | + obs - Observation container | |
| 181 | + srcname - Source name | |
| 181 | 182 | Keywords: |
| 182 | 183 | log - Create log file(s) |
| 183 | 184 | debug - Create screen dump |
| ... | ... | @@ -186,6 +187,9 @@ def cterror(obs, log=False, debug=False, chatter=2): |
| 186 | 187 | """ |
| 187 | 188 | # Allocate cterror application |
| 188 | 189 | error = ctools.cterror(obs) |
| 190 | + | |
| 191 | + # Set cterror parameters | |
| 192 | + error["srcname"].string(srcname) | |
| 189 | 193 | |
| 190 | 194 | # Optionally open the log file |
| 191 | 195 | if log: |
| ... | ... | @@ -196,7 +200,7 @@ def cterror(obs, log=False, debug=False, chatter=2): |
| 196 | 200 | error["debug"].boolean(True) |
| 197 | 201 | |
| 198 | 202 | # Set chatter level |
| 199 | - error["chatter"].chatter = chatter | |
| 203 | + error["chatter"].integer(chatter) | |
| 200 | 204 | |
| 201 | 205 | # Run cterror application. |
| 202 | 206 | error.run() | ... | ... |
scripts/cspull.py
| ... | ... | @@ -166,8 +166,9 @@ class cspull(ctools.cscript): |
| 166 | 166 | self.m_profile = self["profile"].boolean() |
| 167 | 167 | |
| 168 | 168 | # Set some fixed parameters |
| 169 | - self.m_log = False # Logging in client tools | |
| 170 | - self.m_debug = self["debug"].boolean() # Debugging in client tools | |
| 169 | + self.m_log = False # Logging in client tools | |
| 170 | + self.m_chatter = self["chatter"].integer() # Chatter level | |
| 171 | + self.m_debug = self["debug"].boolean() # Debugging in client tools | |
| 171 | 172 | |
| 172 | 173 | # Return |
| 173 | 174 | return |
| ... | ... | @@ -276,7 +277,7 @@ class cspull(ctools.cscript): |
| 276 | 277 | seed - Random number generator seed |
| 277 | 278 | """ |
| 278 | 279 | # Write header |
| 279 | - if self.logExplicit(): | |
| 280 | + if self.logNormal(): | |
| 280 | 281 | self.log.header2("Trial "+str(seed+1)) |
| 281 | 282 | |
| 282 | 283 | # Simulate events |
| ... | ... | @@ -286,7 +287,9 @@ class cspull(ctools.cscript): |
| 286 | 287 | binsz=self.m_binsz, \ |
| 287 | 288 | npix=self.m_npix, \ |
| 288 | 289 | edisp=self.m_edisp, \ |
| 289 | - log=self.m_log, debug=self.m_debug) | |
| 290 | + log=self.m_log, \ | |
| 291 | + debug=self.m_debug, \ | |
| 292 | + chatter=self.m_chatter) | |
| 290 | 293 | |
| 291 | 294 | # Determine number of events in simulation |
| 292 | 295 | nevents = 0.0 |
| ... | ... | @@ -294,7 +297,7 @@ class cspull(ctools.cscript): |
| 294 | 297 | nevents += run.events().number() |
| 295 | 298 | |
| 296 | 299 | # Write simulation results |
| 297 | - if self.logExplicit(): | |
| 300 | + if self.logNormal(): | |
| 298 | 301 | self.log.header3("Simulation") |
| 299 | 302 | self.log.parformat("Number of simulated events") |
| 300 | 303 | self.log(nevents) |
| ... | ... | @@ -302,11 +305,18 @@ class cspull(ctools.cscript): |
| 302 | 305 | |
| 303 | 306 | # Fit model |
| 304 | 307 | if self.m_profile: |
| 305 | - like = obsutils.cterror(obs, \ | |
| 306 | - log=self.m_log, debug=self.m_debug) | |
| 308 | + models = obs.models() | |
| 309 | + for i in range(models.size()): | |
| 310 | + model_name = models[i].name() | |
| 311 | + like = obsutils.cterror(obs, model_name, \ | |
| 312 | + log=self.m_log, \ | |
| 313 | + debug=self.m_debug, | |
| 314 | + chatter=self.m_chatter) | |
| 307 | 315 | else: |
| 308 | 316 | like = obsutils.fit(obs, edisp=self.m_edisp, \ |
| 309 | - log=self.m_log, debug=self.m_debug) | |
| 317 | + log=self.m_log, \ | |
| 318 | + debug=self.m_debug, \ | |
| 319 | + chatter=self.m_chatter) | |
| 310 | 320 | |
| 311 | 321 | # Store results |
| 312 | 322 | logL = like.opt().value() |
| ... | ... | @@ -314,7 +324,7 @@ class cspull(ctools.cscript): |
| 314 | 324 | models = like.obs().models() |
| 315 | 325 | |
| 316 | 326 | # Write result header |
| 317 | - if self.logExplicit(): | |
| 327 | + if self.logNormal(): | |
| 318 | 328 | self.log.header3("Pulls") |
| 319 | 329 | |
| 320 | 330 | # Gather results |
| ... | ... | @@ -356,7 +366,7 @@ class cspull(ctools.cscript): |
| 356 | 366 | values["Unc_"+name] = error |
| 357 | 367 | |
| 358 | 368 | # Write result |
| 359 | - if self.logExplicit(): | |
| 369 | + if self.logNormal(): | |
| 360 | 370 | self.log.parformat(name) |
| 361 | 371 | self.log(pull) |
| 362 | 372 | self.log(" (") | ... | ... |
src/cterror/cterror.cpp
| ... | ... | @@ -37,7 +37,6 @@ |
| 37 | 37 | #define G_GET_PARAMETERS "cterror::get_parameters()" |
| 38 | 38 | #define G_ERR_BISECTION "cterror::error_bisection(double&, double&)" |
| 39 | 39 | #define G_EVALUATE "cterror::evaluate(double&)" |
| 40 | -#define G_RUN "cterror::run()" | |
| 41 | 40 | |
| 42 | 41 | /* __ Debug definitions __________________________________________________ */ |
| 43 | 42 | |
| ... | ... | @@ -241,6 +240,9 @@ void cterror::run(void) |
| 241 | 240 | m_obs.errors(*m_opt); |
| 242 | 241 | m_best_logL = m_obs.logL(); |
| 243 | 242 | |
| 243 | + // Store optimizer for later recovery | |
| 244 | + GOptimizerLM best_opt = *m_opt; | |
| 245 | + | |
| 244 | 246 | // Write optimised model into logger |
| 245 | 247 | if (logTerse()) { |
| 246 | 248 | log << *m_opt << std::endl; |
| ... | ... | @@ -255,14 +257,8 @@ void cterror::run(void) |
| 255 | 257 | // Save best fitting models |
| 256 | 258 | GModels models_best = m_obs.models(); |
| 257 | 259 | |
| 258 | - // Get pointer on sky model | |
| 259 | - GModelSky* model = dynamic_cast<GModelSky*>(models_best[m_srcname]); | |
| 260 | - if (model == NULL) { | |
| 261 | - std::string msg = "Source \""+m_srcname+"\" is not a sky model. " | |
| 262 | - "Please specify the name of a sky model for " | |
| 263 | - "parameter error computation."; | |
| 264 | - throw GException::invalid_value(G_RUN, msg); | |
| 265 | - } | |
| 260 | + // Get pointer on model | |
| 261 | + GModel* model = models_best[m_srcname]; | |
| 266 | 262 | |
| 267 | 263 | // Get number of parameters |
| 268 | 264 | int npars = model->size(); |
| ... | ... | @@ -278,10 +274,9 @@ void cterror::run(void) |
| 278 | 274 | // Initialise with best fitting models |
| 279 | 275 | m_obs.models(models_best); |
| 280 | 276 | |
| 281 | - // Get pointer on sky model parameter | |
| 282 | - GModels& current_models = const_cast<GModels&>(m_obs.models()); | |
| 283 | - GModelSky* current_model = dynamic_cast<GModelSky*>(current_models[m_srcname]); | |
| 284 | - m_model_par = &(current_model->at(i)); | |
| 277 | + // Get pointer on model parameter | |
| 278 | + GModels& current_models = const_cast<GModels&>(m_obs.models()); | |
| 279 | + m_model_par = &(current_models[m_srcname]->at(i)); | |
| 285 | 280 | |
| 286 | 281 | // Extract current value |
| 287 | 282 | m_value = m_model_par->factor_value(); |
| ... | ... | @@ -360,6 +355,9 @@ void cterror::run(void) |
| 360 | 355 | |
| 361 | 356 | } // endif: source model exists |
| 362 | 357 | |
| 358 | + // Recover optimizer | |
| 359 | + *m_opt = best_opt; | |
| 360 | + | |
| 363 | 361 | // Return |
| 364 | 362 | return; |
| 365 | 363 | } |
| ... | ... | @@ -595,6 +593,11 @@ double cterror::error_bisection(const double& min, const double& max) |
| 595 | 593 | break; |
| 596 | 594 | } |
| 597 | 595 | |
| 596 | + // Check if interval is smaller than 1.0e-6 | |
| 597 | + if (std::abs(wrk_max-wrk_min) < 1.0e-6) { | |
| 598 | + break; | |
| 599 | + } | |
| 600 | + | |
| 598 | 601 | // If we are on the crescent side of the parabola ... |
| 599 | 602 | if (mid > m_value) { |
| 600 | 603 | ... | ... |