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 fSource->Attach();
56 fMetrics.ObserveMetrics(fSource->GetMetrics());
57}
58
59ROOT::Experimental::RNTupleReader::RNTupleReader(std::unique_ptr<ROOT::Experimental::RNTupleModel> model,
60 std::unique_ptr<ROOT::Experimental::Internal::RPageSource> source)
61 : fSource(std::move(source)), fModel(std::move(model)), fMetrics("RNTupleReader")
62{
63 // TODO(jblomer): properly support projected fields
64 if (!fModel->GetProjectedFields().IsEmpty()) {
65 throw RException(R__FAIL("model has projected fields, which is incompatible with providing a read model"));
66 }
67 fModel->Freeze();
70}
71
72ROOT::Experimental::RNTupleReader::RNTupleReader(std::unique_ptr<ROOT::Experimental::Internal::RPageSource> source)
73 : fSource(std::move(source)), fModel(nullptr), fMetrics("RNTupleReader")
74{
76}
77
79
80std::unique_ptr<ROOT::Experimental::RNTupleReader>
81ROOT::Experimental::RNTupleReader::Open(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName,
82 std::string_view storage, const RNTupleReadOptions &options)
83{
84 return std::unique_ptr<RNTupleReader>(
85 new RNTupleReader(std::move(model), Internal::RPageSource::Create(ntupleName, storage, options)));
86}
87
88std::unique_ptr<ROOT::Experimental::RNTupleReader>
89ROOT::Experimental::RNTupleReader::Open(std::string_view ntupleName, std::string_view storage,
90 const RNTupleReadOptions &options)
91{
92 return std::unique_ptr<RNTupleReader>(
93 new RNTupleReader(Internal::RPageSource::Create(ntupleName, storage, options)));
94}
95
96std::unique_ptr<ROOT::Experimental::RNTupleReader>
98{
99 return std::unique_ptr<RNTupleReader>(
101}
102
103std::unique_ptr<ROOT::Experimental::RNTupleReader>
104ROOT::Experimental::RNTupleReader::Open(std::unique_ptr<RNTupleModel> model, ROOT::Experimental::RNTuple *ntuple,
105 const RNTupleReadOptions &options)
106{
107 return std::unique_ptr<RNTupleReader>(
108 new RNTupleReader(std::move(model), Internal::RPageSourceFile::CreateFromAnchor(*ntuple, options)));
109}
110
111std::unique_ptr<ROOT::Experimental::RNTupleReader>
113{
114 std::vector<std::unique_ptr<Internal::RPageSource>> sources;
115 for (const auto &n : ntuples) {
116 sources.emplace_back(Internal::RPageSource::Create(n.fNTupleName, n.fStorage, n.fOptions));
117 }
118 return std::unique_ptr<RNTupleReader>(
119 new RNTupleReader(std::make_unique<Internal::RPageSourceFriends>("_friends", sources)));
120}
121
123{
124 if (!fModel) {
125 fModel = fSource->GetSharedDescriptorGuard()->CreateModel();
126 ConnectModel(*fModel);
127 }
128 return *fModel;
129}
130
132{
133 // TODO(lesimon): In a later version, these variables may be defined by the user or the ideal width may be read out
134 // from the terminal.
135 char frameSymbol = '*';
136 int width = 80;
137 /*
138 if (width < 30) {
139 output << "The width is too small! Should be at least 30." << std::endl;
140 return;
141 }
142 */
143 switch (what) {
145 std::string name;
146 std::unique_ptr<RNTupleModel> fullModel;
147 {
148 auto descriptorGuard = fSource->GetSharedDescriptorGuard();
149 name = descriptorGuard->GetName();
150 fullModel = descriptorGuard->CreateModel();
151 }
152
153 for (int i = 0; i < (width / 2 + width % 2 - 4); ++i)
154 output << frameSymbol;
155 output << " NTUPLE ";
156 for (int i = 0; i < (width / 2 - 4); ++i)
157 output << frameSymbol;
158 output << std::endl;
159 // FitString defined in RFieldVisitor.cxx
160 output << frameSymbol << " N-Tuple : " << RNTupleFormatter::FitString(name, width - 13) << frameSymbol
161 << std::endl; // prints line with name of ntuple
162 output << frameSymbol << " Entries : " << RNTupleFormatter::FitString(std::to_string(GetNEntries()), width - 13)
163 << frameSymbol << std::endl; // prints line with number of entries
164
165 // Traverses through all fields to gather information needed for printing.
166 RPrepareVisitor prepVisitor;
167 // Traverses through all fields to do the actual printing.
168 RPrintSchemaVisitor printVisitor(output);
169
170 // Note that we do not need to connect the model, we are only looking at its tree of fields
171 fullModel->GetFieldZero().AcceptVisitor(prepVisitor);
172
173 printVisitor.SetFrameSymbol(frameSymbol);
174 printVisitor.SetWidth(width);
175 printVisitor.SetDeepestLevel(prepVisitor.GetDeepestLevel());
176 printVisitor.SetNumFields(prepVisitor.GetNumFields());
177
178 for (int i = 0; i < width; ++i)
179 output << frameSymbol;
180 output << std::endl;
181 fullModel->GetFieldZero().AcceptVisitor(printVisitor);
182 for (int i = 0; i < width; ++i)
183 output << frameSymbol;
184 output << std::endl;
185 break;
186 }
187 case ENTupleInfo::kStorageDetails: fSource->GetSharedDescriptorGuard()->PrintInfo(output); break;
188 case ENTupleInfo::kMetrics: fMetrics.Print(output); break;
189 default:
190 // Unhandled case, internal error
191 R__ASSERT(false);
192 }
193}
194
196{
197 if (!fDisplayReader)
198 fDisplayReader = Clone();
199 return fDisplayReader.get();
200}
201
203{
204 auto reader = GetDisplayReader();
205 const auto &entry = reader->GetModel().GetDefaultEntry();
206
207 reader->LoadEntry(index);
208 output << "{";
209 for (auto iValue = entry.begin(); iValue != entry.end();) {
210 output << std::endl;
211 RPrintValueVisitor visitor(*iValue, output, 1 /* level */);
212 iValue->GetField().AcceptVisitor(visitor);
213
214 if (++iValue == entry.end()) {
215 output << std::endl;
216 break;
217 } else {
218 output << ",";
219 }
220 }
221 output << "}" << std::endl;
222}
223
225{
226 auto descriptorGuard = fSource->GetSharedDescriptorGuard();
227 if (!fCachedDescriptor || fCachedDescriptor->GetGeneration() != descriptorGuard->GetGeneration())
228 fCachedDescriptor = descriptorGuard->Clone();
229 return *fCachedDescriptor;
230}
231
233{
234 auto fieldId = fSource->GetSharedDescriptorGuard()->FindFieldId(fieldName);
235 if (fieldId == kInvalidDescriptorId) {
236 throw RException(R__FAIL("no field named '" + std::string(fieldName) + "' in RNTuple '" +
237 fSource->GetSharedDescriptorGuard()->GetName() + "'"));
238 }
239 return fieldId;
240}
#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)
Definition TError.h:118
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:945
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 set the on-disk f...
Common user-tunable settings for reading ntuples.
An RNTuple that is used to read data from storage.
static std::unique_ptr< RNTupleReader > OpenFriends(std::span< ROpenSpec > ntuples)
Open RNTuples as one virtual, horizontally combined ntuple.
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 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 PrintInfo(const ENTupleInfo what=ENTupleInfo::kSummary, std::ostream &output=std::cout)
Prints a detailed summary of the ntuple, including a list of fields.
std::unique_ptr< RNTupleModel > fModel
Needs to be destructed before fSource.
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:388
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()