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