From 780713c2a73e0d81f35bb9fde968c402bb0db94f Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Tue, 24 Feb 2015 08:14:11 +0100 Subject: [PATCH] Add source name check before accessing source model in get_model_parameter() method. --- src/ctulimit/ctulimit.cpp | 55 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/src/ctulimit/ctulimit.cpp b/src/ctulimit/ctulimit.cpp index b32b095..44e3d50 100644 --- a/src/ctulimit/ctulimit.cpp +++ b/src/ctulimit/ctulimit.cpp @@ -537,31 +537,36 @@ void ctulimit::get_parameters(void) ***************************************************************************/ void ctulimit::get_model_parameter(void) { - // Get relevant model and parameter for upper limit computation. - GModels& models = const_cast(m_obs.models()); - m_skymodel = dynamic_cast(models[m_srcname]); - if (m_skymodel == NULL) { - std::string msg = "Source \""+m_srcname+"\" is not a sky model. " - "Please specify the name of a sky model for " - "upper limit computation."; - throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg); - } - if (m_skymodel->spectral()->has_par("Normalization")) { - m_model_par = &(m_skymodel->spectral()->operator[]("Normalization")); - } - else if (m_skymodel->spectral()->has_par("Prefactor")) { - m_model_par = &(m_skymodel->spectral()->operator[]("Prefactor")); - } - else if (m_skymodel->spectral()->has_par("Integral")) { - m_model_par = &(m_skymodel->spectral()->operator[]("Integral")); - } - else { - std::string msg = "Require spectral parameter \"Normalization\", " - "\"Prefactor\" or \"Integral\" for upper limit " - "computation. The specified source \""+m_srcname+ - "\" does not have such a parameter."; - throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg); - } + // Continue only if source model exists + if (m_obs.models().contains(m_srcname)) { + + // Get relevant model and parameter for upper limit computation. + GModels& models = const_cast(m_obs.models()); + m_skymodel = dynamic_cast(models[m_srcname]); + if (m_skymodel == NULL) { + std::string msg = "Source \""+m_srcname+"\" is not a sky model. " + "Please specify the name of a sky model for " + "upper limit computation."; + throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg); + } + if (m_skymodel->spectral()->has_par("Normalization")) { + m_model_par = &(m_skymodel->spectral()->operator[]("Normalization")); + } + else if (m_skymodel->spectral()->has_par("Prefactor")) { + m_model_par = &(m_skymodel->spectral()->operator[]("Prefactor")); + } + else if (m_skymodel->spectral()->has_par("Integral")) { + m_model_par = &(m_skymodel->spectral()->operator[]("Integral")); + } + else { + std::string msg = "Require spectral parameter \"Normalization\", " + "\"Prefactor\" or \"Integral\" for upper limit " + "computation. The specified source \""+m_srcname+ + "\" does not have such a parameter."; + throw GException::invalid_value(G_GET_MODEL_PARAMETER, msg); + } + + } // endif: source model existed // Return return; -- libgit2 0.21.4