GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GPhotons.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GPhotons.cpp - Photon container class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2012-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 GPhotons.cpp
23  * @brief Photon container class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GTools.hpp"
32 #include "GException.hpp"
33 #include "GPhotons.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 #define G_OP_ACCESS "GPhotons::operator[](int&)"
37 #define G_INSERT "GPhotons::insert(int&, GPhoton&)"
38 #define G_REMOVE "GPhotons::remove(int&)"
39 
40 /* __ Macros _____________________________________________________________ */
41 
42 /* __ Coding definitions _________________________________________________ */
43 
44 /* __ Debug definitions __________________________________________________ */
45 
46 
47 /*==========================================================================
48  = =
49  = Constructors/destructors =
50  = =
51  ==========================================================================*/
52 
53 /***********************************************************************//**
54  * @brief Void constructor
55  ***************************************************************************/
57 {
58  // Initialise members
59  init_members();
60 
61  // Return
62  return;
63 }
64 
65 
66 /***********************************************************************//**
67  * @brief Copy constructor
68  *
69  * @param photons Photon container.
70  ***************************************************************************/
72 {
73  // Initialise members
74  init_members();
75 
76  // Copy members
77  copy_members(photons);
78 
79  // Return
80  return;
81 }
82 
83 
84 /***********************************************************************//**
85  * @brief Destructor
86  ***************************************************************************/
88 {
89  // Free members
90  free_members();
91 
92  // Return
93  return;
94 }
95 
96 
97 /*==========================================================================
98  = =
99  = Operators =
100  = =
101  ==========================================================================*/
102 
103 /***********************************************************************//**
104  * @brief Assignment operator
105  *
106  * @param[in] photons Photon container.
107  * @return Photon container.
108  ***************************************************************************/
110 {
111  // Execute only if object is not identical
112  if (this != &photons) {
113 
114  // Free members
115  free_members();
116 
117  // Initialise members
118  init_members();
119 
120  // Copy members
121  copy_members(photons);
122 
123  } // endif: object was not identical
124 
125  // Return this object
126  return *this;
127 }
128 
129 
130 /***********************************************************************//**
131  * @brief Return reference to photon
132  *
133  * @param[in] index Index of photon [0,...,size()-1]
134  *
135  * @exception GException::out_of_range
136  * Photon index is out of range.
137  ***************************************************************************/
138 GPhoton& GPhotons::operator[](const int& index)
139 {
140  // If index is outside boundary then throw an error
141  #if defined(G_RANGE_CHECK)
142  if (index < 0 || index >= size()) {
143  throw GException::out_of_range(G_OP_ACCESS, index, 0, size()-1);
144  }
145  #endif
146 
147  // Return reference
148  return m_photons[index];
149 }
150 
151 
152 /***********************************************************************//**
153  * @brief Return reference to photon (const version)
154  *
155  * @param[in] index Index of photon [0,...,size()-1]
156  *
157  * @exception GException::out_of_range
158  * Photon index is out of range.
159  ***************************************************************************/
160 const GPhoton& GPhotons::operator[](const int& index) const
161 {
162  // If index is outside boundary then throw an error
163  #if defined(G_RANGE_CHECK)
164  if (index < 0 || index >= size()) {
165  throw GException::out_of_range(G_OP_ACCESS, index, 0, size()-1);
166  }
167  #endif
168 
169  // Return reference
170  return m_photons[index];
171 }
172 
173 
174 /*==========================================================================
175  = =
176  = Public methods =
177  = =
178  ==========================================================================*/
179 
180 /***********************************************************************//**
181  * @brief Clear container
182  ***************************************************************************/
183 void GPhotons::clear(void)
184 {
185  // Free members
186  free_members();
187 
188  // Initialise members
189  init_members();
190 
191  // Return
192  return;
193 }
194 
195 
196 /***********************************************************************//**
197  * @brief Clone object
198  *
199  * @return Pointer to deep copy of photon container.
200  ***************************************************************************/
202 {
203  // Clone this image
204  return new GPhotons(*this);
205 }
206 
207 
208 /***********************************************************************//**
209  * @brief Append photon to container
210  *
211  * @param[in] photon Observation.
212  *
213  * This method appends a photon to the container by copying it.
214  ***************************************************************************/
215 void GPhotons::append(const GPhoton& photon)
216 {
217  // Append photon to list
218  m_photons.push_back(photon);
219 
220  // Return
221  return;
222 }
223 
224 
225 /***********************************************************************//**
226  * @brief Insert photon into container
227  *
228  * @param[in] index Photon index (0,...,size()-1).
229  * @param[in] photon Photon.
230  *
231  * @exception GException::out_of_range
232  * Photon index is out of range.
233  *
234  * Inserts a @p photon into the container before the photon with the
235  * specified @p index.
236  ***************************************************************************/
237 void GPhotons::insert(const int& index, const GPhoton& photon)
238 {
239  // Compile option: raise exception if index is out of range
240  #if defined(G_RANGE_CHECK)
241  if (is_empty()) {
242  if (index > 0) {
243  throw GException::out_of_range(G_INSERT, index, 0, size()-1);
244  }
245  }
246  else {
247  if (index < 0 || index >= size()) {
248  throw GException::out_of_range(G_INSERT, index, 0, size()-1);
249  }
250  }
251  #endif
252 
253  // Inserts photon
254  m_photons.insert(m_photons.begin()+index, photon);
255 
256  // Return
257  return;
258 }
259 
260 
261 /***********************************************************************//**
262  * @brief Remove photon from container
263  *
264  * @param[in] index Photon index (0,...,size()-1).
265  *
266  * @exception GException::out_of_range
267  * Photon index is out of range.
268  *
269  * Remove photon of specified @p index from container.
270  ***************************************************************************/
271 void GPhotons::remove(const int& index)
272 {
273  // Compile option: raise exception if index is out of range
274  #if defined(G_RANGE_CHECK)
275  if (index < 0 || index >= size()) {
276  throw GException::out_of_range(G_REMOVE, index, 0, size()-1);
277  }
278  #endif
279 
280  // Erase photon component from container
281  m_photons.erase(m_photons.begin() + index);
282 
283  // Return
284  return;
285 }
286 
287 
288 /***********************************************************************//**
289  * @brief Append photon container
290  *
291  * @param[in] photons Photon container.
292  *
293  * Append photon container to the container.
294  ***************************************************************************/
295 void GPhotons::extend(const GPhotons& photons)
296 {
297  // Do nothing if photon container is empty
298  if (!photons.is_empty()) {
299 
300  // Get size. Note that we extract the size first to avoid an
301  // endless loop that arises when a container is appended to
302  // itself.
303  int num = photons.size();
304 
305  // Reserve enough space
306  reserve(size() + num);
307 
308  // Loop over all elements and append them to container
309  for (int i = 0; i < num; ++i) {
310  m_photons.push_back(photons[i]);
311  }
312 
313  } // endif: photon container was not empty
314 
315  // Return
316  return;
317 }
318 
319 
320 /***********************************************************************//**
321  * @brief Reserve memory for photons in container
322  *
323  * @param[in] num Number of photons.
324  *
325  * This method reserves memory for @p num photons in the container.
326  ***************************************************************************/
327 void GPhotons::reserve(const int& num)
328 {
329  // Reserve memory
330  m_photons.reserve(num);
331 
332  // Return
333  return;
334 }
335 
336 
337 /***********************************************************************//**
338  * @brief Print photon container information
339  *
340  * @param[in] chatter Chattiness (defaults to NORMAL).
341  * @return String containing photon container information.
342  ***************************************************************************/
343 std::string GPhotons::print(const GChatter& chatter) const
344 {
345  // Initialise result string
346  std::string result;
347 
348  // Continue only if chatter is not silent
349  if (chatter != SILENT) {
350 
351  // Append header
352  result.append("=== GPhotons ===");
353 
354  // Append photon container information
355  result.append("\n"+gammalib::parformat("Number of photons"));
356  result.append(gammalib::str(size()));
357 
358  // EXPLICIT: Append photons
359  if (chatter >= EXPLICIT) {
360  for (int i = 0; i < size(); ++i) {
361  result.append("\n");
362  result.append(m_photons[i].print());
363  }
364  }
365 
366  } // endif: chatter was not silent
367 
368  // Return result
369  return result;
370 }
371 
372 
373 /*==========================================================================
374  = =
375  = Private methods =
376  = =
377  ==========================================================================*/
378 
379 /***********************************************************************//**
380  * @brief Initialise class members
381  ***************************************************************************/
383 {
384  // Initialise members
385  m_photons.clear();
386 
387  // Return
388  return;
389 }
390 
391 
392 /***********************************************************************//**
393  * @brief Copy class members
394  *
395  * @param[in] photons Photon container.
396  ***************************************************************************/
397 void GPhotons::copy_members(const GPhotons& photons)
398 {
399  // Copy attributes
400  m_photons = photons.m_photons;
401 
402  // Return
403  return;
404 }
405 
406 
407 /***********************************************************************//**
408  * @brief Delete class members
409  ***************************************************************************/
411 {
412  // Return
413  return;
414 }
GPhotons * clone(void) const
Clone object.
Definition: GPhotons.cpp:201
bool is_empty(void) const
Signal if there are no photons.
Definition: GPhotons.hpp:112
void init_members(void)
Initialise class members.
Definition: GPhotons.cpp:382
void clear(void)
Clear container.
Definition: GPhotons.cpp:183
#define G_INSERT
Definition: GPhotons.cpp:37
void remove(const int &index)
Remove photon from container.
Definition: GPhotons.cpp:271
Photon container class.
Definition: GPhotons.hpp:45
Gammalib tools definition.
void extend(const GPhotons &photons)
Append photon container.
Definition: GPhotons.cpp:295
#define G_OP_ACCESS
Definition: GPhotons.cpp:36
std::vector< GPhoton > m_photons
List of photons.
Definition: GPhotons.hpp:78
Photon container class definition.
Class that handles photons.
Definition: GPhoton.hpp:47
void free_members(void)
Delete class members.
Definition: GPhotons.cpp:410
#define G_REMOVE
Definition: GPhotons.cpp:38
std::string print(const GChatter &chatter=NORMAL) const
Print photon container information.
Definition: GPhotons.cpp:343
GPhotons(void)
Void constructor.
Definition: GPhotons.cpp:56
GPhotons & operator=(const GPhotons &photons)
Assignment operator.
Definition: GPhotons.cpp:109
GChatter
Definition: GTypemaps.hpp:33
void copy_members(const GPhotons &photons)
Copy class members.
Definition: GPhotons.cpp:397
void insert(const int &index, const GPhoton &photon)
Insert photon into container.
Definition: GPhotons.cpp:237
void reserve(const int &num)
Reserve memory for photons in container.
Definition: GPhotons.cpp:327
GPhoton & operator[](const int &index)
Return reference to photon.
Definition: GPhotons.cpp:138
void append(const GPhoton &photon)
Append photon to container.
Definition: GPhotons.cpp:215
Exception handler interface definition.
virtual ~GPhotons(void)
Destructor.
Definition: GPhotons.cpp:87
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1022
int size(void) const
Return number of photons.
Definition: GPhotons.hpp:100
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:413