00001 /*************************************************************************** 00002 * GEnergy.hpp - Energy class * 00003 * ----------------------------------------------------------------------- * 00004 * copyright (C) 2010-2014 by Juergen Knoedlseder * 00005 * ----------------------------------------------------------------------- * 00006 * * 00007 * This program is free software: you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation, either version 3 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 00019 * * 00020 ***************************************************************************/ 00021 /** 00022 * @file GEnergy.hpp 00023 * @brief Energy value class definition. 00024 * @author Juergen Knoedlseder 00025 */ 00026 00027 #ifndef GENERGY_HPP 00028 #define GENERGY_HPP 00029 00030 /* __ Includes ___________________________________________________________ */ 00031 #include <string> 00032 #include "GBase.hpp" 00033 00034 00035 /***********************************************************************//** 00036 * @class GEnergy 00037 * 00038 * @brief Class that handles energies in a unit independent way. 00039 * 00040 * The GEnergy class stores an energy value in units of MeV and implements 00041 * methods that provide automatic conversion of the energy values in other 00042 * units. This makes instrument specific implementations more robust and 00043 * reduces the risk of unit errors. The method also holds on request the 00044 * log10 of the energy in MeV, which is only recomputed when the energy 00045 * value is changed. This considerably reduces the number of log10 operations 00046 * in many applications and speeds up the performance. 00047 ***************************************************************************/ 00048 class GEnergy : public GBase { 00049 00050 // Operator friends 00051 friend GEnergy operator+(const GEnergy& a, const GEnergy& b); 00052 friend GEnergy operator-(const GEnergy& a, const GEnergy& b); 00053 friend GEnergy operator*(const double& a, const GEnergy& b); 00054 friend GEnergy operator*(const GEnergy& a, const double& b); 00055 friend GEnergy operator/(const GEnergy& a, const double& b); 00056 friend double operator/(const GEnergy& a, const GEnergy& b); 00057 friend bool operator==(const GEnergy& a, const GEnergy& b); 00058 friend bool operator!=(const GEnergy& a, const GEnergy& b); 00059 friend bool operator<(const GEnergy& a, const GEnergy& b); 00060 friend bool operator<=(const GEnergy& a, const GEnergy& b); 00061 friend bool operator>(const GEnergy& a, const GEnergy& b); 00062 friend bool operator>=(const GEnergy& a, const GEnergy& b); 00063 00064 public: 00065 // Constructors and destructors 00066 GEnergy(void); 00067 GEnergy(const GEnergy& eng); 00068 GEnergy(const double& eng, const std::string& unit); 00069 virtual ~GEnergy(void); 00070 00071 // Operators 00072 GEnergy& operator=(const GEnergy& eng); 00073 GEnergy& operator+=(const GEnergy& eng); 00074 GEnergy& operator-=(const GEnergy& eng); 00075 GEnergy& operator*=(const double& scale); 00076 GEnergy& operator/=(const double& scale); 00077 void operator()(const double& eng, const std::string& unit); 00078 double operator()(const std::string& unit) const; 00079 00080 // Methods 00081 void clear(void); 00082 GEnergy* clone(void) const; 00083 std::string classname(void) const; 00084 double erg(void) const; 00085 double keV(void) const; 00086 double MeV(void) const; 00087 double GeV(void) const; 00088 double TeV(void) const; 00089 double Angstrom(void) const; 00090 double log10erg(void) const; 00091 double log10keV(void) const; 00092 double log10MeV(void) const; 00093 double log10GeV(void) const; 00094 double log10TeV(void) const; 00095 double log10(const std::string& unit) const; 00096 void erg(const double& eng); 00097 void keV(const double& eng); 00098 void MeV(const double& eng); 00099 void GeV(const double& eng); 00100 void TeV(const double& eng); 00101 void Angstrom(const double& wavelength); 00102 void log10erg(const double& eng); 00103 void log10keV(const double& eng); 00104 void log10MeV(const double& eng); 00105 void log10GeV(const double& eng); 00106 void log10TeV(const double& eng); 00107 void log10(const double& eng, const std::string& unit); 00108 std::string print(const GChatter& chatter = NORMAL) const; 00109 00110 protected: 00111 // Protected methods 00112 void init_members(void); 00113 void copy_members(const GEnergy& eng); 00114 void free_members(void); 00115 00116 // Protected data members 00117 double m_energy; //!< Energy in MeV 00118 mutable double m_elog10; //!< log10 of energy in MeV 00119 mutable bool m_has_log10; //!< log10 of energy is valid 00120 }; 00121 00122 00123 /***********************************************************************//** 00124 * @brief Return class name 00125 * 00126 * @return String containing the class name ("GEnergy"). 00127 ***************************************************************************/ 00128 inline 00129 std::string GEnergy::classname(void) const 00130 { 00131 return ("GEnergy"); 00132 } 00133 00134 00135 /***********************************************************************//** 00136 * @brief Energy unary addition operator 00137 * 00138 * @param[in] eng Energy. 00139 * @return Sum of energies. 00140 ***************************************************************************/ 00141 inline 00142 GEnergy& GEnergy::operator+=(const GEnergy& eng) 00143 { 00144 m_energy += eng.m_energy; 00145 m_has_log10 = false; 00146 return *this; 00147 } 00148 00149 00150 /***********************************************************************//** 00151 * @brief Energy unary differnce operator 00152 * 00153 * @param[in] eng Energy. 00154 * @return Difference of energies. 00155 ***************************************************************************/ 00156 inline 00157 GEnergy& GEnergy::operator-=(const GEnergy& eng) 00158 { 00159 m_energy -= eng.m_energy; 00160 m_has_log10 = false; 00161 return *this; 00162 } 00163 00164 00165 /***********************************************************************//** 00166 * @brief Energy unary multiplication operator 00167 * 00168 * @param[in] scale Scale. 00169 * @return Energy multiplied with scale. 00170 ***************************************************************************/ 00171 inline 00172 GEnergy& GEnergy::operator*=(const double& scale) 00173 { 00174 m_energy *= scale; 00175 m_has_log10 = false; 00176 return *this; 00177 } 00178 00179 00180 /***********************************************************************//** 00181 * @brief Energy unary division operator 00182 * 00183 * @param[in] scale Scale. 00184 * @return Energy divided by scale. 00185 ***************************************************************************/ 00186 inline 00187 GEnergy& GEnergy::operator/=(const double& scale) 00188 { 00189 m_energy /= scale; 00190 m_has_log10 = false; 00191 return *this; 00192 } 00193 00194 00195 /***********************************************************************//** 00196 * @brief Energy addition operator friend 00197 * 00198 * @param[in] a First energy. 00199 * @param[in] b Second energy. 00200 * @return Sum of energies. 00201 ***************************************************************************/ 00202 inline 00203 GEnergy operator+(const GEnergy& a, const GEnergy& b) 00204 { 00205 GEnergy result; 00206 result.m_energy = a.m_energy + b.m_energy; 00207 return result; 00208 } 00209 00210 00211 /***********************************************************************//** 00212 * @brief Energy subtraction operator friend 00213 * 00214 * @param[in] a First energy. 00215 * @param[in] b Second energy. 00216 * @return Difference of energies. 00217 ***************************************************************************/ 00218 inline 00219 GEnergy operator-(const GEnergy& a, const GEnergy& b) 00220 { 00221 GEnergy result; 00222 result.m_energy = a.m_energy - b.m_energy; 00223 return result; 00224 } 00225 00226 00227 /***********************************************************************//** 00228 * @brief Energy multiplication operator friend 00229 * 00230 * @param[in] a Scale. 00231 * @param[in] b Energy. 00232 * @return Energy multiplied by scale. 00233 ***************************************************************************/ 00234 inline 00235 GEnergy operator*(const double& a, const GEnergy& b) 00236 { 00237 GEnergy result; 00238 result.m_energy = a * b.m_energy; 00239 return result; 00240 } 00241 00242 00243 /***********************************************************************//** 00244 * @brief Energy multiplication operator friend 00245 * 00246 * @param[in] a Energy. 00247 * @param[in] b Scale. 00248 * @return Energy multiplied by scale. 00249 ***************************************************************************/ 00250 inline 00251 GEnergy operator*(const GEnergy& a, const double& b) 00252 { 00253 GEnergy result; 00254 result.m_energy = b * a.m_energy; 00255 return result; 00256 } 00257 00258 00259 /***********************************************************************//** 00260 * @brief Energy division operator friend 00261 * 00262 * @param[in] a Energy. 00263 * @param[in] b Scale. 00264 * @return Energy divided by scale. 00265 ***************************************************************************/ 00266 inline 00267 GEnergy operator/(const GEnergy& a, const double& b) 00268 { 00269 GEnergy result; 00270 result.m_energy = a.m_energy / b; 00271 return result; 00272 } 00273 00274 00275 /***********************************************************************//** 00276 * @brief Energy division operator friend 00277 * 00278 * @param[in] a Energy. 00279 * @param[in] b Energy. 00280 * @return Fraction of energies. 00281 ***************************************************************************/ 00282 inline 00283 double operator/(const GEnergy& a, const GEnergy& b) 00284 { 00285 return (a.m_energy / b.m_energy); 00286 } 00287 00288 00289 /***********************************************************************//** 00290 * @brief Energy equality operator friend 00291 * 00292 * @param[in] a First energy. 00293 * @param[in] b Second energy. 00294 * @return True if both energies are equal. 00295 ***************************************************************************/ 00296 inline 00297 bool operator==(const GEnergy &a, const GEnergy &b) 00298 { 00299 return (a.m_energy == b.m_energy); 00300 } 00301 00302 00303 /***********************************************************************//** 00304 * @brief Energy non-equality operator friend 00305 * 00306 * @param[in] a First energy. 00307 * @param[in] b Second energy. 00308 * @return True if both energies are not equal. 00309 ***************************************************************************/ 00310 inline 00311 bool operator!=(const GEnergy &a, const GEnergy &b) 00312 { 00313 return (a.m_energy != b.m_energy); 00314 } 00315 00316 00317 /***********************************************************************//** 00318 * @brief Energy smaller than operator friend 00319 * 00320 * @param[in] a First energy. 00321 * @param[in] b Second energy. 00322 * @return True if first energy is smaller than second energy. 00323 ***************************************************************************/ 00324 inline 00325 bool operator<(const GEnergy &a, const GEnergy &b) 00326 { 00327 return (a.m_energy < b.m_energy); 00328 } 00329 00330 00331 /***********************************************************************//** 00332 * @brief Energy smaller than or equal to operator friend 00333 * 00334 * @param[in] a First energy. 00335 * @param[in] b Second energy. 00336 * @return True if first energy is smaller than or equal to second energy. 00337 ***************************************************************************/ 00338 inline 00339 bool operator<=(const GEnergy &a, const GEnergy &b) 00340 { 00341 return (a.m_energy <= b.m_energy); 00342 } 00343 00344 00345 /***********************************************************************//** 00346 * @brief Energy larger than operator friend 00347 * 00348 * @param[in] a First energy. 00349 * @param[in] b Second energy. 00350 * @return True if first energy is larger than second energy. 00351 ***************************************************************************/ 00352 inline 00353 bool operator>(const GEnergy &a, const GEnergy &b) 00354 { 00355 return (a.m_energy > b.m_energy); 00356 } 00357 00358 00359 /***********************************************************************//** 00360 * @brief Energy larger than or equal to operator friend 00361 * 00362 * @param[in] a First energy. 00363 * @param[in] b Second energy. 00364 * @return True if first energy is larger than or equal to second energy. 00365 ***************************************************************************/ 00366 inline 00367 bool operator>=(const GEnergy &a, const GEnergy &b) 00368 { 00369 return (a.m_energy >= b.m_energy); 00370 } 00371 00372 #endif /* GENERGY_HPP */