Loading [MathJax]/jax/input/TeX/config.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ColumnarMemoryTest.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_TEST_FIXTURES__COLUMNAR_MEMORY_TEST_H
9 #define COLUMNAR_TEST_FIXTURES__COLUMNAR_MEMORY_TEST_H
10 
11 #include <gtest/gtest.h>
12 
13 #include <AsgTools/AsgTool.h>
20 #include <span>
21 
22 namespace columnar
23 {
24  struct ColumnarMemoryTest : public testing::Test
25  {
27 
29  std::string makeUniqueName ();
30 
32  static bool checkMode ();
33 
35  struct ColumnMapType;
36  };
37 
38 
39 
45 
47  {
50 
51  public:
52 
53  explicit ColumnarTestToolHandle (asg::AsgTool& val_tool);
54 
56  void renameContainers (const std::vector<std::pair<std::string,std::string>>& renames);
57 
59  void initialize ();
60 
62  void applySystematicVariation (const std::string& sysName);
63 
65  [[nodiscard]] std::vector<ColumnInfo> getColumnInfo () const;
66 
68  std::vector<std::string> getColumnNames () const;
69 
71  std::vector<std::string> getRecommendedSystematics () const;
72 
74  [[nodiscard]] const ColumnarToolWrapper& getToolWrapper () const;
75 
77  [[nodiscard]] IColumnarTool* getTool ();
78 
79 
80 
83 
84  private:
85 
86  IColumnarTool* m_tool = nullptr;
88 
89  std::shared_ptr<const ColumnarToolWrapper> m_toolWrapper;
90  };
91 
92 
93 
95  {
96  ColumnMapType (ColumnarTestToolHandle& val_toolHandle);
97 
98  void addColumn (const std::string& name, std::vector<std::any> data);
99 
100  void setExpectation (const std::string& name, const std::vector<std::any> & values);
101 
103  void connectColumnsToTool ();
104 
105  void call ();
106 
107  void checkExpectations ();
108 
109  private:
110 
111  template<typename T> T extractAny (const std::string& columnName, const std::any& value)
112  {
113  if (value.type() == typeid(float))
114  return std::any_cast<float> (value);
115  if (value.type() == typeid(double))
116  return std::any_cast<double> (value);
117  if (value.type() == typeid(int))
118  return std::any_cast<int> (value);
119  if (value.type() == typeid(unsigned))
120  return std::any_cast<unsigned> (value);
121  if (value.type() == typeid(std::size_t))
122  return std::any_cast<std::size_t> (value);
123  throw std::logic_error ("column " + columnName + " received unsupported input " + value.type().name() + ", cast value or extend test handler to support it");
124  }
125 
126  template<typename T> void addInputTyped (const std::string& name, const std::vector<std::any>& data)
127  {
128  std::vector<T> typedData;
129  for (auto& value : data)
130  typedData.emplace_back (extractAny<T> (name, value));
131  m_inputs.emplace (name, std::move (typedData));
132  }
133 
134  template<typename T> void addTypedColumn (const std::string& name, std::vector<T> data)
135  {
136  auto column = m_columnMap.find (name);
137  if (column == m_columnMap.end())
138  throw std::runtime_error ("adding unknown column: " + name);
139  if (m_inputs.contains (name))
140  throw std::runtime_error ("column added twice: " + name);
141  if (column->second.type != &typeid(T))
142  throw std::runtime_error ("column " + name + " has wrong type: " + column->second.type->name());
143  m_inputs.emplace (name, std::move (data));
144  }
145 
146  template<typename T> void addExpectationTyped (const std::string& name, std::vector<std::any> data)
147  {
148  std::vector<T> typedData;
149  for (auto& value : data)
150  typedData.emplace_back (extractAny<T> (name, value));
151  m_expectations.emplace (name, std::move (typedData));
152  }
153 
154  ColumnarOffsetType columnSize (const std::string& name);
155 
156  template<typename T> std::span<const T> getOutputColumn (const std::string& name)
157  {
158  auto info = m_columnMap.find (name);
159  if (info == m_columnMap.end())
160  throw std::runtime_error ("output column not found: " + name);
161  auto iter = m_activeColumns.find (name);
162  if (iter == m_activeColumns.end())
163  throw std::runtime_error ("output column not set: " + name);
164  if (!std::holds_alternative<std::vector<T>> (iter->second))
165  throw std::runtime_error ("output column has wrong type: " + name);
166  return std::span<const T> (std::get<std::vector<T>> (iter->second));
167  }
168 
169  template<typename T> void checkExpectationTyped (const std::string& columnName)
170  {
171  auto outputIter = m_activeColumns.find (columnName);
172  if (outputIter == m_activeColumns.end())
173  throw std::runtime_error ("output column not set: " + columnName);
174  if (!std::holds_alternative<std::vector<T>> (outputIter->second))
175  throw std::runtime_error ("output column has wrong type: " + columnName);
176  auto& output = std::get<std::vector<T>> (outputIter->second);
177 
178  auto expectationIter = m_expectations.find (columnName);
179  if (expectationIter == m_expectations.end())
180  throw std::runtime_error ("output column not found: " + columnName);
181  if (!std::holds_alternative<std::vector<T>> (expectationIter->second))
182  throw std::runtime_error ("output column has wrong type: " + columnName);
183  auto& expectation = std::get<std::vector<T>> (expectationIter->second);
184 
185  SCOPED_TRACE (columnName);
186  EXPECT_EQ (output.size(), expectation.size());
187  for (std::size_t index = 0; index != std::min (output.size(), expectation.size()); ++ index)
188  {
189  SCOPED_TRACE (index);
190  if constexpr (std::is_floating_point_v<T>)
191  EXPECT_NEAR (output[index], expectation[index], 1e-6);
192  else
193  EXPECT_EQ (output[index], expectation[index]);
194  }
195  std::cout << " m_columnMap.setExpectation (\"" << columnName << "\", {";
196  for (std::size_t index = 0; index != expectation.size(); ++ index)
197  {
198  if (index != 0)
199  std::cout << ", ";
200  if constexpr (std::is_floating_point_v<T>){
201  auto ss = std::cout.precision();
202  std::cout << std::setprecision (8) << output[index];
203  std::cout.precision(ss); //restore ostream state
204  } else if constexpr (std::is_same_v<T,char>){
205  std::cout << int (output[index]);
206  } else {
207  std::cout << output[index];
208  }
209  }
210  std::cout << "});" << std::endl;
211  }
212 
214 
215  std::unique_ptr<ColumnarToolWrapperData> m_columnData;
216 
217  std::unordered_map<std::string,const ColumnInfo> m_columnMap;
218 
219  std::unordered_map<std::string, std::variant<std::vector<float>,std::vector<char>,std::vector<int>,std::vector<std::uint8_t>,std::vector<std::uint16_t>,std::vector<std::uint32_t>,std::vector<std::uint64_t>>> m_inputs;
220  std::unordered_map<std::string, std::variant<std::vector<float>,std::vector<char>,std::vector<int>,std::vector<std::uint8_t>,std::vector<std::uint16_t>,std::vector<std::uint32_t>,std::vector<std::uint64_t>>> m_activeColumns;
221  std::unordered_map<std::string, std::variant<std::vector<float>,std::vector<char>,std::vector<int>,std::vector<std::uint8_t>,std::vector<std::uint16_t>,std::vector<std::uint32_t>,std::vector<std::uint64_t>>> m_expectations;
222  };
223 }
224 
225 #endif
grepfile.info
info
Definition: grepfile.py:38
CaloCondBlobAlgs_fillNoiseFromASCII.sysName
sysName
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:93
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
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:237
columnar::IColumnarTool
an interface for tools that operate on columnar data
Definition: IColumnarTool.h:214
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
columnar::ColumnarMemoryTest::ColumnMapType::ColumnMapType
ColumnMapType(ColumnarTestToolHandle &val_toolHandle)
Definition: ColumnarMemoryTest.cxx:156
columnar::ColumnarMemoryTest
Definition: ColumnarMemoryTest.h:25
columnar::ColumnarMemoryTest::ColumnMapType::extractAny
T extractAny(const std::string &columnName, const std::any &value)
Definition: ColumnarMemoryTest.h:111
columnar::ColumnarMemoryTest::ColumnMapType::checkExpectations
void checkExpectations()
Definition: ColumnarMemoryTest.cxx:276
columnar::ColumnarMemoryTest::ColumnMapType::m_toolHandle
ColumnarTestToolHandle * m_toolHandle
Definition: ColumnarMemoryTest.h:213
index
Definition: index.py:1
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::m_systTool
CP::ISystematicsTool * m_systTool
Definition: ColumnarMemoryTest.h:87
columnar::ColumnarMemoryTest::ColumnMapType::columnSize
ColumnarOffsetType columnSize(const std::string &name)
Definition: ColumnarMemoryTest.cxx:226
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::ColumnarTestToolHandle
ColumnarTestToolHandle(asg::AsgTool &val_tool)
Definition: ColumnarMemoryTest.cxx:61
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::m_tool
IColumnarTool * m_tool
Definition: ColumnarMemoryTest.h:86
DeMoUpdate.column
dictionary column
Definition: DeMoUpdate.py:1110
athena.value
value
Definition: athena.py:124
columnar::ColumnarMemoryTest::ColumnMapType::m_expectations
std::unordered_map< std::string, std::variant< std::vector< float >, std::vector< char >, std::vector< int >, std::vector< std::uint8_t >, std::vector< std::uint16_t >, std::vector< std::uint32_t >, std::vector< std::uint64_t > > > m_expectations
Definition: ColumnarMemoryTest.h:221
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:72
columnar::ColumnarMemoryTest::ColumnMapType::addInputTyped
void addInputTyped(const std::string &name, const std::vector< std::any > &data)
Definition: ColumnarMemoryTest.h:126
columnar::ColumnarMemoryTest::ColumnMapType::m_columnData
std::unique_ptr< ColumnarToolWrapperData > m_columnData
Definition: ColumnarMemoryTest.h:215
ISystematicsTool.h
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:805
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::getColumnNames
std::vector< std::string > getColumnNames() const
get the expected column names
Definition: ColumnarMemoryTest.cxx:115
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::applySystematicVariation
void applySystematicVariation(const std::string &sysName)
set the tool to apply the given systematic variation
Definition: ColumnarMemoryTest.cxx:92
columnar::ColumnarMemoryTest::ColumnMapType::call
void call()
Definition: ColumnarMemoryTest.cxx:268
columnar::ColumnarMemoryTest::ColumnMapType::m_inputs
std::unordered_map< std::string, std::variant< std::vector< float >, std::vector< char >, std::vector< int >, std::vector< std::uint8_t >, std::vector< std::uint16_t >, std::vector< std::uint32_t >, std::vector< std::uint64_t > > > m_inputs
Definition: ColumnarMemoryTest.h:219
IColumnarTool.h
columnar::ColumnarMemoryTest::ColumnMapType::addColumn
void addColumn(const std::string &name, std::vector< std::any > data)
Definition: ColumnarMemoryTest.cxx:170
python.LArMinBiasAlgConfig.int
int
Definition: LArMinBiasAlgConfig.py:59
columnar::ColumnarMemoryTest::makeUniqueName
std::string makeUniqueName()
make a unique tool name to be used in unit tests
Definition: ColumnarMemoryTest.cxx:44
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::getToolWrapper
const ColumnarToolWrapper & getToolWrapper() const
get the tool wrapper
Definition: ColumnarMemoryTest.cxx:138
columnar::ColumnarMemoryTest::ColumnMapType::m_columnMap
std::unordered_map< std::string, const ColumnInfo > m_columnMap
Definition: ColumnarMemoryTest.h:217
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::getColumnInfo
std::vector< ColumnInfo > getColumnInfo() const
get the expected column info
Definition: ColumnarMemoryTest.cxx:105
columnar::ColumnarMemoryTest::ColumnarMemoryTest
ColumnarMemoryTest()
Definition: ColumnarMemoryTest.cxx:27
columnar::ColumnarMemoryTest::ColumnMapType::addTypedColumn
void addTypedColumn(const std::string &name, std::vector< T > data)
Definition: ColumnarMemoryTest.h:134
ColumnarToolWrapper.h
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::getRecommendedSystematics
std::vector< std::string > getRecommendedSystematics() const
get the recommended systematics
Definition: ColumnarMemoryTest.cxx:125
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::m_toolWrapper
std::shared_ptr< const ColumnarToolWrapper > m_toolWrapper
Definition: ColumnarMemoryTest.h:89
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
columnar::final
CM final
Definition: ColumnAccessor.h:106
columnar::ColumnarToolWrapper
a class that wraps an IColumnarTool for use in Python
Definition: ColumnarToolWrapper.h:36
columnar::ColumnarMemoryTest::ColumnMapType
Definition: ColumnarMemoryTest.h:95
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::initialize
void initialize()
initialize the tool
Definition: ColumnarMemoryTest.cxx:84
columnar::ColumnarMemoryTest::ColumnMapType::checkExpectationTyped
void checkExpectationTyped(const std::string &columnName)
Definition: ColumnarMemoryTest.h:169
CP::ISystematicsTool
Interface for all CP tools supporting systematic variations.
Definition: ISystematicsTool.h:32
DeMoScan.index
string index
Definition: DeMoScan.py:364
columnar::ColumnarMemoryTest::ColumnMapType::getOutputColumn
std::span< const T > getOutputColumn(const std::string &name)
Definition: ColumnarMemoryTest.h:156
columnar
Definition: ClusterDef.h:16
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
ColumnarToolHelpers.h
columnar::ColumnarMemoryTest::ColumnMapType::addExpectationTyped
void addExpectationTyped(const std::string &name, std::vector< std::any > data)
Definition: ColumnarMemoryTest.h:146
ColumnInfo.h
columnar::ColumnarMemoryTest::checkMode
static bool checkMode()
check whether we have the right mode
Definition: ColumnarMemoryTest.cxx:53
AsgTool.h
columnar::ColumnarMemoryTest::ColumnMapType::m_activeColumns
std::unordered_map< std::string, std::variant< std::vector< float >, std::vector< char >, std::vector< int >, std::vector< std::uint8_t >, std::vector< std::uint16_t >, std::vector< std::uint32_t >, std::vector< std::uint64_t > > > m_activeColumns
Definition: ColumnarMemoryTest.h:220
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:25
columnar::ColumnarMemoryTest::ColumnMapType::setExpectation
void setExpectation(const std::string &name, const std::vector< std::any > &values)
Definition: ColumnarMemoryTest.cxx:198
columnar::ColumnarMemoryTest::ColumnarTestToolHandle
a handle to a columnar tool for running tests
Definition: ColumnarMemoryTest.h:47
columnar::ColumnarMemoryTest::ColumnarTestToolHandle::getTool
IColumnarTool * getTool()
get the contained tool
Definition: ColumnarMemoryTest.cxx:148
SystematicsUtil.h