ATLAS Offline Software
Loading...
Searching...
No Matches
LinkColumn.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8#ifndef COLUMNAR_CORE_LINK_COLUMN_H
9#define COLUMNAR_CORE_LINK_COLUMN_H
10
11#include <AthLinks/ElementLink.h>
17#include <SGCore/sgkey_t.h>
18
19#include <algorithm>
20#include <ranges>
21#include <string>
22#include <type_traits>
23#include <vector>
24
25namespace columnar
26{
27 namespace detail
28 {
35 template<typename CI>
36 struct LinkClidTypeImp final { using type = void; };
37 template<typename CI>
38 requires requires { CI::containerClid(); }
39 struct LinkClidTypeImp<CI> final { using type = typename CI::xAODElementLinkType; };
40 template<typename CI>
42
48 template<typename... Types>
49 struct LinkDistinctTypeCount final : std::integral_constant<unsigned,0u> {};
50 template<typename T,typename... Rest>
51 struct LinkDistinctTypeCount<T,Rest...> final : std::integral_constant<unsigned,((std::is_void_v<T> || (std::is_same_v<T,Rest> || ...)) ? 0u : 1u) + LinkDistinctTypeCount<Rest...>::value> {};
52 }
53
54
55
93 template<ContainerIdConcept LT,ContainerIdConcept ELT = LT,ContainerIdConcept... ExtraELT>
95 {
96 static_assert ((requires { ExtraELT::containerClid(); } && ...),
97 "the extra link types of a LinkCastColumn only add keys, so they need to provide a CLID");
98
108 };
109
110
111
112 namespace detail
113 {
116 template<ContainerIdConcept LT>
118 {
119 if constexpr (requires { LT::containerClid(); })
120 return LT::containerClid();
121 else
122 return 0;
123 }
124
125 // the links with the implicit types can simply redirect to the code
126 // for typed links. for key+index links this happens at the level of
127 // the ContainerFreeAccessor instead (see below).
128 template<ContainerIdConcept LT,ColumnarModeConcept CM>
129 requires (!CM::useKeyIndexAsLink)
131 {
132 public:
133
135 static_assert (BaseAccessor::isDefined, "MemoryAccessor for LinkCastColumn must be defined");
136
137 static constexpr bool isDefined = true;
138 static constexpr bool viewIsReference = BaseAccessor::viewIsReference;
139 static constexpr bool hasSetter = false;
140 using MemoryType = typename BaseAccessor::MemoryType;
141
142 [[nodiscard]] static auto makeViewer (void** dataArea)
143 {
144 return BaseAccessor::makeViewer(dataArea);
145 }
146 };
147
148 template<ContainerIdConcept LT,ContainerIdConcept ELT,ContainerIdConcept... ExtraELT>
149 class MemoryAccessor<LinkCastColumn<LT,ELT,ExtraELT...>,ColumnarModeXAOD> final
150 {
151 public:
153 static constexpr bool isDefined = true;
154 static constexpr bool viewIsReference = false;
155 static constexpr bool hasSetter = false;
157 static auto makeViewer (void**)
158 {
160 {
161 if (link.isValid())
162 {
163 auto *ptr = *link.cptr();
164 if (!ptr) return OptObjectId<LT,CM> ();
165 auto *ptr2 = dynamic_cast<typename LT::xAODObjectIdType*>(ptr);
166 if (!ptr2) throw std::runtime_error ("link not of expected type");
167 return OptObjectId<LT,CM> (ptr2);
168 } else
169 {
170 return OptObjectId<LT,CM> ();
171 }
172 };
173 }
174 };
175
176
177
178
180 template<ColumnarModeConcept CM>
181 [[nodiscard]] bool isNullLink (typename CM::LinkKeyColumnType key, typename CM::LinkIndexColumnType index) noexcept
182 {
183 return key == CM::nullLinkKey || index == CM::nullLinkIndex;
184 }
185
190 inline void addLinkClid (std::vector<CLID>& clids, CLID clid)
191 {
192 if (clid != 0 && std::find (clids.begin(), clids.end(), clid) == clids.end())
193 clids.push_back (clid);
194 }
195
197 inline void fillLinkKeys (std::vector<SG::sgkey_t>& keys, const std::string& name, const std::vector<CLID>& clids)
198 {
199 keys.clear();
200 for (CLID clid : clids)
201 keys.push_back (computeSgKey (name, clid));
202 }
203
205 [[nodiscard]] inline bool isLinkKey (const std::vector<SG::sgkey_t>& keys, SG::sgkey_t key) noexcept
206 {
207 return std::find (keys.begin(), keys.end(), key) != keys.end();
208 }
209
218 [[noreturn]] [[gnu::cold]] [[gnu::noinline]]
219 void throwLinkIndexOutOfRange (const std::string& columnName, std::size_t index, ColumnarOffsetType event, std::size_t numTargets);
220
226 [[noreturn]] [[gnu::cold]] [[gnu::noinline]]
227 void throwLinkKeyMismatch (const std::string& columnName, SG::sgkey_t key, ColumnarOffsetType event, const std::vector<SG::sgkey_t>& expected);
228
231 template<typename CM>
232 [[nodiscard]] inline std::size_t resolveLinkIndex (void** dataArea, unsigned targetOffsetsIndex, ColumnarOffsetType event, std::size_t index, const std::string& columnName)
233 {
234 auto *offsets = static_cast<const ColumnarOffsetType*>(dataArea[targetOffsetsIndex]);
235 const std::size_t result = offsets[event] + index;
236 if constexpr (!CM::skipLinkValidation)
237 {
238 if (result >= offsets[event+1])
239 throwLinkIndexOutOfRange (columnName, index, event, offsets[event+1]-offsets[event]);
240 }
241 return result;
242 }
243
244
245
246 // when using key+index links the link consists of the key and index
247 // columns as found in the input file, with the index being relative
248 // to the start of the event. To turn that into an OptObjectId this
249 // needs the event-index column (for the offset map of the link
250 // column) and the offset map of the target container.
251 //
252 // It also calculates the keys it accepts for the target container,
253 // i.e. the StoreGate keys calculated from the container name and
254 // the CLIDs of the target and the link types. The keys are shared
255 // with the columnar tool, which updates them whenever the link
256 // target name gets set or changes.
258 requires (CM::useKeyIndexAsLink)
260 {
263 public:
264
265 static_assert (LT::perEventRange, "links can only target containers with per-event ranges");
266
267 static constexpr bool isDefined = true;
268 static constexpr unsigned internalOffsetColumns = 0;
269
271
272 ContainerFreeAccessor (ColumnarTool<CM>& columnarTool, ColumnAccessorOptions&& options, ColumnAccessorOptionsArray&& optionsArray)
273 : m_columnName (optionsArray.baseName)
274 {
275 std::vector<CLID> clids;
278 (addLinkClid (clids, linkTargetClid<ExtraELT>()), ...);
279 // until the target name is known the keys are all the null key,
280 // which no (non-null) link can match. this way the vector has
281 // its final size from the start, and the accessor never has to
282 // check for it being empty.
283 m_targetKeys = std::make_shared<std::vector<typename CM::LinkKeyColumnType>> (clids.size(), CM::nullLinkKey);
284 columnarTool.watchLinkTargetName (LT::idName, [keys = m_targetKeys, clids = std::move (clids)] (const std::string& name)
285 {
286 fillLinkKeys (*keys, name, clids);
287 });
288
289 auto keyInfo = options.makeColumnInfo();
290 keyInfo.offsetName = optionsArray.offsetName;
292 m_keyData = std::make_unique<ColumnAccessorDataArray> (&m_keyIndex, &m_keyData, &typeid (typename CM::LinkKeyColumnType), ColumnAccessMode::input);
293 columnarTool.addColumn (optionsArray.baseName + "." + std::string (CM::linkKeyColumnName), m_keyData.get(), std::move (keyInfo));
294
295 auto indexInfo = options.makeColumnInfo();
296 indexInfo.offsetName = optionsArray.offsetName;
298 m_indexData = std::make_unique<ColumnAccessorDataArray> (&m_indexIndex, &m_indexData, &typeid (typename CM::LinkIndexColumnType), ColumnAccessMode::input);
299 columnarTool.addColumn (optionsArray.baseName + "." + std::string (CM::linkIndexColumnName), m_indexData.get(), std::move (indexInfo));
300
301 // the event-index column is shared by all columns using the
302 // same offset map, i.e. it is named after the container for
303 // scalar links, and after the vector for links inside vectors
304 std::string eventName = optionsArray.numOffsets == 0u ? std::string (optionsArray.offsetName) : optionsArray.baseName;
305 eventName += ".";
306 eventName += CM::eventIndexColumnName;
307 ColumnInfo eventInfo;
308 eventInfo.offsetName = optionsArray.offsetName;
309 eventInfo.isOptional = options.isOptional;
310 eventInfo.isEventIndex = true;
312 m_eventData = std::make_unique<ColumnAccessorDataArray> (&m_eventIndex, &m_eventData, &typeid (ColumnarOffsetType), ColumnAccessMode::input);
313 columnarTool.addColumn (eventName, m_eventData.get(), std::move (eventInfo));
314
315 ColumnInfo targetInfo;
316 targetInfo.offsetName = eventContextCIName;
317 targetInfo.isOffset = true;
318 targetInfo.isOptional = options.isOptional;
320 m_targetOffsetsData = std::make_unique<ColumnAccessorDataArray> (&m_targetOffsetsIndex, &m_targetOffsetsData, &typeid (ColumnarOffsetType), ColumnAccessMode::input);
321 columnarTool.addColumn (std::string (LT::idName), m_targetOffsetsData.get(), std::move (targetInfo));
322 }
323
325 : m_columnName (std::move (that.m_columnName)), m_targetKeys (std::move (that.m_targetKeys))
326 {
327 moveAccessor (m_keyIndex, m_keyData, that.m_keyIndex, that.m_keyData);
328 moveAccessor (m_indexIndex, m_indexData, that.m_indexIndex, that.m_indexData);
329 moveAccessor (m_eventIndex, m_eventData, that.m_eventIndex, that.m_eventData);
330 moveAccessor (m_targetOffsetsIndex, m_targetOffsetsData, that.m_targetOffsetsIndex, that.m_targetOffsetsData);
331 }
332
334 {
335 if (this != &that)
336 {
337 moveAccessor (m_keyIndex, m_keyData, that.m_keyIndex, that.m_keyData);
338 moveAccessor (m_indexIndex, m_indexData, that.m_indexIndex, that.m_indexData);
339 moveAccessor (m_eventIndex, m_eventData, that.m_eventIndex, that.m_eventData);
340 moveAccessor (m_targetOffsetsIndex, m_targetOffsetsData, that.m_targetOffsetsIndex, that.m_targetOffsetsData);
341 m_columnName = std::move (that.m_columnName);
342 m_targetKeys = std::move (that.m_targetKeys);
343 }
344 return *this;
345 }
346
347 [[nodiscard]] OptObjectId<LT,CM> operator () (void** dataArea, std::size_t index) const
348 {
349 const auto key = static_cast<const typename CM::LinkKeyColumnType*>(dataArea[m_keyIndex])[index];
350 const auto linkIndex = static_cast<const typename CM::LinkIndexColumnType*>(dataArea[m_indexIndex])[index];
351 if (isNullLink<CM> (key, linkIndex))
352 return OptObjectId<LT,CM> ();
353 const auto event = static_cast<const ColumnarOffsetType*>(dataArea[m_eventIndex])[index];
354 if constexpr (!CM::skipLinkValidation)
355 {
356 if constexpr (LinkCastColumn<LT,ELT,ExtraELT...>::numLinkTypes == 1u)
357 {
358 // with a single accepted type there is exactly one key
359 if (m_targetKeys->front() != key)
361 } else
362 {
363 if (!isLinkKey (*m_targetKeys, key))
365 }
366 }
367 return OptObjectId<LT,CM> (dataArea, resolveLinkIndex<CM> (dataArea, m_targetOffsetsIndex, event, linkIndex, m_columnName));
368 }
369
370 [[nodiscard]] auto operator () (void** dataArea, std::size_t beginIndex, std::size_t endIndex) const
371 {
372 return VectorConvertView ([this,dataArea] (std::size_t index) {return (*this) (dataArea, index);},
373 std::views::iota (beginIndex, endIndex));
374 }
375
376 [[nodiscard]] bool isAvailable (void** dataArea) const noexcept
377 {
378 return dataArea[m_keyIndex] != nullptr && dataArea[m_indexIndex] != nullptr && dataArea[m_eventIndex] != nullptr && dataArea[m_targetOffsetsIndex] != nullptr;
379 }
380
382 [[nodiscard]] typename CM::LinkKeyColumnType targetKey () const noexcept
383 {
384 return m_targetKeys->empty() ? CM::nullLinkKey : m_targetKeys->front();
385 }
386
388 [[nodiscard]] const auto& allTargetKeys () const noexcept
389 {
390 return *m_targetKeys;
391 }
392
395 private:
396
397 std::string m_columnName;
398 std::shared_ptr<std::vector<typename CM::LinkKeyColumnType>> m_targetKeys;
399 unsigned m_keyIndex = 0u;
400 std::unique_ptr<ColumnAccessorDataArray> m_keyData;
401 unsigned m_indexIndex = 0u;
402 std::unique_ptr<ColumnAccessorDataArray> m_indexData;
403 unsigned m_eventIndex = 0u;
404 std::unique_ptr<ColumnAccessorDataArray> m_eventData;
405 unsigned m_targetOffsetsIndex = 0u;
406 std::unique_ptr<ColumnAccessorDataArray> m_targetOffsetsData;
407 };
408
409 // the links with the implicit types redirect to the typed links
410 template<RegularContainerIdConcept LT,ColumnarModeConcept CM>
411 requires (CM::useKeyIndexAsLink)
413 {
416 public:
417
419
420 static constexpr bool isDefined = true;
421 static constexpr unsigned internalOffsetColumns = BaseAccessor::internalOffsetColumns;
422
424
425 ContainerFreeAccessor (ColumnarTool<CM>& columnarTool, ColumnAccessorOptions&& options, ColumnAccessorOptionsArray&& optionsArray)
426 : m_accessor (columnarTool, std::move (options), std::move (optionsArray))
427 {}
428
429 [[nodiscard]] auto operator () (void** dataArea, std::size_t index) const
430 {
431 return m_accessor (dataArea, index);
432 }
433
434 [[nodiscard]] auto operator () (void** dataArea, std::size_t beginIndex, std::size_t endIndex) const
435 {
436 return m_accessor (dataArea, beginIndex, endIndex);
437 }
438
439 [[nodiscard]] bool isAvailable (void** dataArea) const noexcept
440 {
441 return m_accessor.isAvailable (dataArea);
442 }
443
445 [[nodiscard]] auto targetKey () const noexcept
446 {
447 return m_accessor.targetKey ();
448 }
449
451 [[nodiscard]] const auto& allTargetKeys () const noexcept
452 {
453 return m_accessor.allTargetKeys ();
454 }
455
458 private:
459
461 };
462
463
464
465 template<ContainerIdConcept LT,ContainerIdConcept ELT,ContainerIdConcept... ExtraELT>
466 class MemoryAccessor<LinkCastColumn<LT,ELT,ExtraELT...>,ColumnarModeXAODArray> final
467 {
468 public:
470 static constexpr bool isDefined = true;
471 static constexpr bool viewIsReference = false;
472 static constexpr bool hasSetter = false;
474
475 static auto makeViewer (void** dataArea)
476 {
477 return [dataArea] (const ElementLink<typename ELT::xAODElementLinkType>& link)
478 {
479 if (link.isValid())
480 {
481 return OptObjectId<LT,CM> (dataArea, link.index());
482 } else
483 {
484 return OptObjectId<LT,CM> ();
485 }
486 };
487 }
488 };
489 }
490}
491
492#endif
uint32_t CLID
The Class ID type.
void operator()(T1)
the base class for all columnar components
a class representing a single optional object (electron, muons, etc.)
ContainerFreeAccessor(ColumnarTool< CM > &columnarTool, ColumnAccessorOptions &&options, ColumnAccessorOptionsArray &&optionsArray)
Definition LinkColumn.h:272
const auto & allTargetKeys() const noexcept
all the keys accepted for the target container
Definition LinkColumn.h:388
CM::LinkKeyColumnType targetKey() const noexcept
the key of the target container based on its own type
Definition LinkColumn.h:382
const auto & allTargetKeys() const noexcept
all the keys accepted for the target container
Definition LinkColumn.h:451
auto targetKey() const noexcept
the key of the target container based on its own type
Definition LinkColumn.h:445
ContainerFreeAccessor(ColumnarTool< CM > &columnarTool, ColumnAccessorOptions &&options, ColumnAccessorOptionsArray &&optionsArray)
Definition LinkColumn.h:425
ContainerFreeAccessor< LinkCastColumn< LT >, ColumnAccessMode::input, CM > BaseAccessor
Definition LinkColumn.h:418
MemoryAccessor< LinkCastColumn< LT >, CM > BaseAccessor
Definition LinkColumn.h:134
a help implementation of AccessorTemplate that handles type conversions
concept for a container id
uint32_t sgkey_t
Type used for hashed StoreGate key+CLID pairs.
Definition sgkey_t.h:32
Container id definitions for the columnar infrastructure.
bool isNullLink(typename CM::LinkKeyColumnType key, typename CM::LinkIndexColumnType index) noexcept
whether the given key and index represent a null link
Definition LinkColumn.h:181
std::size_t resolveLinkIndex(void **dataArea, unsigned targetOffsetsIndex, ColumnarOffsetType event, std::size_t index, const std::string &columnName)
convert the event-relative index of a link into an index into the target column, checking it is insid...
Definition LinkColumn.h:232
void throwLinkIndexOutOfRange(const std::string &columnName, std::size_t index, ColumnarOffsetType event, std::size_t numTargets)
throw the exception for a link index outside its event
void addLinkClid(std::vector< CLID > &clids, CLID clid)
add a CLID to the list of CLIDs a link accepts
Definition LinkColumn.h:190
void fillLinkKeys(std::vector< SG::sgkey_t > &keys, const std::string &name, const std::vector< CLID > &clids)
calculate the keys a link accepts for the given container name
Definition LinkColumn.h:197
VectorConvertView(FunctionType &&, ViewType &&) -> VectorConvertView< std::decay_t< FunctionType >, std::decay_t< ViewType > >
bool isLinkKey(const std::vector< SG::sgkey_t > &keys, SG::sgkey_t key) noexcept
whether the given key is in the list of accepted keys
Definition LinkColumn.h:205
typename LinkClidTypeImp< CI >::type LinkClidType
Definition LinkColumn.h:41
CLID linkTargetClid()
the CLID of the xAOD container a link column targets, or 0 if the container id does not provide one
Definition LinkColumn.h:117
void throwLinkKeyMismatch(const std::string &columnName, SG::sgkey_t key, ColumnarOffsetType event, const std::vector< SG::sgkey_t > &expected)
throw the exception for a link key that doesn't match any of the keys accepted for the link target
void moveAccessor(unsigned &dataIndex, std::unique_ptr< ColumnAccessorDataArray > &accessorData, unsigned &sourceIndex, std::unique_ptr< ColumnAccessorDataArray > &sourceData)
constexpr std::string_view eventContextCIName
the name for the event context container id
@ input
an input column
Definition ColumnInfo.h:22
void setColumnType(ColumnInfo &info) noexcept
set the type information in the ColumnInfo for the given C++ type
Definition ColumnInfo.h:227
std::size_t ColumnarOffsetType
the type used for the size and offsets in the columnar data
SG::sgkey_t computeSgKey(const std::string &name, std::uint32_t clid)
compute the StoreGate hashed key for a container
Definition index.py:1
STL namespace.
a struct that contains meta-information about each column that's needed to interface the column with ...
Definition ColumnInfo.h:73
std::string offsetName
the name of the offset column used for this column (or empty string for none)
Definition ColumnInfo.h:120
bool isOptional
whether this column is optional
Definition ColumnInfo.h:167
bool isEventIndex
whether this is the index of the event for each entry
Definition ColumnInfo.h:182
bool isOffset
whether this is an offset column
Definition ColumnInfo.h:138
a special column type that behaves like an OptObjectId, but applies an internal cast in xAOD mode
Definition LinkColumn.h:95
static constexpr unsigned numLinkTypes
the number of keys a key+index link accepts for its target, i.e.
Definition LinkColumn.h:107
typename CI::xAODElementLinkType type
Definition LinkColumn.h:39
the type of the container a link column targets, for the purpose of counting the keys a link accepts,...
Definition LinkColumn.h:36
the number of distinct types in a list of types, ignoring void
Definition LinkColumn.h:49
std::map< std::string, HypoJetVector >::const_iterator CI