CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalText2DetIdConverter.cc
Go to the documentation of this file.
1 
4 #include <stdlib.h>
5 #include <iostream>
6 #include <iomanip>
7 #include <cstdio>
8 
10 
16 
18 
19 namespace {
20  std::string strip (const std::string& fString) {
21  if (fString.empty ()) return fString;
22  int startIndex = fString.find_first_not_of(" \t\n");
23  int endIndex = fString.find_last_not_of(" \t\n");
24  return fString.substr(startIndex, (endIndex-startIndex)+1);
25  }
26 
27  int calibChannel (const std::string& fName) {
28  return
29  fName == "Mixer-High" ? 1 :
30  fName == "Mixer-Low" ? 2 :
31  fName == "Megatile" ? 3 :
32  fName == "Mixer-Scintillator" ? 4 :
33  fName == "RadDam1" ? 5 :
34  fName == "RadDam2" ? 6 :
35  fName == "RadDam3" ? 7 :
36  atoi(fName.c_str());
37  // 0;
38  }
39 }
40 
42  const std::string& fField2, const std::string& fField3) {
43  if (!init (fFlavor, fField1, fField2, fField3)) {
44  std::cerr << "HcalText2DetIdConverter::HcalText2DetIdConverter-> Can not initiate detId from items: "
45  << fFlavor << '/' << fField1 << '/' << fField2 << '/' << fField3 << std::endl;
46  throw cms::Exception("HcalGenDetId initialization error")
47  << " Can not initiate detId from items: "
48  << fFlavor << '/' << fField1 << '/' << fField2 << '/' << fField3 << std::endl;
49  }
50 }
51 
53  init (fId);
54 }
55 
57  return HcalGenericDetId (mId).isHcalDetId ();
58 }
59 
62 }
63 
66 }
67 
70 }
71 
73  bool result = true;
74  flavorName = "UNKNOWN";
75  mId = fId;
76  HcalGenericDetId genId (mId);
77  if (fId == HcalDetId::Undefined) {
78  flavorName = "NA";
79  }
80  else if (genId.isHcalDetId ()) {
81  HcalDetId hcalId (mId);
85  genId.genericSubdet () == HcalGenericDetId::HcalGenForward ? "HF" : "H_UNKNOWN";
86  setField (1, hcalId.ieta());
87  setField (2, hcalId.iphi());
88  setField (3, hcalId.depth());
89  }
90  else if (genId.isHcalTrigTowerDetId ()) {
91  HcalTrigTowerDetId triggerId (mId);
92  if (triggerId == HcalTrigTowerDetId::Undefined) {
93  flavorName = "NT";
94  }
95  else {
96  flavorName = "HT";
97  setField (1, triggerId.ieta());
98  setField (2, triggerId.iphi());
99  setField (3, 1);
100  }
101  }
102  else if (genId.isHcalZDCDetId ()) {
103  HcalZDCDetId zdcId (mId);
104  switch (zdcId.section()) {
105  case HcalZDCDetId::EM: flavorName = "ZDC_EM"; break;
106  case HcalZDCDetId::HAD: flavorName = "ZDC_HAD"; break;
107  case HcalZDCDetId::LUM: flavorName = "ZDC_LUM"; break;
108  default: result = false;
109  }
110  setField (1, zdcId.zside());
111  setField (2, zdcId.channel());
112  setField (3, -99);
113  }
114  else if (genId.isHcalCalibDetId ()) {
115  HcalCalibDetId calibId (mId);
117  switch (calibId.hcalSubdet()) {
118  case HcalBarrel: flavorName = "CALIB_HB"; break;
119  case HcalEndcap: flavorName = "CALIB_HE"; break;
120  case HcalOuter: flavorName = "CALIB_HO"; break;
121  case HcalForward: flavorName = "CALIB_HF"; break;
122  default: result = false;
123  }
124  setField (1, calibId.ieta());
125  setField (2, calibId.iphi());
126  setField (3, calibId.cboxChannel() );
127  } else if (calibId.calibFlavor()==HcalCalibDetId::HOCrosstalk) {
128  flavorName="HOX";
129  setField (1, calibId.ieta());
130  setField (2, calibId.iphi());
131  setField (3, -999);
132  }
133  }
134  else {
135  flavorName = "UNKNOWN_FLAVOR";
136  std::cerr << "HcalText2DetIdConverter::init-> Unknown detId: " << std::hex << std::showbase << mId.rawId() << std::endl;
137  result = false;
138  }
139  return result;
140 }
141 
142 
143 bool HcalText2DetIdConverter::init (const std::string& fFlavor, const std::string& fField1,
144  const std::string& fField2, const std::string& fField3) {
145  bool result = true;
146  flavorName = strip (fFlavor);
147  field1 = strip (fField1);
148  field2 = strip (fField2);
149  field3 = strip (fField3);
150  if (flavorName == "HB" ||
151  flavorName == "HE" ||
152  flavorName == "HF" ||
153  flavorName == "HO") {
154  HcalSubdetector sub = flavorName == "HB" ? HcalBarrel :
155  flavorName == "HE" ? HcalEndcap :
156  flavorName == "HO" ? HcalOuter :
157  HcalForward;
158  mId = HcalDetId (sub, getField (1), getField (2), getField (3));
159  }
160  else if (flavorName == "HT") {
162  }
163  else if (flavorName.find ("ZDC_") == 0) {
164  HcalZDCDetId::Section section = flavorName == "ZDC_EM" ? HcalZDCDetId::EM :
165  flavorName == "ZDC_HAD" ? HcalZDCDetId::HAD :
167  mId = HcalZDCDetId (section, getField (1)>0, getField (2));
168  }
169  else if (flavorName.find ("CALIB_") == 0) {
171  if (flavorName.find("HB")!=std::string::npos) sd=HcalBarrel;
172  else if (flavorName.find("HE")!=std::string::npos) sd=HcalEndcap;
173  else if (flavorName.find("HO")!=std::string::npos) sd=HcalOuter;
174  else if (flavorName.find("HF")!=std::string::npos) sd=HcalForward;
175 
176  int ieta=getField(1);
177  int iphi=getField(2);
178  int channel = calibChannel (field3);
179  mId = HcalCalibDetId (sd, ieta,iphi,channel);
180  }
181  else if (flavorName=="HOX") {
182  int ieta=getField(1);
183  int iphi=getField(2);
184  mId = HcalCalibDetId (ieta,iphi);
185  }
186  else if (flavorName == "NA") {
188  }
189  else {
190  std::cerr << "HcalText2DetIdConverter::init-> Unknown HcalDetId flavor: " << flavorName << std::endl;
191  result = false;
192  }
193  return result;
194 }
195 
196 
198  char* endptr;
199  const char* nptr = i == 1 ? field1.c_str() :
200  i == 2 ? field2.c_str() : field3.c_str();
201  long result = strtol (nptr, &endptr, 0);
202  if (*nptr != '\0' && *endptr == '\0') {
203  return result;
204  }
205  if (i == 2 && isHcalCalibDetId ()) {
206  int result = calibChannel (field2);
207  if (i) return result;
208  }
209  if (*nptr != '\0') {
210  std::cerr << "HcalText2DetIdConverter::getField-> Can not convert string "<< nptr << " to int. Bad symbol: " << *endptr << std::endl;
211  }
212  return 0;
213  }
214 
215 void HcalText2DetIdConverter::setField (int i, int fValue) {
216  char buffer [128];
217  sprintf (buffer, "%d", fValue);
218  if (i == 1) field1 = buffer;
219  else if (i == 2) field2 = buffer;
220  else field3 = buffer;
221 }
222 
224  return flavorName + " " + field1 + " " + field2 + " " + field3;
225 }
static const HcalDetId Undefined
Definition: HcalDetId.h:50
int i
Definition: DBlmapReader.cc:9
bool isHcalZDCDetId() const
CalibDetType calibFlavor() const
get the flavor of this calibration detid
int zside() const
get the z-side of the cell (1/-1)
Definition: HcalZDCDetId.h:34
int ieta() const
get the tower ieta
int ieta() const
get the rbx name (if relevant)
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
int depth() const
get the tower depth
Definition: HcalDetId.h:40
bool isHcalTrigTowerDetId() const
void setField(int i, int fValue)
tuple result
Definition: query.py:137
int ieta() const
get the cell ieta
Definition: HcalDetId.h:36
HcalSubdetector
Definition: HcalAssistant.h:31
HcalText2DetIdConverter(const std::string &fFlavor="NA", const std::string &fField1="0", const std::string &fField2="0", const std::string &fField3="0")
Section section() const
get the section
Definition: HcalZDCDetId.h:36
bool isHcalDetId() const
int iphi() const
get the low-edge iphi (if relevant)
bool init(const std::string &fFlavor, const std::string &fField1, const std::string &fField2, const std::string &fField3)
int iphi() const
get the cell iphi
Definition: HcalDetId.h:38
Definition: DetId.h:18
double sd
int channel() const
get the channel
Definition: HcalZDCDetId.h:40
static const HcalTrigTowerDetId Undefined
int cboxChannel() const
get the calibration box channel (if relevant)
bool isHcalCalibDetId() const
HcalSubdetector hcalSubdet() const
get the HcalSubdetector (if relevant)
HcalGenericSubdetector genericSubdet() const
int iphi() const
get the tower iphi