CMS 3D CMS Logo

CSCDQM_Utility.cc
Go to the documentation of this file.
1 /* =====================================================================================
2  *
3  * Filename: CSCDQM_Utility.cc
4  *
5  * Description: Histogram Utility code
6  *
7  * Version: 1.0
8  * Created: 04/18/2008 03:39:49 PM
9  * Revision: none
10  * Compiler: gcc
11  *
12  * Author: Valdas Rapsevicius (VR), Valdas.Rapsevicius@cern.ch
13  * Company: CERN, CH
14  *
15  * =====================================================================================
16  */
17 
18 #ifdef CSC_RENDER_PLUGIN
19 #include "CSCDQM_Utility.h"
20 #else
21 #include "CSCDQM_Utility.h"
22 #endif
23 
24 #include <cstdint>
25 
26 namespace cscdqm {
27 
34  if (cstr == "ME-4/2")
35  return 0;
36  if (cstr == "ME-4/1")
37  return 1;
38  if (cstr == "ME-3/2")
39  return 2;
40  if (cstr == "ME-3/1")
41  return 3;
42  if (cstr == "ME-2/2")
43  return 4;
44  if (cstr == "ME-2/1")
45  return 5;
46  if (cstr == "ME-1/3")
47  return 6;
48  if (cstr == "ME-1/2")
49  return 7;
50  if (cstr == "ME-1/1")
51  return 8;
52  if (cstr == "ME+1/1")
53  return 9;
54  if (cstr == "ME+1/2")
55  return 10;
56  if (cstr == "ME+1/3")
57  return 11;
58  if (cstr == "ME+2/1")
59  return 12;
60  if (cstr == "ME+2/2")
61  return 13;
62  if (cstr == "ME+3/1")
63  return 14;
64  if (cstr == "ME+3/2")
65  return 15;
66  if (cstr == "ME+4/1")
67  return 16;
68  if (cstr == "ME+4/2")
69  return 17;
70  return 0;
71  }
72 
81  std::string label = "Unknown";
82  std::ostringstream st;
83  if ((endcap > 0) && (station > 0) && (ring > 0)) {
84  if (endcap == 1) {
85  st << "ME+" << station << "/" << ring;
86  label = st.str();
87  } else if (endcap == 2) {
88  st << "ME-" << station << "/" << ring;
89  label = st.str();
90  } else {
91  label = "Unknown";
92  }
93  }
94  return label;
95  }
96 
104  int Utility::tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters) {
105  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
106  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
107  while (std::string::npos != pos || std::string::npos != lastPos) {
108  tokens.push_back(str.substr(lastPos, pos - lastPos));
109  lastPos = str.find_first_not_of(delimiters, pos);
110  pos = str.find_first_of(delimiters, lastPos);
111  }
112  return tokens.size();
113  }
114 
122  void Utility::splitString(const std::string& str, const std::string& delim, std::vector<std::string>& results) {
123  std::string::size_type lastPos = str.find_first_not_of(delim, 0);
124  std::string::size_type pos = str.find_first_of(delim, lastPos);
125  while (std::string::npos != pos || std::string::npos != lastPos) {
126  results.push_back(str.substr(lastPos, pos - lastPos));
127  lastPos = str.find_first_not_of(delim, pos);
128  pos = str.find_first_of(delim, lastPos);
129  }
130  }
131 
137  std::string::size_type pos = str.find_last_not_of(' ');
138  if (pos != std::string::npos) {
139  str.erase(pos + 1);
140  pos = str.find_first_not_of(' ');
141  if (pos != std::string::npos) {
142  str.erase(0, pos);
143  }
144  } else {
145  str.erase(str.begin(), str.end());
146  }
147  }
148 
155  bool Utility::regexMatch(const TPRegexp& re_expression, const std::string& message) {
156  TPRegexp* re = const_cast<TPRegexp*>(&re_expression);
157  return re->MatchB(message);
158  }
159 
166  bool Utility::regexMatch(const std::string& expression, const std::string& message) {
167  return regexMatch(TPRegexp(expression), message);
168  }
169 
178  Utility::regexReplace(TPRegexp(expression), message, replace);
179  }
180 
188  void Utility::regexReplace(const TPRegexp& re_expression, std::string& message, const std::string replace) {
189  TString s(message);
190  TPRegexp* re = const_cast<TPRegexp*>(&re_expression);
191  re->Substitute(s, replace);
192  message = static_cast<const char*>(s);
193  }
194 
204  const std::string& message,
205  const std::string replace) {
206  return regexReplaceStr(TPRegexp(expression), message, replace);
207  }
208 
217  std::string Utility::regexReplaceStr(const TPRegexp& re_expression,
218  const std::string& message,
219  const std::string replace) {
220  TString s(message);
221  TPRegexp* re = const_cast<TPRegexp*>(&re_expression);
222  re->Substitute(s, replace);
223  return s.Data();
224  }
225 
226 #undef get16bits
227 #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) || defined(_MSC_VER) || defined(__BORLANDC__) || \
228  defined(__TURBOC__)
229 #define get16bits(d) (*((const uint16_t*)(d)))
230 #endif
231 
232 #if !defined(get16bits)
233 #define get16bits(d) ((((uint32_t)(((const uint8_t*)(d))[1])) << 8) + (uint32_t)(((const uint8_t*)(d))[0]))
234 #endif
235 
242  uint32_t Utility::fastHash(const char* data, int len) {
243  uint32_t hash = len, tmp;
244  int rem;
245 
246  if (len <= 0 || data == nullptr)
247  return 0;
248  rem = len & 3;
249  len >>= 2;
250 
251  /* Main loop */
252  for (; len > 0; len--) {
253  hash += get16bits(data);
254  tmp = (get16bits(data + 2) << 11) ^ hash;
255  hash = (hash << 16) ^ tmp;
256  data += 2 * sizeof(uint16_t);
257  hash += hash >> 11;
258  }
259 
260  /* Handle end cases */
261  switch (rem) {
262  case 3:
263  hash += get16bits(data);
264  hash ^= hash << 16;
265  hash ^= data[sizeof(uint16_t)] << 18;
266  hash += hash >> 11;
267  break;
268  case 2:
269  hash += get16bits(data);
270  hash ^= hash << 11;
271  hash += hash >> 17;
272  break;
273  case 1:
274  hash += *data;
275  hash ^= hash << 10;
276  hash += hash >> 1;
277  }
278 
279  /* Force "avalanching" of final 127 bits */
280  hash ^= hash << 3;
281  hash += hash >> 5;
282  hash ^= hash << 4;
283  hash += hash >> 17;
284  hash ^= hash << 25;
285  hash += hash >> 6;
286 
287  return hash;
288  }
289 
302  short Utility::checkOccupancy(const unsigned int N,
303  const unsigned int n,
304  const double low_threshold,
305  const double high_threshold,
306  const double low_sigfail,
307  const double high_sigfail) {
308  if (N > 0) {
309  double eps_meas = (1.0 * n) / (1.0 * N);
310  if (eps_meas < low_threshold) {
311  double S = Utility::SignificanceLevelLow(N, n, low_threshold);
312  if (S > low_sigfail)
313  return -1;
314  } else if (eps_meas > high_threshold) {
315  double S = Utility::SignificanceLevelHigh(N, n);
316  if (S > high_sigfail)
317  return 1;
318  }
319  }
320  return 0;
321  }
322 
331  bool Utility::checkError(const unsigned int N, const unsigned int n, const double threshold, const double sigfail) {
332  if (N > 0) {
333  const double eps_meas = (1.0 * n) / (1.0 * N);
334  if (eps_meas > threshold) {
335  if (Utility::SignificanceLevelLow(N, n, threshold) > sigfail) {
336  return true;
337  }
338  }
339  }
340  return false;
341  }
342 
352  double Utility::SignificanceLevelLow(const unsigned int N, const unsigned int n, const double eps) {
355  double l_eps = eps;
356  if (l_eps <= 0.0)
357  l_eps = 0.000001;
358  if (l_eps >= 1.0)
359  l_eps = 0.999999;
360 
361  double eps_meas = (1.0 * n) / (1.0 * N);
362  double a = 1.0, b = 1.0;
363 
364  if (n > 0) {
365  for (unsigned int r = 0; r < n; r++)
366  a = a * (eps_meas / l_eps);
367  }
368 
369  if (n < N) {
370  for (unsigned int r = 0; r < (N - n); r++)
371  b = b * (1 - eps_meas) / (1 - l_eps);
372  }
373 
374  return sqrt(2.0 * log(a * b));
375  }
376 
385  double Utility::SignificanceLevelHigh(const unsigned int N, const unsigned int n) {
386  if (N > n)
387  return 0.0;
389  double no = 1.0 * n, ne = 1.0 * N;
390  return sqrt(2.0 * (no * (log(no / ne) - 1) + ne));
391  }
392 
397  int Utility::getRUIfromDDUId(unsigned ddu_id) {
398  int rui = -1;
399  const unsigned postLS1_map[] = {841, 842, 843, 844, 845, 846, 847, 848, 849, 831, 832, 833,
400  834, 835, 836, 837, 838, 839, 861, 862, 863, 864, 865, 866,
401  867, 868, 869, 851, 852, 853, 854, 855, 856, 857, 858, 859};
402  if ((ddu_id >= FEDNumbering::MINCSCDDUFEDID) && (ddu_id <= FEDNumbering::MAXCSCDDUFEDID)) {
403  for (int i = 0; i < 36; i++) {
404  if (ddu_id == postLS1_map[i]) {
405  rui = i + 1;
406  return rui;
407  }
408  }
409  } else {
410  rui = ddu_id & 0xFF;
411  }
412  return rui;
413  }
414 
415 } // namespace cscdqm
static void splitString(const std::string &str, const std::string &delim, std::vector< std::string > &results)
Split string according to delimiter.
static double SignificanceLevelHigh(const unsigned int N, const unsigned int n)
Calculate error significance alpha for the given number of events based on reference number of errors...
static std::string regexReplaceStr(const std::string &expression, const std::string &message, const std::string replace="")
Replace string part that matches RegExp expression with some string.
static int getRUIfromDDUId(unsigned ddu_id)
Get RUI Number from DDU source ID for post LS1 configuration.
static double SignificanceLevelLow(const unsigned int N, const unsigned int n, const double eps)
Calculate error significance alpha for the given number of errors based on reference number of errors...
def replace(string, replacements)
static bool regexMatch(const std::string &expression, const std::string &message)
Match RegExp expression string against string message and return result.
static bool checkError(const unsigned int N, const unsigned int n, const double threshold, const double sigfail)
Check the hypothesis that error events (n) value above threshold comparing with the expected 0 and st...
static uint32_t fastHash(const char *data, int len)
Calculate super fast hash (from http://www.azillionmonkeys.com/qed/hash.html)
uint16_t size_type
static void trimString(std::string &str)
Trim string.
char const * label
T sqrt(T t)
Definition: SSEVec.h:19
static std::string getCSCTypeLabel(int endcap, int station, int ring)
Get CSC label from CSC parameters.
static int getCSCTypeBin(const std::string &cstr)
Get CSC y-axis position from chamber string.
#define N
Definition: blowfish.cc:9
double b
Definition: hdecay.h:120
static short checkOccupancy(const unsigned int N, const unsigned int n, const double low_threshold, const double high_threshold, const double low_sigfail, const double high_sigfail)
Check the hypothesis that observed events (n) value is too low or too high comparing with the expecte...
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
static void regexReplace(const std::string &expression, std::string &message, const std::string replace="")
Replace string part that matches RegExp expression with some string.
double a
Definition: hdecay.h:121
#define get16bits(d)
results
Definition: mysort.py:8
#define str(s)
tmp
align.sh
Definition: createJobs.py:716
static int tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
Break string into tokens.