File indexing completed on 2026-10-02 09:01:29
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 void clear() final;
0167
0168
0169 static void __cppyy_pythonize__(PyObject* klass, const std::string& name) {
0170 podio::detail::pythonizations::pythonize_subscript(klass, name);
0171 }
0172
0173
0174 void print(std::ostream& os = std::cout, bool flush = true) const final;
0175
0176
0177 MutableEventHeader create();
0178
0179
0180
0181 template <typename... Args>
0182 MutableEventHeader create(Args&&... args);
0183
0184
0185 std::size_t size() const final;
0186
0187
0188 std::size_t max_size() const final;
0189
0190
0191 bool empty() const final;
0192
0193
0194 const std::string_view getTypeName() const final { return typeName; }
0195
0196 const std::string_view getValueTypeName() const final { return valueTypeName; }
0197
0198 const std::string_view getDataTypeName() const final { return dataTypeName; }
0199
0200 podio::SchemaVersionT getSchemaVersion() const final;
0201
0202 bool isSubsetCollection() const final { return m_isSubsetColl; }
0203
0204 void setSubsetCollection(bool setSubset = true) final;
0205
0206
0207 EventHeader operator[](std::size_t index) const;
0208
0209 MutableEventHeader operator[](std::size_t index);
0210
0211 EventHeader at(std::size_t index) const;
0212
0213 MutableEventHeader at(std::size_t index);
0214
0215
0216 void push_back(const MutableEventHeader& object);
0217
0218 void push_back(const EventHeader& object);
0219
0220 void prepareForWrite() const final;
0221 void prepareAfterRead() final;
0222 bool setReferences(const podio::ICollectionProvider* collectionProvider) final;
0223
0224
0225 podio::CollectionWriteBuffers getBuffers() final;
0226
0227 void setID(uint32_t ID) final {
0228 m_collectionID = ID;
0229 if (!m_isSubsetColl) {
0230 std::for_each(m_storage.entries.begin(), m_storage.entries.end(),
0231 [ID](EventHeaderObj* obj) { obj->id = {obj->id.index, static_cast<uint32_t>(ID)}; });
0232 }
0233 }
0234
0235 uint32_t getID() const final { return m_collectionID; }
0236
0237
0238 bool hasID() const final {
0239 return getID() != static_cast<uint32_t>(podio::ObjectID::untracked) &&
0240 getID() != static_cast<uint32_t>(podio::ObjectID::invalid);
0241 }
0242
0243 [[deprecated("isValid will be removed, use hasID() if you want to check if it has an ID, otherwise assume the "
0244 "collection is valid")]] bool
0245 isValid() const final {
0246 return hasID();
0247 }
0248
0249 size_t getDatamodelRegistryIndex() const final;
0250
0251
0252 iterator begin() { return iterator(0, &m_storage.entries); }
0253 const_iterator begin() const { return const_iterator(0, &m_storage.entries); }
0254 const_iterator cbegin() const { return begin(); }
0255 iterator end() { return iterator(m_storage.entries.size(), &m_storage.entries); }
0256 const_iterator end() const { return const_iterator(m_storage.entries.size(), &m_storage.entries); }
0257 const_iterator cend() const { return end(); }
0258
0259 reverse_iterator rbegin() { return reverse_iterator(end()); }
0260 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
0261 const_reverse_iterator crbegin() const { return rbegin(); }
0262 reverse_iterator rend() { return reverse_iterator(begin()); }
0263 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
0264 const_reverse_iterator crend() const { return rend(); }
0265
0266 std::vector<std::uint64_t> eventNumber(const size_t nElem = 0) const;
0267 std::vector<std::uint32_t> runNumber(const size_t nElem = 0) const;
0268 std::vector<std::uint64_t> timeStamp(const size_t nElem = 0) const;
0269 std::vector<double> weight(const size_t nElem = 0) const;
0270
0271 private:
0272
0273
0274
0275 friend class EventHeaderCollectionData;
0276
0277 mutable bool m_isPrepared{false};
0278 bool m_isSubsetColl{false};
0279 uint32_t m_collectionID{static_cast<uint32_t>(podio::ObjectID::untracked)};
0280 mutable std::unique_ptr<std::mutex> m_storageMtx{std::make_unique<std::mutex>()};
0281 mutable EventHeaderCollectionData m_storage{};
0282 };
0283
0284 std::ostream& operator<<(std::ostream& o, const EventHeaderCollection& v);
0285
0286 template <typename... Args>
0287 MutableEventHeader EventHeaderCollection::create(Args&&... args) {
0288 if (m_isSubsetColl) {
0289 throw std::logic_error("Cannot create new elements on a subset collection");
0290 }
0291 auto obj =
0292 new EventHeaderObj({static_cast<int>(m_storage.entries.size()), m_collectionID}, {std::forward<Args>(args)...});
0293 m_storage.entries.push_back(obj);
0294
0295
0296 obj->m_weights = new std::vector<double>();
0297 m_storage.createRelations(obj);
0298 return MutableEventHeader(podio::utils::MaybeSharedPtr(obj));
0299 }
0300
0301 #if defined(__cpp_lib_containers_ranges)
0302 template <podio::detail::RangeConvertibleTo<EventHeaderCollection::value_type> R>
0303 EventHeaderCollection::EventHeaderCollection(std::from_range_t, R&& range) : EventHeaderCollection() {
0304 setSubsetCollection(podio::detail::RangeOf<R, value_type>);
0305
0306 for (auto&& elem : range) {
0307 push_back(std::forward<decltype(elem)>(elem));
0308 }
0309 }
0310 #endif
0311
0312 template <podio::detail::RangeConvertibleTo<EventHeaderCollection::value_type> R>
0313 EventHeaderCollection EventHeaderCollection::from(R&& range) {
0314 EventHeaderCollection coll;
0315 if constexpr (podio::detail::RangeOf<R, value_type>) {
0316 coll.setSubsetCollection();
0317 }
0318
0319 for (auto&& elem : range) {
0320 coll.push_back(std::forward<decltype(elem)>(elem));
0321 }
0322
0323 return coll;
0324 }
0325
0326 #if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
0327 void to_json(nlohmann::json& j, const EventHeaderCollection& collection);
0328 #endif
0329
0330 }
0331
0332
0333
0334
0335 #if defined(__clang__)
0336 #pragma clang diagnostic push
0337 #pragma clang diagnostic ignored "-Wunknown-warning-option"
0338 #pragma clang diagnostic ignored "-Wdeprecated-redundant-constexpr-static-def"
0339 #pragma clang diagnostic ignored "-Wdeprecated"
0340 constexpr std::string_view edm4hep::EventHeaderCollection::typeName;
0341 constexpr std::string_view edm4hep::EventHeaderCollection::valueTypeName;
0342 constexpr std::string_view edm4hep::EventHeaderCollection::dataTypeName;
0343 #pragma clang diagnostic pop
0344 #elif defined(__GNUC__)
0345 #pragma GCC diagnostic push
0346 #pragma GCC diagnostic ignored "-Wdeprecated"
0347 constexpr std::string_view edm4hep::EventHeaderCollection::typeName;
0348 constexpr std::string_view edm4hep::EventHeaderCollection::valueTypeName;
0349 constexpr std::string_view edm4hep::EventHeaderCollection::dataTypeName;
0350 #pragma GCC diagnostic pop
0351 #endif
0352
0353 #endif