Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleReader.cxx
Go to the documentation of this file.
1/// \file RNTupleReader.cxx
2/// \ingroup NTuple
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2024-02-20
5
6/*************************************************************************
7 * Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. *
8 * All rights reserved. *
9 * *
10 * For the licensing terms see $ROOTSYS/LICENSE. *
11 * For the list of contributors see $ROOTSYS/README/CREDITS. *
12 *************************************************************************/
13
15
16#include <ROOT/RField.hxx>
19#include <ROOT/RNTuple.hxx>
20#include <ROOT/RNTupleModel.hxx>
22
23#include <TROOT.h>
24
26{
28 // We must not use the descriptor guard to prevent recursive locking in field.ConnectPageSource
29 ROOT::DescriptorId_t fieldZeroId = fSource->GetSharedDescriptorGuard()->GetFieldZeroId();
30 fieldZero.SetOnDiskId(fieldZeroId);
31 // Iterate only over fieldZero's direct subfields; their descendants are recursively handled in
32 // RFieldBase::ConnectPageSource
33 for (auto &field : fieldZero.GetMutableSubfields()) {
34 // If the model has been created from the descriptor, the on-disk IDs are already set.
35 // User-provided models instead need to find their corresponding IDs in the descriptor.
36 if (field->GetOnDiskId() == ROOT::kInvalidDescriptorId) {
37 field->SetOnDiskId(fSource->GetSharedDescriptorGuard()->FindFieldId(field->GetFieldName(), fieldZeroId));
38 }
40 }
41}
42
44{
45#ifdef R__USE_IMT
46 if (IsImplicitMTEnabled() &&
47 fSource->GetReadOptions().GetUseImplicitMT() == ROOT::RNTupleReadOptions::EImplicitMT::kDefault) {
48 fUnzipTasks = std::make_unique<Experimental::Internal::RNTupleImtTaskScheduler>();
49 fSource->SetTaskScheduler(fUnzipTasks.get());
50 }
51#endif
52 fMetrics.ObserveMetrics(fSource->GetMetrics());
53 if (enableMetrics)
54 EnableMetrics();
55 fSource->Attach();
56 fNEntries = fSource->GetNEntries();
57}
58
59ROOT::RNTupleReader::RNTupleReader(std::unique_ptr<ROOT::RNTupleModel> model,
60 std::unique_ptr<ROOT::Internal::RPageSource> source,
61 const ROOT::RNTupleReadOptions &options)
62 : fSource(std::move(source)), fModel(std::move(model)), fMetrics("RNTupleReader")
63{
64 // TODO(jblomer): properly support projected fields
66 if (!projectedFields.IsEmpty()) {
67 throw RException(R__FAIL("model has projected fields, which is incompatible with providing a read model"));
68 }
69 fModel->Freeze();
72}
73
74ROOT::RNTupleReader::RNTupleReader(std::unique_ptr<ROOT::Internal::RPageSource> source,
75 const ROOT::RNTupleReadOptions &options)
76 : fSource(std::move(source)), fModel(nullptr), fMetrics("RNTupleReader")
77{
79}
80
82
83std::unique_ptr<ROOT::RNTupleReader> ROOT::RNTupleReader::Open(std::unique_ptr<ROOT::RNTupleModel> model,
84 std::string_view ntupleName, std::string_view storage,
85 const ROOT::RNTupleReadOptions &options)
86{
87 return std::unique_ptr<RNTupleReader>(
88 new RNTupleReader(std::move(model), Internal::RPageSource::Create(ntupleName, storage, options), options));
89}
90
91std::unique_ptr<ROOT::RNTupleReader> ROOT::RNTupleReader::Open(std::string_view ntupleName, std::string_view storage,
92 const ROOT::RNTupleReadOptions &options)
93{
94 return std::unique_ptr<RNTupleReader>(
96}
97
98std::unique_ptr<ROOT::RNTupleReader>
100{
101 return std::unique_ptr<RNTupleReader>(
103}
104
105std::unique_ptr<ROOT::RNTupleReader> ROOT::RNTupleReader::Open(std::unique_ptr<ROOT::RNTupleModel> model,
106 const ROOT::RNTuple &ntuple,
107 const ROOT::RNTupleReadOptions &options)
108{
109 return std::unique_ptr<RNTupleReader>(
110 new RNTupleReader(std::move(model), Internal::RPageSourceFile::CreateFromAnchor(ntuple, options), options));
111}
112
113std::unique_ptr<ROOT::RNTupleReader>
115 std::string_view ntupleName, std::string_view storage,
116 const ROOT::RNTupleReadOptions &options)
117{
118 auto reader = std::unique_ptr<RNTupleReader>(
120 reader->fCreateModelOptions = createModelOpts;
121 return reader;
122}
123
124std::unique_ptr<ROOT::RNTupleReader>
126 const ROOT::RNTuple &ntuple, const ROOT::RNTupleReadOptions &options)
127{
128 auto reader = std::unique_ptr<RNTupleReader>(
130 reader->fCreateModelOptions = createModelOpts;
131 return reader;
132}
133
135{
136 if (!fModel) {
137 fModel = fSource->GetSharedDescriptorGuard()->CreateModel(
138 fCreateModelOptions.value_or(ROOT::RNTupleDescriptor::RCreateModelOptions{}));
139 ConnectModel(*fModel);
140 }
141 return *fModel;
142}
143
144std::unique_ptr<ROOT::REntry> ROOT::RNTupleReader::CreateEntry()
145{
146 return GetModel().CreateEntry();
147}
148
150{
151 using namespace ROOT::Internal;
152
153 // TODO(lesimon): In a later version, these variables may be defined by the user or the ideal width may be read out
154 // from the terminal.
155 char frameSymbol = '*';
156 int width = 80;
157 /*
158 if (width < 30) {
159 output << "The width is too small! Should be at least 30." << std::endl;
160 return;
161 }
162 */
163 switch (what) {
165 std::string name;
166 std::unique_ptr<ROOT::RNTupleModel> fullModel;
167 {
168 auto descriptorGuard = fSource->GetSharedDescriptorGuard();
169 name = descriptorGuard->GetName();
171 opts.SetCreateBare(true);
172 // When printing the schema we always try to reconstruct the whole thing even when we are missing the
173 // dictionaries.
174 opts.SetEmulateUnknownTypes(true);
175 fullModel = descriptorGuard->CreateModel(opts);
176 }
177
178 for (int i = 0; i < (width / 2 + width % 2 - 4); ++i)
180 output << " NTUPLE ";
181 for (int i = 0; i < (width / 2 - 4); ++i)
183 output << "\n";
184 // FitString defined in RFieldVisitor.cxx
185 output << frameSymbol << " N-Tuple : " << RNTupleFormatter::FitString(name, width - 13) << frameSymbol
186 << "\n"; // prints line with name of ntuple
187 output << frameSymbol << " Entries : " << RNTupleFormatter::FitString(std::to_string(GetNEntries()), width - 13)
188 << frameSymbol << "\n"; // prints line with number of entries
189
190 // Traverses through all fields to gather information needed for printing.
192 // Traverses through all fields to do the actual printing.
194
195 // Note that we do not need to connect the model, we are only looking at its tree of fields
196 fullModel->GetConstFieldZero().AcceptVisitor(prepVisitor);
197
198 printVisitor.SetFrameSymbol(frameSymbol);
199 printVisitor.SetWidth(width);
200 printVisitor.SetDeepestLevel(prepVisitor.GetDeepestLevel());
201 printVisitor.SetNumFields(prepVisitor.GetNumFields());
202
203 for (int i = 0; i < width; ++i)
205 output << "\n";
206 fullModel->GetConstFieldZero().AcceptVisitor(printVisitor);
207 for (int i = 0; i < width; ++i)
209 output << std::endl;
210 break;
211 }
212 case ENTupleInfo::kStorageDetails: fSource->GetSharedDescriptorGuard()->PrintInfo(output); break;
213 case ENTupleInfo::kMetrics: fMetrics.Print(output); break;
214 default:
215 // Unhandled case, internal error
216 R__ASSERT(false);
217 }
218}
219
221{
222 if (!fDisplayReader) {
224 opts.SetEmulateUnknownTypes(true);
225 auto fullModel = fSource->GetSharedDescriptorGuard()->CreateModel(opts);
226 fDisplayReader = std::unique_ptr<RNTupleReader>(
227 new RNTupleReader(std::move(fullModel), fSource->Clone(), ROOT::RNTupleReadOptions{}));
228 }
229 return fDisplayReader.get();
230}
231
233{
234 auto reader = GetDisplayReader();
235 const auto &entry = reader->GetModel().GetDefaultEntry();
236
237 reader->LoadEntry(index);
238 output << "{";
239 for (auto iValue = entry.begin(); iValue != entry.end();) {
240 output << std::endl;
242 iValue->GetField().AcceptVisitor(visitor);
243
244 if (++iValue == entry.end()) {
245 output << std::endl;
246 break;
247 } else {
248 output << ",";
249 }
250 }
251 output << "}" << std::endl;
252}
253
255{
256 auto descriptorGuard = fSource->GetSharedDescriptorGuard();
257 if (!fCachedDescriptor || fCachedDescriptor->GetGeneration() != descriptorGuard->GetGeneration())
258 fCachedDescriptor = descriptorGuard->Clone();
259 return *fCachedDescriptor;
260}
261
263{
264 auto fieldId = fSource->GetSharedDescriptorGuard()->FindFieldId(fieldName);
266 throw RException(R__FAIL("no field named '" + std::string(fieldName) + "' in RNTuple '" +
267 fSource->GetSharedDescriptorGuard()->GetName() + "'"));
268 }
269 return fieldId;
270}
#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:300
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:125
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t width
char name[80]
Definition TGX11.cxx:110
static std::string FitString(const std::string &str, int availableSpace)
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)
Visitor used for a pre-processing run to collect information needed by another visitor class.
Contains settings for printing and prints a summary of an RField instance.
Renders a JSON value corresponding to the field.
Base class for all ROOT issued exceptions.
Definition RError.hxx:79
The on-storage metadata of an RNTuple.
The RNTupleModel encapulates the schema of an RNTuple.
Common user-tunable settings for reading RNTuples.
Reads RNTuple data from storage.
std::unique_ptr< ROOT::REntry > CreateEntry()
static std::unique_ptr< RNTupleReader > Open(std::string_view ntupleName, std::string_view storage, const ROOT::RNTupleReadOptions &options=ROOT::RNTupleReadOptions())
Open an RNTuple for reading.
void InitPageSource(bool enableMetrics)
std::unique_ptr< ROOT::RNTupleModel > fModel
Needs to be destructed before fSource.
const ROOT::RNTupleDescriptor & GetDescriptor()
Returns a cached copy of the page source descriptor.
void PrintInfo(const ENTupleInfo what=ENTupleInfo::kSummary, std::ostream &output=std::cout) const
Prints a detailed summary of the RNTuple, including a list of fields.
std::unique_ptr< Internal::RPageSource > fSource
RNTupleReader(std::unique_ptr< ROOT::RNTupleModel > model, std::unique_ptr< Internal::RPageSource > source, const ROOT::RNTupleReadOptions &options)
const ROOT::RNTupleModel & GetModel()
void ConnectModel(ROOT::RNTupleModel &model)
ROOT::DescriptorId_t RetrieveFieldId(std::string_view fieldName) const
void Show(ROOT::NTupleSize_t index, std::ostream &output=std::cout)
Shows the values of the i-th entry/row, starting with 0 for the first entry.
RNTupleReader * GetDisplayReader()
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:67
const_iterator begin() const
const_iterator end() const
ROOT::RFieldZero & GetFieldZeroOfModel(RNTupleModel &model)
void CallConnectPageSourceOnField(RFieldBase &, ROOT::Internal::RPageSource &)
RProjectedFields & GetProjectedFieldsOfModel(RNTupleModel &model)
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:600
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
ENTupleInfo
Listing of the different options that can be printed by RNTupleReader::GetInfo()
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
constexpr DescriptorId_t kInvalidDescriptorId
static const char * what
Definition stlLoader.cc:5
static void output()