Logo ROOT  
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 : 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 
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 }
ROOT::Experimental::RPrintValueVisitor::PrintName
void PrintName(const Detail::RFieldBase &field)
Definition: RFieldVisitor.cxx:119
ROOT::Experimental::RArrayField
The generic field for fixed size arrays, which do not need an offset column.
Definition: RField.hxx:341
ROOT::Experimental::RField< float >
Definition: RField.hxx:549
n
const Int_t n
Definition: legend1.C:16
ROOT::Experimental::Detail::RFieldBase
Definition: RField.hxx:74
ROOT::Experimental::RPrintSchemaVisitor
Contains settings for printing and prints a summary of an RField instance.
Definition: RFieldVisitor.hxx:111
f
#define f(i)
Definition: RSha256.hxx:122
ROOT::Experimental::RPrintValueVisitor::VisitStringField
void VisitStringField(const RField< std::string > &field) final
Definition: RFieldVisitor.cxx:191
ROOT::Experimental::RPrintValueVisitor::VisitFloatField
void VisitFloatField(const RField< float > &field) final
Definition: RFieldVisitor.cxx:175
ROOT::Experimental::RPrintValueVisitor::RPrintOptions::fPrintName
bool fPrintName
Definition: RFieldVisitor.hxx:175
ROOT::Experimental::RPrepareVisitor::fNumFields
unsigned int fNumFields
Definition: RFieldVisitor.hxx:90
RField.hxx
ROOT::Experimental::RNTupleFormatter::FitString
static std::string FitString(const std::string &str, int availableSpace)
Definition: RFieldVisitor.cxx:275
ROOT::Experimental::RPrintValueVisitor
Renders a JSON value corresponding to the field.
Definition: RFieldVisitor.hxx:171
ROOT::Experimental::RPrintValueVisitor::VisitField
void VisitField(const Detail::RFieldBase &field) final
Definition: RFieldVisitor.cxx:148
uint8_t
uint8_t
Definition: Converters.cxx:858
ROOT::Experimental::RPrintValueVisitor::VisitUInt8Field
void VisitUInt8Field(const RField< std::uint8_t > &field) final
Definition: RFieldVisitor.cxx:200
ROOT::Experimental::RPrintValueVisitor::PrintIndent
void PrintIndent()
Definition: RFieldVisitor.cxx:109
ROOT::Experimental::RPrintValueVisitor::VisitVectorField
void VisitVectorField(const RVectorField &field) final
Definition: RFieldVisitor.cxx:260
ROOT::Experimental::Detail::RFieldBase::GetSubFields
std::vector< const RFieldBase * > GetSubFields() const
Definition: RField.cxx:244
ROOT::Experimental::Detail::RFieldBase::GetType
std::string GetType() const
Definition: RField.hxx:220
ROOT::Experimental::Detail::RFieldBase::GetName
std::string GetName() const
Definition: RField.hxx:219
ROOT::Experimental::RPrintSchemaVisitor::VisitField
void VisitField(const Detail::RFieldBase &field) final
Prints summary of Field.
Definition: RFieldVisitor.cxx:67
ROOT::Experimental::RPrintValueVisitor::VisitArrayField
void VisitArrayField(const RArrayField &field) final
Definition: RFieldVisitor.cxx:224
ROOT::Experimental::RPrintSchemaVisitor::fFieldNoPrefix
std::string fFieldNoPrefix
Definition: RFieldVisitor.hxx:125
ROOT::Experimental::RPrepareVisitor::VisitField
void VisitField(const Detail::RFieldBase &field) final
Definition: RFieldVisitor.cxx:33
ROOT::Experimental::RClassField::SplitValue
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:590
ROOT::Experimental::RField< double >
Definition: RField.hxx:587
ROOT::Experimental::RPrintValueVisitor::VisitUInt64Field
void VisitUInt64Field(const RField< std::uint64_t > &field) final
Definition: RFieldVisitor.cxx:216
ROOT::Experimental::RPrintValueVisitor::VisitUInt32Field
void VisitUInt32Field(const RField< std::uint32_t > &field) final
Definition: RFieldVisitor.cxx:208
ROOT::Experimental::RPrintValueVisitor::VisitVectorBoolField
void VisitVectorBoolField(const RField< std::vector< bool >> &field) final
Definition: RFieldVisitor.cxx:266
ROOT::Experimental::RPrintSchemaVisitor::fFieldNo
int fFieldNo
Definition: RFieldVisitor.hxx:123
ROOT::Experimental::RPrintValueVisitor::RPrintOptions
Definition: RFieldVisitor.hxx:173
ROOT::Experimental::RPrintSchemaVisitor::SetNumFields
void SetNumFields(int n)
Definition: RFieldVisitor.cxx:61
RNTupleUtil.hxx
RNTuple.hxx
ROOT::Experimental::RPrintValueVisitor::VisitBoolField
void VisitBoolField(const RField< bool > &field) final
Definition: RFieldVisitor.cxx:156
ROOT::Experimental::RPrintValueVisitor::PrintCollection
void PrintCollection(const Detail::RFieldBase &field)
Definition: RFieldVisitor.cxx:126
ROOT::Experimental::RFieldZero
The container field for an ntuple model, which itself has no physical representation.
Definition: RField.hxx:259
ROOT::Experimental::RVectorField
The generic field for a (nested) std::vector<Type> except for std::vector<bool>
Definition: RField.hxx:305
ROOT::Experimental::RPrintValueVisitor::VisitClassField
void VisitClassField(const RClassField &field) final
Definition: RFieldVisitor.cxx:230
ROOT::Experimental::RPrintValueVisitor::VisitIntField
void VisitIntField(const RField< int > &field) final
Definition: RFieldVisitor.cxx:183
ROOT::Experimental::RField< bool >
Definition: RField.hxx:512
ROOT::Experimental::RPrintSchemaVisitor::SetDeepestLevel
void SetDeepestLevel(int d)
Definition: RFieldVisitor.cxx:56
ROOT::Experimental::RPrepareVisitor
Visitor used for a pre-processing run to collect information needed by another visitor class.
Definition: RFieldVisitor.hxx:87
ROOT::Experimental::RPrintSchemaVisitor::fTreePrefix
std::string fTreePrefix
Definition: RFieldVisitor.hxx:124
ROOT::Experimental::RPrepareVisitor::fDeepestLevel
unsigned int fDeepestLevel
Definition: RFieldVisitor.hxx:89
RFieldVisitor.hxx
d
#define d(i)
Definition: RSha256.hxx:120
ROOT::Experimental::Detail::RFieldBase::SplitValue
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:230
ROOT::Experimental::RPrepareVisitor::VisitFieldZero
void VisitFieldZero(const RFieldZero &field) final
Definition: RFieldVisitor.cxx:45
ROOT::Experimental::RPrintValueVisitor::RPrintOptions::fPrintSingleLine
bool fPrintSingleLine
Definition: RFieldVisitor.hxx:174
ROOT::Experimental::RClassField
The field for a class with dictionary.
Definition: RField.hxx:276
ROOT::Experimental::RPrintValueVisitor::VisitDoubleField
void VisitDoubleField(const RField< double > &field) final
Definition: RFieldVisitor.cxx:167
RFieldValue.hxx
ROOT::Experimental::RPrintSchemaVisitor::VisitFieldZero
void VisitFieldZero(const RFieldZero &fieldZero) final
Definition: RFieldVisitor.cxx:96
ROOT::Experimental::RField
Classes with dictionaries that can be inspected by TClass.
Definition: RField.hxx:411
RNTupleView.hxx