Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:46

0001 #ifndef DataFormats_MuonDetId_GEMDetId_h
0002 #define DataFormats_MuonDetId_GEMDetId_h
0003 
0004 /** \class GEMDetId
0005  *
0006  *  DetUnit identifier for GEMs
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   // in the detId there is space to go up to 5 stations. Only 3 implemented now (0,1,2)
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;  // LayerId = 0 is superChamber
0031   static constexpr int32_t maxLayerId0 = 6;
0032   static constexpr int32_t maxLayerId = 2;  // GE1/GE2 has 2 layers
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   /** Create a null detId */
0075   constexpr GEMDetId() : DetId(DetId::Muon, MuonSubdetId::GEM) {}
0076   /** Construct from a packed id. It is required that the Detector part of
0077       id is Muon and the SubDet part is GEM, otherwise an exception is thrown*/
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   /** Construct from a generic cell ID */
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   /// Construct from fully qualified identifier.
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   /** Assignment from a generic cell id */
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   /** Comparison operator */
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   /** Sort Operator based on the raw detector id */
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   /** Check the format */
0168   constexpr bool v11Format() const { return ((id_ & kGEMIdFormat) == 0); }
0169 
0170   /** Region id: 0 for Barrel Not in use, +/-1 For +/- Endcap */
0171   constexpr int region() const { return (static_cast<int>((id_ >> RegionStartBit) & RegionMask) + minRegionId); }
0172 
0173   /** Ring id: GEM are installed only on ring 1
0174       the ring is the group of chambers with same r (distance of beam axis)
0175       and increasing phi */
0176   constexpr int ring() const { return (static_cast<int>((id_ >> RingStartBit) & RingMask) + minRingId); }
0177 
0178   /** Station id : the station is the set of chambers at same disk */
0179   constexpr int station() const { return (static_cast<int>((id_ >> StationStartBit) & StationMask) + minStationId0); }
0180 
0181   /** Chamber id: it identifies a chamber in a ring it goes from 1 to 36
0182       for GE1 and GE2 and 1 to 18 for ME0 */
0183   constexpr int chamber() const {
0184     return (static_cast<int>((id_ >> ChamberStartBit) & ChamberMask) + (minChamberId + 1));
0185   }
0186 
0187   /** Layer id: each station have two layers of chambers for GE1 and GE2:
0188       layer 1 is the inner chamber and layer 2 is the outer chamber
0189       For ME0 there are 6 layers of chambers */
0190   constexpr int layer() const { return (static_cast<int>((id_ >> LayerStartBit) & LayerMask) + minLayerId); }
0191 
0192   /** EtaPartition id  (also known as eta partition): each chamber is divided along
0193       the strip direction in  several parts  (ietas) GEM up to 12 */
0194   constexpr int roll() const {
0195     return (static_cast<int>((id_ >> EtaPartitionStartBit) & EtaPartitionMask));  // value 0 is used as wild card
0196   }
0197 
0198   /** Return the corresponding EtaPartition id (same as roll) */
0199   constexpr int ieta() const {
0200     return (static_cast<int>((id_ >> EtaPartitionStartBit) & EtaPartitionMask));  // value 0 is used as wild card
0201   }
0202 
0203   /** Return the corresponding ChamberId */
0204   constexpr GEMDetId chamberId() const { return GEMDetId(id_ & chamberIdMask); }
0205 
0206   /** Return the corresponding superChamberId */
0207   constexpr GEMDetId superChamberId() const { return GEMDetId(id_ & superChamberIdMask); }
0208 
0209   /** Return the corresponding LayerId (mask eta partition) */
0210   constexpr GEMDetId layerId() const { return GEMDetId(id_ & chamberIdMask); }
0211 
0212   /** Return total # of layers for this type of detector */
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   // subsystem
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 };  // GEMDetId
0278 
0279 std::ostream& operator<<(std::ostream& os, const GEMDetId& id);
0280 
0281 #endif