45 template<
typename T,
typename U>
53 }
catch (
const std::exception& e) {
60 inline std::string
to_str(
const T& val) {
66 inline std::string
to_str(
const std::vector<T>& vec) {
68 for (
size_t i = 0; i < vec.size(); ++i) {
70 if (i < vec.size()-1) rtn +=
", ";
79 ss << setfill(
'0') << setw(nchars) << val;
84 inline std::string
join(
const std::vector<std::string>& svec,
const std::string& sep) {
86 for (
size_t i = 0; i < svec.size(); ++i) {
88 if (i < svec.size()-1) rtn +=
", ";
94 inline std::vector<std::string>
split(
const std::string& s,
const std::string& sep) {
98 const size_t delim_pos = tmp.find(sep);
99 if (delim_pos == string::npos)
break;
100 const string s = tmp.substr(0, delim_pos);
101 if (!s.empty()) rtn.push_back(s);
102 tmp.replace(0, delim_pos+1,
"");
104 if (!tmp.empty()) rtn.push_back(tmp);
109 inline bool contains(
const std::string& s,
const std::string& sub) {
110 return s.find(sub) != string::npos;
114 inline bool startswith(
const std::string& s,
const std::string& sub) {
115 return s.find(sub) == 0;
119 inline bool endswith(
const std::string& s,
const std::string& sub) {
120 return s.find(sub) == s.length()-sub.length();
124 inline size_t countchar(
const std::string& s,
const char c) {
125 return std::count(s.begin(), s.end(), c);
129 inline std::string
trim(
const std::string& s) {
130 const size_t firstnonspacepos = s.find_first_not_of(
" ");
131 const size_t lastnonspacepos = s.find_last_not_of(
" ");
132 if (firstnonspacepos == std::string::npos)
return "";
133 return s.substr(firstnonspacepos, lastnonspacepos-firstnonspacepos+1);
137 inline std::string
to_lower(
const std::string& s) {
139 transform(rtn.begin(), rtn.end(), rtn.begin(), (int(*)(int)) tolower);
144 inline std::string
to_upper(
const std::string& s) {
146 transform(rtn.begin(), rtn.end(), rtn.begin(), (int(*)(int)) toupper);
159 return (stat(p.c_str(), &st) == 0);
165 return (stat(p.c_str(), &st) == 0 && S_ISREG(st.st_mode));
171 return (stat(p.c_str(), &st) == 0 && S_ISDIR(st.st_mode));
175 inline std::string
operator / (
const std::string& a,
const std::string& b) {
177 const string anorm = (a.find(
"/") != std::string::npos) ? a.substr(0, a.find_last_not_of(
"/")+1) : a;
178 const string bnorm = (b.find(
"/") != std::string::npos) ? b.substr(b.find_first_not_of(
"/")) : b;
179 return anorm +
"/" + bnorm;
183 inline std::string
basename(
const std::string& p) {
185 return p.substr(p.rfind(
"/")+1);
189 inline std::string
dirname(
const std::string& p) {
191 return p.substr(0, p.rfind(
"/"));
197 return f.substr(0, f.rfind(
"."));
203 return f.substr(f.rfind(
".")+1);
215 template <
typename N>
216 inline N
sqr(
const N& x) {
return x*x; }
219 template <
typename N>
220 inline int sgn(N val) {
return (N(0) < val) - (val < N(0)); }
223 inline int in_range(
double x,
double low,
double high) {
return x >= low && x < high; }
226 inline int in_closed_range(
double x,
double low,
double high) {
return x >= low && x <= high; }
229 inline int in_open_range(
double x,
double low,
double high) {
return x > low && x < high; }
246 template <
typename T>
247 inline bool contains(
const std::vector<T>& container,
const T& item) {
248 return find(container.begin(), container.end(), item) != container.end();
258 template <
typename K,
typename T>
259 inline bool has_key(
const std::map<K,T>& container,
const K& key) {
260 return container.find(key) != container.end();
int in_open_range(double x, double low, double high)
Check if a number is in a range (open-open)
Definition: Utils.h:229
std::string to_str(const T &val)
Make a string representation of val.
Definition: Utils.h:60
size_t countchar(const std::string &s, const char c)
How many times does a string s contain the character c?
Definition: Utils.h:124
int sgn(N val)
Get the sign of a number.
Definition: Utils.h:220
std::string to_str_zeropad(int val, size_t nchars=4)
Format an integer val as a zero-padded string of length nchars.
Definition: Utils.h:77
std::vector< std::string > split(const std::string &s, const std::string &sep)
Split a string by a given separator.
Definition: Utils.h:94
bool startswith(const std::string &s, const std::string &sub)
Does a string s start with the sub substring?
Definition: Utils.h:114
T lexical_cast(const U &in)
Convert between any types via stringstream.
Definition: Utils.h:46
std::string basename(const std::string &p)
Get the basename (i.e. terminal file name) from a path p.
Definition: Utils.h:183
bool file_exists(const std::string &p)
Check if a file p exists.
Definition: Utils.h:163
std::string join(const std::vector< std::string > &svec, const std::string &sep)
Concatenate strings with separator strings between each element.
Definition: Utils.h:84
int in_closed_range(double x, double low, double high)
Check if a number is in a range (closed-closed)
Definition: Utils.h:226
When lexical_cast goes bad.
Definition: Utils.h:40
N sqr(const N &x)
Convenience function for squaring (of any type)
Definition: Utils.h:216
bool endswith(const std::string &s, const std::string &sub)
Does a string s end with the sub substring?
Definition: Utils.h:119
std::string to_lower(const std::string &s)
Convert a string to lower-case (not in-place)
Definition: Utils.h:137
std::string dirname(const std::string &p)
Get the dirname (i.e. path to the penultimate directory) from a path p.
Definition: Utils.h:189
double chisquared_quantile(double p, double ndf)
Quantiles of the chi-squared probability distribution function.
Namespace for all LHAPDF functions and classes.
Definition: AlphaS.h:14
int in_range(double x, double low, double high)
Check if a number is in a range (closed-open)
Definition: Utils.h:223
bool contains(const std::string &s, const std::string &sub)
Does a string s contain the sub substring?
Definition: Utils.h:109
std::string file_stem(const std::string &f)
Get the stem (i.e. part without a file extension) from a filename f.
Definition: Utils.h:195
bool dir_exists(const std::string &p)
Check if a dir p exists.
Definition: Utils.h:169
bool has_key(const std::map< K, T > &container, const K &key)
Does the map<K,T> container have a key K key?
Definition: Utils.h:259
std::string to_upper(const std::string &s)
Convert a string to upper-case (not in-place)
Definition: Utils.h:144
std::string trim(const std::string &s)
Strip leading and trailing spaces (not in-place)
Definition: Utils.h:129
std::string operator/(const std::string &a, const std::string &b)
Operator for joining strings a and b with filesystem separators.
Definition: Utils.h:175
double norm_quantile(double p)
Quantiles of the standard normal probability distribution function.
std::string file_extn(const std::string &f)
Get the file extension from a filename f.
Definition: Utils.h:201
bool path_exists(const std::string &p)
Check if a path p (either file or dir) exists.
Definition: Utils.h:157