Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleInspector.cxx
Go to the documentation of this file.
1/// \file RNTupleInspector.cxx
2/// \author Florine de Geus <florine.willemijn.de.geus@cern.ch>
3/// \date 2023-01-09
4/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
5/// is welcome!
6
7/*************************************************************************
8 * Copyright (C) 1995-2023, Rene Brun and Fons Rademakers. *
9 * All rights reserved. *
10 * *
11 * For the licensing terms see $ROOTSYS/LICENSE. *
12 * For the list of contributors see $ROOTSYS/README/CREDITS. *
13 *************************************************************************/
14
16#include <ROOT/RError.hxx>
20#include "ROOT/RNTupleUtils.hxx"
21
22#include <TFile.h>
23
24#include <algorithm>
25#include <cstring>
26#include <deque>
27#include <exception>
28#include <functional>
29#include <iomanip>
30#include <iostream>
31
33
34ROOT::Experimental::RNTupleInspector::RNTupleInspector(std::unique_ptr<ROOT::Internal::RPageSource> pageSource)
35 : fPageSource(std::move(pageSource))
36{
37 fPageSource->Attach();
38 auto descriptorGuard = fPageSource->GetSharedDescriptorGuard();
39 fDescriptor = descriptorGuard->Clone();
40
43}
44
45// NOTE: outlined to avoid including RPageStorage in the header
47
49{
50 fCompressedSize = 0;
51 fUncompressedSize = 0;
52
53 std::vector<DescriptorId_t> clusterIds;
54 for (const auto &cgDesc : fDescriptor.GetClusterGroupIterable()) {
55 R__ASSERT(cgDesc.HasClusterDetails());
56 clusterIds.insert(clusterIds.end(), cgDesc.GetClusterIds().begin(), cgDesc.GetClusterIds().end());
57 }
58
59 for (const auto &colDesc : fDescriptor.GetColumnIterable()) {
60 if (colDesc.IsAliasColumn())
61 continue;
62
63 auto colId = colDesc.GetPhysicalId();
64
65 // We generate the default memory representation for the given column type in order
66 // to report the size _in memory_ of column elements.
67 std::uint32_t elemSize = RColumnElementBase::Generate(colDesc.GetType())->GetSize();
68 std::uint64_t nElems = 0;
69 std::vector<std::uint64_t> compressedPageSizes{};
70
71 for (auto cid : clusterIds) {
72 const auto &clusterDescriptor = fDescriptor.GetClusterDescriptor(cid);
73 if (!clusterDescriptor.ContainsColumn(colId)) {
74 continue;
75 }
76
77 auto columnRange = clusterDescriptor.GetColumnRange(colId);
78 if (columnRange.IsSuppressed())
79 continue;
80
81 nElems += columnRange.GetNElements();
82
83 if (!fCompressionSettings && columnRange.GetCompressionSettings()) {
84 fCompressionSettings = columnRange.GetCompressionSettings();
85 } else if (fCompressionSettings && columnRange.GetCompressionSettings() &&
86 (*fCompressionSettings != *columnRange.GetCompressionSettings())) {
87 // Note that currently all clusters and columns are compressed with the same settings and it is not yet
88 // possible to do otherwise. This means that currently, this exception should never be thrown, but this
89 // could change in the future.
90 throw RException(R__FAIL("compression setting mismatch between column ranges (" +
91 std::to_string(*fCompressionSettings) + " vs " +
92 std::to_string(*columnRange.GetCompressionSettings()) +
93 ") for column with physical ID " + std::to_string(colId)));
94 }
95
96 const auto &pageRange = clusterDescriptor.GetPageRange(colId);
97
98 for (const auto &page : pageRange.GetPageInfos()) {
99 compressedPageSizes.emplace_back(page.GetLocator().GetNBytesOnStorage());
100 fUncompressedSize += page.GetNElements() * elemSize;
101 }
102 }
103
104 fCompressedSize +=
105 std::accumulate(compressedPageSizes.begin(), compressedPageSizes.end(), static_cast<std::uint64_t>(0));
106 fColumnInfo.emplace(colId, RColumnInspector(colDesc, compressedPageSizes, elemSize, nElems));
107 }
108}
109
112{
113 std::uint64_t compressedSize = 0;
114 std::uint64_t uncompressedSize = 0;
115
116 for (const auto &colDescriptor : fDescriptor.GetColumnIterable(fieldId)) {
117 auto colInfo = GetColumnInspector(colDescriptor.GetPhysicalId());
118 compressedSize += colInfo.GetCompressedSize();
119 uncompressedSize += colInfo.GetUncompressedSize();
120 }
121
122 for (const auto &subFieldDescriptor : fDescriptor.GetFieldIterable(fieldId)) {
123 auto subFieldId = subFieldDescriptor.GetId();
124
125 auto subFieldInfo = CollectFieldTreeInfo(subFieldId);
126
127 compressedSize += subFieldInfo.GetCompressedSize();
128 uncompressedSize += subFieldInfo.GetUncompressedSize();
129 }
130
131 auto fieldInfo = RFieldTreeInspector(fDescriptor.GetFieldDescriptor(fieldId), compressedSize, uncompressedSize);
132 fFieldTreeInfo.emplace(fieldId, fieldInfo);
133 return fieldInfo;
134}
135
136std::vector<ROOT::DescriptorId_t>
138{
139 std::vector<ROOT::DescriptorId_t> colIds;
140 std::deque<ROOT::DescriptorId_t> fieldIdQueue{fieldId};
141
142 while (!fieldIdQueue.empty()) {
143 auto currId = fieldIdQueue.front();
144 fieldIdQueue.pop_front();
145
146 for (const auto &col : fDescriptor.GetColumnIterable(currId)) {
147 if (col.IsAliasColumn()) {
148 continue;
149 }
150
151 colIds.emplace_back(col.GetPhysicalId());
152 }
153
154 for (const auto &fld : fDescriptor.GetFieldIterable(currId)) {
155 fieldIdQueue.push_back(fld.GetId());
156 }
157 }
158
159 return colIds;
160}
161
162std::unique_ptr<ROOT::Experimental::RNTupleInspector>
164{
165 auto pageSource = ROOT::Internal::RPageSourceFile::CreateFromAnchor(sourceNTuple);
166 return std::unique_ptr<RNTupleInspector>(new RNTupleInspector(std::move(pageSource)));
167}
168
169std::unique_ptr<ROOT::Experimental::RNTupleInspector>
170ROOT::Experimental::RNTupleInspector::Create(std::string_view ntupleName, std::string_view sourceFileName)
171{
172 auto pageSource = ROOT::Internal::RPageSource::Create(ntupleName, sourceFileName);
173 return std::unique_ptr<RNTupleInspector>(new RNTupleInspector(std::move(pageSource)));
174}
175
177{
178 if (!fCompressionSettings)
179 return "unknown";
180
181 int algorithm = *fCompressionSettings / 100;
182 int level = *fCompressionSettings - (algorithm * 100);
183
185 " (level " + std::to_string(level) + ")";
186}
187
188//------------------------------------------------------------------------------
189
192{
193 if (physicalColumnId > fDescriptor.GetNPhysicalColumns()) {
194 throw RException(R__FAIL("No column with physical ID " + std::to_string(physicalColumnId) + " present"));
195 }
196
197 return fColumnInfo.at(physicalColumnId);
198}
199
201{
202 size_t typeCount = 0;
203
204 for (auto &[colId, colInfo] : fColumnInfo) {
205 if (colInfo.GetType() == colType) {
206 ++typeCount;
207 }
208 }
209
210 return typeCount;
211}
212
213std::vector<ROOT::DescriptorId_t>
215{
216 std::vector<ROOT::DescriptorId_t> colIds;
217
218 for (const auto &[colId, colInfo] : fColumnInfo) {
219 if (colInfo.GetType() == colType)
220 colIds.emplace_back(colId);
221 }
222
223 return colIds;
224}
225
226std::vector<ROOT::ENTupleColumnType> ROOT::Experimental::RNTupleInspector::GetColumnTypes()
227{
228 std::set<ROOT::ENTupleColumnType> colTypes;
229
230 for (const auto &[colId, colInfo] : fColumnInfo) {
231 colTypes.emplace(colInfo.GetType());
232 }
233
234 return std::vector(colTypes.begin(), colTypes.end());
235}
236
238{
239 struct ColumnTypeInfo {
240 std::uint64_t nElems = 0;
241 std::uint64_t compressedSize = 0;
242 std::uint64_t uncompressedSize = 0;
243 std::uint64_t nPages = 0;
244 std::uint32_t count = 0;
245
246 void operator+=(const RColumnInspector &colInfo)
247 {
248 this->count++;
249 this->nElems += colInfo.GetNElements();
250 this->compressedSize += colInfo.GetCompressedSize();
251 this->uncompressedSize += colInfo.GetUncompressedSize();
252 this->nPages += colInfo.GetNPages();
253 }
254
255 // Helper method to calculate compression factor
256 float GetCompressionFactor() const
257 {
258 if (compressedSize == 0)
259 return 1.0;
260 return static_cast<float>(uncompressedSize) / static_cast<float>(compressedSize);
261 }
262 };
263
264 std::map<ENTupleColumnType, ColumnTypeInfo> colTypeInfo;
265
266 // Collect information for each column
267 for (const auto &[colId, colInfo] : fColumnInfo) {
268 colTypeInfo[colInfo.GetType()] += colInfo;
269 }
270
271 switch (format) {
273 output << " column type | count | # elements | compressed bytes | uncompressed bytes | compression ratio | "
274 "# pages \n"
275 << "----------------|---------|-------------|------------------|--------------------|-------------------|-"
276 "------\n";
277 for (const auto &[colType, typeInfo] : colTypeInfo)
278 output << std::setw(15) << RColumnElementBase::GetColumnTypeName(colType) << " |" << std::setw(8)
279 << typeInfo.count << " |" << std::setw(12) << typeInfo.nElems << " |" << std::setw(17)
280 << typeInfo.compressedSize << " |" << std::setw(19) << typeInfo.uncompressedSize << " |" << std::fixed
281 << std::setprecision(3) << std::setw(18) << typeInfo.GetCompressionFactor() << " |" << std::setw(6)
282 << typeInfo.nPages << " \n";
283 break;
285 output << "columnType,count,nElements,compressedSize,uncompressedSize,compressionFactor,nPages\n";
286 for (const auto &[colType, typeInfo] : colTypeInfo) {
287 output << RColumnElementBase::GetColumnTypeName(colType) << "," << typeInfo.count << "," << typeInfo.nElems
288 << "," << typeInfo.compressedSize << "," << typeInfo.uncompressedSize << "," << std::fixed
289 << std::setprecision(3) << typeInfo.GetCompressionFactor() << "," << typeInfo.nPages << '\n';
290 }
291 break;
292 default: R__ASSERT(false && "Invalid print format");
293 }
294}
295
296std::unique_ptr<TH1D>
298 std::string_view histName, std::string_view histTitle)
299{
300 if (histName.empty()) {
301 switch (histKind) {
302 case ENTupleInspectorHist::kCount: histName = "colTypeCountHist"; break;
303 case ENTupleInspectorHist::kNElems: histName = "colTypeElemCountHist"; break;
304 case ENTupleInspectorHist::kCompressedSize: histName = "colTypeCompSizeHist"; break;
305 case ENTupleInspectorHist::kUncompressedSize: histName = "colTypeUncompSizeHist"; break;
306 default: throw RException(R__FAIL("Unknown histogram type"));
307 }
308 }
309
310 if (histTitle.empty()) {
311 switch (histKind) {
312 case ENTupleInspectorHist::kCount: histTitle = "Column count by type"; break;
313 case ENTupleInspectorHist::kNElems: histTitle = "Number of elements by column type"; break;
314 case ENTupleInspectorHist::kCompressedSize: histTitle = "Compressed size by column type"; break;
315 case ENTupleInspectorHist::kUncompressedSize: histTitle = "Uncompressed size by column type"; break;
316 default: throw RException(R__FAIL("Unknown histogram type"));
317 }
318 }
319
320 auto hist = std::make_unique<TH1D>(std::string(histName).c_str(), std::string(histTitle).c_str(), 1, 0, 1);
321
322 double data;
323 for (const auto &[colId, colInfo] : fColumnInfo) {
324 switch (histKind) {
325 case ENTupleInspectorHist::kCount: data = 1.; break;
326 case ENTupleInspectorHist::kNElems: data = colInfo.GetNElements(); break;
327 case ENTupleInspectorHist::kCompressedSize: data = colInfo.GetCompressedSize(); break;
328 case ENTupleInspectorHist::kUncompressedSize: data = colInfo.GetUncompressedSize(); break;
329 default: throw RException(R__FAIL("Unknown histogram type"));
330 }
331
332 hist->AddBinContent(hist->GetXaxis()->FindBin(RColumnElementBase::GetColumnTypeName(colInfo.GetType())), data);
333 }
334
335 return hist;
336}
337
338std::unique_ptr<TH1D>
340 std::string histName, std::string histTitle, size_t nBins)
341{
342 if (histTitle.empty())
343 histTitle = "Page size distribution for column with ID " + std::to_string(physicalColumnId);
344
345 return GetPageSizeDistribution({physicalColumnId}, histName, histTitle, nBins);
346}
347
349 std::string histName,
350 std::string histTitle, size_t nBins)
351{
352 if (histName.empty())
353 histName = "pageSizeHistCol" + std::string{RColumnElementBase::GetColumnTypeName(colType)};
354 if (histTitle.empty())
355 histTitle =
356 "Page size distribution for columns with type " + std::string{RColumnElementBase::GetColumnTypeName(colType)};
357
358 auto perTypeHist = GetPageSizeDistribution({colType}, histName, histTitle, nBins);
359
360 if (perTypeHist->GetNhists() < 1)
361 return std::make_unique<TH1D>(histName.c_str(), histTitle.c_str(), 64, 0, 0);
362
363 auto hist = std::unique_ptr<TH1D>(dynamic_cast<TH1D *>(perTypeHist->GetHists()->First()));
364
365 hist->SetName(histName.c_str());
366 hist->SetTitle(histTitle.c_str());
367 hist->SetXTitle("Page size (B)");
368 hist->SetYTitle("N_{pages}");
369 return hist;
370}
371
372std::unique_ptr<TH1D>
373ROOT::Experimental::RNTupleInspector::GetPageSizeDistribution(std::initializer_list<ROOT::DescriptorId_t> colIds,
374 std::string histName, std::string histTitle, size_t nBins)
375{
376 auto hist = std::make_unique<TH1D>();
377
378 if (histName.empty())
379 histName = "pageSizeHist";
380 hist->SetName(histName.c_str());
381 if (histTitle.empty())
382 histTitle = "Page size distribution";
383 hist->SetTitle(histTitle.c_str());
384 hist->SetXTitle("Page size (B)");
385 hist->SetYTitle("N_{pages}");
386
387 std::vector<std::uint64_t> pageSizes;
388 std::for_each(colIds.begin(), colIds.end(), [this, &pageSizes](const auto colId) {
389 auto colInfo = GetColumnInspector(colId);
390 pageSizes.insert(pageSizes.end(), colInfo.GetCompressedPageSizes().begin(),
391 colInfo.GetCompressedPageSizes().end());
392 });
393
394 if (!pageSizes.empty()) {
395 auto histMinMax = std::minmax_element(pageSizes.begin(), pageSizes.end());
396 hist->SetBins(nBins, *histMinMax.first,
397 *histMinMax.second + ((*histMinMax.second - *histMinMax.first) / static_cast<double>(nBins)));
398
399 for (const auto pageSize : pageSizes) {
400 hist->Fill(pageSize);
401 }
402 }
403
404 return hist;
405}
406
407std::unique_ptr<THStack>
408ROOT::Experimental::RNTupleInspector::GetPageSizeDistribution(std::initializer_list<ROOT::ENTupleColumnType> colTypes,
409 std::string histName, std::string histTitle, size_t nBins)
410{
411 if (histName.empty())
412 histName = "pageSizeHist";
413 if (histTitle.empty())
414 histTitle = "Per-column type page size distribution";
415
416 auto stackedHist = std::make_unique<THStack>(histName.c_str(), histTitle.c_str());
417
418 double histMin = std::numeric_limits<double>::max();
419 double histMax = 0;
420 std::map<ROOT::ENTupleColumnType, std::vector<std::uint64_t>> pageSizes;
421
422 std::vector<ROOT::ENTupleColumnType> colTypeVec = colTypes;
423 if (std::empty(colTypes)) {
424 colTypeVec = GetColumnTypes();
425 }
426
427 for (const auto colType : colTypeVec) {
428 auto colIds = GetColumnsByType(colType);
429
430 if (colIds.empty())
431 continue;
432
433 std::vector<std::uint64_t> pageSizesForColType;
434 std::for_each(colIds.cbegin(), colIds.cend(), [this, &pageSizesForColType](const auto colId) {
435 auto colInfo = GetColumnInspector(colId);
436 pageSizesForColType.insert(pageSizesForColType.end(), colInfo.GetCompressedPageSizes().begin(),
437 colInfo.GetCompressedPageSizes().end());
438 });
439 if (pageSizesForColType.empty())
440 continue;
441
442 pageSizes.emplace(colType, pageSizesForColType);
443
444 auto histMinMax = std::minmax_element(pageSizesForColType.begin(), pageSizesForColType.end());
445 histMin = std::min(histMin, static_cast<double>(*histMinMax.first));
446 histMax = std::max(histMax, static_cast<double>(*histMinMax.second));
447 }
448
449 for (const auto &[colType, pageSizesForColType] : pageSizes) {
450 auto hist = std::make_unique<TH1D>(
451 TString::Format("%s%s", histName.c_str(), RColumnElementBase::GetColumnTypeName(colType)),
452 RColumnElementBase::GetColumnTypeName(colType), nBins, histMin,
453 histMax + ((histMax - histMin) / static_cast<double>(nBins)));
454
455 for (const auto pageSize : pageSizesForColType) {
456 hist->Fill(pageSize);
457 }
458
459 stackedHist->Add(hist.release());
460 }
461
462 return stackedHist;
463}
464
466 std::initializer_list<ROOT::ENTupleColumnType> colTypes, std::string_view histName, std::string_view histTitle,
467 size_t nBins)
468{
469 if (histName.empty())
470 histName = "pagesPerClusterHist";
471 if (histTitle.empty())
472 histTitle = "#pages per cluster";
473
474 auto stackedHist = std::make_unique<THStack>(std::string(histName).c_str(), std::string(histTitle).c_str());
475 // Each element of the vector contains the sum of all pages of all columns of each type.
476 std::vector<std::unordered_map<ENTupleColumnType, std::uint64_t>> pagesPerClusterPerColumnType;
477
478 std::vector<ROOT::ENTupleColumnType> colTypeVec = colTypes;
479 if (std::empty(colTypes)) {
480 colTypeVec = GetColumnTypes();
481 }
482
483 R__ASSERT(fDescriptor.GetNClusters() == fDescriptor.GetNActiveClusters());
484 for (const auto &clDesc : fDescriptor.GetActiveClusterIterable()) {
485 auto &pagesPerColumnTypeInThisCluster = pagesPerClusterPerColumnType.emplace_back();
486 for (const auto &colRange : clDesc.GetColumnRangeIterable()) {
487 const auto colId = colRange.GetPhysicalColumnId();
488 const auto &colDesc = fDescriptor.GetColumnDescriptor(colId);
489 const auto nPages = clDesc.GetPageRange(colId).GetPageInfos().size();
490 auto &nPagesForThisType = pagesPerColumnTypeInThisCluster[colDesc.GetType()];
491 nPagesForThisType += nPages;
492 }
493 }
494
495 double histMin = std::numeric_limits<double>::max();
496 double histMax = 0;
497 for (const auto &pagesPerType : pagesPerClusterPerColumnType) {
498 auto [min, max] = std::minmax_element(pagesPerType.begin(), pagesPerType.end(),
499 [](const auto &a, const auto &b) { return a.second < b.second; });
500 histMin = std::min<double>(histMin, min->second);
501 histMax = std::max<double>(histMax, max->second);
502 }
503
504 std::array<std::unique_ptr<TH1L>, static_cast<std::size_t>(ENTupleColumnType::kMax)> histsPerType = {};
505 for (const auto &pagesPerType : pagesPerClusterPerColumnType) {
506 for (const auto &[colType, nPages] : pagesPerType) {
507 auto &hist = histsPerType[static_cast<std::size_t>(colType)];
508 if (!hist) {
509 hist = std::make_unique<TH1L>(ROOT::Internal::RColumnElementBase::GetColumnTypeName(colType),
511 histMin, histMax + ((histMax - histMin) / static_cast<double>(nBins)));
512 }
513 hist->Fill(nPages);
514 }
515 }
516
517 for (auto &hist : histsPerType) {
518 if (hist) {
519 stackedHist->Add(hist.release());
520 }
521 }
522
523 return stackedHist;
524}
525
526//------------------------------------------------------------------------------
527
530{
531 if (fieldId >= fDescriptor.GetNFields()) {
532 throw RException(R__FAIL("No field with ID " + std::to_string(fieldId) + " present"));
533 }
534
535 return fFieldTreeInfo.at(fieldId);
536}
537
540{
541 auto fieldId = fDescriptor.FindFieldId(fieldName);
542
543 if (fieldId == kInvalidDescriptorId) {
544 throw RException(R__FAIL("Could not find field `" + std::string(fieldName) + "`"));
545 }
546
547 return GetFieldTreeInspector(fieldId);
548}
549
550size_t ROOT::Experimental::RNTupleInspector::GetFieldCountByType(const std::regex &typeNamePattern,
551 bool includeSubfields) const
552{
553 size_t typeCount = 0;
554
555 for (auto &[fldId, fldInfo] : fFieldTreeInfo) {
556 if (!includeSubfields && fldInfo.GetDescriptor().GetParentId() != fDescriptor.GetFieldZeroId()) {
557 continue;
558 }
559
560 if (std::regex_match(fldInfo.GetDescriptor().GetTypeName(), typeNamePattern)) {
561 typeCount++;
562 }
563 }
564
565 return typeCount;
566}
567
568std::vector<ROOT::DescriptorId_t>
569ROOT::Experimental::RNTupleInspector::GetFieldsByName(const std::regex &fieldNamePattern, bool searchInSubfields) const
570{
571 std::vector<ROOT::DescriptorId_t> fieldIds;
572
573 for (auto &[fldId, fldInfo] : fFieldTreeInfo) {
574
575 if (!searchInSubfields && fldInfo.GetDescriptor().GetParentId() != fDescriptor.GetFieldZeroId()) {
576 continue;
577 }
578
579 if (std::regex_match(fldInfo.GetDescriptor().GetFieldName(), fieldNamePattern)) {
580 fieldIds.emplace_back(fldId);
581 }
582 }
583
584 return fieldIds;
585}
586
588 std::ostream &output) const
589{
590 const auto &tupleDescriptor = GetDescriptor();
591 const bool isZeroField = fieldDescriptor.GetParentId() == ROOT::kInvalidDescriptorId;
592 if (isZeroField) {
593 output << "digraph D {\n";
594 output << "node[shape=box]\n";
595 }
596 const std::string &nodeId = (isZeroField) ? "0" : std::to_string(fieldDescriptor.GetId() + 1);
597 const std::string &description = fieldDescriptor.GetFieldDescription();
598 const std::uint32_t &version = fieldDescriptor.GetFieldVersion();
599
600 auto htmlEscape = [&](const std::string &in) -> std::string {
601 std::string out;
602 out.reserve(in.size());
603 for (const char &c : in) {
604 switch (c) {
605 case '&': out += "&amp;"; break;
606 case '<': out += "&lt;"; break;
607 case '>': out += "&gt;"; break;
608 case '\"': out += "&quot;"; break;
609 case '\'': out += "&#39;"; break;
610 default: out += c; break;
611 }
612 }
613 return out;
614 };
615
616 output << nodeId << "[label=<";
617 if (!isZeroField) {
618 output << "<b>Name: </b>" << htmlEscape(fieldDescriptor.GetFieldName()) << "<br></br>";
619 output << "<b>Type: </b>" << htmlEscape(fieldDescriptor.GetTypeName()) << "<br></br>";
620 output << "<b>ID: </b>" << std::to_string(fieldDescriptor.GetId()) << "<br></br>";
621 if (description != "")
622 output << "<b>Description: </b>" << htmlEscape(description) << "<br></br>";
623 if (version != 0)
624 output << "<b>Version: </b>" << version << "<br></br>";
625 } else
626 output << "<b>RFieldZero</b>";
627 output << ">]\n";
628 for (const auto &childFieldId : fieldDescriptor.GetLinkIds()) {
629 const auto &childFieldDescriptor = tupleDescriptor.GetFieldDescriptor(childFieldId);
630 output << nodeId + "->" + std::to_string(childFieldDescriptor.GetId() + 1) + "\n";
631 PrintFieldTreeAsDot(childFieldDescriptor, output);
632 }
633 if (isZeroField)
634 output << "}";
635}
636
637namespace {
638
639struct SpeedscopeFrame {
640 std::string fString;
641 std::uint64_t fOpeningPosition = 0;
642 std::uint64_t fClosingPosition = 0;
643};
644
645static void PrintSpeedscopeFrames(const std::vector<SpeedscopeFrame> &frames, std::ostream &output)
646{
647 output << "{\n";
648 output << " \"$schema\":\"https://www.speedscope.app/file-format-schema.json\",\n";
649 output << " \"shared\":{\n";
650 output << " \"frames\":[\n";
651
652 for (std::size_t i = 0; i < frames.size(); ++i) {
653 output << " { \"name\":\"" << frames[i].fString << "\" }" << (i + 1 < frames.size() ? ",\n" : "\n");
654 }
655
656 output << " ]\n";
657 output << " },\n";
658 output << " \"profiles\":[\n";
659 output << " {\n";
660 output << " \"type\":\"evented\",\n";
661 output << " \"name\":\"Flattened Timeline\",\n";
662 output << " \"unit\":\"bytes\",\n";
663 output << " \"startValue\":0,\n";
664 output << " \"endValue\":" << frames.back().fClosingPosition << ",\n";
665 output << " \"events\":[\n";
666
667 bool first = true;
668
669 // Parameter idx Index of the frame being processed
670 // Parameter limit
671 // - If the frame is not root: Closing Position of its father
672 // - If the frame is root: Closing Position of the last element of frames
673 // Returns the next index to be processed
674 std::function<std::size_t(std::size_t, std::uint32_t)> processRecursive = [&](std::size_t nextIdxToProcess,
675 std::uint32_t limit) -> std::size_t {
676 while (nextIdxToProcess < frames.size() && frames[nextIdxToProcess].fOpeningPosition < limit) {
677 const std::size_t currentIdx = nextIdxToProcess;
678
679 if (!first)
680 output << ",\n";
681
682 output << " {\"type\":\"O\",\"frame\":" << currentIdx
683 << ",\"at\":" << frames[currentIdx].fOpeningPosition << "}";
684 first = false;
685
686 nextIdxToProcess = processRecursive(nextIdxToProcess + 1, frames[currentIdx].fClosingPosition);
687
688 output << ",\n {\"type\":\"C\",\"frame\":" << currentIdx
689 << ",\"at\":" << frames[currentIdx].fClosingPosition << "}";
690 }
691 return nextIdxToProcess;
692 };
693
694 processRecursive(0, frames.back().fClosingPosition);
695
696 output << "\n ]\n";
697 output << " }\n";
698 output << " ]\n";
699 output << "}\n";
700}
701} // namespace
702
704 [[maybe_unused]] ESchemaProfileFormat format) const
705{
706 // There is only one format at the moment
708
709 const auto &tupleDescriptor = GetDescriptor();
710 ROOT::DescriptorId_t rootId = tupleDescriptor.GetFieldZeroId();
711 const auto &rootFieldDescriptor = tupleDescriptor.GetFieldDescriptor(rootId);
712
713 std::vector<SpeedscopeFrame> frames;
714 std::uint64_t positionCursor = 0;
715
716 // Returns size of the visited field
717 auto visitFieldsRecursive = [&](auto &self, const ROOT::RFieldDescriptor &fieldDescriptor) -> std::size_t {
718 SpeedscopeFrame fieldSpeedscopeFrame;
719 fieldSpeedscopeFrame.fString =
720 tupleDescriptor.GetQualifiedFieldName(fieldDescriptor.GetId()) + " (" + fieldDescriptor.GetTypeName() + ")";
721 fieldSpeedscopeFrame.fOpeningPosition = positionCursor;
722 frames.push_back(fieldSpeedscopeFrame);
723
724 std::size_t fieldSpeedscopeFrameIndex = frames.size() - 1;
725
726 std::size_t subTreeSize = 0;
727 const auto &childIds = fieldDescriptor.GetLinkIds();
728
729 for (const auto &childFieldId : childIds) {
730 const auto &childFieldDescriptor = tupleDescriptor.GetFieldDescriptor(childFieldId);
731 subTreeSize += self(self, childFieldDescriptor);
732 }
733
734 for (const auto &columnDescriptor : tupleDescriptor.GetColumnIterable(fieldDescriptor.GetId())) {
735 const auto &columnInfo = GetColumnInspector(columnDescriptor.GetPhysicalId());
736 std::size_t columnSize = columnInfo.GetCompressedSize();
737
738 SpeedscopeFrame columnSpeedscopeFrame;
739 columnSpeedscopeFrame.fString =
740 "[col#" + std::to_string(columnDescriptor.GetPhysicalId()) + "] " +
741 tupleDescriptor.GetQualifiedFieldName(fieldDescriptor.GetId()) + " (" +
742 ROOT::Internal::RColumnElementBase::GetColumnTypeName(columnDescriptor.GetType()) + ")";
743 columnSpeedscopeFrame.fOpeningPosition = positionCursor;
744 positionCursor += columnSize;
745 columnSpeedscopeFrame.fClosingPosition = positionCursor;
746 frames.push_back(columnSpeedscopeFrame);
747 subTreeSize += columnSize;
748 }
749
750 frames[fieldSpeedscopeFrameIndex].fClosingPosition = positionCursor;
751
752 return subTreeSize;
753 };
754
755 const auto &topLevelIds = rootFieldDescriptor.GetLinkIds();
756 for (const auto &childId : topLevelIds) {
757 const auto &childFieldDescriptor = tupleDescriptor.GetFieldDescriptor(childId);
758 visitFieldsRecursive(visitFieldsRecursive, childFieldDescriptor);
759 }
760
761 PrintSpeedscopeFrames(frames, output);
762}
763
765 [[maybe_unused]] ESchemaProfileFormat format) const
766{
767 // There is only one format at the moment
769
770 const auto *pageSourceFile = dynamic_cast<const ROOT::Internal::RPageSourceFile *>(fPageSource.get());
771 // GetAnchorFromFile() only supports file-based backend, so better to check early
772 if (!pageSourceFile)
773 throw RException(R__FAIL("Disk profile is only supported for file-based page sources"));
774 const auto anchor = ROOT::Internal::GetAnchorFromFile(*pageSourceFile);
775 if (!anchor)
776 R__LOG_WARNING(ROOT::Internal::NTupleLog()) << "Cannot retrieve RNTuple anchor";
777
778 const auto &descriptor = GetDescriptor();
779
780 struct RDiskPageLeaf {
781 std::uint64_t fPosition = 0;
782 std::uint64_t fSize = 0;
783 std::string fName;
784 std::array<DescriptorId_t, 3> fAncestors;
785 };
786 static constexpr std::array<const char *, 3> kAncestorsNames = {"cluster group", "cluster", "column range"};
787 std::vector<RDiskPageLeaf> pageLeaves;
788
789 // Collect all pageLeaves in whichever order the iterator provides
790 for (const auto &clusterGroupDescriptor : descriptor.GetClusterGroupIterable()) {
791 const auto groupId = clusterGroupDescriptor.GetId();
792
793 for (const auto clusterId : clusterGroupDescriptor.GetClusterIds()) {
794 const auto &clusterDescriptor = descriptor.GetClusterDescriptor(clusterId);
795
796 for (const auto &columnRange : clusterDescriptor.GetColumnRangeIterable()) {
797 const auto columnId = columnRange.GetPhysicalColumnId();
798
799 const auto &pageRange = clusterDescriptor.GetPageRange(columnId);
800 for (const auto &pageInfo : pageRange.GetPageInfos()) {
801 const auto &locator = pageInfo.GetLocator();
802
803 RDiskPageLeaf pageLeaf;
804 pageLeaf.fPosition = locator.GetPosition<std::uint64_t>();
805 pageLeaf.fSize = locator.GetNBytesOnStorage() +
807 pageLeaf.fName = "[page @" + std::to_string(pageLeaf.fPosition) + "]";
808 pageLeaf.fAncestors = {groupId, clusterId, columnId};
809 pageLeaves.push_back(pageLeaf);
810 }
811 }
812 }
813 }
814
815 // Sort pageLeafs by on-disk address
816 std::sort(pageLeaves.begin(), pageLeaves.end(),
817 [](const RDiskPageLeaf &a, const RDiskPageLeaf &b) { return a.fPosition < b.fPosition; });
818
819 // Remove aliases (the ntuple specification allows complete, but not partial, overlap between pages)
820 pageLeaves.erase(
821 std::unique(pageLeaves.begin(), pageLeaves.end(),
822 [](const RDiskPageLeaf &a, const RDiskPageLeaf &b) { return a.fPosition == b.fPosition; }),
823 pageLeaves.end());
824
825 std::vector<SpeedscopeFrame> frames;
826
827 // Construct frame for ntuple header
828 if (anchor) {
829 SpeedscopeFrame headerFrame;
830 headerFrame.fString = "ntuple header";
831 headerFrame.fOpeningPosition = anchor->GetSeekHeader();
832 headerFrame.fClosingPosition = anchor->GetSeekHeader() + anchor->GetNBytesHeader();
833 frames.push_back(headerFrame);
834 }
835
836 struct ROpenFrame {
837 ROOT::DescriptorId_t fId = 0; // clusterGroup, cluster, columnRange id
838 std::size_t fIndex = 0; // index in frames vector
839 };
840 std::vector<ROpenFrame> openFrames;
841 std::uint64_t previouspageLeafEnd = 0;
842
843 // Construct frames from the bottom (leafs ordered by disk address) upwards
844 for (const auto &pageLeaf : pageLeaves) {
845 std::size_t sharedDepth = 0;
846
847 // How many of the currently open ancestors does this pageLeaf share?
848 while (sharedDepth < openFrames.size() && sharedDepth < pageLeaf.fAncestors.size() &&
849 openFrames[sharedDepth].fId == pageLeaf.fAncestors[sharedDepth]) {
850 sharedDepth++;
851 }
852
853 // Close ancestors not shared with this pageLeaf (innermost first order)
854 while (openFrames.size() > sharedDepth) {
855 frames[openFrames.back().fIndex].fClosingPosition = previouspageLeafEnd;
856 openFrames.pop_back();
857 }
858
859 // Open the ancestors this pageLeaf needs (outermost first order)
860 for (std::size_t depth = sharedDepth; depth < pageLeaf.fAncestors.size(); ++depth) {
861 SpeedscopeFrame ancestorFrame;
862 ancestorFrame.fString =
863 "[" + std::string(kAncestorsNames[depth]) + " " + std::to_string(pageLeaf.fAncestors[depth]) + "]";
864 ancestorFrame.fOpeningPosition = pageLeaf.fPosition;
865 frames.push_back(ancestorFrame);
866
867 ROpenFrame openFrame;
868 openFrame.fId = pageLeaf.fAncestors[depth];
869 openFrame.fIndex = frames.size() - 1;
870 openFrames.push_back(openFrame);
871 }
872
873 // Emit the pageLeaf itself
874 SpeedscopeFrame pageLeafFrame;
875 pageLeafFrame.fString = pageLeaf.fName;
876 pageLeafFrame.fOpeningPosition = pageLeaf.fPosition;
877 pageLeafFrame.fClosingPosition = pageLeaf.fPosition + pageLeaf.fSize;
878 frames.push_back(pageLeafFrame);
879
880 previouspageLeafEnd = pageLeaf.fPosition + pageLeaf.fSize;
881 }
882
883 // Close whatever is still open after the last pageLeaf
884 while (!openFrames.empty()) {
885 frames[openFrames.back().fIndex].fClosingPosition = previouspageLeafEnd;
886 openFrames.pop_back();
887 }
888
889 // Construct frames for page lists
890 for (const auto &clusterGroupDescriptor : descriptor.GetClusterGroupIterable()) {
891 const auto locator = clusterGroupDescriptor.GetPageListLocator();
892
893 SpeedscopeFrame pageListFrame;
894 pageListFrame.fString = "[page list " + std::to_string(clusterGroupDescriptor.GetId()) + "]";
895 pageListFrame.fOpeningPosition = locator.GetPosition<std::uint64_t>();
896 pageListFrame.fClosingPosition = locator.GetPosition<std::uint64_t>() + locator.GetNBytesOnStorage();
897 frames.push_back(pageListFrame);
898 }
899
900 // Construct frame for ntuple footer
901 if (anchor) {
902 SpeedscopeFrame footerFrame;
903 footerFrame.fString = "ntuple footer";
904 footerFrame.fOpeningPosition = anchor->GetSeekFooter();
905 footerFrame.fClosingPosition = anchor->GetSeekFooter() + anchor->GetNBytesFooter();
906 frames.push_back(footerFrame);
907 }
908
909 PrintSpeedscopeFrames(frames, output);
910}
dim_t fSize
#define R__FAIL(msg)
Short-hand to return an RResult<T> in an error state; the RError is implicitly converted into RResult...
Definition RError.hxx:322
#define R__LOG_WARNING(...)
Definition RLogger.hxx:357
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:139
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
std::string & operator+=(std::string &left, const TString &right)
Definition TString.h:496
The available trivial, native content types of a column.
Provides column-level storage information.
Inspect on-disk and storage-related information of an RNTuple.
std::vector< ROOT::DescriptorId_t > GetFieldsByName(const std::regex &fieldNamePattern, bool searchInSubfields=true) const
Get the IDs of (sub-)fields whose name matches the given string.
const RFieldTreeInspector & GetFieldTreeInspector(ROOT::DescriptorId_t fieldId) const
Get storage information for a given (sub)field by ID.
std::unique_ptr< TH1D > GetPageSizeDistribution(ROOT::DescriptorId_t physicalColumnId, std::string histName="", std::string histTitle="", size_t nBins=64)
Get a histogram containing the size distribution of the compressed pages for an individual column.
size_t GetColumnCountByType(ROOT::ENTupleColumnType colType) const
Get the number of columns of a given type present in the RNTuple.
std::vector< ROOT::ENTupleColumnType > GetColumnTypes()
Get all column types present in the RNTuple being inspected.
size_t GetFieldCountByType(const std::regex &typeNamePattern, bool searchInSubfields=true) const
Get the number of fields of a given type or class present in the RNTuple.
void PrintSchemaProfile(std::ostream &output=std::cout, ESchemaProfileFormat format=ESchemaProfileFormat::kSpeedscopeJSON) const
Print a string that represents the tree of the (sub)fields and columns of an RNTuple in a format whic...
std::vector< ROOT::DescriptorId_t > GetColumnsByType(ROOT::ENTupleColumnType colType)
Get the IDs of all columns with the given type.
std::string GetCompressionSettingsAsString() const
Get a string describing compression settings of the RNTuple being inspected.
RFieldTreeInspector CollectFieldTreeInfo(ROOT::DescriptorId_t fieldId)
Recursively gather field-level information.
RNTupleInspector(std::unique_ptr< ROOT::Internal::RPageSource > pageSource)
void PrintColumnTypeInfo(ENTupleInspectorPrintFormat format=ENTupleInspectorPrintFormat::kTable, std::ostream &output=std::cout)
Print storage information per column type.
std::unique_ptr< THStack > GetPagesPerClusterDistribution(std::initializer_list< ROOT::ENTupleColumnType > colTypes={}, std::string_view histName="", std::string_view histTitle="", size_t nBins=32)
void PrintDiskProfile(std::ostream &output=std::cout, ESchemaProfileFormat format=ESchemaProfileFormat::kSpeedscopeJSON) const
Print a string that represents the on-disk storage of the cluster groups, clusters,...
const RColumnInspector & GetColumnInspector(ROOT::DescriptorId_t physicalColumnId) const
Get storage information for a given column.
std::unique_ptr< ROOT::Internal::RPageSource > fPageSource
static std::unique_ptr< RNTupleInspector > Create(const RNTuple &sourceNTuple)
Create a new RNTupleInspector.
void CollectColumnInfo()
Gather column-level and RNTuple-level information.
void PrintFieldTreeAsDot(const ROOT::RFieldDescriptor &fieldDescriptor, std::ostream &output=std::cout) const
Print a .dot string that represents the tree of the (sub)fields of an RNTuple.
std::vector< ROOT::DescriptorId_t > GetAllColumnsOfField(ROOT::DescriptorId_t fieldId) const
Get the columns that make up the given field, including its subfields.
std::unique_ptr< TH1D > GetColumnTypeInfoAsHist(ENTupleInspectorHist histKind, std::string_view histName="", std::string_view histTitle="")
Get a histogram showing information for each column type present,.
A column element encapsulates the translation between basic C++ types and their column representation...
static const char * GetColumnTypeName(ROOT::ENTupleColumnType type)
static std::unique_ptr< RColumnElementBase > Generate(ROOT::ENTupleColumnType type)
If CppT == void, use the default C++ type for the given column type.
Storage provider that reads ntuple pages from a file.
static std::unique_ptr< RPageSourceFile > CreateFromAnchor(const RNTuple &anchor, const ROOT::RNTupleReadOptions &options=ROOT::RNTupleReadOptions())
Used from the RNTuple class to build a datasource if the anchor is already available.
static std::unique_ptr< RPageSource > Create(std::string_view ntupleName, std::string_view location, const ROOT::RNTupleReadOptions &options=ROOT::RNTupleReadOptions())
Guess the concrete derived page source from the file name (location)
static constexpr std::size_t kNBytesPageChecksum
The page checksum is a 64bit xxhash3.
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
Metadata stored for every field of an RNTuple.
ROOT::DescriptorId_t GetId() const
std::uint32_t GetFieldVersion() const
const std::vector< ROOT::DescriptorId_t > & GetLinkIds() const
ROOT::DescriptorId_t GetParentId() const
const std::string & GetFieldDescription() const
const std::string & GetFieldName() const
const std::string & GetTypeName() const
ROOT::NTupleSize_t R__DEPRECATED(6, 46, "This function is ill-conceived in the descriptor " "as not all cluster descriptors may be present. This interface is no longer publicly exposed.") GetNElements(ROOT ROOT::DescriptorId_t GetFieldZeroId() const
Returns the logical parent of all top-level RNTuple data fields.
RNTupleDescriptor Clone() const
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:67
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2460
@ kSpeedscopeJSON
https://www.speedscope.app/file-format-schema.json
ROOT::RLogChannel & NTupleLog()
Log channel for RNTuple diagnostics.
const ROOT::RNTuple * GetAnchorFromFile(const RPageSourceFile &source)
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
constexpr DescriptorId_t kInvalidDescriptorId
EValues
Note: this is only temporarily a struct and will become a enum class hence the name convention used.
Definition Compression.h:88
static std::string AlgorithmToString(EAlgorithm::EValues algorithm)