ATLAS Offline Software
Loading...
Searching...
No Matches
VariantAccessor.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8#ifndef COLUMNAR_VARIANT_VARIANT_ACCESSOR_H
9#define COLUMNAR_VARIANT_VARIANT_ACCESSOR_H
10
13#include <boost/mp11/tuple.hpp>
14
15namespace columnar
16{
17 // the column accessor for variant objects in ColumnarModeXAOD
18 //
19 // This just wraps a regular accessor, as in xAOD mode variants aren't
20 // really a thing.
21 template<ContainerIdConcept CIBase,ContainerIdConcept... CIList,typename CT,ColumnAccessMode CAM>
23 class AccessorTemplate<VariantContainerId<CIBase,CIList...>,CT,CAM,ColumnarModeXAOD> final
24 {
27 public:
28
29 static_assert (!std::is_const_v<CT>, "CT must not be const");
30
31 using CI = VariantContainerId<CIBase,CIList...>;
33 using AccessorTuple = std::tuple<AccessorTemplate<CIList,CT,CAM,CM>...>;
34
35 AccessorTemplate () noexcept = default;
36
37 AccessorTemplate (ColumnarTool<CM>& columnarTool, const std::string& name, ColumnInfo&& info = {})
38 {
39 resetAccessor (m_accessor, columnarTool, name, ColumnInfo(info));
40 }
41
43 {
44 m_accessor = std::move (that.m_accessor);
45 }
46
48 {
49 if (this != &that)
50 m_accessor = std::move (that.m_accessor);
51 return *this;
52 }
53
55 AccessorTemplate& operator = (const AccessorTemplate&) = delete;
56
57 void reset (ColumnarTool<CM>& columnarTool, const std::string& name, ColumnInfo&& info = {})
58 {
59 resetAccessor (m_accessor, columnarTool, name, ColumnInfo(info));
60 }
61
62 [[nodiscard]] decltype(auto) operator () (ObjectId<CI,CM> id) const noexcept
63 {
64 return m_accessor (id.getBaseObject());
65 }
66
67 [[nodiscard]] bool isAvailable (ObjectId<CI,CM> id) const noexcept
68 {
69 return m_accessor.isAvailable (id.getBaseObject());
70 }
71
74 private:
75
77 };
78
79
80
81 // the column accessor for variant objects in array columnar modes
82 //
83 // This internally contains a tuple of accessors, one for each
84 // variant. This is probably not the best way to implement it, but it
85 // fits best with the current accessor infrastructure.
86 template<ContainerIdConcept CIBase,ContainerIdConcept... CIList,typename CT,ColumnAccessMode CAM,ColumnarArrayMode CM>
87 requires requires { AccessorTemplate<CIBase,CT,CAM,CM>{}; }
88 class AccessorTemplate<VariantContainerId<CIBase,CIList...>,CT,CAM,CM> final
89 {
92 public:
93
94 static_assert (!std::is_const_v<CT>, "CT must not be const");
95
96 using CI = VariantContainerId<CIBase,CIList...>;
97 using AccessorTuple = std::tuple<AccessorTemplate<CIList,CT,CAM,CM>...>;
98
99 AccessorTemplate () noexcept = default;
100
101 AccessorTemplate (ColumnarTool<CM>& columnarTool, const std::string& name, ColumnInfo&& info = {})
102 {
103 boost::mp11::tuple_for_each (m_accessors, [&columnarTool,&name,&info] (auto& accessor)
104 {
105 resetAccessor (accessor, columnarTool, name, ColumnInfo(info));
106 });
107 }
108
110 {
111 m_accessors = std::move (that.m_accessors);
112 }
113
115 {
116 if (this != &that)
117 m_accessors = std::move (that.m_accessors);
118 return *this;
119 }
120
122 AccessorTemplate& operator = (const AccessorTemplate&) = delete;
123
124 void reset (ColumnarTool<CM>& columnarTool, const std::string& name, ColumnInfo&& info = {})
125 {
126 boost::mp11::tuple_for_each (m_accessors, [&columnarTool,&name,&info] (auto& accessor)
127 {
128 resetAccessor (accessor, columnarTool, name, ColumnInfo(info));
129 });
130 }
131
132 [[nodiscard]] decltype(auto) operator () (ObjectId<CI,CM> id) const noexcept
133 {
134 return internalGet<0> (id);
135 }
136
137 [[nodiscard]] bool isAvailable (ObjectId<CI,CM> id) const noexcept
138 {
139 return internalIsAvailable<0> (id);
140 }
141
144 private:
145
147
148 template<unsigned Index>
149 void internalInit (ColumnarTool<CM>& columnarTool, const std::string& name, const ColumnInfo& info)
150 {
151 resetAccessor (std::get<Index>(m_accessors), columnarTool, name, ColumnInfo(info));
152 if constexpr (Index + 1 < CI::numVariants)
153 internalInit<Index + 1>(columnarTool, name, info);
154 }
155
156 template<unsigned Index>
157 decltype(auto) internalGet (const ObjectId<CI,CM>& id) const noexcept
158 {
159 if (id.getVariantIndex() == Index)
160 {
161 using CI2 = std::tuple_element_t<Index,std::tuple<CIList...>>;
162 ObjectId<CI2,CM> objId {id.getData(), id.getObjectIndex()};
163 return std::get<Index>(m_accessors)(objId);
164 } else if constexpr (Index+1 < CI::numVariants)
165 return internalGet<Index + 1>(id);
166 else
167 {
168 std::cerr << "Invalid variant index: " << id.getVariantIndex() << std::endl;
169 std::abort ();
170 }
171 }
172
173 template<unsigned Index>
174 bool internalIsAvailable (const ObjectId<CI,CM>& id) const noexcept
175 {
176 if (id.getVariantIndex() == Index)
177 {
178 using CI2 = std::tuple_element_t<Index,std::tuple<CIList...>>;
179 ObjectId<CI2,CM> objId {id.getData(), id.getObjectIndex()};
180 return std::get<Index>(m_accessors).isAvailable(objId);
181 } else if constexpr (Index+1 < CI::numVariants)
183 else
184 {
185 std::cerr << "Invalid variant index: " << id.getVariantIndex() << std::endl;
186 std::abort ();
187 }
188 }
189 };
190}
191
192#endif
IndexedConstituentUserInfo::Index Index
void reset(ColumnarTool< CM > &columnarTool, const std::string &name, ColumnInfo &&info={})
std::tuple< AccessorTemplate< CIList, CT, CAM, CM >... > AccessorTuple
void internalInit(ColumnarTool< CM > &columnarTool, const std::string &name, const ColumnInfo &info)
decltype(auto) internalGet(const ObjectId< CI, CM > &id) const noexcept
void reset(ColumnarTool< CM > &columnarTool, const std::string &name, ColumnInfo &&info={})
the raw column accessor template class
the base class for all columnar components
a class representing a single object (electron, muons, etc.)
concept for a container id
void resetAccessor(AccessorTemplate< CI, CT, CAM, CM > &accessor, ColumnarTool< CM > &columnBase, const std::string &name, ColumnInfo &&info={})
reset a column accessor to point to a new column
ColumnAccessMode
an enum for the different access modes for a column
Definition ColumnInfo.h:19
STL namespace.
a struct that contains meta-information about each column that's needed to interface the column with ...
Definition ColumnInfo.h:35
a "variant" ContainerId
Definition VariantDef.h:98