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 {
22  assert(c);
23  assert(result);
24  for (unsigned i=1; i<nSsubdetNames; ++i)
25  if (strcmp(c, subdetNames[i]) == 0)
26  {
27  *result = static_cast<HcalSubdetector>(i);
28  return true;
29  }
30  return false;
31 }
32 
33 static bool parse_int(const char *c, int *result)
34 {
35  assert(c);
36  assert(result);
37  char *endptr;
38  errno = 0;
39  *result = strtol(c, &endptr, 0);
40  return !errno && *endptr == '\0';
41 }
42 
44 {
45  const unsigned ind = static_cast<unsigned>(subdet);
46  assert(ind < nSsubdetNames);
47  return subdetNames[ind];
48 }
49 
51 {
52  using namespace std;
53 
54  // Expected string contents:
55  //
56  // ieta iphi depth subdetector
57  //
58  // subdetector is one of "HB", "HE", "HF", or "HO"
59  //
61  istringstream iss(s);
62  vector<string> tokens(istream_iterator<string>{iss},
63  istream_iterator<string>{});
64  if (tokens.size() == 4)
65  {
66  HcalSubdetector subdet;
67  int ieta, iphi, depth;
68  if (parse_int(tokens[0].c_str(), &ieta)
69  && parse_int(tokens[1].c_str(), &iphi)
70  && parse_int(tokens[2].c_str(), &depth)
71  && parseSubdetector(tokens[3].c_str(), &subdet)
72  )
73  result = HcalDetId(subdet, ieta, iphi, depth);
74  }
75  return result;
76 }
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[]