Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PodioDynamicColumns.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PodioDynamicColumns.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include <any>
12 #include <string>
13 
14 #include <podio/Frame.h>
15 #include <podio/UserDataCollection.h>
16 
17 namespace Acts::podio_detail {
18 
21 
22  virtual ~ConstDynamicColumnBase() = default;
23 
24  virtual std::any get(size_t i) const = 0;
25 
26  virtual size_t size() const = 0;
27 
28  protected:
30 };
31 
34 
35  virtual std::any get(size_t i) = 0;
36  std::any get(size_t i) const override = 0;
37 
38  virtual void add() = 0;
39  virtual void clear() = 0;
40  virtual void erase(size_t i) = 0;
41  virtual void copyFrom(size_t dstIdx, const DynamicColumnBase& src,
42  size_t srcIdx) = 0;
43 
44  virtual std::unique_ptr<DynamicColumnBase> clone(
45  bool empty = false) const = 0;
46 
47  virtual void releaseInto(podio::Frame& frame, const std::string& prefix) = 0;
48 };
49 
50 template <typename T>
53  podio::UserDataCollection<T> collection = {})
54  : DynamicColumnBase(name), m_collection{std::move(collection)} {}
55 
56  std::any get(size_t i) override { return &m_collection.vec().at(i); }
57 
58  std::any get(size_t i) const override { return &m_collection.vec().at(i); }
59 
60  void add() override { m_collection.vec().emplace_back(); }
61  void clear() override { m_collection.clear(); }
62  void erase(size_t i) override {
63  m_collection.vec().erase(m_collection.vec().begin() + i);
64  }
65  size_t size() const override { return m_collection.size(); }
66 
67  std::unique_ptr<DynamicColumnBase> clone(bool empty) const override {
68  if (empty) {
69  return std::make_unique<DynamicColumn<T>>(m_name);
70  }
71  podio::UserDataCollection<T> copy;
72  copy.vec().reserve(m_collection.size());
73  for (const T& v : m_collection) {
74  copy.push_back(v);
75  }
76  return std::make_unique<DynamicColumn<T>>(m_name, std::move(copy));
77  }
78 
79  void copyFrom(size_t dstIdx, const DynamicColumnBase& src,
80  size_t srcIdx) override {
81  const auto* other = dynamic_cast<const DynamicColumn<T>*>(&src);
82  assert(other != nullptr &&
83  "Source column is not of same type as destination");
84  m_collection.vec().at(dstIdx) = other->m_collection.vec().at(srcIdx);
85  }
86 
87  void releaseInto(podio::Frame& frame, const std::string& prefix) override {
88  frame.put(std::move(m_collection), prefix + m_name);
89  }
90 
91  podio::UserDataCollection<T> m_collection;
92 };
93 
94 template <typename T>
97  const podio::UserDataCollection<T>& collection)
98  : ConstDynamicColumnBase(name), m_collection{collection} {}
99 
100  std::any get(size_t i) const override { return &m_collection.vec().at(i); }
101  size_t size() const override { return m_collection.size(); }
102 
103  const podio::UserDataCollection<T>& m_collection;
104 };
105 } // namespace Acts::podio_detail