CMS 3D CMS Logo

parseHcalDetId.cc
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <cerrno>
3 #include <cassert>
4 #include <vector>
5 #include <sstream>
6 #include <cstring>
7 #include <iterator>
8 
10 
11 static const char* subdetNames[] = {
12  "",
13  "HB",
14  "HE",
15  "HO",
16  "HF",
17 };
18 static const unsigned nSsubdetNames = sizeof(subdetNames) / sizeof(subdetNames[0]);
19 
20 static bool parseSubdetector(const char* c, HcalSubdetector* result) {
21  assert(c);
22  assert(result);
23  for (unsigned i = 1; i < nSsubdetNames; ++i)
24  if (strcmp(c, subdetNames[i]) == 0) {
25  *result = static_cast<HcalSubdetector>(i);
26  return true;
27  }
28  return false;
29 }
30 
31 static bool parse_int(const char* c, int* result) {
32  assert(c);
33  assert(result);
34  char* endptr;
35  errno = 0;
36  *result = strtol(c, &endptr, 0);
37  return !errno && *endptr == '\0';
38 }
39 
40 const char* hcalSubdetectorName(HcalSubdetector subdet) {
41  const unsigned ind = static_cast<unsigned>(subdet);
42  assert(ind < nSsubdetNames);
43  return subdetNames[ind];
44 }
45 
47  using namespace std;
48 
49  // Expected string contents:
50  //
51  // ieta iphi depth subdetector
52  //
53  // subdetector is one of "HB", "HE", "HF", or "HO"
54  //
56  istringstream iss(s);
57  vector<string> tokens(istream_iterator<string>{iss}, istream_iterator<string>{});
58  if (tokens.size() == 4) {
59  HcalSubdetector subdet;
60  int ieta, iphi, depth;
61  if (parse_int(tokens[0].c_str(), &ieta) && parse_int(tokens[1].c_str(), &iphi) &&
62  parse_int(tokens[2].c_str(), &depth) && parseSubdetector(tokens[3].c_str(), &subdet))
63  result = HcalDetId(subdet, ieta, iphi, depth);
64  }
65  return result;
66 }
assert(be >=bs)
static const unsigned nSsubdetNames
static bool parseSubdetector(const char *c, HcalSubdetector *result)
HcalSubdetector
Definition: HcalAssistant.h:31
const char * hcalSubdetectorName(HcalSubdetector subdet)
static bool parse_int(const char *c, int *result)
HcalDetId parseHcalDetId(const std::string &s)
static const char * subdetNames[]