CMS 3D CMS Logo

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 ()) return fString;
19  int startIndex = fString.find_first_not_of(" \t\n");
20  int endIndex = fString.find_last_not_of(" \t\n");
21  return fString.substr(startIndex, (endIndex-startIndex)+1);
22  }
23 }
24 
26  const std::string& fField2, const std::string& fField3) {
27  if (!init (fFlavor, fField1, fField2, fField3)) {
28  std::cerr << "CastorText2DetIdConverter::CastorText2DetIdConverter-> Can not initiate detId from items: "
29  << fFlavor << '/' << fField1 << '/' << fField2 << '/' << fField3 << std::endl;
30  throw cms::Exception("HcalGenDetId initialization error")
31  << " Can not initiate detId from items: "
32  << fFlavor << '/' << fField1 << '/' << fField2 << '/' << fField3 << std::endl;
33  }
34 }
35 
37  init (fId);
38 }
39 
42 }
43 
45  bool result = true;
46  flavorName = "UNKNOWN";
47  mId = fId;
48  HcalGenericDetId genId (mId);
49  if (fId == HcalDetId::Undefined) {
50  flavorName = "NA";
51  }
52 
53  else if (genId.isHcalCastorDetId ()) {
54  HcalCastorDetId castorId (mId);
55  switch (castorId.section()) {
56  case HcalCastorDetId::EM: flavorName = "CASTOR_EM"; break;
57  case HcalCastorDetId::HAD: flavorName = "CASTOR_HAD"; break;
58  default: result = false;
59  }
60  setField (1, castorId.zside());
61  setField (2, castorId.sector());
62  setField (3, castorId.module());
63  }
64 
65  else {
66  flavorName = "UNKNOWN_FLAVOR";
67  std::cerr << "CastorText2DetIdConverter::init-> Unknown detId: " << std::hex << std::showbase << mId.rawId() << std::endl;
68  result = false;
69  }
70  return result;
71 }
72 
73 
74 bool CastorText2DetIdConverter::init (const std::string& fFlavor, const std::string& fField1,
75  const std::string& fField2, const std::string& fField3) {
76  bool result = true;
77  flavorName = strip (fFlavor);
78  field1 = strip (fField1);
79  field2 = strip (fField2);
80  field3 = strip (fField3);
81  if (flavorName.find ("CASTOR_") == 0) {
84  mId = HcalCastorDetId (section, getField (1)>0, getField (2), getField(3));
85  }
86 
87  else {
88  std::cerr << "CastorText2DetIdConverter::init-> Unknown DetId flavor: " << flavorName << std::endl;
89  result = false;
90  }
91  return result;
92 }
93 
94 
96  char* endptr;
97  const char* nptr = i == 1 ? field1.c_str() :
98  i == 2 ? field2.c_str() : field3.c_str();
99  long result = strtol (nptr, &endptr, 0);
100  if (*nptr != '\0' && *endptr == '\0') {
101  return result;
102  }
103  if (*nptr != '\0') {
104  std::cerr << "CastorText2DetIdConverter::getField-> Can not convert string "<< nptr << " to int. Bad symbol: " << *endptr << std::endl;
105  }
106  return 0;
107 }
108 
109 void CastorText2DetIdConverter::setField (int i, int fValue) {
110  char buffer [128];
111  sprintf (buffer, "%d", fValue);
112  if (i == 1) field1 = buffer;
113  else if (i == 2) field2 = buffer;
114  else field3 = buffer;
115 }
116 
118  return flavorName + " " + field1 + " " + field2 + " " + field3;
119 }
static const HcalDetId Undefined
Definition: HcalDetId.h:268
int sector() const
get the sector (1-16)
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:47
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")
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:18
bool isHcalCastorDetId() const