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