Logo ROOT   6.16/01
Reference Guide
RDisplay.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, Danilo Piparo CERN, Massimo Tumolo Politecnico di Torino 08/2018
2
3/*************************************************************************
4 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#ifndef ROOT_RDFDISPLAYER
12#define ROOT_RDFDISPLAYER
13
14#include "ROOT/TypeTraits.hxx"
15#include "TClassEdit.h"
16
17#include <vector>
18#include <string>
19#include <iostream>
20#include <sstream>
21
22namespace ROOT {
23namespace Internal {
24namespace RDF {
25
26template<typename T>
28std::string PrettyPrintAddr(const void *const addr);
29
31private:
33 std::string fRepresentation;
35
36public:
37 RDisplayElement(const std::string &representation);
39 void SetPrint();
40 void SetIgnore();
41 void SetDots();
42 bool IsPrint() const;
43 bool IsIgnore() const;
44 bool IsDot() const;
45 const std::string &GetRepresentation() const;
46 bool IsEmpty() const;
47};
48} // namespace RDF
49} // namespace Internal
50
51namespace RDF {
52
53/**
54 * \class ROOT::RDF::RDisplay
55 * \ingroup dataframe
56 * This class is the textual representation of the content of a columnar dataset.
57 *
58 * This class is provided to the user, and it can be used to print on screen
59 * the entries of the dataset requested through the Display action in a compact
60 * representation or to return the full representation of the events as a string.
61 * In order to apply proper formatting the content is buffered in memory as strings.
62 */
63class RDisplay {
64 template<typename T>
66private:
67 using VecStr_t = std::vector<std::string>;
69 static constexpr char fgSeparator = ' '; ///< Spacing used to align the table entries
70 static constexpr unsigned fgMaxWidth = 80;
71
72 VecStr_t fTypes; ///< This attribute stores the type of each column. It is needed by the interpreter to print it.
73 std::vector<bool> fIsCollection; ///< True if the column contains a collection. Collections are treated differently
74 ///< during the printing.
75 std::vector<std::vector<DElement_t>> fTable; ///< String representation of the data to be printed.
76 std::vector<unsigned short> fWidths; ///< Tracks the maximum width of each column, based on the largest element.
77
78 VecStr_t fRepresentations; ///< Used by the JITted code to store the string representation of the data.
79 std::vector<VecStr_t> fCollectionsRepresentations; ///< Used by the JITted code to store the string representation of
80 ///< the data in case of collection. Each row corresponds to a
81 ///< column, each column to a value of the collection.
82
83 size_t fNColumns; ///< Number of columns to be printed
84
85 size_t fCurrentRow = 0; ///< Row that is being filled
86 size_t fNextRow = 1; ///< Next row to be filled.
87 size_t fCurrentColumn = 0; ///< Column that is being filled.
88
89 size_t fEntries; ///< Number of events to process for each column (i.e. number of rows).
90
91 ////////////////////////////////////////////////////////////////////////////
92 /// Appends a cling::printValue call to the stringstream.
93 /// \tparam T the type of the event to convert
94 /// \param[in] stream Where the conversion function call will be chained.
95 /// \param[in] element The event to convert to its string representation
96 /// \param[in] index To which column the event belongs to
97 /// \return false, the event is not a collection
98 template <typename T, typename std::enable_if<!ROOT::TypeTraits::IsContainer<T>::value, int>::type = 0>
99 bool AddInterpreterString(std::stringstream &stream, T &element, const int &index)
100 {
101 stream << "*((std::string*)" << ROOT::Internal::RDF::PrettyPrintAddr(&(fRepresentations[index]))
102 << ") = cling::printValue((" << fTypes[index] << "*)" << ROOT::Internal::RDF::PrettyPrintAddr(&element) << ");";
103 return false;
104 }
105
106 ////////////////////////////////////////////////////////////////////////////
107 /// Appends collection.size() cling::printValue call to the stringstream.
108 /// \tparam T the type of the event to convert
109 /// \param[in] stream Where the conversion function call will be chained.
110 /// \param[in] element The event to convert to its string representation
111 /// \param[in] index To which column the event belongs to
112 /// \return true, the event is a collection
113 /// This function chains a sequence of call to cling::printValue, one for each element of the collection.
114 template <typename T, typename std::enable_if<ROOT::TypeTraits::IsContainer<T>::value, int>::type = 0>
115 bool AddInterpreterString(std::stringstream &stream, T &collection, const int &index)
116 {
117 size_t collectionSize = std::distance(std::begin(collection), std::end(collection));
118 // Prepare the row to contain as many elements as the number of elements in the collection
119 fCollectionsRepresentations[index] = VecStr_t(collectionSize);
120
121 // Use GetSplit to get the encapsulated type of the collection. For example, GetSplit on
122 // std::vector<std::vector<int>> will return std::vector<int>
124 int nestedLoc = 0;
125 TClassEdit::GetSplit(fTypes[index].c_str(), output, nestedLoc);
126
127 // For each element, append a call and feed the proper type returned by GetSplit
128 for (size_t i = 0; i < collectionSize; ++i) {
129 stream << "*((std::string*)" << ROOT::Internal::RDF::PrettyPrintAddr(&(fCollectionsRepresentations[index][i]))
130 << ") = cling::printValue((" << output[1] << "*)" << ROOT::Internal::RDF::PrettyPrintAddr(&(collection[i])) << ");";
131 }
132 return true;
133 }
134
135 ////////////////////////////////////////////////////////////////////////////
136 /// Adds a single element to the next slot in the table
137 void AddToRow(const std::string &stringEle);
138
139 ////////////////////////////////////////////////////////////////////////////
140 /// Adds a collection to the table
141 ///
142 /// Starting from the slot, the elements are added one under the other, each
143 /// one using a single cell of an entire row
145
146 ////////////////////////////////////////////////////////////////////////////
147 /// Moves to the next cell
148 ///
149 /// Moves to the next cell, and if the row is full moves to the next row.
150 void MovePosition();
151
152 ////////////////////////////////////////////////////////////////////////////
153 /// Feed a piece of code to cling and handle errors
154 void CallInterpreter(const std::string &code);
155
156 ////////////////////////////////////////////////////////////////////////////
157 /// Get the number of columns that do NOT fit in the characters limit
158 size_t GetNColumnsToShorten() const;
159
160 ////////////////////////////////////////////////////////////////////////////
161 /// Adds a row of events to the table
162 template <typename... Columns>
163 void AddRow(Columns... columns)
164 {
165 std::stringstream calc; // JITted code
166 int columnIndex = 0;
167 // Unwrapping the parameters to create the JITted code.
168 fIsCollection = {AddInterpreterString(calc, columns, columnIndex++)...};
169
170 // Let cling::printValue handle the conversion. This can be done only through cling-compiled code.
171 CallInterpreter(calc.str());
172
173 // Populate the fTable using the results of the JITted code.
174 for (size_t i = 0; i < fNColumns; ++i) {
175 if (fIsCollection[i]) {
177 } else {
179 }
180 }
181 // This row has been parsed
182 fEntries--;
183 }
184
185 ////////////////////////////////////////////////////////////////////////////
186 /// If the number of required rows has been parsed, returns false.
187 bool HasNext() { return fEntries > 0; }
188
189public:
190 ////////////////////////////////////////////////////////////////////////////
191 /// Creates an RDisplay to print the event values
192 /// \param[in] columnNames Columns to print
193 /// \param[in] types The type of each column
194 /// \param[in] entries How many events per column (row) must be processed.
195 RDisplay(const VecStr_t &columnNames, const VecStr_t &types, int entries);
196
197 ////////////////////////////////////////////////////////////////////////////
198 /// Prints the representation to the standard output
199 ///
200 /// Collections are shortened to the first and last element. The overall width
201 /// is shortened to a fixed size of TODO
202 void Print() const;
203
204 ////////////////////////////////////////////////////////////////////////////
205 /// Returns the representation as a string
206 std::string AsString() const;
207};
208
209} // namespace RDF
210} // namespace ROOT
211
212#endif
int type
Definition: TGX11.cxx:120
Helper class to let Display print compact tabular representations of the events.
Definition: RDisplay.hxx:30
bool IsIgnore() const
Return if the cell has to be skipped.
Definition: RDFDisplay.cxx:66
bool IsDot() const
Return if the cell has to be replaced by "...".
Definition: RDFDisplay.cxx:73
const std::string & GetRepresentation() const
Definition: RDFDisplay.cxx:78
bool IsPrint() const
Return if the cell has to be printed.
Definition: RDFDisplay.cxx:59
void SetDots()
Flag this cell to be replaced by "...".
Definition: RDFDisplay.cxx:52
void SetPrint()
Flag this cell as to be printed.
Definition: RDFDisplay.cxx:38
void SetIgnore()
Flag this cell as to be skipped.
Definition: RDFDisplay.cxx:45
RDisplayElement()
Constructor assuming an empty representation to be printed.
Definition: RDFDisplay.cxx:31
This class is the textual representation of the content of a columnar dataset.
Definition: RDisplay.hxx:63
void AddRow(Columns... columns)
Adds a row of events to the table.
Definition: RDisplay.hxx:163
size_t fEntries
Number of events to process for each column (i.e. number of rows).
Definition: RDisplay.hxx:89
void AddCollectionToRow(const VecStr_t &collection)
Adds a collection to the table.
Definition: RDFDisplay.cxx:105
size_t fCurrentColumn
Column that is being filled.
Definition: RDisplay.hxx:87
bool HasNext()
If the number of required rows has been parsed, returns false.
Definition: RDisplay.hxx:187
std::vector< std::vector< DElement_t > > fTable
String representation of the data to be printed.
Definition: RDisplay.hxx:75
std::vector< bool > fIsCollection
True if the column contains a collection.
Definition: RDisplay.hxx:73
std::string AsString() const
Returns the representation as a string.
Definition: RDFDisplay.cxx:249
void AddToRow(const std::string &stringEle)
Adds a single element to the next slot in the table.
Definition: RDFDisplay.cxx:92
size_t fCurrentRow
Row that is being filled.
Definition: RDisplay.hxx:85
VecStr_t fRepresentations
Used by the JITted code to store the string representation of the data.
Definition: RDisplay.hxx:78
void MovePosition()
Moves to the next cell.
Definition: RDFDisplay.cxx:147
std::vector< unsigned short > fWidths
Tracks the maximum width of each column, based on the largest element.
Definition: RDisplay.hxx:76
bool AddInterpreterString(std::stringstream &stream, T &collection, const int &index)
Appends collection.size() cling::printValue call to the stringstream.
Definition: RDisplay.hxx:115
static constexpr unsigned fgMaxWidth
Definition: RDisplay.hxx:70
RDisplay(const VecStr_t &columnNames, const VecStr_t &types, int entries)
Creates an RDisplay to print the event values.
Definition: RDFDisplay.cxx:169
void CallInterpreter(const std::string &code)
Feed a piece of code to cling and handle errors.
Definition: RDFDisplay.cxx:159
size_t fNextRow
Next row to be filled.
Definition: RDisplay.hxx:86
std::vector< std::string > VecStr_t
Definition: RDisplay.hxx:67
void Print() const
Prints the representation to the standard output.
Definition: RDFDisplay.cxx:196
std::vector< VecStr_t > fCollectionsRepresentations
Used by the JITted code to store the string representation of the data in case of collection.
Definition: RDisplay.hxx:79
size_t GetNColumnsToShorten() const
Get the number of columns that do NOT fit in the characters limit.
Definition: RDFDisplay.cxx:181
static constexpr char fgSeparator
Spacing used to align the table entries.
Definition: RDisplay.hxx:69
bool AddInterpreterString(std::stringstream &stream, T &element, const int &index)
Appends a cling::printValue call to the stringstream.
Definition: RDisplay.hxx:99
VecStr_t fTypes
This attribute stores the type of each column. It is needed by the interpreter to print it.
Definition: RDisplay.hxx:72
size_t fNColumns
Number of columns to be printed.
Definition: RDisplay.hxx:83
void collection()
Definition: collection.C:252
std::string PrettyPrintAddr(const void *const addr)
double T(double x)
Definition: ChebyshevPol.h:34
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
RooCmdArg Columns(Int_t ncol)
int GetSplit(const char *type, std::vector< std::string > &output, int &nestedLoc, EModType mode=TClassEdit::kNone)
Stores in output (after emptying it) the split type.
Definition: TClassEdit.cxx:948