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