Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleReader.cxx
Go to the documentation of this file.
1/// \file RNTupleReader.cxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2024-02-20
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-2024, 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
18#include <ROOT/RField.hxx>
21#include <ROOT/RNTuple.hxx>
22#include <ROOT/RNTupleModel.hxx>
25
26#include <TROOT.h>
27
29{
30 auto &fieldZero = model.GetFieldZero();
31 // We must not use the descriptor guard to prevent recursive locking in field.ConnectPageSource
32 DescriptorId_t fieldZeroId = fSource->GetSharedDescriptorGuard()->GetFieldZeroId();
33 fieldZero.SetOnDiskId(fieldZeroId);
34 // Iterate only over fieldZero's direct subfields; their descendants are recursively handled in
35 // RFieldBase::ConnectPageSource
36 for (auto &field : fieldZero.GetSubFields()) {
37 // If the model has been created from the descriptor, the on-disk IDs are already set.
38 // User-provided models instead need to find their corresponding IDs in the descriptor.
39 if (field->GetOnDiskId() == kInvalidDescriptorId) {
40 field->SetOnDiskId(fSource->GetSharedDescriptorGuard()->FindFieldId(field->GetFieldName(), fieldZeroId));
41 }
43 }
44}
45
47{
48#ifdef R__USE_IMT
49 if (IsImplicitMTEnabled() &&
50 fSource->GetReadOptions().GetUseImplicitMT() == RNTupleReadOptions::EImplicitMT::kDefault) {
51 fUnzipTasks = std::make_unique<Internal::RNTupleImtTaskScheduler>();
52 fSource->SetTaskScheduler(fUnzipTasks.get());
53 }
54#endif
55 fMetrics.ObserveMetrics(fSource->GetMetrics());
56 if (enableMetrics)
57 EnableMetrics();
58 fSource->Attach();
59}
60
61ROOT::Experimental::RNTupleReader::RNTupleReader(std::unique_ptr<ROOT::Experimental::RNTupleModel> model,
62 std::unique_ptr<ROOT::Experimental::Internal::RPageSource> source,
63 const RNTupleReadOptions &options)
64 : fSource(std::move(source)), fModel(std::move(model)), fMetrics("RNTupleReader")
65{
66 // TODO(jblomer): properly support projected fields
67 if (!fModel->GetProjectedFields().IsEmpty()) {
68 throw RException(R__FAIL("model has projected fields, which is incompatible with providing a read model"));
69 }
70 fModel->Freeze();
73}
74
75ROOT::Experimental::RNTupleReader::RNTupleReader(std::unique_ptr<ROOT::Experimental::Internal::RPageSource> source,
76 const RNTupleReadOptions &options)
77 : fSource(std::move(source)), fModel(nullptr), fMetrics("RNTupleReader")
78{
80}
81
83
84std::unique_ptr<ROOT::Experimental::RNTupleReader>
85ROOT::Experimental::RNTupleReader::Open(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName,
86 std::string_view storage, const RNTupleReadOptions &options)
87{
88 return std::unique_ptr<RNTupleReader>(
89 new RNTupleReader(std::move(model), Internal::RPageSource::Create(ntupleName, storage, options), options));
90}
91
92std::unique_ptr<ROOT::Experimental::RNTupleReader>
93ROOT::Experimental::RNTupleReader::Open(std::string_view ntupleName, std::string_view storage,
94 const RNTupleReadOptions &options)
95{
96 return std::unique_ptr<RNTupleReader>(
97 new RNTupleReader(Internal::RPageSource::Create(ntupleName, storage, options), options));
98}
99
100std::unique_ptr<ROOT::Experimental::RNTupleReader>
102{
103 return std::unique_ptr<RNTupleReader>(
104 new RNTupleReader(Internal::RPageSourceFile::CreateFromAnchor(ntuple, options), options));
105}
106
107std::unique_ptr<ROOT::Experimental::RNTupleReader>
108ROOT::Experimental::RNTupleReader::Open(std::unique_ptr<RNTupleModel> model, const ROOT::Experimental::RNTuple &ntuple,
109 const RNTupleReadOptions &options)
110{
111 return std::unique_ptr<RNTupleReader>(
112 new RNTupleReader(std::move(model), Internal::RPageSourceFile::CreateFromAnchor(ntuple, options), options));
113}
114
115std::unique_ptr<ROOT::Experimental::RNTupleReader>
116ROOT::Experimental::RNTupleReader::OpenFriends(std::span<ROpenSpec> ntuples, const RNTupleReadOptions &options)
117{
118 std::vector<std::unique_ptr<Internal::RPageSource>> sources;
119 sources.reserve(ntuples.size());
120 for (const auto &n : ntuples) {
121 sources.emplace_back(Internal::RPageSource::Create(n.fNTupleName, n.fStorage, n.fOptions));
122 }
123 return std::unique_ptr<RNTupleReader>(
124 new RNTupleReader(std::make_unique<Internal::RPageSourceFriends>("_friends", sources), options));
125}
126
128{
129 if (!fModel) {
130 fModel = fSource->GetSharedDescriptorGuard()->CreateModel();
131 ConnectModel(*fModel);
132 }
133 return *fModel;
134}
135
137{
138 // TODO(lesimon): In a later version, these variables may be defined by the user or the ideal width may be read out
139 // from the terminal.
140 char frameSymbol = '*';
141 int width = 80;
142 /*
143 if (width < 30) {
144 output << "The width is too small! Should be at least 30." << std::endl;
145 return;
146 }
147 */
148 switch (what) {
150 std::string name;
151 std::unique_ptr<RNTupleModel> fullModel;
152 {
153 auto descriptorGuard = fSource->GetSharedDescriptorGuard();
154 name = descriptorGuard->GetName();
155 fullModel = descriptorGuard->CreateModel();
156 }
157
158 for (int i = 0; i < (width / 2 + width % 2 - 4); ++i)
159 output << frameSymbol;
160 output << " NTUPLE ";
161 for (int i = 0; i < (width / 2 - 4); ++i)
162 output << frameSymbol;
163 output << "\n";
164 // FitString defined in RFieldVisitor.cxx
165 output << frameSymbol << " N-Tuple : " << RNTupleFormatter::FitString(name, width - 13) << frameSymbol
166 << "\n"; // prints line with name of ntuple
167 output << frameSymbol << " Entries : " << RNTupleFormatter::FitString(std::to_string(GetNEntries()), width - 13)
168 << frameSymbol << "\n"; // prints line with number of entries
169
170 // Traverses through all fields to gather information needed for printing.
171 RPrepareVisitor prepVisitor;
172 // Traverses through all fields to do the actual printing.
173 RPrintSchemaVisitor printVisitor(output);
174
175 // Note that we do not need to connect the model, we are only looking at its tree of fields
176 fullModel->GetFieldZero().AcceptVisitor(prepVisitor);
177
178 printVisitor.SetFrameSymbol(frameSymbol);
179 printVisitor.SetWidth(width);
180 printVisitor.SetDeepestLevel(prepVisitor.GetDeepestLevel());
181 printVisitor.SetNumFields(prepVisitor.GetNumFields());
182
183 for (int i = 0; i < width; ++i)
184 output << frameSymbol;
185 output << "\n";
186 fullModel->GetFieldZero().AcceptVisitor(printVisitor);
187 for (int i = 0; i < width; ++i)
188 output << frameSymbol;
189 output << std::endl;
190 break;
191 }
192 case ENTupleInfo::kStorageDetails: fSource->GetSharedDescriptorGuard()->PrintInfo(output); break;
193 case ENTupleInfo::kMetrics: fMetrics.Print(output); break;
194 default:
195 // Unhandled case, internal error
196 R__ASSERT(false);
197 }
198}
199
201{
202 if (!fDisplayReader)
203 fDisplayReader = Clone();
204 return fDisplayReader.get();
205}
206
208{
209 auto reader = GetDisplayReader();
210 const auto &entry = reader->GetModel().GetDefaultEntry();
211
212 reader->LoadEntry(index);
213 output << "{";
214 for (auto iValue = entry.begin(); iValue != entry.end();) {
215 output << std::endl;
216 RPrintValueVisitor visitor(*iValue, output, 1 /* level */);
217 iValue->GetField().AcceptVisitor(visitor);
218
219 if (++iValue == entry.end()) {
220 output << std::endl;
221 break;
222 } else {
223 output << ",";
224 }
225 }
226 output << "}" << std::endl;
227}
228
230{
231 auto descriptorGuard = fSource->GetSharedDescriptorGuard();
232 if (!fCachedDescriptor || fCachedDescriptor->GetGeneration() != descriptorGuard->GetGeneration())
233 fCachedDescriptor = descriptorGuard->Clone();
234 return *fCachedDescriptor;
235}
236
238{
239 auto fieldId = fSource->GetSharedDescriptorGuard()->FindFieldId(fieldName);
240 if (fieldId == kInvalidDescriptorId) {
241 throw RException(R__FAIL("no field named '" + std::string(fieldName) + "' in RNTuple '" +
242 fSource->GetSharedDescriptorGuard()->GetName() + "'"));
243 }
244 return fieldId;
245}
#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
#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::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
void SetOnDiskId(DescriptorId_t id)
Definition RField.cxx:1071
The on-storage meta-data of an ntuple.
std::unique_ptr< RNTupleDescriptor > Clone() const
static std::string FitString(const std::string &str, int availableSpace)
The RNTupleModel encapulates the schema of an ntuple.
RFieldZero & GetFieldZero()
Non-const access to the root field is used to commit clusters during writing, and to make adjustments...
Common user-tunable settings for reading ntuples.
An RNTuple that is used to read data from storage.
DescriptorId_t RetrieveFieldId(std::string_view fieldName) const
void Show(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(std::unique_ptr< RNTupleModel > model, std::unique_ptr< Internal::RPageSource > source, const RNTupleReadOptions &options)
const RNTupleDescriptor & GetDescriptor()
Returns a cached copy of the page source descriptor.
std::unique_ptr< Internal::RPageSource > fSource
static std::unique_ptr< RNTupleReader > Open(std::string_view ntupleName, std::string_view storage, const RNTupleReadOptions &options=RNTupleReadOptions())
Open an RNTuple for reading.
void InitPageSource(bool enableMetrics)
void PrintInfo(const ENTupleInfo what=ENTupleInfo::kSummary, std::ostream &output=std::cout) const
Prints a detailed summary of the ntuple, including a list of fields.
std::unique_ptr< RNTupleModel > fModel
Needs to be destructed before fSource.
static std::unique_ptr< RNTupleReader > OpenFriends(std::span< ROpenSpec > ntuples, const RNTupleReadOptions &options=RNTupleReadOptions())
Open RNTuples as one virtual, horizontally combined ntuple.
void ConnectModel(RNTupleModel &model)
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:61
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.
const Int_t n
Definition legend1.C:16
void CallConnectPageSourceOnField(RFieldBase &, RPageSource &)
Definition RField.cxx:424
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.
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
constexpr DescriptorId_t kInvalidDescriptorId
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:570
static const char * what
Definition stlLoader.cc:5
static void output()