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>
24
25#include <TROOT.h>
26
28{
29 auto &fieldZero = Internal::GetFieldZeroOfModel(model);
30 // We must not use the descriptor guard to prevent recursive locking in field.ConnectPageSource
31 DescriptorId_t fieldZeroId = fSource->GetSharedDescriptorGuard()->GetFieldZeroId();
32 fieldZero.SetOnDiskId(fieldZeroId);
33 // Iterate only over fieldZero's direct subfields; their descendants are recursively handled in
34 // RFieldBase::ConnectPageSource
35 for (auto &field : fieldZero.GetSubFields()) {
36 // If the model has been created from the descriptor, the on-disk IDs are already set.
37 // User-provided models instead need to find their corresponding IDs in the descriptor.
38 if (field->GetOnDiskId() == kInvalidDescriptorId) {
39 field->SetOnDiskId(fSource->GetSharedDescriptorGuard()->FindFieldId(field->GetFieldName(), fieldZeroId));
40 }
42 }
43}
44
46{
47#ifdef R__USE_IMT
48 if (IsImplicitMTEnabled() &&
49 fSource->GetReadOptions().GetUseImplicitMT() == RNTupleReadOptions::EImplicitMT::kDefault) {
50 fUnzipTasks = std::make_unique<Internal::RNTupleImtTaskScheduler>();
51 fSource->SetTaskScheduler(fUnzipTasks.get());
52 }
53#endif
54 fMetrics.ObserveMetrics(fSource->GetMetrics());
55 if (enableMetrics)
56 EnableMetrics();
57 fSource->Attach();
58}
59
60ROOT::Experimental::RNTupleReader::RNTupleReader(std::unique_ptr<ROOT::Experimental::RNTupleModel> model,
61 std::unique_ptr<ROOT::Experimental::Internal::RPageSource> source,
62 const RNTupleReadOptions &options)
63 : fSource(std::move(source)), fModel(std::move(model)), fMetrics("RNTupleReader")
64{
65 // TODO(jblomer): properly support projected fields
66 auto &projectedFields = Internal::GetProjectedFieldsOfModel(*fModel);
67 if (!projectedFields.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::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
116{
117 if (!fModel) {
118 fModel = fSource->GetSharedDescriptorGuard()->CreateModel();
119 ConnectModel(*fModel);
120 }
121 return *fModel;
122}
123
125{
126 // TODO(lesimon): In a later version, these variables may be defined by the user or the ideal width may be read out
127 // from the terminal.
128 char frameSymbol = '*';
129 int width = 80;
130 /*
131 if (width < 30) {
132 output << "The width is too small! Should be at least 30." << std::endl;
133 return;
134 }
135 */
136 switch (what) {
138 std::string name;
139 std::unique_ptr<RNTupleModel> fullModel;
140 {
141 auto descriptorGuard = fSource->GetSharedDescriptorGuard();
142 name = descriptorGuard->GetName();
143 fullModel = descriptorGuard->CreateModel();
144 }
145
146 for (int i = 0; i < (width / 2 + width % 2 - 4); ++i)
147 output << frameSymbol;
148 output << " NTUPLE ";
149 for (int i = 0; i < (width / 2 - 4); ++i)
150 output << frameSymbol;
151 output << "\n";
152 // FitString defined in RFieldVisitor.cxx
153 output << frameSymbol << " N-Tuple : " << RNTupleFormatter::FitString(name, width - 13) << frameSymbol
154 << "\n"; // prints line with name of ntuple
155 output << frameSymbol << " Entries : " << RNTupleFormatter::FitString(std::to_string(GetNEntries()), width - 13)
156 << frameSymbol << "\n"; // prints line with number of entries
157
158 // Traverses through all fields to gather information needed for printing.
159 RPrepareVisitor prepVisitor;
160 // Traverses through all fields to do the actual printing.
161 RPrintSchemaVisitor printVisitor(output);
162
163 // Note that we do not need to connect the model, we are only looking at its tree of fields
164 fullModel->GetConstFieldZero().AcceptVisitor(prepVisitor);
165
166 printVisitor.SetFrameSymbol(frameSymbol);
167 printVisitor.SetWidth(width);
168 printVisitor.SetDeepestLevel(prepVisitor.GetDeepestLevel());
169 printVisitor.SetNumFields(prepVisitor.GetNumFields());
170
171 for (int i = 0; i < width; ++i)
172 output << frameSymbol;
173 output << "\n";
174 fullModel->GetConstFieldZero().AcceptVisitor(printVisitor);
175 for (int i = 0; i < width; ++i)
176 output << frameSymbol;
177 output << std::endl;
178 break;
179 }
180 case ENTupleInfo::kStorageDetails: fSource->GetSharedDescriptorGuard()->PrintInfo(output); break;
181 case ENTupleInfo::kMetrics: fMetrics.Print(output); break;
182 default:
183 // Unhandled case, internal error
184 R__ASSERT(false);
185 }
186}
187
189{
190 if (!fDisplayReader)
191 fDisplayReader = Clone();
192 return fDisplayReader.get();
193}
194
196{
197 auto reader = GetDisplayReader();
198 const auto &entry = reader->GetModel().GetDefaultEntry();
199
200 reader->LoadEntry(index);
201 output << "{";
202 for (auto iValue = entry.begin(); iValue != entry.end();) {
203 output << std::endl;
204 RPrintValueVisitor visitor(*iValue, output, 1 /* level */);
205 iValue->GetField().AcceptVisitor(visitor);
206
207 if (++iValue == entry.end()) {
208 output << std::endl;
209 break;
210 } else {
211 output << ",";
212 }
213 }
214 output << "}" << std::endl;
215}
216
218{
219 auto descriptorGuard = fSource->GetSharedDescriptorGuard();
220 if (!fCachedDescriptor || fCachedDescriptor->GetGeneration() != descriptorGuard->GetGeneration())
221 fCachedDescriptor = descriptorGuard->Clone();
222 return *fCachedDescriptor;
223}
224
226{
227 auto fieldId = fSource->GetSharedDescriptorGuard()->FindFieldId(fieldName);
228 if (fieldId == kInvalidDescriptorId) {
229 throw RException(R__FAIL("no field named '" + std::string(fieldName) + "' in RNTuple '" +
230 fSource->GetSharedDescriptorGuard()->GetName() + "'"));
231 }
232 return fieldId;
233}
#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.
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
RProjectedFields & GetProjectedFieldsOfModel(RNTupleModel &model)
void CallConnectPageSourceOnField(RFieldBase &, RPageSource &)
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()