Logo ROOT   master
Reference Guide
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>
17 #include <ROOT/RFieldValue.hxx>
18 #include <ROOT/RFieldVisitor.hxx>
19 #include <ROOT/RNTuple.hxx>
20 #include <ROOT/RNTupleUtil.hxx>
21 #include <ROOT/RNTupleView.hxx>
22 
23 #include <cassert>
24 #include <iostream>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 
29 
30 //----------------------------- RPrepareVisitor --------------------------------
31 
32 
34 {
35  auto subFields = field.GetSubFields();
36  for (auto f : subFields) {
37  RPrepareVisitor visitor;
38  f->AcceptVisitor(visitor);
39  fNumFields += visitor.fNumFields;
40  fDeepestLevel = std::max(fDeepestLevel, 1 + visitor.fDeepestLevel);
41  }
42 }
43 
44 
46 {
47  VisitField(field);
48  fNumFields--;
49  fDeepestLevel--;
50 }
51 
52 
53 //---------------------------- RPrintSchemaVisitor -----------------------------
54 
55 
57 {
58  fDeepestLevel = d;
59 }
60 
62 {
63  fNumFields = n;
64  SetAvailableSpaceForStrings();
65 }
66 
68 {
69  fOutput << fFrameSymbol << ' ';
70 
71  std::string key = fTreePrefix;
72  key += "Field " + fFieldNoPrefix + std::to_string(fFieldNo);
73  fOutput << RNTupleFormatter::FitString(key, fAvailableSpaceKeyString);
74  fOutput << " : ";
75 
76  std::string value = field.GetName() + " (" + field.GetType() + ")";
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 : rootField.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 
118 
120 {
121  if (fPrintOptions.fPrintName)
122  fOutput << "\"" << field.GetName() << "\": ";
123 }
124 
125 
127 {
128  PrintIndent();
129  PrintName(field);
130  fOutput << "[";
131  auto elems = field.SplitValue(fValue);
132  for (auto iValue = elems.begin(); iValue != elems.end(); ) {
133  RPrintOptions options;
134  options.fPrintSingleLine = true;
135  options.fPrintName = false;
136  RPrintValueVisitor elemVisitor(*iValue, fOutput, 0 /* level */, options);
137  iValue->GetField()->AcceptVisitor(elemVisitor);
138 
139  if (++iValue == elems.end())
140  break;
141  else
142  fOutput << ", ";
143  }
144  fOutput << "]";
145 }
146 
147 
149 {
150  PrintIndent();
151  PrintName(field);
152  fOutput << "\"<unsupported type: " << field.GetType() << ">\"";
153 }
154 
155 
157 {
158  PrintIndent();
159  PrintName(field);
160  if (*fValue.Get<bool>())
161  fOutput << "true";
162  else
163  fOutput << "false";
164 }
165 
166 
168 {
169  PrintIndent();
170  PrintName(field);
171  fOutput << *fValue.Get<double>();
172 }
173 
174 
176 {
177  PrintIndent();
178  PrintName(field);
179  fOutput << *fValue.Get<float>();
180 }
181 
182 
184 {
185  PrintIndent();
186  PrintName(field);
187  fOutput << *fValue.Get<int>();
188 }
189 
190 
192 {
193  PrintIndent();
194  PrintName(field);
195  // TODO(jblomer): escape double quotes
196  fOutput << "\"" << *fValue.Get<std::string>() << "\"";
197 }
198 
199 
201 {
202  PrintIndent();
203  PrintName(field);
204  fOutput << static_cast<int>(*fValue.Get<std::uint8_t>());
205 }
206 
207 
209 {
210  PrintIndent();
211  PrintName(field);
212  fOutput << *fValue.Get<std::uint32_t>();
213 }
214 
215 
217 {
218  PrintIndent();
219  PrintName(field);
220  fOutput << *fValue.Get<std::uint64_t>();
221 }
222 
223 
225 {
226  PrintCollection(field);
227 }
228 
229 
231 {
232  PrintIndent();
233  PrintName(field);
234  fOutput << "{";
235  auto elems = field.SplitValue(fValue);
236  for (auto iValue = elems.begin(); iValue != elems.end(); ) {
237  if (!fPrintOptions.fPrintSingleLine)
238  fOutput << std::endl;
239 
240  RPrintOptions options;
241  options.fPrintSingleLine = fPrintOptions.fPrintSingleLine;
242  RPrintValueVisitor visitor(*iValue, fOutput, fLevel + 1, options);
243  iValue->GetField()->AcceptVisitor(visitor);
244 
245  if (++iValue == elems.end()) {
246  if (!fPrintOptions.fPrintSingleLine)
247  fOutput << std::endl;
248  break;
249  } else {
250  fOutput << ",";
251  if (fPrintOptions.fPrintSingleLine)
252  fOutput << " ";
253  }
254  }
255  PrintIndent();
256  fOutput << "}";
257 }
258 
259 
261 {
262  PrintCollection(field);
263 }
264 
265 
267 {
268  PrintCollection(field);
269 }
270 
271 
272 //---------------------------- RNTupleFormatter --------------------------------
273 
274 
275 std::string ROOT::Experimental::RNTupleFormatter::FitString(const std::string &str, int availableSpace)
276 {
277  int strSize{static_cast<int>(str.size())};
278  if (strSize <= availableSpace)
279  return str + std::string(availableSpace - strSize, ' ');
280  else if (availableSpace < 3)
281  return std::string(availableSpace, '.');
282  return std::string(str, 0, availableSpace - 3) + "...";
283 }
The container field for an ntuple model, which itself has no physical representation.
Definition: RField.hxx:251
Contains settings for printing and prints a summary of an RField instance.
void VisitBoolField(const RField< bool > &field) final
void VisitRootField(const RFieldRoot &rootField) final
#define f(i)
Definition: RSha256.hxx:104
The generic field for fixed size arrays, which do not need an offset column.
Definition: RField.hxx:333
static std::string FitString(const std::string &str, int availableSpace)
void VisitField(const Detail::RFieldBase &field) final
Prints summary of Field.
void VisitUInt8Field(const RField< std::uint8_t > &field) final
std::vector< Detail::RFieldValue > SplitValue(const Detail::RFieldValue &value) const final
Creates the list of direct child values given a value for this field.
Definition: RField.cxx:560
The field for a class with dictionary.
Definition: RField.hxx:268
void VisitFloatField(const RField< float > &field) final
Renders a JSON value corresponding to the field.
void VisitIntField(const RField< int > &field) final
void VisitArrayField(const RFieldArray &field) final
Classes with dictionaries that can be inspected by TClass.
Definition: RField.hxx:403
virtual std::vector< RFieldValue > SplitValue(const RFieldValue &value) const
Creates the list of direct child values given a value for this field.
Definition: RField.cxx:200
void PrintName(const Detail::RFieldBase &field)
void VisitField(const Detail::RFieldBase &field) final
void VisitVectorBoolField(const RField< std::vector< bool >> &field) final
PyObject * fValue
void VisitRootField(const RFieldRoot &field) final
The generic field for a (nested) std::vector<Type> except for std::vector<bool>
Definition: RField.hxx:297
#define d(i)
Definition: RSha256.hxx:102
void VisitUInt64Field(const RField< std::uint64_t > &field) final
void PrintCollection(const Detail::RFieldBase &field)
std::vector< const RFieldBase * > GetSubFields() const
Definition: RField.cxx:214
void VisitField(const Detail::RFieldBase &field) final
void VisitDoubleField(const RField< double > &field) final
void VisitUInt32Field(const RField< std::uint32_t > &field) final
void VisitVectorField(const RFieldVector &field) final
const Int_t n
Definition: legend1.C:16
void VisitClassField(const RFieldClass &field) final
void VisitStringField(const RField< std::string > &field) final
Visitor used for a pre-processing run to collect information needed by another visitor class...