ATLAS Offline Software
ColumnarMemoryTest.cxx
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 //
9 // includes
10 //
11 
13 
15 
16 //
17 // method implementations
18 //
19 
20 namespace columnar
21 {
24  {
25  }
26 
27 
28 
31  {
32  static std::atomic<unsigned> index = 0;
33  return "UniqueMemoryTestTool" + std::to_string(++index);
34  }
35 
36 
37 
39  checkMode ()
40  {
41  return std::is_same_v<ColumnarModeDefault,ColumnarModeArray>;
42  }
43 
44 
45 
48  {
49  m_tool = dynamic_cast<IColumnarTool*> (&val_tool);
50  if (m_tool == nullptr)
51  throw std::runtime_error ("tool does not implement IColumnarTool");
52  m_systTool = dynamic_cast<CP::ISystematicsTool*> (m_tool);
53  }
54 
55 
56 
58  renameContainers (const std::vector<std::pair<std::string,std::string>>& renames)
59  {
61  if (m_toolWrapper)
62  {
63  m_toolWrapper = std::make_shared<ColumnarToolWrapper> (m_tool);
64  }
65  }
66 
67 
68 
70  initialize ()
71  {
72  m_toolWrapper = std::make_shared<ColumnarToolWrapper> (m_tool);
73  }
74 
75 
76 
78  applySystematicVariation (const std::string& sysName)
79  {
80  // by convention setting a systematic on a non-systematics tool
81  // will do nothing
82  if (m_systTool == nullptr)
83  return;
84  if (!m_systTool->applySystematicVariation (CP::SystematicSet (sysName)).isSuccess())
85  throw std::runtime_error ("failed to apply systematic variation");
86  }
87 
88 
89 
90  [[nodiscard]] std::vector<ColumnInfo> ColumnarMemoryTest::ColumnarTestToolHandle ::
91  getColumnInfo () const
92  {
93  if (!m_toolWrapper)
94  throw std::runtime_error ("tool not initialized");
95  return m_toolWrapper->getColumnInfo ();
96  }
97 
98 
99 
101  getColumnNames () const
102  {
103  if (!m_toolWrapper)
104  throw std::runtime_error ("tool not initialized");
105  return m_toolWrapper->getColumnNames ();
106  }
107 
108 
109 
112  {
113  if (!m_systTool)
114  return {""};
115  std::vector<std::string> result;
116  for (auto& sys : CP::make_systematics_vector (m_systTool->recommendedSystematics()))
117  result.push_back (sys.name());
118  return result;
119  }
120 
121 
122 
124  getToolWrapper () const
125  {
126  if (!m_toolWrapper)
127  throw std::runtime_error ("tool not initialized");
128  return *m_toolWrapper;
129  }
130 
131 
132 
134  getTool ()
135  {
136  return m_tool;
137  }
138 
139 
140 
142  ColumnMapType (ColumnarTestToolHandle& val_toolHandle)
143  : m_toolHandle (&val_toolHandle), m_columnData (std::make_unique<ColumnarToolWrapperData> (&val_toolHandle.getToolWrapper()))
144  {
145  for (auto& column : m_toolHandle->getColumnInfo())
146  {
147  auto [iter, success] = m_columnMap.emplace (column.name, ColumnInfo {column});
148  if (!success)
149  throw std::runtime_error ("column already exists: " + column.name);
150  }
151  }
152 
153 
154 
156  addColumn (const std::string& name, std::vector<std::any> data)
157  {
158  auto column = m_columnMap.find (name);
159  if (column == m_columnMap.end())
160  throw std::runtime_error ("adding unknown column: " + name);
161  if (m_inputs.contains (name))
162  throw std::runtime_error ("column added twice: " + name);
163  if (column->second.type == &typeid(float))
164  addInputTyped<float> (name, data);
165  else if (column->second.type == &typeid(char))
166  addInputTyped<char> (name, data);
167  else if (column->second.type == &typeid(int))
168  addInputTyped<int> (name, data);
169  else if (column->second.type == &typeid(std::uint8_t))
170  addInputTyped<std::uint8_t> (name, data);
171  else if (column->second.type == &typeid(std::uint16_t))
172  addInputTyped<std::uint16_t> (name, data);
173  else if (column->second.type == &typeid(std::uint32_t))
174  addInputTyped<std::uint32_t> (name, data);
175  else if (column->second.type == &typeid(std::uint64_t))
176  addInputTyped<std::uint64_t> (name, data);
177  else
178  throw std::logic_error ("column name " + name + " has unsupported type " + column->second.type->name() + ", extend test handler to support it");
179  }
180 
181 
182 
184  setExpectation (const std::string& name, const std::vector<std::any> & values)
185  {
186  auto column = m_columnMap.find (name);
187  if (column == m_columnMap.end())
188  throw std::runtime_error ("adding unknown column: " + name);
189  if (m_expectations.contains (name))
190  throw std::runtime_error ("column added twice: " + name);
191  if (column->second.type == &typeid(float))
192  addExpectationTyped<float> (name, values);
193  else if (column->second.type == &typeid(char))
194  addExpectationTyped<char> (name, values);
195  else if (column->second.type == &typeid(int))
196  addExpectationTyped<int> (name, values);
197  else if (column->second.type == &typeid(std::uint8_t))
198  addExpectationTyped<std::uint8_t> (name, values);
199  else if (column->second.type == &typeid(std::uint16_t))
200  addExpectationTyped<std::uint16_t> (name, values);
201  else if (column->second.type == &typeid(std::uint32_t))
202  addExpectationTyped<std::uint32_t> (name, values);
203  else if (column->second.type == &typeid(std::uint64_t))
204  addExpectationTyped<std::uint64_t> (name, values);
205  else
206  throw std::logic_error ("column name " + name + " has unsupported type " + column->second.type->name() + ", extend test handler to support it");
207  }
208 
209 
210 
212  columnSize (const std::string& name)
213  {
214  auto iter = m_inputs.find (name);
215  if (iter == m_inputs.end())
216  throw std::runtime_error ("column not found: " + name);
217  return std::visit ([] (const auto& data) { return data.size(); }, iter->second);
218  }
219 
220 
221 
224  {
225  for (auto& [name, columnInfo] : m_columnMap)
226  {
227  if (auto iter = m_inputs.find (columnInfo.name); iter != m_inputs.end())
228  {
229  switch (columnInfo.accessMode)
230  {
232  std::visit ([&] (const auto& data)
233  {
234  m_columnData->setColumn (iter->first, data.size(), data.data());
235  }, iter->second);
236  break;
239  std::visit ([&] (auto& data)
240  {
241  m_columnData->setColumn (iter->first, data.size(), data.data());
242  }, m_activeColumns[iter->first] = iter->second);
243  break;
244  default:
245  throw std::runtime_error ("column mode unknown: " + columnInfo.name);
246  }
247  }
248  }
249  }
250 
251 
252 
254  call ()
255  {
256  m_columnData->call ();
257  }
258 
259 
260 
263  {
264  for (auto& [name, info] : m_columnMap)
265  {
266  if (info.accessMode == ColumnAccessMode::input)
267  continue;
268 
269  auto iter = m_activeColumns.find (name);
270  if (iter == m_activeColumns.end())
271  continue;
272 
273  if (info.type == &typeid(float))
274  checkExpectationTyped<float> (name);
275  else if (info.type == &typeid(char))
276  checkExpectationTyped<char> (name);
277  else if (info.type == &typeid(int))
278  checkExpectationTyped<int> (name);
279  else if (info.type == &typeid(std::uint8_t))
280  checkExpectationTyped<std::uint8_t> (name);
281  else if (info.type == &typeid(std::uint16_t))
282  checkExpectationTyped<std::uint16_t> (name);
283  else if (info.type == &typeid(std::uint32_t))
284  checkExpectationTyped<std::uint32_t> (name);
285  else if (info.type == &typeid(std::uint64_t))
286  checkExpectationTyped<std::uint64_t> (name);
287  else
288  throw std::logic_error ("column name " + name + " has unsupported type " + info.type->name() + ", extend test handler to support it");
289  }
290  }
291 }
CaloCondBlobAlgs_fillNoiseFromASCII.sysName
sysName
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:92
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
createLinkingScheme.iter
iter
Definition: createLinkingScheme.py:62
asg::AsgTool
Base class for the dual-use tool implementation classes.
Definition: AsgTool.h:47
columnar::ColumnarMemoryTest::ColumnMapType::connectColumnsToTool
void connectColumnsToTool()
add the columns we have to the tool
Definition: ColumnarMemoryTest.cxx:223
columnar::IColumnarTool
an interface for tools that operate on columnar data
Definition: IColumnarTool.h:214
columnar::ColumnAccessMode::input
@ input
an input column
get_generator_info.result
result
Definition: get_generator_info.py:21
CP::make_systematics_vector
std::vector< CP::SystematicSet > make_systematics_vector(const SystematicSet &systematics)
utility functions for working with systematics
Definition: SystematicsUtil.cxx:25
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:558
columnar::ColumnarMemoryTest::ColumnMapType::ColumnMapType
ColumnMapType(ColumnarTestToolHandle &val_toolHandle)
Definition: ColumnarMemoryTest.cxx:142
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: SkimmingToolEXOT5.cxx:23
columnar::ColumnarMemoryTest::ColumnMapType::checkExpectations
void checkExpectations()
Definition: ColumnarMemoryTest.cxx:262
columnar::ColumnarMemoryTest::ColumnMapType::m_toolHandle
ColumnarTestToolHandle * m_toolHandle
Definition: ColumnarMemoryTest.h:217
index
Definition: index.py:1
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::m_systTool
CP::ISystematicsTool * m_systTool
Definition: ColumnarMemoryTest.h:89
columnar::ColumnarMemoryTest::ColumnMapType::columnSize
ColumnarOffsetType columnSize(const std::string &name)
Definition: ColumnarMemoryTest.cxx:212
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::ColumnarTestToolHandle
ColumnarTestToolHandle(asg::AsgTool &val_tool)
Definition: ColumnarMemoryTest.cxx:47
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::m_tool
IColumnarTool * m_tool
Definition: ColumnarMemoryTest.h:88
DeMoUpdate.column
dictionary column
Definition: DeMoUpdate.py:1110
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::renameContainers
void renameContainers(const std::vector< std::pair< std::string, std::string >> &renames)
rename the columns the tool uses
Definition: ColumnarMemoryTest.cxx:58
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
columnar::ColumnAccessMode::output
@ output
an output column
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:808
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::getColumnNames
std::vector< std::string > getColumnNames() const
get the expected column names
Definition: ColumnarMemoryTest.cxx:101
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::applySystematicVariation
void applySystematicVariation(const std::string &sysName)
set the tool to apply the given systematic variation
Definition: ColumnarMemoryTest.cxx:78
columnar::ColumnarMemoryTest::ColumnMapType::call
void call()
Definition: ColumnarMemoryTest.cxx:254
columnar::ColumnInfo
a struct that contains meta-information about each column that's needed to interface the column with ...
Definition: ColumnInfo.h:35
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:93
columnar::ColumnarMemoryTest::ColumnMapType::addColumn
void addColumn(const std::string &name, std::vector< std::any > data)
Definition: ColumnarMemoryTest.cxx:156
columnar::ColumnarToolWrapperData
a class that holds the columnar data for a single call to ColumnarToolWrapper
Definition: ColumnarToolWrapper.h:136
columnar::ColumnarMemoryTest::makeUniqueName
std::string makeUniqueName()
make a unique tool name to be used in unit tests
Definition: ColumnarMemoryTest.cxx:30
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::getToolWrapper
const ColumnarToolWrapper & getToolWrapper() const
get the tool wrapper
Definition: ColumnarMemoryTest.cxx:124
columnar::ColumnarMemoryTest::ColumnMapType::m_columnMap
std::unordered_map< std::string, const ColumnInfo > m_columnMap
Definition: ColumnarMemoryTest.h:221
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::getColumnInfo
std::vector< ColumnInfo > getColumnInfo() const
get the expected column info
Definition: ColumnarMemoryTest.cxx:91
ColumnarDef.h
columnar::renameContainers
void renameContainers(IColumnarTool &tool, const std::vector< std::pair< std::string, std::string >> &renames)
rename containers in the columnar tool
Definition: ColumnarToolHelpers.cxx:23
columnar::ColumnarMemoryTest::ColumnarMemoryTest
ColumnarMemoryTest()
Definition: ColumnarMemoryTest.cxx:23
columnar::ColumnAccessMode::update
@ update
an updateable column
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::getRecommendedSystematics
std::vector< std::string > getRecommendedSystematics() const
get the recommended systematics
Definition: ColumnarMemoryTest.cxx:111
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
columnar::ColumnarToolWrapper
a class that wraps an IColumnarTool for use in Python
Definition: ColumnarToolWrapper.h:36
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::initialize
void initialize()
initialize the tool
Definition: ColumnarMemoryTest.cxx:70
CP::ISystematicsTool
Interface for all CP tools supporting systematic variations.
Definition: ISystematicsTool.h:32
ColumnarMemoryTest.h
columnar
Definition: ClusterDef.h:16
columnar::ColumnarMemoryTest::checkMode
static bool checkMode()
check whether we have the right mode
Definition: ColumnarMemoryTest.cxx:39
columnar::ColumnarOffsetType
std::size_t ColumnarOffsetType
the type used for the size and offsets in the columnar data
Definition: IColumnarTool.h:20
python.difftuple.renames
dictionary renames
Definition: difftuple.py:23
python.ParticleTypeUtil.info
def info
Definition: ParticleTypeUtil.py:87
columnar::ColumnarMemoryTest::ColumnMapType::setExpectation
void setExpectation(const std::string &name, const std::vector< std::any > &values)
Definition: ColumnarMemoryTest.cxx:184
columnar::ColumnarMemoryTest::ColumnarTestToolHandle
a handle to a columnar tool for running tests
Definition: ColumnarMemoryTest.h:49
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::getTool
IColumnarTool * getTool()
get the contained tool
Definition: ColumnarMemoryTest.cxx:134