TIMBER  beta
Tree Interface for Making Binned Events with RDataFrame
Pythonic.h
1 #ifndef _TIMBER_PYTHONIC
2 #define _TIMBER_PYTHONIC
3 #include <string>
4 #include <sstream>
5 #include <algorithm>
6 #include <iterator>
7 #include <vector>
8 #include <stdexcept>
9 #ifndef _STRUCT_TIMESPEC
10 #define _STRUCT_TIMESPEC
11 #endif
12 #include <sys/stat.h>
13 
14 namespace Pythonic {
25  template <typename IntType>
26  std::vector<IntType> Range(IntType start, IntType stop, IntType step) {
27  if (step == IntType(0)) {
28  throw std::invalid_argument("step for range must be non-zero");
29  }
30 
31  std::vector<IntType> result;
32  IntType i = start;
33  while ((step > 0) ? (i < stop) : (i > stop)) {
34  result.push_back(i);
35  i += step;
36  }
37 
38  return result;
39  }
40 
50  template <typename IntType>
51  std::vector<IntType> Range(IntType start, IntType stop) {
52  return Range(start, stop, IntType(1));
53  }
54 
63  template <typename IntType>
64  std::vector<IntType> Range(IntType stop) {
65  return Range(IntType(0), stop, IntType(1));
66  }
67 
77  std::vector<std::string> Split(const std::string& str, char delim = ' ') {
78  std::vector<std::string> out {};
79  std::stringstream ss(str);
80  std::string token;
81  while (std::getline(ss, token, delim)) {
82  out.push_back(token);
83  }
84  return out;
85  }
86 
87  // Personal
96  template<typename T>
97  bool InList(T obj, std::vector<T> list) {
98  bool out;
99  auto pos = std::find(std::begin(list), std::end(list), obj);
100  if (pos != std::end(list)){
101  out = true;
102  } else {out = false;}
103  return out;
104  }
105 
114  bool InString(std::string sub, std::string main) {
115  bool out;
116  auto found = main.find(sub);
117  if (found != std::string::npos){
118  out = true;
119  } else {out = false;}
120  return out;
121  }
122 
130  template<typename T>
131  void Extend(std::vector<T> base, std::vector<T> extension) {
132  for (int i = 0; i < extension.size(); i++) {
133  base.push_back(extension.at(i));
134  }
135  }
136 
144  bool IsDir(char* dirname) {
145  struct stat sb;
146  bool exists;
147 
148  if (stat(dirname, &sb) == 0 && S_ISDIR(sb.st_mode)) {
149  exists = true;
150  } else {
151  exists = false;
152  }
153  return exists;
154  }
160  void Execute(std::string cmd) {
161  printf("Executing: %s",cmd.c_str());
162  std::system(cmd.c_str());
163  }
164 }
165 #endif
Definition: Pythonic.h:14