Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef DATAFORMATS_HCALDETID_HCALDETID_H
0002 #define DATAFORMATS_HCALDETID_HCALDETID_H 1
0003 
0004 #include <iosfwd>
0005 #include "DataFormats/DetId/interface/DetId.h"
0006 #include "DataFormats/HcalDetId/interface/HcalSubdetector.h"
0007 #include "FWCore/Utilities/interface/Exception.h"
0008 
0009 /** \class HcalDetId
0010  *  Cell identifier class for the HCAL subdetectors, precision readout cells only
0011  */
0012 class HcalDetId : public DetId {
0013 public:
0014   static constexpr uint32_t kHcalPhiMask1 = 0x7F;
0015   static constexpr uint32_t kHcalPhiMask2 = 0x3FF;
0016   static constexpr uint32_t kHcalEtaOffset1 = 7;
0017   static constexpr uint32_t kHcalEtaOffset2 = 10;
0018   static constexpr uint32_t kHcalEtaMask1 = 0x3F;
0019   static constexpr uint32_t kHcalEtaMask2 = 0x1FF;
0020   static constexpr uint32_t kHcalZsideMask1 = 0x2000;
0021   static constexpr uint32_t kHcalZsideMask2 = 0x80000;
0022   static constexpr uint32_t kHcalDepthOffset1 = 14;
0023   static constexpr uint32_t kHcalDepthOffset2 = 20;
0024   static constexpr uint32_t kHcalDepthMask1 = 0x1F;
0025   static constexpr uint32_t kHcalDepthMask2 = 0xF;
0026   static constexpr uint32_t kHcalDepthSet1 = 0x1C000;
0027   static constexpr uint32_t kHcalDepthSet2 = 0xF00000;
0028   static constexpr uint32_t kHcalIdFormat2 = 0x1000000;
0029   static constexpr uint32_t kHcalIdMask = 0xFE000000;
0030 
0031 public:
0032   /** Create a null cellid*/
0033   constexpr HcalDetId() : DetId() {}
0034   /** Create cellid from raw id (0=invalid tower id) */
0035   constexpr HcalDetId(uint32_t rawid) {
0036     if ((DetId::Detector(rawid >> DetId::kDetOffset) & DetId::kDetMask) != Hcal) {
0037       id_ = rawid;
0038     } else {
0039       HcalSubdetector subdet = (HcalSubdetector)((rawid >> DetId::kSubdetOffset) & DetId::kSubdetMask);
0040       if ((subdet == HcalBarrel) || (subdet == HcalEndcap) || (subdet == HcalOuter) || (subdet == HcalForward)) {
0041         id_ = newForm(rawid);
0042       } else {
0043         id_ = rawid;
0044       }
0045     }
0046   }
0047 
0048   /** Constructor from subdetector, signed tower ieta,iphi,and depth */
0049   constexpr HcalDetId(HcalSubdetector subdet, int tower_ieta, int tower_iphi, int depth) : DetId(Hcal, subdet) {
0050     // (no checking at this point!)
0051     id_ |=
0052         (kHcalIdFormat2) | ((depth & kHcalDepthMask2) << kHcalDepthOffset2) |
0053         ((tower_ieta > 0) ? (kHcalZsideMask2 | (tower_ieta << kHcalEtaOffset2)) : ((-tower_ieta) << kHcalEtaOffset2)) |
0054         (tower_iphi & kHcalPhiMask2);
0055   }
0056   /** Constructor from a generic cell id */
0057   constexpr HcalDetId(const DetId& gen) {
0058     if (!gen.null()) {
0059       HcalSubdetector subdet = (HcalSubdetector(gen.subdetId()));
0060       if (gen.det() != Hcal || (subdet != HcalBarrel && subdet != HcalEndcap && subdet != HcalOuter &&
0061                                 subdet != HcalForward && subdet != HcalTriggerTower && subdet != HcalOther)) {
0062         throw cms::Exception("Invalid DetId")
0063             << "Cannot initialize HcalDetId from " << std::hex << gen.rawId() << std::dec;
0064       }
0065       if ((subdet == HcalBarrel) || (subdet == HcalEndcap) || (subdet == HcalOuter) || (subdet == HcalForward)) {
0066         id_ = newForm(gen.rawId());
0067       } else {
0068         id_ = gen.rawId();
0069       }
0070     } else {
0071       id_ = gen.rawId();
0072     }
0073   }
0074   /** Assignment from a generic cell id */
0075   constexpr HcalDetId& operator=(const DetId& gen) {
0076     if (!gen.null()) {
0077       HcalSubdetector subdet = (HcalSubdetector(gen.subdetId()));
0078       if (gen.det() != Hcal || (subdet != HcalBarrel && subdet != HcalEndcap && subdet != HcalOuter &&
0079                                 subdet != HcalForward && subdet != HcalTriggerTower && subdet != HcalOther)) {
0080         throw cms::Exception("Invalid DetId") << "Cannot assign HcalDetId from " << std::hex << gen.rawId() << std::dec;
0081       }
0082       if ((subdet == HcalBarrel) || (subdet == HcalEndcap) || (subdet == HcalOuter) || (subdet == HcalForward)) {
0083         id_ = newForm(gen.rawId());
0084       } else {
0085         id_ = gen.rawId();
0086       }
0087     } else {
0088       id_ = gen.rawId();
0089     }
0090     return (*this);
0091   }
0092 
0093   /** Comparison operator */
0094   constexpr bool operator==(DetId gen) const {
0095     uint32_t rawid = gen.rawId();
0096     if (rawid == id_)
0097       return true;
0098     int zsid{0}, eta{0}, phi{0}, dep{0};
0099     unpackId(rawid, zsid, eta, phi, dep);
0100     bool result = (((id_ & kHcalIdMask) == (rawid & kHcalIdMask)) && (zsid == zside()) && (eta == ietaAbs()) &&
0101                    (phi == iphi()) && (dep == depth()));
0102     return result;
0103   }
0104 
0105   constexpr bool operator!=(DetId gen) const {
0106     uint32_t rawid = gen.rawId();
0107     if (rawid == id_)
0108       return false;
0109     int zsid{0}, eta{0}, phi{0}, dep{0};
0110     unpackId(rawid, zsid, eta, phi, dep);
0111     bool result = (((id_ & kHcalIdMask) != (rawid & kHcalIdMask)) || (zsid != zside()) || (eta != ietaAbs()) ||
0112                    (phi != iphi()) || (dep != depth()));
0113     return result;
0114   }
0115 
0116   constexpr bool operator<(DetId gen) const {
0117     uint32_t rawid = gen.rawId();
0118     if ((rawid & kHcalIdFormat2) == (id_ & kHcalIdFormat2)) {
0119       return id_ < rawid;
0120     } else {
0121       int zsid{0}, eta{0}, phi{0}, dep{0};
0122       unpackId(rawid, zsid, eta, phi, dep);
0123       rawid &= kHcalIdMask;
0124       if (oldFormat()) {
0125         rawid |= (((dep & kHcalDepthMask1) << kHcalDepthOffset1) |
0126                   ((zsid > 0) ? (kHcalZsideMask1 | (eta << kHcalEtaOffset1)) : ((eta) << kHcalEtaOffset1)) |
0127                   (phi & kHcalPhiMask1));
0128       } else {
0129         rawid |= ((kHcalIdFormat2) | ((dep & kHcalDepthMask2) << kHcalDepthOffset2) |
0130                   ((zsid > 0) ? (kHcalZsideMask2 | (eta << kHcalEtaOffset2)) : ((eta) << kHcalEtaOffset2)) |
0131                   (phi & kHcalPhiMask2));
0132       }
0133       return (id_ < rawid);
0134     }
0135   }
0136 
0137   /// get the subdetector
0138   constexpr HcalSubdetector subdet() const { return (HcalSubdetector)(subdetId()); }
0139   constexpr bool oldFormat() const { return ((id_ & kHcalIdFormat2) == 0) ? (true) : (false); }
0140   /// get the z-side of the cell (1/-1)
0141   constexpr int zside() const {
0142     if (oldFormat())
0143       return (id_ & kHcalZsideMask1) ? (1) : (-1);
0144     else
0145       return (id_ & kHcalZsideMask2) ? (1) : (-1);
0146   }
0147   /// get the absolute value of the cell ieta
0148   constexpr int ietaAbs() const {
0149     if (oldFormat())
0150       return (id_ >> kHcalEtaOffset1) & kHcalEtaMask1;
0151     else
0152       return (id_ >> kHcalEtaOffset2) & kHcalEtaMask2;
0153   }
0154   /// get the cell ieta
0155   constexpr int ieta() const { return zside() * ietaAbs(); }
0156   /// get the cell iphi
0157   constexpr int iphi() const {
0158     if (oldFormat())
0159       return id_ & kHcalPhiMask1;
0160     else
0161       return id_ & kHcalPhiMask2;
0162   }
0163   /// get the tower depth
0164   constexpr int depth() const {
0165     if (oldFormat())
0166       return (id_ >> kHcalDepthOffset1) & kHcalDepthMask1;
0167     else
0168       return (id_ >> kHcalDepthOffset2) & kHcalDepthMask2;
0169   }
0170   /// get full depth information for HF
0171   constexpr int hfdepth() const {
0172     int dep = depth();
0173     if (subdet() == HcalForward) {
0174       if (dep > 2)
0175         dep -= 2;
0176     }
0177     return dep;
0178   }
0179   /// get the tower depth
0180   constexpr uint32_t maskDepth() const {
0181     if (oldFormat())
0182       return (id_ | kHcalDepthSet1);
0183     else
0184       return (id_ | kHcalDepthSet2);
0185   }
0186   /// change format
0187   constexpr uint32_t otherForm() const {
0188     uint32_t rawid = (id_ & kHcalIdMask);
0189     if (oldFormat()) {
0190       rawid = newForm(id_);
0191     } else {
0192       rawid |= ((depth() & kHcalDepthMask1) << kHcalDepthOffset1) |
0193                ((ieta() > 0) ? (kHcalZsideMask1 | (ieta() << kHcalEtaOffset1)) : ((-ieta()) << kHcalEtaOffset1)) |
0194                (iphi() & kHcalPhiMask1);
0195     }
0196     return rawid;
0197   }
0198   constexpr void changeForm() { id_ = otherForm(); }
0199   constexpr uint32_t newForm() const { return newForm(id_); }
0200   constexpr static int32_t newForm(const uint32_t& inpid) {
0201     uint32_t rawid(inpid);
0202     if ((rawid & kHcalIdFormat2) == 0) {
0203       int zsid{0}, eta{0}, phi{0}, dep{0};
0204       unpackId(rawid, zsid, eta, phi, dep);
0205       rawid = inpid & kHcalIdMask;
0206       rawid |= ((kHcalIdFormat2) | ((dep & kHcalDepthMask2) << kHcalDepthOffset2) |
0207                 ((zsid > 0) ? (kHcalZsideMask2 | (eta << kHcalEtaOffset2)) : ((eta) << kHcalEtaOffset2)) |
0208                 (phi & kHcalPhiMask2));
0209     }
0210     return rawid;
0211   }
0212   /// base detId for HF dual channels
0213   constexpr bool sameBaseDetId(const DetId& gen) const {
0214     uint32_t rawid = gen.rawId();
0215     if (rawid == id_)
0216       return true;
0217     int zsid{0}, eta{0}, phi{0}, dep{0};
0218     if ((id_ & kHcalIdMask) != (rawid & kHcalIdMask))
0219       return false;
0220     unpackId(rawid, zsid, eta, phi, dep);
0221     if (subdet() == HcalForward && dep > 2)
0222       dep -= 2;
0223     bool result = ((zsid == zside()) && (eta == ietaAbs()) && (phi == iphi()) && (dep == hfdepth()));
0224     return result;
0225   }
0226   constexpr HcalDetId baseDetId() const {
0227     if (subdet() != HcalForward || depth() <= 2) {
0228       return HcalDetId(id_);
0229     } else {
0230       int zsid{0}, eta{0}, phi{0}, dep{0};
0231       unpackId(id_, zsid, eta, phi, dep);
0232       dep -= 2;
0233       uint32_t rawid = id_ & kHcalIdMask;
0234       rawid |= (kHcalIdFormat2) | ((dep & kHcalDepthMask2) << kHcalDepthOffset2) |
0235                ((zsid > 0) ? (kHcalZsideMask2 | (eta << kHcalEtaOffset2)) : ((eta) << kHcalEtaOffset2)) |
0236                (phi & kHcalPhiMask2);
0237       return HcalDetId(rawid);
0238     }
0239   }
0240   /// second PMT anode detId for HF dual channels
0241   constexpr HcalDetId secondAnodeId() const {
0242     if (subdet() != HcalForward || depth() > 2) {
0243       return HcalDetId(id_);
0244     } else {
0245       int zsid{0}, eta{0}, phi{0}, dep{0};
0246       unpackId(id_, zsid, eta, phi, dep);
0247       dep += 2;
0248       uint32_t rawid = id_ & kHcalIdMask;
0249       rawid |= (kHcalIdFormat2) | ((dep & kHcalDepthMask2) << kHcalDepthOffset2) |
0250                ((zsid > 0) ? (kHcalZsideMask2 | (eta << kHcalEtaOffset2)) : ((eta) << kHcalEtaOffset2)) |
0251                (phi & kHcalPhiMask2);
0252       return HcalDetId(rawid);
0253     }
0254   }
0255 
0256   /// get the smallest crystal_ieta of the crystal in front of this tower (HB and HE tower 17 only)
0257   constexpr int crystal_ieta_low() const { return ((ieta() - zside()) * 5) + zside(); }
0258   /// get the largest crystal_ieta of the crystal in front of this tower (HB and HE tower 17 only)
0259   constexpr int crystal_ieta_high() const { return ((ieta() - zside()) * 5) + 5 * zside(); }
0260   /// get the smallest crystal_iphi of the crystal in front of this tower (HB and HE tower 17 only)
0261   constexpr int crystal_iphi_low() const {
0262     int simple_iphi = ((iphi() - 1) * 5) + 1;
0263     simple_iphi += 10;
0264     return ((simple_iphi > 360) ? (simple_iphi - 360) : (simple_iphi));
0265   }
0266   /// get the largest crystal_iphi of the crystal in front of this tower (HB and HE tower 17 only)
0267   constexpr int crystal_iphi_high() const {
0268     int simple_iphi = ((iphi() - 1) * 5) + 5;
0269     simple_iphi += 10;
0270     return ((simple_iphi > 360) ? (simple_iphi - 360) : (simple_iphi));
0271   }
0272 
0273   static const HcalDetId Undefined;
0274 
0275 private:
0276   constexpr void newFromOld(const uint32_t& rawid) { id_ = newForm(rawid); }
0277 
0278   constexpr static void unpackId(const uint32_t& rawid, int& zsid, int& eta, int& phi, int& dep) {
0279     if ((rawid & kHcalIdFormat2) == 0) {
0280       zsid = (rawid & kHcalZsideMask1) ? (1) : (-1);
0281       eta = (rawid >> kHcalEtaOffset1) & kHcalEtaMask1;
0282       phi = rawid & kHcalPhiMask1;
0283       dep = (rawid >> kHcalDepthOffset1) & kHcalDepthMask1;
0284     } else {
0285       zsid = (rawid & kHcalZsideMask2) ? (1) : (-1);
0286       eta = (rawid >> kHcalEtaOffset2) & kHcalEtaMask2;
0287       phi = rawid & kHcalPhiMask2;
0288       dep = (rawid >> kHcalDepthOffset2) & kHcalDepthMask2;
0289     }
0290   }
0291 };
0292 
0293 std::ostream& operator<<(std::ostream&, const HcalDetId& id);
0294 
0295 #endif