Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef DATAFORMATS_HCALDETID_HCALELECTRONICSID_H
0002 #define DATAFORMATS_HCALDETID_HCALELECTRONICSID_H 1
0003 
0004 #include <string>
0005 #include <ostream>
0006 #include <cstdint>
0007 
0008 /** \brief Readout chain identification for Hcal
0009 
0010     [31:27] Unused (so far)
0011     [26] VME (0), uTCA (1)
0012 
0013     For VME Electronics:
0014     [25]    Trigger-chain id flag
0015     [24:20] Readout Crate Id
0016     [19] HTR FPGA selector [t/b]
0017     [18:14] HTR Slot
0018     [13:9]  DCC id
0019     [8:5]   Spigot
0020     [4:2]   FiberIndex or SLB site
0021     [1:0]   FiberChanId or SLB channel
0022 
0023     For uTCA Electronics:
0024 
0025      [25]    Is Trigger Id
0026      [18:13] Readout Crate Id
0027      [12:9] Slot
0028      [8:4]   Fiber
0029      [3:0]   FiberChanId
0030 
0031  */
0032 class HcalElectronicsId {
0033 public:
0034   /** Default constructor -- invalid value */
0035   constexpr HcalElectronicsId() : hcalElectronicsId_{0xffffffffu} {}
0036   /** from raw */
0037   constexpr HcalElectronicsId(uint32_t id) : hcalElectronicsId_{id} {}
0038   /** VME Constructor from fiberchan,fiber index,spigot,dccid */
0039   constexpr HcalElectronicsId(int fiberChan, int fiberIndex, int spigot, int dccid)
0040       : hcalElectronicsId_((uint32_t)((fiberChan & 0x3) | (((fiberIndex - 1) & 0x7) << 2) | ((spigot & 0xF) << 5) |
0041                                       ((dccid & 0x1F) << 9))) {}
0042   /** VME Constructor from slb channel,slb site,spigot,dccid */
0043   constexpr HcalElectronicsId(int slbChan, int slbSite, int spigot, int dccid, int crate, int slot, int tb)
0044       : hcalElectronicsId_(
0045             (uint32_t)((slbChan & 0x3) | (((slbSite) & 0x7) << 2) | ((spigot & 0xF) << 5) | ((dccid & 0x1F) << 9))) {
0046     hcalElectronicsId_ |= ((tb & 0x1) << 19) | ((slot & 0x1f) << 14) | ((crate & 0x3f) << 20);
0047     hcalElectronicsId_ |= 0x02000000;
0048   }
0049   /** uTCA constructor */
0050   constexpr HcalElectronicsId(int crate, int slot, int fiber, int fc, bool isTrigger)
0051       : hcalElectronicsId_((int)((fc & 0xF) | (((fiber) & 0x1F) << 4) | ((slot & 0xF) << 9) | ((crate & 0x3F) << 13))) {
0052     if (isTrigger)
0053       hcalElectronicsId_ |= 0x02000000;
0054     hcalElectronicsId_ |= 0x04000000;
0055   }
0056 
0057   constexpr uint32_t operator()() { return hcalElectronicsId_; }
0058 
0059   constexpr uint32_t rawId() const { return hcalElectronicsId_; }
0060 
0061   constexpr bool isVMEid() const { return (hcalElectronicsId_ & 0x04000000) == 0; }
0062   constexpr bool isUTCAid() const { return (hcalElectronicsId_ & 0x04000000) != 0; }
0063   constexpr bool isTriggerChainId() const { return (hcalElectronicsId_ & 0x02000000) != 0; }
0064 
0065   /** Set the VME htr-related information 1=top, 0=bottom*/
0066   constexpr void setHTR(int crate, int slot, int tb) {
0067     if (isUTCAid())
0068       return;                      // cannot do this for uTCA
0069     hcalElectronicsId_ &= 0x3FFF;  // keep the readout chain info
0070     hcalElectronicsId_ |= ((tb & 0x1) << 19) | ((slot & 0x1f) << 14) | ((crate & 0x3f) << 20);
0071   }
0072 
0073   /// get subtype for this channel (valid for uTCA only)
0074   constexpr int32_t subtype() const {
0075     return (isUTCAid()) ? static_cast<int32_t>((hcalElectronicsId_ >> 21) & 0x1F) : (-1);
0076   }
0077   /// get the fiber channel id (which of channels on a fiber)
0078   constexpr int32_t fiberChanId() const {
0079     return (isVMEid()) ? static_cast<int32_t>(hcalElectronicsId_ & 0x3) : (hcalElectronicsId_ & 0xF);
0080   }
0081   /// get the fiber index.  For VME 1-8 (which of eight fibers carried by a spigot), for uTCA fibers are zero-based
0082   constexpr int32_t fiberIndex() const {
0083     return (isVMEid()) ? (((hcalElectronicsId_ >> 2) & 0x7) + 1) : ((hcalElectronicsId_ >> 4) & 0x1F);
0084   }
0085   /// get the SLB channel index  (valid only for VME trigger-chain ids)
0086   constexpr int32_t slbChannelIndex() const { return hcalElectronicsId_ & 0x3; }
0087   /// get the SLB site number (valid only for VME trigger-chain ids)
0088   constexpr int32_t slbSiteNumber() const { return ((hcalElectronicsId_ >> 2) & 0x7); }
0089 
0090   /// get the HTR channel id (1-24)
0091   constexpr int32_t htrChanId() const { return isVMEid() ? ((fiberChanId() + 1) + ((fiberIndex() - 1) * 3)) : (0); }
0092 
0093   /// get the HTR-wide slb channel code (letter plus number)
0094   std::string slbChannelCode() const;
0095 
0096   /// get the spigot (input number on DCC, AMC card number for uTCA)
0097   constexpr int32_t spigot() const { return (isVMEid()) ? ((hcalElectronicsId_ >> 5) & 0xF) : slot(); }
0098   /// get the (Hcal local) DCC id for VME, crate number for uTCA
0099   constexpr int32_t dccid() const { return (isVMEid()) ? ((hcalElectronicsId_ >> 9) & 0x1F) : crateId(); }
0100   /// get the htr slot
0101   constexpr int32_t htrSlot() const { return slot(); }
0102   /// get the htr or uHTR slot
0103   constexpr int32_t slot() const {
0104     return (isVMEid()) ? ((hcalElectronicsId_ >> 14) & 0x1F) : ((hcalElectronicsId_ >> 9) & 0xF);
0105   }
0106   /// get the htr top/bottom (1=top/0=bottom), valid for VME
0107   constexpr int32_t htrTopBottom() const {
0108     return (isVMEid()) ? static_cast<int32_t>((hcalElectronicsId_ >> 19) & 0x1) : (-1);
0109   }
0110   /// get the readout VME crate number
0111   constexpr int32_t readoutVMECrateId() const { return crateId(); }
0112   /// get the readout VME crate number
0113   constexpr int32_t crateId() const {
0114     return (isVMEid()) ? ((hcalElectronicsId_ >> 20) & 0x1F) : ((hcalElectronicsId_ >> 13) & 0x3F);
0115   }
0116   /// get a fast, compact, unique index for linear lookups
0117   constexpr int32_t linearIndex() const {
0118     return (isVMEid()) ? ((hcalElectronicsId_) & 0x3FFF) : ((hcalElectronicsId_) & 0x7FFFF);
0119   }
0120 
0121   static const int maxLinearIndex = 0x7FFFF;  //
0122   static const int maxDCCId = 31;
0123 
0124   /** Equality operator */
0125   constexpr int operator==(const HcalElectronicsId& id) const { return id.hcalElectronicsId_ == hcalElectronicsId_; }
0126   /** Non-Equality operator */
0127   constexpr int operator!=(const HcalElectronicsId& id) const { return id.hcalElectronicsId_ != hcalElectronicsId_; }
0128   /// Compare the id to another id for use in a map
0129   constexpr int operator<(const HcalElectronicsId& id) const { return hcalElectronicsId_ < id.hcalElectronicsId_; }
0130 
0131 private:
0132   uint32_t hcalElectronicsId_;
0133 };
0134 
0135 std::ostream& operator<<(std::ostream&, const HcalElectronicsId&);
0136 
0137 #endif