Commit a3fa428cb2f8b411bbfb809b0db5fa4f3800f7c2

Authored by Jürgen Knödlseder
1 parent f9a30a18

Add ctool::get_roi() method (#1828)

The ctool::get_roi() replaces some duplicated code in ctselect and ctcubemask. The method extracts the RoI from the user parameters "ra", "dec" and "rad". In case the "usepnt" parameter is present and set to "yes", the "ra" and "dec" parameters will not be read, and it's up to the client to set the RoI centre properly.
pyext/ctool.i
... ... @@ -97,6 +97,7 @@ public:
97 97 %rename(_require_inobs_nocube) require_inobs_nocube;
98 98 %rename(_log_observations) log_observations;
99 99 %rename(_log_models) log_models;
  100 + %rename(_get_roi) get_roi;
100 101 %rename(_set_response) set_response;
101 102 %rename(_set_edisp) set_edisp;
102 103 %rename(_restore_edisp) restore_edisp;
... ... @@ -140,6 +141,9 @@ public:
140 141 const GModels& models,
141 142 const std::string& what = "Model");
142 143  
  144 + // Protected methods that extract user parameters
  145 + GCTARoi get_roi(void);
  146 +
143 147 // Protected support methods
144 148 void set_response(GObservations& obs);
145 149 std::vector<bool> set_edisp(GObservations& obs, const bool& edisp) const;
... ...
src/ctcubemask/ctcubemask.cpp
... ... @@ -377,9 +377,7 @@ void ctcubemask::init_members(void)
377 377 m_outcube.clear();
378 378 m_prefix.clear();
379 379 m_usepnt = false;
380   - m_ra = 0.0;
381   - m_dec = 0.0;
382   - m_rad = 0.0;
  380 + m_roi.clear();
383 381 m_emin = 0.0;
384 382 m_emax = 0.0;
385 383 m_publish = false;
... ... @@ -407,9 +405,7 @@ void ctcubemask::copy_members(const ctcubemask&amp; app)
407 405 m_outcube = app.m_outcube;
408 406 m_prefix = app.m_prefix;
409 407 m_usepnt = app.m_usepnt;
410   - m_ra = app.m_ra;
411   - m_dec = app.m_dec;
412   - m_rad = app.m_rad;
  408 + m_roi = app.m_roi;
413 409 m_emin = app.m_emin;
414 410 m_emax = app.m_emax;
415 411 m_publish = app.m_publish;
... ... @@ -458,26 +454,10 @@ void ctcubemask::get_parameters(void)
458 454 // Get parameters
459 455 m_regfile = (*this)["regfile"].filename();
460 456 m_usepnt = (*this)["usepnt"].boolean();
461   - if (!m_usepnt) {
462 457  
463   - // Check RA/DEC parameters for validity to read
464   - if ((*this)["ra"].is_valid() && (*this)["dec"].is_valid()) {
465   - m_ra = (*this)["ra"].real();
466   - m_dec = (*this)["dec"].real();
467   - m_select_roi = true;
468   - }
469   - else {
470   - m_select_roi = false;
471   - }
472   - }
473   -
474   - // Check if radius is valid for a RoI selection
475   - if (m_select_roi && (*this)["rad"].is_valid()) {
476   - m_rad = (*this)["rad"].real();
477   - }
478   - else {
479   - m_select_roi = false;
480   - }
  458 + // Get the RoI and enable RoI selection if the RoI is valid
  459 + m_roi = get_roi();
  460 + m_select_roi = m_roi.is_valid();
