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