Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleInspector.cxx
Go to the documentation of this file.
1/// \file RNTupleInspector.cxx
2/// \ingroup NTuple ROOT7
3/// \author Florine de Geus <florine.willemijn.de.geus@cern.ch>
4/// \date 2023-01-09
5/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6/// is welcome!
7
8/*************************************************************************
9 * Copyright (C) 1995-2023, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
17#include <ROOT/RError.hxx>
21#include <ROOT/RError.hxx>
22
23#include <TFile.h>
24
25#include <algorithm>
26#include <cstring>
27#include <deque>
28#include <exception>
29#include <iomanip>
30#include <iostream>
31
33 std::unique_ptr<ROOT::Experimental::Internal::RPageSource> pageSource)
34 : fPageSource(std::move(pageSource))
35{
36 fPageSource->Attach();
37 auto descriptorGuard = fPageSource->GetSharedDescriptorGuard();
38 fDescriptor = descriptorGuard->Clone();
39
41 CollectFieldTreeInfo(fDescriptor->GetFieldZeroId());
42}
43
45{
46 fCompressedSize = 0;
47 fUncompressedSize = 0;
48
49 for (const auto &colDesc : fDescriptor->GetColumnIterable()) {
50 if (colDesc.IsAliasColumn())
51 continue;
52
53 auto colId = colDesc.GetPhysicalId();
54
55 // We generate the default memory representation for the given column type in order
56 // to report the size _in memory_ of column elements.
57 auto colType = colDesc.GetModel().GetType();
58 std::uint32_t elemSize = ROOT::Experimental::Internal::RColumnElementBase::Generate(colType)->GetSize();
59 std::uint64_t nElems = 0;
60 std::vector<std::uint64_t> compressedPageSizes{};
61
62 for (const auto &clusterDescriptor : fDescriptor->GetClusterIterable()) {
63 if (!clusterDescriptor.ContainsColumn(colId)) {
64 continue;
65 }
66
67 auto columnRange = clusterDescriptor.GetColumnRange(colId);
68 nElems += columnRange.fNElements;
69
70 if (fCompressionSettings == -1) {
71 fCompressionSettings = columnRange.fCompressionSettings;
72 } else if (fCompressionSettings != columnRange.fCompressionSettings &&
73 columnRange.fCompressionSettings != kUnknownCompressionSettings) {
74 // Note that currently all clusters and columns are compressed with the same settings and it is not yet
75 // possible to do otherwise. This means that currently, this exception should never be thrown, but this
76 // could change in the future.
77 throw RException(R__FAIL("compression setting mismatch between column ranges (" +
78 std::to_string(fCompressionSettings) + " vs " +
79 std::to_string(columnRange.fCompressionSettings) +
80 ") for column with physical ID " + std::to_string(colId)));
81 }
82
83 const auto &pageRange = clusterDescriptor.GetPageRange(colId);
84
85 for (const auto &page : pageRange.fPageInfos) {
86 compressedPageSizes.emplace_back(page.fLocator.fBytesOnStorage);
87 fUncompressedSize += page.fNElements * elemSize;
88 }
89 }
90
91 fCompressedSize += std::accumulate(compressedPageSizes.begin(), compressedPageSizes.end(), 0);
92 fColumnInfo.emplace(colId, RColumnInspector(colDesc, compressedPageSizes, elemSize, nElems));
93 }
94}
95
98{
99 std::uint64_t compressedSize = 0;
100 std::uint64_t uncompressedSize = 0;
101
102 for (const auto &colDescriptor : fDescriptor->GetColumnIterable(fieldId)) {
103 auto colInfo = GetColumnInspector(colDescriptor.GetPhysicalId());
104 compressedSize += colInfo.GetCompressedSize();
105 uncompressedSize += colInfo.GetUncompressedSize();
106 }
107
108 for (const auto &subFieldDescriptor : fDescriptor->GetFieldIterable(fieldId)) {
109 DescriptorId_t subFieldId = subFieldDescriptor.GetId();
110
111 auto subFieldInfo = CollectFieldTreeInfo(subFieldId);
112
113 compressedSize += subFieldInfo.GetCompressedSize();
114 uncompressedSize += subFieldInfo.GetUncompressedSize();
115 }
116
117 auto fieldInfo = RFieldTreeInspector(fDescriptor->GetFieldDescriptor(fieldId), compressedSize, uncompressedSize);
118 fFieldTreeInfo.emplace(fieldId, fieldInfo);
119 return fieldInfo;
120}
121
122std::vector<ROOT::Experimental::DescriptorId_t>
124{
125 std::vector<DescriptorId_t> colIds;
126 std::deque<DescriptorId_t> fieldIdQueue{fieldId};
127
128 while (!fieldIdQueue.empty()) {
129 auto currId = fieldIdQueue.front();
130 fieldIdQueue.pop_front();
131
132 for (const auto &col : fDescriptor->GetColumnIterable(currId)) {
133 if (col.IsAliasColumn()) {
134 continue;
135 }
136
137 colIds.emplace_back(col.GetPhysicalId());
138 }
139
140 for (const auto &fld : fDescriptor->GetFieldIterable(currId)) {
141 fieldIdQueue.push_back(fld.GetId());
142 }
143 }
144
145 return colIds;
146}
147
148std::unique_ptr<ROOT::Experimental::RNTupleInspector>
150{
151 if (!sourceNTuple) {
152 throw RException(R__FAIL("provided RNTuple is null"));
153 }
154
155 auto pageSource = Internal::RPageSourceFile::CreateFromAnchor(*sourceNTuple);
156 return std::unique_ptr<RNTupleInspector>(new RNTupleInspector(std::move(pageSource)));
157}
158
159std::unique_ptr<ROOT::Experimental::RNTupleInspector>
160ROOT::Experimental::RNTupleInspector::Create(std::string_view ntupleName, std::string_view sourceFileName)
161{
162 auto pageSource = ROOT::Experimental::Internal::RPageSource::Create(ntupleName, sourceFileName);
163 return std::unique_ptr<RNTupleInspector>(new RNTupleInspector(std::move(pageSource)));
164}
165
167{
168 int algorithm = fCompressionSettings / 100;
169 int level = fCompressionSettings - (algorithm * 100);
170
172 " (level " + std::to_string(level) + ")";
173}
174
175//------------------------------------------------------------------------------
176
179{
180 if (physicalColumnId > fDescriptor->GetNPhysicalColumns()) {
181 throw RException(R__FAIL("No column with physical ID " + std::to_string(physicalColumnId) + " present"));
182 }
183
184 return fColumnInfo.at(physicalColumnId);
185}
186
188{
189 size_t typeCount = 0;
190
191 for (auto &[colId, colInfo] : fColumnInfo) {
192 if (colInfo.GetType() == colType) {
193 ++typeCount;
194 }
195 }
196
197 return typeCount;
198}
199
200const std::vector<ROOT::Experimental::DescriptorId_t>
202{
203 std::vector<DescriptorId_t> colIds;
204
205 for (const auto &[colId, colInfo] : fColumnInfo) {
206 if (colInfo.GetType() == colType)
207 colIds.emplace_back(colId);
208 }
209
210 return colIds;
211}
212
213const std::vector<ROOT::Experimental::EColumnType> ROOT::Experimental::RNTupleInspector::GetColumnTypes()
214{
215 std::set<EColumnType> colTypes;
216
217 for (const auto &[colId, colInfo] : fColumnInfo) {
218 colTypes.emplace(colInfo.GetType());
219 }
220
221 return std::vector(colTypes.begin(), colTypes.end());
222}
223
225{
226 struct ColumnTypeInfo {
227 std::uint32_t count;
228 std::uint64_t nElems, compressedSize, uncompressedSize;
229
230 void operator+=(const RColumnInspector &colInfo)
231 {
232 this->count++;
233 this->nElems += colInfo.GetNElements();
234 this->compressedSize += colInfo.GetCompressedSize();
235 this->uncompressedSize += colInfo.GetUncompressedSize();
236 }
237 };
238
239 std::map<EColumnType, ColumnTypeInfo> colTypeInfo;
240
241 for (const auto &[colId, colInfo] : fColumnInfo) {
242 colTypeInfo[colInfo.GetType()] += colInfo;
243 }
244
245 switch (format) {
247 output << " column type | count | # elements | compressed bytes | uncompressed bytes\n"
248 << "----------------|---------|-----------------|-------------------|--------------------" << std::endl;
249 for (const auto &[colType, typeInfo] : colTypeInfo) {
250 output << std::setw(15) << Internal::RColumnElementBase::GetTypeName(colType) << " |" << std::setw(8)
251 << typeInfo.count << " |" << std::setw(16) << typeInfo.nElems << " |" << std::setw(18)
252 << typeInfo.compressedSize << " |" << std::setw(18) << typeInfo.uncompressedSize << " " << std::endl;
253 }
254 break;
256 output << "columnType,count,nElements,compressedSize,uncompressedSize" << std::endl;
257 for (const auto &[colType, typeInfo] : colTypeInfo) {
258 output << Internal::RColumnElementBase::GetTypeName(colType) << "," << typeInfo.count << "," << typeInfo.nElems
259 << "," << typeInfo.compressedSize << "," << typeInfo.uncompressedSize << std::endl;
260 }
261 break;
262 default: throw RException(R__FAIL("Invalid print format"));
263 }
264}
265
266std::unique_ptr<TH1D>
268 std::string_view histName, std::string_view histTitle)
269{
270 if (histName.empty()) {
271 switch (histKind) {
272 case ENTupleInspectorHist::kCount: histName = "colTypeCountHist"; break;
273 case ENTupleInspectorHist::kNElems: histName = "colTypeElemCountHist"; break;
274 case ENTupleInspectorHist::kCompressedSize: histName = "colTypeCompSizeHist"; break;
275 case ENTupleInspectorHist::kUncompressedSize: histName = "colTypeUncompSizeHist"; break;
276 default: throw RException(R__FAIL("Unknown histogram type"));
277 }
278 }
279
280 if (histTitle.empty()) {
281 switch (histKind) {
282 case ENTupleInspectorHist::kCount: histTitle = "Column count by type"; break;
283 case ENTupleInspectorHist::kNElems: histTitle = "Number of elements by column type"; break;
284 case ENTupleInspectorHist::kCompressedSize: histTitle = "Compressed size by column type"; break;
285 case ENTupleInspectorHist::kUncompressedSize: histTitle = "Uncompressed size by column type"; break;
286 default: throw RException(R__FAIL("Unknown histogram type"));
287 }
288 }
289
290 auto hist = std::make_unique<TH1D>(std::string(histName).c_str(), std::string(histTitle).c_str(), 1, 0, 1);
291
292 double data;
293 for (const auto &[colId, colInfo] : fColumnInfo) {
294 switch (histKind) {
295 case ENTupleInspectorHist::kCount: data = 1.; break;
296 case ENTupleInspectorHist::kNElems: data = colInfo.GetNElements(); break;
297 case ENTupleInspectorHist::kCompressedSize: data = colInfo.GetCompressedSize(); break;
298 case ENTupleInspectorHist::kUncompressedSize: data = colInfo.GetUncompressedSize(); break;
299 default: throw RException(R__FAIL("Unknown histogram type"));
300 }
301
302 hist->AddBinContent(
303 hist->GetXaxis()->FindBin(Internal::RColumnElementBase::GetTypeName(colInfo.GetType()).c_str()), data);
304 }
305
306 return hist;
307}
308
310 std::string histName,
311 std::string histTitle, size_t nBins)
312{
313 if (histTitle.empty())
314 histTitle = "Page size distribution for column with ID " + std::to_string(physicalColumnId);
315
316 return GetPageSizeDistribution({physicalColumnId}, histName, histTitle, nBins);
317}
318
319std::unique_ptr<TH1D>
321 std::string histName, std::string histTitle, size_t nBins)
322{
323 if (histName.empty())
324 histName = "pageSizeHistCol" + Internal::RColumnElementBase::GetTypeName(colType);
325 if (histTitle.empty())
326 histTitle = "Page size distribution for columns with type " + Internal::RColumnElementBase::GetTypeName(colType);
327
328 auto perTypeHist = GetPageSizeDistribution({colType}, histName, histTitle, nBins);
329
330 if (perTypeHist->GetNhists() < 1)
331 return std::make_unique<TH1D>(histName.c_str(), histTitle.c_str(), 64, 0, 0);
332
333 auto hist = std::unique_ptr<TH1D>(dynamic_cast<TH1D *>(perTypeHist->GetHists()->First()));
334
335 hist->SetName(histName.c_str());
336 hist->SetTitle(histTitle.c_str());
337 hist->SetXTitle("Page size (B)");
338 hist->SetYTitle("N_{pages}");
339 return hist;
340}
341
342std::unique_ptr<TH1D>
343ROOT::Experimental::RNTupleInspector::GetPageSizeDistribution(std::initializer_list<DescriptorId_t> colIds,
344 std::string histName, std::string histTitle, size_t nBins)
345{
346 auto hist = std::make_unique<TH1D>();
347
348 if (histName.empty())
349 histName = "pageSizeHist";
350 hist->SetName(histName.c_str());
351 if (histTitle.empty())
352 histTitle = "Page size distribution";
353 hist->SetTitle(histTitle.c_str());
354 hist->SetXTitle("Page size (B)");
355 hist->SetYTitle("N_{pages}");
356
357 std::vector<std::uint64_t> pageSizes;
358 std::for_each(colIds.begin(), colIds.end(), [this, &pageSizes](const auto colId) {
359 auto colInfo = GetColumnInspector(colId);
360 pageSizes.insert(pageSizes.end(), colInfo.GetCompressedPageSizes().begin(),
361 colInfo.GetCompressedPageSizes().end());
362 });
363
364 auto histMinMax = std::minmax_element(pageSizes.begin(), pageSizes.end());
365 hist->SetBins(nBins, *histMinMax.first,
366 *histMinMax.second + ((*histMinMax.second - *histMinMax.first) / static_cast<double>(nBins)));
367
368 for (const auto pageSize : pageSizes) {
369 hist->Fill(pageSize);
370 }
371
372 return hist;
373}
374
376 std::initializer_list<ROOT::Experimental::EColumnType> colTypes, std::string histName, std::string histTitle,
377 size_t nBins)
378{
379 if (histName.empty())
380 histName = "pageSizeHist";
381 if (histTitle.empty())
382 histTitle = "Per-column type page size distribution";
383
384 auto stackedHist = std::make_unique<THStack>(histName.c_str(), histTitle.c_str());
385
386 double histMin = std::numeric_limits<double>::max();
387 double histMax = 0;
388 std::map<EColumnType, std::vector<std::uint64_t>> pageSizes;
389
390 std::vector<EColumnType> colTypeVec = colTypes;
391 if (std::empty(colTypes)) {
392 colTypeVec = GetColumnTypes();
393 }
394
395 for (const auto colType : colTypeVec) {
396 auto colIds = GetColumnsByType(colType);
397
398 if (colIds.empty())
399 continue;
400
401 std::vector<std::uint64_t> pageSizesForColType;
402 std::for_each(colIds.cbegin(), colIds.cend(), [this, &pageSizesForColType](const auto colId) {
403 auto colInfo = GetColumnInspector(colId);
404 pageSizesForColType.insert(pageSizesForColType.end(), colInfo.GetCompressedPageSizes().begin(),
405 colInfo.GetCompressedPageSizes().end());
406 });
407 pageSizes.emplace(colType, pageSizesForColType);
408
409 auto histMinMax = std::minmax_element(pageSizesForColType.begin(), pageSizesForColType.end());
410 histMin = std::min(histMin, static_cast<double>(*histMinMax.first));
411 histMax = std::max(histMax, static_cast<double>(*histMinMax.second));
412 }
413
414 for (const auto &[colType, pageSizesForColType] : pageSizes) {
415 auto hist = std::make_unique<TH1D>(
416 TString::Format("%s%s", histName.c_str(), Internal::RColumnElementBase::GetTypeName(colType).c_str()),
417 Internal::RColumnElementBase::GetTypeName(colType).c_str(), nBins, histMin,
418 histMax + ((histMax - histMin) / static_cast<double>(nBins)));
419
420 for (const auto pageSize : pageSizesForColType) {
421 hist->Fill(pageSize);
422 }
423
424 stackedHist->Add(hist.release());
425 }
426
427 return stackedHist;
428}
429
430//------------------------------------------------------------------------------
431
434{
435 if (fieldId >= fDescriptor->GetNFields()) {
436 throw RException(R__FAIL("No field with ID " + std::to_string(fieldId) + " present"));
437 }
438
439 return fFieldTreeInfo.at(fieldId);
440}
441
444{
445 DescriptorId_t fieldId = fDescriptor->FindFieldId(fieldName);
446
447 if (fieldId == kInvalidDescriptorId) {
448 throw RException(R__FAIL("Could not find field `" + std::string(fieldName) + "`"));
449 }
450
451 return GetFieldTreeInspector(fieldId);
452}
453
454size_t ROOT::Experimental::RNTupleInspector::GetFieldCountByType(const std::regex &typeNamePattern,
455 bool includeSubFields) const
456{
457 size_t typeCount = 0;
458
459 for (auto &[fldId, fldInfo] : fFieldTreeInfo) {
460 if (!includeSubFields && fldInfo.GetDescriptor().GetParentId() != fDescriptor->GetFieldZeroId()) {
461 continue;
462 }
463
464 if (std::regex_match(fldInfo.GetDescriptor().GetTypeName(), typeNamePattern)) {
465 typeCount++;
466 }
467 }
468
469 return typeCount;
470}
471
472const std::vector<ROOT::Experimental::DescriptorId_t>
473ROOT::Experimental::RNTupleInspector::GetFieldsByName(const std::regex &fieldNamePattern, bool searchInSubFields) const
474{
475 std::vector<DescriptorId_t> fieldIds;
476
477 for (auto &[fldId, fldInfo] : fFieldTreeInfo) {
478
479 if (!searchInSubFields && fldInfo.GetDescriptor().GetParentId() != fDescriptor->GetFieldZeroId()) {
480 continue;
481 }
482
483 if (std::regex_match(fldInfo.GetDescriptor().GetFieldName(), fieldNamePattern)) {
484 fieldIds.emplace_back(fldId);
485 }
486 }
487
488 return fieldIds;
489}
#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:290
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:486
The available trivial, native content types of a column.
static std::string GetTypeName(EColumnType type)
static std::unique_ptr< RColumnElementBase > Generate(EColumnType type)
If CppT == void, use the default C++ type for the given column type.
static std::unique_ptr< RPageSourceFile > CreateFromAnchor(const RNTuple &anchor, const RNTupleReadOptions &options=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 RNTupleReadOptions &options=RNTupleReadOptions())
Guess the concrete derived page source from the file name (location)
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
Provides column-level storage information.
Inspect on-disk and storage-related information of an RNTuple.
const RColumnInspector & GetColumnInspector(DescriptorId_t physicalColumnId) const
Get storage information for a given column.
const std::vector< DescriptorId_t > GetFieldsByName(const std::regex &fieldNamePattern, bool searchInSubFields=true) const
Get the IDs of (sub-)fields whose name matches the given string.
std::unique_ptr< Internal::RPageSource > fPageSource
const std::vector< EColumnType > GetColumnTypes()
Get all column types present in the RNTuple being inspected.
size_t GetColumnCountByType(EColumnType colType) const
Get the number of columns of a given type present in the RNTuple.
RNTupleInspector(std::unique_ptr< Internal::RPageSource > pageSource)
std::string GetCompressionSettingsAsString() const
Get a string describing compression settings of the RNTuple being inspected.
static std::unique_ptr< RNTupleInspector > Create(RNTuple *sourceNTuple)
Create a new RNTupleInspector.
std::unique_ptr< TH1D > GetPageSizeDistribution(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.
const std::vector< DescriptorId_t > GetColumnsByType(EColumnType colType)
Get the IDs of all columns with the given type.
void PrintColumnTypeInfo(ENTupleInspectorPrintFormat format=ENTupleInspectorPrintFormat::kTable, std::ostream &output=std::cout)
Print storage information per column type.
RFieldTreeInspector CollectFieldTreeInfo(DescriptorId_t fieldId)
Recursively gather field-level information.
std::unique_ptr< RNTupleDescriptor > fDescriptor
std::vector< DescriptorId_t > GetColumnsByFieldId(DescriptorId_t fieldId) const
Get the columns that make up the given field, including its subfields.
void CollectColumnInfo()
Gather column-level and RNTuple-level information.
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.
const RFieldTreeInspector & GetFieldTreeInspector(DescriptorId_t fieldId) const
Get storage information for a given (sub)field by ID.
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,.
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:61
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:670
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:2378
constexpr int kUnknownCompressionSettings
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)
static void output()