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