CMS 3D CMS Logo

Util.h
Go to the documentation of this file.
1 #ifndef L1Trigger_TrackFindingTracklet_interface_Util_h
2 #define L1Trigger_TrackFindingTracklet_interface_Util_h
3 
4 #include <sstream>
5 #include <fstream>
6 #include <cassert>
7 #include <cmath>
8 #include <string>
9 #include <algorithm>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 
15 
16 namespace trklet {
17 
18  //Converts string in binary to hex (used in writing out memory content)
19  inline std::string hexFormat(const std::string& binary) {
20  std::stringstream ss;
21 
22  unsigned int radix = 1, value = 0;
23  for (int i = binary.length() - 1; i >= 0; i--) {
24  if (binary.at(i) != '0' && binary.at(i) != '1')
25  continue;
26  value += (binary.at(i) - '0') * radix;
27  if (radix == 8) {
28  ss << std::hex << value;
29  radix = 1;
30  value = 0;
31  } else
32  radix <<= 1;
33  }
34  if (radix != 1)
35  ss << std::hex << value;
36 
37  std::string str = ss.str() + "x0";
38  std::reverse(str.begin(), str.end());
39  return str;
40  }
41 
42  inline double bendstrip(double r, double rinv, double stripPitch) {
43  constexpr double dr = 0.18;
44  double delta = r * dr * 0.5 * rinv;
45  double bend = delta / stripPitch;
46  return bend;
47  }
48 
49  inline double rinv(double phi1, double phi2, double r1, double r2) {
50  assert(r1 < r2); //Can not form tracklet should not call function with r2<=r1
51 
52  double dphi = phi2 - phi1;
53  double dr = r2 - r1;
54 
55  return 2.0 * sin(dphi) / dr / sqrt(1.0 + 2 * r1 * r2 * (1.0 - cos(dphi)) / (dr * dr));
56  }
57 
58  inline std::string convertHexToBin(const std::string& stubwordhex) {
59  std::string stubwordbin = "";
60 
61  for (char word : stubwordhex) {
62  std::string hexword = "";
63  if (word == '0')
64  hexword = "0000";
65  else if (word == '1')
66  hexword = "0001";
67  else if (word == '2')
68  hexword = "0010";
69  else if (word == '3')
70  hexword = "0011";
71  else if (word == '4')
72  hexword = "0100";
73  else if (word == '5')
74  hexword = "0101";
75  else if (word == '6')
76  hexword = "0110";
77  else if (word == '7')
78  hexword = "0111";
79  else if (word == '8')
80  hexword = "1000";
81  else if (word == '9')
82  hexword = "1001";
83  else if (word == 'A')
84  hexword = "1010";
85  else if (word == 'B')
86  hexword = "1011";
87  else if (word == 'C')
88  hexword = "1100";
89  else if (word == 'D')
90  hexword = "1101";
91  else if (word == 'E')
92  hexword = "1110";
93  else if (word == 'F')
94  hexword = "1111";
95  else {
96  throw cms::Exception("Inconsistency")
97  << __FILE__ << " " << __LINE__ << " hex string format invalid: " << stubwordhex;
98  }
99  stubwordbin += hexword;
100  }
101  return stubwordbin;
102  }
103 
104  inline int ilog2(double factor) {
105  double power = log(factor) / log(2);
106  int ipower = round(power);
107  assert(std::abs(power - ipower) < 0.1);
108  return ipower;
109  }
110 
111  /******************************************************************************
112  * Checks to see if a directory exists. Note: This method only checks the
113  * existence of the full path AND if path leaf is a dir.
114  *
115  * @return 1 if dir exists AND is a dir,
116  * 0 if dir does not exist OR exists but not a dir,
117  * -1 if an error occurred (errno is also set)
118  *****************************************************************************/
119  inline int dirExists(const std::string& path) {
120  struct stat info;
121 
122  int statRC = stat(path.c_str(), &info);
123  if (statRC != 0) {
124  if (errno == ENOENT) {
125  return 0;
126  } // something along the path does not exist
127  if (errno == ENOTDIR) {
128  return 0;
129  } // something in path prefix is not a dir
130  return -1;
131  }
132 
133  return (info.st_mode & S_IFDIR) ? 1 : 0;
134  }
135 
136  //Open file - create directory if not existent.
137  inline std::ofstream openfile(const std::string& dir, const std::string& fname, const char* file, int line) {
138  if (dirExists(dir) != 1) {
139  edm::LogVerbatim("Tracklet") << "Creating directory : " << dir;
140  int fail = system((std::string("mkdir -p ") + dir).c_str());
141  if (fail) {
142  throw cms::Exception("BadDir") << file << " " << line << " could not create directory " << dir;
143  }
144  }
145 
146  std::ofstream out(dir + "/" + fname);
147 
148  if (out.fail()) {
149  throw cms::Exception("BadFile") << file << " " << line << " could not create file " << fname << " in " << dir;
150  }
151 
152  return out;
153  }
154 
155  //Open file - create directory if not existent.
156  //If first==true open file in create mode, if first==false open in append mode
157  inline void openfile(
158  std::ofstream& out, bool first, const std::string& dir, const std::string& fname, const char* file, int line) {
159  if (dirExists(dir) != 1) {
160  edm::LogVerbatim("Tracklet") << "Creating directory : " << dir;
161  int fail = system((std::string("mkdir -p ") + dir).c_str());
162  if (fail) {
163  throw cms::Exception("BadDir") << file << " " << line << " could not create directory " << dir;
164  }
165  }
166 
167  if (first) {
168  out.open(fname);
169  } else {
170  out.open(fname, std::ofstream::app);
171  }
172 
173  if (out.fail()) {
174  throw cms::Exception("BadFile") << file << " " << line << " could not create file " << fname << " in " << dir;
175  }
176  }
177 
178 }; // namespace trklet
179 #endif
Log< level::Info, true > LogVerbatim
static const TGPicture * info(bool iBackgroundIsBlack)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
assert(be >=bs)
std::string hexFormat(const std::string &binary)
Definition: Util.h:19
uint64_t word
int ilog2(double factor)
Definition: Util.h:104
T sqrt(T t)
Definition: SSEVec.h:19
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Definition: value.py:1
constexpr unsigned int power(unsigned int base, unsigned int exponent)
double rinv(double phi1, double phi2, double r1, double r2)
Definition: Util.h:49
int dirExists(const std::string &path)
Definition: Util.h:119
string fname
main script
#define str(s)
std::string convertHexToBin(const std::string &stubwordhex)
Definition: Util.h:58
double bendstrip(double r, double rinv, double stripPitch)
Definition: Util.h:42
std::ofstream openfile(const std::string &dir, const std::string &fname, const char *file, int line)
Definition: Util.h:137