Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-11 04:36:47

0001 /** \class HcalText2DetIdConverter
0002     \author F.Ratnikov, UMd
0003 */
0004 #include <cstdlib>
0005 #include <iostream>
0006 #include <iomanip>
0007 #include <cstdio>
0008 
0009 #include "FWCore/Utilities/interface/Exception.h"
0010 
0011 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
0012 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0013 #include "DataFormats/HcalDetId/interface/HcalCalibDetId.h"
0014 #include "DataFormats/HcalDetId/interface/HcalTrigTowerDetId.h"
0015 #include "DataFormats/HcalDetId/interface/HcalZDCDetId.h"
0016 
0017 #include "CalibFormats/HcalObjects/interface/HcalText2DetIdConverter.h"
0018 
0019 namespace {
0020   std::string strip(const std::string& fString) {
0021     if (fString.empty())
0022       return fString;
0023     int startIndex = fString.find_first_not_of(" \t\n");
0024     int endIndex = fString.find_last_not_of(" \t\n");
0025     return fString.substr(startIndex, (endIndex - startIndex) + 1);
0026   }
0027 
0028   int calibChannel(const std::string& fName) {
0029     return fName == "Mixer-High"           ? 1
0030            : fName == "Mixer-Low"          ? 2
0031            : fName == "Megatile"           ? 3
0032            : fName == "Mixer-Scintillator" ? 4
0033            : fName == "RadDam1"            ? 5
0034            : fName == "RadDam2"            ? 6
0035            : fName == "RadDam3"            ? 7
0036                                            : atoi(fName.c_str());
0037     //      0;
0038   }
0039 }  // namespace
0040 
0041 HcalText2DetIdConverter::HcalText2DetIdConverter(const std::string& fFlavor,
0042                                                  const std::string& fField1,
0043                                                  const std::string& fField2,
0044                                                  const std::string& fField3) {
0045   if (!init(fFlavor, fField1, fField2, fField3)) {
0046     std::cerr << "HcalText2DetIdConverter::HcalText2DetIdConverter-> Can not initiate detId from items: " << fFlavor
0047               << '/' << fField1 << '/' << fField2 << '/' << fField3 << std::endl;
0048     throw cms::Exception("HcalGenDetId initialization error")
0049         << " Can not initiate detId from items: " << fFlavor << '/' << fField1 << '/' << fField2 << '/' << fField3
0050         << std::endl;
0051   }
0052 }
0053 
0054 HcalText2DetIdConverter::HcalText2DetIdConverter(DetId fId) { init(fId); }
0055 
0056 bool HcalText2DetIdConverter::isHcalDetId() const { return HcalGenericDetId(mId).isHcalDetId(); }
0057 
0058 bool HcalText2DetIdConverter::isHcalCalibDetId() const { return HcalGenericDetId(mId).isHcalCalibDetId(); }
0059 
0060 bool HcalText2DetIdConverter::isHcalTrigTowerDetId() const { return HcalGenericDetId(mId).isHcalTrigTowerDetId(); }
0061 
0062 bool HcalText2DetIdConverter::isHcalZDCDetId() const { return HcalGenericDetId(mId).isHcalZDCDetId(); }
0063 
0064 bool HcalText2DetIdConverter::init(DetId fId) {
0065   bool result = true;
0066   flavorName = "UNKNOWN";
0067   mId = fId;
0068   HcalGenericDetId genId(mId);
0069   if (fId == HcalDetId::Undefined) {
0070     flavorName = "NA";
0071   } else if (genId.isHcalDetId()) {
0072     HcalDetId hcalId(mId);
0073     flavorName = genId.genericSubdet() == HcalGenericDetId::HcalGenBarrel    ? "HB"
0074                  : genId.genericSubdet() == HcalGenericDetId::HcalGenEndcap  ? "HE"
0075                  : genId.genericSubdet() == HcalGenericDetId::HcalGenOuter   ? "HO"
0076                  : genId.genericSubdet() == HcalGenericDetId::HcalGenForward ? "HF"
0077                                                                              : "H_UNKNOWN";
0078     setField(1, hcalId.ieta());
0079     setField(2, hcalId.iphi());
0080     setField(3, hcalId.depth());
0081   } else if (genId.isHcalTrigTowerDetId()) {
0082     HcalTrigTowerDetId triggerId(mId);
0083     if (triggerId == HcalTrigTowerDetId::Undefined) {
0084       flavorName = "NT";
0085     } else {
0086       flavorName = "HT";
0087       setField(1, triggerId.ieta());
0088       setField(2, triggerId.iphi());
0089       setField(3, triggerId.version() * 10 + triggerId.depth());
0090       /*      
0091       if (triggerId.version() == 0) {
0092         setField (1, triggerId.ieta());
0093         setField (2, triggerId.iphi());
0094         setField (3, 1);
0095       } else if (triggerId.version() == 1) {
0096         setField (1, triggerId.ieta());
0097         setField (2, triggerId.iphi());
0098         setField (3, 10);  // We use the tens digit to indicate version
0099       } else {
0100         // Unknown version
0101       }
0102 */
0103     }
0104   } else if (genId.isHcalZDCDetId()) {
0105     HcalZDCDetId zdcId(mId);
0106     switch (zdcId.section()) {
0107       case HcalZDCDetId::EM:
0108         flavorName = "ZDC_EM";
0109         break;
0110       case HcalZDCDetId::HAD:
0111         flavorName = "ZDC_HAD";
0112         break;
0113       case HcalZDCDetId::LUM:
0114         flavorName = "ZDC_LUM";
0115         break;
0116       case HcalZDCDetId::RPD:
0117         flavorName = "ZDC_RPD";
0118         break;
0119       default:
0120         result = false;
0121     }
0122     setField(1, zdcId.zside());
0123     setField(2, zdcId.channel());
0124     setField(3, -99);
0125   } else if (genId.isHcalCalibDetId()) {
0126     HcalCalibDetId calibId(mId);
0127     if (calibId.calibFlavor() == HcalCalibDetId::CalibrationBox) {
0128       switch (calibId.hcalSubdet()) {
0129         case HcalBarrel:
0130           flavorName = "CALIB_HB";
0131           break;
0132         case HcalEndcap:
0133           flavorName = "CALIB_HE";
0134           break;
0135         case HcalOuter:
0136           flavorName = "CALIB_HO";
0137           break;
0138         case HcalForward:
0139           flavorName = "CALIB_HF";
0140           break;
0141         default:
0142           result = false;
0143       }
0144       setField(1, calibId.ieta());
0145       setField(2, calibId.iphi());
0146       setField(3, calibId.cboxChannel());
0147     } else if (calibId.calibFlavor() == HcalCalibDetId::HOCrosstalk) {
0148       flavorName = "HOX";
0149       setField(1, calibId.ieta());
0150       setField(2, calibId.iphi());
0151       setField(3, -999);
0152     } else if (calibId.calibFlavor() == HcalCalibDetId::uMNqie) {
0153       flavorName = "UMNQIE";
0154       setField(1, calibId.channel());
0155       setField(2, -999);
0156       setField(3, -999);
0157     } else if (calibId.calibFlavor() == HcalCalibDetId::LASERMON) {
0158       flavorName = "LASMON";
0159       setField(1, calibId.ieta());
0160       setField(2, calibId.iphi());
0161       setField(3, calibId.cboxChannel());
0162     } else if (calibId.calibFlavor() == HcalCalibDetId::CastorRadFacility) {
0163       flavorName = "CRF";
0164       setField(1, calibId.rm());
0165       setField(2, calibId.fiber());
0166       setField(3, calibId.channel());
0167     } else if (calibId.calibFlavor() == HcalCalibDetId::HBX) {
0168       flavorName = "HBX";
0169       setField(1, calibId.ieta());
0170       setField(2, calibId.iphi());
0171       setField(3, -999);
0172     } else if (calibId.calibFlavor() == HcalCalibDetId::HEX) {
0173       flavorName = "HEX";
0174       setField(1, calibId.ieta());
0175       setField(2, calibId.iphi());
0176       setField(3, -999);
0177     }
0178   } else {
0179     flavorName = "UNKNOWN_FLAVOR";
0180     std::cerr << "HcalText2DetIdConverter::init-> Unknown detId: " << std::hex << std::showbase << mId.rawId()
0181               << std::endl;
0182     result = false;
0183   }
0184   return result;
0185 }
0186 
0187 bool HcalText2DetIdConverter::init(const std::string& fFlavor,
0188                                    const std::string& fField1,
0189                                    const std::string& fField2,
0190                                    const std::string& fField3) {
0191   bool result = true;
0192   flavorName = strip(fFlavor);
0193   field1 = strip(fField1);
0194   field2 = strip(fField2);
0195   field3 = strip(fField3);
0196   if (flavorName == "HB" || flavorName == "HE" || flavorName == "HF" || flavorName == "HO") {
0197     HcalSubdetector sub = flavorName == "HB"   ? HcalBarrel
0198                           : flavorName == "HE" ? HcalEndcap
0199                           : flavorName == "HO" ? HcalOuter
0200                                                : HcalForward;
0201     mId = HcalDetId(sub, getField(1), getField(2), getField(3));
0202   } else if (flavorName == "HT") {
0203     // We use the depth to signal the "version" being used (RCT or 1x1 HF). RCT
0204     // has a 0 in the 10s digit, whereas 1x1 has a 1. The ones digit is still
0205     // used to indicate depth, although in the 1x1 case this must be 0, so we
0206     // set it as such.
0207     mId = HcalTrigTowerDetId(getField(1), getField(2), getField(3));
0208     /*
0209     const int depth_field = getField(3);
0210     const int ones = depth_field % 10;
0211     const int tens = (depth_field - ones) / 10;
0212     if (tens == 0) {
0213       const int depth = ones;
0214       const int version = 0;
0215       mId = HcalTrigTowerDetId (getField (1), getField (2), depth, version);
0216     } else if (tens == 1) {
0217       const int depth = 0;
0218       const int version = 1;
0219       mId = HcalTrigTowerDetId(getField(1), getField(2), depth, version);
0220     } else {
0221       // Undefined version!
0222     }
0223 */
0224   } else if (flavorName.find("ZDC_") == 0) {
0225     HcalZDCDetId::Section section = HcalZDCDetId::Unknown;
0226     if (flavorName == "ZDC_EM")
0227       section = HcalZDCDetId::EM;
0228     else if (flavorName == "ZDC_HAD")
0229       section = HcalZDCDetId::HAD;
0230     else if (flavorName == "ZDC_LUM")
0231       section = HcalZDCDetId::LUM;
0232     else if (flavorName == "ZDC_RPD")
0233       section = HcalZDCDetId::RPD;
0234     mId = HcalZDCDetId(section, getField(1) > 0, getField(2));
0235   } else if (flavorName.find("CALIB_") == 0) {
0236     HcalSubdetector sd = HcalOther;
0237     if (flavorName.find("HB") != std::string::npos)
0238       sd = HcalBarrel;
0239     else if (flavorName.find("HE") != std::string::npos)
0240       sd = HcalEndcap;
0241     else if (flavorName.find("HO") != std::string::npos)
0242       sd = HcalOuter;
0243     else if (flavorName.find("HF") != std::string::npos)
0244       sd = HcalForward;
0245 
0246     int ieta = getField(1);
0247     int iphi = getField(2);
0248     int channel = calibChannel(field3);
0249     mId = HcalCalibDetId(sd, ieta, iphi, channel);
0250   } else if (flavorName == "HOX" || flavorName == "HBX" || flavorName == "HEX") {
0251     int ieta = getField(1);
0252     int iphi = getField(2);
0253     mId = (flavorName == "HOX") ? (HcalCalibDetId(HcalCalibDetId::HOCrosstalk, ieta, iphi))
0254                                 : ((flavorName == "HBX") ? (HcalCalibDetId(HcalCalibDetId::HBX, ieta, iphi))
0255                                                          : (HcalCalibDetId(HcalCalibDetId::HEX, ieta, iphi)));
0256   } else if (flavorName == "UMNQIE") {
0257     int channel = getField(1);
0258     mId = HcalCalibDetId(HcalCalibDetId::uMNqie, channel);
0259   } else if (flavorName == "LASMON") {
0260     int ieta = getField(1);
0261     int iphi = getField(2);
0262     int channel = getField(3);
0263     mId = HcalCalibDetId(HcalCalibDetId::LASERMON, ieta, iphi, channel);
0264   } else if (flavorName == "CRF") {
0265     int rm = getField(1);
0266     int fiber = getField(2);
0267     int channel = getField(3);
0268     mId = HcalCalibDetId(HcalCalibDetId::CastorRadFacility, rm, fiber, channel);
0269   } else if (flavorName == "NA") {
0270     mId = HcalDetId::Undefined;
0271   } else {
0272     std::cerr << "HcalText2DetIdConverter::init-> Unknown HcalDetId flavor: " << flavorName << std::endl;
0273     result = false;
0274   }
0275   return result;
0276 }
0277 
0278 int HcalText2DetIdConverter::getField(int i) const {
0279   char* endptr;
0280   const char* nptr = i == 1 ? field1.c_str() : i == 2 ? field2.c_str() : field3.c_str();
0281   long result = strtol(nptr, &endptr, 0);
0282   if (*nptr != '\0' && *endptr == '\0') {
0283     return result;
0284   }
0285   if (i == 2 && isHcalCalibDetId()) {
0286     int result = calibChannel(field2);
0287     if (i)
0288       return result;
0289   }
0290   if (*nptr != '\0') {
0291     std::cerr << "HcalText2DetIdConverter::getField-> Can not convert string " << nptr
0292               << " to int. Bad symbol: " << *endptr << std::endl;
0293   }
0294   return 0;
0295 }
0296 
0297 void HcalText2DetIdConverter::setField(int i, int fValue) {
0298   char buffer[128];
0299   sprintf(buffer, "%d", fValue);
0300   if (i == 1)
0301     field1 = buffer;
0302   else if (i == 2)
0303     field2 = buffer;
0304   else
0305     field3 = buffer;
0306 }
0307 
0308 std::string HcalText2DetIdConverter::toString() const {
0309   return flavorName + " " + field1 + " " + field2 + " " + field3;
0310 }