EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeometryIdentifier.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GeometryIdentifier.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2016-2020 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 
29  public:
30  using Value = uint64_t;
31 
33  constexpr GeometryIdentifier(Value encoded) : m_value(encoded) {}
35  GeometryIdentifier() = default;
37  GeometryIdentifier(const GeometryIdentifier&) = default;
38  ~GeometryIdentifier() = 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 GeometryIdentifier& 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==(GeometryIdentifier lhs,
111  GeometryIdentifier rhs) {
112  return lhs.m_value == rhs.m_value;
113  }
114  friend constexpr bool operator!=(GeometryIdentifier lhs,
115  GeometryIdentifier rhs) {
116  return lhs.m_value != rhs.m_value;
117  }
118  friend constexpr bool operator<(GeometryIdentifier lhs,
119  GeometryIdentifier rhs) {
120  return lhs.m_value < rhs.m_value;
121  }
122 };
123 
124 std::ostream& operator<<(std::ostream& os, GeometryIdentifier id);
125 
126 } // namespace Acts
127 
128 // specialize std::hash so GeometryIdentifier can be used e.g. in an
129 // unordered_map
130 namespace std {
131 template <>
132 struct hash<Acts::GeometryIdentifier> {
133  auto operator()(Acts::GeometryIdentifier gid) const noexcept {
134  return std::hash<Acts::GeometryIdentifier::Value>()(gid.value());
135  }
136 };
137 } // namespace std