Commit 780713c2a73e0d81f35bb9fde968c402bb0db94f

Authored by Jürgen Knödlseder
1 parent c5b3321f

Add source name check before accessing source model in get_model_parameter() method.

Showing 1 changed file with 30 additions and 25 deletions
src/ctulimit/ctulimit.cpp
... ... @@ -537,31 +537,36 @@ void ctulimit::get_parameters(void)
537 537 ***************************************************************************/
538 538 void ctulimit::get_model_parameter(void)
539 539 {
540   - // Get relevant model and parameter for upper limit computation.
541   - GModels& models = const_cast<GModels&>(m_obs.models());
542   - m_skymodel = dynamic_cast<GModelSky*>(models[m_srcname]);
543   - if (m_skymodel == NULL) {
544   - std::string msg = "Source \""+m_srcname+"\" is not a sky model. "
545   - "Please specify the name of a sky model for "
546   - "upper limit computation.";
547   - throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg);
548   - }
549   - if (m_skymodel->spectral()->has_par("Normalization")) {
550   - m_model_par = &(m_skymodel->spectral()->operator[]("Normalization"));
551   - }
552   - else if (m_skymodel->spectral()->has_par("Prefactor")) {
553   - m_model_par = &(m_skymodel->spectral()->operator[]("Prefactor"));
554   - }
555   - else if (m_skymodel->spectral()->has_par("Integral")) {
556   - m_model_par = &(m_skymodel->spectral()->operator[]("Integral"));
557   - }
558   - else {
559   - std::string msg = "Require spectral parameter \"Normalization\", "
560   - "\"Prefactor\" or \"Integral\" for upper limit "
561   - "computation. The specified source \""+m_srcname+
562   - "\" does not have such a parameter.";
563   - throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg);
564   - }
  540 + // Continue only if source model exists
  541 + if (m_obs.models().contains(m_srcname)) {
  542 +
  543 + // Get relevant model and parameter for upper limit computation.
  544 + GModels& models = const_cast<GModels&>(m_obs.models());
  545 + m_skymodel = dynamic_cast<GModelSky*>(models[m_srcname]);
  546 + if (m_skymodel == NULL) {
  547 + std::string msg = "Source \""+m_srcname+"\" is not a sky model. "
  548 + "Please specify the name of a sky model for "
  549 + "upper limit computation.";
  550 + throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg);
  551 + }
  552 + if (m_skymodel->spectral()->has_par("Normalization")) {
  553 + m_model_par = &(m_skymodel->spectral()->operator[]("Normalization"));
  554 + }
  555 + else if (m_skymodel->spectral()->has_par("Prefactor")) {
  556 + m_model_par = &(m_skymodel->spectral()->operator[]("Prefactor"));
  557 + }
  558 + else if (m_skymodel->spectral()->has_par("Integral")) {
  559 + m_model_par = &(m_skymodel->spectral()->operator[]("Integral"));
  560 + }
  561 + else {
  562 + std::string msg = "Require spectral parameter \"Normalization\", "
  563 + "\"Prefactor\" or \"Integral\" for upper limit "
  564 + "computation. The specified source \""+m_srcname+
  565 + "\" does not have such a parameter.";
  566 + throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg);
  567 + }
  568 +
  569 + } // endif: source model existed
565 570  
566 571 // Return
567 572 return;
... ...