File indexing completed on 2024-04-06 11:58:11
0001
0002
0003 #include <cstdlib>
0004 #include <iostream>
0005 #include <iomanip>
0006 #include <cstdio>
0007
0008 #include "FWCore/Utilities/interface/Exception.h"
0009
0010 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
0011 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
0012 #include "DataFormats/HcalDetId/interface/HcalCastorDetId.h"
0013
0014 #include "CalibFormats/CastorObjects/interface/CastorText2DetIdConverter.h"
0015
0016 namespace {
0017 std::string strip(const std::string& fString) {
0018 if (fString.empty())
0019 return fString;
0020 int startIndex = fString.find_first_not_of(" \t\n");
0021 int endIndex = fString.find_last_not_of(" \t\n");
0022 return fString.substr(startIndex, (endIndex - startIndex) + 1);
0023 }
0024 }
0025
0026 CastorText2DetIdConverter::CastorText2DetIdConverter(const std::string& fFlavor,
0027 const std::string& fField1,
0028 const std::string& fField2,
0029 const std::string& fField3) {
0030 if (!init(fFlavor, fField1, fField2, fField3)) {
0031 std::cerr << "CastorText2DetIdConverter::CastorText2DetIdConverter-> Can not initiate detId from items: " << fFlavor
0032 << '/' << fField1 << '/' << fField2 << '/' << fField3 << std::endl;
0033 throw cms::Exception("HcalGenDetId initialization error")
0034 << " Can not initiate detId from items: " << fFlavor << '/' << fField1 << '/' << fField2 << '/' << fField3
0035 << std::endl;
0036 }
0037 }
0038
0039 CastorText2DetIdConverter::CastorText2DetIdConverter(DetId fId) { init(fId); }
0040
0041 bool CastorText2DetIdConverter::isHcalCastorDetId() const { return HcalGenericDetId(mId).isHcalCastorDetId(); }
0042
0043 bool CastorText2DetIdConverter::init(DetId fId) {
0044 bool result = true;
0045 flavorName = "UNKNOWN";
0046 mId = fId;
0047 HcalGenericDetId genId(mId);
0048 if (fId == HcalDetId::Undefined) {
0049 flavorName = "NA";
0050 }
0051
0052 else if (genId.isHcalCastorDetId()) {
0053 HcalCastorDetId castorId(mId);
0054 switch (castorId.section()) {
0055 case HcalCastorDetId::EM:
0056 flavorName = "CASTOR_EM";
0057 break;
0058 case HcalCastorDetId::HAD:
0059 flavorName = "CASTOR_HAD";
0060 break;
0061 default:
0062 result = false;
0063 }
0064 setField(1, castorId.zside());
0065 setField(2, castorId.sector());
0066 setField(3, castorId.module());
0067 }
0068
0069 else {
0070 flavorName = "UNKNOWN_FLAVOR";
0071 std::cerr << "CastorText2DetIdConverter::init-> Unknown detId: " << std::hex << std::showbase << mId.rawId()
0072 << std::endl;
0073 result = false;
0074 }
0075 return result;
0076 }
0077
0078 bool CastorText2DetIdConverter::init(const std::string& fFlavor,
0079 const std::string& fField1,
0080 const std::string& fField2,
0081 const std::string& fField3) {
0082 bool result = true;
0083 flavorName = strip(fFlavor);
0084 field1 = strip(fField1);
0085 field2 = strip(fField2);
0086 field3 = strip(fField3);
0087 if (flavorName.find("CASTOR_") == 0) {
0088 HcalCastorDetId::Section section = flavorName == "CASTOR_EM" ? HcalCastorDetId::EM
0089 : flavorName == "CASTOR_HAD" ? HcalCastorDetId::HAD
0090 : HcalCastorDetId::Unknown;
0091 mId = HcalCastorDetId(section, getField(1) > 0, getField(2), getField(3));
0092 }
0093
0094 else {
0095 std::cerr << "CastorText2DetIdConverter::init-> Unknown DetId flavor: " << flavorName << std::endl;
0096 result = false;
0097 }
0098 return result;
0099 }
0100
0101 int CastorText2DetIdConverter::getField(int i) const {
0102 char* endptr;
0103 const char* nptr = i == 1 ? field1.c_str() : i == 2 ? field2.c_str() : field3.c_str();
0104 long result = strtol(nptr, &endptr, 0);
0105 if (*nptr != '\0' && *endptr == '\0') {
0106 return result;
0107 }
0108 if (*nptr != '\0') {
0109 std::cerr << "CastorText2DetIdConverter::getField-> Can not convert string " << nptr
0110 << " to int. Bad symbol: " << *endptr << std::endl;
0111 }
0112 return 0;
0113 }
0114
0115 void CastorText2DetIdConverter::setField(int i, int fValue) {
0116 char buffer[128];
0117 sprintf(buffer, "%d", fValue);
0118 if (i == 1)
0119 field1 = buffer;
0120 else if (i == 2)
0121 field2 = buffer;
0122 else
0123 field3 = buffer;
0124 }
0125
0126 std::string CastorText2DetIdConverter::toString() const {
0127 return flavorName + " " + field1 + " " + field2 + " " + field3;
0128 }