481 461  
482 462 // Check for sanity of energy selection parameters
483 463 if ((*this)["emin"].is_valid() && (*this)["emax"].is_valid()) {
... ... @@ -587,7 +567,7 @@ void ctcubemask::apply_mask(GCTAObservation* obs)
587 567 // Set all pixels inside selected energy bands but outside RoI
588 568 // to -1.0 if requested
589 569 if (m_select_roi) {
590   - GSkyRegionCircle roi(m_ra, m_dec, m_rad);
  570 + GSkyRegionCircle roi(m_roi.centre().dir(), m_roi.radius());
591 571 for (int i = e_idx1; i <= e_idx2; ++i) {
592 572 for (int pixel = 0; pixel < npix; ++pixel) {
593 573 GSkyDir dir = map.inx2dir(pixel);
... ... @@ -599,9 +579,9 @@ void ctcubemask::apply_mask(GCTAObservation* obs)
599 579  
600 580 // Log selected RoI
601 581 log_value(NORMAL, "Selected RoI",
602   - "RA="+gammalib::str(m_ra)+" deg, "+
603   - "DEC="+gammalib::str(m_dec)+" deg, "+
604   - "Radius="+gammalib::str(m_rad)+" deg");
  582 + "RA="+gammalib::str(roi.centre().ra_deg())+" deg, "+
  583 + "DEC="+gammalib::str(roi.centre().dec_deg())+" deg, "+
  584 + "Radius="+gammalib::str(roi.radius())+" deg");
605 585  
606 586 } // endif: applied RoI selection
607 587  
... ...
src/ctcubemask/ctcubemask.hpp
... ... @@ -81,9 +81,7 @@ protected:
81 81 GFilename m_outcube; //!< Output event list or XML file
82 82 std::string m_prefix; //!< Prefix for multiple counts maps
83 83 bool m_usepnt; //!< Use pointing instead of RA/DEC parameters
84   - double m_ra; //!< RA of ROI centre
85   - double m_dec; //!< DEC of ROI centre
86   - double m_rad; //!< ROI radius
  84 + GCTARoi m_roi; //!< RoI selection
87 85 double m_emin; //!< Lower energy
88 86 double m_emax; //!< Upper energy
89 87 bool m_publish; //!< Publish counts cube?
... ...
src/ctselect/ctselect.cpp
... ... @@ -408,9 +408,7 @@ void ctselect::init_members(void)
408 408 m_outobs.clear();
409 409 m_prefix.clear();
410 410 m_usepnt = false;
411   - m_ra = -1.0;
412   - m_dec = -1.0;
413   - m_rad = -1.0;
  411 + m_roi.clear();
414 412 m_tmin = 0.0;
415 413 m_tmax = 0.0;
416 414 m_emin = 0.0;
... ... @@ -446,9 +444,7 @@ void ctselect::copy_members(const ctselect&amp; app)
446 444 m_outobs = app.m_outobs;
447 445 m_prefix = app.m_prefix;
448 446 m_usepnt = app.m_usepnt;
449   - m_ra = app.m_ra;
450   - m_dec = app.m_dec;
451   - m_rad = app.m_rad;
  447 + m_roi = app.m_roi;
452 448 m_tmin = app.m_tmin;
453 449 m_tmax = app.m_tmax;
454 450 m_emin = app.m_emin;
... ... @@ -504,28 +500,12 @@ void ctselect::get_parameters(void)
504 500 // information and do not accept counts cubes.
505 501 setup_observations(m_obs, false, true, false);
506 502  
507   - // Get parameters
  503 + // Get some parameters
508 504 m_usepnt = (*this)["usepnt"].boolean();
509   - if (!m_usepnt) {
510 505  
511   - // Check RA/DEC parameters for validity to read
512   - if ((*this)["ra"].is_valid() && (*this)["dec"].is_valid()) {
513   - m_ra = (*this)["ra"].real();
514   - m_dec = (*this)["dec"].real();
515   - m_select_roi = true;
516   - }
517   - else {
518   - m_select_roi = false;
519   - }
520   - }
521   -
522   - // Check if radius is vaild for a RoI selection
523   - if (m_select_roi && (*this)["rad"].is_valid()) {
524   - m_rad = (*this)["rad"].real();
525   - }
526   - else {
527   - m_select_roi = false;
528   - }
  506 + // Get the RoI and enable RoI selection if the RoI is valid
  507 + m_roi = get_roi();
  508 + m_select_roi = m_roi.is_valid();
529 509  
530 510 // Check for sanity of time selection parameters
531 511 if ((*this)["tmin"].is_valid() && (*this)["tmax"].is_valid()) {
... ... @@ -640,11 +620,11 @@ void ctselect::select_events(GCTAObservation* obs,
640 620 // Analyse expression to see if a selection is required
641 621 bool select_expr = (gammalib::strip_whitespace(m_expr).length() > 0);
642 622  
643   - // Set RA/DEC selection. If the "usepnt" parameter is set to true then
644   - // use the pointing direction as the ROI centre.
645   - double ra = m_ra;
646   - double dec = m_dec;
647   - double rad = m_rad;
  623 + // Set RoI selection. If the "usepnt" parameter is set to true then
  624 + // use the pointing direction as the RoI centre.
  625 + double ra = m_roi.centre().dir().ra_deg();
  626 + double dec = m_roi.centre().dir().dec_deg();
  627 + double rad = m_roi.radius();
648 628 if (m_usepnt) {
649 629 const GCTAPointing &pnt = obs->pointing();
650 630 ra = pnt.dir().ra_deg();
... ... @@ -751,7 +731,7 @@ void ctselect::select_events(GCTAObservation* obs,
751 731 // Log the requested RoI
752 732 log_value(NORMAL, "Requested RoI",
753 733 "Centre(RA,DEC)=("+gammalib::str(ra)+", "+
754   - gammalib::str(dec)+") deg, Radius="+gammalib::str(m_rad)+
  734 + gammalib::str(dec)+") deg, Radius="+gammalib::str(rad)+
755 735 " deg");
756 736  
757 737 // If we have already an RoI then make sure that the selected
... ...
src/ctselect/ctselect.hpp
... ... @@ -92,9 +92,7 @@ protected:
92 92 std::string m_outobs; //!< Output event list or XML file
93 93 std::string m_prefix; //!< Prefix for multiple event lists
94 94 bool m_usepnt; //!< Use pointing instead of RA/DEC parameters
95   - double m_ra; //!< RA of ROI centre
96   - double m_dec; //!< DEC of ROI centre
97   - double m_rad; //!< ROI radius
  95 + GCTARoi m_roi; //!< RoI selection
98 96 double m_tmin; //!< Start time
99 97 double m_tmax; //!< Stop time
100 98 double m_emin; //!< Lower energy
... ... @@ -111,7 +109,7 @@ protected:
111 109 GTime m_timemin; //!< Earliest time
112 110 GTime m_timemax; //!< Latest time
113 111 bool m_select_energy; //!< Perform energy selection
114   - bool m_select_roi; //!< Perform ROI selection
  112 + bool m_select_roi; //!< Perform RoI selection
115 113 bool m_select_time; //!< Perform time selection
116 114 };
117 115  
... ...
src/support/ctool.cpp
... ... @@ -997,6 +997,60 @@ void ctool::log_models(const GChatter&amp; chatter,
997 997  
998 998  
999 999 /***********************************************************************//**
  1000 + * @brief Return RoI from user parameters
  1001 + *
  1002 + * @param[in] obs Observation container.
  1003 + * @return Region of interest.
  1004 + *
  1005 + * Returns region of interest from the user parameters "ra", "dec" and "rad".
  1006 + * If the "usepnt" parameter exists and is "yes" then postpone then assume
  1007 + * that the Right Ascension and Declination of the RoI centre will be set
  1008 + * from the pointing direction somewhere else.
  1009 + ***************************************************************************/
  1010 +GCTARoi ctool::get_roi(void)
  1011 +{
  1012 + // Initialise an empty RoI
  1013 + GCTARoi roi;
  1014 +
  1015 + // Signal whether the pointing instead of the user parameters should be
  1016 + // use for the RoI centre
  1017 + bool usepnt = (has_par("usepnt") && (*this)["usepnt"].boolean());
  1018 +
  1019 + // If pointing should not bet used the extract the RoI centre from the
  1020 + // "ra" and "dec" parameters
  1021 + if (!usepnt) {
  1022 +
  1023 + // Read "ra" and "dec" parameters only if both are valid
  1024 + if ((*this)["ra"].is_valid() && (*this)["dec"].is_valid()) {
  1025 +
  1026 + // Read parameters
  1027 + double ra = (*this)["ra"].real();
  1028 + double dec = (*this)["dec"].real();
  1029 +
  1030 + // Set RoI
  1031 + GCTAInstDir instdir;
  1032 + instdir.dir().radec_deg(ra, dec);
  1033 + roi.centre(instdir);
  1034 +
  1035 + // Signal that we have a pointing direction
  1036 + usepnt = true;
  1037 +
  1038 + } // endif: "ra" and "dec" were valid
  1039 +
  1040 + } // endif: pointing was not used for RoI centre
  1041 +
  1042 + // If we have a RoI centre and if the "rad" parameter is valid then
  1043 + // extract the RoI radius from the parameter
  1044 + if (usepnt && (*this)["rad"].is_valid()) {
  1045 + roi.radius((*this)["rad"].real());
  1046 + }
  1047 +
  1048 + // Return RoI
  1049 + return roi;
  1050 +}
  1051 +
  1052 +
  1053 +/***********************************************************************//**
1000 1054 * @brief Set response for all CTA observations in container
1001 1055 *
1002 1056 * @param[in,out] obs Observation container
... ...
src/support/ctool.hpp
... ... @@ -103,6 +103,9 @@ protected:
103 103 const GModels& models,
104 104 const std::string& what = "Model");
105 105  
  106 + // Protected methods that extract user parameters
  107 + GCTARoi get_roi(void);
  108 +
106 109 // Protected support methods
107 110 void set_response(GObservations& obs);
108 111 std::vector<bool> set_edisp(GObservations& obs, const bool& edisp) const;
... ...