DetId

Detector

hash

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#ifndef DATAFORMATS_DETID_H
#define DATAFORMATS_DETID_H

//FIXME shall be removed and implemented where the operator is defined
#include <ostream>

#include <cstdint>
/** \class DetId

Parent class for all detector ids in CMS.  The DetId is a 32-bit
unsigned integer.  The four most significant bits ([31:28]) identify
the large-scale detector (e.g. Tracker or Ecal) while the next three
bits ([27:25]) identify a part of the detector (such as HcalBarrel
(HB) for Hcal).

*/
class DetId {
public:
  static const int kDetMask = 0xF;
  static const int kSubdetMask = 0x7;
  static const int kDetOffset = 28;
  static const int kSubdetOffset = 25;

  enum Detector {
    Tracker = 1,
    Muon = 2,
    Ecal = 3,
    Hcal = 4,
    Calo = 5,
    Forward = 6,
    VeryForward = 7,
    HGCalEE = 8,
    HGCalHSi = 9,
    HGCalHSc = 10,
    HGCalTrigger = 11
  };
  /// Create an empty or null id (also for persistence)
  constexpr DetId() : id_(0) {}
  /// Create an id from a raw number
  constexpr DetId(uint32_t id) : id_(id) {}
  /// Create an id, filling the detector and subdetector fields as specified
  constexpr DetId(Detector det, int subdet)
      : id_(((det & kDetMask) << kDetOffset) | ((subdet & kSubdetMask) << kSubdetOffset)) {}

  /// get the detector field from this detid
  constexpr Detector det() const { return Detector((id_ >> kDetOffset) & kDetMask); }
  /// get the contents of the subdetector field (not cast into any detector's numbering enum)
  constexpr int subdetId() const {
    return ((HGCalEE == det()) || (HGCalHSi == det()) || (HGCalHSc == det()) ? 0
                                                                             : ((id_ >> kSubdetOffset) & kSubdetMask));
  }

  constexpr uint32_t operator()() const { return id_; }
  constexpr operator uint32_t() const { return id_; }

  /// get the raw id
  constexpr uint32_t rawId() const { return id_; }
  /// is this a null id ?
  constexpr bool null() const { return id_ == 0; }

  /// equality
  constexpr bool operator==(DetId id) const { return id_ == id.id_; }
  /// inequality
  constexpr bool operator!=(DetId id) const { return id_ != id.id_; }
  /// comparison
  constexpr bool operator<(DetId id) const { return id_ < id.id_; }

protected:
  uint32_t id_;
};

/// equality
constexpr inline bool operator==(uint32_t i, DetId id) { return i == id(); }
constexpr inline bool operator==(DetId id, uint32_t i) { return i == id(); }
/// inequality
constexpr inline bool operator!=(uint32_t i, DetId id) { return i != id(); }
constexpr inline bool operator!=(DetId id, uint32_t i) { return i != id(); }
/// comparison
constexpr inline bool operator<(uint32_t i, DetId id) { return i < id(); }
constexpr inline bool operator<(DetId id, uint32_t i) { return id() < i; }

//std::ostream& operator<<(std::ostream& s, const DetId& id);

namespace std {
  template <>
  struct hash<DetId> {
    typedef DetId argument_type;
    typedef std::size_t result_type;
    result_type operator()(argument_type const& id) const noexcept { return std::hash<uint32_t>()(id.rawId()); }
  };
}  // namespace std

#endif