Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFieldVisitor.cxx
Go to the documentation of this file.
1/// \file RFieldVisitor.cxx
2/// \ingroup NTuple ROOT7
3/// \author Simon Leisibach <simon.leisibach@gmail.com>
4/// \date 2019-06-11
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/RField.hxx>
18#include <ROOT/RNTuple.hxx>
19#include <ROOT/RNTupleUtil.hxx>
20#include <ROOT/RNTupleView.hxx>
21
22#include <cassert>
23#include <iostream>
24#include <sstream>
25#include <string>
26#include <vector>
27
28
29//----------------------------- RPrepareVisitor --------------------------------
30
31
33{
34 auto subFields = field.GetSubFields();
35 for (auto f : subFields) {
36 RPrepareVisitor visitor;
37 f->AcceptVisitor(visitor);
38 fNumFields += visitor.fNumFields;
39 fDeepestLevel = std::max(fDeepestLevel, 1 + visitor.fDeepestLevel);
40 }
41}
42
43
45{
46 VisitField(field);
47 fNumFields--;
48 fDeepestLevel--;
49}
50
51
52//---------------------------- RPrintSchemaVisitor -----------------------------
53
54
56{
57 fDeepestLevel = d;
58}
59
61{
62 fNumFields = n;
63 SetAvailableSpaceForStrings();
64}
65
67{
68 fOutput << fFrameSymbol << ' ';
69
70 std::string key = fTreePrefix;
71 key += "Field " + fFieldNoPrefix + std::to_string(fFieldNo);
72 fOutput << RNTupleFormatter::FitString(key, fAvailableSpaceKeyString);
73 fOutput << " : ";
74
75 std::string value = field.GetName();
76 if (!field.GetType().empty())
77 value += " (" + field.GetType() + ")";
78 fOutput << RNTupleFormatter::FitString(value, fAvailableSpaceValueString);
79 fOutput << fFrameSymbol << std::endl;
80
81 auto subFields = field.GetSubFields();
82 auto fieldNo = 1;
83 for (auto iField = subFields.begin(); iField != subFields.end(); ) {
84 RPrintSchemaVisitor visitor(*this);
85 visitor.fFieldNo = fieldNo++;
86 visitor.fFieldNoPrefix += std::to_string(fFieldNo) + ".";
87
88 auto f = *iField;
89 ++iField;
90 // TODO(jblomer): implement tree drawing
91 visitor.fTreePrefix += " ";
92 f->AcceptVisitor(visitor);
93 }
94}
95
96
98{
99 auto fieldNo = 1;
100 for (auto f : fieldZero.GetSubFields()) {
101 RPrintSchemaVisitor visitor(*this);
102 visitor.fFieldNo = fieldNo++;
103 f->AcceptVisitor(visitor);
104 }
105}
106
107
108//--------------------------- RPrintValueVisitor -------------------------------
109
111{
112 if (fPrintOptions.fPrintSingleLine)
113 return;
114
115 for (unsigned int i = 0; i < fLevel; ++i)
116 fOutput << " ";
117}
118
119
121{
122 if (fPrintOptions.fPrintName)
123 fOutput << "\"" << field.GetName() << "\": ";
124}
125
126
128{
129 PrintIndent();
130 PrintName(field);
131 fOutput << "[";
132 auto elems = field.SplitValue(fValue);
133 for (auto iValue = elems.begin(); iValue != elems.end(); ) {
134 RPrintOptions options;
135 options.fPrintSingleLine = true;
136 options.fPrintName = false;
137 RPrintValueVisitor elemVisitor(iValue->GetNonOwningCopy(), fOutput, 0 /* level */, options);
138 iValue->GetField()->AcceptVisitor(elemVisitor);
139
140 if (++iValue == elems.end())
141 break;
142 else
143 fOutput << ", ";
144 }
145 fOutput << "]";
146}
147
148
150{
151 PrintIndent();
152 PrintName(field);
153 fOutput << "\"<unsupported type: " << field.GetType() << ">\"";
154}
155
156
158{
159 PrintIndent();
160 PrintName(field);
161 if (*fValue.Get<bool>())
162 fOutput << "true";
163 else
164 fOutput << "false";
165}
166
167
169{
170 PrintIndent();
171 PrintName(field);
172 fOutput << *fValue.Get<double>();
173}
174
175
177{
178 PrintIndent();
179 PrintName(field);
180 fOutput << *fValue.Get<float>();
181}
182
184{
185 PrintIndent();
186 PrintName(field);
187 fOutput << *fValue.Get<char>();
188}
189
191{
192 PrintIndent();
193 PrintName(field);
194 fOutput << *fValue.Get<std::int8_t>();
195}
196
198{
199 PrintIndent();
200 PrintName(field);
201 fOutput << *fValue.Get<std::int16_t>();
202}
203
205{
206 PrintIndent();
207 PrintName(field);
208 fOutput << *fValue.Get<int>();
209}
210
212{
213 PrintIndent();
214 PrintName(field);
215 fOutput << *fValue.Get<std::int64_t>();
216}
217
219{
220 PrintIndent();
221 PrintName(field);
222 // TODO(jblomer): escape double quotes
223 fOutput << "\"" << *fValue.Get<std::string>() << "\"";
224}
225
226
228{
229 PrintIndent();
230 PrintName(field);
231 fOutput << static_cast<int>(*fValue.Get<std::uint8_t>());
232}
233
235{
236 PrintIndent();
237 PrintName(field);
238 fOutput << *fValue.Get<std::uint16_t>();
239}
240
242{
243 PrintIndent();
244 PrintName(field);
245 fOutput << *fValue.Get<std::uint32_t>();
246}
247
248
250{
251 PrintIndent();
252 PrintName(field);
253 fOutput << *fValue.Get<std::uint64_t>();
254}
255
257{
258 PrintIndent();
259 PrintName(field);
260 if (field.As32Bit()) {
261 fOutput << *fValue.Get<std::uint32_t>();
262 return;
263 }
264 if (field.As64Bit()) {
265 fOutput << *fValue.Get<std::uint64_t>();
266 return;
267 }
268 R__ASSERT(false && "unsupported cardinality size type");
269}
270
272{
273 constexpr auto nBitsULong = sizeof(unsigned long) * 8;
274 const auto *asULongArray = fValue.Get<unsigned long>();
275
276 PrintIndent();
277 PrintName(field);
278 fOutput << "\"";
279 std::size_t i = 0;
280 std::string str;
281 for (std::size_t word = 0; word < (field.GetN() + nBitsULong - 1) / nBitsULong; ++word) {
282 for (std::size_t mask = 0; (mask < nBitsULong) && (i < field.GetN()); ++mask, ++i) {
283 bool isSet = (asULongArray[word] & (static_cast<unsigned long>(1) << mask)) != 0;
284 str = std::to_string(isSet) + str;
285 }
286 }
287 fOutput << str << "\"";
288}
289
291{
292 PrintCollection(field);
293}
294
295
297{
298 PrintIndent();
299 PrintName(field);
300 fOutput << "{";
301 auto elems = field.SplitValue(fValue);
302 for (auto iValue = elems.begin(); iValue != elems.end();) {
303 if (!fPrintOptions.fPrintSingleLine)
304 fOutput << std::endl;
305
306 RPrintOptions options;
307 options.fPrintSingleLine = fPrintOptions.fPrintSingleLine;
308 RPrintValueVisitor visitor(iValue->GetNonOwningCopy(), fOutput, fLevel + 1, options);
309 iValue->GetField()->AcceptVisitor(visitor);
310
311 if (++iValue == elems.end()) {
312 if (!fPrintOptions.fPrintSingleLine)
313 fOutput << std::endl;
314 break;
315 } else {
316 fOutput << ",";
317 if (fPrintOptions.fPrintSingleLine)
318 fOutput << " ";
319 }
320 }
321 PrintIndent();
322 fOutput << "}";
323}
324
325
327{
328 PrintIndent();
329 PrintName(field);
330 fOutput << "{";
331 auto elems = field.SplitValue(fValue);
332 for (auto iValue = elems.begin(); iValue != elems.end(); ) {
333 if (!fPrintOptions.fPrintSingleLine)
334 fOutput << std::endl;
335
336 RPrintOptions options;
337 options.fPrintSingleLine = fPrintOptions.fPrintSingleLine;
338 RPrintValueVisitor visitor(iValue->GetNonOwningCopy(), fOutput, fLevel + 1, options);
339 iValue->GetField()->AcceptVisitor(visitor);
340
341 if (++iValue == elems.end()) {
342 if (!fPrintOptions.fPrintSingleLine)
343 fOutput << std::endl;
344 break;
345 } else {
346 fOutput << ",";
347 if (fPrintOptions.fPrintSingleLine)
348 fOutput << " ";
349 }
350 }
351 PrintIndent();
352 fOutput << "}";
353}
354
356{
357 PrintIndent();
358 PrintName(field);
359 auto elems = field.SplitValue(fValue);
360 if (elems.empty()) {
361 fOutput << "null";
362 } else {
363 RPrintOptions options;
364 options.fPrintSingleLine = true;
365 options.fPrintName = false;
366 RPrintValueVisitor visitor(elems[0].GetNonOwningCopy(), fOutput, fLevel, options);
367 elems[0].GetField()->AcceptVisitor(visitor);
368 }
369}
370
372{
373 PrintIndent();
374 PrintName(field);
375 auto intValue = field.SplitValue(fValue)[0].GetNonOwningCopy();
376 RPrintOptions options;
377 options.fPrintSingleLine = true;
378 options.fPrintName = false;
379 RPrintValueVisitor visitor(intValue.GetNonOwningCopy(), fOutput, fLevel, options);
380 intValue.GetField()->AcceptVisitor(visitor);
381}
382
384{
385 PrintCollection(field);
386}
387
389{
390 PrintCollection(field);
391}
392
394{
395 PrintCollection(field);
396}
397
399{
400 PrintCollection(field);
401}
402
403//---------------------------- RNTupleFormatter --------------------------------
404
405
406std::string ROOT::Experimental::RNTupleFormatter::FitString(const std::string &str, int availableSpace)
407{
408 int strSize{static_cast<int>(str.size())};
409 if (strSize <= availableSpace)
410 return str + std::string(availableSpace - strSize, ' ');
411 else if (availableSpace < 3)
412 return std::string(availableSpace, '.');
413 return std::string(str, 0, availableSpace - 3) + "...";
414}
#define d(i)
Definition RSha256.hxx:102
#define f(i)
Definition RSha256.hxx:104
#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 Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t mask
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
A field translates read and write calls from/to underlying columns to/from tree values.
Definition RField.hxx:83
virtual std::vector< RValue > SplitValue(const RValue &value) const
Creates the list of direct child values given a value for this field.
Definition RField.cxx:569
std::vector< RFieldBase * > GetSubFields() const
Definition RField.cxx:598
The generic field for fixed size arrays, which do not need an offset column.
Definition RField.hxx:1032
The generic field an std::bitset<N>.
Definition RField.hxx:1067
std::size_t GetN() const
Get the number of bits in the bitset, i.e. the N in std::bitset<N>
Definition RField.hxx:1099
An artificial field that transforms an RNTuple column that contains the offset of collections into co...
Definition RField.hxx:1422
const RField< RNTupleCardinality< std::uint32_t > > * As32Bit() const
Definition RField.cxx:853
const RField< RNTupleCardinality< std::uint64_t > > * As64Bit() const
Definition RField.cxx:859
The field for a class with dictionary.
Definition RField.hxx:674
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given a value for this field.
Definition RField.cxx:1403
The field for an unscoped or scoped enum with dictionary.
Definition RField.hxx:727
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given a value for this field.
Definition RField.cxx:1479
The container field for an ntuple model, which itself has no physical representation.
Definition RField.hxx:655
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:1224
static std::string FitString(const std::string &str, int availableSpace)
The field for values that may or may not be present in an entry.
Definition RField.hxx:1165
Visitor used for a pre-processing run to collect information needed by another visitor class.
void VisitField(const Detail::RFieldBase &field) final
void VisitFieldZero(const RFieldZero &field) final
Contains settings for printing and prints a summary of an RField instance.
void VisitField(const Detail::RFieldBase &field) final
Prints summary of Field.
void VisitFieldZero(const RFieldZero &fieldZero) final
Renders a JSON value corresponding to the field.
void VisitVectorBoolField(const RField< std::vector< bool > > &field) final
void VisitDoubleField(const RField< double > &field) final
void VisitUInt16Field(const RField< std::uint16_t > &field) final
void VisitArrayField(const RArrayField &field) final
void VisitFloatField(const RField< float > &field) final
void VisitRecordField(const RRecordField &field) final
void VisitStringField(const RField< std::string > &field) final
void VisitInt8Field(const RField< std::int8_t > &field) final
void VisitNullableField(const RNullableField &field) final
void PrintName(const Detail::RFieldBase &field)
void VisitUInt8Field(const RField< std::uint8_t > &field) final
void VisitBitsetField(const RBitsetField &field) final
void VisitCardinalityField(const RCardinalityField &field) final
void VisitField(const Detail::RFieldBase &field) final
void PrintCollection(const Detail::RFieldBase &field)
void VisitRVecField(const RRVecField &field) final
void VisitUInt64Field(const RField< std::uint64_t > &field) final
void VisitEnumField(const REnumField &field) final
void VisitBoolField(const RField< bool > &field) final
void VisitProxiedCollectionField(const RProxiedCollectionField &field) final
void VisitInt16Field(const RField< std::int16_t > &field) final
void VisitVectorField(const RVectorField &field) final
void VisitCharField(const RField< char > &field) final
void VisitInt64Field(const RField< std::int64_t > &field) final
void VisitClassField(const RClassField &field) final
void VisitIntField(const RField< int > &field) final
void VisitUInt32Field(const RField< std::uint32_t > &field) final
The field for a class representing a collection of elements via TVirtualCollectionProxy.
Definition RField.hxx:764
The type-erased field for a RVec<Type>
Definition RField.hxx:983
The field for an untyped record.
Definition RField.hxx:889
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given a value for this field.
Definition RField.cxx:1777
The generic field for a (nested) std::vector<Type> except for std::vector<bool>
Definition RField.hxx:943
const Int_t n
Definition legend1.C:16