File indexing completed on 2025-09-18 09:18:42
0001
0002
0003 #ifndef PODIODATAMODEL_EventInfoCollection_H
0004 #define PODIODATAMODEL_EventInfoCollection_H
0005
0006
0007 #include "PodioDatamodel/EventInfo.h"
0008 #include "PodioDatamodel/MutableEventInfo.h"
0009 #include "PodioDatamodel/EventInfoObj.h"
0010 #include "PodioDatamodel/EventInfoCollectionData.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
0033
0034
0035 class EventInfoCollectionIterator {
0036 public:
0037 using value_type = EventInfo;
0038 using difference_type = ptrdiff_t;
0039 using reference = EventInfo;
0040 using pointer = EventInfo*;
0041 using iterator_category = std::input_iterator_tag;
0042
0043
0044 using iterator_concept = std::random_access_iterator_tag;
0045
0046 EventInfoCollectionIterator(size_t index, const EventInfoObjPointerContainer* collection) : m_index(index), m_object(podio::utils::MaybeSharedPtr<EventInfoObj>{nullptr}), m_collection(collection) {}
0047 EventInfoCollectionIterator() = default;
0048
0049 EventInfoCollectionIterator(const EventInfoCollectionIterator&) = default;
0050 EventInfoCollectionIterator(EventInfoCollectionIterator&&) = default;
0051 EventInfoCollectionIterator& operator=(const EventInfoCollectionIterator&) = default;
0052 EventInfoCollectionIterator& operator=(EventInfoCollectionIterator&&) = default;
0053 ~EventInfoCollectionIterator() = default;
0054
0055 auto operator<=>(const EventInfoCollectionIterator& other) const {
0056 return m_index <=> other.m_index;
0057 }
0058
0059 bool operator==(const EventInfoCollectionIterator& x) const {
0060 return m_index == x.m_index;
0061 }
0062
0063 reference operator*() const;
0064 pointer operator->();
0065 EventInfoCollectionIterator& operator++();
0066 EventInfoCollectionIterator operator++(int);
0067 EventInfoCollectionIterator& operator--();
0068 EventInfoCollectionIterator operator--(int);
0069 EventInfoCollectionIterator& operator+=(difference_type n);
0070 EventInfoCollectionIterator operator+(difference_type n) const;
0071 friend EventInfoCollectionIterator operator+(difference_type n, const EventInfoCollectionIterator& it);
0072 EventInfoCollectionIterator& operator-=(difference_type n);
0073 EventInfoCollectionIterator operator-(difference_type n) const;
0074 reference operator[](difference_type n) const;
0075 difference_type operator-(const EventInfoCollectionIterator& other) const;
0076
0077 private:
0078 size_t m_index{0};
0079 EventInfo m_object { podio::utils::MaybeSharedPtr<EventInfoObj>{nullptr} };
0080 const EventInfoObjPointerContainer* m_collection{nullptr};
0081 };
0082
0083
0084 class EventInfoMutableCollectionIterator {
0085 public:
0086 using value_type = EventInfo;
0087 using difference_type = ptrdiff_t;
0088 using reference = MutableEventInfo;
0089 using pointer = MutableEventInfo*;
0090 using iterator_category = std::input_iterator_tag;
0091
0092
0093 using iterator_concept = std::random_access_iterator_tag;
0094
0095 EventInfoMutableCollectionIterator(size_t index, const EventInfoObjPointerContainer* collection) : m_index(index), m_object(podio::utils::MaybeSharedPtr<EventInfoObj>{nullptr}), m_collection(collection) {}
0096 EventInfoMutableCollectionIterator() = default;
0097
0098 EventInfoMutableCollectionIterator(const EventInfoMutableCollectionIterator&) = default;
0099 EventInfoMutableCollectionIterator(EventInfoMutableCollectionIterator&&) = default;
0100 EventInfoMutableCollectionIterator& operator=(const EventInfoMutableCollectionIterator&) = default;
0101 EventInfoMutableCollectionIterator& operator=(EventInfoMutableCollectionIterator&&) = default;
0102 ~EventInfoMutableCollectionIterator() = default;
0103
0104 auto operator<=>(const EventInfoMutableCollectionIterator& other) const {
0105 return m_index <=> other.m_index;
0106 }
0107
0108 bool operator==(const EventInfoMutableCollectionIterator& x) const {
0109 return m_index == x.m_index;
0110 }
0111
0112 reference operator*() const;
0113 pointer operator->();
0114 EventInfoMutableCollectionIterator& operator++();
0115 EventInfoMutableCollectionIterator operator++(int);
0116 EventInfoMutableCollectionIterator& operator--();
0117 EventInfoMutableCollectionIterator operator--(int);
0118 EventInfoMutableCollectionIterator& operator+=(difference_type n);
0119 EventInfoMutableCollectionIterator operator+(difference_type n) const;
0120 friend EventInfoMutableCollectionIterator operator+(difference_type n, const EventInfoMutableCollectionIterator& it);
0121 EventInfoMutableCollectionIterator& operator-=(difference_type n);
0122 EventInfoMutableCollectionIterator operator-(difference_type n) const;
0123 reference operator[](difference_type n) const;
0124 difference_type operator-(const EventInfoMutableCollectionIterator& other) const;
0125
0126 private:
0127 size_t m_index{0};
0128 MutableEventInfo m_object { podio::utils::MaybeSharedPtr<EventInfoObj>{nullptr} };
0129 const EventInfoObjPointerContainer* m_collection{nullptr};
0130 };
0131
0132
0133
0134
0135
0136 class EventInfoCollection : public podio::CollectionBase {
0137 public:
0138 using value_type = EventInfo;
0139 using mutable_type = MutableEventInfo;
0140 using const_iterator = EventInfoCollectionIterator;
0141 using iterator = EventInfoMutableCollectionIterator;
0142 using difference_type = ptrdiff_t;
0143 using size_type = size_t;
0144 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
0145 using reverse_iterator = std::reverse_iterator<iterator>;
0146
0147 EventInfoCollection();
0148 EventInfoCollection(EventInfoCollectionData&& data, bool isSubsetColl);
0149
0150 EventInfoCollection(const EventInfoCollection& ) = delete;
0151 EventInfoCollection& operator=(const EventInfoCollection& ) = delete;
0152 EventInfoCollection(EventInfoCollection&&) = default;
0153 EventInfoCollection& operator=(EventInfoCollection&&) = default;
0154
0155
0156 ~EventInfoCollection() override;
0157
0158 constexpr static std::string_view typeName = "EventInfoCollection";
0159 constexpr static std::string_view valueTypeName = "EventInfo";
0160 constexpr static std::string_view dataTypeName = "EventInfoData";
0161
0162 void clear() final;
0163
0164
0165 void print(std::ostream& os=std::cout, bool flush=true) const final;
0166
0167
0168 EventInfoCollection* operator->() { return static_cast<EventInfoCollection*>(this); }
0169
0170
0171 MutableEventInfo create();
0172
0173
0174
0175 template<typename... Args>
0176 MutableEventInfo create(Args&&... args);
0177
0178
0179 std::size_t size() const final;
0180
0181
0182 std::size_t max_size() const final;
0183
0184
0185 bool empty() const final;
0186
0187
0188 const std::string_view getTypeName() const final { return typeName; }
0189
0190 const std::string_view getValueTypeName() const final { return valueTypeName; }
0191
0192 const std::string_view getDataTypeName() const final { return dataTypeName; }
0193
0194 podio::SchemaVersionT getSchemaVersion() const final;
0195
0196 bool isSubsetCollection() const final {
0197 return m_isSubsetColl;
0198 }
0199
0200 void setSubsetCollection(bool setSubset=true) final;
0201
0202
0203 EventInfo operator[](std::size_t index) const;
0204
0205 MutableEventInfo operator[](std::size_t index);
0206
0207 EventInfo at(std::size_t index) const;
0208
0209 MutableEventInfo at(std::size_t index);
0210
0211
0212
0213 void push_back(const MutableEventInfo& object);
0214
0215 void push_back(const EventInfo& object);
0216
0217 void prepareForWrite() const final;
0218 void prepareAfterRead() final;
0219 bool setReferences(const podio::ICollectionProvider* collectionProvider) final;
0220
0221
0222 podio::CollectionWriteBuffers getBuffers() final;
0223
0224 void setID(uint32_t ID) final {
0225 m_collectionID = ID;
0226 if (!m_isSubsetColl) {
0227 std::for_each(m_storage.entries.begin(), m_storage.entries.end(),
0228 [ID] (EventInfoObj* obj) { obj->id = {obj->id.index, static_cast<uint32_t>(ID)}; }
0229 );
0230 }
0231 m_isValid = true;
0232 }
0233
0234 uint32_t getID() const final {
0235 return m_collectionID;
0236 }
0237
0238 bool isValid() const final {
0239 return m_isValid;
0240 }
0241
0242 size_t getDatamodelRegistryIndex() const final;
0243
0244
0245 iterator begin() {
0246 return iterator(0, &m_storage.entries);
0247 }
0248 const_iterator begin() const {
0249 return const_iterator(0, &m_storage.entries);
0250 }
0251 const_iterator cbegin() const {
0252 return begin();
0253 }
0254 iterator end() {
0255 return iterator(m_storage.entries.size(), &m_storage.entries);
0256 }
0257 const_iterator end() const {
0258 return const_iterator(m_storage.entries.size(), &m_storage.entries);
0259 }
0260 const_iterator cend() const {
0261 return end();
0262 }
0263
0264 reverse_iterator rbegin() {
0265 return reverse_iterator(end());
0266 }
0267 const_reverse_iterator rbegin() const {
0268 return const_reverse_iterator(end());
0269 }
0270 const_reverse_iterator crbegin() const {
0271 return rbegin();
0272 }
0273 reverse_iterator rend() {
0274 return reverse_iterator(begin());
0275 }
0276 const_reverse_iterator rend() const {
0277 return const_reverse_iterator(begin());
0278 }
0279 const_reverse_iterator crend() const {
0280 return rend();
0281 }
0282
0283
0284 std::vector<int> EventNumber(const size_t nElem = 0) const;
0285 std::vector<int> TimesliceNumber(const size_t nElem = 0) const;
0286 std::vector<int> RunNumber(const size_t nElem = 0) const;
0287
0288 private:
0289
0290
0291
0292 friend class EventInfoCollectionData;
0293
0294 bool m_isValid{false};
0295 mutable bool m_isPrepared{false};
0296 bool m_isSubsetColl{false};
0297 uint32_t m_collectionID{0};
0298 mutable std::unique_ptr<std::mutex> m_storageMtx{nullptr};
0299 mutable EventInfoCollectionData m_storage{};
0300 };
0301
0302 std::ostream& operator<<(std::ostream& o, const EventInfoCollection& v);
0303
0304 template<typename... Args>
0305 MutableEventInfo EventInfoCollection::create(Args&&... args) {
0306 if (m_isSubsetColl) {
0307 throw std::logic_error("Cannot create new elements on a subset collection");
0308 }
0309 const int size = m_storage.entries.size();
0310 auto obj = new EventInfoObj({size, m_collectionID}, {std::forward<Args>(args)...});
0311 m_storage.entries.push_back(obj);
0312
0313 return MutableEventInfo(podio::utils::MaybeSharedPtr(obj));
0314 }
0315
0316 #if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
0317 void to_json(nlohmann::json& j, const EventInfoCollection& collection);
0318 #endif
0319
0320
0321
0322 #endif