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 = Internal::GetFieldZeroOfModel(model);
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 auto &projectedFields = Internal::GetProjectedFieldsOfModel(*fModel);
68 if (!projectedFields.IsEmpty()) {
69 throw RException(R__FAIL("model has projected fields, which is incompatible with providing a read model"));
70 }
71 fModel->Freeze();
74}
75
76ROOT::Experimental::RNTupleReader::RNTupleReader(std::unique_ptr<ROOT::Experimental::Internal::RPageSource> source,
77 const RNTupleReadOptions &options)
78 : fSource(std::move(source)), fModel(nullptr), fMetrics("RNTupleReader")
79{
81}
82
84
85std::unique_ptr<ROOT::Experimental::RNTupleReader>
86ROOT::Experimental::RNTupleReader::Open(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName,
87 std::string_view storage, const RNTupleReadOptions &options)
88{
89 return std::unique_ptr<RNTupleReader>(
90 new RNTupleReader(std::move(model), Internal::RPageSource::Create(ntupleName, storage, options), options));
91}
92
93std::unique_ptr<ROOT::Experimental::RNTupleReader>
94ROOT::Experimental::RNTupleReader::Open(std::string_view ntupleName, std::string_view storage,
95 const RNTupleReadOptions &options)
96{
97 return std::unique_ptr<RNTupleReader>(
98 new RNTupleReader(Internal::RPageSource::Create(ntupleName, storage, options), options));
99}
100
101std::unique_ptr<ROOT::Experimental::RNTupleReader>
103{
104 return std::unique_ptr<RNTupleReader>(
105 new RNTupleReader(Internal::RPageSourceFile::CreateFromAnchor(ntuple, options), options));
106}
107
108std::unique_ptr<ROOT::Experimental::RNTupleReader>
109ROOT::Experimental::RNTupleReader::Open(std::unique_ptr<RNTupleModel> model, const ROOT::RNTuple &ntuple,
110 const RNTupleReadOptions &options)
111{
112 return std::unique_ptr<RNTupleReader>(
113 new RNTupleReader(std::move(model), Internal::RPageSourceFile::CreateFromAnchor(ntuple, options), options));
114}
115
116std::unique_ptr<ROOT::Experimental::RNTupleReader>
117ROOT::Experimental::RNTupleReader::OpenFriends(std::span<RNTupleOpenSpec> ntuples, const RNTupleReadOptions &options)
118{
119 std::vector<std::unique_ptr<Internal::RPageSource>> sources;
120 sources.reserve(ntuples.size());
121 for (const auto &n : ntuples) {
122 sources.emplace_back(Internal::RPageSource::Create(n.fNTupleName, n.fStorage, n.fOptions));
123 }
124 return std::unique_ptr<RNTupleReader>(
125 new RNTupleReader(std::make_unique<Internal::RPageSourceFriends>("_friends", sources), options));
126}
127
129{
130 if (!fModel) {
131 fModel = fSource->GetSharedDescriptorGuard()->CreateModel();
132 ConnectModel(*fModel);
133 }
134 return *fModel;
135}
136
138{
139 // TODO(lesimon): In a later version, these variables may be defined by the user or the ideal width may be read out
140 // from the terminal.
141 char frameSymbol = '*';
142 int width = 80;
143 /*
144 if (width < 30) {
145 output << "The width is too small! Should be at least 30." << std::endl;
146 return;
147 }
148 */
149 switch (what) {
151 std::string name;
152 std::unique_ptr<RNTupleModel> fullModel;
153 {
154 auto descriptorGuard = fSource->GetSharedDescriptorGuard();
155 name = descriptorGuard->GetName();
156 fullModel = descriptorGuard->CreateModel();
157 }
158
159 for (int i = 0; i < (width / 2 + width % 2 - 4); ++i)
160 output << frameSymbol;
161 output << " NTUPLE ";
162 for (int i = 0; i < (width / 2 - 4); ++i)
163 output << frameSymbol;
164 output << "\n";
165 // FitString defined in RFieldVisitor.cxx
166 output << frameSymbol << " N-Tuple : " << RNTupleFormatter::FitString(name, width - 13) << frameSymbol
167 << "\n"; // prints line with name of ntuple
168 output << frameSymbol << " Entries : " << RNTupleFormatter::FitString(std::to_string(GetNEntries()), width - 13)
169 << frameSymbol << "\n"; // prints line with number of entries
170
171 // Traverses through all fields to gather information needed for printing.
172 RPrepareVisitor prepVisitor;
173 // Traverses through all fields to do the actual printing.
174 RPrintSchemaVisitor printVisitor(output);
175
176 // Note that we do not need to connect the model, we are only looking at its tree of fields
177 fullModel->GetConstFieldZero().AcceptVisitor(prepVisitor);
178
179 printVisitor.SetFrameSymbol(frameSymbol);
180 printVisitor.SetWidth(width);
181 printVisitor.SetDeepestLevel(prepVisitor.GetDeepestLevel());
182 printVisitor.SetNumFields(prepVisitor.GetNumFields());
183
184 for (int i = 0; i < width; ++i)
185 output << frameSymbol;
186 output << "\n";
187 fullModel->GetConstFieldZero().AcceptVisitor(printVisitor);
188 for (int i = 0; i < width; ++i)
189 output << frameSymbol;
190 output << std::endl;
191 break;
192 }
193 case ENTupleInfo::kStorageDetails: fSource->GetSharedDescriptorGuard()->PrintInfo(output); break;
194 case ENTupleInfo::kMetrics: fMetrics.Print(output); break;
195 default:
196 // Unhandled case, internal error
197 R__ASSERT(false);
198 }
199}
200
202{
203 if (!fDisplayReader)
204 fDisplayReader = Clone();
205 return fDisplayReader.get();
206}
207
209{
210 auto reader = GetDisplayReader();
211 const auto &entry = reader->GetModel().GetDefaultEntry();
212
213 reader->LoadEntry(index);
214 output << "{";
215 for (auto iValue = entry.begin(); iValue != entry.end();) {
216 output << std::endl;
217 RPrintValueVisitor visitor(*iValue, output, 1 /* level */);
218 iValue->GetField().AcceptVisitor(visitor);
219
220 if (++iValue == entry.end()) {
221 output << std::endl;
222 break;
223 } else {
224 output << ",";
225 }
226 }
227 output << "}" << std::endl;
228}
229
231{
232 auto descriptorGuard = fSource->GetSharedDescriptorGuard();
233 if (!fCachedDescriptor || fCachedDescriptor->GetGeneration() != descriptorGuard->GetGeneration())
234 fCachedDescriptor = descriptorGuard->Clone();
235 return *fCachedDescriptor;
236}
237
239{
240 auto fieldId = fSource->GetSharedDescriptorGuard()->FindFieldId(fieldName);
241 if (fieldId == kInvalidDescriptorId) {
242 throw RException(R__FAIL("no field named '" + std::string(fieldName) + "' in RNTuple '" +
243 fSource->GetSharedDescriptorGuard()->GetName() + "'"));
244 }
245 return fieldId;
246}
#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
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.
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.
static std::unique_ptr< RNTupleReader > OpenFriends(std::span< RNTupleOpenSpec > ntuples, const RNTupleReadOptions &options=RNTupleReadOptions())
Open RNTuples as one virtual, horizontally combined ntuple.
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.
void ConnectModel(RNTupleModel &model)
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.
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:69
const Int_t n
Definition legend1.C:16
RProjectedFields & GetProjectedFieldsOfModel(RNTupleModel &model)
void CallConnectPageSourceOnField(RFieldBase &, RPageSource &)
Definition RField.cxx:411
RFieldZero & GetFieldZeroOfModel(RNTupleModel &model)
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()