91 return "CSV data source";
102const std::map<RCsvDS::ColType_t, std::string>
103 RCsvDS::fgColTypeMap({{
'b',
"bool"}, {
'd',
"double"}, {
'l',
"Long64_t"}, {
's',
"std::string"}});
108 for (
auto &col : columns) {
115 std::istringstream lineStream(
line);
120 for (
auto &col : columns) {
125 record.emplace_back(
new double(std::stod(col)));
129 record.emplace_back(
new Long64_t(std::stoll(col)));
134 record.emplace_back(
b);
135 std::istringstream is(col);
136 is >> std::boolalpha >> *
b;
140 record.emplace_back(
new std::string(col));
150 for (
size_t i = 0; i < size; ++i) {
151 fHeaders.push_back(
"Col" + std::to_string(i));
157 const auto colType =
GetType(colName);
159 if ((colType ==
'd' &&
typeid(
double) != ti) || (colType ==
'l' &&
typeid(
Long64_t) != ti) ||
160 (colType ==
's' &&
typeid(std::string) != ti) || (colType ==
'b' &&
typeid(
bool) != ti)) {
161 std::string err =
"The type selected for column \"";
163 err +=
"\" does not correspond to column type, which is ";
165 throw std::runtime_error(err);
169 const auto index = std::distance(colNames.begin(), std::find(colNames.begin(), colNames.end(), colName));
170 std::vector<void *> ret(
fNSlots);
173 if (ti ==
typeid(
double)) {
175 }
else if (ti ==
typeid(
Long64_t)) {
177 }
else if (ti ==
typeid(std::string)) {
190 for (
auto &col : columns) {
220 std::vector<std::string> columns;
222 for (
size_t i = 0; i <
line.size(); ++i) {
231 std::stringstream val;
234 for (; i <
line.size(); ++i) {
237 }
else if (
line[i] ==
'"') {
239 if (
line[i + 1] !=
'"') {
249 columns.emplace_back(val.str());
261 : fReadHeaders(readHeaders),
262 fStream(std::string(fileName)),
263 fDelimiter(delimiter),
264 fLinesChunkSize(linesChunkSize)
273 std::string msg =
"Error reading headers of CSV file ";
275 throw std::runtime_error(msg);
283 }
while (
line.empty());
298 std::string msg =
"Could not infer column types of CSV file ";
300 throw std::runtime_error(msg);
307 for (
size_t i = 0; i < record.size(); ++i) {
312 delete static_cast<double *
>(p);
320 delete static_cast<bool *
>(p);
324 delete static_cast<std::string *
>(p);
363 if (
line.empty())
continue;
371 Info(
"GetEntryRanges",
"Attempted to read entire CSV file into memory, %lu lines read",
fRecords.size());
373 Info(
"GetEntryRanges",
"Attempted to read chunk of %lld lines of CSV file into memory, %lu lines read",
fLinesChunkSize,
fRecords.size());
377 std::vector<std::pair<ULong64_t, ULong64_t>> entryRanges;
378 const auto nRecords =
fRecords.size();
382 const auto chunkSize = nRecords /
fNSlots;
390 entryRanges.emplace_back(start, end);
393 entryRanges.back().second += remainder;
404 std::string msg =
"The dataset does not have column ";
406 throw std::runtime_error(msg);
426 const auto recordPos = entry - offset;
429 auto dataPtr =
fRecords[recordPos][colIndex];
455 R__ASSERT(0U ==
fNSlots &&
"Setting the number of slots even if the number of slots is different from zero.");
459 const auto nColumns =
fHeaders.size();
477 ROOT::RDataFrame tdf(std::make_unique<RCsvDS>(fileName, readHeaders, delimiter, linesChunkSize));
static RooMathCoreReg dummy
unsigned long long ULong64_t
void Info(const char *location, const char *msgfmt,...)
typedef void((*Func_t)())
RCsvDS(std::string_view fileName, bool readHeaders=true, char delimiter=',', Long64_t linesChunkSize=-1LL)
Constructor to create a CSV RDataSource for RDataFrame.
std::map< std::string, ColType_t > fColTypes
static TRegexp falseRegex
void FillRecord(const std::string &, Record_t &)
ColType_t GetType(std::string_view colName) const
std::vector< std::vector< double > > fDoubleEvtValues
void InferType(const std::string &, unsigned int)
size_t ParseValue(const std::string &, std::vector< std::string > &, size_t)
void GenerateHeaders(size_t)
std::vector< std::vector< void * > > fColAddresses
std::string GetLabel()
Return a string representation of the datasource type.
const Long64_t fLinesChunkSize
std::vector< std::string > fHeaders
static TRegexp doubleRegex2
ULong64_t fEntryRangesRequested
const std::vector< std::string > & GetColumnNames() const
Returns a reference to the collection of the dataset's column names.
ULong64_t fProcessedLines
std::vector< void * > GetColumnReadersImpl(std::string_view, const std::type_info &)
type-erased vector of pointers to pointers to column values - one per slot
void InferColTypes(std::vector< std::string > &)
std::vector< std::vector< Long64_t > > fLong64EvtValues
static const std::map< ColType_t, std::string > fgColTypeMap
std::vector< Record_t > fRecords
static TRegexp doubleRegex3
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges()
Return ranges of entries to distribute to tasks.
bool SetEntry(unsigned int slot, ULong64_t entry)
Advance the "cursors" returned by GetColumnReaders to the selected entry for a particular slot.
bool HasColumn(std::string_view colName) const
Checks if the dataset has a certain column.
static TRegexp doubleRegex1
void SetNSlots(unsigned int nSlots)
Inform RDataSource of the number of processing slots (i.e.
std::string GetTypeName(std::string_view colName) const
Type of a column as a string, e.g.
std::vector< std::string > ParseColumns(const std::string &)
void FillHeaders(const std::string &)
void Finalise()
Convenience method called after concluding an event-loop.
std::vector< std::vector< std::string > > fStringEvtValues
std::vector< std::deque< bool > > fBoolEvtValues
std::list< ColType_t > fColTypesList
std::vector< void * > Record_t
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
A pseudo container class which is a generator of indices.
Regular expression class.
Ssiz_t Index(const TString &str, Ssiz_t *len, Ssiz_t start=0) const
Find the first occurrence of the regexp in string and return the position, or -1 if there is no match...
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.