GammaLib 2.2.0.dev
Loading...
Searching...
No Matches
GCOMEventBin.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GCOMEventBin.cpp - COMPTEL event bin class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2012-2018 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 GCOMEventBin.cpp
23 * @brief COMPTEL event bin class implementation.
24 * @author Juergen Knoedlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include <string>
32#include <cmath>
33#include "GCOMEventBin.hpp"
34#include "GTools.hpp"
35
36/* __ Method name definitions ____________________________________________ */
37#define G_DIR_GET "GCOMEventBin::dir()"
38#define G_ENERGY_GET "GCOMEventBin::energy()"
39#define G_TIME_GET "GCOMEventBin::time()"
40#define G_COUNTS_GET "GCOMEventBin::counts()"
41#define G_SOLIDANGLE_GET "GCOMEventBin::solidangle()"
42#define G_EWIDTH_GET "GCOMEventBin::ewidth()"
43#define G_ONTIME_GET "GCOMEventBin::ontime()"
44#define G_DIR_SET "GCOMEventBin::dir(GCOMInstDir&)"
45#define G_ENERGY_SET "GCOMEventBin::energy(GEnergy&)"
46#define G_TIME_SET "GCOMEventBin::time(GTime&)"
47#define G_COUNTS_SET "GCOMEventBin::counts(double&)"
48#define G_SOLIDANGLE_SET "GCOMEventBin::solidangle(double&)"
49#define G_EWIDTH_SET "GCOMEventBin::ewidth(GEnergy&)"
50#define G_ONTIME_SET "GCOMEventBin::ontime(double&)"
51
52/* __ Macros _____________________________________________________________ */
53
54/* __ Coding definitions _________________________________________________ */
55
56/* __ Debug definitions __________________________________________________ */
57
58
59/*==========================================================================
60 = =
61 = Constructors/destructors =
62 = =
63 ==========================================================================*/
64
65/***********************************************************************//**
66 * @brief Void constructor
67 ***************************************************************************/
69{
70 // Initialise class members for clean destruction
72
73 // Return
74 return;
75}
76
77
78/***********************************************************************//**
79 * @brief Copy constructor
80 *
81 * @param[in] bin Event bin.
82 ***************************************************************************/
84{
85 // Initialise class members for clean destruction
87
88 // Copy members
89 copy_members(bin);
90
91 // Return
92 return;
93}
94
95
96/***********************************************************************//**
97 * @brief Destructor
98 ***************************************************************************/
100{
101 // Free members
102 free_members();
103
104 // Return
105 return;
106}
107
108
109/*==========================================================================
110 = =
111 = Operators =
112 = =
113 ==========================================================================*/
114
115/***********************************************************************//**
116 * @brief Assignment operator
117 *
118 * @param[in] bin Event bin.
119 * @return Event bin.
120 ***************************************************************************/
122{
123 // Execute only if object is not identical
124 if (this != &bin) {
125
126 // Copy base class members
127 this->GEventBin::operator=(bin);
128
129 // Free members
130 free_members();
131
132 // Initialise private members for clean destruction
133 init_members();
134
135 // Copy members
136 copy_members(bin);
137
138 } // endif: object was not identical
139
140 // Return this object
141 return *this;
142}
143
144
145/*==========================================================================
146 = =
147 = Public methods =
148 = =
149 ==========================================================================*/
150
151/***********************************************************************//**
152 * @brief Clear instance
153 *
154 * This method properly resets the instance to an initial state.
155 ***************************************************************************/
157{
158 // Free class members (base and derived classes, derived class first)
159 free_members();
161 this->GEvent::free_members();
162
163 // Initialise members
164 this->GEvent::init_members();
166 init_members();
167
168 // Return
169 return;
170}
171
172
173/***********************************************************************//**
174 * @brief Clone instance
175 *
176 * @return Pointer to deep copy of event bin.
177 ***************************************************************************/
179{
180 return new GCOMEventBin(*this);
181}
182
183
184/***********************************************************************//**
185 * @brief Return size of event bin
186 *
187 * @return Size of event bin (sr MeV s)
188 *
189 * The size of the event bin (units: sr MeV s) is given by
190 * \f[size = \Omega \times \Delta E \times \Delta T\f]
191 * where
192 * \f$\Omega\f$ is the size of the spatial bin in sr,
193 * \f$\Delta E\f$ is the size of the energy bin in MeV, and
194 * \f$\Delta T\f$ is the ontime of the observation in seconds.
195 ***************************************************************************/
196double GCOMEventBin::size(void) const
197{
198 // Compute bin size
199 double size = solidangle() * ewidth().MeV() * ontime();
200
201 // Return bin size
202 return size;
203}
204
205
206/***********************************************************************//**
207 * @brief Return instrument direction of event bin
208 *
209 * @return Instrument direction of event bin
210 *
211 * @exception GException::invalid_value
212 * Invalid instrument direction pointer.
213 *
214 * Returns reference to the instrument direction of the event bin.
215 ***************************************************************************/
217{
218 // Throw an exception if instrument direction pointer is not valid
219 if (m_dir == NULL) {
220 std::string msg = "No memory has been allocated from which the "
221 "instrument direction could be retrieved.";
223 }
224
225 // Return instrument direction
226 return *m_dir;
227}
228
229
230/***********************************************************************//**
231 * @brief Return energy of event bin
232 *
233 * @return Energy of event bin
234 *
235 * @exception GException::invalid_value
236 * Invalid energy pointer.
237 *
238 * Returns reference to the energy of the event bin.
239 ***************************************************************************/
241{
242 // Throw an exception if energy pointer is not valid
243 if (m_energy == NULL) {
244 std::string msg = "No memory has been allocated from which the energy "
245 "could be retrieved.";
247 }
248
249 // Return energy
250 return *m_energy;
251}
252
253
254/***********************************************************************//**
255 * @brief Return time of event bin
256 *
257 * @return Time of event bin
258 *
259 * @exception GException::invalid_value
260 * Invalid time pointer.
261 *
262 * Returns reference to the time of the event bin.
263 ***************************************************************************/
264const GTime& GCOMEventBin::time(void) const
265{
266 // Throw an exception if time pointer is not valid
267 if (m_energy == NULL) {
268 std::string msg = "No memory has been allocated for which the time "
269 "could be retrieved.";
271 }
272
273 // Return time
274 return *m_time;
275}
276
277
278/***********************************************************************//**
279 * @brief Return number of counts in event bin
280 *
281 * @return Number of counts in event bin
282 *
283 * @exception GCTAException::invalid_value
284 * Invalid counts pointer.
285 *
286 * Returns number of counts in the event bin.
287 ***************************************************************************/
288double GCOMEventBin::counts(void) const
289{
290 // Throw an exception if counts pointer is not valid
291 if (m_counts == NULL) {
292 std::string msg = "No memory has been allocated from which the counts "
293 "could be retrieved.";
295 }
296
297 // Return counts
298 return *m_counts;
299}
300
301
302/***********************************************************************//**
303 * @brief Return error in number of counts
304 *
305 * @return Error in number of counts in event bin.
306 *
307 * Returns \f$\sqrt(counts+delta)\f$ as the uncertainty in the number of
308 * counts in the bin. Adding delta avoids uncertainties of 0 which will
309 * lead in the optimisation step to the exlusion of the corresponding bin.
310 * In the actual implementation delta=1e-50.
311 *
312 * @todo The choice of delta has been made somewhat arbitrary, mainly
313 * because the optimizer routines filter error^2 below 1e-100.
314 ***************************************************************************/
315double GCOMEventBin::error(void) const
316{
317 // Compute uncertainty
318 double error = std::sqrt(counts()+1.0e-50);
319
320 // Return error
321 return error;
322}
323
324
325/***********************************************************************//**
326 * @brief Return solid angle of event bin
327 *
328 * @return Solid angle of event bin
329 *
330 * @exception GException::invalid_value
331 * Invalid solid angle pointer.
332 *
333 * Returns reference to the solid angle of the event bin.
334 ***************************************************************************/
335const double& GCOMEventBin::solidangle(void) const
336{
337 // Throw an exception if counts pointer is not valid
338 if (m_solidangle == NULL) {
339 std::string msg = "No memory has been allocated from which solid angle "
340 "could be retrieved.";
342 }
343
344 // Return solid angle
345 return *m_solidangle;
346}
347
348
349/***********************************************************************//**
350 * @brief Return energy width of event bin
351 *
352 * @return Energy width of event bin
353 *
354 * @exception GException::invalid_value
355 * Invalid energy width pointer.
356 *
357 * Returns reference to the energy width of the event bin.
358 ***************************************************************************/
360{
361 // Throw an exception if energy width pointer is not valid
362 if (m_ewidth == NULL) {
363 std::string msg = "No memory has been allocated from which the energy "
364 "width could be retrieved.";
366 }
367
368 // Return energy width
369 return *m_ewidth;
370}
371
372
373/***********************************************************************//**
374 * @brief Return ontime of event bin
375 *
376 * @return Ontime of event bin
377 *
378 * @exception GException::invalid_value
379 * Invalid ontime pointer.
380 *
381 * Returns reference to the ontime of the event bin.
382 ***************************************************************************/
383const double& GCOMEventBin::ontime(void) const
384{
385 // Throw an exception if energy width pointer is not valid
386 if (m_ontime == NULL) {
387 std::string msg = "No memory has been allocated from which the ontime "
388 "could be retrieved.";
390 }
391
392 // Return ontime
393 return *m_ontime;
394}
395
396
397/***********************************************************************//**
398 * @brief Set instrument direction of event bin
399 *
400 * @param[in] dir Instrument direction of event bin
401 *
402 * @exception GException::invalid_value
403 * No memory allocated to store the instrument direction.
404 *
405 * Sets the instrument direction of the event bin.
406 ***************************************************************************/
408{
409 // Throw an exception if no memory has been allocated
410 if (m_dir == NULL) {
411 std::string msg = "No memory has been allocated to store the "
412 "instrument direction.";
414 }
415
416 // Set instrument direction
417 *m_dir = dir;
418
419 // Return
420 return;
421}
422
423
424/***********************************************************************//**
425 * @brief Set energy of event bin
426 *
427 * @param[in] energy Energy of event bin
428 *
429 * @exception GException::invalid_value
430 * No memory allocated to store energy.
431 *
432 * Sets the energy of the event bin.
433 ***************************************************************************/
434void GCOMEventBin::energy(const GEnergy& energy)
435{
436 // Throw an exception if no memory has been allocated
437 if (m_energy == NULL) {
438 std::string msg = "No memory has been allocated to store the energy.";
440 }
441
442 // Set energy
443 *m_energy = energy;
444
445 // Return
446 return;
447}
448
449
450/***********************************************************************//**
451 * @brief Set time of event bin
452 *
453 * @param[in] time Time of event bin
454 *
455 * @exception GException::invalid_value
456 * No memory allocated to store the time.
457 *
458 * Sets the time of the event bin.
459 ***************************************************************************/
460void GCOMEventBin::time(const GTime& time)
461{
462 // Throw an exception if no memory has been allocated
463 if (m_time == NULL) {
464 std::string msg = "No memory has been allocated to hold store the "
465 "time.";
467 }
468
469 // Set time
470 *m_time = time;
471
472 // Return
473 return;
474}
475
476
477/***********************************************************************//**
478 * @brief Set number of counts in event bin
479 *
480 * @param[in] counts Number of counts.
481 *
482 * @exception GException::invalid_value
483 * No memory allocated to store counts.
484 *
485 * Set the number of counts in the event bin.
486 ***************************************************************************/
487void GCOMEventBin::counts(const double& counts)
488{
489 // Throw an exception if counts pointer is not valid
490 if (m_counts == NULL) {
491 std::string msg = "No memory has been allocated to store the counts.";
493 }
494
495 // Set number of counts in event bin
496 *m_counts = counts;
497
498 // Return
499 return;
500}
501
502
503/***********************************************************************//**
504 * @brief Set solid angle of event bin
505 *
506 * @param[in] solidangle Solid angle of event bin
507 *
508 * @exception GException::invalid_value
509 * No memory allocated to store solid angle.
510 *
511 * Sets the solid angle of the event bin.
512 ***************************************************************************/
513void GCOMEventBin::solidangle(const double& solidangle)
514{
515 // Throw an exception if no memory has been allocated
516 if (m_solidangle == NULL) {
517 std::string msg = "No memory has been allocated to store the solid "
518 "angle.";
520 }
521
522 // Set solid angle
524
525 // Return
526 return;
527}
528
529
530/***********************************************************************//**
531 * @brief Set energy width of event bin
532 *
533 * @param[in] ewidth Energy width of event bin
534 *
535 * @exception GException::invalid_value
536 * No memory allocated to store energy width.
537 *
538 * Sets the energy width of the event bin.
539 ***************************************************************************/
540void GCOMEventBin::ewidth(const GEnergy& ewidth)
541{
542 // Throw an exception if no memory has been allocated
543 if (m_ewidth == NULL) {
544 std::string msg = "No memory has been allocated to store the energy "
545 "width.";
547 }
548
549 // Set energy width
550 *m_ewidth = ewidth;
551
552 // Return
553 return;
554}
555
556
557/***********************************************************************//**
558 * @brief Set ontime of event bin
559 *
560 * @param[in] ontime Ontime of event bin (sec).
561 *
562 * @exception GException::invalid_value
563 * No memory allocated to store ontime.
564 *
565 * Sets the ontime of the event bin.
566 ***************************************************************************/
567void GCOMEventBin::ontime(const double& ontime)
568{
569 // Throw an exception if no memory has been allocated
570 if (m_ontime == NULL) {
571 std::string msg = "No memory has been allocated to store the ontime.";
573 }
574
575 // Set solid angle
576 *m_ontime = ontime;
577
578 // Return
579 return;
580}
581
582
583/***********************************************************************//**
584 * @brief Print event information
585 *
586 * @param[in] chatter Chattiness.
587 * @return String containing event information.
588 ***************************************************************************/
589std::string GCOMEventBin::print(const GChatter& chatter) const
590{
591 // Initialise result string
592 std::string result;
593
594 // Continue only if chatter is not silent
595 if (chatter != SILENT) {
596
597 // Append number of counts
598 result.append(gammalib::str(counts()));
599
600 } // endif: chatter was not silent
601
602 // Return result
603 return result;
604}
605
606
607/*==========================================================================
608 = =
609 = Private methods =
610 = =
611 ==========================================================================*/
612
613/***********************************************************************//**
614 * @brief Initialise class members
615 *
616 * This method allocates memory for all event bin attributes and intialises
617 * the attributes to well defined initial values.
618 *
619 * The method assumes that on entry no memory is hold by the member pointers.
620 ***************************************************************************/
622{
623 // Allocate members
624 m_alloc = true;
625 m_index = -1; // Signals that event bin does not correspond to cube
626 m_dir = new GCOMInstDir;
627 m_time = new GTime;
628 m_energy = new GEnergy;
629 m_ewidth = new GEnergy;
630 m_counts = new double;
631 m_solidangle = new double;
632 m_ontime = new double;
633
634 // Initialise members
635 m_dir->clear();
636 m_time->clear();
637 m_energy->clear();
638 m_ewidth->clear();
639 *m_counts = 0.0;
640 *m_solidangle = 0.0;
641 *m_ontime = 0.0;
642
643 // Return
644 return;
645}
646
647
648/***********************************************************************//**
649 * @brief Copy class members
650 *
651 * @param[in] bin Event bin.
652 ***************************************************************************/
654{
655 // First de-allocate existing memory if needed
656 free_members();
657
658 // Copy members by cloning
659 m_dir = new GCOMInstDir(*bin.m_dir);
660 m_time = new GTime(*bin.m_time);
661 m_energy = new GEnergy(*bin.m_energy);
662 m_ewidth = new GEnergy(*bin.m_ewidth);
663 m_counts = new double(*bin.m_counts);
664 m_solidangle = new double(*bin.m_solidangle);
665 m_ontime = new double(*bin.m_ontime);
666
667 // Copy non-pointer members
668 m_index = bin.m_index;
669
670 // Signal memory allocation
671 m_alloc = true;
672
673 // Return
674 return;
675}
676
677
678/***********************************************************************//**
679 * @brief Delete class members
680 *
681 * This method frees all memory of the class attributes and sets the member
682 * pointers to NULL. This method should only be called if new memory is
683 * allocated immediately afterwards (for example by cloning another event
684 * bin), or upon destruction of the object.
685 *
686 * Note that some logic has been implemented that frees only memory that also
687 * has indeed been allocated by the class. Thus if the class only serves as
688 * container to hold memory pointer allocated by someone else (for example
689 * the GCOMEventCube class), no memory is freed.
690 ***************************************************************************/
692{
693 // If memory was allocated then free members now
694 if (m_alloc) {
695 if (m_dir != NULL) delete m_dir;
696 if (m_time != NULL) delete m_time;
697 if (m_energy != NULL) delete m_energy;
698 if (m_ewidth != NULL) delete m_ewidth;
699 if (m_counts != NULL) delete m_counts;
700 if (m_solidangle != NULL) delete m_solidangle;
701 if (m_ontime != NULL) delete m_ontime;
702 }
703
704 // Signal member pointers as free
705 m_dir = NULL;
706 m_time = NULL;
707 m_energy = NULL;
708 m_ewidth = NULL;
709 m_counts = NULL;
710 m_solidangle = NULL;
711 m_ontime = NULL;
712
713 // Signal memory de-allocation
714 m_alloc = false;
715
716 // Return
717 return;
718}
#define G_TIME_GET
#define G_TIME_SET
#define G_DIR_GET
#define G_SOLIDANGLE_GET
#define G_EWIDTH_SET
#define G_COUNTS_GET
#define G_ONTIME_SET
#define G_COUNTS_SET
#define G_SOLIDANGLE_SET
#define G_EWIDTH_GET
#define G_DIR_SET
#define G_ONTIME_GET
COMPTEL event bin class interface definition.
#define G_ENERGY_GET
#define G_ENERGY_SET
Gammalib tools definition.
GChatter
Definition GTypemaps.hpp:33
@ SILENT
Definition GTypemaps.hpp:34
COMPTEL event bin class.
GEnergy * m_ewidth
Pointer to energy width of bin.
double * m_counts
Pointer to number of counts.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print event information.
virtual const GTime & time(void) const
Return time of event bin.
void init_members(void)
Initialise class members.
GTime * m_time
Pointer to bin time.
GCOMInstDir * m_dir
Pointer to bin direction.
GCOMEventBin(void)
Void constructor.
double * m_ontime
Pointer to ontime of bin (seconds)
const double & ontime(void) const
Return ontime of event bin.
virtual const GCOMInstDir & dir(void) const
Return instrument direction of event bin.
virtual GCOMEventBin & operator=(const GCOMEventBin &bin)
Assignment operator.
double * m_solidangle
Pointer to solid angle of pixel (sr)
virtual double error(void) const
Return error in number of counts.
const GEnergy & ewidth(void) const
Return energy width of event bin.
virtual double size(void) const
Return size of event bin.
int m_index
Dataspace index.
virtual const GEnergy & energy(void) const
Return energy of event bin.
void copy_members(const GCOMEventBin &bin)
Copy class members.
virtual GCOMEventBin * clone(void) const
Clone instance.
virtual void clear(void)
Clear instance.
virtual double counts(void) const
Return number of counts in event bin.
bool m_alloc
Signals proper memory allocation.
virtual ~GCOMEventBin(void)
Destructor.
GEnergy * m_energy
Pointer to bin energy.
const double & solidangle(void) const
Return solid angle of event bin.
void free_members(void)
Delete class members.
Interface for the COMPTEL instrument direction class.
virtual void clear(void)
Clear instance.
Class that handles energies in a unit independent way.
Definition GEnergy.hpp:48
double MeV(void) const
Return energy in MeV.
Definition GEnergy.cpp:342
void clear(void)
Clear instance.
Definition GEnergy.cpp:267
Abstract interface for the event bin class.
Definition GEventBin.hpp:64
void free_members(void)
Delete class members.
virtual GEventBin & operator=(const GEventBin &bin)
Assignment operator.
void init_members(void)
Initialise class members.
void free_members(void)
Delete class members.
Definition GEvent.cpp:163
void init_members(void)
Initialise class members.
Definition GEvent.cpp:141
Time class.
Definition GTime.hpp:55
void clear(void)
Clear time.
Definition GTime.cpp:252
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition GTools.cpp:508