GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GPhases.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GPhases.cpp - Phase intervals class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2017 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 GPhases.cpp
23  * @brief Phase intervals class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GException.hpp"
32 #include "GTools.hpp"
33 #include "GPhases.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 #define G_REMOVE "GPhases::remove(int&)"
37 #define G_PMIN "GPhases::pmin(int&)"
38 #define G_PMAX "GPhases::pmax(int&)"
39 #define G_INSERT_INTERVAL "GPhases::insert_interval(int&, double&, double&)"
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  * Constructs empty phase intervals.
58  ***************************************************************************/
60 {
61  // Initialise class members
62  init_members();
63 
64  // Return
65  return;
66 }
67 
68 
69 /***********************************************************************//**
70  * @brief Copy constructor
71  *
72  * @param[in] phases Phase intervals.
73  *
74  * Constructs phase intervals by copying other phase intervals.
75  ***************************************************************************/
76 GPhases::GPhases(const GPhases& phases)
77 {
78  // Initialise class members
79  init_members();
80 
81  // Copy members
82  copy_members(phases);
83 
84  // Return
85  return;
86 }
87 
88 
89 /***********************************************************************//**
90  * @brief Single phase interval constructor
91  *
92  * @param[in] pmin Lower boundary of phase interval.
93  * @param[in] pmax Upper boundary of phase interval.
94  *
95  * Constructs Good Time Intervals from a single phase interval, given by
96  * [p@ pmin, @p pmax].
97  ***************************************************************************/
98 GPhases::GPhases(const double& pmin, const double& pmax)
99 {
100  // Initialise members
101  init_members();
102 
103  // Append phase interval
104  append(pmin, pmax);
105 
106  // Return
107  return;
108 }
109 
110 
111 /***********************************************************************//**
112  * @brief Destructor
113  ***************************************************************************/
115 {
116  // Free members
117  free_members();
118 
119  // Return
120  return;
121 }
122 
123 
124 /*==========================================================================
125  = =
126  = Operators =
127  = =
128  ==========================================================================*/
129 
130 /***********************************************************************//**
131  * @brief Assignment operator
132  *
133  * @param[in] phases Phase intervals.
134  * @return Phase intervals.
135  ***************************************************************************/
137 {
138  // Execute only if object is not identical
139  if (this != &phases) {
140 
141  // Free members
142  free_members();
143 
144  // Initialise private members
145  init_members();
146 
147  // Copy members
148  copy_members(phases);
149 
150  } // endif: object was not identical
151 
152  // Return this object
153  return *this;
154 }
155 
156 
157 /*==========================================================================
158  = =
159  = Public methods =
160  = =
161  ==========================================================================*/
162 
163 /***********************************************************************//**
164  * @brief Clear phase intervals
165  ***************************************************************************/
166 void GPhases::clear(void)
167 {
168  // Free members
169  free_members();
170 
171  // Initialise private members
172  init_members();
173 
174  // Return
175  return;
176 }
177 
178 
179 /***********************************************************************//**
180  * @brief Clone phase intervals
181  *
182  * @return Pointer to deep copy of phase intervals.
183  ***************************************************************************/
185 {
186  return new GPhases(*this);
187 }
188 
189 
190 /***********************************************************************//**
191  * @brief Append phase interval
192  *
193  * @param[in] pmin Lower boundary of phase interval.
194  * @param[in] pmax Upper boundary of phase interval.
195  *
196  * Appends a phase interval at the end of the container.
197  ***************************************************************************/
198 void GPhases::append(const double& pmin, const double& pmax)
199 {
200  // Insert phase interval at the end of the list
201  insert_interval(size(), pmin, pmax);
202 
203  // Return
204  return;
205 }
206 
207 
208 /***********************************************************************//**
209  * @brief Remove phase interval
210  *
211  * @param[in] index Phase interval index (0,...,size()-1).
212  *
213  * Removes phase interval at @p index from the container. All intervals
214  * after the specified @p index are moved forward by one position.
215  ***************************************************************************/
216 void GPhases::remove(const int& index)
217 {
218  #if defined(G_RANGE_CHECK)
219  // If index is outside boundary then throw an error
220  if (index < 0 || index >= size()) {
221  throw GException::out_of_range(G_REMOVE, "Phase interval", index,
222  size());
223  }
224  #endif
225 
226  // Remove lower and upper boundaries
227  m_pmin.erase(m_pmin.begin() + index);
228  m_pmax.erase(m_pmax.begin() + index);
229 
230  // Return
231  return;
232 }
233 
234 
235 /***********************************************************************//**
236  * @brief Reserve space for phase intervals
237  *
238  * @param[in] num Number of elements.
239  ***************************************************************************/
240 void GPhases::reserve(const int& num)
241 {
242  // Reserves memory
243  m_pmin.reserve(num);
244  m_pmax.reserve(num);
245 
246  // Return
247  return;
248 }
249 
250 
251 /***********************************************************************//**
252  * @brief Append phase intervals
253  *
254  * @param[in] phases Phase intervals.
255  *
256  * Append phase intervals to the container.
257  ***************************************************************************/
258 void GPhases::extend(const GPhases& phases)
259 {
260  // Continue only if phase intervals are not empty
261  if (!phases.is_empty()) {
262 
263  // Get size. Note that we extract the size first to avoid an
264  // endless loop that arises when a container is appended to
265  // itself.
266  int num = phases.size();
267 
268  // Resize the boundary vectors
269  reserve(size() + num);
270 
271  // Loop over all elements and append them to container
272  for (int i = 0; i < num; ++i) {
273  m_pmin.push_back(phases.pmin(i));
274  m_pmax.push_back(phases.pmax(i));
275  }
276 
277  } // endif: Phase intervals were not empty
278 
279  // Return
280  return;
281 }
282 
283 
284 /***********************************************************************//**
285  * @brief Returns lower boundary for a given phase interval
286  *
287  * @param[in] index Phase interval index (0,...,size()-1).
288  * @return Lower boundary of phase interval.
289  *
290  * @exception GException::out_of_range
291  * Specified index is out of range.
292  ***************************************************************************/
293 double GPhases::pmin(const int& index) const
294 {
295  #if defined(G_RANGE_CHECK)
296  // If index is outside boundary then throw an error
297  if (index < 0 || index >= size()) {
298  throw GException::out_of_range(G_PMIN, "Phase interval", index,
299  size());
300  }
301  #endif
302 
303  // Return
304  return (m_pmin[index]);
305 }
306 
307 
308 /***********************************************************************//**
309  * @brief Returns upper boundary for a given phase interval
310  *
311  * @param[in] index Phase interval index (0,...,size()-1).
312  * @return Upper boundary of phase interval.
313  *
314  * @exception GException::out_of_range
315  * Specified index is out of range.
316  ***************************************************************************/
317 double GPhases::pmax(const int& index) const
318 {
319  #if defined(G_RANGE_CHECK)
320  // If index is outside boundary then throw an error
321  if (index < 0 || index >= size()) {
322  throw GException::out_of_range(G_PMAX, "Phase interval", index,
323  size());
324  }
325  #endif
326 
327  // Return
328  return (m_pmax[index]);
329 }
330 
331 
332 /***********************************************************************//**
333  * @brief Print phase intervals
334  *
335  * @param[in] chatter Chattiness.
336  * @return String containing phase interval information.
337  ***************************************************************************/
338 std::string GPhases::print(const GChatter& chatter) const
339 {
340  // Initialise result string
341  std::string result;
342 
343  // Continue only if chatter is not silent
344  if (chatter != SILENT) {
345 
346  // Append header
347  result.append("=== GPhases ===");
348 
349  // Append phase interval information
350  result.append("\n"+gammalib::parformat("Number of intervals"));
351  result.append(gammalib::str(size()));
352 
353  } // endif: chatter was not silent
354 
355  // Return result
356  return result;
357 }
358 
359 
360 /*==========================================================================
361  = =
362  = Private methods =
363  = =
364  ==========================================================================*/
365 
366 /***********************************************************************//**
367  * @brief Initialise class members
368  ***************************************************************************/
370 {
371  // Initialise members
372  m_pmin.clear();
373  m_pmax.clear();
374 
375  // Return
376  return;
377 }
378 
379 
380 /***********************************************************************//**
381  * @brief Copy class members
382  *
383  * @param[in] phases Phase intervals.
384  ***************************************************************************/
385 void GPhases::copy_members(const GPhases& phases)
386 {
387  // Copy attributes
388  m_pmin = phases.m_pmin;
389  m_pmax = phases.m_pmax;
390 
391  // Return
392  return;
393 }
394 
395 
396 /***********************************************************************//**
397  * @brief Delete class members
398  ***************************************************************************/
400 {
401  // Return
402  return;
403 }
404 
405 
406 /***********************************************************************//**
407  * @brief Insert phase interval
408  *
409  * @param[in] index Index after which interval is inserted.
410  * @param[in] pmin Lower boundary of interval.
411  * @param[in] pmax Upper boundary of interval.
412  *
413  * @exception GException::invalid_argument
414  * Invalid phase interval boundaries specified
415  *
416  * Inserts a phase interval before the interval with the specified @p index.
417  * If no interval with the specified index exists then append the interval
418  * at the end of the existing phase intervals.
419  ***************************************************************************/
420 void GPhases::insert_interval(const int& index,
421  const double& pmin,
422  const double& pmax)
423 {
424  // Throw an exception if phase minimum is outside the range [0,1]
425  if (pmin < 0.0 || pmin > 1.0) {
426  std::string msg = "Phase minimum "+gammalib::str(pmin)+
427  " outside the valid range [0,1]. Please "
428  "specify phase interval boundaries comprised "
429  "within 0 and 1.";
431  }
432 
433  // Throw an exception if phase minimum is outside the range [0,1]
434  if (pmax < 0.0 || pmax > 1.0) {
435  std::string msg = "Phase maximum "+gammalib::str(pmax)+
436  " outside the valid range [0,1]. Please "
437  "specify phase interval boundaries comprised "
438  "within 0 and 1.";
440  }
441 
442  // If phase minimum is smaller than phase maximum then append or insert
443  // a single interval ...
444  if (pmin < pmax) {
445  if (index >= size()) {
446  m_pmin.push_back(pmin);
447  m_pmax.push_back(pmax);
448  }
449  else {
450  m_pmin.insert(m_pmin.begin()+index, pmin);
451  m_pmax.insert(m_pmax.begin()+index, pmax);
452  }
453  }
454 
455  // ... otherwise if the phase minimum is larger than the maximum then
456  // consider this a wrap around interval and append or insert one interval
457  // from phase minimum to 1 and another interval from 0 to phase maximum
458  else if (pmin > pmax) {
459  if (pmin < 1.0) {
460  if (index >= size()) {
461  m_pmin.push_back(pmin);
462  m_pmax.push_back(1.0);
463  }
464  else {
465  m_pmin.insert(m_pmin.begin()+index, pmin);
466  m_pmax.insert(m_pmax.begin()+index, 1.0);
467  }
468  }
469  if (pmax > 0.0) {
470  if (index >= size()) {
471  m_pmin.push_back(0.0);
472  m_pmax.push_back(pmax);
473  }
474  else {
475  m_pmin.insert(m_pmin.begin()+index, 0.0);
476  m_pmax.insert(m_pmax.begin()+index, pmax);
477  }
478  }
479  }
480 
481  // Return
482  return;
483 }
void clear(void)
Clear phase intervals.
Definition: GPhases.cpp:166
void remove(const int &index)
Remove phase interval.
Definition: GPhases.cpp:216
#define G_REMOVE
Definition: GPhases.cpp:36
#define G_INSERT_INTERVAL
Definition: GPhases.cpp:39
#define G_PMIN
Definition: GPhases.cpp:37
void append(const double &pmin, const double &pmax)
Append phase interval.
Definition: GPhases.cpp:198
double pmax(const int &index) const
Returns upper boundary for a given phase interval.
Definition: GPhases.cpp:317
void reserve(const int &num)
Reserve space for phase intervals.
Definition: GPhases.cpp:240
int size(void) const
Return number of phase intervals.
Definition: GPhases.hpp:100
#define G_PMAX
Definition: GPhases.cpp:38
Gammalib tools definition.
GPhases * clone(void) const
Clone phase intervals.
Definition: GPhases.cpp:184
virtual ~GPhases(void)
Destructor.
Definition: GPhases.cpp:114
bool is_empty(void) const
Signal if there are no phase intervals.
Definition: GPhases.hpp:112
void init_members(void)
Initialise class members.
Definition: GPhases.cpp:369
Phase intervals class interface definition.
void insert_interval(const int &index, const double &pmin, const double &pmax)
Insert phase interval.
Definition: GPhases.cpp:420
GChatter
Definition: GTypemaps.hpp:33
void copy_members(const GPhases &phases)
Copy class members.
Definition: GPhases.cpp:385
void extend(const GPhases &phases)
Append phase intervals.
Definition: GPhases.cpp:258
std::vector< double > m_pmax
Definition: GPhases.hpp:78
Exception handler interface definition.
void free_members(void)
Delete class members.
Definition: GPhases.cpp:399
GPhases & operator=(const GPhases &phases)
Assignment operator.
Definition: GPhases.cpp:136
Phase Intervals class.
Definition: GPhases.hpp:42
std::vector< double > m_pmin
Definition: GPhases.hpp:77
double pmin(const int &index) const
Returns lower boundary for a given phase interval.
Definition: GPhases.cpp:293
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1022
GPhases(void)
Void constructor.
Definition: GPhases.cpp:59
std::string print(const GChatter &chatter=NORMAL) const
Print phase intervals.
Definition: GPhases.cpp:338
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:413