GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GCTAEventBin.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GCTAEventBin.cpp - CTA event bin 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 GCTAEventBin.cpp
23 * @brief CTA 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 "GException.hpp"
34#include "GTools.hpp"
35#include "GCTAEventBin.hpp"
36
37/* __ Method name definitions ____________________________________________ */
38#define G_DIR_GET "GCTAEventBin::dir()"
39#define G_ENERGY "GCTAEventBin::energy()"
40#define G_TIME "GCTAEventBin::time()"
41#define G_COUNTS_GET "GCTAEventBin::counts()"
42#define G_SOLIDANGLE "GCTAEventBin::solidangle()"
43#define G_EWIDTH "GCTAEventBin::ewidth()"
44#define G_ONTIME "GCTAEventBin::ontime()"
45#define G_WEIGHT "GCTAEventBin::weight()"
46#define G_DIR_SET "GCTAEventBin::dir(GCTAInstDir&)"
47#define G_ENERGY_SET "GCTAEventBin::energy(GEnergy&)"
48#define G_TIME_SET "GCTAEventBin::time(GTime&)"
49#define G_COUNTS_SET "GCTAEventBin::counts(double&)"
50#define G_SOLIDANGLE_SET "GCTAEventBin::solidangle(double&)"
51#define G_EWIDTH_SET "GCTAEventBin::ewidth(GEnergy&)"
52#define G_ONTIME_SET "GCTAEventBin::ontime(double&)"
53#define G_WEIGHT_SET "GCTAEventBin::weight(double&)"
54
55/* __ Macros _____________________________________________________________ */
56
57/* __ Coding definitions _________________________________________________ */
58
59/* __ Debug definitions __________________________________________________ */
60
61
62/*==========================================================================
63 = =
64 = Constructors/destructors =
65 = =
66 ==========================================================================*/
67
68/***********************************************************************//**
69 * @brief Void constructor
70 ***************************************************************************/
72{
73 // Initialise class members for clean destruction
75
76 // Return
77 return;
78}
79
80
81/***********************************************************************//**
82 * @brief Copy constructor
83 *
84 * @param[in] bin Event bin.
85 ***************************************************************************/
87{
88 // Initialise class members for clean destruction
90
91 // Copy members
92 copy_members(bin);
93
94 // Return
95 return;
96}
97
98
99/***********************************************************************//**
100 * @brief Destructor
101 ***************************************************************************/
103{
104 // Free members
105 free_members();
106
107 // Return
108 return;
109}
110
111
112/*==========================================================================
113 = =
114 = Operators =
115 = =
116 ==========================================================================*/
117
118/***********************************************************************//**
119 * @brief Assignment operator
120 *
121 * @param[in] bin Event bin.
122 * @return Event bin.
123 ***************************************************************************/
125{
126 // Execute only if object is not identical
127 if (this != &bin) {
128
129 // Copy base class members
130 this->GEventBin::operator=(bin);
131
132 // Free members
133 free_members();
134
135 // Initialise private members for clean destruction
136 init_members();
137
138 // Copy members
139 copy_members(bin);
140
141 } // endif: object was not identical
142
143 // Return this object
144 return *this;
145}
146
147
148/*==========================================================================
149 = =
150 = Public methods =
151 = =
152 ==========================================================================*/
153
154/***********************************************************************//**
155 * @brief Clear eventbin
156 ***************************************************************************/
158{
159 // Free class members (base and derived classes, derived class first)
160 free_members();
162 this->GEvent::free_members();
163
164 // Initialise members
165 this->GEvent::init_members();
167 init_members();
168
169 // Return
170 return;
171}
172
173
174/***********************************************************************//**
175 * @brief Clone event bin
176 *
177 * @return Pointer to deep copy of event bin.
178 ***************************************************************************/
180{
181 return new GCTAEventBin(*this);
182}
183
184
185/***********************************************************************//**
186 * @brief Return size of event bin
187 *
188 * The size of the event bin (units sr MeV s) is given by
189 * \f[size = \Omega \times \Delta E \times \Delta T \times W\f]
190 * where
191 * \f$\Omega\f$ is the size of the spatial bin in sr,
192 * \f$\Delta E\f$ is the size of the energy bin in MeV,
193 * \f$\Delta T\f$ is the ontime of the observation in seconds, and
194 * \f$W\f$ is the weight of the bin.
195 ***************************************************************************/
196double GCTAEventBin::size(void) const
197{
198 // Compute bin size
199 double size = solidangle() * ewidth().MeV() * ontime() * weight();
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 encountered.
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 = "Invalid instrument direction pointer encountered. "
221 "Please set up the event bin correctly.";
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 encountered.
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 = "Invalid energy pointer encountered. Please set up "
245 "the event bin correctly.";
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 encountered.
261 *
262 * Returns reference to the time of the event bin.
263 ***************************************************************************/
264const GTime& GCTAEventBin::time(void) const
265{
266 // Throw an exception if time pointer is not valid
267 if (m_time == NULL) {
268 std::string msg = "Invalid time pointer encountered. Please set up "
269 "the event bin correctly.";
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 GException::invalid_value
284 * Invalid counts pointer.
285 *
286 * Returns reference to the number of counts in the event bin.
287 ***************************************************************************/
288double GCTAEventBin::counts(void) const
289{
290 // Throw an exception if counts pointer is not valid
291 if (m_counts == NULL) {
292 std::string msg = "Invalid counts pointer encountered. Please set up "
293 "the event bin correctly.";
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 GCTAEventBin::error(void) const
316{
317 // Compute uncertainty
318 double error = 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 encountered.
332 *
333 * Returns reference to the solid angle of the event bin.
334 ***************************************************************************/
335const double& GCTAEventBin::solidangle(void) const
336{
337 // Throw an exception if counts pointer is not valid
338 if (m_solidangle == NULL) {
339 std::string msg = "Invalid solid angle pointer encountered. Please "
340 "set up the event bin correctly.";
342 }
343
344 // Return solid angle
345 return *m_solidangle;
346}
347
348
349/***********************************************************************//**
350 * @brief Return minimum energy of event bin
351 *
352 * @return Minimum energy of event bin
353 *
354 * Returns minimum energy of event bin, computed using
355 *
356 * \f[
357 * E_{\rm min} = \frac{-\Delta E + sqrt{\Delta E^2 + 4 E^2}}{2}
358 * \f]
359 *
360 * where
361 * \f$\Delta E\f$ is the energy bin width, returned by ewidth() and
362 * \f$E\f$ is the energy, returned by energy().
363 ***************************************************************************/
365{
366 // Get energy and energy width in MeV
367 double energy = this->energy().MeV();
368 double ewidth = this->ewidth().MeV();
369
370 // Compute minimum energy in MeV
371 double arg = ewidth * ewidth + 4.0 * energy * energy;
372 double e_min = 0.5 * (-ewidth + std::sqrt(arg));
373
374 // Set minimum energy
376 emin.MeV(e_min);
377
378 // Return minimum energy
379 return emin;
380}
381
382
383/***********************************************************************//**
384 * @brief Return energy width of event bin
385 *
386 * @return Energy width of event bin
387 *
388 * @exception GException::invalid_value
389 * Invalid energy width pointer encountered.
390 *
391 * Returns reference to the energy width of the event bin.
392 ***************************************************************************/
394{
395 // Throw an exception if energy width pointer is not valid
396 if (m_ewidth == NULL) {
397 std::string msg = "Invalid energy width pointer encountered. Please "
398 "set up the event bin correctly.";
400 }
401
402 // Return energy width
403 return *m_ewidth;
404}
405
406
407/***********************************************************************//**
408 * @brief Return ontime of event bin
409 *
410 * @return Ontime of event bin
411 *
412 * @exception GException::invalid_value
413 * Invalid ontime pointer encountered.
414 *
415 * Returns reference to the ontime of the event bin.
416 ***************************************************************************/
417const double& GCTAEventBin::ontime(void) const
418{
419 // Throw an exception if energy width pointer is not valid
420 if (m_ontime == NULL) {
421 std::string msg = "Invalid ontime pointer encountered. Please set up "
422 "the event bin correctly.";
424 }
425
426 // Return ontime
427 return *m_ontime;
428}
429
430
431/***********************************************************************//**
432 * @brief Return weight of event bin
433 *
434 * @return Weight of event bin
435 *
436 * @exception GException::invalid_value
437 * Invalid weight pointer encountered.
438 *
439 * Returns reference to the weight of the event bin.
440 ***************************************************************************/
441const double& GCTAEventBin::weight(void) const
442{
443 // Throw an exception if weight pointer is not valid
444 if (m_weight == NULL) {
445 std::string msg = "Invalid weight pointer encountered. Please set up "
446 "the event bin correctly.";
448 }
449
450 // Return weight
451 return *m_weight;
452}
453
454
455/***********************************************************************//**
456 * @brief Set instrument direction of event bin
457 *
458 * @param[in] dir Instrument direction of event bin
459 *
460 * @exception GException::invalid_value
461 * No memory available to hold instrument direction.
462 *
463 * Sets the instrument direction of the event bin.
464 ***************************************************************************/
466{
467 // Throw an exception if no memory has been allocated
468 if (m_dir == NULL) {
469 std::string msg = "No memory available to hold instrument direction.";
471 }
472
473 // Set instrument direction
474 *m_dir = dir;
475
476 // Return
477 return;
478}
479
480
481/***********************************************************************//**
482 * @brief Set energy of event bin
483 *
484 * @param[in] energy Energy of event bin
485 *
486 * @exception GException::invalid_value
487 * No memory available to hold energy.
488 *
489 * Sets the energy of the event bin.
490 ***************************************************************************/
491void GCTAEventBin::energy(const GEnergy& energy)
492{
493 // Throw an exception if no memory has been allocated
494 if (m_energy == NULL) {
495 std::string msg = "No memory available to hold energy.";
497 }
498
499 // Set energy
500 *m_energy = energy;
501
502 // Return
503 return;
504}
505
506
507/***********************************************************************//**
508 * @brief Set time of event bin
509 *
510 * @param[in] time Time of event bin
511 *
512 * @exception GException::invalid_value
513 * No memory available to hold time.
514 *
515 * Sets the time of the event bin.
516 ***************************************************************************/
517void GCTAEventBin::time(const GTime& time)
518{
519 // Throw an exception if no memory has been allocated
520 if (m_time == NULL) {
521 std::string msg = "No memory available to hold time.";
523 }
524
525 // Set time
526 *m_time = time;
527
528 // Return
529 return;
530}
531
532
533/***********************************************************************//**
534 * @brief Set number of counts in event bin
535 *
536 * @param[in] counts Number of counts.
537 *
538 * @exception GException::invalid_value
539 * No memory available to hold counts.
540 *
541 * Set the number of counts in the event bin.
542 ***************************************************************************/
543void GCTAEventBin::counts(const double& counts)
544{
545 // Throw an exception if counts pointer is not valid
546 if (m_counts == NULL) {
547 std::string msg = "No memory available to hold counts.";
549 }
550
551 // Set number of counts in event bin
552 *m_counts = counts;
553
554 // Return
555 return;
556}
557
558
559/***********************************************************************//**
560 * @brief Set solid angle of event bin
561 *
562 * @param[in] solidangle Solid angle of event bin
563 *
564 * @exception GException::invalid_value
565 * No memory available to hold solid angle.
566 *
567 * Sets the solid angle of the event bin.
568 ***************************************************************************/
569void GCTAEventBin::solidangle(const double& solidangle)
570{
571 // Throw an exception if no memory has been allocated
572 if (m_solidangle == NULL) {
573 std::string msg = "No memory available to hold solid angle.";
575 }
576
577 // Set solid angle
579
580 // Return
581 return;
582}
583
584
585/***********************************************************************//**
586 * @brief Set energy width of event bin
587 *
588 * @param[in] ewidth Energy width of event bin
589 *
590 * @exception GException::invalid_value
591 * No memory available to hold energy width.
592 *
593 * Sets the energy width of the event bin.
594 ***************************************************************************/
595void GCTAEventBin::ewidth(const GEnergy& ewidth)
596{
597 // Throw an exception if no memory has been allocated
598 if (m_ewidth == NULL) {
599 std::string msg = "No memory available to hold energy width.";
601 }
602
603 // Set energy width
604 *m_ewidth = ewidth;
605
606 // Return
607 return;
608}
609
610
611/***********************************************************************//**
612 * @brief Set ontime of event bin
613 *
614 * @param[in] ontime Ontime of event bin (sec).
615 *
616 * @exception GException::invalid_value
617 * No memory available to hold ontime.
618 *
619 * Sets the ontime of the event bin.
620 ***************************************************************************/
621void GCTAEventBin::ontime(const double& ontime)
622{
623 // Throw an exception if no memory has been allocated
624 if (m_ontime == NULL) {
625 std::string msg = "No memory available to hold ontime.";
627 }
628
629 // Set solid angle
630 *m_ontime = ontime;
631
632 // Return
633 return;
634}
635
636
637/***********************************************************************//**
638 * @brief Set weight of event bin
639 *
640 * @param[in] weight Weight angle of event bin
641 *
642 * @exception GException::invalid_value
643 * No memory available to hold weight.
644 *
645 * Sets the weight of the event bin.
646 ***************************************************************************/
647void GCTAEventBin::weight(const double& weight)
648{
649 // Throw an exception if no memory has been allocated
650 if (m_weight == NULL) {
651 std::string msg = "No memory available to hold weight.";
653 }
654
655 // Set weight
656 *m_weight = weight;
657
658 // Return
659 return;
660}
661
662
663/***********************************************************************//**
664 * @brief Print event information
665 *
666 * @param[in] chatter Chattiness.
667 * @return String containing event information.
668 ***************************************************************************/
669std::string GCTAEventBin::print(const GChatter& chatter) const
670{
671 // Initialise result string
672 std::string result;
673
674 // Continue only if chatter is not silent
675 if (chatter != SILENT) {
676
677 // Append number of counts
678 result.append(gammalib::str(counts()));
679
680 } // endif: chatter was not silent
681
682 // Return result
683 return result;
684}
685
686
687/*==========================================================================
688 = =
689 = Private methods =
690 = =
691 ==========================================================================*/
692
693/***********************************************************************//**
694 * @brief Initialise class members
695 *
696 * This method allocates memory for all event bin attributes and intialises
697 * the attributes to well defined initial values.
698 *
699 * The method assumes that on entry no memory is hold by the member pointers.
700 ***************************************************************************/
702{
703 // Initialise members
704 m_alloc = true;
705 m_ipix = -1; //!< Not part of an event cube
706 m_ieng = -1; //!< Not part of an event cube
707 m_dir = new GCTAInstDir;
708 m_time = new GTime;
709 m_energy = new GEnergy;
710 m_ewidth = new GEnergy;
711 m_counts = new double;
712 m_solidangle = new double;
713 m_ontime = new double;
714 m_weight = new double;
715
716 // Initialise members
717 m_dir->clear();
718 m_time->clear();
719 m_energy->clear();
720 m_ewidth->clear();
721 *m_counts = 0.0;
722 *m_solidangle = 0.0;
723 *m_ontime = 0.0;
724 *m_weight = 0.0;
725
726 // Return
727 return;
728}
729
730
731/***********************************************************************//**
732 * @brief Copy class members
733 *
734 * @param[in] bin Event bin.
735 ***************************************************************************/
737{
738 // First de-allocate existing memory if needed
739 free_members();
740
741 // Copy members by cloning
742 m_dir = new GCTAInstDir(*bin.m_dir);
743 m_time = new GTime(*bin.m_time);
744 m_energy = new GEnergy(*bin.m_energy);
745 m_ewidth = new GEnergy(*bin.m_ewidth);
746 m_counts = new double(*bin.m_counts);
747 m_solidangle = new double(*bin.m_solidangle);
748 m_ontime = new double(*bin.m_ontime);
749 m_weight = new double(*bin.m_weight);
750
751 // Copy non-pointer members
752 m_ipix = bin.m_ipix;
753 m_ieng = bin.m_ieng;
754
755 // Signal memory allocation
756 m_alloc = true;
757
758 // Return
759 return;
760}
761
762
763/***********************************************************************//**
764 * @brief Delete class members
765 *
766 * This method frees all memory of the class attributes and sets the member
767 * pointers to NULL. This method should only be called if new memory is
768 * allocated immediately afterwards (for example by cloning another event
769 * bin), or upon destruction of the object.
770 *
771 * Note that some logic has been implemented that frees only memory that also
772 * has indeed been allocated by the class. Thus if the class only serves as
773 * container to hold memory pointer allocated by someone else (for example
774 * the GCTAEventCube class), no memory is freed.
775 ***************************************************************************/
777{
778 // If memory was allocated then free members now
779 if (m_alloc) {
780 if (m_dir != NULL) delete m_dir;
781 if (m_time != NULL) delete m_time;
782 if (m_energy != NULL) delete m_energy;
783 if (m_ewidth != NULL) delete m_ewidth;
784 if (m_counts != NULL) delete m_counts;
785 if (m_solidangle != NULL) delete m_solidangle;
786 if (m_ontime != NULL) delete m_ontime;
787 if (m_weight != NULL) delete m_weight;
788 }
789
790 // Signal member pointers as free
791 m_dir = NULL;
792 m_time = NULL;
793 m_energy = NULL;
794 m_ewidth = NULL;
795 m_counts = NULL;
796 m_solidangle = NULL;
797 m_ontime = NULL;
798 m_weight = NULL;
799
800 // Signal memory de-allocation
801 m_alloc = false;
802
803 // Return
804 return;
805}
#define G_TIME_SET
#define G_DIR_GET
#define G_EWIDTH_SET
#define G_COUNTS_GET
#define G_ONTIME_SET
#define G_COUNTS_SET
#define G_SOLIDANGLE_SET
#define G_DIR_SET
#define G_ONTIME
#define G_TIME
#define G_WEIGHT
#define G_ENERGY
#define G_SOLIDANGLE
#define G_WEIGHT_SET
CTA event bin class interface definition.
#define G_EWIDTH
Definition GEbounds.cpp:54
Exception handler interface definition.
#define G_ENERGY_SET
Gammalib tools definition.
GChatter
Definition GTypemaps.hpp:33
@ SILENT
Definition GTypemaps.hpp:34
GVector sqrt(const GVector &vector)
Computes square root of vector elements.
Definition GVector.cpp:1426
GCTAEventBin class interface definition.
int m_ieng
Index of energy layer.
double * m_weight
Pointer to weight of bin.
double * m_solidangle
Pointer to solid angle of pixel (sr)
virtual std::string print(const GChatter &chatter=NORMAL) const
Print event information.
virtual double counts(void) const
Return number of counts in event bin.
virtual GCTAEventBin * clone(void) const
Clone event bin.
virtual double size(void) const
Return size of event bin.
GEnergy * m_energy
Pointer to bin energy.
double * m_counts
Pointer to number of counts.
const double & weight(void) const
Return weight of event bin.
const GEnergy & ewidth(void) const
Return energy width of event bin.
virtual GCTAEventBin & operator=(const GCTAEventBin &bin)
Assignment operator.
GEnergy * m_ewidth
Pointer to energy width of bin.
void copy_members(const GCTAEventBin &bin)
Copy class members.
virtual ~GCTAEventBin(void)
Destructor.
GTime * m_time
Pointer to bin time.
virtual const GTime & time(void) const
Return time of event bin.
GCTAEventBin(void)
Void constructor.
virtual const GCTAInstDir & dir(void) const
Return instrument direction of event bin.
void free_members(void)
Delete class members.
virtual double error(void) const
Return error in number of counts.
const double & ontime(void) const
Return ontime of event bin.
virtual const GEnergy & energy(void) const
Return energy of event bin.
int m_ipix
Index in spatial map.
void init_members(void)
Initialise class members.
GCTAInstDir * m_dir
Pointer to bin direction.
double * m_ontime
Pointer to ontime of bin (seconds)
const double & solidangle(void) const
Return solid angle of event bin.
virtual void clear(void)
Clear eventbin.
GEnergy emin(void) const
Return minimum energy of event bin.
bool m_alloc
Signals proper memory allocation.
CTA instrument direction class.
virtual void clear(void)
Clear CTA instrument direction.
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