GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GModelSpatialPointSource.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GModelSpatialPointSource.cpp - Spatial point source model class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2009-2022 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 GModelSpatialPointSource.cpp
23  * @brief Point source spatial model class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GException.hpp"
32 #include "GTools.hpp"
33 #include "GMath.hpp"
36 
37 /* __ Constants __________________________________________________________ */
38 const double tolerance = 0.000027777778; // angular tolerance is 1 arcsec
39 
40 
41 /* __ Globals ____________________________________________________________ */
43 const GModelSpatialRegistry g_spatial_ptsrc_registry(&g_spatial_ptsrc_seed);
44 #if defined(G_LEGACY_XML_FORMAT)
45 const GModelSpatialPointSource g_spatial_ptsrc_legacy_seed(true, "SkyDirFunction");
46 const GModelSpatialRegistry g_spatial_ptsrc_legacy_registry(&g_spatial_ptsrc_legacy_seed);
47 #endif
48 
49 /* __ Method name definitions ____________________________________________ */
50 #define G_CONSTRUCTOR1 "GModelSpatialPointSource::GModelSpatialPointSource("\
51  "GSkyDir&, std::string&)"
52 #define G_CONSTRUCTOR2 "GModelSpatialPointSource::GModelSpatialPointSource("\
53  "double&, double&, std::string&)"
54 #define G_READ "GModelSpatialPointSource::read(GXmlElement&)"
55 #define G_WRITE "GModelSpatialPointSource::write(GXmlElement&)"
56 
57 /* __ Macros _____________________________________________________________ */
58 
59 /* __ Coding definitions _________________________________________________ */
60 
61 /* __ Debug definitions __________________________________________________ */
62 
63 
64 /*==========================================================================
65  = =
66  = Constructors/destructors =
67  = =
68  ==========================================================================*/
69 
70 /***********************************************************************//**
71  * @brief Void constructor
72  *
73  * Constructs empty point source model.
74  ***************************************************************************/
76 {
77  // Initialise members
78  init_members();
79 
80  // Return
81  return;
82 }
83 
84 
85 /***********************************************************************//**
86  * @brief Model type constructor
87  *
88  * @param[in] dummy Dummy flag.
89  * @param[in] type Model type.
90  *
91  * Constructs empty point source model by specifying a model @p type.
92  ***************************************************************************/
94  const std::string& type) :
96 {
97  // Initialise members
98  init_members();
99 
100  // Set model type
101  m_type = type;
102 
103  // Return
104  return;
105 }
106 
107 
108 /***********************************************************************//**
109  * @brief Sky direction constructor
110  *
111  * @param[in] dir Sky direction.
112  * @param[in] coordsys Coordinate system (either "CEL" or "GAL")
113  *
114  * @exception GException::invalid_argument
115  * Invalid @p coordsys argument specified.
116  *
117  * Construct a point source spatial model from a sky direction. The
118  * @p coordsys parameter specifies whether the sky direction should be
119  * interpreted in the celestial or Galactic coordinate system.
120  ***************************************************************************/
122  const std::string& coordsys) :
123  GModelSpatial()
124 {
125  // Throw an exception if the coordinate system is invalid
126  if ((coordsys != "CEL") && (coordsys != "GAL")) {
127  std::string msg = "Invalid coordinate system \""+coordsys+"\" "
128  "specified. Please specify either \"CEL\" or "
129  "\"GAL\".";
131  }
132 
133  // Initialise members
134  init_members();
135 
136  // Set parameter names
137  if (coordsys == "CEL") {
138  m_lon.name("RA");
139  m_lat.name("DEC");
140  }
141  else {
142  m_lon.name("GLON");
143  m_lat.name("GLAT");
144  }
145 
146  // Assign direction
147  this->dir(dir);
148 
149  // Return
150  return;
151 }
152 
153 
154 /***********************************************************************//**
155  * @brief Value constructor
156  *
157  * @param[in] lon Longitude of point source (deg).
158  * @param[in] lat Latitude of point source (deg).
159  * @param[in] coordsys Coordinate system (either "CEL" or "GAL")
160  *
161  * @exception GException::invalid_argument
162  * Invalid @p coordsys argument specified.
163  *
164  * Construct a point source spatial model from the longitude and latitude
165  * of a point source. The @p coordsys parameter specifies the coordinate
166  * system in which @p lon and @p lat are provided. By default a celestial
167  * coordinate system is assumed, which means that @p lon and @p lat are
168  * taken as Right Ascension and Declination. If "GAL" is specified, @p lon
169  * and @p lat are taken as Galactic longitude and latitude.
170  ***************************************************************************/
172  const double& lat,
173  const std::string& coordsys) :
174  GModelSpatial()
175 {
176  // Throw an exception if the coordinate system is invalid
177  if ((coordsys != "CEL") && (coordsys != "GAL")) {
178  std::string msg = "Invalid coordinate system \""+coordsys+"\" "
179  "specified. Please specify either \"CEL\" or "
180  "\"GAL\".";
182  }
183 
184  // Initialise members
185  init_members();
186 
187  // Set parameter names
188  if (coordsys == "CEL") {
189  m_lon.name("RA");
190  m_lat.name("DEC");
191  }
192  else {
193  m_lon.name("GLON");
194  m_lat.name("GLAT");
195  }
196 
197  // Set values
198  m_lon.value(lon);
199  m_lat.value(lat);
200 
201  // Return
202  return;
203 }
204 
205 
206 /***********************************************************************//**
207  * @brief XML constructor
208  *
209  * @param[in] xml XML element.
210  *
211  * Construct a point source spatial model by extracting information from an
212  * XML element. See the read() method for more information about the expected
213  * structure of the XML element.
214  ***************************************************************************/
216  GModelSpatial()
217 {
218  // Initialise members
219  init_members();
220 
221  // Read information from XML element
222  read(xml);
223 
224  // Return
225  return;
226 }
227 
228 
229 /***********************************************************************//**
230  * @brief Copy constructor
231  *
232  * @param[in] model Point source spatial model.
233  ***************************************************************************/
235  GModelSpatial(model)
236 {
237  // Initialise members
238  init_members();
239 
240  // Copy members
241  copy_members(model);
242 
243  // Return
244  return;
245 }
246 
247 
248 /***********************************************************************//**
249  * @brief Destructor
250  ***************************************************************************/
252 {
253  // Free members
254  free_members();
255 
256  // Return
257  return;
258 }
259 
260 
261 /*==========================================================================
262  = =
263  = Operators =
264  = =
265  ==========================================================================*/
266 
267 /***********************************************************************//**
268  * @brief Assignment operator
269  *
270  * @param[in] model Point source spatial model.
271  * @return Point source spatial model.
272  ***************************************************************************/
274 {
275  // Execute only if object is not identical
276  if (this != &model) {
277 
278  // Copy base class members
279  this->GModelSpatial::operator=(model);
280 
281  // Free members
282  free_members();
283 
284  // Initialise members
285  init_members();
286 
287  // Copy members
288  copy_members(model);
289 
290  } // endif: object was not identical
291 
292  // Return
293  return *this;
294 }
295 
296 
297 /*==========================================================================
298  = =
299  = Public methods =
300  = =
301  ==========================================================================*/
302 
303 /***********************************************************************//**
304  * @brief Clear point source model
305  ***************************************************************************/
307 {
308  // Free class members (base and derived classes, derived class first)
309  free_members();
311 
312  // Initialise members
314  init_members();
315 
316  // Return
317  return;
318 }
319 
320 
321 /***********************************************************************//**
322  * @brief Clone point source model
323  *
324  * @return Pointer to deep copy of point source model.
325  ***************************************************************************/
327 {
328  // Clone point source model
329  return new GModelSpatialPointSource(*this);
330 }
331 
332 
333 /***********************************************************************//**
334  * @brief Evaluate function
335  *
336  * @param[in] photon Incident photon.
337  * @param[in] gradients Compute gradients?
338  * @return Model value.
339  *
340  * Evaluates the spatial part for a point source model. It implements a delta
341  * function with respect to the coordinates of the source. For numerical
342  * reasons, a certain tolerance is accepted (typically 0.1 arcsec, i.e. well
343  * below the angular resolution of gamma-ray telescopes).
344  *
345  * The method will not compute analytical parameter gradients, even if the
346  * @p gradients argument is set to true. Point source parameter gradients
347  * need to be computed numerically.
348  ***************************************************************************/
350  const bool& gradients) const
351 {
352  // Set value dependent on source distance
353  double value = (photon.dir().dist_deg(dir()) < tolerance) ? 1.0 : 0.0;
354 
355  // Return value
356  return value;
357 }
358 
359 
360 /***********************************************************************//**
361  * @brief Returns MC sky direction
362  *
363  * @param[in] energy Photon energy.
364  * @param[in] time Photon arrival time.
365  * @param[in,out] ran Random number generator.
366  * @return Sky direction.
367  *
368  * Draws an arbitrary sky direction for the point source model. As the point
369  * source is a point in the sky, the methods always returns the directon of
370  * the point source.
371  ***************************************************************************/
373  const GTime& time,
374  GRan& ran) const
375 {
376  // Return sky direction
377  return (dir());
378 }
379 
380 
381 /***********************************************************************//**
382  * @brief Checks where model contains specified sky direction
383  *
384  * @param[in] dir Sky direction.
385  * @param[in] margin Margin to be added to sky direction (degrees)
386  * @return True if the model contains the sky direction.
387  *
388  * Signals whether a sky direction is contained in the point source
389  * model.
390  ***************************************************************************/
392  const double& margin) const
393 {
394  // Compute distance to centre (radian)
395  double distance = dir.dist(this->dir());
396 
397  // Return flag
398  return (distance <= margin*gammalib::deg2rad);
399 }
400 
401 
402 /***********************************************************************//**
403  * @brief Read model from XML element
404  *
405  * @param[in] xml XML element containing point source model information.
406  *
407  * Read the point source information from an XML element with the following
408  * format
409  *
410  * <spatialModel type="PointSource">
411  * <parameter name="RA" scale="1" value="83.6331" min="-360" max="360" free="0" />
412  * <parameter name="DEC" scale="1" value="22.0145" min="-90" max="90" free="0" />
413  * </spatialModel>
414  *
415  * or
416  *
417  * <spatialModel type="PointSource">
418  * <parameter name="GLON" scale="1" value="184.5575" min="-360" max="360" free="0" />
419  * <parameter name="GLAT" scale="1" value="-5.7843" min="-90" max="90" free="0" />
420  * </spatialModel>
421  *
422  ***************************************************************************/
424 {
425  // Verify number of model parameters
427 
428  // Read RA/DEC parameters
429  if (gammalib::xml_has_par(xml, "RA") && gammalib::xml_has_par(xml, "DEC")) {
430 
431  // Get parameters
432  const GXmlElement* ra = gammalib::xml_get_par(G_READ, xml, "RA");
433  const GXmlElement* dec = gammalib::xml_get_par(G_READ, xml, "DEC");
434 
435  // Read parameters
436  m_lon.read(*ra);
437  m_lat.read(*dec);
438 
439  }
440 
441  // ... otherwise read GLON/GLAT parameters
442  else {
443 
444  // Get parameters
445  const GXmlElement* glon = gammalib::xml_get_par(G_READ, xml, "GLON");
446  const GXmlElement* glat = gammalib::xml_get_par(G_READ, xml, "GLAT");
447 
448  // Read parameters
449  m_lon.read(*glon);
450  m_lat.read(*glat);
451 
452  }
453 
454  // Return
455  return;
456 }
457 
458 
459 /***********************************************************************//**
460  * @brief Write model into XML element
461  *
462  * @param[in] xml XML element into which model information is written.
463  *
464  * Depending on the coordinate system, write the point source information
465  * into an XML element with the following format
466  *
467  * <spatialModel type="PointSource">
468  * <parameter name="RA" scale="1" value="83.6331" min="-360" max="360" free="0" />
469  * <parameter name="DEC" scale="1" value="22.0145" min="-90" max="90" free="0" />
470  * </spatialModel>
471  *
472  * or
473  *
474  * <spatialModel type="PointSource">
475  * <parameter name="GLON" scale="1" value="184.5575" min="-360" max="360" free="0" />
476  * <parameter name="GLAT" scale="1" value="-5.7843" min="-90" max="90" free="0" />
477  * </spatialModel>
478  *
479  ***************************************************************************/
481 {
482  // Verify model type
484 
485  // Get or create parameters
488 
489  // Write parameters
490  m_lon.write(*lon);
491  m_lat.write(*lat);
492 
493  // Return
494  return;
495 }
496 
497 
498 /***********************************************************************//**
499  * @brief Returns model flux integrated in sky region
500  *
501  * @param[in] region Sky region.
502  * @param[in] srcEng Energy.
503  * @param[in] srcTime Time.
504  * @return Flux (adimensional or ph/cm2/s).
505  *
506  * Returns point source flux within a sky region. If the point source is
507  * contained within the sky region the flux will be 1, otherwise the flux
508  * will be 0.
509  ***************************************************************************/
511  const GEnergy& srcEng,
512  const GTime& srcTime) const
513 {
514  // Initialise flux
515  double flux = 0.0;
516 
517  // If point source region overlaps with sky region then set flux to 1
518  if (this->region()->overlaps(region)) {
519  flux = 1.0;
520  }
521 
522  // Return flux
523  return flux;
524 }
525 
526 
527 /***********************************************************************//**
528  * @brief Print point source information
529  *
530  * @param[in] chatter Chattiness.
531  * @return String containing point source information.
532  ***************************************************************************/
533 std::string GModelSpatialPointSource::print(const GChatter& chatter) const
534 {
535  // Initialise result string
536  std::string result;
537 
538  // Continue only if chatter is not silent
539  if (chatter != SILENT) {
540 
541  // Append header
542  result.append("=== GModelSpatialPointSource ===");
543 
544  // Append parameters
545  result.append("\n"+gammalib::parformat("Number of parameters"));
546  result.append(gammalib::str(size()));
547  for (int i = 0; i < size(); ++i) {
548  result.append("\n"+m_pars[i]->print(chatter));
549  }
550 
551  } // endif: chatter was not silent
552 
553  // Return result
554  return result;
555 }
556 
557 
558 /***********************************************************************//**
559  * @brief Return position of point source
560  *
561  * @return Point source sky direction.
562  *
563  * Returns the sky direction of the point source.
564  ***************************************************************************/
566 {
567  // Get longitude and latitude values
568  double lon = m_lon.value();
569  double lat = m_lat.value();
570 
571  // If longitude or latitude values have changed then update sky
572  // direction cache
573  if ((lon != m_last_lon) || (lat != m_last_lat)) {
574 
575  // Update last values
576  m_last_lon = lon;
577  m_last_lat = lat;
578 
579  // Update sky direction dependent on model coordinate system
580  if (is_celestial()) {
582  }
583  else {
585  }
586 
587  } // endif: update of sky direction cache required
588 
589  // Return sky direction
590  return (m_dir);
591 }
592 
593 
594 /***********************************************************************//**
595  * @brief Set position of point source
596  *
597  * @param[in] dir Sky direction of point source.
598  *
599  * Sets the sky direction of the point source.
600  ***************************************************************************/
602 {
603  // Assign sky direction depending on the model coordinate system
604  if (is_celestial()) {
605  m_lon.value(dir.ra_deg());
606  m_lat.value(dir.dec_deg());
607  }
608  else {
609  m_lon.value(dir.l_deg());
610  m_lat.value(dir.b_deg());
611  }
612 
613  // Return
614  return;
615 }
616 
617 
618 /*==========================================================================
619  = =
620  = Private methods =
621  = =
622  ==========================================================================*/
623 
624 /***********************************************************************//**
625  * @brief Initialise class members
626  ***************************************************************************/
628 {
629  // Initialise model type
630  m_type = "PointSource";
631 
632  // Initialise Right Ascension
633  m_lon.clear();
634  m_lon.name("RA");
635  m_lon.unit("deg");
636  m_lon.fix();
637  m_lon.scale(1.0);
638  m_lon.gradient(0.0);
639  m_lon.has_grad(false);
640 
641  // Initialise Declination
642  m_lat.clear();
643  m_lat.name("DEC");
644  m_lat.unit("deg");
645  m_lat.fix();
646  m_lat.scale(1.0);
647  m_lat.gradient(0.0);
648  m_lat.has_grad(false);
649 
650  // Set parameter pointer(s)
651  m_pars.clear();
652  m_pars.push_back(&m_lon);
653  m_pars.push_back(&m_lat);
654 
655  // Initialise cache
656  m_dir.clear();
657  m_last_lon = -9999.0;
658  m_last_lat = -9999.0;
659 
660  // Return
661  return;
662 }
663 
664 
665 /***********************************************************************//**
666  * @brief Copy class members
667  *
668  * @param[in] model Point source spatial model.
669  ***************************************************************************/
671 {
672  // Copy members
673  m_type = model.m_type; // Needed to conserve model type
674  m_lon = model.m_lon;
675  m_lat = model.m_lat;
676 
677  // Set parameter pointer(s)
678  m_pars.clear();
679  m_pars.push_back(&m_lon);
680  m_pars.push_back(&m_lat);
681 
682  // Copy cache
683  m_dir = model.m_dir;
684  m_last_lon = model.m_last_lon;
685  m_last_lat = model.m_last_lat;
686 
687  // Return
688  return;
689 }
690 
691 
692 /***********************************************************************//**
693  * @brief Delete class members
694  ***************************************************************************/
696 {
697  // Return
698  return;
699 }
700 
701 
702 /***********************************************************************//**
703  * @brief Set boundary sky region
704  ***************************************************************************/
706 {
707  // Set sky region circle
708  GSkyRegionCircle region(dir(), 0.0);
709 
710  // Set region (circumvent const correctness)
711  const_cast<GModelSpatialPointSource*>(this)->m_region = region;
712 
713  // Return
714  return;
715 }
void free_members(void)
Delete class members.
double dist_deg(const GSkyDir &dir) const
Compute angular distance between sky directions in degrees.
Definition: GSkyDir.hpp:286
#define G_CONSTRUCTOR1
double dec_deg(void) const
Returns Declination in degrees.
Definition: GSkyDir.hpp:256
Point source spatial model class interface definition.
const std::string & name(void) const
Return parameter name.
double gradient(void) const
Return parameter gradient.
const GSkyDir & dir(void) const
Return position of point source.
int size(void) const
Return number of parameters.
void write(GXmlElement &xml) const
Set or update parameter attributes in XML element.
Definition: GModelPar.cpp:351
XML element node class.
Definition: GXmlElement.hpp:48
virtual GModelSpatialPointSource * clone(void) const
Clone point source model.
virtual GModelSpatial & operator=(const GModelSpatial &model)
Assignment operator.
virtual double eval(const GPhoton &photon, const bool &gradients=false) const
Evaluate function.
Random number generator class.
Definition: GRan.hpp:44
GModelPar m_lon
Right Ascension or Galactic longitude (deg)
Time class.
Definition: GTime.hpp:55
Gammalib tools definition.
#define G_CONSTRUCTOR2
Interface for the circular sky region class.
virtual void clear(void)
Clear point source model.
Interface definition for the spatial model registry class.
GSkyRegionCircle m_region
Bounding circle.
void copy_members(const GModelSpatialPointSource &model)
Copy class members.
Class that handles photons.
Definition: GPhoton.hpp:47
const double & scale(void) const
Return parameter scale.
const double deg2rad
Definition: GMath.hpp:43
bool has_grad(void) const
Signal if parameter gradient is computed analytically.
Abstract interface for the sky region class.
Definition: GSkyRegion.hpp:57
virtual std::string print(const GChatter &chatter=NORMAL) const
Print point source information.
virtual ~GModelSpatialPointSource(void)
Destructor.
virtual void write(GXmlElement &xml) const
Write model into XML element.
virtual GSkyDir mc(const GEnergy &energy, const GTime &time, GRan &ran) const
Returns MC sky direction.
std::vector< GModelPar * > m_pars
Parameter pointers.
const double tolerance
virtual double flux(const GSkyRegion &region, const GEnergy &srcEng=GEnergy(), const GTime &srcTime=GTime()) const
Returns model flux integrated in sky region.
void fix(void)
Fix a parameter.
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
virtual void read(const GXmlElement &xml)
Read model from XML element.
void clear(void)
Clear parameter.
Spatial model registry class definition.
double ra_deg(void) const
Returns Right Ascension in degrees.
Definition: GSkyDir.hpp:229
bool is_celestial(void) const
Check if model holds celestial coordinates.
GChatter
Definition: GTypemaps.hpp:33
std::string type(void) const
Return model type.
void radec_deg(const double &ra, const double &dec)
Set equatorial sky direction (degrees)
Definition: GSkyDir.cpp:224
void xml_check_parnum(const std::string &origin, const GXmlElement &xml, const int &number)
Checks number of parameters.
Definition: GTools.cpp:1777
const GModelSpatialPointSource g_spatial_ptsrc_seed
Point source spatial model.
bool xml_has_par(const GXmlElement &xml, const std::string &name)
Checks if parameter with given name in XML element exists.
Definition: GTools.cpp:1596
const GSkyRegion * region(void) const
Return boundary sky region.
void clear(void)
Clear sky direction.
Definition: GSkyDir.cpp:164
void free_members(void)
Delete class members.
virtual bool contains(const GSkyDir &dir, const double &margin=0.0) const
Checks where model contains specified sky direction.
std::string m_type
Spatial model type.
void read(const GXmlElement &xml)
Extract parameter attributes from XML element.
Definition: GModelPar.cpp:229
double value(void) const
Return parameter value.
GSkyDir m_dir
Sky direction representing parameters.
const std::string & unit(void) const
Return parameter unit.
void lb_deg(const double &l, const double &b)
Set galactic sky direction (degrees)
Definition: GSkyDir.cpp:278
Exception handler interface definition.
double dist(const GSkyDir &dir) const
Compute angular distance between sky directions in radians.
Definition: GSkyDir.hpp:271
GModelSpatialPointSource(void)
Void constructor.
void init_members(void)
Initialise class members.
Abstract spatial model base class.
virtual void set_region(void) const
Set boundary sky region.
#define G_WRITE
void init_members(void)
Initialise class members.
virtual GModelSpatialPointSource & operator=(const GModelSpatialPointSource &model)
Assignment operator.
double l_deg(void) const
Return galactic longitude in degrees.
Definition: GSkyDir.hpp:175
double b_deg(void) const
Returns galactic latitude in degrees.
Definition: GSkyDir.hpp:202
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1143
#define G_READ
Sky direction class.
Definition: GSkyDir.hpp:62
const GSkyDir & dir(void) const
Return photon sky direction.
Definition: GPhoton.hpp:110
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
GModelPar m_lat
Declination or Galactic latitude (deg)
Mathematical function definitions.
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:489
void xml_check_type(const std::string &origin, GXmlElement &xml, const std::string &type)
Checks the model type.
Definition: GTools.cpp:1819