File indexing completed on 2024-04-06 12:04:46
0001 #ifndef DataFormats_MuonDetId_GEMDetId_h
0002 #define DataFormats_MuonDetId_GEMDetId_h
0003
0004
0005
0006
0007
0008
0009
0010 #include "DataFormats/DetId/interface/DetId.h"
0011 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
0012 #include "DataFormats/MuonDetId/interface/GEMSubDetId.h"
0013 #include "FWCore/Utilities/interface/Exception.h"
0014
0015 #include <iosfwd>
0016 #include <iostream>
0017
0018 class GEMDetId : public DetId {
0019 public:
0020 static constexpr int32_t minRegionId = -1;
0021 static constexpr int32_t maxRegionId = 1;
0022 static constexpr int32_t minRingId = 1;
0023 static constexpr int32_t maxRingId = 3;
0024 static constexpr int32_t minStationId0 = 0;
0025 static constexpr int32_t minStationId = 1;
0026
0027 static constexpr int32_t maxStationId = 2;
0028 static constexpr int32_t minChamberId = 0;
0029 static constexpr int32_t maxChamberId = 36;
0030 static constexpr int32_t minLayerId = 0;
0031 static constexpr int32_t maxLayerId0 = 6;
0032 static constexpr int32_t maxLayerId = 2;
0033 static constexpr int32_t minEtaPartitionId = 0;
0034 static constexpr int32_t maxEtaPartitionId = 16;
0035 static constexpr int32_t minRollId = minEtaPartitionId;
0036 static constexpr int32_t maxRollId = maxEtaPartitionId;
0037
0038 private:
0039 static constexpr uint32_t RegionNumBits = 2;
0040 static constexpr uint32_t RegionStartBit = 0;
0041 static constexpr uint32_t RegionMask = 0x3;
0042 static constexpr uint32_t RingNumBits = 3;
0043 static constexpr uint32_t RingStartBit = RegionStartBit + RegionNumBits;
0044 static constexpr uint32_t RingMask = 0x7;
0045 static constexpr uint32_t StationNumBits = 3;
0046 static constexpr uint32_t StationStartBit = RingStartBit + RingNumBits;
0047 static constexpr uint32_t StationMask = 0x7;
0048 static constexpr uint32_t ChamberNumBits = 6;
0049 static constexpr uint32_t ChamberStartBit = StationStartBit + StationNumBits;
0050 static constexpr uint32_t ChamberStartBitM = RegionStartBit + RegionNumBits;
0051 static constexpr uint32_t ChamberMask = 0x3F;
0052 static constexpr uint32_t LayerNumBits = 5;
0053 static constexpr uint32_t LayerNumBitsP = 2;
0054 static constexpr uint32_t LayerStartBit = ChamberStartBit + ChamberNumBits;
0055 static constexpr uint32_t LayerStartBitM = ChamberStartBitM + ChamberNumBits;
0056 static constexpr uint32_t LayerMask = 0x1F;
0057 static constexpr uint32_t LayerMaskP = 0x3;
0058 static constexpr uint32_t EtaPartitionNumBits = 5;
0059 static constexpr uint32_t EtaPartitionStartBit = LayerStartBit + LayerNumBits;
0060 static constexpr uint32_t EtaPartitionStartBitP = LayerStartBit + LayerNumBitsP;
0061 static constexpr uint32_t EtaPartitionStartBitM = LayerStartBitM + LayerNumBits;
0062 static constexpr uint32_t EtaPartitionMask = 0x1F;
0063 static constexpr uint32_t FormatNumBits = 1;
0064 static constexpr uint32_t FormatStartBit = EtaPartitionStartBit + EtaPartitionNumBits;
0065 static constexpr uint32_t FormatMask = 0x1;
0066 static constexpr uint32_t kGEMIdFormat = 0x1000000;
0067 static constexpr uint32_t kMuonIdMask = 0xF0000000;
0068
0069 public:
0070 static constexpr uint32_t chamberIdMask = ~(EtaPartitionMask << EtaPartitionStartBit);
0071 static constexpr uint32_t superChamberIdMask = chamberIdMask + ~(LayerMask << LayerStartBit);
0072
0073 public:
0074
0075 constexpr GEMDetId() : DetId(DetId::Muon, MuonSubdetId::GEM) {}
0076
0077
0078 constexpr GEMDetId(uint32_t id) : DetId(id) {
0079 if (det() != DetId::Muon || (subdetId() != MuonSubdetId::GEM && subdetId() != MuonSubdetId::ME0))
0080 throw cms::Exception("InvalidDetId")
0081 << "GEMDetId ctor: det: " << det() << " subdet: " << subdetId() << " is not a valid GEM id\n";
0082
0083 if (v11Format())
0084 id_ = v12Form(id);
0085 }
0086
0087 constexpr GEMDetId(DetId id) : DetId(id) {
0088 if (det() != DetId::Muon || (subdetId() != MuonSubdetId::GEM && subdetId() != MuonSubdetId::ME0))
0089 throw cms::Exception("InvalidDetId")
0090 << "GEMDetId ctor: det: " << det() << " subdet: " << subdetId() << " is not a valid GEM id\n";
0091 if (v11Format())
0092 id_ = v12Form(id.rawId());
0093 }
0094
0095 constexpr GEMDetId(int region, int ring, int station, int layer, int chamber, int ieta)
0096 : DetId(DetId::Muon, MuonSubdetId::GEM) {
0097 if (region < minRegionId || region > maxRegionId || ring < minRingId || ring > maxRingId ||
0098 station < minStationId0 || station > maxStationId || layer < minLayerId || layer > maxLayerId0 ||
0099 chamber < minChamberId || chamber > maxChamberId || ieta < minEtaPartitionId || ieta > maxEtaPartitionId)
0100 throw cms::Exception("InvalidDetId")
0101 << "GEMDetId ctor: Invalid parameters: region " << region << " ring " << ring << " station " << station
0102 << " layer " << layer << " chamber " << chamber << " ieta " << ieta << std::endl;
0103
0104 int regionInBits = region - minRegionId;
0105 int ringInBits = ring - minRingId;
0106 int stationInBits = station - minStationId0;
0107 int layerInBits = layer - minLayerId;
0108 int chamberInBits = chamber - (minChamberId + 1);
0109 int ietaInBits = ieta;
0110
0111 id_ |= ((regionInBits & RegionMask) << RegionStartBit | (ringInBits & RingMask) << RingStartBit |
0112 (stationInBits & StationMask) << StationStartBit | (layerInBits & LayerMask) << LayerStartBit |
0113 (chamberInBits & ChamberMask) << ChamberStartBit | (ietaInBits & EtaPartitionMask) << EtaPartitionStartBit |
0114 kGEMIdFormat);
0115 }
0116
0117
0118 constexpr GEMDetId& operator=(const DetId& gen) {
0119 if (!gen.null()) {
0120 int subdet = gen.subdetId();
0121 if (gen.det() != Muon || (subdet != MuonSubdetId::GEM && subdet != MuonSubdetId::ME0))
0122 throw cms::Exception("InvalidDetId")
0123 << "GEMDetId ctor: Cannot assign GEMDetID from " << std::hex << gen.rawId() << std::dec;
0124 if (v11Format())
0125 id_ = v12Form(gen.rawId());
0126 else
0127 id_ = gen.rawId();
0128 } else {
0129 id_ = gen.rawId();
0130 }
0131 return (*this);
0132 }
0133
0134
0135 constexpr bool operator==(const GEMDetId& gen) const {
0136 uint32_t rawid = gen.rawId();
0137 if (rawid == id_)
0138 return true;
0139 int reg(0), ri(0), stn(-1), lay(0), chamb(0), iet(0);
0140 unpackId(rawid, reg, ri, stn, lay, chamb, iet);
0141 return (((id_ & kMuonIdMask) == (rawid & kMuonIdMask)) && (reg == region()) && (ri == ring()) &&
0142 (stn == station()) && (lay == layer()) && (chamb == chamber()) && (iet == ieta()));
0143 }
0144 constexpr bool operator!=(const GEMDetId& gen) const {
0145 uint32_t rawid = gen.rawId();
0146 if (rawid == id_)
0147 return false;
0148 int reg(0), ri(0), stn(-1), lay(0), chamb(0), iet(0);
0149 unpackId(rawid, reg, ri, stn, lay, chamb, iet);
0150 return (((id_ & kMuonIdMask) != (rawid & kMuonIdMask)) || (reg != region()) || (ri != ring()) ||
0151 (stn != station()) || (lay != layer()) || (chamb != chamber()) || (iet != ieta()));
0152 }
0153
0154
0155 constexpr bool operator<(const GEMDetId& r) const {
0156 if (r.station() == this->station()) {
0157 if (this->layer() == r.layer()) {
0158 return this->rawId() < r.rawId();
0159 } else {
0160 return (this->layer() < r.layer());
0161 }
0162 } else {
0163 return this->station() < r.station();
0164 }
0165 }
0166
0167
0168 constexpr bool v11Format() const { return ((id_ & kGEMIdFormat) == 0); }
0169
0170
0171 constexpr int region() const { return (static_cast<int>((id_ >> RegionStartBit) & RegionMask) + minRegionId); }
0172
0173
0174
0175
0176 constexpr int ring() const { return (static_cast<int>((id_ >> RingStartBit) & RingMask) + minRingId); }
0177
0178
0179 constexpr int station() const { return (static_cast<int>((id_ >> StationStartBit) & StationMask) + minStationId0); }
0180
0181
0182
0183 constexpr int chamber() const {
0184 return (static_cast<int>((id_ >> ChamberStartBit) & ChamberMask) + (minChamberId + 1));
0185 }
0186
0187
0188
0189
0190 constexpr int layer() const { return (static_cast<int>((id_ >> LayerStartBit) & LayerMask) + minLayerId); }
0191
0192
0193
0194 constexpr int roll() const {
0195 return (static_cast<int>((id_ >> EtaPartitionStartBit) & EtaPartitionMask));
0196 }
0197
0198
0199 constexpr int ieta() const {
0200 return (static_cast<int>((id_ >> EtaPartitionStartBit) & EtaPartitionMask));
0201 }
0202
0203
0204 constexpr GEMDetId chamberId() const { return GEMDetId(id_ & chamberIdMask); }
0205
0206
0207 constexpr GEMDetId superChamberId() const { return GEMDetId(id_ & superChamberIdMask); }
0208
0209
0210 constexpr GEMDetId layerId() const { return GEMDetId(id_ & chamberIdMask); }
0211
0212
0213 constexpr int nlayers() const {
0214 return ((station() == 0) ? maxLayerId0 : ((station() > maxStationId) ? 0 : maxLayerId));
0215 }
0216
0217 constexpr uint32_t v12Form() const { return v12Form(id_); }
0218
0219 constexpr static uint32_t v12Form(const uint32_t& inpid) {
0220 uint32_t rawid(inpid);
0221 if ((rawid & kGEMIdFormat) == 0) {
0222 int region(0), ring(0), station(-1), layer(0), chamber(0), ieta(0);
0223 unpackId(rawid, region, ring, station, layer, chamber, ieta);
0224 int regionInBits = region - minRegionId;
0225 int ringInBits = ring - minRingId;
0226 int stationInBits = station - minStationId0;
0227 int layerInBits = layer - minLayerId;
0228 int chamberInBits = chamber - (minChamberId + 1);
0229 int ietaInBits = ieta;
0230 rawid = (((DetId::Muon & DetId::kDetMask) << DetId::kDetOffset) |
0231 ((MuonSubdetId::GEM & DetId::kSubdetMask) << DetId::kSubdetOffset) |
0232 ((regionInBits & RegionMask) << RegionStartBit) | ((ringInBits & RingMask) << RingStartBit) |
0233 ((stationInBits & StationMask) << StationStartBit) | ((layerInBits & LayerMask) << LayerStartBit) |
0234 ((chamberInBits & ChamberMask) << ChamberStartBit) |
0235 ((ietaInBits & EtaPartitionMask) << EtaPartitionStartBit) | kGEMIdFormat);
0236 }
0237 return rawid;
0238 }
0239
0240
0241 GEMSubDetId::Station subsystem() const;
0242 bool isGE11() const;
0243 bool isGE21() const;
0244 bool isME0() const;
0245
0246 private:
0247 constexpr void v12FromV11(const uint32_t& rawid) { id_ = v12Form(rawid); }
0248
0249 constexpr static void unpackId(
0250 const uint32_t& rawid, int& region, int& ring, int& station, int& layer, int& chamber, int& ieta) {
0251 if (((rawid >> DetId::kDetOffset) & DetId::kDetMask) == DetId::Muon) {
0252 int subdet = ((rawid >> DetId::kSubdetOffset) & DetId::kSubdetMask);
0253 if (subdet == MuonSubdetId::GEM) {
0254 region = static_cast<int>(((rawid >> RegionStartBit) & RegionMask) + minRegionId);
0255 ring = (static_cast<int>((rawid >> RingStartBit) & RingMask) + minRingId);
0256 chamber = (static_cast<int>((rawid >> ChamberStartBit) & ChamberMask) + (minChamberId + 1));
0257 if ((rawid & kGEMIdFormat) == 0) {
0258 station = (static_cast<int>((rawid >> StationStartBit) & StationMask) + minStationId);
0259 layer = (static_cast<int>((rawid >> LayerStartBit) & LayerMaskP) + minLayerId);
0260 ieta = (static_cast<int>((rawid >> EtaPartitionStartBitP) & EtaPartitionMask));
0261 } else {
0262 station = (static_cast<int>((rawid >> StationStartBit) & StationMask) + minStationId0);
0263 layer = (static_cast<int>((rawid >> LayerStartBit) & LayerMask) + minLayerId);
0264 ieta = (static_cast<int>((rawid >> EtaPartitionStartBit) & EtaPartitionMask));
0265 }
0266 } else if (subdet == MuonSubdetId::ME0) {
0267 region = static_cast<int>(((rawid >> RegionStartBit) & RegionMask) + minRegionId);
0268 ring = 1;
0269 station = 0;
0270 chamber = (static_cast<int>((rawid >> ChamberStartBitM) & ChamberMask) + (minChamberId));
0271 layer = (static_cast<int>((rawid >> LayerStartBitM) & LayerMask) + minLayerId);
0272 ieta = (static_cast<int>((rawid >> EtaPartitionStartBitM) & EtaPartitionMask));
0273 }
0274 }
0275 }
0276
0277 };
0278
0279 std::ostream& operator<<(std::ostream& os, const GEMDetId& id);
0280
0281 #endif