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  if (r2 <= r1) { //FIXME can not form tracklet should not call function with r2<=r1
51  return 20.0;
52  }
53 
54  double dphi = phi2 - phi1;
55  double dr = r2 - r1;
56 
57  return 2.0 * sin(dphi) / dr / sqrt(1.0 + 2 * r1 * r2 * (1.0 - cos(dphi)) / (dr * dr));
58  }
59 
60  inline std::string convertHexToBin(const std::string& stubwordhex) {
61  std::string stubwordbin = "";
62 
63  for (char word : stubwordhex) {
64  std::string hexword = "";
65  if (word == '0')
66  hexword = "0000";
67  else if (word == '1')
68  hexword = "0001";
69  else if (word == '2')
70  hexword = "0010";
71  else if (word == '3')
72  hexword = "0011";
73  else if (word == '4')
74  hexword = "0100";
75  else if (word == '5')
76  hexword = "0101";
77  else if (word == '6')
78  hexword = "0110";
79  else if (word == '7')
80  hexword = "0111";
81  else if (word == '8')
82  hexword = "1000";
83  else if (word == '9')
84  hexword = "1001";
85  else if (word == 'A')
86  hexword = "1010";
87  else if (word == 'B')
88  hexword = "1011";
89  else if (word == 'C')
90  hexword = "1100";
91  else if (word == 'D')
92  hexword = "1101";
93  else if (word == 'E')
94  hexword = "1110";
95  else if (word == 'F')
96  hexword = "1111";
97  else {
98  throw cms::Exception("Inconsistency")
99  << __FILE__ << " " << __LINE__ << " hex string format invalid: " << stubwordhex;
100  }
101  stubwordbin += hexword;
102  }
103  return stubwordbin;
104  }
105 
106  inline int ilog2(double factor) {
107  double power = log(factor) / log(2);
108  int ipower = round(power);
109  assert(std::abs(power - ipower) < 0.1);
110  return ipower;
111  }
112 
113  /******************************************************************************
114  * Checks to see if a directory exists. Note: This method only checks the
115  * existence of the full path AND if path leaf is a dir.
116  *
117  * @return 1 if dir exists AND is a dir,
118  * 0 if dir does not exist OR exists but not a dir,
119  * -1 if an error occurred (errno is also set)
120  *****************************************************************************/
121  inline int dirExists(const std::string& path) {
122  struct stat info;
123 
124  int statRC = stat(path.c_str(), &info);
125  if (statRC != 0) {
126  if (errno == ENOENT) {
127  return 0;
128  } // something along the path does not exist
129  if (errno == ENOTDIR) {
130  return 0;
131  } // something in path prefix is not a dir
132  return -1;
133  }
134 
135  return (info.st_mode & S_IFDIR) ? 1 : 0;
136  }
137 
138  //Open file - create directory if not existent.
139  inline std::ofstream openfile(const std::string& dir, const std::string& fname, const char* file, int line) {
140  if (dirExists(dir) != 1) {
141  edm::LogVerbatim("Tracklet") << "Creating directory : " << dir;
142  int fail = system((std::string("mkdir -p ") + dir).c_str());
143  if (fail) {
144  throw cms::Exception("BadDir") << file << " " << line << " could not create directory " << dir;
145  }
146  }
147 
148  std::ofstream out(dir + "/" + fname);
149 
150  if (out.fail()) {
151  throw cms::Exception("BadFile") << file << " " << line << " could not create file " << fname << " in " << dir;
152  }
153 
154  return out;
155  }
156 
157  //Open file - create directory if not existent.
158  //If first==true open file in create mode, if first==false open in append mode
159  inline void openfile(
160  std::ofstream& out, bool first, const std::string& dir, const std::string& fname, const char* file, int line) {
161  if (dirExists(dir) != 1) {
162  edm::LogVerbatim("Tracklet") << "Creating directory : " << dir;
163  int fail = system((std::string("mkdir -p ") + dir).c_str());
164  if (fail) {
165  throw cms::Exception("BadDir") << file << " " << line << " could not create directory " << dir;
166  }
167  }
168 
169  if (first) {
170  out.open(fname);
171  } else {
172  out.open(fname, std::ofstream::app);
173  }
174 
175  if (out.fail()) {
176  throw cms::Exception("BadFile") << file << " " << line << " could not create file " << fname << " in " << dir;
177  }
178  }
179 
180 }; // namespace trklet
181 #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:106
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
double rinv(double phi1, double phi2, double r1, double r2)
Definition: Util.h:49
int dirExists(const std::string &path)
Definition: Util.h:121
string fname
main script
#define str(s)
std::string convertHexToBin(const std::string &stubwordhex)
Definition: Util.h:60
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:139