Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:42

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2023 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <any>
0012 #include <string>
0013 #include <string_view>
0014 
0015 #include <podio/Frame.h>
0016 #include <podio/UserDataCollection.h>
0017 
0018 namespace Acts::podio_detail {
0019 
0020 struct ConstDynamicColumnBase {
0021   ConstDynamicColumnBase(std::string_view name) : m_name{name} {}
0022 
0023   virtual ~ConstDynamicColumnBase() = default;
0024 
0025   virtual std::any get(std::size_t i) const = 0;
0026 
0027   virtual std::size_t size() const = 0;
0028 
0029  protected:
0030   std::string m_name;
0031 };
0032 
0033 template <typename T>
0034 struct ConstDynamicColumn : public ConstDynamicColumnBase {
0035   ConstDynamicColumn(std::string_view name,
0036                      const podio::UserDataCollection<T>& collection)
0037       : ConstDynamicColumnBase(name), m_collection{collection} {}
0038 
0039   std::any get(std::size_t i) const override {
0040     return &m_collection.vec().at(i);
0041   }
0042   std::size_t size() const override { return m_collection.size(); }
0043 
0044   const podio::UserDataCollection<T>& m_collection;
0045 };
0046 
0047 struct DynamicColumnBase : public ConstDynamicColumnBase {
0048   DynamicColumnBase(std::string_view name) : ConstDynamicColumnBase{name} {}
0049 
0050   virtual std::any get(std::size_t i) = 0;
0051   std::any get(std::size_t i) const override = 0;
0052 
0053   virtual void add() = 0;
0054   virtual void clear() = 0;
0055   virtual void erase(std::size_t i) = 0;
0056   virtual void copyFrom(std::size_t dstIdx, const DynamicColumnBase& src,
0057                         std::size_t srcIdx) = 0;
0058   virtual void copyFrom(std::size_t dstIdx, const std::any& srcPtr) = 0;
0059 
0060   virtual std::unique_ptr<DynamicColumnBase> clone(
0061       bool empty = false) const = 0;
0062 
0063   virtual std::unique_ptr<ConstDynamicColumnBase> asConst() const = 0;
0064 
0065   virtual void releaseInto(podio::Frame& frame, const std::string& prefix) = 0;
0066 };
0067 
0068 template <typename T>
0069 struct DynamicColumn : public DynamicColumnBase {
0070   DynamicColumn(std::string_view name,
0071                 podio::UserDataCollection<T> collection = {})
0072       : DynamicColumnBase(name), m_collection{std::move(collection)} {}
0073 
0074   std::any get(std::size_t i) override { return &m_collection.vec().at(i); }
0075 
0076   std::any get(std::size_t i) const override {
0077     return &m_collection.vec().at(i);
0078   }
0079 
0080   void add() override { m_collection.vec().emplace_back(); }
0081   void clear() override { m_collection.clear(); }
0082   void erase(std::size_t i) override {
0083     m_collection.vec().erase(m_collection.vec().begin() + i);
0084   }
0085   std::size_t size() const override { return m_collection.size(); }
0086 
0087   std::unique_ptr<DynamicColumnBase> clone(bool empty) const override {
0088     if (empty) {
0089       return std::make_unique<DynamicColumn<T>>(m_name);
0090     }
0091     podio::UserDataCollection<T> copy;
0092     copy.vec().reserve(m_collection.size());
0093     for (const T& v : m_collection) {
0094       copy.push_back(v);
0095     }
0096     return std::make_unique<DynamicColumn<T>>(m_name, std::move(copy));
0097   }
0098 
0099   std::unique_ptr<ConstDynamicColumnBase> asConst() const override {
0100     return std::make_unique<ConstDynamicColumn<T>>(m_name, m_collection);
0101   }
0102 
0103   void copyFrom(std::size_t dstIdx, const DynamicColumnBase& src,
0104                 std::size_t srcIdx) override {
0105     const auto* other = dynamic_cast<const DynamicColumn<T>*>(&src);
0106     assert(other != nullptr &&
0107            "Source column is not of same type as destination");
0108     m_collection.vec().at(dstIdx) = other->m_collection.vec().at(srcIdx);
0109   }
0110 
0111   void copyFrom(std::size_t dstIdx, const std::any& srcPtr) override {
0112     const auto* other = std::any_cast<const T*>(srcPtr);
0113     assert(other != nullptr &&
0114            "Source column is not of same type as destination");
0115     m_collection.vec().at(dstIdx) = *other;
0116   }
0117 
0118   void releaseInto(podio::Frame& frame, const std::string& prefix) override {
0119     frame.put(std::move(m_collection), prefix + m_name);
0120   }
0121 
0122   podio::UserDataCollection<T> m_collection;
0123 };
0124 
0125 }  // namespace Acts::podio_detail