Logo ROOT   6.14/05
Reference Guide
RCsvDS.hxx
Go to the documentation of this file.
1 // Author: Enric Tejedor CERN 10/2017
2 
3 /*************************************************************************
4  * Copyright (C) 1995-2017, 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_RCSVTDS
12 #define ROOT_RCSVTDS
13 
14 #include "ROOT/RDataFrame.hxx"
15 #include "ROOT/RDataSource.hxx"
16 
17 #include <deque>
18 #include <list>
19 #include <map>
20 #include <vector>
21 
22 #include <TRegexp.h>
23 
24 namespace ROOT {
25 
26 namespace RDF {
27 
28 class RCsvDS final : public ROOT::RDF::RDataSource {
29 
30 private:
31  // Possible values are d, b, l, s. This is possible only because we treat double, bool, Long64_t and string
32  using ColType_t = char;
33  static const std::map<ColType_t, std::string> fgColTypeMap;
34 
35  unsigned int fNSlots = 0U;
36  std::ifstream fStream;
37  const char fDelimiter;
40  ULong64_t fProcessedLines = 0ULL; // marks the progress of the consumption of the csv lines
41  std::vector<std::string> fHeaders;
42  std::map<std::string, ColType_t> fColTypes;
43  std::list<ColType_t> fColTypesList;
44  std::vector<std::vector<void *>> fColAddresses; // fColAddresses[column][slot]
45  std::vector<Record_t> fRecords; // fRecords[entry][column]
46  std::vector<std::vector<double>> fDoubleEvtValues; // one per column per slot
47  std::vector<std::vector<Long64_t>> fLong64EvtValues; // one per column per slot
48  std::vector<std::vector<std::string>> fStringEvtValues; // one per column per slot
49  // This must be a deque to avoid the specialisation vector<bool>. This would not
50  // work given that the pointer to the boolean in that case cannot be taken
51  std::vector<std::deque<bool>> fBoolEvtValues; // one per column per slot
52 
54 
55  void FillHeaders(const std::string &);
56  void FillRecord(const std::string &, Record_t &);
57  void GenerateHeaders(size_t);
58  std::vector<void *> GetColumnReadersImpl(std::string_view, const std::type_info &);
59  void InferColTypes(std::vector<std::string> &);
60  void InferType(const std::string &, unsigned int);
61  std::vector<std::string> ParseColumns(const std::string &);
62  size_t ParseValue(const std::string &, std::vector<std::string> &, size_t);
63  ColType_t GetType(std::string_view colName) const;
64 
65 public:
66  RCsvDS(std::string_view fileName, bool readHeaders = true, char delimiter = ',', Long64_t linesChunkSize = -1LL);
67  void FreeRecords();
68  ~RCsvDS();
69  const std::vector<std::string> &GetColumnNames() const;
70  std::vector<std::pair<ULong64_t, ULong64_t>> GetEntryRanges();
71  std::string GetTypeName(std::string_view colName) const;
72  bool HasColumn(std::string_view colName) const;
73  bool SetEntry(unsigned int slot, ULong64_t entry);
74  void SetNSlots(unsigned int nSlots);
75 };
76 
77 ////////////////////////////////////////////////////////////////////////////////////////////////
78 /// \brief Factory method to create a CSV RDataFrame.
79 /// \param[in] fileName Path of the CSV file.
80 /// \param[in] readHeaders `true` if the CSV file contains headers as first row, `false` otherwise
81 /// (default `true`).
82 /// \param[in] delimiter Delimiter character (default ',').
83 RDataFrame MakeCsvDataFrame(std::string_view fileName, bool readHeaders = true, char delimiter = ',', Long64_t linesChunkSize = -1LL);
84 
85 } // ns RDF
86 
87 } // ns ROOT
88 
89 #endif
std::vector< std::string > ParseColumns(const std::string &)
Definition: RCsvDS.cxx:209
std::vector< std::string > fHeaders
Definition: RCsvDS.hxx:41
size_t ParseValue(const std::string &, std::vector< std::string > &, size_t)
Definition: RCsvDS.cxx:220
void FillRecord(const std::string &, Record_t &)
Definition: RCsvDS.cxx:106
RDataFrame MakeCsvDataFrame(std::string_view fileName, bool readHeaders=true, char delimiter=',', Long64_t linesChunkSize=-1LL)
Factory method to create a CSV RDataFrame.
Definition: RCsvDS.cxx:439
long long Long64_t
Definition: RtypesCore.h:69
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
const char fDelimiter
Definition: RCsvDS.hxx:37
void FreeRecords()
Definition: RCsvDS.cxx:286
static TRegexp doubleRegex1
Definition: RCsvDS.hxx:53
static TRegexp trueRegex
Definition: RCsvDS.hxx:53
void SetNSlots(unsigned int nSlots)
Inform RDataSource of the number of processing slots (i.e.
Definition: RCsvDS.cxx:422
static TRegexp doubleRegex2
Definition: RCsvDS.hxx:53
std::vector< std::vector< void * > > fColAddresses
Definition: RCsvDS.hxx:44
Regular expression class.
Definition: TRegexp.h:31
std::vector< std::vector< double > > fDoubleEvtValues
Definition: RCsvDS.hxx:46
void InferType(const std::string &, unsigned int)
Definition: RCsvDS.cxx:189
ULong64_t fEntryRangesRequested
Definition: RCsvDS.hxx:39
static TRegexp intRegex
Definition: RCsvDS.hxx:53
std::map< std::string, ColType_t > fColTypes
Definition: RCsvDS.hxx:42
static TRegexp falseRegex
Definition: RCsvDS.hxx:53
bool SetEntry(unsigned int slot, ULong64_t entry)
Advance the "cursors" returned by GetColumnReaders to the selected entry for a particular slot...
Definition: RCsvDS.cxx:391
RDataFrame data source class for reading CSV files.
Definition: RCsvDS.hxx:28
bool HasColumn(std::string_view colName) const
Checks if the dataset has a certain column.
Definition: RCsvDS.cxx:386
std::ifstream fStream
Definition: RCsvDS.hxx:36
std::string GetTypeName(std::string_view colName) const
Type of a column as a string, e.g.
Definition: RCsvDS.cxx:381
RCsvDS(std::string_view fileName, bool readHeaders=true, char delimiter=',', Long64_t linesChunkSize=-1LL)
Constructor to create a CSV RDataSource for RDataFrame.
Definition: RCsvDS.cxx:251
std::vector< std::vector< Long64_t > > fLong64EvtValues
Definition: RCsvDS.hxx:47
void InferColTypes(std::vector< std::string > &)
Definition: RCsvDS.cxx:180
unsigned int fNSlots
Definition: RCsvDS.hxx:35
const Long64_t fLinesChunkSize
Definition: RCsvDS.hxx:38
std::vector< Record_t > fRecords
Definition: RCsvDS.hxx:45
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges()
Return ranges of entries to distribute to tasks.
Definition: RCsvDS.cxx:327
ULong64_t fProcessedLines
Definition: RCsvDS.hxx:40
void GenerateHeaders(size_t)
Definition: RCsvDS.cxx:141
std::vector< std::vector< std::string > > fStringEvtValues
Definition: RCsvDS.hxx:48
ROOT&#39;s RDataFrame offers a high level interface for analyses of data stored in TTrees, CSV&#39;s and other data formats.
Definition: RDataFrame.hxx:42
std::vector< void * > Record_t
Definition: RDataSource.hxx:94
unsigned long long ULong64_t
Definition: RtypesCore.h:70
~RCsvDS()
Destructor.
Definition: RCsvDS.cxx:317
basic_string_view< char > string_view
Definition: RStringView.hxx:35
const std::vector< std::string > & GetColumnNames() const
Returns a reference to the collection of the dataset&#39;s column names.
Definition: RCsvDS.cxx:322
void FillHeaders(const std::string &)
Definition: RCsvDS.cxx:98
static const std::map< ColType_t, std::string > fgColTypeMap
Definition: RCsvDS.hxx:33
std::vector< void * > GetColumnReadersImpl(std::string_view, const std::type_info &)
type-erased vector of pointers to pointers to column values - one per slot
Definition: RCsvDS.cxx:148
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
Definition: RDataSource.hxx:91
std::vector< std::deque< bool > > fBoolEvtValues
Definition: RCsvDS.hxx:51
std::list< ColType_t > fColTypesList
Definition: RCsvDS.hxx:43
ColType_t GetType(std::string_view colName) const
Definition: RCsvDS.cxx:370