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