GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GLATEventBin.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GLATEventBin.cpp - Fermi/LAT event bin class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2009-2013 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 GLATEventBin.cpp
23  * @brief Fermi/LAT event bin class implementation
24  * @author Juergen Knodlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include <string>
32 #include <cmath>
33 #include "GLATEventBin.hpp"
34 #include "GLATException.hpp"
35 #include "GTools.hpp"
36 
37 /* __ Method name definitions ____________________________________________ */
38 #define G_DIR "GLATEventBin::dir()"
39 #define G_ENERGY "GLATEventBin::energy()"
40 #define G_TIME "GLATEventBin::time()"
41 #define G_COUNTS_GET "GLATEventBin::counts()"
42 #define G_COUNTS_SET "GLATEventBin::counts(double&)"
43 #define G_SOLIDANGLE "GLATEventBin::solidangle()"
44 #define G_EWIDTH "GLATEventBin::ewidth()"
45 #define G_ONTIME "GLATEventBin::ontime()"
46 
47 /* __ Macros _____________________________________________________________ */
48 
49 /* __ Coding definitions _________________________________________________ */
50 
51 /* __ Debug definitions __________________________________________________ */
52 
53 
54 /*==========================================================================
55  = =
56  = Constructors/destructors =
57  = =
58  ==========================================================================*/
59 
60 /***********************************************************************//**
61  * @brief Void constructor
62  ***************************************************************************/
64 {
65  // Initialise class members for clean destruction
66  init_members();
67 
68  // Return
69  return;
70 }
71 
72 
73 /***********************************************************************//**
74  * @brief Copy constructor
75  *
76  * @param[in] bin Event bin.
77  ***************************************************************************/
79 {
80  // Initialise class members for clean destruction
81  init_members();
82 
83  // Copy members
84  copy_members(bin);
85 
86  // Return
87  return;
88 }
89 
90 
91 /***********************************************************************//**
92  * @brief Destructor
93  ***************************************************************************/
95 {
96  // Free members
97  free_members();
98 
99  // Return
100  return;
101 }
102 
103 
104 /*==========================================================================
105  = =
106  = Operators =
107  = =
108  ==========================================================================*/
109 
110 /***********************************************************************//**
111  * @brief Assignment operator
112  *
113  * @param[in] bin LAT event bin.
114  * @return Lat event bin.
115  ***************************************************************************/
117 {
118  // Execute only if object is not identical
119  if (this != &bin) {
120 
121  // Copy base class members
122  this->GEventBin::operator=(bin);
123 
124  // Free members
125  free_members();
126 
127  // Initialise private members for clean destruction
128  init_members();
129 
130  // Copy members
131  copy_members(bin);
132 
133  } // endif: object was not identical
134 
135  // Return this object
136  return *this;
137 }
138 
139 
140 /*==========================================================================
141  = =
142  = Public methods =
143  = =
144  ==========================================================================*/
145 
146 /***********************************************************************//**
147  * @brief Clear event bin
148  ***************************************************************************/
150 {
151  // Free class members (base and derived classes, derived class first)
152  free_members();
153  this->GEventBin::free_members();
154  this->GEvent::free_members();
155 
156  // Initialise members
157  this->GEvent::init_members();
158  this->GEventBin::init_members();
159  init_members();
160 
161  // Return
162  return;
163 }
164 
165 
166 /***********************************************************************//**
167  * @brief Clone event bin
168  *
169  * @return Pointer to deep copy of Fermi/LAT event bin.
170  ***************************************************************************/
172 {
173  return new GLATEventBin(*this);
174 }
175 
176 
177 /***********************************************************************//**
178  * @brief Return size of event bin
179  *
180  * @return Size of event bin in units of sr MeV s.
181  *
182  * The size of the event bin (units sr MeV s) is given by
183  * \f[size = \Omega \times \Delta E \times \Delta T\f]
184  * where
185  * \f$\Omega\f$ is the size of the spatial bin in sr,
186  * \f$\Delta E\f$ is the size of the energy bin in MeV, and
187  * \f$\Delta T\f$ is the ontime of the observation in seconds.
188  ***************************************************************************/
189 double GLATEventBin::size(void) const
190 {
191  // Compute bin size
192  double size = solidangle() * ewidth().MeV() * ontime();
193 
194  // Return bin size
195  return size;
196 }
197 
198 
199 /***********************************************************************//**
200  * @brief Return instrument direction of event bin
201  *
202  * @return Instrument direction of event bin.
203  *
204  * @exception GLATException::no_member
205  * Invalid instrument direction pointer.
206  *
207  * Returns reference to the instrument direction of the event bin.
208  ***************************************************************************/
209 const GLATInstDir& GLATEventBin::dir(void) const
210 {
211  // Throw an exception if instrument direction pointer is not valid
212  if (m_dir == NULL) {
214  "Invalid instrument direction pointer.");
215  }
216 
217  // Return instrument direction
218  return *m_dir;
219 }
220 
221 
222 /***********************************************************************//**
223  * @brief Return energy of event bin
224  *
225  * @return Energy of event bin.
226  *
227  * @exception GLATException::no_member
228  * Invalid energy pointer.
229  *
230  * Returns reference to the energy of the event bin.
231  ***************************************************************************/
232 const GEnergy& GLATEventBin::energy(void) const
233 {
234  // Throw an exception if energy pointer is not valid
235  if (m_energy == NULL) {
237  "Invalid energy pointer.");
238  }
239 
240  // Return energy
241  return *m_energy;
242 }
243 
244 
245 /***********************************************************************//**
246  * @brief Return time of event bin
247  *
248  * @return Time of event bin.
249  *
250  * @exception GLATException::no_member
251  * Invalid time pointer.
252  *
253  * Returns reference to the time of the event bin.
254  ***************************************************************************/
255 const GTime& GLATEventBin::time(void) const
256 {
257  // Throw an exception if time pointer is not valid
258  if (m_time == NULL) {
260  "Invalid time pointer.");
261  }
262 
263  // Return time
264  return *m_time;
265 }
266 
267 
268 /***********************************************************************//**
269  * @brief Return number of counts in event bin
270  *
271  * @return Number of counts in event bin.
272  *
273  * @exception GLATException::no_member
274  * Invalid counts pointer.
275  *
276  * Returns reference to the number of counts in the event bin.
277  ***************************************************************************/
278 double GLATEventBin::counts(void) const
279 {
280  // Throw an exception if counts pointer is not valid
281  if (m_counts == NULL) {
283  "Invalid counts pointer.");
284  }
285 
286  // Return counts
287  return *m_counts;
288 }
289 
290 
291 /***********************************************************************//**
292  * @brief Set number of counts in event bin
293  *
294  * @param[in] counts Number of counts.
295  *
296  * @exception GLATException::no_member
297  * Invalid counts pointer.
298  *
299  * Set the number of counts in the event bin.
300  ***************************************************************************/
301 void GLATEventBin::counts(const double& counts)
302 {
303  // Throw an exception if counts pointer is not valid
304  if (m_counts == NULL) {
306  "Invalid counts pointer.");
307  }
308 
309  // Set number of counts in event bin
310  *m_counts = counts;
311 
312  // Return
313  return;
314 }
315 
316 
317 /***********************************************************************//**
318  * @brief Return error in number of counts
319  *
320  * @return Error in number of counts in event bin.
321  *
322  * Returns \f$\sqrt(counts+delta)\f$ as the uncertainty in the number of
323  * counts in the bin. Adding delta avoids uncertainties of 0 which will
324  * lead in the optimisation step to the exlusion of the corresponding bin.
325  * In the actual implementation delta=1e-50.
326  *
327  * @todo The choice of delta has been made somewhat arbitrary, mainly
328  * because the optimizer routines filter error^2 below 1e-100.
329  ***************************************************************************/
330 double GLATEventBin::error(void) const
331 {
332  // Compute uncertainty
333  double error = sqrt(counts()+1.0e-50);
334 
335  // Return error
336  return error;
337 }
338 
339 
340 /***********************************************************************//**
341  * @brief Return solid angle of event bin
342  *
343  * @return Solid angle of event bin.
344  *
345  * @exception GLATException::no_member
346  * Invalid solid angle pointer.
347  *
348  * Returns reference to the solid angle of the event bin.
349  ***************************************************************************/
350 const double& GLATEventBin::solidangle(void) const
351 {
352  // Throw an exception if solid angle pointer is not valid
353  if (m_solidangle == NULL) {
355  "Invalid solid angle pointer.");
356  }
357 
358  // Return solid angle
359  return *m_solidangle;
360 }
361 
362 
363 /***********************************************************************//**
364  * @brief Return energy width of event bin
365  *
366  * @return Energy width of event bin.
367  *
368  * @exception GLATException::no_member
369  * Invalid energy width pointer.
370  *
371  * Returns reference to the energy width of the event bin.
372  ***************************************************************************/
373 const GEnergy& GLATEventBin::ewidth(void) const
374 {
375  // Throw an exception if energy width pointer is not valid
376  if (m_ewidth == NULL) {
378  "Invalid energy width pointer.");
379  }
380 
381  // Return energy width
382  return *m_ewidth;
383 }
384 
385 
386 /***********************************************************************//**
387  * @brief Return ontime of event bin
388  *
389  * @return Ontime of event bin.
390  *
391  * @exception GLATException::no_member
392  * Invalid ontime pointer.
393  *
394  * Returns reference to the ontime of the event bin.
395  ***************************************************************************/
396 const double& GLATEventBin::ontime(void) const
397 {
398  // Throw an exception if ontime pointer is not valid
399  if (m_ontime == NULL) {
401  "Invalid ontime pointer.");
402  }
403 
404  // Return ontime
405  return *m_ontime;
406 }
407 
408 
409 /***********************************************************************//**
410  * @brief Print event information
411  *
412  * @param[in] chatter Chattiness (defaults to NORMAL).
413  * @return String containing number of counts in event bin.
414  ***************************************************************************/
415 std::string GLATEventBin::print(const GChatter& chatter) const
416 {
417  // Initialise result string
418  std::string result;
419 
420  // Continue only if chatter is not silent
421  if (chatter != SILENT) {
422 
423  // Append number of counts
424  result.append(gammalib::str(counts()));
425 
426  } // endif: chatter was not silent
427 
428  // Return result
429  return result;
430 }
431 
432 
433 /*==========================================================================
434  = =
435  = Private methods =
436  = =
437  ==========================================================================*/
438 
439 /***********************************************************************//**
440  * @brief Initialise class members
441  ***************************************************************************/
443 {
444  // Initialise members
445  m_cube = NULL;
446  m_index = -1;
447  m_ipix = -1;
448  m_ieng = -1;
449  m_energy = NULL;
450  m_dir = NULL;
451  m_time = NULL;
452  m_counts = NULL;
453  m_solidangle = NULL;
454  m_ewidth = NULL;
455  m_ontime = NULL;
456 
457  // Return
458  return;
459 }
460 
461 
462 /***********************************************************************//**
463  * @brief Copy class members
464  *
465  * @param[in] bin Event bin.
466  ***************************************************************************/
468 {
469  // Copy members
470  m_cube = bin.m_cube;
471  m_index = bin.m_index;
472  m_ipix = bin.m_ipix;
473  m_ieng = bin.m_ieng;
474  m_energy = bin.m_energy;
475  m_dir = bin.m_dir;
476  m_time = bin.m_time;
477  m_counts = bin.m_counts;
479  m_ewidth = bin.m_ewidth;
480  m_ontime = bin.m_ontime;
481 
482  // Return
483  return;
484 }
485 
486 
487 /***********************************************************************//**
488  * @brief Delete class members
489  ***************************************************************************/
491 {
492  // Return
493  return;
494 }
#define G_TIME
int m_index
Actual skymap index.
Fermi/LAT instrument direction class.
Definition: GLATInstDir.hpp:44
Abstract interface for the event bin class.
Definition: GEventBin.hpp:64
double MeV(void) const
Return energy in MeV.
Definition: GEnergy.cpp:321
Time class.
Definition: GTime.hpp:54
#define G_EWIDTH
Gammalib tools definition.
GLATInstDir * m_dir
Pointer to bin direction.
GEnergy * m_ewidth
Pointer to energy width of bin.
#define G_SOLIDANGLE
virtual double error(void) const
Return error in number of counts.
GEnergy * m_energy
Pointer to bin energy.
virtual GLATEventBin * clone(void) const
Clone event bin.
int m_ipix
Actual spatial index.
const double & solidangle(void) const
Return solid angle of event bin.
Fermi/LAT event bin class.
int m_ieng
Actual energy index.
GVector sqrt(const GVector &vector)
Computes square root of vector elements.
Definition: GVector.cpp:1268
void free_members(void)
Delete class members.
#define G_ENERGY
virtual const GEnergy & energy(void) const
Return energy of event bin.
Fermi/LAT event bin class interface definition.
virtual ~GLATEventBin(void)
Destructor.
#define G_COUNTS_GET
double * m_solidangle
Pointer to solid angle of pixel (sr)
virtual double size(void) const
Return size of event bin.
LAT exception handler interface definition.
const GEnergy & ewidth(void) const
Return energy width of event bin.
virtual const GTime & time(void) const
Return time of event bin.
virtual GLATEventBin & operator=(const GLATEventBin &bin)
Assignment operator.
GLATEventBin(void)
Void constructor.
void init_members(void)
Initialise class members.
GChatter
Definition: GTypemaps.hpp:33
void init_members(void)
Initialise class members.
Definition: GEvent.cpp:141
virtual const GLATInstDir & dir(void) const
Return instrument direction of event bin.
const double & ontime(void) const
Return ontime of event bin.
void free_members(void)
Delete class members.
Definition: GEventBin.cpp:182
virtual void clear(void)
Clear event bin.
double * m_ontime
Pointer to ontime of bin (seconds)
double * m_counts
Pointer to number of counts.
void copy_members(const GLATEventBin &bin)
Copy class members.
virtual double counts(void) const
Return number of counts in event bin.
void free_members(void)
Delete class members.
Definition: GEvent.cpp:163
virtual GEventBin & operator=(const GEventBin &bin)
Assignment operator.
Definition: GEventBin.cpp:105
GLATEventCube * m_cube
Event cube back pointer.
#define G_DIR
GTime * m_time
Pointer to bin time.
#define G_COUNTS_SET
virtual std::string print(const GChatter &chatter=NORMAL) const
Print event information.
void init_members(void)
Initialise class members.
Definition: GEventBin.cpp:160
#define G_ONTIME
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:413