Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RFieldVisitor.cxx
Go to the documentation of this file.
1 /// \file RFieldVisitor.cxx
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 #include <algorithm>
17 #include <iomanip>
18 #include <iostream>
19 #include <sstream>
20 #include <string>
21 #include <vector>
22 
23 #include "ROOT/RField.hxx"
24 #include "ROOT/RFieldVisitor.hxx"
25 #include "ROOT/RNTuple.hxx"
26 
27 
28 //---------------------------- RPrintVisitor ------------------------------------
29 
30 void ROOT::Experimental::RPrintVisitor::SetDeepestLevel(int d) {
31  fDeepestLevel = d;
32  fFlagForVerticalLines.resize(d - 1);
33 }
34 
35 void ROOT::Experimental::RPrintVisitor::SetNumFields(int n) {
36  fNumFields = n;
37  SetAvailableSpaceForStrings();
38 }
39 
40 std::string ROOT::Experimental::RPrintVisitor::MakeKeyString(const Detail::RFieldBase &field, int level)
41 {
42  std::string result{""};
43  if (level==1) {
44  result += "Field ";
45  result += std::to_string(field.GetLevelInfo().GetOrder());
46  } else {
47  if (field.GetLevelInfo().GetOrder() == field.GetLevelInfo().GetNumSiblings()) { fFlagForVerticalLines.at(level-2) = false;
48  } else {
49  fFlagForVerticalLines.at(level-2) = true;
50  }
51  for(int i = 0; i < level-2; ++i) {
52  if (fFlagForVerticalLines.at(i)) {
53  result+= "| ";
54  } else {
55  result += " ";
56  }
57  }
58  result += "|__Field ";
59  result += RNTupleFormatter::HierarchialFieldOrder(field);
60  }
61  return result;
62 }
63 
64 std::string ROOT::Experimental::RPrintVisitor::MakeValueString(const Detail::RFieldBase &field)
65 {
66  std::string nameAndType{field.GetName() + " (" + field.GetType() + ")"};
67  return nameAndType;
68 }
69 
70 // Entire function only prints 1 Line, when if statement is disregarded.
71 void ROOT::Experimental::RPrintVisitor::VisitField(const Detail::RFieldBase &field, int level)
72 {
73  if (level == 1)
74  {
75  for (int i = 0; i < fWidth; ++i) {
76  fOutput << fFrameSymbol;
77  }
78  fOutput << std::endl;
79  }
80  fOutput << fFrameSymbol << ' ';
81  fOutput << RNTupleFormatter::FitString(MakeKeyString(field, level), fAvailableSpaceKeyString);
82  fOutput << " : ";
83  fOutput << RNTupleFormatter::FitString(MakeValueString(field), fAvailableSpaceValueString);
84  fOutput << fFrameSymbol << std::endl;
85 }
86 
87 //---------------------- RPrepareVisitor -------------------------------
88 
89 
90 void ROOT::Experimental::RPrepareVisitor::VisitField(const Detail::RFieldBase &/*field*/, int level)
91 {
92  ++fNumFields;
93  if (level > fDeepestLevel)
94  fDeepestLevel = level;
95 }
96 
97 //------------------------ RNTupleFormatter -----------------------------
98 
99 //E.g. ("ExampleString" , space= 8) => "Examp..."
100 std::string ROOT::Experimental::RNTupleFormatter::FitString(const std::string &str, int availableSpace) {
101  int strSize{static_cast<int>(str.size())};
102  if (strSize <= availableSpace)
103  return str + std::string(availableSpace - strSize, ' ');
104  else if (availableSpace < 3)
105  return std::string(availableSpace, '.');
106  return std::string(str, 0, availableSpace - 3) + "...";
107 }
108 
109 // Returns std::string of form "1" or "2.1.1"
110 std::string ROOT::Experimental::RNTupleFormatter::HierarchialFieldOrder(const ROOT::Experimental::Detail::RFieldBase &field)
111 {
112  std::string hierarchialOrder{std::to_string(field.GetLevelInfo().GetOrder())};
113  const ROOT::Experimental::Detail::RFieldBase* parentPtr{field.GetParent()};
114  // To avoid having the index of the RootField (-1) in the return value, it is checked if the grandparent is a nullptr (in that case RootField is parent)
115  while (parentPtr && (parentPtr->GetLevelInfo().GetOrder() != -1)) {
116  hierarchialOrder = std::to_string(parentPtr->GetLevelInfo().GetOrder()) + "." + hierarchialOrder;
117  parentPtr = parentPtr->GetParent();
118  }
119  return hierarchialOrder;
120 }