Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-04 11:03:01

0001 #ifndef PODIO_DETAIL_LINKCOLLECTIONDATA_H
0002 #define PODIO_DETAIL_LINKCOLLECTIONDATA_H
0003 
0004 #include "podio/detail/LinkFwd.h"
0005 #include "podio/detail/LinkObj.h"
0006 
0007 #include "podio/CollectionBase.h"
0008 #include "podio/CollectionBuffers.h"
0009 #include "podio/ICollectionProvider.h"
0010 #include "podio/detail/RelationIOHelpers.h"
0011 
0012 #include <memory>
0013 #include <vector>
0014 
0015 namespace podio {
0016 
0017 template <typename FromT, typename ToT>
0018 class LinkCollectionData {
0019 public:
0020   LinkObjPointerContainer<FromT, ToT> entries{};
0021 
0022   LinkCollectionData() :
0023       m_rel_from(std::make_unique<std::vector<FromT>>()),
0024       m_rel_to(std::make_unique<std::vector<ToT>>()),
0025       m_data(std::make_unique<LinkDataContainer>()) {
0026     m_refCollections.reserve(2);
0027     m_refCollections.emplace_back(std::make_unique<std::vector<podio::ObjectID>>());
0028     m_refCollections.emplace_back(std::make_unique<std::vector<podio::ObjectID>>());
0029   }
0030 
0031   LinkCollectionData(podio::CollectionReadBuffers&& buffers, bool isSubsetColl) :
0032       m_rel_from(std::make_unique<std::vector<FromT>>()),
0033       m_rel_to(std::make_unique<std::vector<ToT>>()),
0034       m_refCollections(std::move(*buffers.references)) {
0035     if (!isSubsetColl) {
0036       m_data.reset(buffers.dataAsVector<LinkData>());
0037       buffers.data = nullptr;
0038     }
0039 
0040     delete buffers.references;
0041     buffers.references = nullptr;
0042     delete buffers.vectorMembers;
0043     buffers.vectorMembers = nullptr;
0044   }
0045 
0046   LinkCollectionData(const LinkCollectionData&) = delete;
0047   LinkCollectionData& operator=(const LinkCollectionData&) = delete;
0048   LinkCollectionData(LinkCollectionData&&) = default;
0049   LinkCollectionData& operator=(LinkCollectionData&&) = default;
0050   ~LinkCollectionData() = default;
0051 
0052   podio::CollectionWriteBuffers getCollectionBuffers(bool isSubsetColl) {
0053     return {isSubsetColl ? nullptr : static_cast<void*>(&m_data), static_cast<void*>(m_data.get()), &m_refCollections,
0054             &m_vecInfo};
0055   }
0056 
0057   void clear(bool isSubsetColl) {
0058     if (isSubsetColl) {
0059       // We don't own the objects so no cleanup to do here
0060       entries.clear();
0061       // Clear the ObjectID I/O buffer
0062       for (auto& pointer : m_refCollections) {
0063         pointer->clear();
0064       }
0065       return;
0066     }
0067 
0068     // Normal collections manage a bit more and have to clean up a bit more
0069     if (m_data) {
0070       m_data->clear();
0071     }
0072     for (const auto& pointer : m_refCollections) {
0073       pointer->clear();
0074     }
0075     if (m_rel_from) {
0076       for (auto& item : (*m_rel_from)) {
0077         item.unlink();
0078       }
0079       m_rel_from->clear();
0080     }
0081 
0082     if (m_rel_to) {
0083       for (auto& item : (*m_rel_to)) {
0084         item.unlink();
0085       }
0086       m_rel_to->clear();
0087     }
0088 
0089     for (const auto& obj : entries) {
0090       delete obj;
0091     }
0092     entries.clear();
0093   }
0094 
0095   void prepareForWrite(bool isSubsetColl) {
0096     for (const auto& pointer : m_refCollections) {
0097       pointer->clear();
0098     }
0099 
0100     // If this is a subset collection use the relation storing mechanism to
0101     // store the ObjectIDs of all reference objects and nothing else
0102     if (isSubsetColl) {
0103       for (const auto* obj : entries) {
0104         m_refCollections[0]->emplace_back(obj->id);
0105       }
0106       return;
0107     }
0108 
0109     m_data->reserve(entries.size());
0110     for (const auto obj : entries) {
0111       m_data->push_back(obj->data);
0112 
0113       if (obj->m_from) {
0114         m_refCollections[0]->emplace_back(obj->m_from->getObjectID());
0115       } else {
0116         m_refCollections[0]->push_back({podio::ObjectID::invalid, 0});
0117       }
0118 
0119       if (obj->m_to) {
0120         m_refCollections[1]->emplace_back(obj->m_to->getObjectID());
0121       } else {
0122         m_refCollections[1]->push_back({podio::ObjectID::invalid, 0});
0123       }
0124     }
0125   }
0126 
0127   void prepareAfterRead(uint32_t collectionID) {
0128     int index = 0;
0129     for (const auto data : *m_data) {
0130       auto obj = new LinkObj<FromT, ToT>({index++, collectionID}, data);
0131       entries.emplace_back(obj);
0132     }
0133 
0134     // Keep the I/O data buffer to keep the preparedForWrite state intact
0135   }
0136 
0137   bool setReferences(const podio::ICollectionProvider* collectionProvider, bool isSubsetColl) {
0138     if (isSubsetColl) {
0139       for (const auto& id : *m_refCollections[0]) {
0140         podio::CollectionBase* coll{nullptr};
0141         LinkObj<FromT, ToT>* obj{nullptr};
0142         if (collectionProvider->get(id.collectionID, coll)) {
0143           auto* tmp_coll = static_cast<LinkCollection<FromT, ToT>*>(coll);
0144           obj = tmp_coll->m_storage.entries[id.index];
0145         }
0146         entries.push_back(obj);
0147       }
0148       return true; // TODO: check success, how?
0149     }
0150 
0151     // Normal collections have to resolve all relations
0152     for (size_t i = 0; i < entries.size(); ++i) {
0153       const auto id = (*m_refCollections[0])[i];
0154       if (id.index != podio::ObjectID::invalid) {
0155         podio::CollectionBase* coll = nullptr;
0156         if (!collectionProvider->get(id.collectionID, coll)) {
0157           entries[i]->m_from = nullptr;
0158           continue;
0159         }
0160         podio::detail::addSingleRelation(entries[i]->m_from, coll, id);
0161       } else {
0162         entries[i]->m_from = nullptr;
0163       }
0164     }
0165 
0166     for (size_t i = 0; i < entries.size(); ++i) {
0167       const auto id = (*m_refCollections[1])[i];
0168       if (id.index != podio::ObjectID::invalid) {
0169         podio::CollectionBase* coll = nullptr;
0170         if (!collectionProvider->get(id.collectionID, coll)) {
0171           entries[i]->m_to = nullptr;
0172           continue;
0173         }
0174         podio::detail::addSingleRelation(entries[i]->m_to, coll, id);
0175       } else {
0176         entries[i]->m_to = nullptr;
0177       }
0178     }
0179 
0180     return true; // TODO: check success, how?
0181   }
0182 
0183   void makeSubsetCollection() {
0184     // Subset collections do not need all the data buffers that normal
0185     // collections need, so we can free them here
0186     m_data.reset(nullptr);
0187 
0188     m_rel_from.reset(nullptr);
0189     m_rel_to.reset(nullptr);
0190 
0191     // Subset collections need one vector of ObjectIDs for I/O purposes.
0192     m_refCollections.resize(1);
0193     m_refCollections[0] = std::make_unique<std::vector<podio::ObjectID>>();
0194   }
0195 
0196 private:
0197   // members to handle relations
0198   podio::UVecPtr<FromT> m_rel_from{nullptr};
0199   podio::UVecPtr<ToT> m_rel_to{nullptr};
0200 
0201   // I/O related buffers (as far as necessary)
0202   podio::CollRefCollection m_refCollections{};
0203   podio::VectorMembersInfo m_vecInfo{};
0204   std::unique_ptr<LinkDataContainer> m_data{nullptr};
0205 };
0206 
0207 } // namespace podio
0208 
0209 #endif // PODIO_DETAIL_LINKCOLLECTIONDATA_H