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