File indexing completed on 2026-05-06 08:38:04
0001
0002
0003 #ifndef EDM4HEP_TrackerHit_H
0004 #define EDM4HEP_TrackerHit_H
0005
0006 #include "edm4hep/SenseWireHitCollection.h"
0007 #include "edm4hep/TrackerHit3DCollection.h"
0008 #include "edm4hep/TrackerHitPlaneCollection.h"
0009
0010 #include "podio/ObjectID.h"
0011 #include "podio/detail/OrderKey.h"
0012 #include "podio/utilities/TypeHelpers.h"
0013
0014 #include <memory>
0015 #include <ostream>
0016 #include <stdexcept>
0017
0018 namespace edm4hep {
0019
0020
0021
0022
0023
0024 class TrackerHit {
0025 public:
0026
0027
0028 using interfaced_types = std::tuple<::edm4hep::TrackerHit3D, ::edm4hep::TrackerHitPlane, ::edm4hep::SenseWireHit>;
0029
0030
0031
0032
0033
0034 using mutable_type = podio::det::nonesuch;
0035
0036 private:
0037
0038
0039 using InterfacedMutableTypes = podio::detail::TupleOfMutableTypes<interfaced_types>;
0040
0041
0042 template <typename T>
0043 constexpr static bool isInterfacedType = podio::detail::isInTuple<T, interfaced_types>;
0044
0045 public:
0046
0047
0048 template <typename T>
0049 constexpr static bool isInitializableFrom =
0050 isInterfacedType<T> || podio::detail::isInTuple<T, InterfacedMutableTypes>;
0051
0052 private:
0053 struct Concept {
0054 virtual ~Concept() = default;
0055 virtual std::unique_ptr<Concept> clone() const = 0;
0056 virtual void print(std::ostream&) const = 0;
0057
0058 virtual podio::ObjectID getObjectID() const = 0;
0059 virtual bool isAvailable() const = 0;
0060 virtual void unlink() = 0;
0061 virtual std::uint64_t getCellID() const = 0;
0062
0063 virtual std::int32_t getType() const = 0;
0064
0065 virtual std::int32_t getQuality() const = 0;
0066
0067 virtual float getTime() const = 0;
0068
0069 virtual float getEDep() const = 0;
0070
0071 virtual float getEDepError() const = 0;
0072
0073 virtual const edm4hep::Vector3d& getPosition() const = 0;
0074
0075 virtual const std::type_info& typeInfo() const = 0;
0076 virtual bool equal(const Concept* rhs) const = 0;
0077 virtual podio::detail::OrderKey objOrderKey() const = 0;
0078 virtual size_t objHash() const = 0;
0079 };
0080
0081 template <typename ValueT>
0082 struct Model final : Concept {
0083 ~Model() = default;
0084 Model(ValueT value) : m_value(value) {}
0085
0086 std::unique_ptr<Concept> clone() const final { return std::make_unique<Model<ValueT>>(m_value); }
0087
0088 void print(std::ostream& os) const final { os << m_value; }
0089
0090 void unlink() final { m_value.unlink(); }
0091 bool isAvailable() const final { return m_value.isAvailable(); }
0092 podio::ObjectID getObjectID() const final { return m_value.getObjectID(); }
0093
0094 const std::type_info& typeInfo() const final { return typeid(ValueT); }
0095
0096 bool equal(const Concept* rhs) const final {
0097 if (typeInfo() == rhs->typeInfo()) {
0098 return m_value == static_cast<const Model<ValueT>*>(rhs)->m_value;
0099 }
0100 return false;
0101 }
0102
0103 podio::detail::OrderKey objOrderKey() const final { return podio::detail::getOrderKey(m_value); }
0104
0105 size_t objHash() const final { return std::hash<ValueT>{}(m_value); }
0106
0107 std::uint64_t getCellID() const final { return m_value.getCellID(); }
0108
0109 std::int32_t getType() const final { return m_value.getType(); }
0110
0111 std::int32_t getQuality() const final { return m_value.getQuality(); }
0112
0113 float getTime() const final { return m_value.getTime(); }
0114
0115 float getEDep() const final { return m_value.getEDep(); }
0116
0117 float getEDepError() const final { return m_value.getEDepError(); }
0118
0119 const edm4hep::Vector3d& getPosition() const final { return m_value.getPosition(); }
0120
0121 ValueT m_value{};
0122 };
0123
0124 std::unique_ptr<Concept> m_self{nullptr};
0125
0126 public:
0127
0128
0129 template <typename ValueT>
0130 requires isInitializableFrom<ValueT>
0131 TrackerHit(ValueT value) : m_self(std::make_unique<Model<podio::detail::GetDefaultHandleType<ValueT>>>(value)) {}
0132
0133 TrackerHit(const TrackerHit& other) : m_self(other.m_self->clone()) {}
0134 TrackerHit& operator=(const TrackerHit& other) {
0135 TrackerHit tmp{other};
0136 std::swap(tmp.m_self, this->m_self);
0137 return *this;
0138 }
0139
0140 ~TrackerHit() = default;
0141 TrackerHit(TrackerHit&&) = default;
0142 TrackerHit& operator=(TrackerHit&&) = default;
0143
0144
0145 static TrackerHit makeEmpty() {
0146
0147
0148 return ::edm4hep::TrackerHit3D::makeEmpty();
0149 }
0150
0151 static constexpr std::string_view typeName = "edm4hep::TrackerHit";
0152
0153
0154 bool isAvailable() const { return m_self->isAvailable(); }
0155
0156 void unlink() { m_self->unlink(); }
0157
0158 podio::ObjectID id() const { return getObjectID(); }
0159 podio::ObjectID getObjectID() const { return m_self->getObjectID(); }
0160
0161
0162 template <typename T>
0163 bool isA() const {
0164 static_assert(isInterfacedType<T>, "TrackerHit can only ever be one of the following types: "
0165 "::edm4hep::TrackerHit3D, ::edm4hep::TrackerHitPlane, ::edm4hep::SenseWireHit");
0166 return typeid(T) == m_self->typeInfo();
0167 }
0168
0169
0170
0171
0172 template <typename T>
0173 T as() const {
0174 if (!isA<T>()) {
0175 throw std::runtime_error("Cannot get value as object currently holds another type");
0176 }
0177
0178 return static_cast<Model<T>*>(m_self.get())->m_value;
0179 }
0180
0181 template <typename T>
0182 [[deprecated("Use 'as' instead.")]] T getValue() const {
0183 return as<T>();
0184 }
0185
0186 friend bool operator==(const TrackerHit& lhs, const TrackerHit& rhs) { return lhs.m_self->equal(rhs.m_self.get()); }
0187
0188 friend bool operator!=(const TrackerHit& lhs, const TrackerHit& rhs) { return !(lhs == rhs); }
0189
0190 friend bool operator<(const TrackerHit& lhs, const TrackerHit& rhs) {
0191 return lhs.m_self->objOrderKey() < rhs.m_self->objOrderKey();
0192 }
0193
0194
0195 std::uint64_t getCellID() const { return m_self->getCellID(); }
0196
0197
0198 std::int32_t getType() const { return m_self->getType(); }
0199
0200
0201 std::int32_t getQuality() const { return m_self->getQuality(); }
0202
0203
0204 float getTime() const { return m_self->getTime(); }
0205
0206
0207 float getEDep() const { return m_self->getEDep(); }
0208
0209
0210 float getEDepError() const { return m_self->getEDepError(); }
0211
0212
0213
0214 const edm4hep::Vector3d& getPosition() const { return m_self->getPosition(); }
0215
0216 friend std::ostream& operator<<(std::ostream& os, const TrackerHit& value) {
0217 value.m_self->print(os);
0218 return os;
0219 }
0220
0221 friend std::hash<TrackerHit>;
0222 };
0223
0224 }
0225
0226 template <>
0227 struct std::hash<edm4hep::TrackerHit> {
0228 std::size_t operator()(const edm4hep::TrackerHit& obj) const { return obj.m_self->objHash(); }
0229 };
0230
0231 #endif