00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "DQM/CSCMonitorModule/interface/CSCUtility.h"
00021
00028 std::string CSCUtility::getDDUTag(const unsigned int& dduNumber, std::string& buffer) {
00029 std::stringstream oss;
00030 oss << std::setfill('0');
00031 oss << "DDU_" << std::setw(2) << dduNumber;
00032 buffer = oss.str();
00033 return buffer;
00034 }
00035
00043 bool CSCUtility::findHistoValue(Histo& h, const std::string name, std::string& value) {
00044 HistoIter i = h.find(name);
00045 if(i == h.end()) {
00046 return false;
00047 } else {
00048 value = i->second;
00049 }
00050 return true;
00051 }
00052
00060 bool CSCUtility::findHistoValue(Histo& h, const std::string name, int& value) {
00061 HistoIter i = h.find(name);
00062 if(i == h.end()) {
00063 return false;
00064 } else {
00065 if(EOF == std::sscanf(i->second.c_str(), "%d", &value)) {
00066 return false;
00067 }
00068 }
00069 return true;
00070 }
00071
00079 bool CSCUtility::findHistoValue(Histo& h, const std::string name, double& value) {
00080 HistoIter i = h.find(name);
00081 if(i == h.end()) {
00082 return false;
00083 } else {
00084 if(EOF == std::sscanf(i->second.c_str(), "%lf", &value)) {
00085 return false;
00086 }
00087 }
00088 return true;
00089 }
00090
00099 std::string CSCUtility::getHistoValue(Histo& h, const std::string name, std::string& value, const std::string def_value) {
00100 if(!findHistoValue(h, name, value)) {
00101 value = def_value;
00102 }
00103 return value;
00104 }
00105
00114 int CSCUtility::getHistoValue(Histo& h, const std::string name, int& value, const int def_value) {
00115 if(!findHistoValue(h, name, value)) {
00116 value = def_value;
00117 }
00118 return value;
00119 }
00120
00129 double CSCUtility::getHistoValue(Histo& h, const std::string name, double& value, const int def_value) {
00130 if(!findHistoValue(h, name, value)) {
00131 value = def_value;
00132 }
00133 return value;
00134 }
00135
00142 int CSCUtility::ParseAxisLabels(const std::string& s, std::map<int, std::string>& labels) {
00143 std::string tmp = s;
00144 std::string::size_type pos = tmp.find("|");
00145 char* stopstring = NULL;
00146
00147 while (pos != std::string::npos) {
00148 std::string label_pair = tmp.substr(0, pos);
00149 tmp.replace(0,pos+1,"");
00150 if (label_pair.find("=") != std::string::npos) {
00151 int nbin = strtol(label_pair.substr(0,label_pair.find("=")).c_str(), &stopstring, 10);
00152 std::string label = label_pair.substr(label_pair.find("=")+1, label_pair.length());
00153 while (label.find("\'") != std::string::npos) {
00154 label.erase(label.find("\'"),1);
00155 }
00156 labels[nbin] = label;
00157 }
00158 pos = tmp.find("|");
00159 }
00160 return labels.size();
00161 }
00162
00168 int CSCUtility::getCSCTypeBin(const std::string& cstr) {
00169 if (cstr.compare("ME-4/2") == 0) return 0;
00170 if (cstr.compare("ME-4/1") == 0) return 1;
00171 if (cstr.compare("ME-3/2") == 0) return 2;
00172 if (cstr.compare("ME-3/1") == 0) return 3;
00173 if (cstr.compare("ME-2/2") == 0) return 4;
00174 if (cstr.compare("ME-2/1") == 0) return 5;
00175 if (cstr.compare("ME-1/3") == 0) return 6;
00176 if (cstr.compare("ME-1/2") == 0) return 7;
00177 if (cstr.compare("ME-1/1") == 0) return 8;
00178 if (cstr.compare("ME+1/1") == 0) return 9;
00179 if (cstr.compare("ME+1/2") == 0) return 10;
00180 if (cstr.compare("ME+1/3") == 0) return 11;
00181 if (cstr.compare("ME+2/1") == 0) return 12;
00182 if (cstr.compare("ME+2/2") == 0) return 13;
00183 if (cstr.compare("ME+3/1") == 0) return 14;
00184 if (cstr.compare("ME+3/2") == 0) return 15;
00185 if (cstr.compare("ME+4/1") == 0) return 16;
00186 if (cstr.compare("ME+4/2") == 0) return 17;
00187 return 0;
00188 }
00189
00197 std::string CSCUtility::getCSCTypeLabel(int endcap, int station, int ring ) {
00198 std::string label = "Unknown";
00199 std::ostringstream st;
00200 if ((endcap > 0) && (station > 0) && (ring > 0)) {
00201 if (endcap == 1) {
00202 st << "ME+" << station << "/" << ring;
00203 label = st.str();
00204 } else if (endcap==2) {
00205 st << "ME-" << station << "/" << ring;
00206 label = st.str();
00207 } else {
00208 label = "Unknown";
00209 }
00210 }
00211 return label;
00212 }
00213
00214
00222 int CSCUtility::tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters) {
00223 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00224 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
00225 while (std::string::npos != pos || std::string::npos != lastPos) {
00226 tokens.push_back(str.substr(lastPos, pos - lastPos));
00227 lastPos = str.find_first_not_of(delimiters, pos);
00228 pos = str.find_first_of(delimiters, lastPos);
00229 }
00230 return tokens.size();
00231 }
00232
00240 void CSCUtility::splitString(std::string str, const std::string delim, std::vector<std::string>& results) {
00241 unsigned int cutAt;
00242 while ((cutAt = str.find_first_of(delim)) != str.npos) {
00243 if(cutAt > 0) {
00244 results.push_back(str.substr(0, cutAt));
00245 }
00246 str = str.substr(cutAt + 1);
00247 }
00248 if(str.length() > 0) {
00249 results.push_back(str);
00250 }
00251 }
00252
00253
00259 void CSCUtility::trimString(std::string& str) {
00260 std::string::size_type pos = str.find_last_not_of(' ');
00261 if(pos != std::string::npos) {
00262 str.erase(pos + 1);
00263 pos = str.find_first_not_of(' ');
00264 if(pos != std::string::npos)
00265 str.erase(0, pos);
00266 } else
00267 str.erase(str.begin(), str.end());
00268 }
00269
00277 uint32_t CSCUtility::fastHash(const char * data, int len) {
00278 uint32_t hash = len, tmp;
00279 int rem;
00280
00281 if (len <= 0 || data == NULL) return 0;
00282 rem = len & 3;
00283 len >>= 2;
00284
00285
00286 for (;len > 0; len--) {
00287 hash += get16bits (data);
00288 tmp = (get16bits (data+2) << 11) ^ hash;
00289 hash = (hash << 16) ^ tmp;
00290 data += 2*sizeof (uint16_t);
00291 hash += hash >> 11;
00292 }
00293
00294
00295 switch (rem) {
00296 case 3: hash += get16bits (data);
00297 hash ^= hash << 16;
00298 hash ^= data[sizeof (uint16_t)] << 18;
00299 hash += hash >> 11;
00300 break;
00301 case 2: hash += get16bits (data);
00302 hash ^= hash << 11;
00303 hash += hash >> 17;
00304 break;
00305 case 1: hash += *data;
00306 hash ^= hash << 10;
00307 hash += hash >> 1;
00308 }
00309
00310
00311 hash ^= hash << 3;
00312 hash += hash >> 5;
00313 hash ^= hash << 4;
00314 hash += hash >> 17;
00315 hash ^= hash << 25;
00316 hash += hash >> 6;
00317
00318 return hash;
00319 }