5 #include "Rivet/Tools/RivetSTL.hh" 6 #include "Rivet/Tools/PrettyPrint.hh" 7 #include "Rivet/Tools/Exceptions.hh" 25 #if __GNUC__ && __cplusplus && RIVET_NO_DEPRECATION_WARNINGS == 0 26 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) 27 #if GCC_VERSION >= 40500 28 #if __cplusplus > 201103L 29 #define DEPRECATED(x) [[deprecated(x)]] 31 #define DEPRECATED(x) __attribute__((deprecated(x))) 34 #define DEPRECATED(x) __attribute__((deprecated)) 46 static constexpr
double DBL_NAN = std::numeric_limits<double>::quiet_NaN();
58 template<
typename T,
typename U>
66 }
catch (
const std::exception& e) {
88 inline string&
replace_first(
string& str,
const string& patt,
const string& repl) {
89 if (!
contains(str, patt))
return str;
90 str.replace(str.find(patt), patt.size(), repl);
99 inline string&
replace_all(
string& str,
const string& patt,
const string& repl) {
100 if (!
contains(str, patt))
return str;
102 string::size_type it = str.find(patt);
103 if (it == string::npos)
break;
104 str.replace(it, patt.size(), repl);
112 string::const_iterator it1 = s1.begin();
113 string::const_iterator it2 = s2.begin();
114 while ( (it1 != s1.end()) && (it2 != s2.end()) ) {
115 if(::toupper(*it1) != ::toupper(*it2)) {
117 return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1;
123 size_t size1 = s1.size(), size2 = s2.size();
125 if (size1 == size2)
return 0;
126 return (size1 < size2) ? -1 : 1;
139 std::transform(out.begin(), out.end(), out.begin(), (int(*)(int)) std::tolower);
147 std::transform(out.begin(), out.end(), out.begin(), (int(*)(int)) std::toupper);
153 inline bool startsWith(
const string& s,
const string& start) {
154 if (s.length() < start.length())
return false;
155 return s.substr(0, start.length()) == start;
160 inline bool endsWith(
const string& s,
const string& end) {
161 if (s.length() < end.length())
return false;
162 return s.substr(s.length() - end.length()) == end;
167 template <
typename T>
168 inline string join(
const vector<T>& v,
const string& sep=
" ") {
170 for (
size_t i = 0; i < v.size(); ++i) {
171 if (i != 0) rtn += sep;
179 inline string join(
const vector<string>& v,
const string& sep) {
181 for (
size_t i = 0; i < v.size(); ++i) {
182 if (i != 0) rtn += sep;
189 template <
typename T>
190 inline string join(
const set<T>& s,
const string& sep=
" ") {
192 for (
const T& x : s) {
193 if (rtn.size() > 0) rtn += sep;
201 inline string join(
const set<string>& s,
const string& sep) {
203 for (
const string & x : s) {
204 if (rtn.size() > 0) rtn += sep;
211 inline vector<string>
split(
const string& s,
const string& sep) {
215 const size_t delim_pos = tmp.find(sep);
216 if (delim_pos == string::npos)
break;
217 const string dir = tmp.substr(0, delim_pos);
218 if (dir.length()) dirs.push_back(dir);
219 tmp.replace(0, delim_pos+1,
"");
221 if (tmp.length()) dirs.push_back(tmp);
226 inline string lpad(
const string& s,
size_t width,
const string& padchar=
" ") {
227 if (s.size() >= width)
return s;
228 return string(width - s.size(), padchar[0]) + s;
232 inline string rpad(
const string& s,
size_t width,
const string& padchar=
" ") {
233 if (s.size() >= width)
return s;
234 return s + string(width - s.size(), padchar[0]);
248 return split(path,
":");
255 inline string pathjoin(
const vector<string>& paths) {
256 return join(paths,
":");
260 inline string operator / (
const string& a,
const string& b) {
262 const string anorm = (a.find(
"/") != string::npos) ? a.substr(0, a.find_last_not_of(
"/")+1) : a;
263 const string bnorm = (b.find(
"/") != string::npos) ? b.substr(b.find_first_not_of(
"/")) : b;
264 return anorm +
"/" + bnorm;
270 return p.substr(p.rfind(
"/")+1);
276 return p.substr(0, p.rfind(
"/"));
282 return f.substr(0, f.rfind(
"."));
288 return f.substr(f.rfind(
".")+1);
299 template <
typename CONTAINER>
300 inline unsigned int count(
const CONTAINER& c) {
302 unsigned int rtn = 0;
303 for (
const auto& x : c)
if (
bool(x)) rtn += 1;
314 template <
typename CONTAINER,
typename FN>
315 inline unsigned int count(
const CONTAINER& c,
const FN& f) {
316 return std::count_if(std::begin(c), std::end(c), f);
322 template <
typename CONTAINER>
323 inline bool any(
const CONTAINER& c) {
325 for (
const auto& x : c)
if (
bool(x))
return true;
336 template <
typename CONTAINER,
typename FN>
337 inline bool any(
const CONTAINER& c,
const FN& f) {
338 return std::any_of(std::begin(c), std::end(c), f);
344 template <
typename CONTAINER>
345 inline bool all(
const CONTAINER& c) {
347 for (
const auto& x : c)
if (!
bool(x))
return false;
358 template <
typename CONTAINER,
typename FN>
359 inline bool all(
const CONTAINER& c,
const FN& f) {
360 return std::all_of(std::begin(c), std::end(c), f);
366 template <
typename CONTAINER>
367 inline bool none(
const CONTAINER& c) {
369 for (
const auto& x : c)
if (
bool(x))
return false;
380 template <
typename CONTAINER,
typename FN>
381 inline bool none(
const CONTAINER& c,
const FN& f) {
382 return std::none_of(std::begin(c), std::end(c), f);
396 template <
typename CONTAINER1,
typename CONTAINER2,
typename FN>
397 inline const CONTAINER2& transform(
const CONTAINER1& in, CONTAINER2& out,
const FN& f) {
398 out.clear(); out.resize(in.size());
405 template <
typename CONTAINER1,
typename T2>
406 inline std::vector<T2> transform(
const CONTAINER1& in,
const std::function<T2(
typename CONTAINER1::value_type)>& f) {
407 std::vector<T2> out(in.size());
408 transform(in, out, f);
422 template <
typename CONTAINER1,
typename T,
typename FN>
423 inline T
accumulate(
const CONTAINER1& in,
const T& init,
const FN& f) {
433 template <
typename CONTAINER>
434 inline typename CONTAINER::value_type
sum(
const CONTAINER& c) {
435 typename CONTAINER::value_type rtn;
436 for (
const auto& x : c) rtn += x;
443 template <
typename CONTAINER,
typename T>
444 inline T
sum(
const CONTAINER& c,
const T& start) {
446 for (
const auto& x : c) rtn += x;
451 template <
typename CONTAINER,
typename FN,
typename T>
452 inline T
sum(
const CONTAINER& c,
const FN& f,
const T& start=T()) {
454 for (
const auto& x : c) rtn += f(x);
463 template <
typename CONTAINER,
typename T>
464 inline T&
isum(
const CONTAINER& c, T& out) {
465 for (
const auto& x : c) out += x;
472 template <
typename CONTAINER,
typename FN,
typename T>
473 inline T&
isum(
const CONTAINER& c,
const FN& f, T& out) {
474 for (
const auto& x : c) out += f(x);
482 template <
typename CONTAINER,
typename FN>
484 const auto newend = std::remove_if(std::begin(c), std::end(c), f);
485 c.erase(newend, c.end());
489 template <
typename CONTAINER,
typename FN>
490 inline CONTAINER&
idiscard(CONTAINER& c,
const FN& f) {
497 template <
typename CONTAINER,
typename FN>
503 template <
typename CONTAINER,
typename FN>
504 inline CONTAINER&
discard(CONTAINER& c,
const FN& f) {
511 template <
typename CONTAINER,
typename FN>
512 inline CONTAINER&
filter_discard(
const CONTAINER& c,
const FN& f, CONTAINER& out) {
517 template <
typename CONTAINER,
typename FN>
518 inline CONTAINER&
discard(CONTAINER& c,
const FN& f, CONTAINER& out) {
526 template <
typename CONTAINER,
typename FN>
529 auto invf = [&](
const typename CONTAINER::value_type& x){
return !f(x); };
533 template <
typename CONTAINER,
typename FN>
534 inline CONTAINER&
iselect(CONTAINER& c,
const FN& f) {
540 template <
typename CONTAINER,
typename FN>
546 template <
typename CONTAINER,
typename FN>
547 inline CONTAINER
select(
const CONTAINER& c,
const FN& f) {
554 template <
typename CONTAINER,
typename FN>
555 inline CONTAINER&
filter_select(
const CONTAINER& c,
const FN& f, CONTAINER& out) {
560 template <
typename CONTAINER,
typename FN>
561 inline CONTAINER&
select(CONTAINER& c,
const FN& f, CONTAINER& out) {
571 template <
typename CONTAINER>
572 inline CONTAINER
slice(
const CONTAINER& c,
int i,
int j) {
574 const size_t off1 = (i >= 0) ? i : c.size() + i;
575 const size_t off2 = (j >= 0) ? j : c.size() + j;
576 if (off1 > c.size() || off2 > c.size())
throw RangeError(
"Attempting to slice beyond requested offsets");
577 if (off2 < off1)
throw RangeError(
"Requested offsets in invalid order");
578 rtn.resize(off2 - off1);
579 std::copy(c.begin()+off1, c.begin()+off2, rtn.begin());
586 template <
typename CONTAINER>
587 inline CONTAINER
slice(
const CONTAINER& c,
int i) {
588 return slice(c, i, c.size());
594 template <
typename CONTAINER>
595 inline CONTAINER
head(
const CONTAINER& c,
int n) {
597 if (n < 0) n =
std::max(0, (
int)c.size()+n);
599 return slice(c, 0, n);
605 template <
typename CONTAINER>
606 inline CONTAINER
tail(
const CONTAINER& c,
int n) {
608 if (n < 0) n =
std::max(0, (
int)c.size()+n);
610 return slice(c, c.size()-n);
618 inline double min(
const vector<double>& in,
double errval=DBL_NAN) {
619 const auto e = std::min_element(in.begin(), in.end());
620 return e != in.end() ? *e : errval;
624 inline double max(
const vector<double>& in,
double errval=DBL_NAN) {
625 const auto e = std::max_element(in.begin(), in.end());
626 return e != in.end() ? *e : errval;
630 inline pair<double,double>
minmax(
const vector<double>& in,
double errval=DBL_NAN) {
631 const auto e = std::minmax_element(in.begin(), in.end());
632 const double rtnmin = e.first != in.end() ? *e.first : errval;
633 const double rtnmax = e.second != in.end() ? *e.first : errval;
634 return std::make_pair(rtnmin, rtnmax);
639 inline int min(
const vector<int>& in,
int errval=-1) {
640 const auto e = std::min_element(in.begin(), in.end());
641 return e != in.end() ? *e : errval;
645 inline int max(
const vector<int>& in,
int errval=-1) {
646 const auto e = std::max_element(in.begin(), in.end());
647 return e != in.end() ? *e : errval;
651 inline pair<int,int>
minmax(
const vector<int>& in,
int errval=-1) {
652 const auto e = std::minmax_element(in.begin(), in.end());
653 const double rtnmin = e.first != in.end() ? *e.first : errval;
654 const double rtnmax = e.second != in.end() ? *e.first : errval;
655 return std::make_pair(rtnmin, rtnmax);
666 template <
typename T>
668 char* env = getenv(name.c_str());
T lexical_cast(const U &in)
Convert between any types via stringstream.
Definition: Utils.hh:59
Definition: MC_Cent_pPb.hh:10
bool any(const CONTAINER &c)
Return true if x is true for any x in container c, otherwise false.
Definition: Utils.hh:323
T getEnvParam(const std::string name, const T &fallback)
Get a parameter from a named environment variable, with automatic type conversion.
Definition: Utils.hh:667
CONTAINER head(const CONTAINER &c, int n)
Head slice of the n first container elements.
Definition: Utils.hh:595
string dirname(const string &p)
Get the dirname (i.e. path to the penultimate directory) from a path p.
Definition: Utils.hh:274
unsigned int count(const CONTAINER &c)
Return number of true elements in the container c .
Definition: Utils.hh:300
Jets filter_select(const Jets &jets, const Cut &c)
Filter a jet collection in-place to the subset that passes the supplied Cut.
Definition: JetUtils.hh:160
std::enable_if< std::is_arithmetic< N1 >::value &&std::is_arithmetic< N2 >::value, typename std::common_type< N1, N2 >::type >::type min(N1 a, N2 b)
Get the minimum of two numbers.
Definition: MathUtils.hh:102
T & isum(const CONTAINER &c, T &out)
Definition: Utils.hh:464
Exception class for throwing from lexical_cast when a parse goes wrong.
Definition: Utils.hh:53
static constexpr double DBL_NAN
Convenient const for getting the double NaN value.
Definition: Utils.hh:46
string & replace_all(string &str, const string &patt, const string &repl)
Replace all instances of patt with repl.
Definition: Utils.hh:99
bool endsWith(const string &s, const string &end)
Check whether a string end is found at the end of s.
Definition: Utils.hh:160
string basename(const string &p)
Get the basename (i.e. terminal file name) from a path p.
Definition: Utils.hh:268
bool startsWith(const string &s, const string &start)
Check whether a string start is found at the start of s.
Definition: Utils.hh:153
bool none(const CONTAINER &c)
Return true if x is false for all x in container c, otherwise false.
Definition: Utils.hh:367
string & replace_first(string &str, const string &patt, const string &repl)
Replace the first instance of patt with repl.
Definition: Utils.hh:88
std::enable_if< std::is_arithmetic< N1 >::value &&std::is_arithmetic< N2 >::value, typename std::common_type< N1, N2 >::type >::type max(N1 a, N2 b)
Get the maximum of two numbers.
Definition: MathUtils.hh:111
int nocase_cmp(const string &s1, const string &s2)
Case-insensitive string comparison function.
Definition: Utils.hh:111
string lpad(const string &s, size_t width, const string &padchar=" ")
Left-pad the given string s to width width.
Definition: Utils.hh:226
string toLower(const string &s)
Convert a string to lower-case.
Definition: Utils.hh:137
CONTAINER tail(const CONTAINER &c, int n)
Tail slice of the n last container elements.
Definition: Utils.hh:606
string to_str(const T &x)
Convert any object to a string.
Definition: Utils.hh:75
pair< double, double > minmax(const vector< double > &in, double errval=DBL_NAN)
Find the minimum and maximum values in the vector.
Definition: Utils.hh:630
T accumulate(const CONTAINER1 &in, const T &init, const FN &f)
A single-container-arg version of std::accumulate, aka reduce.
Definition: Utils.hh:423
Jets filter_discard(const Jets &jets, const Cut &c)
Filter a jet collection in-place to the subset that fails the supplied Cut.
Definition: JetUtils.hh:191
string file_stem(const string &f)
Get the stem (i.e. part without a file extension) from a filename f.
Definition: Utils.hh:280
int min(const vector< int > &in, int errval=-1)
Find the minimum value in the vector.
Definition: Utils.hh:639
int max(const vector< int > &in, int errval=-1)
Find the maximum value in the vector.
Definition: Utils.hh:645
Jets discard(const Jets &jets, const Cut &c)
New alias for filter_discard.
Definition: JetUtils.hh:196
CONTAINER::value_type sum(const CONTAINER &c)
Generic sum function, adding x for all x in container c.
Definition: Utils.hh:434
Jets select(const Jets &jets, const Cut &c)
New alias for filter_select.
Definition: JetUtils.hh:168
vector< string > split(const string &s, const string &sep)
Split a string on a specified separator string.
Definition: Utils.hh:211
std::vector< T2 > transform(const CONTAINER1 &in, const std::function< T2(typename CONTAINER1::value_type)> &f)
Definition: Utils.hh:406
string rpad(const string &s, size_t width, const string &padchar=" ")
Right-pad the given string s to width width.
Definition: Utils.hh:232
string file_extn(const string &f)
Get the file extension from a filename f.
Definition: Utils.hh:286
bool contains(const std::string &s, const std::string &sub)
Does s contain sub as a substring?
Definition: RivetSTL.hh:93
string join(const vector< T > &v, const string &sep=" ")
Make a string containing the string representations of each item in v, separated by sep...
Definition: Utils.hh:168
std::string toString(const AnalysisInfo &ai)
String representation.
Jets & ifilter_select(Jets &jets, const Cut &c)
Filter a jet collection in-place to the subset that passes the supplied Cut.
CONTAINER slice(const CONTAINER &c, int i, int j)
Slice of the container elements cf. Python's [i:j] syntax.
Definition: Utils.hh:572
string toUpper(const string &s)
Convert a string to upper-case.
Definition: Utils.hh:145
Jets & iselect(Jets &jets, const Cut &c)
New alias for ifilter_select.
Definition: JetUtils.hh:156
bool all(const CONTAINER &c)
Return true if x is true for all x in container c, otherwise false.
Definition: Utils.hh:345
vector< string > pathsplit(const string &path)
Split a path string with colon delimiters.
Definition: Utils.hh:247
string pathjoin(const vector< string > &paths)
Join several filesystem paths together with the standard ':' delimiter.
Definition: Utils.hh:255
Jets & ifilter_discard(Jets &jets, const Cut &c)
Filter a jet collection in-place to the subset that fails the supplied Cut.
Jets & idiscard(Jets &jets, const Cut &c)
New alias for ifilter_discard.
Definition: JetUtils.hh:187
Error for e.g. use of invalid bin ranges.
Definition: Exceptions.hh:22
bool nocase_equals(const string &s1, const string &s2)
Case-insensitive string equality function.
Definition: Utils.hh:131