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();
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));
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>
168
169std::unique_ptr<ROOT::Experimental::RNTupleInspector>
171{
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
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>
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
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>(
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
465//------------------------------------------------------------------------------
466
469{
470 if (fieldId >= fDescriptor.GetNFields()) {
471 throw RException(R__FAIL("No field with ID " + std::to_string(fieldId) + " present"));
472 }
473
474 return fFieldTreeInfo.at(fieldId);
475}
476
479{
480 auto fieldId = fDescriptor.FindFieldId(fieldName);
481
483 throw RException(R__FAIL("Could not find field `" + std::string(fieldName) + "`"));
484 }
485
486 return GetFieldTreeInspector(fieldId);
487}
488
490 bool includeSubfields) const
491{
492 size_t typeCount = 0;
493
494 for (auto &[fldId, fldInfo] : fFieldTreeInfo) {
495 if (!includeSubfields && fldInfo.GetDescriptor().GetParentId() != fDescriptor.GetFieldZeroId()) {
496 continue;
497 }
498
499 if (std::regex_match(fldInfo.GetDescriptor().GetTypeName(), typeNamePattern)) {
500 typeCount++;
501 }
502 }
503
504 return typeCount;
505}
506
507std::vector<ROOT::DescriptorId_t>
509{
510 std::vector<ROOT::DescriptorId_t> fieldIds;
511
512 for (auto &[fldId, fldInfo] : fFieldTreeInfo) {
513
514 if (!searchInSubfields && fldInfo.GetDescriptor().GetParentId() != fDescriptor.GetFieldZeroId()) {
515 continue;
516 }
517
518 if (std::regex_match(fldInfo.GetDescriptor().GetFieldName(), fieldNamePattern)) {
519 fieldIds.emplace_back(fldId);
520 }
521 }
522
523 return fieldIds;
524}
525
527 std::ostream &output) const
528{
529 const auto &tupleDescriptor = GetDescriptor();
530 const bool isZeroField = fieldDescriptor.GetParentId() == ROOT::kInvalidDescriptorId;
531 if (isZeroField) {
532 output << "digraph D {\n";
533 output << "node[shape=box]\n";
534 }
535 const std::string &nodeId = (isZeroField) ? "0" : std::to_string(fieldDescriptor.GetId() + 1);
536 const std::string &description = fieldDescriptor.GetFieldDescription();
537 const std::uint32_t &version = fieldDescriptor.GetFieldVersion();
538
539 auto htmlEscape = [&](const std::string &in) -> std::string {
540 std::string out;
541 out.reserve(in.size());
542 for (const char &c : in) {
543 switch (c) {
544 case '&': out += "&amp;"; break;
545 case '<': out += "&lt;"; break;
546 case '>': out += "&gt;"; break;
547 case '\"': out += "&quot;"; break;
548 case '\'': out += "&#39;"; break;
549 default: out += c; break;
550 }
551 }
552 return out;
553 };
554
555 output << nodeId << "[label=<";
556 if (!isZeroField) {
557 output << "<b>Name: </b>" << htmlEscape(fieldDescriptor.GetFieldName()) << "<br></br>";
558 output << "<b>Type: </b>" << htmlEscape(fieldDescriptor.GetTypeName()) << "<br></br>";
559 output << "<b>ID: </b>" << std::to_string(fieldDescriptor.GetId()) << "<br></br>";
560 if (description != "")
561 output << "<b>Description: </b>" << htmlEscape(description) << "<br></br>";
562 if (version != 0)
563 output << "<b>Version: </b>" << version << "<br></br>";
564 } else
565 output << "<b>RFieldZero</b>";
566 output << ">]\n";
567 for (const auto &childFieldId : fieldDescriptor.GetLinkIds()) {
568 const auto &childFieldDescriptor = tupleDescriptor.GetFieldDescriptor(childFieldId);
569 output << nodeId + "->" + std::to_string(childFieldDescriptor.GetId() + 1) + "\n";
570 PrintFieldTreeAsDot(childFieldDescriptor, output);
571 }
572 if (isZeroField)
573 output << "}";
574}
575
576namespace {
577
578struct SpeedscopeFrame {
579 std::string fString;
580 std::uint64_t fOpeningPosition = 0;
581 std::uint64_t fClosingPosition = 0;
582};
583
584static void PrintSpeedscopeFrames(const std::vector<SpeedscopeFrame> &frames, std::ostream &output)
585{
586 output << "{\n";
587 output << " \"$schema\":\"https://www.speedscope.app/file-format-schema.json\",\n";
588 output << " \"shared\":{\n";
589 output << " \"frames\":[\n";
590
591 for (std::size_t i = 0; i < frames.size(); ++i) {
592 output << " { \"name\":\"" << frames[i].fString << "\" }" << (i + 1 < frames.size() ? ",\n" : "\n");
593 }
594
595 output << " ]\n";
596 output << " },\n";
597 output << " \"profiles\":[\n";
598 output << " {\n";
599 output << " \"type\":\"evented\",\n";
600 output << " \"name\":\"Flattened Timeline\",\n";
601 output << " \"unit\":\"bytes\",\n";
602 output << " \"startValue\":0,\n";
603 output << " \"endValue\":" << frames.back().fClosingPosition << ",\n";
604 output << " \"events\":[\n";
605
606 bool first = true;
607
608 // Parameter idx Index of the frame being processed
609 // Parameter limit
610 // - If the frame is not root: Closing Position of its father
611 // - If the frame is root: Closing Position of the last element of frames
612 // Returns the next index to be processed
613 std::function<std::size_t(std::size_t, std::uint32_t)> processRecursive = [&](std::size_t nextIdxToProcess,
614 std::uint32_t limit) -> std::size_t {
615 while (nextIdxToProcess < frames.size() && frames[nextIdxToProcess].fOpeningPosition < limit) {
616 const std::size_t currentIdx = nextIdxToProcess;
617
618 if (!first)
619 output << ",\n";
620
621 output << " {\"type\":\"O\",\"frame\":" << currentIdx
622 << ",\"at\":" << frames[currentIdx].fOpeningPosition << "}";
623 first = false;
624
626
627 output << ",\n {\"type\":\"C\",\"frame\":" << currentIdx
628 << ",\"at\":" << frames[currentIdx].fClosingPosition << "}";
629 }
630 return nextIdxToProcess;
631 };
632
633 processRecursive(0, frames.back().fClosingPosition);
634
635 output << "\n ]\n";
636 output << " }\n";
637 output << " ]\n";
638 output << "}\n";
639}
640} // namespace
641
644{
645 // There is only one format at the moment
647
648 const auto &tupleDescriptor = GetDescriptor();
650 const auto &rootFieldDescriptor = tupleDescriptor.GetFieldDescriptor(rootId);
651
652 std::vector<SpeedscopeFrame> frames;
653 std::uint64_t positionCursor = 0;
654
655 // Returns size of the visited field
656 auto visitFieldsRecursive = [&](auto &self, const ROOT::RFieldDescriptor &fieldDescriptor) -> std::size_t {
657 SpeedscopeFrame fieldSpeedscopeFrame;
658 fieldSpeedscopeFrame.fString =
659 tupleDescriptor.GetQualifiedFieldName(fieldDescriptor.GetId()) + " (" + fieldDescriptor.GetTypeName() + ")";
660 fieldSpeedscopeFrame.fOpeningPosition = positionCursor;
661 frames.push_back(fieldSpeedscopeFrame);
662
663 std::size_t fieldSpeedscopeFrameIndex = frames.size() - 1;
664
665 std::size_t subTreeSize = 0;
666 const auto &childIds = fieldDescriptor.GetLinkIds();
667
668 for (const auto &childFieldId : childIds) {
669 const auto &childFieldDescriptor = tupleDescriptor.GetFieldDescriptor(childFieldId);
671 }
672
673 for (const auto &columnDescriptor : tupleDescriptor.GetColumnIterable(fieldDescriptor.GetId())) {
674 const auto &columnInfo = GetColumnInspector(columnDescriptor.GetPhysicalId());
675 std::size_t columnSize = columnInfo.GetCompressedSize();
676
677 SpeedscopeFrame columnSpeedscopeFrame;
678 columnSpeedscopeFrame.fString =
679 "[col#" + std::to_string(columnDescriptor.GetPhysicalId()) + "] " +
680 tupleDescriptor.GetQualifiedFieldName(fieldDescriptor.GetId()) + " (" +
682 columnSpeedscopeFrame.fOpeningPosition = positionCursor;
684 columnSpeedscopeFrame.fClosingPosition = positionCursor;
685 frames.push_back(columnSpeedscopeFrame);
687 }
688
690
691 return subTreeSize;
692 };
693
694 const auto &topLevelIds = rootFieldDescriptor.GetLinkIds();
695 for (const auto &childId : topLevelIds) {
696 const auto &childFieldDescriptor = tupleDescriptor.GetFieldDescriptor(childId);
698 }
699
701}
702
705{
706 // There is only one format at the moment
708
709 const auto *pageSourceFile = dynamic_cast<const ROOT::Internal::RPageSourceFile *>(fPageSource.get());
710 // GetAnchorFromFile() only supports file-based backend, so better to check early
711 if (!pageSourceFile)
712 throw RException(R__FAIL("Disk profile is only supported for file-based page sources"));
714 if (!anchor)
715 R__LOG_WARNING(ROOT::Internal::NTupleLog()) << "Cannot retrieve RNTuple anchor";
716
717 const auto &descriptor = GetDescriptor();
718
719 struct RDiskPageLeaf {
720 std::uint64_t fPosition = 0;
721 std::uint64_t fSize = 0;
722 std::string fName;
723 std::array<DescriptorId_t, 3> fAncestors;
724 };
725 static constexpr std::array<const char *, 3> kAncestorsNames = {"cluster group", "cluster", "column range"};
726 std::vector<RDiskPageLeaf> pageLeaves;
727
728 // Collect all pageLeaves in whichever order the iterator provides
729 for (const auto &clusterGroupDescriptor : descriptor.GetClusterGroupIterable()) {
730 const auto groupId = clusterGroupDescriptor.GetId();
731
732 for (const auto clusterId : clusterGroupDescriptor.GetClusterIds()) {
733 const auto &clusterDescriptor = descriptor.GetClusterDescriptor(clusterId);
734
735 for (const auto &columnRange : clusterDescriptor.GetColumnRangeIterable()) {
736 const auto columnId = columnRange.GetPhysicalColumnId();
737
738 const auto &pageRange = clusterDescriptor.GetPageRange(columnId);
739 for (const auto &pageInfo : pageRange.GetPageInfos()) {
740 const auto &locator = pageInfo.GetLocator();
741
743 pageLeaf.fPosition = locator.GetPosition<std::uint64_t>();
744 pageLeaf.fSize = locator.GetNBytesOnStorage() +
746 pageLeaf.fName = "[page @" + std::to_string(pageLeaf.fPosition) + "]";
747 pageLeaf.fAncestors = {groupId, clusterId, columnId};
748 pageLeaves.push_back(pageLeaf);
749 }
750 }
751 }
752 }
753
754 // Sort pageLeafs by on-disk address
755 std::sort(pageLeaves.begin(), pageLeaves.end(),
756 [](const RDiskPageLeaf &a, const RDiskPageLeaf &b) { return a.fPosition < b.fPosition; });
757
758 // Remove aliases (the ntuple specification allows complete, but not partial, overlap between pages)
759 pageLeaves.erase(
760 std::unique(pageLeaves.begin(), pageLeaves.end(),
761 [](const RDiskPageLeaf &a, const RDiskPageLeaf &b) { return a.fPosition == b.fPosition; }),
762 pageLeaves.end());
763
764 std::vector<SpeedscopeFrame> frames;
765
766 // Construct frame for ntuple header
767 if (anchor) {
768 SpeedscopeFrame headerFrame;
769 headerFrame.fString = "ntuple header";
770 headerFrame.fOpeningPosition = anchor->GetSeekHeader();
771 headerFrame.fClosingPosition = anchor->GetSeekHeader() + anchor->GetNBytesHeader();
772 frames.push_back(headerFrame);
773 }
774
775 struct ROpenFrame {
776 ROOT::DescriptorId_t fId = 0; // clusterGroup, cluster, columnRange id
777 std::size_t fIndex = 0; // index in frames vector
778 };
779 std::vector<ROpenFrame> openFrames;
780 std::uint64_t previouspageLeafEnd = 0;
781
782 // Construct frames from the bottom (leafs ordered by disk address) upwards
783 for (const auto &pageLeaf : pageLeaves) {
784 std::size_t sharedDepth = 0;
785
786 // How many of the currently open ancestors does this pageLeaf share?
787 while (sharedDepth < openFrames.size() && sharedDepth < pageLeaf.fAncestors.size() &&
788 openFrames[sharedDepth].fId == pageLeaf.fAncestors[sharedDepth]) {
789 sharedDepth++;
790 }
791
792 // Close ancestors not shared with this pageLeaf (innermost first order)
793 while (openFrames.size() > sharedDepth) {
794 frames[openFrames.back().fIndex].fClosingPosition = previouspageLeafEnd;
795 openFrames.pop_back();
796 }
797
798 // Open the ancestors this pageLeaf needs (outermost first order)
799 for (std::size_t depth = sharedDepth; depth < pageLeaf.fAncestors.size(); ++depth) {
800 SpeedscopeFrame ancestorFrame;
801 ancestorFrame.fString =
802 "[" + std::string(kAncestorsNames[depth]) + " " + std::to_string(pageLeaf.fAncestors[depth]) + "]";
803 ancestorFrame.fOpeningPosition = pageLeaf.fPosition;
804 frames.push_back(ancestorFrame);
805
807 openFrame.fId = pageLeaf.fAncestors[depth];
808 openFrame.fIndex = frames.size() - 1;
809 openFrames.push_back(openFrame);
810 }
811
812 // Emit the pageLeaf itself
813 SpeedscopeFrame pageLeafFrame;
814 pageLeafFrame.fString = pageLeaf.fName;
815 pageLeafFrame.fOpeningPosition = pageLeaf.fPosition;
816 pageLeafFrame.fClosingPosition = pageLeaf.fPosition + pageLeaf.fSize;
817 frames.push_back(pageLeafFrame);
818
819 previouspageLeafEnd = pageLeaf.fPosition + pageLeaf.fSize;
820 }
821
822 // Close whatever is still open after the last pageLeaf
823 while (!openFrames.empty()) {
824 frames[openFrames.back().fIndex].fClosingPosition = previouspageLeafEnd;
825 openFrames.pop_back();
826 }
827
828 // Construct frames for page lists
829 for (const auto &clusterGroupDescriptor : descriptor.GetClusterGroupIterable()) {
830 const auto locator = clusterGroupDescriptor.GetPageListLocator();
831
832 SpeedscopeFrame pageListFrame;
833 pageListFrame.fString = "[page list " + std::to_string(clusterGroupDescriptor.GetId()) + "]";
834 pageListFrame.fOpeningPosition = locator.GetPosition<std::uint64_t>();
835 pageListFrame.fClosingPosition = locator.GetPosition<std::uint64_t>() + locator.GetNBytesOnStorage();
836 frames.push_back(pageListFrame);
837 }
838
839 // Construct frame for ntuple footer
840 if (anchor) {
841 SpeedscopeFrame footerFrame;
842 footerFrame.fString = "ntuple footer";
843 footerFrame.fOpeningPosition = anchor->GetSeekFooter();
844 footerFrame.fClosingPosition = anchor->GetSeekFooter() + anchor->GetNBytesFooter();
845 frames.push_back(footerFrame);
846 }
847
849}
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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:130
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.
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::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.
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:67
const_iterator begin() const
const_iterator end() const
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)