GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GCTARoi.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GCTARoi.cpp - CTA region of interest class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2021 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GCTARoi.cpp
23  * @brief CTA region of interest class interface implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GTools.hpp"
32 #include "GEvent.hpp"
33 #include "GXmlElement.hpp"
34 #include "GCTARoi.hpp"
35 
36 /* __ Method name definitions ____________________________________________ */
37 #define G_READ "GCTARoi::read(GXmlElement&)"
38 #define G_WRITE "GCTARoi::write(GXmlElement&)"
39 #define G_RADIUS "GCTARoi::radius(double&)"
40 
41 /* __ Macros _____________________________________________________________ */
42 
43 /* __ Coding definitions _________________________________________________ */
44 
45 /* __ Debug definitions __________________________________________________ */
46 
47 
48 
49 /*==========================================================================
50  = =
51  = Constructors/destructors =
52  = =
53  ==========================================================================*/
54 
55 /***********************************************************************//**
56  * @brief Void constructor
57  ***************************************************************************/
59 {
60  // Initialise class members
61  init_members();
62 
63  // Return
64  return;
65 }
66 
67 
68 /***********************************************************************//**
69  * @brief XML element constructor
70  *
71  * @param[in] xml XML element.
72  *
73  * Constructs region of interest from an XML element.
74  ***************************************************************************/
76 {
77  // Initialise members
78  init_members();
79 
80  // Read region of interest from XML element
81  read(xml);
82 
83  // Return
84  return;
85 }
86 
87 
88 /***********************************************************************//**
89  * @brief Region of interest constructor
90  *
91  * @param[in] centre Region of interest centre.
92  * @param[in] radius Radius of region of interest (degrees).
93  ***************************************************************************/
94 GCTARoi::GCTARoi(const GCTAInstDir& centre, const double& radius) : GRoi()
95 {
96  // Initialise class members
97  init_members();
98 
99  // Set members
100  this->centre(centre);
101  this->radius(radius);
102 
103  // Return
104  return;
105 }
106 
107 
108 /***********************************************************************//**
109  * @brief Copy constructor
110  *
111  * @param[in] roi Region of interest.
112  ***************************************************************************/
113 GCTARoi::GCTARoi(const GCTARoi& roi) : GRoi(roi)
114 {
115  // Initialise class members
116  init_members();
117 
118  // Copy members
119  copy_members(roi);
120 
121  // Return
122  return;
123 }
124 
125 
126 /***********************************************************************//**
127  * @brief Destructor
128  ***************************************************************************/
130 {
131  // Free members
132  free_members();
133 
134  // Return
135  return;
136 }
137 
138 
139 /*==========================================================================
140  = =
141  = Operators =
142  = =
143  ==========================================================================*/
144 
145 /***********************************************************************//**
146  * @brief Assignment operator
147  *
148  * @param[in] roi Region of interest.
149  * @return Region of interest.
150  ***************************************************************************/
152 {
153  // Execute only if object is not identical
154  if (this != &roi) {
155 
156  // Copy base class members
157  this->GRoi::operator=(roi);
158 
159  // Free members
160  free_members();
161 
162  // Initialise private members
163  init_members();
164 
165  // Copy members
166  copy_members(roi);
167 
168  } // endif: object was not identical
169 
170  // Return this object
171  return *this;
172 }
173 
174 
175 /*==========================================================================
176  = =
177  = Public methods =
178  = =
179  ==========================================================================*/
180 
181 /***********************************************************************//**
182  * @brief Clear instance
183  ***************************************************************************/
184 void GCTARoi::clear(void)
185 {
186  // Free members
187  free_members();
188  this->GRoi::free_members();
189 
190  // Initialise private members
191  this->GRoi::init_members();
192  init_members();
193 
194  // Return
195  return;
196 }
197 
198 
199 /***********************************************************************//**
200  * @brief Clone instance
201  *
202  * @return Pointer to deep copy of region of interest
203  ***************************************************************************/
205 {
206  return new GCTARoi(*this);
207 }
208 
209 
210 /***********************************************************************//**
211  * @brief Check if region of interest contains an event
212  *
213  * @return True if region of interest contains event, false otherwise
214  ***************************************************************************/
215 bool GCTARoi::contains(const GEvent& event) const
216 {
217  // Initialise flag to non-containment
218  bool contains = false;
219 
220  // Get pointer to CTA instrument direction
221  const GCTAInstDir* dir = dynamic_cast<const GCTAInstDir*>(&event.dir());
222 
223  // If instrument direction is a CTA instrument direction then check on
224  // containment
225  if (dir != NULL) {
226  contains = this->contains(*dir);
227  }
228 
229  // Return containment flag
230  return contains;
231 }
232 
233 
234 /***********************************************************************//**
235  * @brief Check if region of interest contains an instrument direction
236  *
237  * @return True if region of interest contains instrument direction, false
238  * otherwise
239  ***************************************************************************/
240 bool GCTARoi::contains(const GCTAInstDir& dir) const
241 {
242  // Set containment flag
243  bool contains = (m_centre.dir().dist_deg(dir.dir()) <= m_radius);
244 
245  // Return containment flag
246  return contains;
247 }
248 
249 
250 /***********************************************************************//**
251  * @brief Read region of interest from XML element
252  *
253  * @param[in] xml XML element.
254  *
255  * @exception GException::invalid_value
256  * Invalid XML format encountered.
257  *
258  * Read region of interest from an XML element. The format of the region
259  * of interest is
260  *
261  * <parameter name="RegionOfInterest" ra="..." dec="..." rad="..."/>
262  *
263  * The units are deg.
264  ***************************************************************************/
265 void GCTARoi::read(const GXmlElement& xml)
266 {
267  // Clear region of interest
268  clear();
269 
270  // Get region of interest parameter
271  const GXmlElement* par = gammalib::xml_get_par(G_READ, xml, "RegionOfInterest");
272 
273  // Extract position attributes
274  if (par->has_attribute("ra") &&
275  par->has_attribute("dec") &&
276  par->has_attribute("rad")) {
277  double ra = gammalib::todouble(par->attribute("ra"));
278  double dec = gammalib::todouble(par->attribute("dec"));
279  double rad = gammalib::todouble(par->attribute("rad"));
280  GSkyDir centre;
281  centre.radec_deg(ra,dec);
282  this->centre(GCTAInstDir(centre));
283  this->radius(rad);
284  }
285  else {
286  std::string msg = "Attributes \"ra\", \"dec\" and/or \"rad\" not found"
287  " in XML parameter \"RegionOfInterest\"."
288  " Please verify the XML format.";
289  throw GException::invalid_value(G_READ, msg);
290  }
291 
292  // Return
293  return;
294 }
295 
296 
297 /***********************************************************************//**
298  * @brief Write region of interest into XML element
299  *
300  * @param[in] xml XML element.
301  *
302  * Writes region of interest into an XML element. The format of the region
303  * of interest is
304  *
305  * <parameter name="RegionOfInterest" ra="..." dec="..." rad="..."/>
306  *
307  * The units are deg.
308  *
309  * This method does nothing if the region of interest is not valid (signaled
310  * by a radius of 0 deg).
311  ***************************************************************************/
312 void GCTARoi::write(GXmlElement& xml) const
313 {
314  // Continue only if the ROI is valid
315  if (m_radius > 0.0) {
316 
317  // Get parameter
318  GXmlElement* par = gammalib::xml_need_par(G_WRITE, xml, "RegionOfInterest");
319 
320  // Write attributes
321  par->attribute("ra", gammalib::str(m_centre.dir().ra_deg()));
322  par->attribute("dec", gammalib::str(m_centre.dir().dec_deg()));
323  par->attribute("rad", gammalib::str(m_radius));
324 
325  } // endif: ROI was valid
326 
327  // Return
328  return;
329 }
330 
331 
332 /***********************************************************************//**
333  * @brief Print ROI information
334  *
335  * @param[in] chatter Chattiness.
336  * @return String containing ROI information.
337  ***************************************************************************/
338 std::string GCTARoi::print(const GChatter& chatter) const
339 {
340  // Initialise result string
341  std::string result;
342 
343  // Continue only if chatter is not silent
344  if (chatter != SILENT) {
345 
346  // Append header
347  result.append("=== GCTARoi ===");
348 
349  // Append information
350  result.append("\n"+gammalib::parformat("RoI centre"));
351  result.append(m_centre.print());
352  result.append("\n"+gammalib::parformat("RoI radius"));
353  result.append(gammalib::str(m_radius)+" deg");
354 
355  } // endif: chatter was not silent
356 
357  // Return result
358  return result;
359 }
360 
361 /***********************************************************************//**
362  * @brief Set radius of region of interest
363  *
364  * @param[in] radius Region of interest radius (degrees).
365  *
366  * @exception GException::invalid_argument
367  * Non-positive ROI radius encountered
368  *
369  * Set the radius of the region of interest.
370  ***************************************************************************/
371 void GCTARoi::radius(const double& radius)
372 {
373  // Throw an exception if argument is not valid
374  if (radius <= 0.0) {
375  std::string msg = "Invalid RoI radius "+gammalib::str(radius)+
376  " encountered. Please specify a strictly"
377  " positive radius.";
379  }
380 
381  // Set radius value
382  m_radius = radius;
383 
384  // Return
385  return;
386 }
387 
388 /*==========================================================================
389  = =
390  = Private methods =
391  = =
392  ==========================================================================*/
393 
394 /***********************************************************************//**
395  * @brief Initialise class members
396  ***************************************************************************/
398 {
399  // Initialise members
400  m_centre.clear();
401  m_radius = 0.0;
402 
403  // Return
404  return;
405 }
406 
407 
408 /***********************************************************************//**
409  * @brief Copy class members
410  *
411  * @param[in] roi Region of interest from which members should be copied.
412  ***************************************************************************/
414 {
415  // Copy attributes
416  m_centre = roi.m_centre;
417  m_radius = roi.m_radius;
418 
419  // Return
420  return;
421 }
422 
423 
424 /***********************************************************************//**
425  * @brief Delete class members
426  ***************************************************************************/
428 {
429  // Return
430  return;
431 }
virtual GRoi & operator=(const GRoi &roi)
Assignment operator.
Definition: GRoi.cpp:104
virtual std::string print(const GChatter &chatter=NORMAL) const
Print ROI information.
Definition: GCTARoi.cpp:338
virtual ~GCTARoi(void)
Destructor.
Definition: GCTARoi.cpp:129
XML element node class interface definition.
void free_members(void)
Delete class members.
Definition: GCTARoi.cpp:427
Abstract interface for the event classes.
Definition: GEvent.hpp:71
const double & radius(void) const
Returns radius of region of interest in degrees.
Definition: GCTARoi.hpp:125
XML element node class.
Definition: GXmlElement.hpp:48
Interface for the CTA region of interest class.
Definition: GCTARoi.hpp:49
Gammalib tools definition.
void write(GXmlElement &xml) const
Write region of interest into XML element.
Definition: GCTARoi.cpp:312
void dir(const GSkyDir &dir)
Set sky direction.
std::string centre(const std::string &s, const int &n, const char &c= ' ')
Centre string to achieve a length of n characters.
Definition: GTools.cpp:1118
const GXmlAttribute * attribute(const int &index) const
Return attribute.
void copy_members(const GCTARoi &roi)
Copy class members.
Definition: GCTARoi.cpp:413
GCTARoi(void)
Void constructor.
Definition: GCTARoi.cpp:58
const GCTAInstDir & centre(void) const
Returns region of interest centre.
Definition: GCTARoi.hpp:111
void init_members(void)
Initialise class members.
Definition: GRoi.cpp:141
bool has_attribute(const std::string &name) const
Check if element has a given attribute.
virtual GCTARoi * clone(void) const
Clone instance.
Definition: GCTARoi.cpp:204
GXmlElement * xml_need_par(const std::string &origin, GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition: GTools.cpp:1637
Abstract event base class definition.
virtual bool contains(const GEvent &event) const
Check if region of interest contains an event.
Definition: GCTARoi.cpp:215
virtual void clear(void)
Clear instance.
Definition: GCTARoi.cpp:184
GChatter
Definition: GTypemaps.hpp:33
virtual std::string print(const GChatter &chatter=NORMAL) const
Print instrument direction information.
CTA region of interest class interface definition.
void free_members(void)
Delete class members.
Definition: GRoi.cpp:163
#define G_RADIUS
Definition: GCTARoi.cpp:39
#define G_READ
Definition: GCTARoi.cpp:37
virtual void clear(void)
Clear CTA instrument direction.
double m_radius
Radius of ROI in degrees.
Definition: GCTARoi.hpp:87
CTA instrument direction class.
Definition: GCTAInstDir.hpp:63
GCTARoi & operator=(const GCTARoi &roi)
Assignment operator.
Definition: GCTARoi.cpp:151
Interface for the region of interest classes.
Definition: GRoi.hpp:48
void read(const GXmlElement &xml)
Read region of interest from XML element.
Definition: GCTARoi.cpp:265
void init_members(void)
Initialise class members.
Definition: GCTARoi.cpp:397
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1143
GCTAInstDir m_centre
Centre of ROI in instrument coordinates.
Definition: GCTARoi.hpp:86
#define G_WRITE
Definition: GCTARoi.cpp:38
Sky direction class.
Definition: GSkyDir.hpp:62
const GXmlElement * xml_get_par(const std::string &origin, const GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition: GTools.cpp:1689
double todouble(const std::string &arg)
Convert string into double precision value.
Definition: GTools.cpp:926
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:489