Analysis Software
Documentation for sPHENIX simulation 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 #include <utility>
15 
16 namespace Acts {
17 
18 class Surface;
19 
32  public:
33  using Value = uint64_t;
34 
36  constexpr GeometryIdentifier(Value encoded) : m_value(encoded) {}
38  GeometryIdentifier() = default;
40  GeometryIdentifier(const GeometryIdentifier&) = default;
41  ~GeometryIdentifier() = default;
44 
46  constexpr Value value() const { return m_value; }
47 
49  constexpr Value volume() const { return getBits(kVolumeMask); }
51  constexpr Value boundary() const { return getBits(kBoundaryMask); }
53  constexpr Value layer() const { return getBits(kLayerMask); }
55  constexpr Value approach() const { return getBits(kApproachMask); }
57  constexpr Value passive() const { return getBits(kApproachMask); }
59  constexpr Value sensitive() const { return getBits(kSensitiveMask); }
63  constexpr Value extra() const { return getBits(kExtraMask); }
64 
67  return setBits(kVolumeMask, volume);
68  }
71  return setBits(kBoundaryMask, boundary);
72  }
75  return setBits(kLayerMask, layer);
76  }
79  return setBits(kApproachMask, approach);
80  }
83  return setBits(kApproachMask, approach);
84  }
87  return setBits(kSensitiveMask, sensitive);
88  }
91  return setBits(kExtraMask, extra);
92  }
93 
94  private:
95  // clang-format off
97  static constexpr Value kVolumeMask = 0xff00000000000000;
99  static constexpr Value kBoundaryMask = 0x00ff000000000000;
101  static constexpr Value kLayerMask = 0x0000fff000000000;
103  static constexpr Value kApproachMask = 0x0000000ff0000000;
104  static constexpr Value kPassiveMask = kApproachMask;
106  static constexpr Value kSensitiveMask = 0x000000000fffff00;
108  static constexpr Value kExtraMask = 0x00000000000000ff;
109  // clang-format on
110 
112 
114  static constexpr int extractShift(Value mask) {
115  // use compiler builtin to extract the number of trailing bits from the
116  // mask. the builtin should be available on all supported compilers.
117  // need unsigned long long version (...ll) to ensure uint64_t compatibility.
118  // WARNING undefined behaviour for mask == 0 which we should not have.
119  return __builtin_ctzll(mask);
120  }
122  constexpr Value getBits(Value mask) const {
123  return (m_value & mask) >> extractShift(mask);
124  }
127  m_value = (m_value & ~mask) | ((id << extractShift(mask)) & mask);
128  // return *this here so we need to write less lines in the set... methods
129  return *this;
130  }
131 
132  friend constexpr bool operator==(GeometryIdentifier lhs,
134  return lhs.m_value == rhs.m_value;
135  }
136  friend constexpr bool operator!=(GeometryIdentifier lhs,
138  return lhs.m_value != rhs.m_value;
139  }
140  friend constexpr bool operator<(GeometryIdentifier lhs,
142  return lhs.m_value < rhs.m_value;
143  }
144 };
145 
146 std::ostream& operator<<(std::ostream& os, GeometryIdentifier id);
147 
151  virtual ~GeometryIdentifierHook() = default;
154 };
155 
156 } // namespace Acts
157 
158 // specialize std::hash so GeometryIdentifier can be used e.g. in an
159 // unordered_map
160 namespace std {
161 template <>
162 struct hash<Acts::GeometryIdentifier> {
163  auto operator()(Acts::GeometryIdentifier gid) const noexcept {
164  return std::hash<Acts::GeometryIdentifier::Value>()(gid.value());
165  }
166 };
167 } // namespace std