ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeometryID.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GeometryID.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2016-2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include <cstdint>
12 #include <functional>
13 #include <iosfwd>
14 
15 namespace Acts {
16 
28 class GeometryID {
29  public:
30  using Value = uint64_t;
31 
33  constexpr GeometryID(Value encoded) : m_value(encoded) {}
35  GeometryID() = default;
36  GeometryID(GeometryID&&) = default;
37  GeometryID(const GeometryID&) = default;
38  ~GeometryID() = default;
39  GeometryID& operator=(GeometryID&&) = default;
40  GeometryID& operator=(const GeometryID&) = default;
41 
43  constexpr Value value() const { return m_value; }
44 
46  constexpr Value volume() const { return getBits(kVolumeMask); }
48  constexpr Value boundary() const { return getBits(kBoundaryMask); }
50  constexpr Value layer() const { return getBits(kLayerMask); }
52  constexpr Value approach() const { return getBits(kApproachMask); }
54  constexpr Value sensitive() const { return getBits(kSensitiveMask); }
55 
58  return setBits(kVolumeMask, volume);
59  }
62  return setBits(kBoundaryMask, boundary);
63  }
66  return setBits(kLayerMask, layer);
67  }
70  return setBits(kApproachMask, approach);
71  }
74  return setBits(kSensitiveMask, sensitive);
75  }
76 
77  private:
78  // (2^8)-1 = 255 volumes
79  static constexpr Value kVolumeMask = 0xff00000000000000;
80  // (2^8)-1 = 255 boundaries
81  static constexpr Value kBoundaryMask = 0x00ff000000000000;
82  // (2^12)-1 = 4096 layers
83  static constexpr Value kLayerMask = 0x0000fff000000000;
84  // (2^8)-1 = 255 approach surfaces
85  static constexpr Value kApproachMask = 0x0000000ff0000000;
86  // (2^28)-1 sensitive surfaces
87  static constexpr Value kSensitiveMask = 0x000000000fffffff;
88 
90 
92  static constexpr int extractShift(Value mask) {
93  // use compiler builtin to extract the number of trailing bits from the
94  // mask. the builtin should be available on all supported compilers.
95  // need unsigned long long version (...ll) to ensure uint64_t compatibility.
96  // WARNING undefined behaviour for mask == 0 which we should not have.
97  return __builtin_ctzll(mask);
98  }
100  constexpr Value getBits(Value mask) const {
101  return (m_value & mask) >> extractShift(mask);
102  }
104  constexpr GeometryID& setBits(Value mask, Value id) {
105  m_value = (m_value & ~mask) | ((id << extractShift(mask)) & mask);
106  // return *this here so we need to write less lines in the set... methods
107  return *this;
108  }
109 
110  friend constexpr bool operator==(GeometryID lhs, GeometryID rhs) {
111  return lhs.m_value == rhs.m_value;
112  }
113  friend constexpr bool operator<(GeometryID lhs, GeometryID rhs) {
114  return lhs.m_value < rhs.m_value;
115  }
116 };
117 
118 std::ostream& operator<<(std::ostream& os, GeometryID id);
119 
120 } // namespace Acts
121 
122 // specialize std::hash so GeometryId can be used e.g. in an unordered_map
123 namespace std {
124 template <>
125 struct hash<Acts::GeometryID> {
126  auto operator()(Acts::GeometryID gid) const noexcept {
127  return std::hash<Acts::GeometryID::Value>()(gid.value());
128  }
129 };
130 } // namespace std