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/RNTupleUtil.hxx>
19#include <ROOT/RNTupleView.hxx>
20
21#include <cassert>
22#include <iomanip>
23#include <iostream>
24#include <sstream>
25#include <string>
26#include <vector>
27
28
29//----------------------------- RPrepareVisitor --------------------------------
30
32{
33 auto subFields = field.GetSubFields();
34 for (auto f : subFields) {
35 RPrepareVisitor visitor;
36 f->AcceptVisitor(visitor);
37 fNumFields += visitor.fNumFields;
38 fDeepestLevel = std::max(fDeepestLevel, 1 + visitor.fDeepestLevel);
39 }
40}
41
42
44{
45 VisitField(field);
46 fNumFields--;
47 fDeepestLevel--;
48}
49
50
51//---------------------------- RPrintSchemaVisitor -----------------------------
52
53
55{
56 fDeepestLevel = d;
57}
58
60{
61 fNumFields = n;
62 SetAvailableSpaceForStrings();
63}
64
66{
67 fOutput << fFrameSymbol << ' ';
68
69 std::string key = fTreePrefix;
70 key += "Field " + fFieldNoPrefix + std::to_string(fFieldNo);
71 fOutput << RNTupleFormatter::FitString(key, fAvailableSpaceKeyString);
72 fOutput << " : ";
73
74 std::string value = field.GetFieldName();
75 if (!field.GetTypeName().empty())
76 value += " (" + field.GetTypeName() + ")";
77 fOutput << RNTupleFormatter::FitString(value, fAvailableSpaceValueString);
78 fOutput << fFrameSymbol << std::endl;
79
80 auto subFields = field.GetSubFields();
81 auto fieldNo = 1;
82 for (auto iField = subFields.begin(); iField != subFields.end(); ) {
83 RPrintSchemaVisitor visitor(*this);
84 visitor.fFieldNo = fieldNo++;
85 visitor.fFieldNoPrefix += std::to_string(fFieldNo) + ".";
86
87 auto f = *iField;
88 ++iField;
89 // TODO(jblomer): implement tree drawing
90 visitor.fTreePrefix += " ";
91 f->AcceptVisitor(visitor);
92 }
93}
94
95
97{
98 auto fieldNo = 1;
99 for (auto f : fieldZero.GetSubFields()) {
100 RPrintSchemaVisitor visitor(*this);
101 visitor.fFieldNo = fieldNo++;
102 f->AcceptVisitor(visitor);
103 }
104}
105
106
107//--------------------------- RPrintValueVisitor -------------------------------
108
110{
111 if (fPrintOptions.fPrintSingleLine)
112 return;
113
114 for (unsigned int i = 0; i < fLevel; ++i)
115 fOutput << " ";
116}
117
119{
120 if (fPrintOptions.fPrintName)
121 fOutput << "\"" << field.GetFieldName() << "\": ";
122}
123
125{
126 PrintIndent();
127 PrintName(field);
128 fOutput << "[";
129 auto elems = field.SplitValue(fValue);
130 for (auto iValue = elems.begin(); iValue != elems.end(); ) {
131 RPrintOptions options;
132 options.fPrintSingleLine = true;
133 options.fPrintName = false;
134 RPrintValueVisitor elemVisitor(*iValue, fOutput, 0 /* level */, options);
135 iValue->GetField().AcceptVisitor(elemVisitor);
136
137 if (++iValue == elems.end())
138 break;
139 else
140 fOutput << ", ";
141 }
142 fOutput << "]";
143}
144
146{
147 PrintIndent();
148 PrintName(field);
149 fOutput << "\"<unsupported type: " << field.GetTypeName() << ">\"";
150}
151
152
154{
155 PrintIndent();
156 PrintName(field);
157 if (fValue.GetRef<bool>())
158 fOutput << "true";
159 else
160 fOutput << "false";
161}
162
163
165{
166 PrintIndent();
167 PrintName(field);
168 fOutput << fValue.GetRef<double>();
169}
170
171
173{
174 PrintIndent();
175 PrintName(field);
176 fOutput << fValue.GetRef<float>();
177}
178
180{
181 PrintIndent();
182 PrintName(field);
183 char prev = std::cout.fill();
184 fOutput << "0x" << std::setw(2) << std::setfill('0') << std::hex << (fValue.GetRef<unsigned char>() & 0xff);
185 fOutput << std::resetiosflags(std::ios_base::basefield);
186 std::cout.fill(prev);
187}
188
190{
191 PrintIndent();
192 PrintName(field);
193 fOutput << fValue.GetRef<char>();
194}
195
197{
198 PrintIndent();
199 PrintName(field);
200 fOutput << fValue.GetRef<std::int8_t>();
201}
202
204{
205 PrintIndent();
206 PrintName(field);
207 fOutput << fValue.GetRef<std::int16_t>();
208}
209
211{
212 PrintIndent();
213 PrintName(field);
214 fOutput << fValue.GetRef<int>();
215}
216
218{
219 PrintIndent();
220 PrintName(field);
221 fOutput << fValue.GetRef<std::int64_t>();
222}
223
225{
226 PrintIndent();
227 PrintName(field);
228 // TODO(jblomer): escape double quotes
229 fOutput << "\"" << fValue.GetRef<std::string>() << "\"";
230}
231
233{
234 PrintIndent();
235 PrintName(field);
236 fOutput << static_cast<int>(fValue.GetRef<std::uint8_t>());
237}
238
240{
241 PrintIndent();
242 PrintName(field);
243 fOutput << fValue.GetRef<std::uint16_t>();
244}
245
247{
248 PrintIndent();
249 PrintName(field);
250 fOutput << fValue.GetRef<std::uint32_t>();
251}
252
253
255{
256 PrintIndent();
257 PrintName(field);
258 fOutput << fValue.GetRef<std::uint64_t>();
259}
260
262{
263 PrintIndent();
264 PrintName(field);
265 if (field.As32Bit()) {
266 fOutput << fValue.GetRef<std::uint32_t>();
267 return;
268 }
269 if (field.As64Bit()) {
270 fOutput << fValue.GetRef<std::uint64_t>();
271 return;
272 }
273 R__ASSERT(false && "unsupported cardinality size type");
274}
275
277{
278 constexpr auto nBitsULong = sizeof(unsigned long) * 8;
279 const auto *asULongArray = fValue.GetPtr<unsigned long>().get();
280
281 PrintIndent();
282 PrintName(field);
283 fOutput << "\"";
284 std::size_t i = 0;
285 std::string str;
286 for (std::size_t word = 0; word < (field.GetN() + nBitsULong - 1) / nBitsULong; ++word) {
287 for (std::size_t mask = 0; (mask < nBitsULong) && (i < field.GetN()); ++mask, ++i) {
288 bool isSet = (asULongArray[word] & (static_cast<unsigned long>(1) << mask)) != 0;
289 str = std::to_string(isSet) + str;
290 }
291 }
292 fOutput << str << "\"";
293}
294
296{
297 PrintCollection(field);
298}
299
301{
302 PrintCollection(field);
303}
304
306{
307 PrintIndent();
308 PrintName(field);
309 fOutput << "{";
310 auto elems = field.SplitValue(fValue);
311 for (auto iValue = elems.begin(); iValue != elems.end();) {
312 if (!fPrintOptions.fPrintSingleLine)
313 fOutput << std::endl;
314
315 RPrintOptions options;
316 options.fPrintSingleLine = fPrintOptions.fPrintSingleLine;
317 RPrintValueVisitor visitor(*iValue, fOutput, fLevel + 1, options);
318 iValue->GetField().AcceptVisitor(visitor);
319
320 if (++iValue == elems.end()) {
321 if (!fPrintOptions.fPrintSingleLine)
322 fOutput << std::endl;
323 break;
324 } else {
325 fOutput << ",";
326 if (fPrintOptions.fPrintSingleLine)
327 fOutput << " ";
328 }
329 }
330 PrintIndent();
331 fOutput << "}";
332}
333
334
336{
337 PrintIndent();
338 PrintName(field);
339 fOutput << "{";
340 auto elems = field.SplitValue(fValue);
341 for (auto iValue = elems.begin(); iValue != elems.end(); ) {
342 if (!fPrintOptions.fPrintSingleLine)
343 fOutput << std::endl;
344
345 RPrintOptions options;
346 options.fPrintSingleLine = fPrintOptions.fPrintSingleLine;
347 RPrintValueVisitor visitor(*iValue, fOutput, fLevel + 1, options);
348 iValue->GetField().AcceptVisitor(visitor);
349
350 if (++iValue == elems.end()) {
351 if (!fPrintOptions.fPrintSingleLine)
352 fOutput << std::endl;
353 break;
354 } else {
355 fOutput << ",";
356 if (fPrintOptions.fPrintSingleLine)
357 fOutput << " ";
358 }
359 }
360 PrintIndent();
361 fOutput << "}";
362}
363
365{
366 PrintIndent();
367 PrintName(field);
368 auto elems = field.SplitValue(fValue);
369 if (elems.empty()) {
370 fOutput << "null";
371 } else {
372 RPrintOptions options;
373 options.fPrintSingleLine = true;
374 options.fPrintName = false;
375 RPrintValueVisitor visitor(elems[0], fOutput, fLevel, options);
376 elems[0].GetField().AcceptVisitor(visitor);
377 }
378}
379
381{
382 PrintIndent();
383 PrintName(field);
384 auto intValue = field.SplitValue(fValue)[0];
385 RPrintOptions options;
386 options.fPrintSingleLine = true;
387 options.fPrintName = false;
388 RPrintValueVisitor visitor(intValue, fOutput, fLevel, options);
389 intValue.GetField().AcceptVisitor(visitor);
390}
391
393{
394 PrintIndent();
395 PrintName(field);
396 auto itemValue = field.SplitValue(fValue)[0];
397 RPrintOptions options;
398 options.fPrintSingleLine = true;
399 options.fPrintName = false;
400 RPrintValueVisitor visitor(itemValue, fOutput, fLevel, options);
401 itemValue.GetField().AcceptVisitor(visitor);
402}
403
405{
406 PrintCollection(field);
407}
408
410{
411 PrintCollection(field);
412}
413
415{
416 PrintCollection(field);
417}
418
420{
421 PrintCollection(field);
422}
423
424//---------------------------- RNTupleFormatter --------------------------------
425
426
427std::string ROOT::Experimental::RNTupleFormatter::FitString(const std::string &str, int availableSpace)
428{
429 int strSize{static_cast<int>(str.size())};
430 if (strSize <= availableSpace)
431 return str + std::string(availableSpace - strSize, ' ');
432 else if (availableSpace < 3)
433 return std::string(availableSpace, '.');
434 return std::string(str, 0, availableSpace - 3) + "...";
435}
#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 for fixed-size arrays that are represented as RVecs in memory.
Definition RField.hxx:1236
The generic field for fixed size arrays, which do not need an offset column.
Definition RField.hxx:1184
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:3519
The generic field an std::bitset<N>.
Definition RField.hxx:1279
std::size_t GetN() const
Get the number of bits in the bitset, i.e. the N in std::bitset<N>
Definition RField.hxx:1310
An artificial field that transforms an RNTuple column that contains the offset of collections into co...
Definition RField.hxx:1714
const RField< RNTupleCardinality< std::uint32_t > > * As32Bit() const
Definition RField.cxx:1144
const RField< RNTupleCardinality< std::uint64_t > > * As64Bit() const
Definition RField.cxx:1150
The field for a class with dictionary.
Definition RField.hxx:759
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:1725
The field for an unscoped or scoped enum with dictionary.
Definition RField.hxx:820
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:1803
A field translates read and write calls from/to underlying columns to/from tree values.
Definition RField.hxx:94
std::string GetFieldName() const
Definition RField.hxx:664
std::vector< RFieldBase * > GetSubFields()
Definition RField.cxx:873
std::string GetTypeName() const
Definition RField.hxx:667
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:845
The container field for an ntuple model, which itself has no physical representation.
Definition RField.hxx:716
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:1505
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:1410
Visitor used for a pre-processing run to collect information needed by another visitor class.
void VisitField(const RFieldBase &field) final
void VisitFieldZero(const RFieldZero &field) final
Contains settings for printing and prints a summary of an RField instance.
void VisitField(const 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 PrintName(const RFieldBase &field)
void VisitNullableField(const RNullableField &field) final
void VisitUInt8Field(const RField< std::uint8_t > &field) final
void VisitBitsetField(const RBitsetField &field) final
void VisitCardinalityField(const RCardinalityField &field) final
void VisitAtomicField(const RAtomicField &field) final
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 VisitField(const RFieldBase &field) final
void VisitProxiedCollectionField(const RProxiedCollectionField &field) final
void VisitInt16Field(const RField< std::int16_t > &field) final
void VisitArrayAsRVecField(const RArrayAsRVecField &field) final
void VisitByteField(const RField< std::byte > &field) final
void PrintCollection(const RFieldBase &field)
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:857
The type-erased field for a RVec<Type>
Definition RField.hxx:1122
The field for an untyped record.
Definition RField.hxx:1000
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:2121
The generic field for a (nested) std::vector<Type> except for std::vector<bool>
Definition RField.hxx:1067
const Int_t n
Definition legend1.C:16