File indexing completed on 2026-10-28 09:06:54
0001
0002
0003 #ifndef EDM4HEP_ClusterCollection_H
0004 #define EDM4HEP_ClusterCollection_H
0005
0006
0007 #include "edm4hep/Cluster.h"
0008 #include "edm4hep/ClusterCollectionData.h"
0009 #include "edm4hep/ClusterObj.h"
0010 #include "edm4hep/MutableCluster.h"
0011
0012
0013 #include "podio/CollectionBase.h"
0014 #include "podio/ICollectionProvider.h"
0015 #include "podio/detail/Pythonizations.h"
0016 #include "podio/utilities/TypeHelpers.h"
0017
0018 #if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
0019 #include "nlohmann/json_fwd.hpp"
0020 #endif
0021
0022 #include <algorithm>
0023 #include <cstddef>
0024 #include <memory>
0025 #include <mutex>
0026 #include <ostream>
0027 #include <string_view>
0028 #include <vector>
0029
0030 namespace podio {
0031 struct RelationNames;
0032 }
0033
0034 namespace edm4hep {
0035
0036 class ClusterCollectionIterator {
0037 public:
0038 using value_type = Cluster;
0039 using difference_type = ptrdiff_t;
0040 using reference = Cluster;
0041 using pointer = Cluster*;
0042 using iterator_category = std::input_iterator_tag;
0043
0044
0045 using iterator_concept = std::random_access_iterator_tag;
0046
0047 ClusterCollectionIterator(size_t index, const ClusterObjPointerContainer* collection)
0048 : m_index(index), m_object(podio::utils::MaybeSharedPtr<ClusterObj>{nullptr}), m_collection(collection) {}
0049 ClusterCollectionIterator() = default;
0050
0051 ClusterCollectionIterator(const ClusterCollectionIterator&) = default;
0052 ClusterCollectionIterator(ClusterCollectionIterator&&) = default;
0053 ClusterCollectionIterator& operator=(const ClusterCollectionIterator&) = default;
0054 ClusterCollectionIterator& operator=(ClusterCollectionIterator&&) = default;
0055 ~ClusterCollectionIterator() = default;
0056
0057 constexpr auto operator<=>(const ClusterCollectionIterator& other) const { return m_index <=> other.m_index; }
0058
0059 constexpr bool operator==(const ClusterCollectionIterator& x) const { return m_index == x.m_index; }
0060
0061 reference operator*() const;
0062 pointer operator->();
0063 ClusterCollectionIterator& operator++();
0064 ClusterCollectionIterator operator++(int);
0065 ClusterCollectionIterator& operator--();
0066 ClusterCollectionIterator operator--(int);
0067 ClusterCollectionIterator& operator+=(difference_type n);
0068 ClusterCollectionIterator operator+(difference_type n) const;
0069 friend ClusterCollectionIterator operator+(difference_type n, const ClusterCollectionIterator& it);
0070 ClusterCollectionIterator& operator-=(difference_type n);
0071 ClusterCollectionIterator operator-(difference_type n) const;
0072 reference operator[](difference_type n) const;
0073 difference_type operator-(const ClusterCollectionIterator& other) const;
0074
0075 private:
0076 size_t m_index{0};
0077 Cluster m_object{podio::utils::MaybeSharedPtr<ClusterObj>{nullptr}};
0078 const ClusterObjPointerContainer* m_collection{nullptr};
0079 };
0080
0081 class ClusterMutableCollectionIterator {
0082 public:
0083 using value_type = Cluster;
0084 using difference_type = ptrdiff_t;
0085 using reference = MutableCluster;
0086 using pointer = MutableCluster*;
0087 using iterator_category = std::input_iterator_tag;
0088
0089
0090 using iterator_concept = std::random_access_iterator_tag;
0091
0092 ClusterMutableCollectionIterator(size_t index, const ClusterObjPointerContainer* collection)
0093 : m_index(index), m_object(podio::utils::MaybeSharedPtr<ClusterObj>{nullptr}), m_collection(collection) {}
0094 ClusterMutableCollectionIterator() = default;
0095
0096 ClusterMutableCollectionIterator(const ClusterMutableCollectionIterator&) = default;
0097 ClusterMutableCollectionIterator(ClusterMutableCollectionIterator&&) = default;
0098 ClusterMutableCollectionIterator& operator=(const ClusterMutableCollectionIterator&) = default;
0099 ClusterMutableCollectionIterator& operator=(ClusterMutableCollectionIterator&&) = default;
0100 ~ClusterMutableCollectionIterator() = default;
0101
0102 constexpr auto operator<=>(const ClusterMutableCollectionIterator& other) const { return m_index <=> other.m_index; }
0103
0104 constexpr bool operator==(const ClusterMutableCollectionIterator& x) const { return m_index == x.m_index; }
0105
0106 reference operator*() const;
0107 pointer operator->();
0108 ClusterMutableCollectionIterator& operator++();
0109 ClusterMutableCollectionIterator operator++(int);
0110 ClusterMutableCollectionIterator& operator--();
0111 ClusterMutableCollectionIterator operator--(int);
0112 ClusterMutableCollectionIterator& operator+=(difference_type n);
0113 ClusterMutableCollectionIterator operator+(difference_type n) const;
0114 friend ClusterMutableCollectionIterator operator+(difference_type n, const ClusterMutableCollectionIterator& it);
0115 ClusterMutableCollectionIterator& operator-=(difference_type n);
0116 ClusterMutableCollectionIterator operator-(difference_type n) const;
0117 reference operator[](difference_type n) const;
0118 difference_type operator-(const ClusterMutableCollectionIterator& other) const;
0119
0120 private:
0121 size_t m_index{0};
0122 MutableCluster m_object{podio::utils::MaybeSharedPtr<ClusterObj>{nullptr}};
0123 const ClusterObjPointerContainer* m_collection{nullptr};
0124 };
0125
0126
0127
0128
0129 class ClusterCollection : public podio::CollectionBase {
0130 public:
0131 using value_type = Cluster;
0132 using mutable_type = MutableCluster;
0133 using const_iterator = ClusterCollectionIterator;
0134 using iterator = ClusterMutableCollectionIterator;
0135 using difference_type = ptrdiff_t;
0136 using size_type = size_t;
0137 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
0138 using reverse_iterator = std::reverse_iterator<iterator>;
0139
0140 ClusterCollection() = default;
0141 ClusterCollection(ClusterCollectionData&& data, bool isSubsetColl);
0142
0143 ClusterCollection(const ClusterCollection&) = delete;
0144 ClusterCollection& operator=(const ClusterCollection&) = delete;
0145 ClusterCollection(ClusterCollection&&) = default;
0146 ClusterCollection& operator=(ClusterCollection&&) = default;
0147
0148 ~ClusterCollection() override;
0149 #if defined(__cpp_lib_containers_ranges)
0150 template <podio::detail::RangeConvertibleTo<value_type> R>
0151 ClusterCollection(std::from_range_t, R&& range);
0152 #endif
0153
0154
0155
0156 template <podio::detail::RangeConvertibleTo<value_type> R>
0157 static ClusterCollection from(R&& range);
0158
0159 constexpr static std::string_view typeName = "edm4hep::ClusterCollection";
0160 constexpr static std::string_view valueTypeName = "edm4hep::Cluster";
0161 constexpr static std::string_view dataTypeName = "edm4hep::ClusterData";
0162
0163 void clear() final;
0164
0165
0166 static void __cppyy_pythonize__(PyObject* klass, const std::string& name) {
0167 podio::detail::pythonizations::pythonize_subscript(klass, name);
0168 }
0169
0170
0171 void print(std::ostream& os = std::cout, bool flush = true) const final;
0172
0173
0174 MutableCluster create();
0175
0176
0177
0178 template <typename... Args>
0179 MutableCluster create(Args&&... args);
0180
0181
0182 std::size_t size() const final;
0183
0184
0185 std::size_t max_size() const final;
0186
0187
0188 bool empty() const final;
0189
0190
0191 const std::string_view getTypeName() const final { return typeName; }
0192
0193 const std::string_view getValueTypeName() const final { return valueTypeName; }
0194
0195 const std::string_view getDataTypeName() const final { return dataTypeName; }
0196
0197 podio::SchemaVersionT getSchemaVersion() const final;
0198
0199 bool isSubsetCollection() const final { return m_isSubsetColl; }
0200
0201 void setSubsetCollection(bool setSubset = true) final;
0202
0203
0204 Cluster operator[](std::size_t index) const;
0205
0206 MutableCluster operator[](std::size_t index);
0207
0208 Cluster at(std::size_t index) const;
0209
0210 MutableCluster at(std::size_t index);
0211
0212
0213 void push_back(const MutableCluster& object);
0214
0215 void push_back(const Cluster& object);
0216
0217 void prepareForWrite() const final;
0218 void prepareAfterRead() final;
0219 bool setReferences(const podio::ICollectionProvider* collectionProvider) final;
0220
0221
0222 podio::CollectionWriteBuffers getBuffers() final;
0223
0224 void setID(uint32_t ID) final {
0225 m_collectionID = ID;
0226 if (!m_isSubsetColl) {
0227 std::for_each(m_storage.entries.begin(), m_storage.entries.end(),
0228 [ID](ClusterObj* obj) { obj->id = {obj->id.index, static_cast<uint32_t>(ID)}; });
0229 }
0230 }
0231
0232 uint32_t getID() const final { return m_collectionID; }
0233
0234
0235 bool hasID() const final {
0236 return getID() != static_cast<uint32_t>(podio::ObjectID::untracked) &&
0237 getID() != static_cast<uint32_t>(podio::ObjectID::invalid);
0238 }
0239
0240 [[deprecated("isValid will be removed, use hasID() if you want to check if it has an ID, otherwise assume the "
0241 "collection is valid")]] bool
0242 isValid() const final {
0243 return hasID();
0244 }
0245
0246 size_t getDatamodelRegistryIndex() const final;
0247
0248
0249 iterator begin() { return iterator(0, &m_storage.entries); }
0250 const_iterator begin() const { return const_iterator(0, &m_storage.entries); }
0251 const_iterator cbegin() const { return begin(); }
0252 iterator end() { return iterator(m_storage.entries.size(), &m_storage.entries); }
0253 const_iterator end() const { return const_iterator(m_storage.entries.size(), &m_storage.entries); }
0254 const_iterator cend() const { return end(); }
0255
0256 reverse_iterator rbegin() { return reverse_iterator(end()); }
0257 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
0258 const_reverse_iterator crbegin() const { return rbegin(); }
0259 reverse_iterator rend() { return reverse_iterator(begin()); }
0260 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
0261 const_reverse_iterator crend() const { return rend(); }
0262
0263 std::vector<std::int32_t> type(const size_t nElem = 0) const;
0264 std::vector<float> energy(const size_t nElem = 0) const;
0265 std::vector<float> energyError(const size_t nElem = 0) const;
0266 std::vector<edm4hep::Vector3f> position(const size_t nElem = 0) const;
0267 std::vector<edm4hep::CovMatrix3f> positionError(const size_t nElem = 0) const;
0268 std::vector<float> iTheta(const size_t nElem = 0) const;
0269 std::vector<float> iPhi(const size_t nElem = 0) const;
0270 std::vector<edm4hep::Vector3f> directionError(const size_t nElem = 0) const;
0271
0272 private:
0273
0274
0275
0276 friend class ClusterCollectionData;
0277
0278 mutable bool m_isPrepared{false};
0279 bool m_isSubsetColl{false};
0280 uint32_t m_collectionID{static_cast<uint32_t>(podio::ObjectID::untracked)};
0281 mutable std::unique_ptr<std::mutex> m_storageMtx{std::make_unique<std::mutex>()};
0282 mutable ClusterCollectionData m_storage{};
0283 };
0284
0285 std::ostream& operator<<(std::ostream& o, const ClusterCollection& v);
0286
0287 template <typename... Args>
0288 MutableCluster ClusterCollection::create(Args&&... args) {
0289 if (m_isSubsetColl) {
0290 throw std::logic_error("Cannot create new elements on a subset collection");
0291 }
0292 auto obj =
0293 new ClusterObj({static_cast<int>(m_storage.entries.size()), m_collectionID}, {std::forward<Args>(args)...});
0294 m_storage.entries.push_back(obj);
0295
0296
0297 obj->m_clusters = new std::vector<edm4hep::Cluster>();
0298 obj->m_hits = new std::vector<edm4hep::CalorimeterHit>();
0299 obj->m_shapeParameters = new std::vector<float>();
0300 obj->m_subdetectorEnergies = new std::vector<float>();
0301 m_storage.createRelations(obj);
0302 return MutableCluster(podio::utils::MaybeSharedPtr(obj));
0303 }
0304
0305 #if defined(__cpp_lib_containers_ranges)
0306 template <podio::detail::RangeConvertibleTo<ClusterCollection::value_type> R>
0307 ClusterCollection::ClusterCollection(std::from_range_t, R&& range) : ClusterCollection() {
0308 setSubsetCollection(podio::detail::RangeOf<R, value_type>);
0309
0310 for (auto&& elem : range) {
0311 push_back(std::forward<decltype(elem)>(elem));
0312 }
0313 }
0314 #endif
0315
0316 template <podio::detail::RangeConvertibleTo<ClusterCollection::value_type> R>
0317 ClusterCollection ClusterCollection::from(R&& range) {
0318 ClusterCollection coll;
0319 if constexpr (podio::detail::RangeOf<R, value_type>) {
0320 coll.setSubsetCollection();
0321 }
0322
0323 for (auto&& elem : range) {
0324 coll.push_back(std::forward<decltype(elem)>(elem));
0325 }
0326
0327 return coll;
0328 }
0329
0330 #if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
0331 void to_json(nlohmann::json& j, const ClusterCollection& collection);
0332 #endif
0333
0334 }
0335
0336
0337
0338
0339 #if defined(__clang__)
0340 #pragma clang diagnostic push
0341 #pragma clang diagnostic ignored "-Wunknown-warning-option"
0342 #pragma clang diagnostic ignored "-Wdeprecated-redundant-constexpr-static-def"
0343 #pragma clang diagnostic ignored "-Wdeprecated"
0344 constexpr std::string_view edm4hep::ClusterCollection::typeName;
0345 constexpr std::string_view edm4hep::ClusterCollection::valueTypeName;
0346 constexpr std::string_view edm4hep::ClusterCollection::dataTypeName;
0347 #pragma clang diagnostic pop
0348 #elif defined(__GNUC__)
0349 #pragma GCC diagnostic push
0350 #pragma GCC diagnostic ignored "-Wdeprecated"
0351 constexpr std::string_view edm4hep::ClusterCollection::typeName;
0352 constexpr std::string_view edm4hep::ClusterCollection::valueTypeName;
0353 constexpr std::string_view edm4hep::ClusterCollection::dataTypeName;
0354 #pragma GCC diagnostic pop
0355 #endif
0356
0357 #endif