GammaLib  2.1.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-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 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 Photon index [0,...,size()[.
134  * @return Photon.
135  *
136  * @exception GException::out_of_range
137  * Photon index is out of range.
138  ***************************************************************************/
139 GPhoton& GPhotons::operator[](const int& index)
140 {
141  // If index is outside boundary then throw an error
142  #if defined(G_RANGE_CHECK)
143  if (index < 0 || index >= size()) {
144  throw GException::out_of_range(G_OP_ACCESS, "Photon index",
145  index, size());
146  }
147  #endif
148 
149  // Return reference
150  return m_photons[index];
151 }
152 
153 
154 /***********************************************************************//**
155  * @brief Return reference to photon (const version)
156  *
157  * @param[in] index Photon index [0,...,size()[.
158  * @return Photon.
159  *
160  * @exception GException::out_of_range
161  * Photon index is out of range.
162  ***************************************************************************/
163 const GPhoton& GPhotons::operator[](const int& index) const
164 {
165  // If index is outside boundary then throw an error
166  #if defined(G_RANGE_CHECK)
167  if (index < 0 || index >= size()) {
168  throw GException::out_of_range(G_OP_ACCESS, "Photon index",
169  index, size());
170  }
171  #endif
172 
173  // Return reference
174  return m_photons[index];
175 }
176 
177 
178 /*==========================================================================
179  = =
180  = Public methods =
181  = =
182  ==========================================================================*/
183 
184 /***********************************************************************//**
185  * @brief Clear container
186  ***************************************************************************/
187 void GPhotons::clear(void)
188 {
189  // Free members
190  free_members();
191 
192  // Initialise members
193  init_members();
194 
195  // Return
196  return;
197 }
198 
199 
200 /***********************************************************************//**
201  * @brief Clone object
202  *
203  * @return Pointer to deep copy of photon container.
204  ***************************************************************************/
206 {
207  // Clone this image
208  return new GPhotons(*this);
209 }
210 
211 
212 /***********************************************************************//**
213  * @brief Append photon to container
214  *
215  * @param[in] photon Observation.
216  *
217  * This method appends a photon to the container by copying it.
218  ***************************************************************************/
219 void GPhotons::append(const GPhoton& photon)
220 {
221  // Append photon to list
222  m_photons.push_back(photon);
223 
224  // Return
225  return;
226 }
227 
228 
229 /***********************************************************************//**
230  * @brief Insert photon into container
231  *
232  * @param[in] index Photon index [0,...,size()[.
233  * @param[in] photon Photon.
234  *
235  * @exception GException::out_of_range
236  * Photon index is out of range.
237  *
238  * Inserts a @p photon into the container before the photon with the
239  * specified @p index.
240  ***************************************************************************/
241 void GPhotons::insert(const int& index, const GPhoton& photon)
242 {
243  // Compile option: raise exception if index is out of range
244  #if defined(G_RANGE_CHECK)
245  if (is_empty()) {
246  if (index > 0) {
247  throw GException::out_of_range(G_INSERT, "Photon index",
248  index, size());
249  }
250  }
251  else {
252  if (index < 0 || index >= size()) {
253  throw GException::out_of_range(G_INSERT, "Photon index",
254  index, size());
255  }
256  }
257  #endif
258 
259  // Inserts photon
260  m_photons.insert(m_photons.begin()+index, photon);
261 
262  // Return
263  return;
264 }
265 
266 
267 /***********************************************************************//**
268  * @brief Remove photon from container
269  *
270  * @param[in] index Photon index [0,...,size()[.
271  *
272  * @exception GException::out_of_range
273  * Photon index is out of range.
274  *
275  * Remove photon of specified @p index from container.
276  ***************************************************************************/
277 void GPhotons::remove(const int& index)
278 {
279  // Compile option: raise exception if index is out of range
280  #if defined(G_RANGE_CHECK)
281  if (index < 0 || index >= size()) {
282  throw GException::out_of_range(G_REMOVE, "Photon index",
283  index, size());
284  }
285  #endif
286 
287  // Erase photon component from container
288  m_photons.erase(m_photons.begin() + index);
289 
290  // Return
291  return;
292 }
293 
294 
295 /***********************************************************************//**
296  * @brief Append photon container
297  *
298  * @param[in] photons Photon container.
299  *
300  * Append photon container to the container.
301  ***************************************************************************/
302 void GPhotons::extend(const GPhotons& photons)
303 {
304  // Do nothing if photon container is empty
305  if (!photons.is_empty()) {
306 
307  // Get size. Note that we extract the size first to avoid an
308  // endless loop that arises when a container is appended to
309  // itself.
310  int num = photons.size();
311 
312  // Reserve enough space
313  reserve(size() + num);
314 
315  // Loop over all elements and append them to container
316  for (int i = 0; i < num; ++i) {
317  m_photons.push_back(photons[i]);
318  }
319 
320  } // endif: photon container was not empty
321 
322  // Return
323  return;
324 }
325 
326 
327 /***********************************************************************//**
328  * @brief Reserve memory for photons in container
329  *
330  * @param[in] num Number of photons.
331  *
332  * This method reserves memory for @p num photons in the container.
333  ***************************************************************************/
334 void GPhotons::reserve(const int& num)
335 {
336  // Reserve memory
337  m_photons.reserve(num);
338 
339  // Return
340  return;
341 }
342 
343 
344 /***********************************************************************//**
345  * @brief Print photon container information
346  *
347  * @param[in] chatter Chattiness.
348  * @return String containing photon container information.
349  ***************************************************************************/
350 std::string GPhotons::print(const GChatter& chatter) const
351 {
352  // Initialise result string
353  std::string result;
354 
355  // Continue only if chatter is not silent
356  if (chatter != SILENT) {
357 
358  // Append header
359  result.append("=== GPhotons ===");
360 
361  // Append photon container information
362  result.append("\n"+gammalib::parformat("Number of photons"));
363  result.append(gammalib::str(size()));
364 
365  // EXPLICIT: Append photons
366  if (chatter >= EXPLICIT) {
367  for (int i = 0; i < size(); ++i) {
368  result.append("\n");
369  result.append(m_photons[i].print());
370  }
371  }
372 
373  } // endif: chatter was not silent
374 
375  // Return result
376  return result;
377 }
378 
379 
380 /*==========================================================================
381  = =
382  = Private methods =
383  = =
384  ==========================================================================*/
385 
386 /***********************************************************************//**
387  * @brief Initialise class members
388  ***************************************************************************/
390 {
391  // Initialise members
392  m_photons.clear();
393 
394  // Return
395  return;
396 }
397 
398 
399 /***********************************************************************//**
400  * @brief Copy class members
401  *
402  * @param[in] photons Photon container.
403  ***************************************************************************/
404 void GPhotons::copy_members(const GPhotons& photons)
405 {
406  // Copy attributes
407  m_photons = photons.m_photons;
408 
409  // Return
410  return;
411 }
412 
413 
414 /***********************************************************************//**
415  * @brief Delete class members
416  ***************************************************************************/
418 {
419  // Return
420  return;
421 }
GPhotons * clone(void) const
Clone object.
Definition: GPhotons.cpp:205
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:389
void clear(void)
Clear container.
Definition: GPhotons.cpp:187
#define G_INSERT
Definition: GPhotons.cpp:37
void remove(const int &index)
Remove photon from container.
Definition: GPhotons.cpp:277
Photon container class.
Definition: GPhotons.hpp:45
Gammalib tools definition.
void extend(const GPhotons &photons)
Append photon container.
Definition: GPhotons.cpp:302
#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:417
#define G_REMOVE
Definition: GPhotons.cpp:38
std::string print(const GChatter &chatter=NORMAL) const
Print photon container information.
Definition: GPhotons.cpp:350
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:404
void insert(const int &index, const GPhoton &photon)
Insert photon into container.
Definition: GPhotons.cpp:241
void reserve(const int &num)
Reserve memory for photons in container.
Definition: GPhotons.cpp:334
GPhoton & operator[](const int &index)
Return reference to photon.
Definition: GPhotons.cpp:139
void append(const GPhoton &photon)
Append photon to container.
Definition: GPhotons.cpp:219
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:1143
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:489