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