File indexing completed on 2024-05-29 04:20:23
0001 #ifndef DataFormats_HcalDetId_HcalZDCDetId_h_included
0002 #define DataFormats_HcalDetId_HcalZDCDetId_h_included 1
0003
0004 #include <ostream>
0005 #include "DataFormats/DetId/interface/DetId.h"
0006 #include "FWCore/Utilities/interface/Exception.h"
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 class HcalZDCDetId : public DetId {
0025 public:
0026 static constexpr uint32_t kZDCChannelMask1 = 0xF;
0027 static constexpr uint32_t kZDCChannelMask2 = 0x3F;
0028 static constexpr uint32_t kZDCSectionMask = 0x3;
0029 static constexpr uint32_t kZDCSectionOffset = 4;
0030 static constexpr uint32_t kZDCZsideMask = 0x40;
0031 static constexpr uint32_t kZDCRPDMask = 0x80;
0032 static constexpr uint32_t kZDCnewFormat = 0x100;
0033 enum Section { Unknown = 0, EM = 1, HAD = 2, LUM = 3, RPD = 4 };
0034
0035 static constexpr int32_t SubdetectorId = 2;
0036
0037 static constexpr int32_t kDepEM = 5;
0038 static constexpr int32_t kDepHAD = 4;
0039 static constexpr int32_t kDepLUM = 2;
0040 static constexpr int32_t kDepRPD = 16;
0041 static constexpr int32_t kDepRun1 = (kDepEM + kDepHAD + kDepLUM);
0042 static constexpr int32_t kDepTot = (kDepRun1 + kDepRPD);
0043 static constexpr int32_t kDepRun3 = kDepTot;
0044
0045
0046 constexpr HcalZDCDetId() : DetId() {}
0047
0048 constexpr HcalZDCDetId(uint32_t rawid) : DetId(rawid) {}
0049
0050 constexpr HcalZDCDetId(Section section, bool true_for_positive_eta, int32_t channel) {
0051 id_ = packHcalZDCDetId(section, true_for_positive_eta, channel);
0052 }
0053
0054 constexpr HcalZDCDetId(const DetId& gen) {
0055 if (!gen.null() && (gen.det() != Calo || gen.subdetId() != SubdetectorId)) {
0056 throw cms::Exception("Invalid DetId")
0057 << "Cannot initialize ZDCDetId from " << std::hex << gen.rawId() << std::dec;
0058 }
0059 id_ = newForm(gen.rawId());
0060 }
0061
0062 constexpr HcalZDCDetId& operator=(const DetId& gen) {
0063 if (!gen.null() && (gen.det() != Calo || gen.subdetId() != SubdetectorId)) {
0064 throw cms::Exception("Invalid DetId") << "Cannot assign ZDCDetId from " << std::hex << gen.rawId() << std::dec;
0065 }
0066 id_ = newForm(gen.rawId());
0067 return *this;
0068 }
0069
0070 constexpr bool operator==(DetId gen) const {
0071 if (gen.rawId() == id_) {
0072 return true;
0073 } else {
0074 uint32_t id1 = newForm(gen.rawId());
0075 uint32_t id2 = newForm(id_);
0076 return (id1 == id2);
0077 }
0078 }
0079 constexpr bool operator!=(DetId gen) const {
0080 if (gen.rawId() != id_) {
0081 return true;
0082 } else {
0083 uint32_t id1 = newForm(gen.rawId());
0084 uint32_t id2 = newForm(id_);
0085 return (id1 != id2);
0086 }
0087 }
0088
0089
0090 constexpr int32_t zside() const { return ((id_ & kZDCZsideMask) ? (1) : (-1)); }
0091
0092 constexpr Section section() const {
0093 uint32_t id = newForm(id_);
0094 if (id & kZDCRPDMask)
0095 return RPD;
0096 else
0097 return (Section)((id >> kZDCSectionOffset) & kZDCSectionMask);
0098 }
0099
0100 constexpr int32_t depth() const {
0101 const int se(section());
0102 if (se == EM)
0103 return 1;
0104 else if (se == HAD)
0105 return (channel() + 2);
0106 else if (se == RPD)
0107 return 2;
0108 else
0109 return channel();
0110 }
0111
0112 constexpr int32_t channel() const {
0113 const int32_t se(section());
0114 uint32_t id = newForm(id_);
0115 if (se == RPD)
0116 return (1 + (id & kZDCChannelMask2));
0117 else
0118 return (id & kZDCChannelMask1);
0119 }
0120
0121 constexpr static bool newFormat(const uint32_t& di) { return (di & kZDCnewFormat); }
0122 constexpr static uint32_t newForm(const uint32_t& di) {
0123 uint32_t id(di);
0124 if (!newFormat(id)) {
0125 Section se(Unknown);
0126 bool zside(true);
0127 int32_t channel(0);
0128 unpackHcalZDCDetId(id, se, zside, channel);
0129 id = packHcalZDCDetId(se, zside, channel);
0130 }
0131 return id;
0132 }
0133
0134 constexpr uint32_t denseIndex() const {
0135 const int32_t se(section());
0136 uint32_t di =
0137 (channel() - 1 +
0138 (se == RPD ? 2 * kDepRun1 + (zside() < 0 ? 0 : kDepRPD)
0139 : ((zside() < 0 ? 0 : kDepRun1) + (se == HAD ? kDepEM : (se == LUM ? kDepEM + kDepHAD : 0)))));
0140 return di;
0141 }
0142
0143 constexpr static bool validDenseIndex(const uint32_t& di) { return (di < kSizeForDenseIndexing); }
0144
0145 constexpr static HcalZDCDetId detIdFromDenseIndex(uint32_t di) {
0146 if (validDenseIndex(di)) {
0147 bool lz(false);
0148 uint32_t dp(0);
0149 Section se(Unknown);
0150 if (di >= 2 * kDepRun1) {
0151 lz = (di >= (kDepRun1 + kDepTot));
0152 se = RPD;
0153 dp = 1 + ((di - 2 * kDepRun1) % kDepRPD);
0154 } else {
0155 lz = (di >= kDepRun1);
0156 uint32_t in = (di % kDepRun1);
0157 se = (in < kDepEM ? EM : (in < kDepEM + kDepHAD ? HAD : LUM));
0158 dp = (se == EM ? in + 1 : (se == HAD ? in - kDepEM + 1 : in - kDepEM - kDepHAD + 1));
0159 }
0160 return HcalZDCDetId(se, lz, dp);
0161 } else {
0162 return HcalZDCDetId();
0163 }
0164 }
0165
0166 constexpr static bool validDetId(Section se, int32_t dp) {
0167 bool flag = (dp >= 1 && (((se == EM) && (dp <= kDepEM)) || ((se == HAD) && (dp <= kDepHAD)) ||
0168 ((se == LUM) && (dp <= kDepLUM)) || ((se == RPD) && (dp <= kDepRPD))));
0169 return flag;
0170 }
0171
0172 private:
0173 constexpr static uint32_t packHcalZDCDetId(const Section& se, const bool& zside, const int32_t& channel) {
0174 uint32_t id = DetId(DetId::Calo, SubdetectorId);
0175 id |= kZDCnewFormat;
0176 if (se == RPD) {
0177 id |= kZDCRPDMask;
0178 id |= ((channel - 1) & kZDCChannelMask2);
0179 } else {
0180 id |= (se & kZDCSectionMask) << kZDCSectionOffset;
0181 id |= (channel & kZDCChannelMask1);
0182 }
0183 if (zside)
0184 id |= kZDCZsideMask;
0185 return id;
0186 }
0187
0188 constexpr static void unpackHcalZDCDetId(const uint32_t& id, Section& se, bool& zside, int32_t& channel) {
0189 if (id & kZDCnewFormat) {
0190 se = (id & kZDCRPDMask) ? RPD : (Section)((id >> kZDCSectionOffset) & kZDCSectionMask);
0191 channel = (se == RPD) ? (1 + (id & kZDCChannelMask2)) : (id & kZDCChannelMask1);
0192 zside = (id & kZDCZsideMask);
0193 } else {
0194 se = (id & kZDCRPDMask) ? RPD : (Section)((id >> kZDCSectionOffset) & kZDCSectionMask);
0195 channel = (se == RPD) ? (1 + (id & kZDCChannelMask1)) : (id & kZDCChannelMask1);
0196 zside = (id & kZDCZsideMask);
0197 }
0198 }
0199
0200 public:
0201 constexpr static int32_t kSizeForDenseIndexingRun1 = 2 * kDepRun1;
0202 constexpr static int32_t kSizeForDenseIndexingRun3 = 2 * kDepRun3;
0203 constexpr static int32_t kSizeForDenseIndexing = kSizeForDenseIndexingRun1;
0204 };
0205
0206 std::ostream& operator<<(std::ostream&, const HcalZDCDetId& id);
0207
0208 #endif