Logo ROOT  
Reference Guide
Go to the documentation of this file.
1// Author: Enric Tejedor CERN 10/2017
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 *************************************************************************/
11#ifndef ROOT_RCSVTDS
12#define ROOT_RCSVTDS
14#include "ROOT/RDataFrame.hxx"
15#include "ROOT/RDataSource.hxx"
17#include <cstdint>
18#include <deque>
19#include <list>
20#include <map>
21#include <memory>
22#include <vector>
24#include <TRegexp.h>
26namespace ROOT {
28namespace Internal {
29class RRawFile;
32namespace RDF {
34class RCsvDS final : public ROOT::RDF::RDataSource {
37 // Possible values are d, b, l, s. This is possible only because we treat double, bool, Long64_t and string
38 using ColType_t = char;
39 static const std::map<ColType_t, std::string> fgColTypeMap;
41 // Regular expressions for type inference
44 std::uint64_t fDataPos = 0;
45 bool fReadHeaders = false;
46 unsigned int fNSlots = 0U;
47 std::unique_ptr<ROOT::Internal::RRawFile> fCsvFile;
48 const char fDelimiter;
51 ULong64_t fProcessedLines = 0ULL; // marks the progress of the consumption of the csv lines
52 std::vector<std::string> fHeaders;
53 std::map<std::string, ColType_t> fColTypes;
54 std::list<ColType_t> fColTypesList;
55 std::vector<std::vector<void *>> fColAddresses; // fColAddresses[column][slot]
56 std::vector<Record_t> fRecords; // fRecords[entry][column]
57 std::vector<std::vector<double>> fDoubleEvtValues; // one per column per slot
58 std::vector<std::vector<Long64_t>> fLong64EvtValues; // one per column per slot
59 std::vector<std::vector<std::string>> fStringEvtValues; // one per column per slot
60 // This must be a deque to avoid the specialisation vector<bool>. This would not
61 // work given that the pointer to the boolean in that case cannot be taken
62 std::vector<std::deque<bool>> fBoolEvtValues; // one per column per slot
64 void FillHeaders(const std::string &);
65 void FillRecord(const std::string &, Record_t &);
66 void GenerateHeaders(size_t);
67 std::vector<void *> GetColumnReadersImpl(std::string_view, const std::type_info &);
68 void InferColTypes(std::vector<std::string> &);
69 void InferType(const std::string &, unsigned int);
70 std::vector<std::string> ParseColumns(const std::string &);
71 size_t ParseValue(const std::string &, std::vector<std::string> &, size_t);
72 ColType_t GetType(std::string_view colName) const;
75 std::string AsString();
78 RCsvDS(std::string_view fileName, bool readHeaders = true, char delimiter = ',', Long64_t linesChunkSize = -1LL);
79 void Finalise();
80 void FreeRecords();
81 ~RCsvDS();
82 const std::vector<std::string> &GetColumnNames() const;
83 std::vector<std::pair<ULong64_t, ULong64_t>> GetEntryRanges();
84 std::string GetTypeName(std::string_view colName) const;
85 bool HasColumn(std::string_view colName) const;
86 bool SetEntry(unsigned int slot, ULong64_t entry);
87 void SetNSlots(unsigned int nSlots);
88 std::string GetLabel();
92/// \brief Factory method to create a CSV RDataFrame.
93/// \param[in] fileName Path of the CSV file.
94/// \param[in] readHeaders `true` if the CSV file contains headers as first row, `false` otherwise
95/// (default `true`).
96/// \param[in] delimiter Delimiter character (default ',').
97RDataFrame MakeCsvDataFrame(std::string_view fileName, bool readHeaders = true, char delimiter = ',',
98 Long64_t linesChunkSize = -1LL);
100} // ns RDF
102} // ns ROOT
long long Long64_t
Definition: RtypesCore.h:80
unsigned long long ULong64_t
Definition: RtypesCore.h:81
RDataFrame data source class for reading CSV files.
Definition: RCsvDS.hxx:34
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:261
std::map< std::string, ColType_t > fColTypes
Definition: RCsvDS.hxx:53
void FillRecord(const std::string &, Record_t &)
Definition: RCsvDS.cxx:114
ColType_t GetType(std::string_view colName) const
Definition: RCsvDS.cxx:401
std::vector< std::vector< double > > fDoubleEvtValues
Definition: RCsvDS.hxx:57
void InferType(const std::string &, unsigned int)
Definition: RCsvDS.cxx:197
std::uint64_t fDataPos
Definition: RCsvDS.hxx:44
size_t ParseValue(const std::string &, std::vector< std::string > &, size_t)
Definition: RCsvDS.cxx:230
static const TRegexp fgTrueRegex
Definition: RCsvDS.hxx:42
void GenerateHeaders(size_t)
Definition: RCsvDS.cxx:149
std::vector< std::vector< void * > > fColAddresses
Definition: RCsvDS.hxx:55
unsigned int fNSlots
Definition: RCsvDS.hxx:46
std::string GetLabel()
Return a string representation of the datasource type.
Definition: RCsvDS.cxx:470
const Long64_t fLinesChunkSize
Definition: RCsvDS.hxx:49
std::vector< std::string > fHeaders
Definition: RCsvDS.hxx:52
ULong64_t fEntryRangesRequested
Definition: RCsvDS.hxx:50
const std::vector< std::string > & GetColumnNames() const
Returns a reference to the collection of the dataset's column names.
Definition: RCsvDS.cxx:349
ULong64_t fProcessedLines
Definition: RCsvDS.hxx:51
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:156
void InferColTypes(std::vector< std::string > &)
Definition: RCsvDS.cxx:188
std::vector< std::vector< Long64_t > > fLong64EvtValues
Definition: RCsvDS.hxx:58
static const std::map< ColType_t, std::string > fgColTypeMap
Definition: RCsvDS.hxx:39
const char fDelimiter
Definition: RCsvDS.hxx:48
static const TRegexp fgDoubleRegex2
Definition: RCsvDS.hxx:42
std::vector< Record_t > fRecords
Definition: RCsvDS.hxx:56
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges()
Return ranges of entries to distribute to tasks.
Definition: RCsvDS.cxx:354
static const TRegexp fgFalseRegex
Definition: RCsvDS.hxx:42
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:422
bool HasColumn(std::string_view colName) const
Checks if the dataset has a certain column.
Definition: RCsvDS.cxx:417
static const TRegexp fgDoubleRegex3
Definition: RCsvDS.hxx:42
void SetNSlots(unsigned int nSlots)
Inform RDataSource of the number of processing slots (i.e.
Definition: RCsvDS.cxx:453
static const TRegexp fgIntRegex
Definition: RCsvDS.hxx:42
std::string GetTypeName(std::string_view colName) const
Type of a column as a string, e.g.
Definition: RCsvDS.cxx:412
std::vector< std::string > ParseColumns(const std::string &)
Definition: RCsvDS.cxx:219
void FillHeaders(const std::string &)
Definition: RCsvDS.cxx:106
std::unique_ptr< ROOT::Internal::RRawFile > fCsvFile
Definition: RCsvDS.hxx:47
static const TRegexp fgDoubleRegex1
Definition: RCsvDS.hxx:42
std::string AsString()
Definition: RCsvDS.cxx:90
void Finalise()
Convenience method called after concluding an event-loop.
Definition: RCsvDS.cxx:341
std::vector< std::vector< std::string > > fStringEvtValues
Definition: RCsvDS.hxx:59
std::vector< std::deque< bool > > fBoolEvtValues
Definition: RCsvDS.hxx:62
void FreeRecords()
Definition: RCsvDS.cxx:305
Definition: RCsvDS.cxx:336
std::list< ColType_t > fColTypesList
Definition: RCsvDS.hxx:54
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
std::vector< void * > Record_t
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTree,...
Definition: RDataFrame.hxx:40
Regular expression class.
Definition: TRegexp.h:31
basic_string_view< char > string_view
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:475
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...