Logo ROOT  
Reference Guide
RFieldVisitor.hxx
Go to the documentation of this file.
1 /// \file ROOT/RFieldVisitor.hxx
2 /// \ingroup NTuple ROOT7
3 /// \author Simon Leisibach <simon.satoshi.rene.leisibach@cern.ch>
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 #ifndef ROOT7_RFieldVisitor
17 #define ROOT7_RFieldVisitor
18 
19 #include <ROOT/RField.hxx>
20 #include <ROOT/RFieldValue.hxx>
21 #include <ROOT/RNTupleUtil.hxx>
22 
23 #include <algorithm>
24 #include <iostream>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 
29 namespace ROOT {
30 namespace Experimental {
31 namespace Detail {
32 
33 
34 // clang-format off
35 /**
36 \class ROOT::Experimental::Detail::RFieldVisitor
37 \ingroup NTuple
38 \brief Abstract base class for classes implementing the visitor design pattern.
39 
40 RFieldVisitor::VisitField() is invoked by RFieldBase::AcceptVisitor(). VisitField() is inherited for instance
41 by the RPrintSchemaVisitor class. The RFieldBase class and classes which inherit from it will be visited.
42 */
43 // clang-format on
44 class RFieldVisitor {
45 public:
46  virtual void VisitField(const Detail::RFieldBase &field) = 0;
47  virtual void VisitFieldZero(const RFieldZero &field) { VisitField(field); }
48  virtual void VisitArrayField(const RArrayField &field) { VisitField(field); }
49  virtual void VisitBoolField(const RField<bool> &field) { VisitField(field); }
50  virtual void VisitClassField(const RClassField &field) { VisitField(field); }
51  virtual void VisitClusterSizeField(const RField<ClusterSize_t> &field) { VisitField(field); }
52  virtual void VisitDoubleField(const RField<double> &field) { VisitField(field); }
53  virtual void VisitFloatField(const RField<float> &field) { VisitField(field); }
54  virtual void VisitIntField(const RField<int> &field) { VisitField(field); }
55  virtual void VisitStringField(const RField<std::string> &field) { VisitField(field); }
56  virtual void VisitUInt32Field(const RField<std::uint32_t> &field) { VisitField(field); }
57  virtual void VisitUInt64Field(const RField<std::uint64_t> &field) { VisitField(field); }
58  virtual void VisitUInt8Field(const RField<std::uint8_t> &field) { VisitField(field); }
59  virtual void VisitVectorField(const RVectorField &field) { VisitField(field); }
60  virtual void VisitVectorBoolField(const RField<std::vector<bool>> &field) { VisitField(field); }
61 }; // class RFieldVisitor
62 
63 } // namespace Detail
64 
65 
66 // clang-format off
67 /**
68 \class ROOT::Experimental::RPrepareVisitor
69 \ingroup NTuple
70 \brief Visitor used for a pre-processing run to collect information needed by another visitor class.
71 
72  Currently used for RPrintSchemaVisitor in RNTupleReader::Print() to collect information about levels, max depth etc.
73 */
74 // clang-format on
76 private:
77  unsigned int fDeepestLevel = 1;
78  unsigned int fNumFields = 1;
79 
80 public:
81  RPrepareVisitor() = default;
82  void VisitField(const Detail::RFieldBase &field) final;
83  void VisitFieldZero(const RFieldZero &field) final;
84 
85  unsigned int GetDeepestLevel() const { return fDeepestLevel; }
86  unsigned int GetNumFields() const { return fNumFields; }
87 };
88 
89 
90 // clang-format off
91 /**
92 \class ROOT::Experimental::RPrintSchemaVisitor
93 \ingroup NTuple
94 \brief Contains settings for printing and prints a summary of an RField instance.
95 
96 This visitor is used by RNTupleReader::Print()
97 */
98 // clang-format on
100 private:
101  /// Where to write the printout to
102  std::ostream &fOutput;
103  /// To render the output, use an asterix (*) by default to draw table lines and boundaries
104  char fFrameSymbol;
105  /// Indicates maximal number of allowed characters per line
106  int fWidth;
107  int fDeepestLevel;
108  int fNumFields;
111  int fFieldNo = 1;
112  std::string fTreePrefix;
113  std::string fFieldNoPrefix;
114 
115 public:
116  RPrintSchemaVisitor(std::ostream &out = std::cout, char frameSymbol = '*', int width = 80, int deepestLevel = 1,
117  int numFields = 1)
118  : fOutput{out}, fFrameSymbol{frameSymbol}, fWidth{width}, fDeepestLevel{deepestLevel}, fNumFields{numFields}
119  {
121  }
122  /// Prints summary of Field
123  void VisitField(const Detail::RFieldBase &field) final;
124  void VisitFieldZero(const RFieldZero &fieldZero) final;
125  void SetFrameSymbol(char s) { fFrameSymbol = s; }
126  void SetWidth(int w) { fWidth = w; }
127  void SetDeepestLevel(int d);
128  void SetNumFields(int n);
129  /// Computes how many characters should be placed between the frame symbol and ':' for left and right side of ':' for
130  /// visually pleasing output.
131  // E.g.
132  // * Field 1 : vpx (std::vector<float>) *
133  // * |__Field 1.1 : vpx/vpx (float) *
134  // int fAvailableSpaceKeyString (num characters on left side between "* " and " : ")
135  // deepestlevel here is 2 (1.1 is deepest and has 2 numbers).
136  // For every additional level an additional "|_" and ".1" (4 characters) is added, so the number of required
137  // additional characters is 4 * fDeepestLevel. For level 1, 8 characters are required ("Field 1 "), so an
138  // additional + 4 is added. To account for cases where the total number of fields is not a single digit number and
139  // more space is required to output big numbers, fNumFields is incorporated into the calculation. To make sure
140  // that there is still enough space for the right side of " : ", an std::min comparision with fWidth - 15 is done.
141  // int fAvailableSpaceValueString(num characters on right side between " : " and '*')
142  // The 6 subtracted characters are "* " (2) in the beginning, " : " (3) and '*' (1) on the far right.
144  {
146  std::min(4 * fDeepestLevel + 4 + static_cast<int>(std::to_string(fNumFields).size()), fWidth - 15);
148  }
149 };
150 
151 
152 // clang-format off
153 /**
154 \class ROOT::Experimental::RPrintValueVisitor
155 \ingroup NTuple
156 \brief Renders a JSON value corresponding to the field.
157 */
158 // clang-format on
160 public:
161  struct RPrintOptions {
162  bool fPrintSingleLine;
163  bool fPrintName;
164 
165  RPrintOptions() : fPrintSingleLine(false), fPrintName(true) {}
166  };
167 
168 private:
170  /// The output is directed to fOutput which may differ from std::cout.
171  std::ostream &fOutput;
172  unsigned int fLevel;
174 
175  void PrintIndent();
176  void PrintName(const Detail::RFieldBase &field);
178 
179 public:
181  std::ostream &output,
182  unsigned int level = 0,
184  : fValue(value), fOutput{output}, fLevel(level), fPrintOptions(options) {}
185 
186  void VisitField(const Detail::RFieldBase &field) final;
187 
188  void VisitBoolField(const RField<bool> &field) final;
189  void VisitDoubleField(const RField<double> &field) final;
190  void VisitFloatField(const RField<float> &field) final;
191  void VisitIntField(const RField<int> &field) final;
192  void VisitStringField(const RField<std::string> &field) final;
193  void VisitUInt8Field(const RField<std::uint8_t> &field) final;
194  void VisitUInt32Field(const RField<std::uint32_t> &field) final;
195  void VisitUInt64Field(const RField<std::uint64_t> &field) final;
196 
197  void VisitArrayField(const RArrayField &field) final;
198  void VisitClassField(const RClassField &field) final;
199  void VisitVectorField(const RVectorField &field) final;
200  void VisitVectorBoolField(const RField<std::vector<bool>> &field) final;
201 };
202 
203 
204 // clang-format off
205 /**
206 \class ROOT::Experimental::RNTupleFormatter
207 \ingroup NTuple
208 \brief Contains helper functions for RNTupleReader::PrintInfo() and RPrintSchemaVisitor::VisitField()
209 
210 The functions in this class format strings which are displayed by RNTupleReader::PrintInfo() and RNTupleReader::Show().
211 */
212 // clang-format on
213 class RNTupleFormatter {
214 public:
215  // Can abbreviate long strings, e.g. ("ExampleString" , space= 8) => "Examp..."
216  static std::string FitString(const std::string &str, int availableSpace);
217 };
218 
219 } // namespace Experimental
220 } // namespace ROOT
221 
222 #endif
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::RPrintSchemaVisitor::SetFrameSymbol
void SetFrameSymbol(char s)
Definition: RFieldVisitor.hxx:137
ROOT::Experimental::Detail::RFieldVisitor
Abstract base class for classes implementing the visitor design pattern.
Definition: RFieldVisitor.hxx:62
ROOT::Experimental::RPrintSchemaVisitor::fAvailableSpaceValueString
int fAvailableSpaceValueString
Definition: RFieldVisitor.hxx:122
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
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::Detail::RFieldVisitor::VisitClusterSizeField
virtual void VisitClusterSizeField(const RField< ClusterSize_t > &field)
Definition: RFieldVisitor.hxx:69
ROOT::Experimental::RPrintSchemaVisitor::SetAvailableSpaceForStrings
void SetAvailableSpaceForStrings()
Computes how many characters should be placed between the frame symbol and ':' for left and right sid...
Definition: RFieldVisitor.hxx:155
ROOT::Experimental::RNTupleFormatter
Contains helper functions for RNTupleReader::PrintInfo() and RPrintSchemaVisitor::VisitField()
Definition: RFieldVisitor.hxx:225
ROOT::Experimental::Detail::RFieldVisitor::VisitArrayField
virtual void VisitArrayField(const RArrayField &field)
Definition: RFieldVisitor.hxx:66
ROOT::Experimental::RPrepareVisitor::fNumFields
unsigned int fNumFields
Definition: RFieldVisitor.hxx:90
ROOT::Experimental::Detail::RFieldVisitor::VisitUInt8Field
virtual void VisitUInt8Field(const RField< std::uint8_t > &field)
Definition: RFieldVisitor.hxx:76
ROOT::Experimental::Detail::RFieldVisitor::VisitClassField
virtual void VisitClassField(const RClassField &field)
Definition: RFieldVisitor.hxx:68
RField.hxx
ROOT::Experimental::Detail::RFieldVisitor::VisitBoolField
virtual void VisitBoolField(const RField< bool > &field)
Definition: RFieldVisitor.hxx:67
ROOT::Experimental::RNTupleFormatter::FitString
static std::string FitString(const std::string &str, int availableSpace)
Definition: RFieldVisitor.cxx:275
output
static void output(int code)
Definition: gifencode.c:226
ROOT::Experimental::RPrintValueVisitor
Renders a JSON value corresponding to the field.
Definition: RFieldVisitor.hxx:171
width
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
ROOT::Experimental::Detail::RFieldVisitor::VisitFloatField
virtual void VisitFloatField(const RField< float > &field)
Definition: RFieldVisitor.hxx:71
ROOT::Experimental::RPrintSchemaVisitor::fWidth
int fWidth
Indicates maximal number of allowed characters per line.
Definition: RFieldVisitor.hxx:118
ROOT::Experimental::RPrintValueVisitor::VisitField
void VisitField(const Detail::RFieldBase &field) final
Definition: RFieldVisitor.cxx:148
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::RPrintValueVisitor::fLevel
unsigned int fLevel
Definition: RFieldVisitor.hxx:184
ROOT::Experimental::RPrepareVisitor::RPrepareVisitor
RPrepareVisitor()=default
ROOT::Experimental::RPrintSchemaVisitor::VisitField
void VisitField(const Detail::RFieldBase &field) final
Prints summary of Field.
Definition: RFieldVisitor.cxx:67
ROOT::Experimental::Detail::RFieldValue
Definition: RFieldValue.hxx:59
ROOT::Experimental::RPrepareVisitor::GetNumFields
unsigned int GetNumFields() const
Definition: RFieldVisitor.hxx:98
ROOT::Experimental::RPrintValueVisitor::VisitArrayField
void VisitArrayField(const RArrayField &field) final
Definition: RFieldVisitor.cxx:224
ROOT::Experimental::RPrintSchemaVisitor::SetWidth
void SetWidth(int w)
Definition: RFieldVisitor.hxx:138
ROOT::Experimental::RPrintValueVisitor::fValue
Detail::RFieldValue fValue
Definition: RFieldVisitor.hxx:181
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::RPrintSchemaVisitor::fAvailableSpaceKeyString
int fAvailableSpaceKeyString
Definition: RFieldVisitor.hxx:121
ROOT::Experimental::RPrintValueVisitor::RPrintValueVisitor
RPrintValueVisitor(const Detail::RFieldValue &value, std::ostream &output, unsigned int level=0, RPrintOptions options=RPrintOptions())
Definition: RFieldVisitor.hxx:192
ROOT::Experimental::RField< double >
Definition: RField.hxx:587
ROOT::Experimental::Detail::RFieldVisitor::VisitUInt64Field
virtual void VisitUInt64Field(const RField< std::uint64_t > &field)
Definition: RFieldVisitor.hxx:75
ROOT::Experimental::RPrintValueVisitor::VisitUInt64Field
void VisitUInt64Field(const RField< std::uint64_t > &field) final
Definition: RFieldVisitor.cxx:216
ROOT::Experimental::RPrintSchemaVisitor::fOutput
std::ostream & fOutput
Where to write the printout to.
Definition: RFieldVisitor.hxx:114
ROOT::Experimental::Detail::RFieldVisitor::VisitFieldZero
virtual void VisitFieldZero(const RFieldZero &field)
Definition: RFieldVisitor.hxx:65
ROOT::Experimental::RPrintSchemaVisitor::RPrintSchemaVisitor
RPrintSchemaVisitor(std::ostream &out=std::cout, char frameSymbol=' *', int width=80, int deepestLevel=1, int numFields=1)
Definition: RFieldVisitor.hxx:128
ROOT::Experimental::RPrintSchemaVisitor::fDeepestLevel
int fDeepestLevel
Definition: RFieldVisitor.hxx:119
ROOT::Experimental::Detail::RFieldVisitor::VisitVectorBoolField
virtual void VisitVectorBoolField(const RField< std::vector< bool >> &field)
Definition: RFieldVisitor.hxx:78
ROOT::Experimental::RPrintValueVisitor::RPrintOptions::RPrintOptions
RPrintOptions()
Definition: RFieldVisitor.hxx:177
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::Detail::RFieldVisitor::VisitDoubleField
virtual void VisitDoubleField(const RField< double > &field)
Definition: RFieldVisitor.hxx:70
ROOT::Experimental::RPrintSchemaVisitor::SetNumFields
void SetNumFields(int n)
Definition: RFieldVisitor.cxx:61
RNTupleUtil.hxx
ROOT::Experimental::RPrintSchemaVisitor::fNumFields
int fNumFields
Definition: RFieldVisitor.hxx:120
ROOT::Experimental::RPrintValueVisitor::VisitBoolField
void VisitBoolField(const RField< bool > &field) final
Definition: RFieldVisitor.cxx:156
ROOT::Experimental::RPrintSchemaVisitor::fFrameSymbol
char fFrameSymbol
To render the output, use an asterix (*) by default to draw table lines and boundaries.
Definition: RFieldVisitor.hxx:116
ROOT::Experimental::RPrintValueVisitor::fPrintOptions
RPrintOptions fPrintOptions
Definition: RFieldVisitor.hxx:185
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::RPrepareVisitor::GetDeepestLevel
unsigned int GetDeepestLevel() const
Definition: RFieldVisitor.hxx:97
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::Detail::RFieldVisitor::VisitUInt32Field
virtual void VisitUInt32Field(const RField< std::uint32_t > &field)
Definition: RFieldVisitor.hxx:74
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
ROOT::Experimental::Detail::RFieldVisitor::VisitStringField
virtual void VisitStringField(const RField< std::string > &field)
Definition: RFieldVisitor.hxx:73
d
#define d(i)
Definition: RSha256.hxx:120
ROOT::Experimental::Detail::RFieldVisitor::VisitIntField
virtual void VisitIntField(const RField< int > &field)
Definition: RFieldVisitor.hxx:72
ROOT::Experimental::RPrintValueVisitor::fOutput
std::ostream & fOutput
The output is directed to fOutput which may differ from std::cout.
Definition: RFieldVisitor.hxx:183
ROOT::Experimental::RPrepareVisitor::VisitFieldZero
void VisitFieldZero(const RFieldZero &field) final
Definition: RFieldVisitor.cxx:45
ROOT::Experimental::Detail::RFieldVisitor::VisitField
virtual void VisitField(const Detail::RFieldBase &field)=0
ROOT::Experimental::Detail::RFieldVisitor::VisitVectorField
virtual void VisitVectorField(const RVectorField &field)
Definition: RFieldVisitor.hxx:77
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
VSD Structures.
Definition: StringConv.hxx:21
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