CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
22 #endif
23 
24 #include <stdint.h>
25 
26 namespace cscdqm {
27 
33  int Utility::getCSCTypeBin(const std::string& cstr) {
34  if (cstr.compare("ME-4/2") == 0) return 0;
35  if (cstr.compare("ME-4/1") == 0) return 1;
36  if (cstr.compare("ME-3/2") == 0) return 2;
37  if (cstr.compare("ME-3/1") == 0) return 3;
38  if (cstr.compare("ME-2/2") == 0) return 4;
39  if (cstr.compare("ME-2/1") == 0) return 5;
40  if (cstr.compare("ME-1/3") == 0) return 6;
41  if (cstr.compare("ME-1/2") == 0) return 7;
42  if (cstr.compare("ME-1/1") == 0) return 8;
43  if (cstr.compare("ME+1/1") == 0) return 9;
44  if (cstr.compare("ME+1/2") == 0) return 10;
45  if (cstr.compare("ME+1/3") == 0) return 11;
46  if (cstr.compare("ME+2/1") == 0) return 12;
47  if (cstr.compare("ME+2/2") == 0) return 13;
48  if (cstr.compare("ME+3/1") == 0) return 14;
49  if (cstr.compare("ME+3/2") == 0) return 15;
50  if (cstr.compare("ME+4/1") == 0) return 16;
51  if (cstr.compare("ME+4/2") == 0) return 17;
52  return 0;
53  }
54 
62  std::string Utility::getCSCTypeLabel(int endcap, int station, int ring ) {
63  std::string label = "Unknown";
64  std::ostringstream st;
65  if ((endcap > 0) && (station > 0) && (ring > 0)) {
66  if (endcap == 1) {
67  st << "ME+" << station << "/" << ring;
68  label = st.str();
69  } else if (endcap==2) {
70  st << "ME-" << station << "/" << ring;
71  label = st.str();
72  } else {
73  label = "Unknown";
74  }
75  }
76  return label;
77  }
78 
79 
87  int Utility::tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters) {
88  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
89  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
90  while (std::string::npos != pos || std::string::npos != lastPos) {
91  tokens.push_back(str.substr(lastPos, pos - lastPos));
92  lastPos = str.find_first_not_of(delimiters, pos);
93  pos = str.find_first_of(delimiters, lastPos);
94  }
95  return tokens.size();
96  }
97 
105  void Utility::splitString(const std::string& str, const std::string& delim, std::vector<std::string>& results) {
106  std::string::size_type lastPos = str.find_first_not_of(delim, 0);
107  std::string::size_type pos = str.find_first_of(delim, lastPos);
108  while (std::string::npos != pos || std::string::npos != lastPos) {
109  results.push_back(str.substr(lastPos, pos - lastPos));
110  lastPos = str.find_first_not_of(delim, pos);
111  pos = str.find_first_of(delim, lastPos);
112  }
113  }
114 
119  void Utility::trimString(std::string& str) {
120  std::string::size_type pos = str.find_last_not_of(' ');
121  if(pos != std::string::npos) {
122  str.erase(pos + 1);
123  pos = str.find_first_not_of(' ');
124  if(pos != std::string::npos) {
125  str.erase(0, pos);
126  }
127  } else {
128  str.erase(str.begin(), str.end());
129  }
130  }
131 
132 
139  bool Utility::regexMatch(const TPRegexp& re_expression, const std::string& message) {
140  TPRegexp *re = const_cast<TPRegexp*>(&re_expression);
141  return re->MatchB(message);
142  }
143 
150  bool Utility::regexMatch(const std::string& expression, const std::string& message) {
151  return regexMatch(TPRegexp(expression), message);
152  }
153 
161  void Utility::regexReplace(const std::string& expression, std::string& message, const std::string replace) {
162  Utility::regexReplace(TPRegexp(expression), message, replace);
163  }
164 
172  void Utility::regexReplace(const TPRegexp& re_expression, std::string& message, const std::string replace) {
173  TString s(message);
174  TPRegexp *re = const_cast<TPRegexp*>(&re_expression);
175  re->Substitute(s, replace);
176  message = s;
177  }
178 
187  std::string Utility::regexReplaceStr(const std::string& expression, const std::string& message, const std::string replace) {
188  return regexReplaceStr(TPRegexp(expression), message, replace);
189  }
190 
199  std::string Utility::regexReplaceStr(const TPRegexp& re_expression, const std::string& message, const std::string replace) {
200  TString s(message);
201  TPRegexp *re = const_cast<TPRegexp*>(&re_expression);
202  re->Substitute(s, replace);
203  return s.Data();
204  }
205 
206 #undef get16bits
207 #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
208 #define get16bits(d) (*((const uint16_t *) (d)))
209 #endif
210 
211 #if !defined (get16bits)
212 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) + (uint32_t)(((const uint8_t *)(d))[0]) )
213 #endif
214 
221  uint32_t Utility::fastHash(const char * data, int len) {
222  uint32_t hash = len, tmp;
223  int rem;
224 
225  if (len <= 0 || data == NULL) return 0;
226  rem = len & 3;
227  len >>= 2;
228 
229  /* Main loop */
230  for (;len > 0; len--) {
231  hash += get16bits (data);
232  tmp = (get16bits (data+2) << 11) ^ hash;
233  hash = (hash << 16) ^ tmp;
234  data += 2*sizeof (uint16_t);
235  hash += hash >> 11;
236  }
237 
238  /* Handle end cases */
239  switch (rem) {
240  case 3: hash += get16bits (data);
241  hash ^= hash << 16;
242  hash ^= data[sizeof (uint16_t)] << 18;
243  hash += hash >> 11;
244  break;
245  case 2: hash += get16bits (data);
246  hash ^= hash << 11;
247  hash += hash >> 17;
248  break;
249  case 1: hash += *data;
250  hash ^= hash << 10;
251  hash += hash >> 1;
252  }
253 
254  /* Force "avalanching" of final 127 bits */
255  hash ^= hash << 3;
256  hash += hash >> 5;
257  hash ^= hash << 4;
258  hash += hash >> 17;
259  hash ^= hash << 25;
260  hash += hash >> 6;
261 
262  return hash;
263  }
264 
265 
278  short Utility::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) {
279  if (N > 0) {
280  double eps_meas = (1.0 * n) / (1.0 * N);
281  if (eps_meas < low_threshold) {
282  double S = Utility::SignificanceLevelLow(N, n, low_threshold);
283  if (S > low_sigfail) return -1;
284  } else
285  if (eps_meas > high_threshold) {
286  double S = Utility::SignificanceLevelHigh(N, n);
287  if (S > high_sigfail) return 1;
288  }
289  }
290  return 0;
291  }
292 
301  bool Utility::checkError(const unsigned int N, const unsigned int n, const double threshold, const double sigfail) {
302  if (N > 0) {
303  const double eps_meas = (1.0 * n) / (1.0 * N);
304  if (eps_meas > threshold) {
305  if (Utility::SignificanceLevelLow(N, n, threshold) > sigfail) {
306  return true;
307  }
308  }
309  }
310  return false;
311  }
312 
322  double Utility::SignificanceLevelLow(const unsigned int N, const unsigned int n, const double eps) {
323 
326  double l_eps = eps;
327  if (l_eps <= 0.0) l_eps = 0.000001;
328  if (l_eps >= 1.0) l_eps = 0.999999;
329 
330  double eps_meas = (1.0 * n) / (1.0 * N);
331  double a = 1.0, b = 1.0;
332 
333  if (n > 0) {
334  for (unsigned int r = 0; r < n; r++) a = a * (eps_meas / l_eps);
335  }
336 
337  if (n < N) {
338  for (unsigned int r = 0; r < (N - n); r++) b = b * (1 - eps_meas) / (1 - l_eps);
339  }
340 
341  return sqrt(2.0 * log(a * b));
342 
343  }
344 
353  double Utility::SignificanceLevelHigh(const unsigned int N, const unsigned int n) {
354  if (N > n) return 0.0;
356  double no = 1.0 * n, ne = 1.0 * N;
357  return sqrt(2.0 * (no * (log(no / ne) - 1) + ne));
358  }
359 
360 }
static void splitString(const std::string &str, const std::string &delim, std::vector< std::string > &results)
Split string according to delimiter.
const std::string & label
Definition: MVAComputer.cc:186
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 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...
#define NULL
Definition: scimark2.h:8
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.
T sqrt(T t)
Definition: SSEVec.h:28
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.
Log< T >::type log(const T &t)
Definition: Log.h:22
double b
Definition: hdecay.h:120
string message
Definition: argparse.py:126
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...
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
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)
string s
Definition: asciidump.py:422
static int tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
Break string into tokens.