GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GSPIEventBin.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GSPIEventBin.cpp - INTEGRAL/SPI event bin class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2020 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 GSPIEventBin.hpp
23  * @brief INTEGRAL/SPI 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 <cmath>
32 #include <string>
33 #include "GTools.hpp"
34 #include "GSPIEventBin.hpp"
35 #include "GEnergy.hpp"
36 #include "GTime.hpp"
37 
38 /* __ Method name definitions ____________________________________________ */
39 #define G_MODEL "GSPIEventBin::model(int&)"
40 
41 /* __ Macros _____________________________________________________________ */
42 
43 /* __ Coding definitions _________________________________________________ */
44 
45 /* __ Debug definitions __________________________________________________ */
46 
47 
48 /*==========================================================================
49  = =
50  = Constructors/destructors =
51  = =
52  ==========================================================================*/
53 
54 /***********************************************************************//**
55  * @brief Void constructor
56  *
57  * Creates an empty INTEGRAL/SPI event bin.
58  ***************************************************************************/
60 {
61  // Initialise class members for clean destruction
62  init_members();
63 
64  // Return
65  return;
66 }
67 
68 
69 /***********************************************************************//**
70  * @brief Copy constructor
71  *
72  * @param[in] bin INTEGRAL/SPI event bin.
73  ***************************************************************************/
75 {
76  // Initialise class members for clean destruction
77  init_members();
78 
79  // Copy members
80  copy_members(bin);
81 
82  // Return
83  return;
84 }
85 
86 
87 /***********************************************************************//**
88  * @brief Destructor
89  ***************************************************************************/
91 {
92  // Free members
93  free_members();
94 
95  // Return
96  return;
97 }
98 
99 
100 /*==========================================================================
101  = =
102  = Operators =
103  = =
104  ==========================================================================*/
105 
106 /***********************************************************************//**
107  * @brief Assignment operator
108  *
109  * @param[in] bin INTEGRAL/SPI event bin.
110  * @return INTEGRAL/SPI event bin.
111  ***************************************************************************/
113 {
114  // Execute only if object is not identical
115  if (this != &bin) {
116 
117  // Copy base class members
118  this->GEventBin::operator=(bin);
119 
120  // Free members
121  free_members();
122 
123  // Initialise private members for clean destruction
124  init_members();
125 
126  // Copy members
127  copy_members(bin);
128 
129  } // endif: object was not identical
130 
131  // Return this object
132  return *this;
133 }
134 
135 
136 /*==========================================================================
137  = =
138  = Public methods =
139  = =
140  ==========================================================================*/
141 
142 /***********************************************************************//**
143  * @brief Clear INTEGRAL/SPI event bin
144  *
145  * Clears INTEGRAL/SPI event bin by resetting all class members to an
146  * initial state. Any information that was present before will be lost.
147  ***************************************************************************/
149 {
150  // Free class members (base and derived classes, derived class first)
151  free_members();
152  this->GEventBin::free_members();
153  this->GEvent::free_members();
154 
155  // Initialise members
156  this->GEvent::init_members();
157  this->GEventBin::init_members();
158  init_members();
159 
160  // Return
161  return;
162 }
163 
164 
165 /***********************************************************************//**
166  * @brief Clone event bin
167  *
168  * @return Pointer to deep copy of INTEGRAL/SPI event bin.
169  ***************************************************************************/
171 {
172  return new GSPIEventBin(*this);
173 }
174 
175 
176 /***********************************************************************//**
177  * @brief Return error in number of counts
178  *
179  * @return Error in number of counts in event bin.
180  *
181  * Returns \f$\sqrt(counts+delta)\f$ as the uncertainty in the number of
182  * counts in the bin. Adding delta avoids uncertainties of 0 which will
183  * lead in the optimisation step to the exlusion of the corresponding bin.
184  * In the actual implementation delta=1e-50.
185  *
186  * @todo The choice of delta has been made somewhat arbitrary, mainly
187  * because the optimizer routines filter error^2 below 1e-100.
188  ***************************************************************************/
189 double GSPIEventBin::error(void) const
190 {
191  // Compute uncertainty
192  double error = std::sqrt(counts()+1.0e-50);
193 
194  // Return error
195  return error;
196 }
197 
198 
199 /***********************************************************************//**
200  * @brief Return model value
201  *
202  * @param[in] index Model index.
203  * @return Model value.
204  *
205  * @exception GException::out_of_range
206  * Invalid model index
207  ***************************************************************************/
208 const double& GSPIEventBin::model(const int& index) const
209 {
210  // Optionally check if the model index is valid
211  #if defined(G_RANGE_CHECK)
212  if (index < 0 || index >= m_num_models) {
213  throw GException::out_of_range(G_MODEL, "Invalid model index",
214  index, m_num_models);
215  }
216  #endif
217 
218  // Return reference to model
219  return (m_models[index]);
220 }
221 
222 
223 /***********************************************************************//**
224  * @brief Print event information
225  *
226  * @param[in] chatter Chattiness.
227  * @return String containing event information.
228  ***************************************************************************/
229 std::string GSPIEventBin::print(const GChatter& chatter) const
230 {
231  // Initialise result string
232  std::string result;
233 
234  // Continue only if chatter is not silent
235  if (chatter != SILENT) {
236 
237  // Append number of counts
238  result.append(gammalib::str(counts()));
239 
240  } // endif: chatter was not silent
241 
242  // Return result
243  return result;
244 }
245 
246 
247 /*==========================================================================
248  = =
249  = Private methods =
250  = =
251  ==========================================================================*/
252 
253 /***********************************************************************//**
254  * @brief Initialise class members
255  *
256  * This method allocates memory for all event bin attributes and intialises
257  * the attributes to well defined initial values.
258  *
259  * The method assumes that on entry no memory is hold by the member pointers.
260  ***************************************************************************/
262 {
263  // Allocate members
264  m_alloc = true;
265  m_index = -1; // Signals that event bin does not correspond to cube
266  m_ipt = -1; // Signals that event bin does not correspond to cube
267  m_idir = -1; // Signals that event bin does not correspond to cube
268  m_iebin = -1; // Signals that event bin does not correspond to cube
269  m_num_models = 0; // No models in event bin
270  m_dir = new GSPIInstDir;
271  m_time = new GTime;
272  m_energy = new GEnergy;
273  m_counts = new double;
274  m_ontime = new double;
275  m_livetime = new double;
276  m_size = new double;
277  m_models = NULL;
278 
279  // Initialise members
280  m_dir->clear();
281  m_time->clear();
282  m_energy->clear();
283  *m_counts = 0.0;
284  *m_ontime = 0.0;
285  *m_livetime = 0.0;
286  *m_size = 0.0;
287 
288  // Return
289  return;
290 }
291 
292 
293 /***********************************************************************//**
294  * @brief Copy class members
295  *
296  * @param[in] bin INTEGRAL/SPI event bin.
297  ***************************************************************************/
299 {
300  // First de-allocate existing memory if needed
301  free_members();
302 
303  // Copy non-pointer members
304  m_index = bin.m_index;
305  m_ipt = bin.m_ipt;
306  m_idir = bin.m_idir;
307  m_iebin = bin.m_iebin;
309 
310  // Copy members by cloning
311  m_dir = new GSPIInstDir(*bin.m_dir);
312  m_time = new GTime(*bin.m_time);
313  m_energy = new GEnergy(*bin.m_energy);
314  m_counts = new double(*bin.m_counts);
315  m_ontime = new double(*bin.m_ontime);
316  m_livetime = new double(*bin.m_livetime);
317  m_size = new double(*bin.m_size);
318 
319  // Copy models
320  if (m_num_models > 0) {
321  m_models = new double[m_num_models];
322  for (int i = 0; i < m_num_models; ++i) {
323  m_models[i] = bin.m_models[i];
324  }
325  }
326 
327  // Signal memory allocation
328  m_alloc = true;
329 
330  // Return
331  return;
332 }
333 
334 
335 /***********************************************************************//**
336  * @brief Delete class members
337  *
338  * This method frees all memory of the class attributes and sets the member
339  * pointers to NULL. This method should only be called if new memory is
340  * allocated immediately afterwards (for example by cloning another event
341  * bin), or upon destruction of the object.
342  *
343  * Note that some logic has been implemented that frees only memory that also
344  * has indeed been allocated by the class. Thus if the class only serves as
345  * container to hold memory pointer allocated by someone else (for example
346  * the GSPIEventCube class), no memory is freed.
347  ***************************************************************************/
349 {
350  // If memory was allocated then free members now
351  if (m_alloc) {
352  if (m_dir != NULL) delete m_dir;
353  if (m_time != NULL) delete m_time;
354  if (m_energy != NULL) delete m_energy;
355  if (m_counts != NULL) delete m_counts;
356  if (m_ontime != NULL) delete m_ontime;
357  if (m_livetime != NULL) delete m_livetime;
358  if (m_size != NULL) delete m_size;
359  if (m_models != NULL) delete [] m_models;
360  }
361 
362  // Signal member pointers as free
363  m_dir = NULL;
364  m_time = NULL;
365  m_energy = NULL;
366  m_counts = NULL;
367  m_ontime = NULL;
368  m_livetime = NULL;
369  m_size = NULL;
370  m_models = NULL;
371 
372  // Signal memory de-allocation
373  m_alloc = false;
374 
375  // Return
376  return;
377 }
GEnergy * m_energy
Pointer to energy of bin.
GSPIInstDir * m_dir
Pointer to direction of bin.
virtual double counts(void) const
Return number of counts.
virtual ~GSPIEventBin(void)
Destructor.
Energy value class definition.
double * m_livetime
Pointer to livetime of bin.
int m_ipt
Pointing index.
#define G_MODEL
Abstract interface for the event bin class.
Definition: GEventBin.hpp:64
virtual double error(void) const
Return error in number of counts.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print event information.
virtual void clear(void)
Clear INTEGRAL/SPI instrument direction.
void clear(void)
Clear time.
Definition: GTime.cpp:252
int m_idir
Direction index.
Time class.
Definition: GTime.hpp:55
Gammalib tools definition.
virtual void clear(void)
Clear INTEGRAL/SPI event bin.
int m_index
Dataspace index.
virtual GSPIEventBin & operator=(const GSPIEventBin &bin)
Assignment operator.
INTEGRAL/SPI instrument direction class.
Definition: GSPIInstDir.hpp:52
bool m_alloc
Signals proper memory allocation.
void free_members(void)
Delete class members.
GVector sqrt(const GVector &vector)
Computes square root of vector elements.
Definition: GVector.cpp:1358
double * m_ontime
Pointer to ontime of bin.
void init_members(void)
Initialise class members.
virtual GSPIEventBin * clone(void) const
Clone event bin.
GChatter
Definition: GTypemaps.hpp:33
double * m_size
Pointer to size of bin.
void init_members(void)
Initialise class members.
Definition: GEvent.cpp:141
void free_members(void)
Delete class members.
Definition: GEventBin.cpp:182
double * m_models
Pointer to models of bin.
void free_members(void)
Delete class members.
Definition: GEvent.cpp:163
GTime * m_time
Pointer to time of bin.
double * m_counts
Pointer to number of counts.
int m_iebin
Energy bin index.
INTEGRAL/SPI event bin class.
virtual GEventBin & operator=(const GEventBin &bin)
Assignment operator.
Definition: GEventBin.cpp:105
void copy_members(const GSPIEventBin &bin)
Copy class members.
const double & model(const int &index) const
Return model value.
INTEGRAL/SPI event bin class definition.
void init_members(void)
Initialise class members.
Definition: GEventBin.cpp:160
Time class interface definition.
GSPIEventBin(void)
Void constructor.
void clear(void)
Clear instance.
Definition: GEnergy.cpp:261
int m_num_models
Number of models in bin.
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