Commit edef69715da57cdb47b7009b2005a70455e3a93f
1 parent
95b93e02
Integrate code
Showing
2 changed files
with
113 additions
and
159 deletions
src/ctulimit/ctulimit.cpp
| ... | ... | @@ -237,17 +237,8 @@ void ctulimit::run(void) |
| 237 | 237 | } |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | - // Write observation(s) into logger | |
| 241 | - if (logTerse()) { | |
| 242 | - log << std::endl; | |
| 243 | - if (m_obs.size() > 1) { | |
| 244 | - log.header1("Observations"); | |
| 245 | - } | |
| 246 | - else { | |
| 247 | - log.header1("Observation"); | |
| 248 | - } | |
| 249 | - log << m_obs << std::endl; | |
| 250 | - } | |
| 240 | + // Write input observation container into logger | |
| 241 | + log_observations(NORMAL, m_obs, "Input observation"); | |
| 251 | 242 | |
| 252 | 243 | // Save original models |
| 253 | 244 | GModels models_orig = m_obs.models(); |
| ... | ... | @@ -260,27 +251,21 @@ void ctulimit::run(void) |
| 260 | 251 | m_best_logL = m_obs.logL(); |
| 261 | 252 | if (m_best_logL == 0.0) { |
| 262 | 253 | |
| 263 | - // Write header | |
| 264 | - if (logTerse()) { | |
| 265 | - log << std::endl; | |
| 266 | - log.header1("Compute best-fit likelihood"); | |
| 267 | - } | |
| 254 | + // Write header into logger | |
| 255 | + log_header1(TERSE, "Compute best-fit likelihood"); | |
| 268 | 256 | |
| 269 | - // Optimize and save best log-likelihood | |
| 270 | - m_obs.optimize(m_opt); | |
| 271 | - m_obs.errors(m_opt); | |
| 272 | - m_best_logL = m_obs.logL(); | |
| 257 | + // Optimize and save best log-likelihood | |
| 258 | + m_obs.optimize(m_opt); | |
| 259 | + m_obs.errors(m_opt); | |
| 260 | + m_best_logL = m_obs.logL(); | |
| 273 | 261 | |
| 274 | - // Store optimizer for later recovery | |
| 275 | - best_opt = m_opt; | |
| 262 | + // Store optimizer for later recovery | |
| 263 | + best_opt = m_opt; | |
| 276 | 264 | |
| 277 | - // Write optimised model into logger | |
| 278 | - if (logTerse()) { | |
| 279 | - log << m_opt << std::endl; | |
| 280 | - log << gammalib::parformat("Maximum log likelihood"); | |
| 281 | - log << gammalib::str(m_best_logL,3) << std::endl; | |
| 282 | - log << m_obs.models() << std::endl; | |
| 283 | - } | |
| 265 | + // Write optimised model into logger | |
| 266 | + log_string(NORMAL, m_opt.print(m_chatter)); | |
| 267 | + log_value(NORMAL,"Maximum log likelihood",gammalib::str(m_best_logL,3)); | |
| 268 | + log_string(NORMAL, m_obs.models().print(m_chatter)); | |
| 284 | 269 | |
| 285 | 270 | } // endif: likelihood was zero |
| 286 | 271 | |
| ... | ... | @@ -299,34 +284,22 @@ void ctulimit::run(void) |
| 299 | 284 | double parmax = std::min(m_model_par->factor_max(), |
| 300 | 285 | value + m_sigma_max * error); |
| 301 | 286 | |
| 302 | - // Write header | |
| 303 | - if (logTerse()) { | |
| 304 | - log << std::endl; | |
| 305 | - log.header1("Compute upper limit"); | |
| 306 | - log << gammalib::parformat("Model name"); | |
| 307 | - log << m_skymodel->name() << std::endl; | |
| 308 | - log << gammalib::parformat("Parameter name"); | |
| 309 | - log << m_model_par->name() << std::endl; | |
| 310 | - log << gammalib::parformat("Confidence level"); | |
| 311 | - log << m_confidence*100.0 << "%" << std::endl; | |
| 312 | - log << gammalib::parformat("Log-likelihood difference"); | |
| 313 | - log << m_dlogL << std::endl; | |
| 314 | - log << gammalib::parformat("Initial parameter range"); | |
| 315 | - log << "["; | |
| 316 | - log << parmin; | |
| 317 | - log << ", "; | |
| 318 | - log << parmax; | |
| 319 | - log << "]" << std::endl; | |
| 320 | - } | |
| 287 | + // Write header into logger | |
| 288 | + log_header1(TERSE, "Compute upper limit"); | |
| 289 | + | |
| 290 | + // Write some parameters into logger | |
| 291 | + log_value(NORMAL, "Model name", m_skymodel->name()); | |
| 292 | + log_value(NORMAL, "Parameter name", m_model_par->name()); | |
| 293 | + log_value(NORMAL, "Confidence level", gammalib::str(m_confidence*100.0)+"%"); | |
| 294 | + log_value(NORMAL, "Log-likelihood difference", m_dlogL); | |
| 295 | + log_value(NORMAL, "Initial parameter range", | |
| 296 | + "["+gammalib::str(parmin)+", "+gammalib::str(parmax)+"]"); | |
| 321 | 297 | |
| 322 | 298 | // Compute upper limit |
| 323 | 299 | ulimit_bisection(parmin, parmax); |
| 324 | 300 | |
| 325 | - // Write final parameter range | |
| 326 | - if (logTerse()) { | |
| 327 | - log << gammalib::parformat("Final parameter"); | |
| 328 | - log << m_model_par->value() << std::endl; | |
| 329 | - } | |
| 301 | + // Write final parameter into logger | |
| 302 | + log_value(NORMAL, "Final parameter", m_model_par->value()); | |
| 330 | 303 | |
| 331 | 304 | // Get reference energy for differential upper limit |
| 332 | 305 | GEnergy eref = GEnergy(m_eref, "TeV"); |
| ... | ... | @@ -340,28 +313,21 @@ void ctulimit::run(void) |
| 340 | 313 | m_flux_ulimit = m_skymodel->spectral()->flux(emin, emax); |
| 341 | 314 | m_eflux_ulimit = m_skymodel->spectral()->eflux(emin, emax); |
| 342 | 315 | |
| 343 | - // Write results to logfile | |
| 344 | - if (logTerse()) { | |
| 345 | - log << std::endl; | |
| 346 | - log.header1("Upper limit results"); | |
| 347 | - log << gammalib::parformat("Differential flux limit"); | |
| 348 | - log << m_diff_ulimit; | |
| 349 | - log << " ph/cm2/s/MeV at "; | |
| 350 | - log << m_eref << " TeV"; | |
| 351 | - log << std::endl; | |
| 352 | - log << gammalib::parformat("Integral flux limit"); | |
| 353 | - log << m_flux_ulimit; | |
| 354 | - log << " ph/cm2/s within ["; | |
| 355 | - log << m_emin << "-" << m_emax; | |
| 356 | - log << "] TeV"; | |
| 357 | - log << std::endl; | |
| 358 | - log << gammalib::parformat("Energy flux limit"); | |
| 359 | - log << m_eflux_ulimit; | |
| 360 | - log << " erg/cm2/s within ["; | |
| 361 | - log << m_emin << "-" << m_emax; | |
| 362 | - log << "] TeV"; | |
| 363 | - log << std::endl; | |
| 364 | - } | |
| 316 | + // Write header into logger | |
| 317 | + log_header1(TERSE, "Upper limit results"); | |
| 318 | + | |
| 319 | + // Write result into logger | |
| 320 | + log_value(TERSE, "Differential flux limit", | |
| 321 | + gammalib::str(m_diff_ulimit)+" ph/cm2/s/MeV at "+ | |
| 322 | + gammalib::str(m_eref)+" TeV"); | |
| 323 | + log_value(TERSE, "Integral flux limit", | |
| 324 | + gammalib::str(m_flux_ulimit)+" ph/cm2/s within ["+ | |
| 325 | + gammalib::str(m_emin)+"-"+ | |
| 326 | + gammalib::str(m_emax)+"] TeV"); | |
| 327 | + log_value(TERSE, "Energy flux limit", | |
| 328 | + gammalib::str(m_eflux_ulimit)+" erg/cm2/s within ["+ | |
| 329 | + gammalib::str(m_emin)+"-"+ | |
| 330 | + gammalib::str(m_emax)+"] TeV"); | |
| 365 | 331 | |
| 366 | 332 | // Recover original models |
| 367 | 333 | m_obs.models(models_orig); |
| ... | ... | @@ -392,27 +358,6 @@ void ctulimit::run(void) |
| 392 | 358 | ***************************************************************************/ |
| 393 | 359 | void ctulimit::save(void) |
| 394 | 360 | { |
| 395 | - /* | |
| 396 | - // Write header | |
| 397 | - if (logTerse()) { | |
| 398 | - log << std::endl; | |
| 399 | - log.header1("Save upper limit"); | |
| 400 | - } | |
| 401 | - | |
| 402 | - // Get output filename | |
| 403 | - m_outfile = (*this)["outfile"].filename(); | |
| 404 | - | |
| 405 | - // Create CSV table with 3 columns | |
| 406 | - GCsv table(1, 3); | |
| 407 | - | |
| 408 | - table.real(0, 0, m_diff_ulimit); | |
| 409 | - table.real(0, 1, m_flux_ulimit); | |
| 410 | - table.real(0, 2, m_eflux_ulimit); | |
| 411 | - | |
| 412 | - // Save CSV table | |
| 413 | - table.save(m_outfile, " ", clobber()); | |
| 414 | - */ | |
| 415 | - | |
| 416 | 361 | // Return |
| 417 | 362 | return; |
| 418 | 363 | } |
| ... | ... | @@ -440,6 +385,7 @@ void ctulimit::init_members(void) |
| 440 | 385 | m_tol = 1.0e-6; |
| 441 | 386 | m_max_iter = 50; |
| 442 | 387 | m_apply_edisp = false; |
| 388 | + m_chatter = static_cast<GChatter>(2); | |
| 443 | 389 | |
| 444 | 390 | // Initialise protected members |
| 445 | 391 | m_obs.clear(); |
| ... | ... | @@ -479,6 +425,7 @@ void ctulimit::copy_members(const ctulimit& app) |
| 479 | 425 | m_tol = app.m_tol; |
| 480 | 426 | m_max_iter = app.m_max_iter; |
| 481 | 427 | m_apply_edisp = app.m_apply_edisp; |
| 428 | + m_chatter = app.m_chatter; | |
| 482 | 429 | |
| 483 | 430 | // Copy protected members |
| 484 | 431 | m_obs = app.m_obs; |
| ... | ... | @@ -543,14 +490,9 @@ void ctulimit::get_parameters(void) |
| 543 | 490 | |
| 544 | 491 | } // endif: no models were associated with observations |
| 545 | 492 | |
| 546 | - // Get name of test source and check container for this name | |
| 493 | + // Get name of test source. The get_model_parameter() method will check | |
| 494 | + // where a source with the given name exists in the model container. | |
| 547 | 495 | m_srcname = (*this)["srcname"].string(); |
| 548 | - if (!m_obs.models().contains(m_srcname)) { | |
| 549 | - std::string msg = "Source \""+m_srcname+"\" not found in model " | |
| 550 | - "container. Please add a source with that name " | |
| 551 | - "or check for possible typos."; | |
| 552 | - throw GException::invalid_value(G_GET_PARAMETERS, msg); | |
| 553 | - } | |
| 554 | 496 | |
| 555 | 497 | // Get relevant model and parameter for upper limit computation |
| 556 | 498 | get_model_parameter(); |
| ... | ... | @@ -576,6 +518,9 @@ void ctulimit::get_parameters(void) |
| 576 | 518 | m_tol = (*this)["tol"].real(); |
| 577 | 519 | m_max_iter = (*this)["max_iter"].integer(); |
| 578 | 520 | |
| 521 | + // Get remaining parameters | |
| 522 | + m_chatter = static_cast<GChatter>((*this)["chatter"].integer()); | |
| 523 | + | |
| 579 | 524 | // Write parameters into logger |
| 580 | 525 | log_parameters(TERSE); |
| 581 | 526 | |
| ... | ... | @@ -588,59 +533,73 @@ void ctulimit::get_parameters(void) |
| 588 | 533 | * @brief Get application parameters |
| 589 | 534 | * |
| 590 | 535 | * @exception GException::invalid_value |
| 591 | - * Did not find a valid model parameter | |
| 536 | + * Did not find a valid model or model parameter | |
| 592 | 537 | * |
| 593 | 538 | * Extracts a pointer to the sky model (m_skymodel) and a pointer to the |
| 594 | - * relevant model parameter (m_model_par) from the model container. | |
| 539 | + * relevant model parameter (m_model_par) from the model container. If no | |
| 595 | 540 | ***************************************************************************/ |
| 596 | 541 | void ctulimit::get_model_parameter(void) |
| 597 | 542 | { |
| 598 | - // Continue only if source model exists | |
| 599 | - if (m_obs.models().contains(m_srcname)) { | |
| 600 | - | |
| 601 | - // Get relevant model and parameter for upper limit computation. | |
| 602 | - GModels& models = const_cast<GModels&>(m_obs.models()); | |
| 603 | - m_skymodel = dynamic_cast<GModelSky*>(models[m_srcname]); | |
| 604 | - if (m_skymodel == NULL) { | |
| 605 | - std::string msg = "Source \""+m_srcname+"\" is not a sky model. " | |
| 606 | - "Please specify the name of a sky model for " | |
| 607 | - "upper limit computation."; | |
| 608 | - throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg); | |
| 609 | - } | |
| 610 | - if (m_skymodel->spectral()->type() == "NodeFunction") { | |
| 611 | - std::string msg = "\"NodeFunction\" cannot be used as spectral " | |
| 612 | - "model for an upper limit computation. " | |
| 613 | - "Please specify another spectral model."; | |
| 614 | - throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg); | |
| 615 | - } | |
| 616 | - if (m_skymodel->spectral()->has_par("Normalization")) { | |
| 617 | - m_model_par = &(m_skymodel->spectral()->operator[]("Normalization")); | |
| 618 | - } | |
| 619 | - else if (m_skymodel->spectral()->has_par("Prefactor")) { | |
| 620 | - m_model_par = &(m_skymodel->spectral()->operator[]("Prefactor")); | |
| 621 | - } | |
| 622 | - else if (m_skymodel->spectral()->has_par("PhotonFlux")) { | |
| 623 | - m_model_par = &(m_skymodel->spectral()->operator[]("PhotonFlux")); | |
| 624 | - } | |
| 625 | - else if (m_skymodel->spectral()->has_par("EnergyFlux")) { | |
| 626 | - m_model_par = &(m_skymodel->spectral()->operator[]("EnergyFlux")); | |
| 627 | - } | |
| 628 | - else if (m_skymodel->spectral()->has_par("Integral")) { | |
| 629 | - m_model_par = &(m_skymodel->spectral()->operator[]("Integral")); | |
| 630 | - } | |
| 631 | - else if (m_skymodel->spectral()->has_par("Value")) { | |
| 632 | - m_model_par = &(m_skymodel->spectral()->operator[]("Value")); | |
| 633 | - } | |
| 634 | - else { | |
| 635 | - std::string msg = "Require spectral parameter \"Normalization\", " | |
| 636 | - "\"Prefactor\", \"PhotonFlux\", \"EnergyFlux\", " | |
| 637 | - "\"Integral\" or \"Value\" for " | |
| 638 | - "upper limit computation. The specified source " | |
| 639 | - "\""+m_srcname+"\" does not have such a parameter."; | |
| 640 | - throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg); | |
| 543 | + // Define list of valid spectral model parameters, terminated with a | |
| 544 | + // NULL pointer to signal the end of the list | |
| 545 | + const char* pars[] = {"Normalization", "Value", "Prefactor", | |
| 546 | + "Integral", "PhotonFlux", "EnergyFlux", | |
| 547 | + NULL}; | |
| 548 | + | |
| 549 | + // Initialises sky model and model parameters pointers | |
| 550 | + m_skymodel = NULL; | |
| 551 | + m_model_par = NULL; | |
| 552 | + | |
| 553 | + // If the model container does not container the specified source then | |
| 554 | + // throw an exception | |
| 555 | + if (!m_obs.models().contains(m_srcname)) { | |
| 556 | + std::string msg = "Source \""+m_srcname+"\" not found in model " | |
| 557 | + "container. Please add a source with that name " | |
| 558 | + "or check for possible typos."; | |
| 559 | + throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg); | |
| 560 | + } | |
| 561 | + | |
| 562 | + // Get relevant sky model for upper limit computation. If the model is not | |
| 563 | + // a sky model or if the model has a "NodeFunction" as spectral component | |
| 564 | + // then throw an exception. | |
| 565 | + GModels& models = const_cast<GModels&>(m_obs.models()); | |
| 566 | + m_skymodel = dynamic_cast<GModelSky*>(models[m_srcname]); | |
| 567 | + if (m_skymodel == NULL) { | |
| 568 | + std::string msg = "Source \""+m_srcname+"\" is not a sky model. Please " | |
| 569 | + "specify the name of a sky model for upper limit " | |
| 570 | + "computation."; | |
| 571 | + throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg); | |
| 572 | + } | |
| 573 | + if (m_skymodel->spectral()->type() == "NodeFunction") { | |
| 574 | + std::string msg = "\"NodeFunction\" cannot be used as spectral model " | |
| 575 | + "for an upper limit computation. Please specify " | |
| 576 | + "another spectral model."; | |
| 577 | + throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg); | |
| 578 | + } | |
| 579 | + | |
| 580 | + // Find appropriate model parameter and store its pointer in the | |
| 581 | + // m_model_par member | |
| 582 | + for (const char** par = pars; *par != NULL; ++par) { | |
| 583 | + if (m_skymodel->spectral()->has_par(*par)) { | |
| 584 | + m_model_par = &(m_skymodel->spectral()->operator[](*par)); | |
| 585 | + break; | |
| 641 | 586 | } |
| 587 | + } | |
| 642 | 588 | |
| 643 | - } // endif: source model existed | |
| 589 | + // If no model parameter was found then throw an exception | |
| 590 | + if (m_model_par == NULL) { | |
| 591 | + std::string msg = "Require one of the following spectral " | |
| 592 | + "parameters for upper limit computation: "; | |
| 593 | + for (const char** par = pars; *par != NULL; ++par) { | |
| 594 | + if (par != pars) { | |
| 595 | + msg.append(", "); | |
| 596 | + } | |
| 597 | + msg.append("\""+std::string(*par)+"\""); | |
| 598 | + } | |
| 599 | + msg.append(". The specified source \""+m_srcname+"\" does not " | |
| 600 | + "have such a parameter."); | |
| 601 | + throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg); | |
| 602 | + } | |
| 644 | 603 | |
| 645 | 604 | // Return |
| 646 | 605 | return; |
| ... | ... | @@ -668,14 +627,8 @@ void ctulimit::ulimit_bisection(const double& min, const double& max) |
| 668 | 627 | while (true) { |
| 669 | 628 | |
| 670 | 629 | // Log information |
| 671 | - if (logExplicit()) { | |
| 672 | - log << gammalib::parformat("Iteration "+gammalib::str(iter)); | |
| 673 | - log << "["; | |
| 674 | - log << wrk_min; | |
| 675 | - log << ", "; | |
| 676 | - log << wrk_max; | |
| 677 | - log << "]" << std::endl; | |
| 678 | - } | |
| 630 | + log_value(EXPLICIT, "Iteration "+gammalib::str(iter), | |
| 631 | + "["+gammalib::str(wrk_min)+", "+gammalib::str(wrk_max)+"]"); | |
| 679 | 632 | |
| 680 | 633 | // Throw exception if maximum iterations are reached |
| 681 | 634 | if (iter > m_max_iter) { | ... | ... |
src/ctulimit/ctulimit.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * ctulimit - Upper limit calculation tool * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2015 by Michael Mayer * | |
| 4 | + * copyright (C) 2015-2016 by Michael Mayer * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -34,7 +34,7 @@ |
| 34 | 34 | |
| 35 | 35 | /* __Definitions _________________________________________________________ */ |
| 36 | 36 | #define CTULIMIT_NAME "ctulimit" |
| 37 | -#define CTULIMIT_VERSION "01-00-00" | |
| 37 | +#define CTULIMIT_VERSION "1.2.0" | |
| 38 | 38 | |
| 39 | 39 | |
| 40 | 40 | /***********************************************************************//** |
| ... | ... | @@ -94,6 +94,7 @@ protected: |
| 94 | 94 | double m_tol; //!< Tolerance for limit determination |
| 95 | 95 | int m_max_iter; //!< Maximum number of iterations |
| 96 | 96 | bool m_apply_edisp; //!< Apply energy dispersion? |
| 97 | + GChatter m_chatter; //!< Chattiness | |
| 97 | 98 | |
| 98 | 99 | // Protected members |
| 99 | 100 | GObservations m_obs; //!< Observation container | ... | ... |