ATLAS Offline Software
Loading...
Searching...
No Matches
ColumnReaderTTree.cxx
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// includes
9//
10
12
16
17#include <TTree.h>
18
19//
20// method implementations
21//
22
23namespace columnar
24{
25 namespace TestUtils
26 {
27 template <typename T>
28 class BranchReader final
29 {
30 std::string m_branchName;
31 TBranch* m_branch = nullptr;
32 bool m_isStatic = std::is_pod_v<T>;
33 T* m_data{new T()};
34
35 public:
36 BranchReader(const std::string& val_branchName)
37 : m_branchName(val_branchName)
38 {
39 if (m_branchName.find("Aux.") != std::string::npos)
40 m_isStatic = true;
41 }
42
43 ~BranchReader() noexcept
44 {
45 delete m_data;
46 }
47
48 BranchReader(const BranchReader&) = delete;
50
51 void setIsStatic(bool isStatic)
52 {
53 m_isStatic = isStatic;
54 }
55
56 [[nodiscard]] const std::string& branchName() const
57 {
58 return m_branchName;
59 }
60
61 [[nodiscard]] std::string columnName() const
62 {
63 std::string columnName = m_branchName;
64 if (auto index = columnName.find("AuxDyn."); index != std::string::npos)
65 columnName.replace(index, 6, "");
66 else if (auto index = columnName.find("Aux."); index != std::string::npos)
67 columnName.replace(index, 3, "");
68 else if (columnName.find(".") != std::string::npos)
69 throw std::runtime_error("branch name does not contain AuxDyn or Aux: " +m_branchName);
70 return columnName;
71 }
72
73 [[nodiscard]] std::string containerName() const
74 {
75 if (auto index = m_branchName.find("AuxDyn."); index != std::string::npos)
76 return m_branchName.substr(0, index);
77 else if (auto index = m_branchName.find("Aux."); index != std::string::npos)
78 return m_branchName.substr(0, index);
79 else if (m_branchName.find(".") == std::string::npos)
80 return m_branchName;
81 else
82 throw std::runtime_error("branch name does not contain AuxDyn or Aux: " +m_branchName);
83 }
84
85 void connectTree(TTree* tree)
86 {
87 m_branch = tree->GetBranch(m_branchName.c_str());
88 if (!m_branch)
89 throw std::runtime_error("failed to get branch: " + m_branchName);
90 m_branch->SetMakeClass(1);
91 if (m_isStatic)
92 m_branch->SetAddress(m_data);
93 else
94 m_branch->SetAddress(&m_data);
95 }
96
97 void connectTree(const Backend& b)
98 {
99 auto* tree = std::get<TTree*>(b); // throws if wrong backend
101 }
102
103 const T& getEntry(Long64_t entry)
104 {
105 if (!m_branch)
106 throw std::runtime_error("branch not connected: " + m_branchName);
107 if (m_branch->GetEntry(entry) <= 0)
108 throw std::runtime_error("failed to get entry " + std::to_string(entry) + " for branch: " + m_branchName);
109 if (m_data == nullptr)
110 throw std::runtime_error("got nullptr reading data for branch: " + m_branchName);
111 return *m_data;
112 }
113
114 const T& getCachedEntry() const
115 {
116 return *m_data;
117 }
118
119 std::optional<float> entrySize() const
120 {
121 if (!m_branch)
122 return std::nullopt;
123 return static_cast<float>(m_branch->GetZipBytes()) / m_branch->GetEntries();
124 }
125
126 std::optional<float> uncompressedSize() const
127 {
128 if (!m_branch)
129 return std::nullopt;
130 return static_cast<float>(m_branch->GetTotBytes()) / m_branch->GetEntries();
131 }
132
133 // technically this is const-correct, but I don't want to convince
134 // the code checker of that
135 std::optional<unsigned> numBaskets()
136 {
137 if (!m_branch)
138 return std::nullopt;
139 return m_branch->GetListOfBaskets()->GetSize();
140 }
141 };
142
143 template <typename T>
144 class BranchReaderArray final
145 {
146 public:
147 std::string m_branchName;
148 TBranch* m_branch = nullptr;
149 std::vector<T> m_dataVec;
150
151 public:
152 BranchReaderArray(const std::string& val_branchName)
153 : m_branchName(val_branchName)
154 {}
155
158
159 [[nodiscard]] std::string columnName() const
160 {
161 std::string columnName = m_branchName;
162 if (auto index = columnName.find("AuxDyn."); index != std::string::npos)
163 columnName.replace(index, 6, "");
164 else if (auto index = columnName.find("Aux."); index != std::string::npos)
165 columnName.replace(index, 3, "");
166 else if (columnName.find(".") != std::string::npos)
167 throw std::runtime_error("branch name does not contain AuxDyn or Aux: " + m_branchName);
168 return columnName;
169 }
170
171 [[nodiscard]] std::string containerName() const
172 {
173 if (auto index = m_branchName.find("AuxDyn."); index != std::string::npos)
174 return m_branchName.substr(0, index);
175 else if (auto index = m_branchName.find("Aux."); index != std::string::npos)
176 return m_branchName.substr(0, index);
177 else if (m_branchName.find(".") == std::string::npos)
178 return m_branchName;
179 else
180 throw std::runtime_error("branch name does not contain AuxDyn or Aux: " + m_branchName);
181 }
182 void connectTree (TTree *tree)
183 {
184 m_branch = tree->GetBranch (m_branchName.c_str());
185 if (!m_branch)
186 throw std::runtime_error ("failed to get branch: " + m_branchName);
187 m_branch->SetMakeClass (1);
188 // FIX ME: I have to have some hard-coded size, see explanation
189 // below.
190 m_dataVec.resize (100);
191 if (!m_dataVec.empty())
192 m_branch->SetAddress (m_dataVec.data());
193 }
194
195 std::span<const T> getEntry (Long64_t entry, std::size_t size)
196 {
197 if (!m_branch)
198 throw std::runtime_error ("branch not connected: " + m_branchName);
199 if (m_dataVec.size() < size)
200 {
201 // FIX ME: in one of the latest releases the repointing below
202 // breaks, and causes memory corruption. so I'm now
203 // preallocating and fail rather than reallocate, and the
204 // problem goes away. maybe it should be investigated at some
205 // point, but this is a test and I already spend a fair amount
206 // of time investigating this. the harm is that this test
207 // consumes a few hundreds bytes more in memory and we may have
208 // to occasionally increase the buffer size to cover all test
209 // files and branch lengths.
210 throw std::runtime_error ("requested size exceeds buffer size for branch: " + m_branchName);
211 // m_dataVec.resize (size);
212 // m_branch->SetAddress (m_dataVec.data());
213 }
214 if (size > 0 && m_branch->GetEntry (entry) <= 0)
215 throw std::runtime_error ("failed to get entry " + std::to_string (entry) + " for branch: " + m_branchName);
216 return std::span<const T>(m_dataVec.data(), size);
217 }
218
219 std::optional<float> entrySize () const
220 {
221 if (!m_branch)
222 return std::nullopt;
223 return static_cast<float>(m_branch->GetZipBytes()) / m_branch->GetEntries();
224 }
225
226 std::optional<float> uncompressedSize () const
227 {
228 if (!m_branch)
229 return std::nullopt;
230 return static_cast<float>(m_branch->GetTotBytes()) / m_branch->GetEntries();
231 }
232
233 // technically this is const-correct, but I don't want to convince
234 // the code checker of that
235 std::optional<unsigned> numBaskets ()
236 {
237 if (!m_branch)
238 return std::nullopt;
239 return m_branch->GetListOfBaskets()->GetSize();
240 }
241 };
242
244 {
248 const std::vector<ColumnarOffsetType>* offsetColumn = nullptr;
249 std::vector<ColumnarOffsetType> offsets = {0};
253 unsigned entries = 0;
254 unsigned nullEntries = 0;
255
256 ColumnReaderVectorSplitLink (const std::string& val_branchName)
257 : branchReaderSize (val_branchName), branchReaderKey (val_branchName + ".m_persKey"), branchReaderIndex (val_branchName + ".m_persIndex"), benchmarkUnpack (branchReaderSize.columnName()+"(unpack)"), benchmark (branchReaderSize.columnName())
258 {
259 outputColumns.push_back ({.name = LinkColumnKeyIndex::dataColumnName (branchReaderSize.columnName())});
260 outputColumns.push_back ({.name = branchReaderSize.containerName(), .isOffset = true, .primary = false});
261 }
262
263 virtual bool connect (Backend source, std::unordered_map<std::string,const std::vector<ColumnarOffsetType>*>& offsetColumns, std::unordered_map<std::string,ColumnInfo>& requestedColumns) override
264 {
265 auto iter = requestedColumns.find (outputColumns.at(0).name);
266 if (iter == requestedColumns.end())
267 return false;
268 outputColumns.at(0).enabled = true;
269 auto* tree = std::get<TTree*>(source);
270 branchReaderSize.connectTree (tree);
271 branchReaderKey.connectTree (tree);
272 branchReaderIndex.connectTree (tree);
273
274 if (iter->second.offsetName != outputColumns.at(1).name)
275 throw std::runtime_error ("offset name mismatch: " + iter->second.offsetName + " != " + outputColumns.at(1).name);
276
277 for (const auto & keyColumn : columnData.connect (iter->second, offsetColumns, requestedColumns))
278 {
279 outputColumns.push_back ({.name = keyColumn, .primary = false, .enabled = true});
280 requestedColumns.erase (keyColumn);
281 }
282 requestedColumns.erase (iter);
283
284 if (auto offsetIter = offsetColumns.find (outputColumns.at(1).name); offsetIter != offsetColumns.end())
285 offsetColumn = offsetIter->second;
286 else
287 offsetColumns.emplace (outputColumns.at(1).name, &offsets);
288
289 iter = requestedColumns.find (outputColumns.at(1).name);
290 if (iter != requestedColumns.end())
291 {
292 outputColumns.at(1).enabled = true;
293 requestedColumns.erase (iter);
294 }
295
296 return true;
297 }
298
299 virtual void clearColumns () override
300 {
301 columnData.clear();
302 offsets.clear();
303 offsets.push_back (0);
304 }
305
306 virtual void getEntry (Long64_t entry) override
307 {
308 benchmark.startTimer ();
309 std::size_t branchDataSize = branchReaderSize.getEntry (entry);
310 auto branchDataKey = branchReaderKey.getEntry (entry, branchDataSize);
311 auto branchDataIndex = branchReaderIndex.getEntry (entry, branchDataSize);
312 benchmark.stopTimer ();
313 benchmarkUnpack.startTimer ();
314 for (std::size_t index = 0; index < branchDataSize; ++index)
315 {
316 if (branchDataIndex[index] == static_cast<UInt_t>(-1))
317 columnData.addEmptyLink ();
318 else
319 columnData.addSplitLink (branchDataIndex[index], branchDataKey[index]);
320 }
321 offsets.push_back (columnData.size());
322 if (offsetColumn)
323 {
324 if (offsetColumn->size() != offsets.size())
325 throw std::runtime_error ("offset column not filled yet: " + outputColumns.at(1).name);
326 if (offsetColumn->back() != offsets.back())
327 throw std::runtime_error ("offset column does not match: " + outputColumns.at(1).name);
328 }
329 benchmarkUnpack.stopTimer ();
330 }
331
332 virtual void setData (ColumnVectorData& colData) override
333 {
334 if (outputColumns.at(0).columnIndex != ColumnVectorHeader::nullIndex)
335 colData.setColumn (outputColumns.at(0).columnIndex, columnData.size(), columnData.data());
336 if (outputColumns.at(1).columnIndex != ColumnVectorHeader::nullIndex)
337 colData.setColumn (outputColumns.at(1).columnIndex, offsets.size(), offsets.data());
338 for (std::size_t i = 2; i < outputColumns.size(); ++ i)
339 {
340 if (outputColumns.at(i).columnIndex != ColumnVectorHeader::nullIndex)
341 colData.setColumn (outputColumns.at(i).columnIndex, columnData.keysColumn().size(), columnData.keysColumn().data());
342 }
343 }
344
345 [[nodiscard]] virtual BranchPerfData getPerfData (float emptyTime) override
346 {
347 BranchPerfData result;
348 result.name = branchReaderSize.columnName();
349 result.timeRead = benchmark.getEntryTime(emptyTime);
350 result.timeUnpack = benchmarkUnpack.getEntryTime(emptyTime);
351 benchmark.setSilence();
352 benchmarkUnpack.setSilence();
353 result.entrySize = branchReaderSize.entrySize().value() + branchReaderKey.entrySize().value() + branchReaderIndex.entrySize().value();
354 result.uncompressedSize = branchReaderSize.uncompressedSize().value() + branchReaderKey.uncompressedSize().value() + branchReaderIndex.uncompressedSize().value();
355 result.numBaskets = branchReaderSize.numBaskets().value() + branchReaderKey.numBaskets().value() + branchReaderIndex.numBaskets().value();
356 result.entries = entries;
357 result.nullEntries = nullEntries;
358 return result;
359 }
360
361 virtual void collectColumnData () override
362 {
363 entries += columnData.size();
364 nullEntries += columnData.countNullLinks();
365 }
366 };
367
368 void makeTTreeColumnReaders (std::vector<std::shared_ptr<TestUtils::ColumnReader>>& knownColumns, TTree *tree)
369 {
370 tree->SetMakeClass(1);
371 {
372 std::unordered_map<std::string, TBranch*> branches;
373 {
374 TIter branchIter(tree->GetListOfBranches());
375 TObject* obj = nullptr;
376 while ((obj = branchIter()))
377 {
378 TBranch* branch = nullptr;
379 if ((branch = dynamic_cast<TBranch*>(obj)))
380 {
381 branches.emplace(branch->GetName(), branch);
382 TIter subBranchIter(branch->GetListOfBranches());
383 while ((obj = subBranchIter()))
384 {
385 if (auto subBranch = dynamic_cast<TBranch*>(obj))
386 branches.emplace(subBranch->GetName(), subBranch);
387 }
388 }
389 }
390 }
391
392 for (const auto& [name, branch] : branches)
393 {
394 if (name.find("AuxDyn.") != std::string::npos ||
395 name.find("Aux.") != std::string::npos)
396 {
397 TClass* branchClass = nullptr;
398 EDataType branchType{};
399 branch->GetExpectedType(branchClass, branchType);
400
401 // the key and index of split links are read by the link
402 // reader below, not as separate columns
403 if (name.ends_with (".m_persKey") || name.ends_with (".m_persIndex"))
404 continue;
405 if (branchClass != nullptr && std::string_view (branchClass->GetName()).find ("ElementLink<") != std::string_view::npos)
406 {
407 if (branch->GetListOfBranches()->GetEntries() > 0)
408 knownColumns.push_back (std::make_shared<ColumnReaderVectorSplitLink> (branch->GetName()));
409 else
410 addKeyIndexLinkReader<BranchReader> (knownColumns, branch->GetName(), branchClass->GetName());
411 continue;
412 }
413
414 if (branchClass == nullptr)
415 {
416 switch (branchType)
417 {
418 case kInt_t:
419 knownColumns.push_back(std::make_shared<ColumnReaderScalar<std::int32_t, BranchReader>>(branch->GetName()));
420 break;
421 case kUInt_t:
422 knownColumns.push_back(std::make_shared<ColumnReaderScalar<std::uint32_t, BranchReader>>(branch->GetName()));
423 break;
424 case kULong_t:
425 knownColumns.push_back(std::make_shared<ColumnReaderScalar<std::uint64_t, BranchReader>>(branch->GetName()));
426 break;
427 case kULong64_t:
428 knownColumns.push_back(std::make_shared<ColumnReaderScalar<std::uint64_t, BranchReader>>(branch->GetName()));
429 break;
430 case kFloat_t:
431 knownColumns.push_back(std::make_shared<ColumnReaderScalar<float, BranchReader>>(branch->GetName()));
432 break;
433 default:
434 // no-op
435 break;
436 }
437 } else
438 {
439 if (*branchClass->GetTypeInfo() == typeid(std::vector<float>))
440 {
441 knownColumns.push_back (std::make_shared<ColumnReaderVector<float,BranchReader>> (branch->GetName()));
442 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<double>))
443 {
444 knownColumns.push_back (std::make_shared<ColumnReaderVector<double,BranchReader>> (branch->GetName()));
445 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<char>))
446 {
447 knownColumns.push_back (std::make_shared<ColumnReaderVector<char,BranchReader>> (branch->GetName()));
448 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::int8_t>))
449 {
450 knownColumns.push_back (std::make_shared<ColumnReaderVector<std::int8_t,BranchReader>> (branch->GetName()));
451 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::uint8_t>))
452 {
453 knownColumns.push_back (std::make_shared<ColumnReaderVector<std::uint8_t,BranchReader>> (branch->GetName()));
454 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::int16_t>))
455 {
456 knownColumns.push_back (std::make_shared<ColumnReaderVector<std::int16_t,BranchReader>> (branch->GetName()));
457 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::uint16_t>))
458 {
459 knownColumns.push_back (std::make_shared<ColumnReaderVector<std::uint16_t,BranchReader>> (branch->GetName()));
460 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::int32_t>))
461 {
462 knownColumns.push_back (std::make_shared<ColumnReaderVector<std::int32_t,BranchReader>> (branch->GetName()));
463 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::uint32_t>))
464 {
465 knownColumns.push_back (std::make_shared<ColumnReaderVector<std::uint32_t,BranchReader>> (branch->GetName()));
466 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::int64_t>))
467 {
468 knownColumns.push_back (std::make_shared<ColumnReaderVector<std::int64_t,BranchReader>> (branch->GetName()));
469 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::uint64_t>))
470 {
471 knownColumns.push_back (std::make_shared<ColumnReaderVector<std::uint64_t,BranchReader>> (branch->GetName()));
472 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::vector<float>>))
473 {
474 knownColumns.push_back (std::make_shared<ColumnReaderVectorVector<float,BranchReader>> (branch->GetName()));
475 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::vector<std::int32_t>>))
476 {
477 knownColumns.push_back (std::make_shared<ColumnReaderVectorVector<std::int32_t,BranchReader>> (branch->GetName()));
478 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::vector<std::uint32_t>>))
479 {
480 knownColumns.push_back (std::make_shared<ColumnReaderVectorVector<std::uint32_t,BranchReader>> (branch->GetName()));
481 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::vector<std::uint64_t>>))
482 {
483 knownColumns.push_back (std::make_shared<ColumnReaderVectorVector<std::uint64_t,BranchReader>> (branch->GetName()));
484 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::vector<std::vector<std::size_t>>>))
485 {
486 knownColumns.push_back (std::make_shared<ColumnReaderVectorVectorVector<std::size_t,BranchReader>> (branch->GetName()));
487 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::vector<std::vector<unsigned char>>>))
488 {
489 knownColumns.push_back (std::make_shared<ColumnReaderVectorVectorVector<unsigned char,BranchReader>> (branch->GetName()));
490 } else if (*branchClass->GetTypeInfo() == typeid(std::vector<std::string>))
491 {
492 knownColumns.push_back (std::make_shared<ColumnReaderMetNames<BranchReader>> (branch->GetName()));
493 }
494 }
495 }
496 }
497 }
498 // This is a fallback for the case that we don't have an explicit
499 // `samplingPattern` branch in our input file (i.e. an older file),
500 // to allow us to still test tools needing it. This is likely not
501 // something that actual users can do (they need the new files), but
502 // for testing it seems like a reasonable workaround.
503 knownColumns.push_back(std::make_shared<ColumnReaderSamplingPattern<BranchReader>>("egammaClusters"));
504 }
505 }
506}
size_t size() const
Number of registered mappings.
a class that holds the columnar data for a single call
void setColumn(std::size_t columnIndex, std::size_t size, CT *dataPtr)
set the data for the given column
static constexpr std::size_t nullIndex
the index used for an invalid index (always has to be 0)
this is a simple benchmarking helper class wrapping timers from std::chrono
Definition Benchmark.h:51
BranchReaderArray(const std::string &val_branchName)
std::span< const T > getEntry(Long64_t entry, std::size_t size)
BranchReaderArray(const BranchReaderArray &)=delete
std::optional< float > uncompressedSize() const
std::optional< float > entrySize() const
BranchReaderArray & operator=(const BranchReaderArray &)=delete
const std::string & branchName() const
std::optional< float > entrySize() const
BranchReader(const BranchReader &)=delete
BranchReader(const std::string &val_branchName)
std::optional< unsigned > numBaskets()
BranchReader & operator=(const BranchReader &)=delete
std::optional< float > uncompressedSize() const
std::vector< OutputColumnInfo > outputColumns
the link columns for key+index links
static std::string dataColumnName(const std::string &baseName)
std::variant< TTree *, RNTupleBackend * > Backend
void makeTTreeColumnReaders(std::vector< std::shared_ptr< TestUtils::ColumnReader > > &knownColumns, TTree *tree)
bool addKeyIndexLinkReader(std::vector< std::shared_ptr< TestUtils::ColumnReader > > &knownColumns, const std::string &name, const std::string &typeName)
add the reader for a key+index link branch of the given type, if there is one
Definition index.py:1
the performance data for reading a single branch/column
TChain * tree