File indexing completed on 2025-07-27 08:35:44
0001
0002
0003 #ifndef EDM4EIC_ReconstructedParticleCollection_H
0004 #define EDM4EIC_ReconstructedParticleCollection_H
0005
0006
0007 #include "edm4eic/ReconstructedParticle.h"
0008 #include "edm4eic/MutableReconstructedParticle.h"
0009 #include "edm4eic/ReconstructedParticleObj.h"
0010 #include "edm4eic/ReconstructedParticleCollectionData.h"
0011
0012
0013 #include "podio/ICollectionProvider.h"
0014 #include "podio/CollectionBase.h"
0015
0016 #if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
0017 #include "nlohmann/json_fwd.hpp"
0018 #endif
0019
0020 #include <string_view>
0021 #include <vector>
0022 #include <algorithm>
0023 #include <ostream>
0024 #include <mutex>
0025 #include <memory>
0026 #include <cstddef>
0027
0028 namespace podio {
0029 struct RelationNames;
0030 }
0031
0032 namespace edm4eic {
0033
0034
0035
0036 class ReconstructedParticleCollectionIterator {
0037 public:
0038 ReconstructedParticleCollectionIterator(size_t index, const ReconstructedParticleObjPointerContainer* collection) : m_index(index), m_object(podio::utils::MaybeSharedPtr<ReconstructedParticleObj>{nullptr}), m_collection(collection) {}
0039
0040 ReconstructedParticleCollectionIterator(const ReconstructedParticleCollectionIterator&) = delete;
0041 ReconstructedParticleCollectionIterator& operator=(const ReconstructedParticleCollectionIterator&) = delete;
0042
0043 bool operator!=(const ReconstructedParticleCollectionIterator& x) const {
0044 return m_index != x.m_index;
0045 }
0046
0047 bool operator==(const ReconstructedParticleCollectionIterator& x) const {
0048 return m_index == x.m_index;
0049 }
0050
0051 ReconstructedParticle operator*();
0052 ReconstructedParticle* operator->();
0053 ReconstructedParticleCollectionIterator& operator++();
0054
0055 private:
0056 size_t m_index;
0057 ReconstructedParticle m_object;
0058 const ReconstructedParticleObjPointerContainer* m_collection;
0059 };
0060
0061
0062 class ReconstructedParticleMutableCollectionIterator {
0063 public:
0064 ReconstructedParticleMutableCollectionIterator(size_t index, const ReconstructedParticleObjPointerContainer* collection) : m_index(index), m_object(podio::utils::MaybeSharedPtr<ReconstructedParticleObj>{nullptr}), m_collection(collection) {}
0065
0066 ReconstructedParticleMutableCollectionIterator(const ReconstructedParticleMutableCollectionIterator&) = delete;
0067 ReconstructedParticleMutableCollectionIterator& operator=(const ReconstructedParticleMutableCollectionIterator&) = delete;
0068
0069 bool operator!=(const ReconstructedParticleMutableCollectionIterator& x) const {
0070 return m_index != x.m_index;
0071 }
0072
0073 bool operator==(const ReconstructedParticleMutableCollectionIterator& x) const {
0074 return m_index == x.m_index;
0075 }
0076
0077 MutableReconstructedParticle operator*();
0078 MutableReconstructedParticle* operator->();
0079 ReconstructedParticleMutableCollectionIterator& operator++();
0080
0081 private:
0082 size_t m_index;
0083 MutableReconstructedParticle m_object;
0084 const ReconstructedParticleObjPointerContainer* m_collection;
0085 };
0086
0087
0088
0089
0090
0091 class ReconstructedParticleCollection : public podio::CollectionBase {
0092 public:
0093 using value_type = ReconstructedParticle;
0094 using const_iterator = ReconstructedParticleCollectionIterator;
0095 using iterator = ReconstructedParticleMutableCollectionIterator;
0096 using difference_type = ptrdiff_t;
0097 using size_type = size_t;
0098
0099 ReconstructedParticleCollection();
0100 ReconstructedParticleCollection(ReconstructedParticleCollectionData&& data, bool isSubsetColl);
0101
0102 ReconstructedParticleCollection(const ReconstructedParticleCollection& ) = delete;
0103 ReconstructedParticleCollection& operator=(const ReconstructedParticleCollection& ) = delete;
0104 ReconstructedParticleCollection(ReconstructedParticleCollection&&) = default;
0105 ReconstructedParticleCollection& operator=(ReconstructedParticleCollection&&) = default;
0106
0107
0108 ~ReconstructedParticleCollection() override;
0109
0110 constexpr static auto typeName = "edm4eic::ReconstructedParticleCollection";
0111 constexpr static auto valueTypeName = "edm4eic::ReconstructedParticle";
0112 constexpr static auto dataTypeName = "edm4eic::ReconstructedParticleData";
0113
0114 void clear() final;
0115
0116
0117 void print(std::ostream& os=std::cout, bool flush=true) const final;
0118
0119
0120 ReconstructedParticleCollection* operator->() { return static_cast<ReconstructedParticleCollection*>(this); }
0121
0122
0123 MutableReconstructedParticle create();
0124
0125
0126
0127 template<typename... Args>
0128 MutableReconstructedParticle create(Args&&... args);
0129
0130
0131 std::size_t size() const final;
0132
0133
0134 std::size_t max_size() const final;
0135
0136
0137 bool empty() const final;
0138
0139
0140 const std::string_view getTypeName() const final { return typeName; }
0141
0142 const std::string_view getValueTypeName() const final { return valueTypeName; }
0143
0144 const std::string_view getDataTypeName() const final { return dataTypeName; }
0145
0146 podio::SchemaVersionT getSchemaVersion() const final;
0147
0148 bool isSubsetCollection() const final {
0149 return m_isSubsetColl;
0150 }
0151
0152 void setSubsetCollection(bool setSubset=true) final;
0153
0154
0155 ReconstructedParticle operator[](std::size_t index) const;
0156
0157 MutableReconstructedParticle operator[](std::size_t index);
0158
0159 ReconstructedParticle at(std::size_t index) const;
0160
0161 MutableReconstructedParticle at(std::size_t index);
0162
0163
0164
0165 void push_back(const MutableReconstructedParticle& object);
0166
0167 void push_back(const ReconstructedParticle& object);
0168
0169 void prepareForWrite() const final;
0170 void prepareAfterRead() final;
0171 bool setReferences(const podio::ICollectionProvider* collectionProvider) final;
0172
0173
0174 podio::CollectionWriteBuffers getBuffers() final;
0175
0176 void setID(uint32_t ID) final {
0177 m_collectionID = ID;
0178 if (!m_isSubsetColl) {
0179 std::for_each(m_storage.entries.begin(), m_storage.entries.end(),
0180 [ID] (ReconstructedParticleObj* obj) { obj->id = {obj->id.index, static_cast<uint32_t>(ID)}; }
0181 );
0182 }
0183 m_isValid = true;
0184 }
0185
0186 uint32_t getID() const final {
0187 return m_collectionID;
0188 }
0189
0190 bool isValid() const final {
0191 return m_isValid;
0192 }
0193
0194 size_t getDatamodelRegistryIndex() const final;
0195
0196
0197 iterator begin() {
0198 return iterator(0, &m_storage.entries);
0199 }
0200 const_iterator begin() const {
0201 return const_iterator(0, &m_storage.entries);
0202 }
0203 const_iterator cbegin() const {
0204 return begin();
0205 }
0206 iterator end() {
0207 return iterator(m_storage.entries.size(), &m_storage.entries);
0208 }
0209 const_iterator end() const {
0210 return const_iterator(m_storage.entries.size(), &m_storage.entries);
0211 }
0212 const_iterator cend() const {
0213 return end();
0214 }
0215
0216 std::vector<std::int32_t> type(const size_t nElem = 0) const;
0217 std::vector<float> energy(const size_t nElem = 0) const;
0218 std::vector<edm4hep::Vector3f> momentum(const size_t nElem = 0) const;
0219 std::vector<edm4hep::Vector3f> referencePoint(const size_t nElem = 0) const;
0220 std::vector<float> charge(const size_t nElem = 0) const;
0221 std::vector<float> mass(const size_t nElem = 0) const;
0222 std::vector<float> goodnessOfPID(const size_t nElem = 0) const;
0223 std::vector<edm4eic::Cov4f> covMatrix(const size_t nElem = 0) const;
0224 std::vector<std::int32_t> PDG(const size_t nElem = 0) const;
0225
0226 private:
0227
0228
0229
0230 friend class ReconstructedParticleCollectionData;
0231
0232 bool m_isValid{false};
0233 mutable bool m_isPrepared{false};
0234 bool m_isSubsetColl{false};
0235 uint32_t m_collectionID{0};
0236 mutable std::unique_ptr<std::mutex> m_storageMtx{nullptr};
0237 mutable ReconstructedParticleCollectionData m_storage{};
0238 };
0239
0240 std::ostream& operator<<(std::ostream& o, const ReconstructedParticleCollection& v);
0241
0242 template<typename... Args>
0243 MutableReconstructedParticle ReconstructedParticleCollection::create(Args&&... args) {
0244 if (m_isSubsetColl) {
0245 throw std::logic_error("Cannot create new elements on a subset collection");
0246 }
0247 const int size = m_storage.entries.size();
0248 auto obj = new ReconstructedParticleObj({size, m_collectionID}, {std::forward<Args>(args)...});
0249 m_storage.entries.push_back(obj);
0250
0251
0252 obj->m_clusters = new std::vector<edm4eic::Cluster>();
0253 obj->m_tracks = new std::vector<edm4eic::Track>();
0254 obj->m_particles = new std::vector<edm4eic::ReconstructedParticle>();
0255 obj->m_particleIDs = new std::vector<edm4hep::ParticleID>();
0256 m_storage.createRelations(obj);
0257 return MutableReconstructedParticle(podio::utils::MaybeSharedPtr(obj));
0258 }
0259
0260 #if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
0261 void to_json(nlohmann::json& j, const ReconstructedParticleCollection& collection);
0262 #endif
0263
0264 }
0265
0266
0267 #endif