00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifdef CSC_RENDER_PLUGIN
00019 #include "CSCDQM_Utility.h"
00020 #else
00021 #include "CSCDQM_Utility.h"
00022 #endif
00023
00024 #include <stdint.h>
00025
00026 namespace cscdqm {
00027
00033 int Utility::getCSCTypeBin(const std::string& cstr) {
00034 if (cstr.compare("ME-4/2") == 0) return 0;
00035 if (cstr.compare("ME-4/1") == 0) return 1;
00036 if (cstr.compare("ME-3/2") == 0) return 2;
00037 if (cstr.compare("ME-3/1") == 0) return 3;
00038 if (cstr.compare("ME-2/2") == 0) return 4;
00039 if (cstr.compare("ME-2/1") == 0) return 5;
00040 if (cstr.compare("ME-1/3") == 0) return 6;
00041 if (cstr.compare("ME-1/2") == 0) return 7;
00042 if (cstr.compare("ME-1/1") == 0) return 8;
00043 if (cstr.compare("ME+1/1") == 0) return 9;
00044 if (cstr.compare("ME+1/2") == 0) return 10;
00045 if (cstr.compare("ME+1/3") == 0) return 11;
00046 if (cstr.compare("ME+2/1") == 0) return 12;
00047 if (cstr.compare("ME+2/2") == 0) return 13;
00048 if (cstr.compare("ME+3/1") == 0) return 14;
00049 if (cstr.compare("ME+3/2") == 0) return 15;
00050 if (cstr.compare("ME+4/1") == 0) return 16;
00051 if (cstr.compare("ME+4/2") == 0) return 17;
00052 return 0;
00053 }
00054
00062 std::string Utility::getCSCTypeLabel(int endcap, int station, int ring ) {
00063 std::string label = "Unknown";
00064 std::ostringstream st;
00065 if ((endcap > 0) && (station > 0) && (ring > 0)) {
00066 if (endcap == 1) {
00067 st << "ME+" << station << "/" << ring;
00068 label = st.str();
00069 } else if (endcap==2) {
00070 st << "ME-" << station << "/" << ring;
00071 label = st.str();
00072 } else {
00073 label = "Unknown";
00074 }
00075 }
00076 return label;
00077 }
00078
00079
00087 int Utility::tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters) {
00088 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00089 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
00090 while (std::string::npos != pos || std::string::npos != lastPos) {
00091 tokens.push_back(str.substr(lastPos, pos - lastPos));
00092 lastPos = str.find_first_not_of(delimiters, pos);
00093 pos = str.find_first_of(delimiters, lastPos);
00094 }
00095 return tokens.size();
00096 }
00097
00105 void Utility::splitString(const std::string& str, const std::string& delim, std::vector<std::string>& results) {
00106 std::string::size_type lastPos = str.find_first_not_of(delim, 0);
00107 std::string::size_type pos = str.find_first_of(delim, lastPos);
00108 while (std::string::npos != pos || std::string::npos != lastPos) {
00109 results.push_back(str.substr(lastPos, pos - lastPos));
00110 lastPos = str.find_first_not_of(delim, pos);
00111 pos = str.find_first_of(delim, lastPos);
00112 }
00113 }
00114
00119 void Utility::trimString(std::string& str) {
00120 std::string::size_type pos = str.find_last_not_of(' ');
00121 if(pos != std::string::npos) {
00122 str.erase(pos + 1);
00123 pos = str.find_first_not_of(' ');
00124 if(pos != std::string::npos) {
00125 str.erase(0, pos);
00126 }
00127 } else {
00128 str.erase(str.begin(), str.end());
00129 }
00130 }
00131
00132
00139 bool Utility::regexMatch(const TPRegexp& re_expression, const std::string& message) {
00140 TPRegexp *re = const_cast<TPRegexp*>(&re_expression);
00141 return re->MatchB(message);
00142 }
00143
00150 bool Utility::regexMatch(const std::string& expression, const std::string& message) {
00151 return regexMatch(TPRegexp(expression), message);
00152 }
00153
00161 void Utility::regexReplace(const std::string& expression, std::string& message, const std::string replace) {
00162 Utility::regexReplace(TPRegexp(expression), message, replace);
00163 }
00164
00172 void Utility::regexReplace(const TPRegexp& re_expression, std::string& message, const std::string replace) {
00173 TString s(message);
00174 TPRegexp *re = const_cast<TPRegexp*>(&re_expression);
00175 re->Substitute(s, replace);
00176 message = s;
00177 }
00178
00187 std::string Utility::regexReplaceStr(const std::string& expression, const std::string& message, const std::string replace) {
00188 return regexReplaceStr(TPRegexp(expression), message, replace);
00189 }
00190
00199 std::string Utility::regexReplaceStr(const TPRegexp& re_expression, const std::string& message, const std::string replace) {
00200 TString s(message);
00201 TPRegexp *re = const_cast<TPRegexp*>(&re_expression);
00202 re->Substitute(s, replace);
00203 return s.Data();
00204 }
00205
00206 #undef get16bits
00207 #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
00208 #define get16bits(d) (*((const uint16_t *) (d)))
00209 #endif
00210
00211 #if !defined (get16bits)
00212 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) + (uint32_t)(((const uint8_t *)(d))[0]) )
00213 #endif
00214
00221 uint32_t Utility::fastHash(const char * data, int len) {
00222 uint32_t hash = len, tmp;
00223 int rem;
00224
00225 if (len <= 0 || data == NULL) return 0;
00226 rem = len & 3;
00227 len >>= 2;
00228
00229
00230 for (;len > 0; len--) {
00231 hash += get16bits (data);
00232 tmp = (get16bits (data+2) << 11) ^ hash;
00233 hash = (hash << 16) ^ tmp;
00234 data += 2*sizeof (uint16_t);
00235 hash += hash >> 11;
00236 }
00237
00238
00239 switch (rem) {
00240 case 3: hash += get16bits (data);
00241 hash ^= hash << 16;
00242 hash ^= data[sizeof (uint16_t)] << 18;
00243 hash += hash >> 11;
00244 break;
00245 case 2: hash += get16bits (data);
00246 hash ^= hash << 11;
00247 hash += hash >> 17;
00248 break;
00249 case 1: hash += *data;
00250 hash ^= hash << 10;
00251 hash += hash >> 1;
00252 }
00253
00254
00255 hash ^= hash << 3;
00256 hash += hash >> 5;
00257 hash ^= hash << 4;
00258 hash += hash >> 17;
00259 hash ^= hash << 25;
00260 hash += hash >> 6;
00261
00262 return hash;
00263 }
00264
00265
00278 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) {
00279 if (N > 0) {
00280 double eps_meas = (1.0 * n) / (1.0 * N);
00281 if (eps_meas < low_threshold) {
00282 double S = Utility::SignificanceLevelLow(N, n, low_threshold);
00283 if (S > low_sigfail) return -1;
00284 } else
00285 if (eps_meas > high_threshold) {
00286 double S = Utility::SignificanceLevelHigh(N, n);
00287 if (S > high_sigfail) return 1;
00288 }
00289 }
00290 return 0;
00291 }
00292
00301 bool Utility::checkError(const unsigned int N, const unsigned int n, const double threshold, const double sigfail) {
00302 if (N > 0) {
00303 const double eps_meas = (1.0 * n) / (1.0 * N);
00304 if (eps_meas > threshold) {
00305 if (Utility::SignificanceLevelLow(N, n, threshold) > sigfail) {
00306 return true;
00307 }
00308 }
00309 }
00310 return false;
00311 }
00312
00322 double Utility::SignificanceLevelLow(const unsigned int N, const unsigned int n, const double eps) {
00323
00326 double l_eps = eps;
00327 if (l_eps <= 0.0) l_eps = 0.000001;
00328 if (l_eps >= 1.0) l_eps = 0.999999;
00329
00330 double eps_meas = (1.0 * n) / (1.0 * N);
00331 double a = 1.0, b = 1.0;
00332
00333 if (n > 0) {
00334 for (unsigned int r = 0; r < n; r++) a = a * (eps_meas / l_eps);
00335 }
00336
00337 if (n < N) {
00338 for (unsigned int r = 0; r < (N - n); r++) b = b * (1 - eps_meas) / (1 - l_eps);
00339 }
00340
00341 return sqrt(2.0 * log(a * b));
00342
00343 }
00344
00353 double Utility::SignificanceLevelHigh(const unsigned int N, const unsigned int n) {
00354 if (N > n) return 0.0;
00356 double no = 1.0 * n, ne = 1.0 * N;
00357 return sqrt(2.0 * (no * (log(no / ne) - 1) + ne));
00358 }
00359
00360 }