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 ColumnarModeArray
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>
88 class AccessorTemplate<VariantContainerId<CIBase,CIList...>,CT,CAM,ColumnarModeArray> 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...>;
98 using AccessorTuple = std::tuple<AccessorTemplate<CIList,CT,CAM,CM>...>;
99
100 AccessorTemplate () noexcept = default;
101
102 AccessorTemplate (ColumnarTool<CM>& columnarTool, const std::string& name, ColumnInfo&& info = {})
103 {
104 boost::mp11::tuple_for_each (m_accessors, [&columnarTool,&name,&info] (auto& accessor)
105 {
106 resetAccessor (accessor, columnarTool, name, ColumnInfo(info));
107 });
108 }
109
111 {
112 m_accessors = std::move (that.m_accessors);
113 }
114
116 {
117 if (this != &that)
118 m_accessors = std::move (that.m_accessors);
119 return *this;
120 }
121
123 AccessorTemplate& operator = (const AccessorTemplate&) = delete;
124
125 void reset (ColumnarTool<CM>& columnarTool, const std::string& name, ColumnInfo&& info = {})
126 {
127 boost::mp11::tuple_for_each (m_accessors, [&columnarTool,&name,&info] (auto& accessor)
128 {
129 resetAccessor (accessor, columnarTool, name, ColumnInfo(info));
130 });
131 }
132
133 [[nodiscard]] decltype(auto) operator () (ObjectId<CI,CM> id) const noexcept
134 {
135 return internalGet<0> (id);
136 }
137
138 [[nodiscard]] bool isAvailable (ObjectId<CI,CM> id) const noexcept
139 {
140 return internalIsAvailable<0> (id);
141 }
142
145 private:
146
148
149 template<unsigned Index>
150 void internalInit (ColumnarTool<CM>& columnarTool, const std::string& name, const ColumnInfo& info)
151 {
152 resetAccessor (std::get<Index>(m_accessors), columnarTool, name, ColumnInfo(info));
153 if constexpr (Index + 1 < CI::numVariants)
154 internalInit<Index + 1>(columnarTool, name, info);
155 }
156
157 template<unsigned Index>
158 decltype(auto) internalGet (const ObjectId<CI,CM>& id) const noexcept
159 {
160 if (id.getVariantIndex() == Index)
161 {
162 using CI2 = std::tuple_element_t<Index,std::tuple<CIList...>>;
163 ObjectId<CI2,CM> objId {id.getData(), id.getObjectIndex()};
164 return std::get<Index>(m_accessors)(objId);
165 } else if constexpr (Index+1 < CI::numVariants)
166 return internalGet<Index + 1>(id);
167 else
168 {
169 std::cerr << "Invalid variant index: " << id.getVariantIndex() << std::endl;
170 std::abort ();
171 }
172 }
173
174 template<unsigned Index>
175 bool internalIsAvailable (const ObjectId<CI,CM>& id) const noexcept
176 {
177 if (id.getVariantIndex() == Index)
178 {
179 using CI2 = std::tuple_element_t<Index,std::tuple<CIList...>>;
180 ObjectId<CI2,CM> objId {id.getData(), id.getObjectIndex()};
181 return std::get<Index>(m_accessors).isAvailable(objId);
182 } else if constexpr (Index+1 < CI::numVariants)
184 else
185 {
186 std::cerr << "Invalid variant index: " << id.getVariantIndex() << std::endl;
187 std::abort ();
188 }
189 }
190 };
191}
192
193#endif
IndexedConstituentUserInfo::Index Index
void reset(ColumnarTool< CM > &columnarTool, const std::string &name, ColumnInfo &&info={})
void reset(ColumnarTool< CM > &columnarTool, const std::string &name, ColumnInfo &&info={})
void internalInit(ColumnarTool< CM > &columnarTool, const std::string &name, const 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