From a5d5b18e928630388ca9cff974931e84255c9958 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Sun, 6 Dec 2015 01:23:46 +0100 Subject: [PATCH] Do not allow counts cube as inobs for ctbin, ctexpcube, ctpsfcube and ctbkgcube. --- pyext/ctool.i | 1 + src/ctbin/ctbin.cpp | 3 +++ src/ctbkgcube/ctbkgcube.cpp | 3 +++ src/ctexpcube/ctexpcube.cpp | 3 +++ src/ctpsfcube/ctpsfcube.cpp | 3 +++ src/support/ctool.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- src/support/ctool.hpp | 1 + 7 files changed, 76 insertions(+), 5 deletions(-) diff --git a/pyext/ctool.i b/pyext/ctool.i index 23cfb08..19d2c80 100644 --- a/pyext/ctool.i +++ b/pyext/ctool.i @@ -99,6 +99,7 @@ public: // Protected methods that check user parameters void require_inobs(const std::string& method); + void require_inobs_nocube(const std::string& method); // Protected support methods void set_response(GObservations& obs); diff --git a/src/ctbin/ctbin.cpp b/src/ctbin/ctbin.cpp index 8dce155..ddffa12 100644 --- a/src/ctbin/ctbin.cpp +++ b/src/ctbin/ctbin.cpp @@ -434,6 +434,9 @@ void ctbin::get_parameters(void) // Throw exception if no input observation file is given require_inobs(G_GET_PARAMETERS); + // Throw exception if counts cube is given + require_inobs_nocube(G_GET_PARAMETERS); + // Get observation container without response (not needed) m_obs = get_observations(false); diff --git a/src/ctbkgcube/ctbkgcube.cpp b/src/ctbkgcube/ctbkgcube.cpp index 6da5b12..47535b0 100644 --- a/src/ctbkgcube/ctbkgcube.cpp +++ b/src/ctbkgcube/ctbkgcube.cpp @@ -450,6 +450,9 @@ void ctbkgcube::get_parameters(void) // Throw exception if no input observation file is given require_inobs(G_GET_PARAMETERS); + // Throw exception if counts cube is given + require_inobs_nocube(G_GET_PARAMETERS); + // Build observation container m_obs = get_observations(); diff --git a/src/ctexpcube/ctexpcube.cpp b/src/ctexpcube/ctexpcube.cpp index 90f610e..21bab79 100644 --- a/src/ctexpcube/ctexpcube.cpp +++ b/src/ctexpcube/ctexpcube.cpp @@ -354,6 +354,9 @@ void ctexpcube::get_parameters(void) // Throw exception if no input observation file is given require_inobs(G_GET_PARAMETERS); + // Throw exception if counts cube is given + require_inobs_nocube(G_GET_PARAMETERS); + // Build observation container m_obs = get_observations(); diff --git a/src/ctpsfcube/ctpsfcube.cpp b/src/ctpsfcube/ctpsfcube.cpp index bbb083a..31b9b56 100644 --- a/src/ctpsfcube/ctpsfcube.cpp +++ b/src/ctpsfcube/ctpsfcube.cpp @@ -353,6 +353,9 @@ void ctpsfcube::get_parameters(void) // Throw exception if no input observation file is given require_inobs(G_GET_PARAMETERS); + // Throw exception if counts cube is given + require_inobs_nocube(G_GET_PARAMETERS); + // Build observation container m_obs = get_observations(); diff --git a/src/support/ctool.cpp b/src/support/ctool.cpp index 59cbda6..c64d41f 100644 --- a/src/support/ctool.cpp +++ b/src/support/ctool.cpp @@ -656,18 +656,23 @@ GCTAObservation ctool::create_cta_obs(void) /***********************************************************************//** - * @brief Throws exception in inobs parameter is not valid + * @brief Throws exception if inobs parameter is not valid * * @param[in] method Method name. + * + * Throw an exception if the inobs parameter is either NONE or an empty + * string. ***************************************************************************/ void ctool::require_inobs(const std::string& method) { + // Get inobs filename + std::string filename = (*this)["inobs"].filename(); + // Throw exception if no infile is given - if ((*this)["inobs"].filename() == "NONE" || - (*this)["inobs"].filename() == "") { + if (filename == "NONE" || gammalib::strip_whitespace(filename) == "") { std::string msg = "A valid file needs to be specified for the " - "\"inobs\" parameter, yet \""+ - (*this)["inobs"].filename()+"\" was given." + "\"inobs\" parameter, yet \""+filename+ + "\" was given." " Specify a valid observation definition or " "FITS file to proceed."; throw GException::invalid_value(method, msg); @@ -679,6 +684,58 @@ void ctool::require_inobs(const std::string& method) /***********************************************************************//** + * @brief Throws exception if inobs parameter is a counts cube + * + * @param[in] method Method name. + * + * Throw an exception if the inobs parameter is a counts cube. + ***************************************************************************/ +void ctool::require_inobs_nocube(const std::string& method) +{ + // Get inobs filename + std::string filename = (*this)["inobs"].filename(); + + // Continue only if we have a FITS file + if (gammalib::is_fits(filename)) { + + // Signal su + bool is_cube = false; + + // Try loading file as counts cube. If this is successful then + // throw an exception + try { + + // Load cube from file + GCTAEventCube cube(filename); + + // If we're still alive then signal that we have a cube + is_cube = true; + + } + + // Catch any exceptions + catch (...) { + ; + } + + // If we have a counts cube then throw an exception + if (is_cube) { + std::string msg = "A counts cube has been specified for the " + "\"inobs\" parameter, yet no counts cube " + "can be specified as input observation." + " Instead, specify an event list or an " + "observation definition file."; + throw GException::invalid_value(method, msg); + } + + } // endif: we had a FITS file + + // Return + return; +} + + +/***********************************************************************//** * @brief Set response for all CTA observations in container * * @param[in,out] obs Observation container diff --git a/src/support/ctool.hpp b/src/support/ctool.hpp index 13672d5..aa4ac8c 100644 --- a/src/support/ctool.hpp +++ b/src/support/ctool.hpp @@ -81,6 +81,7 @@ protected: // Protected methods that check user parameters void require_inobs(const std::string& method); + void require_inobs_nocube(const std::string& method); // Protected support methods void set_response(GObservations& obs); -- libgit2 0.21.4