Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:38:01

0001 #include <cstdlib>
0002 #include <cerrno>
0003 #include <cassert>
0004 #include <vector>
0005 #include <sstream>
0006 #include <cstring>
0007 #include <iterator>
0008 
0009 #include "CondTools/Hcal/interface/parseHcalDetId.h"
0010 
0011 static const char* subdetNames[] = {
0012     "",
0013     "HB",
0014     "HE",
0015     "HO",
0016     "HF",
0017 };
0018 static const unsigned nSsubdetNames = sizeof(subdetNames) / sizeof(subdetNames[0]);
0019 
0020 static bool parseSubdetector(const char* c, HcalSubdetector* result) {
0021   assert(c);
0022   assert(result);
0023   for (unsigned i = 1; i < nSsubdetNames; ++i)
0024     if (strcmp(c, subdetNames[i]) == 0) {
0025       *result = static_cast<HcalSubdetector>(i);
0026       return true;
0027     }
0028   return false;
0029 }
0030 
0031 static bool parse_int(const char* c, int* result) {
0032   assert(c);
0033   assert(result);
0034   char* endptr;
0035   errno = 0;
0036   *result = strtol(c, &endptr, 0);
0037   return !errno && *endptr == '\0';
0038 }
0039 
0040 const char* hcalSubdetectorName(HcalSubdetector subdet) {
0041   const unsigned ind = static_cast<unsigned>(subdet);
0042   assert(ind < nSsubdetNames);
0043   return subdetNames[ind];
0044 }
0045 
0046 HcalDetId parseHcalDetId(const std::string& s) {
0047   using namespace std;
0048 
0049   // Expected string contents:
0050   //
0051   //   ieta  iphi  depth  subdetector
0052   //
0053   // subdetector is one of "HB", "HE", "HF", or "HO"
0054   //
0055   HcalDetId result;
0056   istringstream iss(s);
0057   vector<string> tokens(istream_iterator<string>{iss}, istream_iterator<string>{});
0058   if (tokens.size() == 4) {
0059     HcalSubdetector subdet;
0060     int ieta, iphi, depth;
0061     if (parse_int(tokens[0].c_str(), &ieta) && parse_int(tokens[1].c_str(), &iphi) &&
0062         parse_int(tokens[2].c_str(), &depth) && parseSubdetector(tokens[3].c_str(), &subdet))
0063       result = HcalDetId(subdet, ieta, iphi, depth);
0064   }
0065   return result;
0066 }