Logo ROOT  
Reference Guide
RNTuple.cxx
Go to the documentation of this file.
1/// \file RNTuple.cxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-10-04
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-2019, 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
16#include "ROOT/RNTuple.hxx"
17
19#include "ROOT/RNTupleModel.hxx"
20#include "ROOT/RPageStorage.hxx"
21
22#include <algorithm>
23#include <exception>
24#include <iomanip>
25#include <iostream>
26#include <sstream>
27#include <string>
28#include <unordered_map>
29#include <utility>
30
31#include <TError.h>
32
33
35 std::unordered_map<const Detail::RFieldBase *, DescriptorId_t> fieldPtr2Id;
36 fieldPtr2Id[fModel->GetRootField()] = fSource->GetDescriptor().FindFieldId("", kInvalidDescriptorId);
37 for (auto &field : *fModel->GetRootField()) {
38 auto parentId = fieldPtr2Id[field.GetParent()];
39 auto fieldId = fSource->GetDescriptor().FindFieldId(field.GetName(), parentId);
41 fieldPtr2Id[&field] = fieldId;
42 Detail::RFieldFuse::Connect(fieldId, *fSource, field);
43 }
44}
45
47 std::unique_ptr<ROOT::Experimental::RNTupleModel> model,
48 std::unique_ptr<ROOT::Experimental::Detail::RPageSource> source)
49 : fSource(std::move(source))
50 , fModel(std::move(model))
51 , fMetrics("RNTupleReader")
52{
53 fSource->Attach();
55 fMetrics.ObserveMetrics(fSource->GetMetrics());
56}
57
58ROOT::Experimental::RNTupleReader::RNTupleReader(std::unique_ptr<ROOT::Experimental::Detail::RPageSource> source)
59 : fSource(std::move(source))
60 , fModel(nullptr)
61 , fMetrics("RNTupleReader")
62{
63 fSource->Attach();
64 fModel = fSource->GetDescriptor().GenerateModel();
66 fMetrics.ObserveMetrics(fSource->GetMetrics());
67}
68
70{
71}
72
73std::unique_ptr<ROOT::Experimental::RNTupleReader> ROOT::Experimental::RNTupleReader::Open(
74 std::unique_ptr<RNTupleModel> model,
75 std::string_view ntupleName,
76 std::string_view storage)
77{
78 return std::make_unique<RNTupleReader>(std::move(model), Detail::RPageSource::Create(ntupleName, storage));
79}
80
81std::unique_ptr<ROOT::Experimental::RNTupleReader> ROOT::Experimental::RNTupleReader::Open(
82 std::string_view ntupleName,
83 std::string_view storage)
84{
85 return std::make_unique<RNTupleReader>(Detail::RPageSource::Create(ntupleName, storage));
86}
87
89{
90 // TODO(lesimon): In a later version, these variables may be defined by the user or the ideal width may be read out from the terminal.
91 char frameSymbol = '*';
92 int width = 80;
93 /*
94 if (width < 30) {
95 output << "The width is too small! Should be at least 30." << std::endl;
96 return;
97 }
98 */
99 std::string name = fSource->GetDescriptor().GetName();
100 //prepVisitor traverses through all fields to gather information needed for printing.
101 RPrepareVisitor prepVisitor;
102 //printVisitor traverses through all fields to do the actual printing.
103 RPrintSchemaVisitor printVisitor(output);
104 switch (what) {
106 for (int i = 0; i < (width/2 + width%2 - 4); ++i)
107 output << frameSymbol;
108 output << " NTUPLE ";
109 for (int i = 0; i < (width/2 - 4); ++i)
110 output << frameSymbol;
111 output << std::endl;
112 // FitString defined in RFieldVisitor.cxx
113 output << frameSymbol << " N-Tuple : " << RNTupleFormatter::FitString(name, width-13) << frameSymbol << std::endl; // prints line with name of ntuple
114 output << frameSymbol << " Entries : " << RNTupleFormatter::FitString(std::to_string(GetNEntries()), width - 13) << frameSymbol << std::endl; // prints line with number of entries
115 GetModel()->GetRootField()->AcceptVisitor(prepVisitor);
116
117 printVisitor.SetFrameSymbol(frameSymbol);
118 printVisitor.SetWidth(width);
119 printVisitor.SetDeepestLevel(prepVisitor.GetDeepestLevel());
120 printVisitor.SetNumFields(prepVisitor.GetNumFields());
121
122 for (int i = 0; i < width; ++i)
123 output << frameSymbol;
124 output << std::endl;
125 GetModel()->GetRootField()->AcceptVisitor(printVisitor);
126 for (int i = 0; i < width; ++i)
127 output << frameSymbol;
128 output << std::endl;
129 break;
131 fSource->GetDescriptor().PrintInfo(output);
132 break;
134 fMetrics.Print(output);
135 break;
136 default:
137 // Unhandled case, internal error
138 R__ASSERT(false);
139 }
140}
141
142
144{
145 auto entry = fModel->CreateEntry();
146 LoadEntry(index, entry.get());
147
148 switch(format) {
150 output << "{";
151 for (auto iValue = entry->begin(); iValue != entry->end(); ) {
152 output << std::endl;
153 RPrintValueVisitor visitor(*iValue, output, 1 /* level */);
154 iValue->GetField()->AcceptVisitor(visitor);
155
156 if (++iValue == entry->end()) {
157 output << std::endl;
158 break;
159 } else {
160 output << ",";
161 }
162 }
163 output << "}" << std::endl;
164 break;
165 }
166 default:
167 // Unhandled case, internal error
168 R__ASSERT(false);
169 }
170}
171
172
173//------------------------------------------------------------------------------
174
175
177 std::unique_ptr<ROOT::Experimental::RNTupleModel> model,
178 std::unique_ptr<ROOT::Experimental::Detail::RPageSink> sink)
179 : fSink(std::move(sink))
180 , fModel(std::move(model))
181 , fClusterSizeEntries(kDefaultClusterSizeEntries)
182 , fLastCommitted(0)
183 , fNEntries(0)
184{
185 fSink->Create(*fModel.get());
186}
187
189{
190 CommitCluster();
191 fSink->CommitDataset();
192}
193
194std::unique_ptr<ROOT::Experimental::RNTupleWriter> ROOT::Experimental::RNTupleWriter::Recreate(
195 std::unique_ptr<RNTupleModel> model,
196 std::string_view ntupleName,
197 std::string_view storage,
198 const RNTupleWriteOptions &options)
199{
200 return std::make_unique<RNTupleWriter>(std::move(model), Detail::RPageSink::Create(ntupleName, storage, options));
201}
202
203
205{
206 if (fNEntries == fLastCommitted) return;
207 for (auto& field : *fModel->GetRootField()) {
208 field.Flush();
209 field.CommitCluster();
210 }
211 fSink->CommitCluster(fNEntries);
212 fLastCommitted = fNEntries;
213}
214
215
216//------------------------------------------------------------------------------
217
218
220 : fOffset(0), fDefaultEntry(std::move(defaultEntry))
221{
222}
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
#define R__ASSERT(e)
Definition: TError.h:96
char name[80]
Definition: TGX11.cxx:109
static void Connect(DescriptorId_t fieldId, RPageStorage &pageStorage, RFieldBase &field)
Definition: RField.cxx:115
void ObserveMetrics(RNTupleMetrics &observee)
static std::unique_ptr< RPageSink > Create(std::string_view ntupleName, std::string_view location, const RNTupleWriteOptions &options=RNTupleWriteOptions())
Guess the concrete derived page source from the file name (location)
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)
RCollectionNTuple(std::unique_ptr< REntry > defaultEntry)
Definition: RNTuple.cxx:219
static std::string FitString(const std::string &str, int availableSpace)
static std::unique_ptr< RNTupleReader > Open(std::unique_ptr< RNTupleModel > model, std::string_view ntupleName, std::string_view storage)
Definition: RNTuple.cxx:73
Detail::RNTupleMetrics fMetrics
Definition: RNTuple.hxx:78
void Show(NTupleSize_t index, const ENTupleFormat format=ENTupleFormat::kJSON, std::ostream &output=std::cout)
Shows the values of the i-th entry/row, starting with 0 for the first entry.
Definition: RNTuple.cxx:143
std::unique_ptr< Detail::RPageSource > fSource
Definition: RNTuple.hxx:75
RNTupleReader(std::unique_ptr< RNTupleModel > model, std::unique_ptr< Detail::RPageSource > source)
The user imposes an ntuple model, which must be compatible with the model found in the data on storag...
Definition: RNTuple.cxx:46
void PrintInfo(const ENTupleInfo what=ENTupleInfo::kSummary, std::ostream &output=std::cout)
Prints a detailed summary of the ntuple, including a list of fields.
Definition: RNTuple.cxx:88
std::unique_ptr< RNTupleModel > fModel
Needs to be destructed before fSource.
Definition: RNTuple.hxx:77
Common user-tunable settings for storing ntuples.
void CommitCluster()
Ensure that the data from the so far seen Fill calls has been written to storage.
Definition: RNTuple.cxx:204
RNTupleWriter(std::unique_ptr< RNTupleModel > model, std::unique_ptr< Detail::RPageSink > sink)
Definition: RNTuple.cxx:176
std::unique_ptr< RNTupleModel > fModel
Needs to be destructed before fSink.
Definition: RNTuple.hxx:174
std::unique_ptr< Detail::RPageSink > fSink
Definition: RNTuple.hxx:172
static std::unique_ptr< RNTupleWriter > Recreate(std::unique_ptr< RNTupleModel > model, std::string_view ntupleName, std::string_view storage, const RNTupleWriteOptions &options=RNTupleWriteOptions())
Definition: RNTuple.cxx:194
Visitor used for a pre-processing run to collect information needed by another visitor class.
unsigned int GetDeepestLevel() const
Contains settings for printing and prints a summary of an RField instance.
Renders a JSON value corresponding to the field.
basic_string_view< char > string_view
ENTupleInfo
Listing of the different options that can be printed by RNTupleReader::GetInfo()
Definition: RNTuple.hxx:47
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
Definition: RNTupleUtil.hxx:42
ENTupleFormat
Listing of the different entry output formats of RNTupleReader::Show()
Definition: RNTuple.hxx:56
constexpr DescriptorId_t kInvalidDescriptorId
Definition: RNTupleUtil.hxx:79
static const char * what
Definition: stlLoader.cc:6
static void output(int code)
Definition: gifencode.c:226