GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GEnergy.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GEnergy.hpp - Energy class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2010-2024 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 ***************************************************************************/
48class 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
64public:
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 eV(void) const;
86 double keV(void) const;
87 double MeV(void) const;
88 double GeV(void) const;
89 double TeV(void) const;
90 double Angstrom(void) const;
91 double log10erg(void) const;
92 double log10eV(void) const;
93 double log10keV(void) const;
94 double log10MeV(void) const;
95 double log10GeV(void) const;
96 double log10TeV(void) const;
97 double log10(const std::string& unit) const;
98 void erg(const double& eng);
99 void eV(const double& eng);
100 void keV(const double& eng);
101 void MeV(const double& eng);
102 void GeV(const double& eng);
103 void TeV(const double& eng);
104 void Angstrom(const double& wavelength);
105 void log10erg(const double& eng);
106 void log10eV(const double& eng);
107 void log10keV(const double& eng);
108 void log10MeV(const double& eng);
109 void log10GeV(const double& eng);
110 void log10TeV(const double& eng);
111 void log10(const double& eng, const std::string& unit);
112 std::string print(const GChatter& chatter = NORMAL) const;
113
114protected:
115 // Protected methods
116 void init_members(void);
117 void copy_members(const GEnergy& eng);
118 void free_members(void);
119
120 // Protected data members
121 double m_energy; //!< Energy in MeV
122 mutable double m_elog10; //!< log10 of energy in MeV
123 mutable bool m_has_log10; //!< log10 of energy is valid
124};
125
126
127/***********************************************************************//**
128 * @brief Return class name
129 *
130 * @return String containing the class name ("GEnergy").
131 ***************************************************************************/
132inline
133std::string GEnergy::classname(void) const
134{
135 return ("GEnergy");
136}
137
138
139/***********************************************************************//**
140 * @brief Energy unary addition operator
141 *
142 * @param[in] eng Energy.
143 * @return Sum of energies.
144 ***************************************************************************/
145inline
147{
148 m_energy += eng.m_energy;
149 m_has_log10 = false;
150 return *this;
151}
152
153
154/***********************************************************************//**
155 * @brief Energy unary differnce operator
156 *
157 * @param[in] eng Energy.
158 * @return Difference of energies.
159 ***************************************************************************/
160inline
162{
163 m_energy -= eng.m_energy;
164 m_has_log10 = false;
165 return *this;
166}
167
168
169/***********************************************************************//**
170 * @brief Energy unary multiplication operator
171 *
172 * @param[in] scale Scale.
173 * @return Energy multiplied with scale.
174 ***************************************************************************/
175inline
176GEnergy& GEnergy::operator*=(const double& scale)
177{
178 m_energy *= scale;
179 m_has_log10 = false;
180 return *this;
181}
182
183
184/***********************************************************************//**
185 * @brief Energy unary division operator
186 *
187 * @param[in] scale Scale.
188 * @return Energy divided by scale.
189 ***************************************************************************/
190inline
191GEnergy& GEnergy::operator/=(const double& scale)
192{
193 m_energy /= scale;
194 m_has_log10 = false;
195 return *this;
196}
197
198
199/***********************************************************************//**
200 * @brief Energy addition operator friend
201 *
202 * @param[in] a First energy.
203 * @param[in] b Second energy.
204 * @return Sum of energies.
205 ***************************************************************************/
206inline
207GEnergy operator+(const GEnergy& a, const GEnergy& b)
208{
209 GEnergy result;
210 result.m_energy = a.m_energy + b.m_energy;
211 return result;
212}
213
214
215/***********************************************************************//**
216 * @brief Energy subtraction operator friend
217 *
218 * @param[in] a First energy.
219 * @param[in] b Second energy.
220 * @return Difference of energies.
221 ***************************************************************************/
222inline
223GEnergy operator-(const GEnergy& a, const GEnergy& b)
224{
225 GEnergy result;
226 result.m_energy = a.m_energy - b.m_energy;
227 return result;
228}
229
230
231/***********************************************************************//**
232 * @brief Energy multiplication operator friend
233 *
234 * @param[in] a Scale.
235 * @param[in] b Energy.
236 * @return Energy multiplied by scale.
237 ***************************************************************************/
238inline
239GEnergy operator*(const double& a, const GEnergy& b)
240{
241 GEnergy result;
242 result.m_energy = a * b.m_energy;
243 return result;
244}
245
246
247/***********************************************************************//**
248 * @brief Energy multiplication operator friend
249 *
250 * @param[in] a Energy.
251 * @param[in] b Scale.
252 * @return Energy multiplied by scale.
253 ***************************************************************************/
254inline
255GEnergy operator*(const GEnergy& a, const double& b)
256{
257 GEnergy result;
258 result.m_energy = b * a.m_energy;
259 return result;
260}
261
262
263/***********************************************************************//**
264 * @brief Energy division operator friend
265 *
266 * @param[in] a Energy.
267 * @param[in] b Scale.
268 * @return Energy divided by scale.
269 ***************************************************************************/
270inline
271GEnergy operator/(const GEnergy& a, const double& b)
272{
273 GEnergy result;
274 result.m_energy = a.m_energy / b;
275 return result;
276}
277
278
279/***********************************************************************//**
280 * @brief Energy division operator friend
281 *
282 * @param[in] a Energy.
283 * @param[in] b Energy.
284 * @return Fraction of energies.
285 ***************************************************************************/
286inline
287double operator/(const GEnergy& a, const GEnergy& b)
288{
289 return (a.m_energy / b.m_energy);
290}
291
292
293/***********************************************************************//**
294 * @brief Energy equality operator friend
295 *
296 * @param[in] a First energy.
297 * @param[in] b Second energy.
298 * @return True if both energies are equal.
299 ***************************************************************************/
300inline
301bool operator==(const GEnergy &a, const GEnergy &b)
302{
303 return (a.m_energy == b.m_energy);
304}
305
306
307/***********************************************************************//**
308 * @brief Energy non-equality operator friend
309 *
310 * @param[in] a First energy.
311 * @param[in] b Second energy.
312 * @return True if both energies are not equal.
313 ***************************************************************************/
314inline
315bool operator!=(const GEnergy &a, const GEnergy &b)
316{
317 return (a.m_energy != b.m_energy);
318}
319
320
321/***********************************************************************//**
322 * @brief Energy smaller than operator friend
323 *
324 * @param[in] a First energy.
325 * @param[in] b Second energy.
326 * @return True if first energy is smaller than second energy.
327 ***************************************************************************/
328inline
329bool operator<(const GEnergy &a, const GEnergy &b)
330{
331 return (a.m_energy < b.m_energy);
332}
333
334
335/***********************************************************************//**
336 * @brief Energy smaller than or equal to operator friend
337 *
338 * @param[in] a First energy.
339 * @param[in] b Second energy.
340 * @return True if first energy is smaller than or equal to second energy.
341 ***************************************************************************/
342inline
343bool operator<=(const GEnergy &a, const GEnergy &b)
344{
345 return (a.m_energy <= b.m_energy);
346}
347
348
349/***********************************************************************//**
350 * @brief Energy larger than operator friend
351 *
352 * @param[in] a First energy.
353 * @param[in] b Second energy.
354 * @return True if first energy is larger than second energy.
355 ***************************************************************************/
356inline
357bool operator>(const GEnergy &a, const GEnergy &b)
358{
359 return (a.m_energy > b.m_energy);
360}
361
362
363/***********************************************************************//**
364 * @brief Energy larger than or equal to operator friend
365 *
366 * @param[in] a First energy.
367 * @param[in] b Second energy.
368 * @return True if first energy is larger than or equal to second energy.
369 ***************************************************************************/
370inline
371bool operator>=(const GEnergy &a, const GEnergy &b)
372{
373 return (a.m_energy >= b.m_energy);
374}
375
376#endif /* GENERGY_HPP */
Definition of interface for all GammaLib classes.
bool operator>(const GEnergy &a, const GEnergy &b)
Energy larger than operator friend.
Definition GEnergy.hpp:357
bool operator<=(const GEnergy &a, const GEnergy &b)
Energy smaller than or equal to operator friend.
Definition GEnergy.hpp:343
GEnergy operator+(const GEnergy &a, const GEnergy &b)
Energy addition operator friend.
Definition GEnergy.hpp:207
GEnergy operator*(const double &a, const GEnergy &b)
Energy multiplication operator friend.
Definition GEnergy.hpp:239
bool operator!=(const GEnergy &a, const GEnergy &b)
Energy non-equality operator friend.
Definition GEnergy.hpp:315
GEnergy operator/(const GEnergy &a, const double &b)
Energy division operator friend.
Definition GEnergy.hpp:271
bool operator<(const GEnergy &a, const GEnergy &b)
Energy smaller than operator friend.
Definition GEnergy.hpp:329
bool operator>=(const GEnergy &a, const GEnergy &b)
Energy larger than or equal to operator friend.
Definition GEnergy.hpp:371
bool operator==(const GEnergy &a, const GEnergy &b)
Energy equality operator friend.
Definition GEnergy.hpp:301
GEnergy operator-(const GEnergy &a, const GEnergy &b)
Energy subtraction operator friend.
Definition GEnergy.hpp:223
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
Interface class for all GammaLib classes.
Definition GBase.hpp:52
Class that handles energies in a unit independent way.
Definition GEnergy.hpp:48
friend bool operator>(const GEnergy &a, const GEnergy &b)
Energy larger than operator friend.
Definition GEnergy.hpp:357
double log10(const std::string &unit) const
Set log10 of energy with unit specification.
Definition GEnergy.cpp:508
double GeV(void) const
Return energy in GeV.
Definition GEnergy.cpp:354
double Angstrom(void) const
Return energy as wavelength in Angstrom.
Definition GEnergy.cpp:387
double m_energy
Energy in MeV.
Definition GEnergy.hpp:121
double keV(void) const
Return energy in keV.
Definition GEnergy.cpp:327
void operator()(const double &eng, const std::string &unit)
Unit set operator.
Definition GEnergy.cpp:172
friend bool operator<=(const GEnergy &a, const GEnergy &b)
Energy smaller than or equal to operator friend.
Definition GEnergy.hpp:343
friend GEnergy operator+(const GEnergy &a, const GEnergy &b)
Energy addition operator friend.
Definition GEnergy.hpp:207
GEnergy & operator/=(const double &scale)
Energy unary division operator.
Definition GEnergy.hpp:191
friend GEnergy operator*(const double &a, const GEnergy &b)
Energy multiplication operator friend.
Definition GEnergy.hpp:239
double log10TeV(void) const
Return log10 of energy in TeV.
Definition GEnergy.cpp:492
double log10GeV(void) const
Return log10 of energy in GeV.
Definition GEnergy.cpp:478
double MeV(void) const
Return energy in MeV.
Definition GEnergy.cpp:342
double log10MeV(void) const
Return log10 of energy in MeV.
Definition GEnergy.cpp:458
void free_members(void)
Delete class members.
Definition GEnergy.cpp:909
GEnergy & operator+=(const GEnergy &eng)
Energy unary addition operator.
Definition GEnergy.hpp:146
GEnergy(void)
Void constructor.
Definition GEnergy.cpp:64
friend bool operator!=(const GEnergy &a, const GEnergy &b)
Energy non-equality operator friend.
Definition GEnergy.hpp:315
void copy_members(const GEnergy &eng)
Copy class members.
Definition GEnergy.cpp:894
std::string print(const GChatter &chatter=NORMAL) const
Print energy.
Definition GEnergy.cpp:822
friend GEnergy operator/(const GEnergy &a, const double &b)
Energy division operator friend.
Definition GEnergy.hpp:271
double log10keV(void) const
Return log10 of energy in keV.
Definition GEnergy.cpp:441
GEnergy & operator=(const GEnergy &eng)
Assignment operator.
Definition GEnergy.cpp:139
GEnergy & operator*=(const double &scale)
Energy unary multiplication operator.
Definition GEnergy.hpp:176
void clear(void)
Clear instance.
Definition GEnergy.cpp:267
double TeV(void) const
Return energy in TeV.
Definition GEnergy.cpp:369
std::string classname(void) const
Return class name.
Definition GEnergy.hpp:133
friend bool operator<(const GEnergy &a, const GEnergy &b)
Energy smaller than operator friend.
Definition GEnergy.hpp:329
double erg(void) const
Return energy in erg.
Definition GEnergy.cpp:297
friend bool operator>=(const GEnergy &a, const GEnergy &b)
Energy larger than or equal to operator friend.
Definition GEnergy.hpp:371
double log10erg(void) const
Return log10 of energy in erg.
Definition GEnergy.cpp:410
friend bool operator==(const GEnergy &a, const GEnergy &b)
Energy equality operator friend.
Definition GEnergy.hpp:301
void init_members(void)
Initialise class members.
Definition GEnergy.cpp:877
GEnergy & operator-=(const GEnergy &eng)
Energy unary differnce operator.
Definition GEnergy.hpp:161
virtual ~GEnergy(void)
Destructor.
Definition GEnergy.cpp:117
double log10eV(void) const
Return log10 of energy in eV.
Definition GEnergy.cpp:427
bool m_has_log10
log10 of energy is valid
Definition GEnergy.hpp:123
GEnergy * clone(void) const
Clone object.
Definition GEnergy.cpp:285
friend GEnergy operator-(const GEnergy &a, const GEnergy &b)
Energy subtraction operator friend.
Definition GEnergy.hpp:223
double m_elog10
log10 of energy in MeV
Definition GEnergy.hpp:122
double eV(void) const
Return energy in eV.
Definition GEnergy.cpp:312