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 <iomanip>
24#include <iostream>
25#include <sstream>
26#include <string>
27#include <vector>
28
29
30//----------------------------- RPrepareVisitor --------------------------------
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.GetFieldName();
76 if (!field.GetTypeName().empty())
77 value += " (" + field.GetTypeName() + ")";
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
120{
121 if (fPrintOptions.fPrintName)
122 fOutput << "\"" << field.GetFieldName() << "\": ";
123}
124
126{
127 PrintIndent();
128 PrintName(field);
129 fOutput << "[";
130 auto elems = field.SplitValue(fValue);
131 for (auto iValue = elems.begin(); iValue != elems.end(); ) {
132 RPrintOptions options;
133 options.fPrintSingleLine = true;
134 options.fPrintName = false;
135 RPrintValueVisitor elemVisitor(*iValue, fOutput, 0 /* level */, options);
136 iValue->GetField().AcceptVisitor(elemVisitor);
137
138 if (++iValue == elems.end())
139 break;
140 else
141 fOutput << ", ";
142 }
143 fOutput << "]";
144}
145
147{
148 PrintIndent();
149 PrintName(field);
150 fOutput << "\"<unsupported type: " << field.GetTypeName() << ">\"";
151}
152
153
155{
156 PrintIndent();
157 PrintName(field);
158 if (fValue.GetRef<bool>())
159 fOutput << "true";
160 else
161 fOutput << "false";
162}
163
164
166{
167 PrintIndent();
168 PrintName(field);
169 fOutput << fValue.GetRef<double>();
170}
171
172
174{
175 PrintIndent();
176 PrintName(field);
177 fOutput << fValue.GetRef<float>();
178}
179
181{
182 PrintIndent();
183 PrintName(field);
184 char prev = std::cout.fill();
185 fOutput << "0x" << std::setw(2) << std::setfill('0') << std::hex << (fValue.GetRef<unsigned char>() & 0xff);
186 fOutput << std::resetiosflags(std::ios_base::basefield);
187 std::cout.fill(prev);
188}
189
191{
192 PrintIndent();
193 PrintName(field);
194 fOutput << fValue.GetRef<char>();
195}
196
198{
199 PrintIndent();
200 PrintName(field);
201 fOutput << fValue.GetRef<std::int8_t>();
202}
203
205{
206 PrintIndent();
207 PrintName(field);
208 fOutput << fValue.GetRef<std::int16_t>();
209}
210
212{
213 PrintIndent();
214 PrintName(field);
215 fOutput << fValue.GetRef<int>();
216}
217
219{
220 PrintIndent();
221 PrintName(field);
222 fOutput << fValue.GetRef<std::int64_t>();
223}
224
226{
227 PrintIndent();
228 PrintName(field);
229 // TODO(jblomer): escape double quotes
230 fOutput << "\"" << fValue.GetRef<std::string>() << "\"";
231}
232
234{
235 PrintIndent();
236 PrintName(field);
237 fOutput << static_cast<int>(fValue.GetRef<std::uint8_t>());
238}
239
241{
242 PrintIndent();
243 PrintName(field);
244 fOutput << fValue.GetRef<std::uint16_t>();
245}
246
248{
249 PrintIndent();
250 PrintName(field);
251 fOutput << fValue.GetRef<std::uint32_t>();
252}
253
254
256{
257 PrintIndent();
258 PrintName(field);
259 fOutput << fValue.GetRef<std::uint64_t>();
260}
261
263{
264 PrintIndent();
265 PrintName(field);
266 if (field.As32Bit()) {
267 fOutput << fValue.GetRef<std::uint32_t>();
268 return;
269 }
270 if (field.As64Bit()) {
271 fOutput << fValue.GetRef<std::uint64_t>();
272 return;
273 }
274 R__ASSERT(false && "unsupported cardinality size type");
275}
276
278{
279 constexpr auto nBitsULong = sizeof(unsigned long) * 8;
280 const auto *asULongArray = fValue.GetPtr<unsigned long>().get();
281
282 PrintIndent();
283 PrintName(field);
284 fOutput << "\"";
285 std::size_t i = 0;
286 std::string str;
287 for (std::size_t word = 0; word < (field.GetN() + nBitsULong - 1) / nBitsULong; ++word) {
288 for (std::size_t mask = 0; (mask < nBitsULong) && (i < field.GetN()); ++mask, ++i) {
289 bool isSet = (asULongArray[word] & (static_cast<unsigned long>(1) << mask)) != 0;
290 str = std::to_string(isSet) + str;
291 }
292 }
293 fOutput << str << "\"";
294}
295
297{
298 PrintCollection(field);
299}
300
302{
303 PrintCollection(field);
304}
305
307{
308 PrintIndent();
309 PrintName(field);
310 fOutput << "{";
311 auto elems = field.SplitValue(fValue);
312 for (auto iValue = elems.begin(); iValue != elems.end();) {
313 if (!fPrintOptions.fPrintSingleLine)
314 fOutput << std::endl;
315
316 RPrintOptions options;
317 options.fPrintSingleLine = fPrintOptions.fPrintSingleLine;
318 RPrintValueVisitor visitor(*iValue, fOutput, fLevel + 1, options);
319 iValue->GetField().AcceptVisitor(visitor);
320
321 if (++iValue == elems.end()) {
322 if (!fPrintOptions.fPrintSingleLine)
323 fOutput << std::endl;
324 break;
325 } else {
326 fOutput << ",";
327 if (fPrintOptions.fPrintSingleLine)
328 fOutput << " ";
329 }
330 }
331 PrintIndent();
332 fOutput << "}";
333}
334
335
337{
338 PrintIndent();
339 PrintName(field);
340 fOutput << "{";
341 auto elems = field.SplitValue(fValue);
342 for (auto iValue = elems.begin(); iValue != elems.end(); ) {
343 if (!fPrintOptions.fPrintSingleLine)
344 fOutput << std::endl;
345
346 RPrintOptions options;
347 options.fPrintSingleLine = fPrintOptions.fPrintSingleLine;
348 RPrintValueVisitor visitor(*iValue, fOutput, fLevel + 1, options);
349 iValue->GetField().AcceptVisitor(visitor);
350
351 if (++iValue == elems.end()) {
352 if (!fPrintOptions.fPrintSingleLine)
353 fOutput << std::endl;
354 break;
355 } else {
356 fOutput << ",";
357 if (fPrintOptions.fPrintSingleLine)
358 fOutput << " ";
359 }
360 }
361 PrintIndent();
362 fOutput << "}";
363}
364
366{
367 PrintIndent();
368 PrintName(field);
369 auto elems = field.SplitValue(fValue);
370 if (elems.empty()) {
371 fOutput << "null";
372 } else {
373 RPrintOptions options;
374 options.fPrintSingleLine = true;
375 options.fPrintName = false;
376 RPrintValueVisitor visitor(elems[0], fOutput, fLevel, options);
377 elems[0].GetField().AcceptVisitor(visitor);
378 }
379}
380
382{
383 PrintIndent();
384 PrintName(field);
385 auto intValue = field.SplitValue(fValue)[0];
386 RPrintOptions options;
387 options.fPrintSingleLine = true;
388 options.fPrintName = false;
389 RPrintValueVisitor visitor(intValue, fOutput, fLevel, options);
390 intValue.GetField().AcceptVisitor(visitor);
391}
392
394{
395 PrintIndent();
396 PrintName(field);
397 auto itemValue = field.SplitValue(fValue)[0];
398 RPrintOptions options;
399 options.fPrintSingleLine = true;
400 options.fPrintName = false;
401 RPrintValueVisitor visitor(itemValue, fOutput, fLevel, options);
402 itemValue.GetField().AcceptVisitor(visitor);
403}
404
406{
407 PrintCollection(field);
408}
409
411{
412 PrintCollection(field);
413}
414
416{
417 PrintCollection(field);
418}
419
421{
422 PrintCollection(field);
423}
424
425//---------------------------- RNTupleFormatter --------------------------------
426
427
428std::string ROOT::Experimental::RNTupleFormatter::FitString(const std::string &str, int availableSpace)
429{
430 int strSize{static_cast<int>(str.size())};
431 if (strSize <= availableSpace)
432 return str + std::string(availableSpace - strSize, ' ');
433 else if (availableSpace < 3)
434 return std::string(availableSpace, '.');
435 return std::string(str, 0, availableSpace - 3) + "...";
436}
#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:1183
The generic field for fixed size arrays, which do not need an offset column.
Definition RField.hxx:1130
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:3386
The generic field an std::bitset<N>.
Definition RField.hxx:1228
std::size_t GetN() const
Get the number of bits in the bitset, i.e. the N in std::bitset<N>
Definition RField.hxx:1260
An artificial field that transforms an RNTuple column that contains the offset of collections into co...
Definition RField.hxx:1676
const RField< RNTupleCardinality< std::uint32_t > > * As32Bit() const
Definition RField.cxx:1019
const RField< RNTupleCardinality< std::uint64_t > > * As64Bit() const
Definition RField.cxx:1025
The field for a class with dictionary.
Definition RField.hxx:699
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:1592
The field for an unscoped or scoped enum with dictionary.
Definition RField.hxx:761
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:1670
A field translates read and write calls from/to underlying columns to/from tree values.
Definition RField.hxx:90
std::string GetFieldName() const
Definition RField.hxx:628
std::vector< RFieldBase * > GetSubFields()
Definition RField.cxx:748
std::string GetTypeName() const
Definition RField.hxx:631
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:720
The container field for an ntuple model, which itself has no physical representation.
Definition RField.hxx:680
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:1458
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:1361
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:799
The type-erased field for a RVec<Type>
Definition RField.hxx:1067
The field for an untyped record.
Definition RField.hxx:943
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:1988
The generic field for a (nested) std::vector<Type> except for std::vector<bool>
Definition RField.hxx:1011
const Int_t n
Definition legend1.C:16