ATLAS Offline Software
ObjectColumnXAOD.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /// @author Nils Krumnack
6 
7 
8 #include <ColumnarCore/ColumnarTool.h>
9 #include <ColumnarCore/ObjectId.h>
10 #include <ColumnarCore/ObjectRange.h>
11 #include <ColumnarCore/ContainerId.h>
12 #include <cassert>
13 
14 namespace columnar
15 {
16  template<ContainerIdConcept CI>
17  class AccessorTemplate<CI,ObjectColumn,ColumnAccessMode::input,ColumnarModeXAOD> final
18  {
19  /// Common Public Members
20  /// =====================
21  public:
22 
23  static_assert (CI::perEventRange || CI::perEventId, "ObjectColumn can only be used with per-event ranges or IDs");
24 
25  using CM = ColumnarModeXAOD;
26 
27  AccessorTemplate () = default;
28 
29  AccessorTemplate (ColumnarTool<CM>& columnBase,
30  const std::string& name,
31  ColumnInfo&& /*info*/ = {})
32  : m_columnBase (&columnBase), m_key (name)
33  {
34  }
35 
36  ObjectRange<CI,CM> operator() (ObjectId<ContainerId::eventContext,CM> /*eventId*/) const
37  requires (CI::perEventRange)
38  {
39  return ObjectRange<CI,CM> (retrieveObject ());
40  }
41 
42  ObjectRange<CI,CM> operator() (ObjectRange<ContainerId::eventContext,CM> /*eventRange*/) const
43  requires (CI::perEventRange)
44  {
45  return ObjectRange<CI,CM> (retrieveObject ());
46  }
47 
48  ObjectId<CI,CM> operator() (ObjectId<ContainerId::eventContext,CM> /*eventId*/) const
49  requires (CI::perEventId)
50  {
51  return ObjectId<CI,CM> (retrieveObject ());
52  }
53 
54  ObjectRange<CI,CM> operator() (ObjectRange<ContainerId::eventContext,CM> /*eventRange*/) const
55  requires (CI::perEventId)
56  {
57  return ObjectRange<CI,CM> (retrieveObject ());
58  }
59 
60  [[nodiscard]] bool isAvailable (ObjectId<ContainerId::eventContext,CM> /*eventId*/) const noexcept
61  {
62  return !m_key.empty() && m_columnBase && m_columnBase->eventStore().contains< std::remove_cv_t<typename CI::xAODObjectRangeType> >(m_key);
63  }
64 
65 
66 
67  /// Private Members
68  /// ===============
69  private:
70 
71  ColumnarTool<CM> *m_columnBase = nullptr;
72  std::string m_key;
73 
74  // retrieve the object from the event store. there is probably
75  // something clever that could be done with `SG::ReadHandleKey`, but
76  // in practice all CP tools converted so far (25 Feb 25) do not
77  // actually retrieve objects in XAOD mode. so for simplicity and to
78  // avoid some of the bookkeeping issues with "subtool" objects
79  // holding accessors, I use this "non-tracking" version.
80  auto& retrieveObject () const
81  {
82  if (m_key.empty())
83  throw std::runtime_error ("no container key set for type " + std::string (CI::idName));
84  typename CI::xAODObjectRangeType *container = nullptr;
85  if (!m_columnBase->eventStore().retrieve (container, m_key).isSuccess())
86  throw std::runtime_error ("failed to retrieve " + m_key);
87  return *container;
88  }
89  };
90 }