GammaLib 2.2.0.dev
Loading...
Searching...
No Matches
GTools.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GTools.hpp - GammaLib tools *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2008-2026 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 GTools.hpp
23 * @brief Gammalib tools definition
24 * @author Juergen Knoedlseder
25 */
26
27#ifndef GTOOLS_HPP
28#define GTOOLS_HPP
29
30/* __ Includes ___________________________________________________________ */
31#include <vector>
32#include <string>
33#include <complex>
34#include <cmath>
35#include <cfloat>
36#include <stdint.h> // uint64_t
37#include <cstdio> // snprintf, vnprintf
38
39/* __ Macros for C+98 compliance _________________________________________ */
40#if ((defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) || \
41 (defined __cplusplus && __cplusplus >= 201103L))
42#define gammalib_snprintf(O,S,F,A) std::snprintf(O,S,F,A)
43#define gammalib_vsnprintf(O,S,F,A) std::vsnprintf(O,S,F,A)
44#else
45#define gammalib_snprintf(O,S,F,A) std::sprintf(O,F,A)
46#define gammalib_vsnprintf(O,S,F,A) std::vsprintf(O,F,A)
47#endif
48
49/* __ Forward declarations _______________________________________________ */
50class GEnergy;
51class GFilename;
52class GXmlElement;
53class GXmlNode;
54
55/* __ Constants __________________________________________________________ */
56namespace gammalib {
57 const double MeV2erg = 1.6021765e-6; // MeV => erg
58 const double erg2MeV = 624150.96; // erg => MeV
59 const double MeV2Angstrom = 1.239841875e-2; // MeV => Angstrom
60 const double pc2cm = 3.08568025e18; // pc => cm
61 const double sec_in_day = 86400.0; // seconds per day
62 const double sec2day = 1.0 / sec_in_day; // seconds => days
63 const double tai2tt = 32.184; // TAI => TT time system
64 const double mec2 = 0.5109989461; // Electron rest mass (MeV)
65 const double speed_of_light = 299792458.0; // Vacuum speed of light (m/s)
66}
67
68/* __ Prototypes ________________________________________________________ */
69namespace gammalib {
70 std::string strip_whitespace(const std::string& arg);
71 std::string strip_chars(const std::string& arg,
72 const std::string& chars);
73 std::string rstrip_chars(const std::string& arg,
74 const std::string& chars);
75 std::string replace_segment(const std::string& arg,
76 const std::string& segment,
77 const std::string& replacement);
78 std::string expand_env(const std::string& arg);
79 std::string filepath(const std::string& pathname,
80 const std::string& filename);
81 std::string tmpnam(void);
82 std::string getenv(const std::string& arg);
83 std::string str(const unsigned short int& value);
84 std::string str(const unsigned int& value);
85 std::string str(const unsigned long int& value);
86 std::string str(const unsigned long long int& value);
87 std::string str(const short int& value);
88 std::string str(const int& value, const std::string& fmt = "%d");
89 std::string str(const long int& value);
90 std::string str(const long long int& value);
91 std::string str(const float& value, const int& precision = 0);
92 std::string str(const double& value, const int& precision = 0);
93 std::string str(const std::complex<double>& value,
94 const int& precision = 0);
95 std::string strdate(void);
96 short toshort(const std::string& arg);
97 unsigned short toushort(const std::string& arg);
98 int toint(const std::string& arg);
99 unsigned int touint(const std::string& arg);
100 long tolong(const std::string& arg);
101 unsigned long toulong(const std::string& arg);
102 long long tolonglong(const std::string& arg);
103 unsigned long long toulonglong(const std::string& arg);
104 float tofloat(const std::string& arg);
105 double todouble(const std::string& arg);
106 std::string toupper(const std::string& s);
107 std::string tolower(const std::string& s);
108 std::vector<std::string> split(const std::string& s, const std::string& sep);
109 std::string fill(const std::string& s, const int& n);
110 std::string left(const std::string& s, const int& n,
111 const char& c = ' ');
112 std::string right(const std::string& s, const int& n,
113 const char& c = ' ');
114 std::string centre(const std::string& s, const int& n,
115 const char& c = ' ');
116 std::string parformat(const std::string& s, const int& indent = 0);
117 std::string number(const std::string& noun, const int& number);
118 std::string order(const int& number);
119 double plaw_photon_flux(const double& emin,
120 const double& emax,
121 const double& epivot,
122 const double& gamma);
123 double plaw_energy_flux(const double& emin,
124 const double& emax,
125 const double& epivot,
126 const double& gamma);
127 GEnergy elogmean(const GEnergy& a, const GEnergy& b);
128 bool dir_exists(const std::string& dirname);
129 bool is_infinite(const double& x);
130 bool is_notanumber(const double& x);
131 bool contains(const std::string& str,
132 const std::string& substring);
133 bool contains(const std::vector<std::string>& strings,
134 const std::string& string);
135 void warning(const std::string& origin,
136 const std::string& message);
137 std::string xml2str(const std::string& arg);
138 std::string str2xml(const std::string& arg);
139 bool xml_has_par(const GXmlElement& xml,
140 const std::string& name);
141 GXmlElement* xml_need_par(const std::string& origin,
142 GXmlElement& xml,
143 const std::string& name);
144 const GXmlElement* xml_get_par(const std::string& origin,
145 const GXmlElement& xml,
146 const std::string& name);
147 std::string xml_get_attr(const std::string& origin,
148 const GXmlElement& xml,
149 const std::string& name,
150 const std::string& attribute);
151 void xml_check_parnum(const std::string& origin,
152 const GXmlElement& xml,
153 const int& number);
154 void xml_check_type(const std::string& origin,
155 GXmlElement& xml,
156 const std::string& type);
157 void xml_check_par(const std::string& origin,
158 const std::string& name,
159 const int& number);
161 const std::string& filename);
163 const std::string& filename);
164 void xml_get_name_value_pair(const GXmlNode* node,
165 std::string& name,
166 std::string& value);
168 const std::string& tag,
169 const std::string& name,
170 const std::string& value);
171 int recv(int fd, char *buffer, int len, int flags,
172 int timeout);
173 double roi_arclength(const double& rad,
174 const double& dist,
175 const double& cosdist,
176 const double& sindist,
177 const double& roi,
178 const double& cosroi);
179 bool compare(const double& a,
180 const double& b,
181 const double& tol);
182 std::string http_query(const std::string& host,
183 const std::string& query);
184 std::string host_country(const bool& force_query = false);
185 GFilename gamma_filename(const std::string& name);
186 size_t get_current_rss(void);
187 double get_current_clock(void);
188 bool little_endian(void);
189}
190
191
192/***********************************************************************//**
193 * @brief Signal if argument is infinite
194 *
195 * @param[in] x Argument.
196 * @return True if argument @p x is infinite, false otherwise.
197 *
198 * Signals if the argument @p x is infinite.
199 *
200 * This function has been copied from gnulib.
201 ***************************************************************************/
202inline
203bool gammalib::is_infinite(const double& x)
204{
205 return (x < -DBL_MAX || x > DBL_MAX);
206}
207
208
209/***********************************************************************//**
210 * @brief Signal if argument is not a number
211 *
212 * @param[in] x Argument.
213 * @return True if argument @p x is not a number, false otherwise.
214 *
215 * Signals if the argument @p x is not a number.
216 *
217 * This function is a very simple kluge. It may not work on all systems.
218 ***************************************************************************/
219inline
220bool gammalib::is_notanumber(const double& x)
221{
222 return (x != x);
223}
224
225
226/***********************************************************************//**
227 * @brief Signal if architecture stores integers as little endian
228 *
229 * @return True if architecrture stores integers as little endian, false otherwise.
230 ***************************************************************************/
231inline
233{
234 uint64_t e = 1;
235 return ((*(uint8_t *)&e) == 1);
236}
237
238#endif /* GTOOLS_HPP */
Class that handles energies in a unit independent way.
Definition GEnergy.hpp:48
Filename class.
Definition GFilename.hpp:62
XML element node class.
Abstract XML node base class.
Definition GXmlNode.hpp:57
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition GTools.cpp:1136
std::string number(const std::string &noun, const int &number)
Convert singular noun into number noun.
Definition GTools.cpp:1160
GFilename gamma_filename(const std::string &name)
Returns filename in .gamma directory.
Definition GTools.cpp:2602
bool is_infinite(const double &x)
Signal if argument is infinite.
Definition GTools.hpp:203
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:1230
std::string strdate(void)
Return current date.
Definition GTools.cpp:746
const double erg2MeV
Definition GTools.hpp:58
const double pc2cm
Definition GTools.hpp:60
std::string http_query(const std::string &host, const std::string &query)
Return response to a HTTP query.
Definition GTools.cpp:2341
bool is_notanumber(const double &x)
Signal if argument is not a number.
Definition GTools.hpp:220
GEnergy elogmean(const GEnergy &a, const GEnergy &b)
Computes log mean energy.
Definition GTools.cpp:1320
long tolong(const std::string &arg)
Convert string into long value.
Definition GTools.cpp:844
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition GTools.cpp:508
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:1740
double todouble(const std::string &arg)
Convert string into double precision value.
Definition GTools.cpp:919
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:1789
std::string order(const int &number)
Convert number into order noun.
Definition GTools.cpp:1185
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:1063
bool compare(const double &a, const double &b, const double &tol)
Compare two floating point values with tolerance.
Definition GTools.cpp:2301
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:1087
std::string replace_segment(const std::string &arg, const std::string &segment, const std::string &replacement)
Replace string segment in string.
Definition GTools.cpp:187
std::string filepath(const std::string &pathname, const std::string &filename)
Build file path from path name and file name.
Definition GTools.cpp:412
bool little_endian(void)
Signal if architecture stores integers as little endian.
Definition GTools.hpp:232
std::string tolower(const std::string &s)
Convert string to lower case.
Definition GTools.cpp:948
unsigned long long toulonglong(const std::string &arg)
Convert string into unsigned long long value.
Definition GTools.cpp:889
int toint(const std::string &arg)
Convert string into integer value.
Definition GTools.cpp:814
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:1688
std::string rstrip_chars(const std::string &arg, const std::string &chars)
Strip trailing character from string.
Definition GTools.cpp:150
std::string xml2str(const std::string &arg)
Convert XML character references in string to characters.
Definition GTools.cpp:1449
GFilename xml_file_reduce(const GXmlElement &xml, const std::string &filename)
Reduce file name provided for writing as XML attribute.
Definition GTools.cpp:1997
const double sec2day
Definition GTools.hpp:62
std::string strip_whitespace(const std::string &arg)
Strip leading and trailing whitespace from string.
Definition GTools.cpp:99
std::string str2xml(const std::string &arg)
Convert special characters in string to XML character references.
Definition GTools.cpp:1600
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:2218
float tofloat(const std::string &arg)
Convert string into single precision value.
Definition GTools.cpp:904
std::string expand_env(const std::string &arg)
Expand environment variables in string.
Definition GTools.cpp:233
unsigned long toulong(const std::string &arg)
Convert string into unsigned long value.
Definition GTools.cpp:859
const double MeV2Angstrom
Definition GTools.hpp:59
bool dir_exists(const std::string &dirname)
Checks if directory exists.
Definition GTools.cpp:1342
unsigned int touint(const std::string &arg)
Convert string into unsigned integer value.
Definition GTools.cpp:829
bool contains(const std::string &str, const std::string &substring)
Checks if a substring is in a string.
Definition GTools.cpp:1372
std::string host_country(const bool &force_query=false)
Return two-digit host country code.
Definition GTools.cpp:2444
short toshort(const std::string &arg)
Convert string into short value.
Definition GTools.cpp:784
double get_current_clock(void)
Get current clock in seconds.
Definition GTools.cpp:2686
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:1111
void xml_check_parnum(const std::string &origin, const GXmlElement &xml, const int &number)
Checks number of parameters.
Definition GTools.cpp:1828
std::string tmpnam(void)
Return temporary file name.
Definition GTools.cpp:440
const double tai2tt
Definition GTools.hpp:63
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:2039
void warning(const std::string &origin, const std::string &message)
Emits warning.
Definition GTools.cpp:1416
const GXmlElement * xml_get_element_by_attribute(const GXmlElement *element, const std::string &tag, const std::string &name, const std::string &value)
Return XML element for a given tag and name/value attribute.
Definition GTools.cpp:2088
unsigned short toushort(const std::string &arg)
Convert string into unsigned short value.
Definition GTools.cpp:799
const double speed_of_light
Definition GTools.hpp:65
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:1278
const double mec2
Definition GTools.hpp:64
std::string fill(const std::string &s, const int &n)
Fill string with n strings of same type.
Definition GTools.cpp:1037
const double sec_in_day
Definition GTools.hpp:61
std::string getenv(const std::string &arg)
Return value of environment variable.
Definition GTools.cpp:484
bool xml_has_par(const GXmlElement &xml, const std::string &name)
Checks if parameter with given name in XML element exists.
Definition GTools.cpp:1647
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:1906
std::vector< std::string > split(const std::string &s, const std::string &sep)
Split string.
Definition GTools.cpp:976
int recv(int fd, char *buffer, int len, int flags, int timeout)
Checks whether a parameter has occured once.
Definition GTools.cpp:2147
std::string toupper(const std::string &s)
Convert string to upper case.
Definition GTools.cpp:934
const double MeV2erg
Definition GTools.hpp:57
GFilename xml_file_expand(const GXmlElement &xml, const std::string &filename)
Expand file name provided as XML attribute for loading.
Definition GTools.cpp:1940
long long tolonglong(const std::string &arg)
Convert string into long long value.
Definition GTools.cpp:874
void xml_check_type(const std::string &origin, GXmlElement &xml, const std::string &type)
Checks the model type.
Definition GTools.cpp:1870
std::string strip_chars(const std::string &arg, const std::string &chars)
Strip leading and trailing character from string.
Definition GTools.cpp:113
size_t get_current_rss(void)
Get current resident set size (physical memory use) in Bytes.
Definition GTools.cpp:2639