CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CastorText2DetIdConverter.cc
Go to the documentation of this file.
1 
3 #include <cstdlib>
4 #include <iostream>
5 #include <iomanip>
6 #include <cstdio>
7 
9 
13 
15 
16 namespace {
17  std::string strip(const std::string& fString) {
18  if (fString.empty())
19  return fString;
20  int startIndex = fString.find_first_not_of(" \t\n");
21  int endIndex = fString.find_last_not_of(" \t\n");
22  return fString.substr(startIndex, (endIndex - startIndex) + 1);
23  }
24 } // namespace
25 
27  const std::string& fField1,
28  const std::string& fField2,
29  const std::string& fField3) {
30  if (!init(fFlavor, fField1, fField2, fField3)) {
31  std::cerr << "CastorText2DetIdConverter::CastorText2DetIdConverter-> Can not initiate detId from items: " << fFlavor
32  << '/' << fField1 << '/' << fField2 << '/' << fField3 << std::endl;
33  throw cms::Exception("HcalGenDetId initialization error")
34  << " Can not initiate detId from items: " << fFlavor << '/' << fField1 << '/' << fField2 << '/' << fField3
35  << std::endl;
36  }
37 }
38 
40 
42 
44  bool result = true;
45  flavorName = "UNKNOWN";
46  mId = fId;
47  HcalGenericDetId genId(mId);
48  if (fId == HcalDetId::Undefined) {
49  flavorName = "NA";
50  }
51 
52  else if (genId.isHcalCastorDetId()) {
53  HcalCastorDetId castorId(mId);
54  switch (castorId.section()) {
56  flavorName = "CASTOR_EM";
57  break;
59  flavorName = "CASTOR_HAD";
60  break;
61  default:
62  result = false;
63  }
64  setField(1, castorId.zside());
65  setField(2, castorId.sector());
66  setField(3, castorId.module());
67  }
68 
69  else {
70  flavorName = "UNKNOWN_FLAVOR";
71  std::cerr << "CastorText2DetIdConverter::init-> Unknown detId: " << std::hex << std::showbase << mId.rawId()
72  << std::endl;
73  result = false;
74  }
75  return result;
76 }
77 
79  const std::string& fField1,
80  const std::string& fField2,
81  const std::string& fField3) {
82  bool result = true;
83  flavorName = strip(fFlavor);
84  field1 = strip(fField1);
85  field2 = strip(fField2);
86  field3 = strip(fField3);
87  if (flavorName.find("CASTOR_") == 0) {
89  : flavorName == "CASTOR_HAD" ? HcalCastorDetId::HAD
91  mId = HcalCastorDetId(section, getField(1) > 0, getField(2), getField(3));
92  }
93 
94  else {
95  std::cerr << "CastorText2DetIdConverter::init-> Unknown DetId flavor: " << flavorName << std::endl;
96  result = false;
97  }
98  return result;
99 }
100 
102  char* endptr;
103  const char* nptr = i == 1 ? field1.c_str() : i == 2 ? field2.c_str() : field3.c_str();
104  long result = strtol(nptr, &endptr, 0);
105  if (*nptr != '\0' && *endptr == '\0') {
106  return result;
107  }
108  if (*nptr != '\0') {
109  std::cerr << "CastorText2DetIdConverter::getField-> Can not convert string " << nptr
110  << " to int. Bad symbol: " << *endptr << std::endl;
111  }
112  return 0;
113 }
114 
115 void CastorText2DetIdConverter::setField(int i, int fValue) {
116  char buffer[128];
117  sprintf(buffer, "%d", fValue);
118  if (i == 1)
119  field1 = buffer;
120  else if (i == 2)
121  field2 = buffer;
122  else
123  field3 = buffer;
124 }
125 
127  return flavorName + " " + field1 + " " + field2 + " " + field3;
128 }
static const HcalDetId Undefined
Definition: HcalDetId.h:273
int sector() const
get the sector (1-16)
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
int module() const
get the module (1-2 for EM, 1-12 for HAD)
Section section() const
get the section
CastorText2DetIdConverter(const std::string &fFlavor="NA", const std::string &fField1="0", const std::string &fField2="0", const std::string &fField3="0")
tuple result
Definition: mps_fire.py:311
bool init(const std::string &fFlavor, const std::string &fField1, const std::string &fField2, const std::string &fField3)
int zside() const
get the z-side of the cell (1/-1)
Definition: DetId.h:17
string section
Definition: vertexPlots.py:496
bool isHcalCastorDetId() const