Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:53:13

0001 #ifndef DATAFORMATS_DETID_H
0002 #define DATAFORMATS_DETID_H
0003 
0004 //FIXME shall be removed and implemented where the operator is defined
0005 #include <ostream>
0006 
0007 #include <cstdint>
0008 /** \class DetId
0009 
0010 Parent class for all detector ids in CMS.  The DetId is a 32-bit
0011 unsigned integer.  The four most significant bits ([31:28]) identify
0012 the large-scale detector (e.g. Tracker or Ecal) while the next three
0013 bits ([27:25]) identify a part of the detector (such as HcalBarrel
0014 (HB) for Hcal).
0015 
0016 */
0017 class DetId {
0018 public:
0019   static const int kDetMask = 0xF;
0020   static const int kSubdetMask = 0x7;
0021   static const int kDetOffset = 28;
0022   static const int kSubdetOffset = 25;
0023 
0024   enum Detector {
0025     Tracker = 1,
0026     Muon = 2,
0027     Ecal = 3,
0028     Hcal = 4,
0029     Calo = 5,
0030     Forward = 6,
0031     VeryForward = 7,
0032     HGCalEE = 8,
0033     HGCalHSi = 9,
0034     HGCalHSc = 10,
0035     HGCalTrigger = 11
0036   };
0037   /// Create an empty or null id (also for persistence)
0038   constexpr DetId() : id_(0) {}
0039   /// Create an id from a raw number
0040   constexpr DetId(uint32_t id) : id_(id) {}
0041   /// Create an id, filling the detector and subdetector fields as specified
0042   constexpr DetId(Detector det, int subdet)
0043       : id_(((det & kDetMask) << kDetOffset) | ((subdet & kSubdetMask) << kSubdetOffset)) {}
0044 
0045   /// get the detector field from this detid
0046   constexpr Detector det() const { return Detector((id_ >> kDetOffset) & kDetMask); }
0047   /// get the contents of the subdetector field (not cast into any detector's numbering enum)
0048   constexpr int subdetId() const {
0049     return ((HGCalEE == det()) || (HGCalHSi == det()) || (HGCalHSc == det()) ? 0
0050                                                                              : ((id_ >> kSubdetOffset) & kSubdetMask));
0051   }
0052 
0053   constexpr uint32_t operator()() const { return id_; }
0054   constexpr operator uint32_t() const { return id_; }
0055 
0056   /// get the raw id
0057   constexpr uint32_t rawId() const { return id_; }
0058   /// is this a null id ?
0059   constexpr bool null() const { return id_ == 0; }
0060 
0061   /// equality
0062   constexpr bool operator==(DetId id) const { return id_ == id.id_; }
0063   /// inequality
0064   constexpr bool operator!=(DetId id) const { return id_ != id.id_; }
0065   /// comparison
0066   constexpr bool operator<(DetId id) const { return id_ < id.id_; }
0067 
0068 protected:
0069   uint32_t id_;
0070 };
0071 
0072 /// equality
0073 constexpr inline bool operator==(uint32_t i, DetId id) { return i == id(); }
0074 constexpr inline bool operator==(DetId id, uint32_t i) { return i == id(); }
0075 /// inequality
0076 constexpr inline bool operator!=(uint32_t i, DetId id) { return i != id(); }
0077 constexpr inline bool operator!=(DetId id, uint32_t i) { return i != id(); }
0078 /// comparison
0079 constexpr inline bool operator<(uint32_t i, DetId id) { return i < id(); }
0080 constexpr inline bool operator<(DetId id, uint32_t i) { return id() < i; }
0081 
0082 //std::ostream& operator<<(std::ostream& s, const DetId& id);
0083 
0084 namespace std {
0085   template <>
0086   struct hash<DetId> {
0087     typedef DetId argument_type;
0088     typedef std::size_t result_type;
0089     result_type operator()(argument_type const& id) const noexcept { return std::hash<uint32_t>()(id.rawId()); }
0090   };
0091 }  // namespace std
0092 
0093 #endif