GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GEnergy.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GEnergy.hpp - Energy class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2014 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 GEnergy.hpp
23  * @brief Energy value class definition.
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GENERGY_HPP
28 #define GENERGY_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include "GBase.hpp"
33 
34 
35 /***********************************************************************//**
36  * @class GEnergy
37  *
38  * @brief Class that handles energies in a unit independent way.
39  *
40  * The GEnergy class stores an energy value in units of MeV and implements
41  * methods that provide automatic conversion of the energy values in other
42  * units. This makes instrument specific implementations more robust and
43  * reduces the risk of unit errors. The method also holds on request the
44  * log10 of the energy in MeV, which is only recomputed when the energy
45  * value is changed. This considerably reduces the number of log10 operations
46  * in many applications and speeds up the performance.
47  ***************************************************************************/
48 class GEnergy : public GBase {
49 
50  // Operator friends
51  friend GEnergy operator+(const GEnergy& a, const GEnergy& b);
52  friend GEnergy operator-(const GEnergy& a, const GEnergy& b);
53  friend GEnergy operator*(const double& a, const GEnergy& b);
54  friend GEnergy operator*(const GEnergy& a, const double& b);
55  friend GEnergy operator/(const GEnergy& a, const double& b);
56  friend double operator/(const GEnergy& a, const GEnergy& b);
57  friend bool operator==(const GEnergy& a, const GEnergy& b);
58  friend bool operator!=(const GEnergy& a, const GEnergy& b);
59  friend bool operator<(const GEnergy& a, const GEnergy& b);
60  friend bool operator<=(const GEnergy& a, const GEnergy& b);
61  friend bool operator>(const GEnergy& a, const GEnergy& b);
62  friend bool operator>=(const GEnergy& a, const GEnergy& b);
63 
64 public:
65  // Constructors and destructors
66  GEnergy(void);
67  GEnergy(const GEnergy& eng);
68  GEnergy(const double& eng, const std::string& unit);
69  virtual ~GEnergy(void);
70 
71  // Operators
72  GEnergy& operator=(const GEnergy& eng);
73  GEnergy& operator+=(const GEnergy& eng);
74  GEnergy& operator-=(const GEnergy& eng);
75  GEnergy& operator*=(const double& scale);
76  GEnergy& operator/=(const double& scale);
77  void operator()(const double& eng, const std::string& unit);
78  double operator()(const std::string& unit) const;
79 
80  // Methods
81  void clear(void);
82  GEnergy* clone(void) const;
83  std::string classname(void) const;
84  double erg(void) const;
85  double keV(void) const;
86  double MeV(void) const;
87  double GeV(void) const;
88  double TeV(void) const;
89  double Angstrom(void) const;
90  double log10erg(void) const;
91  double log10keV(void) const;
92  double log10MeV(void) const;
93  double log10GeV(void) const;
94  double log10TeV(void) const;
95  double log10(const std::string& unit) const;
96  void erg(const double& eng);
97  void keV(const double& eng);
98  void MeV(const double& eng);
99  void GeV(const double& eng);
100  void TeV(const double& eng);
101  void Angstrom(const double& wavelength);
102  void log10erg(const double& eng);
103  void log10keV(const double& eng);
104  void log10MeV(const double& eng);
105  void log10GeV(const double& eng);
106  void log10TeV(const double& eng);
107  void log10(const double& eng, const std::string& unit);
108  std::string print(const GChatter& chatter = NORMAL) const;
109 
110 protected:
111  // Protected methods
112  void init_members(void);
113  void copy_members(const GEnergy& eng);
114  void free_members(void);
115 
116  // Protected data members
117  double m_energy; //!< Energy in MeV
118  mutable double m_elog10; //!< log10 of energy in MeV
119  mutable bool m_has_log10; //!< log10 of energy is valid
120 };
121 
122 
123 /***********************************************************************//**
124  * @brief Return class name
125  *
126  * @return String containing the class name ("GEnergy").
127  ***************************************************************************/
128 inline
129 std::string GEnergy::classname(void) const
130 {
131  return ("GEnergy");
132 }
133 
134 
135 /***********************************************************************//**
136  * @brief Energy unary addition operator
137  *
138  * @param[in] eng Energy.
139  * @return Sum of energies.
140  ***************************************************************************/
141 inline
143 {
144  m_energy += eng.m_energy;
145  m_has_log10 = false;
146  return *this;
147 }
148 
149 
150 /***********************************************************************//**
151  * @brief Energy unary differnce operator
152  *
153  * @param[in] eng Energy.
154  * @return Difference of energies.
155  ***************************************************************************/
156 inline
158 {
159  m_energy -= eng.m_energy;
160  m_has_log10 = false;
161  return *this;
162 }
163 
164 
165 /***********************************************************************//**
166  * @brief Energy unary multiplication operator
167  *
168  * @param[in] scale Scale.
169  * @return Energy multiplied with scale.
170  ***************************************************************************/
171 inline
172 GEnergy& GEnergy::operator*=(const double& scale)
173 {
174  m_energy *= scale;
175  m_has_log10 = false;
176  return *this;
177 }
178 
179 
180 /***********************************************************************//**
181  * @brief Energy unary division operator
182  *
183  * @param[in] scale Scale.
184  * @return Energy divided by scale.
185  ***************************************************************************/
186 inline
187 GEnergy& GEnergy::operator/=(const double& scale)
188 {
189  m_energy /= scale;
190  m_has_log10 = false;
191  return *this;
192 }
193 
194 
195 /***********************************************************************//**
196  * @brief Energy addition operator friend
197  *
198  * @param[in] a First energy.
199  * @param[in] b Second energy.
200  * @return Sum of energies.
201  ***************************************************************************/
202 inline
203 GEnergy operator+(const GEnergy& a, const GEnergy& b)
204 {
205  GEnergy result;
206  result.m_energy = a.m_energy + b.m_energy;
207  return result;
208 }
209 
210 
211 /***********************************************************************//**
212  * @brief Energy subtraction operator friend
213  *
214  * @param[in] a First energy.
215  * @param[in] b Second energy.
216  * @return Difference of energies.
217  ***************************************************************************/
218 inline
219 GEnergy operator-(const GEnergy& a, const GEnergy& b)
220 {
221  GEnergy result;
222  result.m_energy = a.m_energy - b.m_energy;
223  return result;
224 }
225 
226 
227 /***********************************************************************//**
228  * @brief Energy multiplication operator friend
229  *
230  * @param[in] a Scale.
231  * @param[in] b Energy.
232  * @return Energy multiplied by scale.
233  ***************************************************************************/
234 inline
235 GEnergy operator*(const double& a, const GEnergy& b)
236 {
237  GEnergy result;
238  result.m_energy = a * b.m_energy;
239  return result;
240 }
241 
242 
243 /***********************************************************************//**
244  * @brief Energy multiplication operator friend
245  *
246  * @param[in] a Energy.
247  * @param[in] b Scale.
248  * @return Energy multiplied by scale.
249  ***************************************************************************/
250 inline
251 GEnergy operator*(const GEnergy& a, const double& b)
252 {
253  GEnergy result;
254  result.m_energy = b * a.m_energy;
255  return result;
256 }
257 
258 
259 /***********************************************************************//**
260  * @brief Energy division operator friend
261  *
262  * @param[in] a Energy.
263  * @param[in] b Scale.
264  * @return Energy divided by scale.
265  ***************************************************************************/
266 inline
267 GEnergy operator/(const GEnergy& a, const double& b)
268 {
269  GEnergy result;
270  result.m_energy = a.m_energy / b;
271  return result;
272 }
273 
274 
275 /***********************************************************************//**
276  * @brief Energy division operator friend
277  *
278  * @param[in] a Energy.
279  * @param[in] b Energy.
280  * @return Fraction of energies.
281  ***************************************************************************/
282 inline
283 double operator/(const GEnergy& a, const GEnergy& b)
284 {
285  return (a.m_energy / b.m_energy);
286 }
287 
288 
289 /***********************************************************************//**
290  * @brief Energy equality operator friend
291  *
292  * @param[in] a First energy.
293  * @param[in] b Second energy.
294  * @return True if both energies are equal.
295  ***************************************************************************/
296 inline
297 bool operator==(const GEnergy &a, const GEnergy &b)
298 {
299  return (a.m_energy == b.m_energy);
300 }
301 
302 
303 /***********************************************************************//**
304  * @brief Energy non-equality operator friend
305  *
306  * @param[in] a First energy.
307  * @param[in] b Second energy.
308  * @return True if both energies are not equal.
309  ***************************************************************************/
310 inline
311 bool operator!=(const GEnergy &a, const GEnergy &b)
312 {
313  return (a.m_energy != b.m_energy);
314 }
315 
316 
317 /***********************************************************************//**
318  * @brief Energy smaller than operator friend
319  *
320  * @param[in] a First energy.
321  * @param[in] b Second energy.
322  * @return True if first energy is smaller than second energy.
323  ***************************************************************************/
324 inline
325 bool operator<(const GEnergy &a, const GEnergy &b)
326 {
327  return (a.m_energy < b.m_energy);
328 }
329 
330 
331 /***********************************************************************//**
332  * @brief Energy smaller than or equal to operator friend
333  *
334  * @param[in] a First energy.
335  * @param[in] b Second energy.
336  * @return True if first energy is smaller than or equal to second energy.
337  ***************************************************************************/
338 inline
339 bool operator<=(const GEnergy &a, const GEnergy &b)
340 {
341  return (a.m_energy <= b.m_energy);
342 }
343 
344 
345 /***********************************************************************//**
346  * @brief Energy larger than operator friend
347  *
348  * @param[in] a First energy.
349  * @param[in] b Second energy.
350  * @return True if first energy is larger than second energy.
351  ***************************************************************************/
352 inline
353 bool operator>(const GEnergy &a, const GEnergy &b)
354 {
355  return (a.m_energy > b.m_energy);
356 }
357 
358 
359 /***********************************************************************//**
360  * @brief Energy larger than or equal to operator friend
361  *
362  * @param[in] a First energy.
363  * @param[in] b Second energy.
364  * @return True if first energy is larger than or equal to second energy.
365  ***************************************************************************/
366 inline
367 bool operator>=(const GEnergy &a, const GEnergy &b)
368 {
369  return (a.m_energy >= b.m_energy);
370 }
371 
372 #endif /* GENERGY_HPP */
GArf operator/(const GArf &arf, const double &scale)
Auxiliary Response File vision operator friend.
Definition: GArf.hpp:357
std::string classname(void) const
Return class name.
Definition: GEnergy.hpp:129
double log10erg(void) const
Return log10 of energy in erg.
Definition: GEnergy.cpp:389
friend bool operator<(const GEnergy &a, const GEnergy &b)
Energy smaller than operator friend.
Definition: GEnergy.hpp:325
friend GEnergy operator-(const GEnergy &a, const GEnergy &b)
Energy subtraction operator friend.
Definition: GEnergy.hpp:219
void operator()(const double &eng, const std::string &unit)
Unit set operator.
Definition: GEnergy.cpp:172
void init_members(void)
Initialise class members.
Definition: GEnergy.cpp:800
Definition of interface for all GammaLib classes.
double m_energy
Energy in MeV.
Definition: GEnergy.hpp:117
GEnergy & operator=(const GEnergy &eng)
Assignment operator.
Definition: GEnergy.cpp:139
double TeV(void) const
Return energy in TeV.
Definition: GEnergy.cpp:348
double log10TeV(void) const
Return log10 of energy in TeV.
Definition: GEnergy.cpp:457
double MeV(void) const
Return energy in MeV.
Definition: GEnergy.cpp:321
friend bool operator<=(const GEnergy &a, const GEnergy &b)
Energy smaller than or equal to operator friend.
Definition: GEnergy.hpp:339
void copy_members(const GEnergy &eng)
Copy class members.
Definition: GEnergy.cpp:817
friend bool operator>(const GEnergy &a, const GEnergy &b)
Energy larger than operator friend.
Definition: GEnergy.hpp:353
GEnergy * clone(void) const
Clone object.
Definition: GEnergy.cpp:279
GArf operator+(const GArf &a, const GArf &b)
Auxiliary Response File addition operator friend.
Definition: GArf.hpp:293
double log10MeV(void) const
Return log10 of energy in MeV.
Definition: GEnergy.cpp:423
bool operator<=(const GEnergy &a, const GEnergy &b)
Energy smaller than or equal to operator friend.
Definition: GEnergy.hpp:339
bool operator<(const GEnergy &a, const GEnergy &b)
Energy smaller than operator friend.
Definition: GEnergy.hpp:325
friend bool operator>=(const GEnergy &a, const GEnergy &b)
Energy larger than or equal to operator friend.
Definition: GEnergy.hpp:367
double Angstrom(void) const
Return energy as wavelength in Angstrom.
Definition: GEnergy.cpp:366
void free_members(void)
Delete class members.
Definition: GEnergy.cpp:832
std::string print(const GChatter &chatter=NORMAL) const
Print energy.
Definition: GEnergy.cpp:748
bool m_has_log10
log10 of energy is valid
Definition: GEnergy.hpp:119
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
double log10GeV(void) const
Return log10 of energy in GeV.
Definition: GEnergy.cpp:443
friend bool operator==(const GEnergy &a, const GEnergy &b)
Energy equality operator friend.
Definition: GEnergy.hpp:297
double m_elog10
log10 of energy in MeV
Definition: GEnergy.hpp:118
double erg(void) const
Return energy in erg.
Definition: GEnergy.cpp:291
double log10keV(void) const
Return log10 of energy in keV.
Definition: GEnergy.cpp:406
GChatter
Definition: GTypemaps.hpp:33
friend GEnergy operator*(const double &a, const GEnergy &b)
Energy multiplication operator friend.
Definition: GEnergy.hpp:235
GArf operator*(const GArf &arf, const double &scale)
Auxiliary Response File scaling operator friend.
Definition: GArf.hpp:325
friend GEnergy operator/(const GEnergy &a, const double &b)
Energy division operator friend.
Definition: GEnergy.hpp:267
double GeV(void) const
Return energy in GeV.
Definition: GEnergy.cpp:333
GEnergy & operator-=(const GEnergy &eng)
Energy unary differnce operator.
Definition: GEnergy.hpp:157
GEnergy & operator+=(const GEnergy &eng)
Energy unary addition operator.
Definition: GEnergy.hpp:142
GEnergy & operator*=(const double &scale)
Energy unary multiplication operator.
Definition: GEnergy.hpp:172
double log10(const std::string &unit) const
Set log10 of energy with unit specification.
Definition: GEnergy.cpp:473
double keV(void) const
Return energy in keV.
Definition: GEnergy.cpp:306
friend GEnergy operator+(const GEnergy &a, const GEnergy &b)
Energy addition operator friend.
Definition: GEnergy.hpp:203
bool operator>=(const GEnergy &a, const GEnergy &b)
Energy larger than or equal to operator friend.
Definition: GEnergy.hpp:367
friend bool operator!=(const GEnergy &a, const GEnergy &b)
Energy non-equality operator friend.
Definition: GEnergy.hpp:311
GEnergy & operator/=(const double &scale)
Energy unary division operator.
Definition: GEnergy.hpp:187
virtual ~GEnergy(void)
Destructor.
Definition: GEnergy.cpp:117
bool operator>(const GEnergy &a, const GEnergy &b)
Energy larger than operator friend.
Definition: GEnergy.hpp:353
bool operator==(const GEnergy &a, const GEnergy &b)
Energy equality operator friend.
Definition: GEnergy.hpp:297
GEnergy(void)
Void constructor.
Definition: GEnergy.cpp:64
GArf operator-(const GArf &a, const GArf &b)
Auxiliary Response File subtraction operator friend.
Definition: GArf.hpp:309
bool operator!=(const GEbounds &a, const GEbounds &b)
Energy boundaries inequality operator friend.
Definition: GEbounds.hpp:213
void clear(void)
Clear instance.
Definition: GEnergy.cpp:261
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48