All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Go to the documentation of this file.
1 /***************************************************************************
2  * GTools.hpp - GammaLib tools *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2008-2020 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 *
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 <>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GTools.hpp
23  * @brief Gammalib tools definition
24  * @author Juergen Knoedlseder
25  */
27 #ifndef GTOOLS_HPP
28 #define GTOOLS_HPP
30 /* __ Includes ___________________________________________________________ */
31 #include <vector>
32 #include <string>
33 #include <complex>
34 #include <cmath>
35 #include <cfloat>
37 /* __ Forward declarations _______________________________________________ */
38 class GEnergy;
39 class GFilename;
40 class GXmlElement;
41 class GXmlNode;
43 /* __ Constants __________________________________________________________ */
44 namespace gammalib {
45  const double MeV2erg = 1.6021765e-6; // MeV => erg
46  const double erg2MeV = 624150.96; // erg => MeV
47  const double MeV2Angstrom = 1.239841875e-2; // MeV => Angstrom
48  const double pc2cm = 3.08568025e18; // pc => cm
49  const double sec_in_day = 86400.0; // seconds per day
50  const double sec2day = 1.0 / sec_in_day; // seconds => days
51  const double tai2tt = 32.184; // TAI => TT time system
52  const double mec2 = 0.5109989461; // Electron rest mass (MeV)
53 }
55 /* __ Prototypes ________________________________________________________ */
56 namespace gammalib {
57  std::string strip_whitespace(const std::string& arg);
58  std::string strip_chars(const std::string& arg,
59  const std::string& chars);
60  std::string rstrip_chars(const std::string& arg,
61  const std::string& chars);
62  std::string replace_segment(const std::string& arg,
63  const std::string& segment,
64  const std::string& replacement);
65  std::string expand_env(const std::string& arg);
66  std::string filepath(const std::string& pathname,
67  const std::string& filename);
68  std::string str(const unsigned short int& value);
69  std::string str(const unsigned int& value);
70  std::string str(const unsigned long int& value);
71  std::string str(const unsigned long long int& value);
72  std::string str(const short int& value);
73  std::string str(const int& value, const std::string& fmt = "%d");
74  std::string str(const long int& value);
75  std::string str(const long long int& value);
76  std::string str(const float& value, const int& precision = 0);
77  std::string str(const double& value, const int& precision = 0);
78  std::string str(const std::complex<double>& value,
79  const int& precision = 0);
80  char* tochar(const std::string& arg);
81  short toshort(const std::string& arg);
82  unsigned short toushort(const std::string& arg);
83  int toint(const std::string& arg);
84  unsigned int touint(const std::string& arg);
85  long tolong(const std::string& arg);
86  unsigned long toulong(const std::string& arg);
87  long long tolonglong(const std::string& arg);
88  unsigned long long toulonglong(const std::string& arg);
89  float tofloat(const std::string& arg);
90  double todouble(const std::string& arg);
91  std::string toupper(const std::string& s);
92  std::string tolower(const std::string& s);
93  std::vector<std::string> split(const std::string& s, const std::string& sep);
94  std::string fill(const std::string& s, const int& n);
95  std::string left(const std::string& s, const int& n,
96  const char& c = ' ');
97  std::string right(const std::string& s, const int& n,
98  const char& c = ' ');
99  std::string centre(const std::string& s, const int& n,
100  const char& c = ' ');
101  std::string parformat(const std::string& s, const int& indent = 0);
102  std::string number(const std::string& noun, const int& number);
103  double plaw_photon_flux(const double& emin,
104  const double& emax,
105  const double& epivot,
106  const double& gamma);
107  double plaw_energy_flux(const double& emin,
108  const double& emax,
109  const double& epivot,
110  const double& gamma);
111  GEnergy elogmean(const GEnergy& a, const GEnergy& b);
112  bool dir_exists(const std::string& dirname);
113  bool is_infinite(const double& x);
114  bool is_notanumber(const double& x);
115  bool contains(const std::string& str,
116  const std::string& substring);
117  bool contains(const std::vector<std::string>& strings,
118  const std::string& string);
119  void warning(const std::string& origin,
120  const std::string& message);
121  std::string xml2str(const std::string& arg);
122  std::string str2xml(const std::string& arg);
123  bool xml_has_par(const GXmlElement& xml,
124  const std::string& name);
125  GXmlElement* xml_need_par(const std::string& origin,
126  GXmlElement& xml,
127  const std::string& name);
128  const GXmlElement* xml_get_par(const std::string& origin,
129  const GXmlElement& xml,
130  const std::string& name);
131  std::string xml_get_attr(const std::string& origin,
132  const GXmlElement& xml,
133  const std::string& name,
134  const std::string& attribute);
135  void xml_check_par(const std::string& origin,
136  const std::string& name,
137  const int& number);
139  const std::string& filename);
141  const std::string& filename);
142  void xml_get_name_value_pair(const GXmlNode* node,
143  std::string& name,
144  std::string& value);
145  int recv(int fd, char *buffer, int len, int flags,
146  int timeout);
147  double roi_arclength(const double& rad,
148  const double& dist,
149  const double& cosdist,
150  const double& sindist,
151  const double& roi,
152  const double& cosroi);
153 }
156 /***********************************************************************//**
157  * @brief Signal if argument is infinite
158  *
159  * @param[in] x Argument.
160  * @return True if argument @p x is infinite, false otherwise.
161  *
162  * Signals if the argument @p x is infinite.
163  *
164  * This function has been copied from gnulib.
165  ***************************************************************************/
166 inline
167 bool gammalib::is_infinite(const double& x)
168 {
169  return (x < -DBL_MAX || x > DBL_MAX);
170 }
173 /***********************************************************************//**
174  * @brief Signal if argument is not a number
175  *
176  * @param[in] x Argument.
177  * @return True if argument @p x is not a number, false otherwise.
178  *
179  * Signals if the argument @p x is not a number.
180  *
181  * This function is a very simple kluge. It may not work on all systems.
182  ***************************************************************************/
183 inline
184 bool gammalib::is_notanumber(const double& x)
185 {
186  return (x != x);
187 }
189 #endif /* GTOOLS_HPP */
const double MeV2Angstrom
Definition: GTools.hpp:47
Abstract XML node base class.
Definition: GXmlNode.hpp:57
const double mec2
Definition: GTools.hpp:52
bool contains(const std::string &str, const std::string &substring)
Checks if a substring is in a string.
Definition: GTools.cpp:1232
void xml_get_name_value_pair(const GXmlNode *node, std::string &name, std::string &value)
Extract name / value pair from XML node.
Definition: GTools.cpp:1761
const double erg2MeV
Definition: GTools.hpp:46
std::string replace_segment(const std::string &arg, const std::string &segment, const std::string &replacement)
Replace string segment in string.
Definition: GTools.cpp:163
double roi_arclength(const double &rad, const double &dist, const double &cosdist, const double &sindist, const double &roi, const double &cosroi)
Returns length of circular arc within circular ROI.
Definition: GTools.cpp:1880
std::string number(const std::string &noun, const int &number)
Convert singular noun into number noun.
Definition: GTools.cpp:1057
void xml_check_par(const std::string &origin, const std::string &name, const int &number)
Checks whether a parameter has occured once.
Definition: GTools.cpp:1669
void warning(const std::string &origin, const std::string &message)
Emits warning.
Definition: GTools.cpp:1276
XML element node class.
Definition: GXmlElement.hpp:47
int recv(int fd, char *buffer, int len, int flags, int timeout)
Checks whether a parameter has occured once.
Definition: GTools.cpp:1809
const double sec2day
Definition: GTools.hpp:50
std::string strip_chars(const std::string &arg, const std::string &chars)
Strip leading and trailing character from string.
Definition: GTools.cpp:89
std::vector< std::string > split(const std::string &s, const std::string &sep)
Split string.
Definition: GTools.cpp:873
long tolong(const std::string &arg)
Convert string into long value.
Definition: GTools.cpp:741
std::string strip_whitespace(const std::string &arg)
Strip leading and trailing whitespace from string.
Definition: GTools.cpp:75
GFilename xml_file_reduce(const GXmlElement &xml, const std::string &filename)
Reduce file name provided for writing as XML attribute.
Definition: GTools.cpp:1733
double plaw_energy_flux(const double &emin, const double &emax, const double &epivot, const double &gamma)
Compute energy flux between two energies for a power law.
Definition: GTools.cpp:1138
std::string centre(const std::string &s, const int &n, const char &c= ' ')
Centre string to achieve a length of n characters.
Definition: GTools.cpp:1008
bool is_notanumber(const double &x)
Signal if argument is not a number.
Definition: GTools.hpp:184
bool is_infinite(const double &x)
Signal if argument is infinite.
Definition: GTools.hpp:167
std::string right(const std::string &s, const int &n, const char &c= ' ')
Right justify string to achieve a length of n characters.
Definition: GTools.cpp:984
const double sec_in_day
Definition: GTools.hpp:49
short toshort(const std::string &arg)
Convert string into short value.
Definition: GTools.cpp:681
char * tochar(const std::string &arg)
Convert string to C string.
Definition: GTools.cpp:657
Filename class.
Definition: GFilename.hpp:62
const double tai2tt
Definition: GTools.hpp:51
std::string left(const std::string &s, const int &n, const char &c= ' ')
Left justify string to achieve a length of n characters.
Definition: GTools.cpp:960
GXmlElement * xml_need_par(const std::string &origin, GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition: GTools.cpp:1527
bool dir_exists(const std::string &dirname)
Checks if directory exists.
Definition: GTools.cpp:1202
unsigned long long toulonglong(const std::string &arg)
Convert string into unsigned long long value.
Definition: GTools.cpp:786
bool xml_has_par(const GXmlElement &xml, const std::string &name)
Checks if parameter with given name in XML element exists.
Definition: GTools.cpp:1486
std::string xml2str(const std::string &arg)
Convert XML character references in string to characters.
Definition: GTools.cpp:1306
long long tolonglong(const std::string &arg)
Convert string into long long value.
Definition: GTools.cpp:771
unsigned long toulong(const std::string &arg)
Convert string into unsigned long value.
Definition: GTools.cpp:756
std::string rstrip_chars(const std::string &arg, const std::string &chars)
Strip trailing character from string.
Definition: GTools.cpp:126
const double pc2cm
Definition: GTools.hpp:48
const double MeV2erg
Definition: GTools.hpp:45
unsigned int touint(const std::string &arg)
Convert string into unsigned integer value.
Definition: GTools.cpp:726
unsigned short toushort(const std::string &arg)
Convert string into unsigned short value.
Definition: GTools.cpp:696
std::string toupper(const std::string &s)
Convert string to upper case.
Definition: GTools.cpp:831
std::string tolower(const std::string &s)
Convert string to lower case.
Definition: GTools.cpp:845
std::string xml_get_attr(const std::string &origin, const GXmlElement &xml, const std::string &name, const std::string &attribute)
Return attribute value for a given parameter in XML element.
Definition: GTools.cpp:1628
std::string expand_env(const std::string &arg)
Expand environment variables in string.
Definition: GTools.cpp:209
int toint(const std::string &arg)
Convert string into integer value.
Definition: GTools.cpp:711
std::string filepath(const std::string &pathname, const std::string &filename)
Build file path from path name and file name.
Definition: GTools.cpp:388
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1033
std::string str2xml(const std::string &arg)
Convert special characters in string to XML character references.
Definition: GTools.cpp:1439
std::string fill(const std::string &s, const int &n)
Fill string with n strings of same type.
Definition: GTools.cpp:934
const GXmlElement * xml_get_par(const std::string &origin, const GXmlElement &xml, const std::string &name)
Return pointer to parameter with given name in XML element.
Definition: GTools.cpp:1579
GEnergy elogmean(const GEnergy &a, const GEnergy &b)
Computes log mean energy.
Definition: GTools.cpp:1180
double todouble(const std::string &arg)
Convert string into double precision value.
Definition: GTools.cpp:816
Class that handles energies in a unit independent way.
Definition: GEnergy.hpp:48
double plaw_photon_flux(const double &emin, const double &emax, const double &epivot, const double &gamma)
Compute photon flux between two energies for a power law.
Definition: GTools.cpp:1090
float tofloat(const std::string &arg)
Convert string into single precision value.
Definition: GTools.cpp:801
GFilename xml_file_expand(const GXmlElement &xml, const std::string &filename)
Expand file name provided as XML attribute for loading.
Definition: GTools.cpp:1703
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:415