Commit a5d5b18e928630388ca9cff974931e84255c9958

Authored by Jürgen Knödlseder
1 parent eb60348d

Do not allow counts cube as inobs for ctbin, ctexpcube, ctpsfcube and ctbkgcube.

Added ctool::require_inobs_nocube() method that throws an exception if a counts cube has been specified as inobs parameter. This method is then used by ctbin, ctexpcube, ctpsfcube and ctbkgcube.
pyext/ctool.i
... ... @@ -99,6 +99,7 @@ public:
99 99  
100 100 // Protected methods that check user parameters
101 101 void require_inobs(const std::string& method);
  102 + void require_inobs_nocube(const std::string& method);
102 103  
103 104 // Protected support methods
104 105 void set_response(GObservations& obs);
... ...
src/ctbin/ctbin.cpp
... ... @@ -434,6 +434,9 @@ void ctbin::get_parameters(void)
434 434 // Throw exception if no input observation file is given
435 435 require_inobs(G_GET_PARAMETERS);
436 436  
  437 + // Throw exception if counts cube is given
  438 + require_inobs_nocube(G_GET_PARAMETERS);
  439 +
437 440 // Get observation container without response (not needed)
438 441 m_obs = get_observations(false);
439 442  
... ...
src/ctbkgcube/ctbkgcube.cpp
... ... @@ -450,6 +450,9 @@ void ctbkgcube::get_parameters(void)
450 450 // Throw exception if no input observation file is given
451 451 require_inobs(G_GET_PARAMETERS);
452 452  
  453 + // Throw exception if counts cube is given
  454 + require_inobs_nocube(G_GET_PARAMETERS);
  455 +
453 456 // Build observation container
454 457 m_obs = get_observations();
455 458  
... ...
src/ctexpcube/ctexpcube.cpp
... ... @@ -354,6 +354,9 @@ void ctexpcube::get_parameters(void)
354 354 // Throw exception if no input observation file is given
355 355 require_inobs(G_GET_PARAMETERS);
356 356  
  357 + // Throw exception if counts cube is given
  358 + require_inobs_nocube(G_GET_PARAMETERS);
  359 +
357 360 // Build observation container
358 361 m_obs = get_observations();
359 362  
... ...
src/ctpsfcube/ctpsfcube.cpp
... ... @@ -353,6 +353,9 @@ void ctpsfcube::get_parameters(void)
353 353 // Throw exception if no input observation file is given
354 354 require_inobs(G_GET_PARAMETERS);
355 355  
  356 + // Throw exception if counts cube is given
  357 + require_inobs_nocube(G_GET_PARAMETERS);
  358 +
356 359 // Build observation container
357 360 m_obs = get_observations();
358 361  
... ...
src/support/ctool.cpp
... ... @@ -656,18 +656,23 @@ GCTAObservation ctool::create_cta_obs(void)
656 656  
657 657  
658 658 /***********************************************************************//**
659   - * @brief Throws exception in inobs parameter is not valid
  659 + * @brief Throws exception if inobs parameter is not valid
660 660 *
661 661 * @param[in] method Method name.
  662 + *
  663 + * Throw an exception if the inobs parameter is either NONE or an empty
  664 + * string.
662 665 ***************************************************************************/
663 666 void ctool::require_inobs(const std::string& method)
664 667 {
  668 + // Get inobs filename
  669 + std::string filename = (*this)["inobs"].filename();
  670 +
665 671 // Throw exception if no infile is given
666   - if ((*this)["inobs"].filename() == "NONE" ||
667   - (*this)["inobs"].filename() == "") {
  672 + if (filename == "NONE" || gammalib::strip_whitespace(filename) == "") {
668 673 std::string msg = "A valid file needs to be specified for the "
669   - "\"inobs\" parameter, yet \""+
670   - (*this)["inobs"].filename()+"\" was given."
  674 + "\"inobs\" parameter, yet \""+filename+
  675 + "\" was given."
671 676 " Specify a valid observation definition or "
672 677 "FITS file to proceed.";
673 678 throw GException::invalid_value(method, msg);
... ... @@ -679,6 +684,58 @@ void ctool::require_inobs(const std::string& method)
679 684  
680 685  
681 686 /***********************************************************************//**
  687 + * @brief Throws exception if inobs parameter is a counts cube
  688 + *
  689 + * @param[in] method Method name.
  690 + *
  691 + * Throw an exception if the inobs parameter is a counts cube.
  692 + ***************************************************************************/
  693 +void ctool::require_inobs_nocube(const std::string& method)
  694 +{
  695 + // Get inobs filename
  696 + std::string filename = (*this)["inobs"].filename();
  697 +
  698 + // Continue only if we have a FITS file
  699 + if (gammalib::is_fits(filename)) {
  700 +
  701 + // Signal su
  702 + bool is_cube = false;
  703 +
  704 + // Try loading file as counts cube. If this is successful then
  705 + // throw an exception
  706 + try {
  707 +
  708 + // Load cube from file
  709 + GCTAEventCube cube(filename);
  710 +
  711 + // If we're still alive then signal that we have a cube
  712 + is_cube = true;
  713 +
  714 + }
  715 +
  716 + // Catch any exceptions
  717 + catch (...) {
  718 + ;
  719 + }
  720 +
  721 + // If we have a counts cube then throw an exception
  722 + if (is_cube) {
  723 + std::string msg = "A counts cube has been specified for the "
  724 + "\"inobs\" parameter, yet no counts cube "
  725 + "can be specified as input observation."
  726 + " Instead, specify an event list or an "
  727 + "observation definition file.";
  728 + throw GException::invalid_value(method, msg);
  729 + }
  730 +
  731 + } // endif: we had a FITS file
  732 +
  733 + // Return
  734 + return;
  735 +}
  736 +
  737 +
  738 +/***********************************************************************//**
682 739 * @brief Set response for all CTA observations in container
683 740 *
684 741 * @param[in,out] obs Observation container
... ...
src/support/ctool.hpp
... ... @@ -81,6 +81,7 @@ protected:
81 81  
82 82 // Protected methods that check user parameters
83 83 void require_inobs(const std::string& method);
  84 + void require_inobs_nocube(const std::string& method);
84 85  
85 86 // Protected support methods
86 87 void set_response(GObservations& obs);
... ...