Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RLazyDSImpl.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, Danilo Piparo CERN 02/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_RLAZYDSIMPL
12#define ROOT_RLAZYDSIMPL
13
14#include "ROOT/RDataSource.hxx"
15#include "ROOT/RResultPtr.hxx"
16#include "ROOT/TSeq.hxx"
17
18#include <algorithm>
19#include <map>
20#include <memory>
21#include <tuple>
22#include <string>
23#include <typeinfo>
24#include <utility> // std::index_sequence
25#include <vector>
26
27namespace ROOT {
28
29namespace RDF {
30////////////////////////////////////////////////////////////////////////////////////////////////
31/// \brief A RDataSource implementation which is built on top of result proxies
32///
33/// This component allows to create a data source on a set of columns coming from
34/// one or multiple data frames. The processing of the parent data frames starts
35/// only when the event loop is triggered in the data frame initialized with a
36/// RLazyDS.
37///
38/// The implementation takes care of matching compile time information with runtime
39/// information, e.g. expanding in a smart way the template parameters packs.
40template <typename... ColumnTypes>
42 using PointerHolderPtrs_t = std::vector<ROOT::Internal::TDS::TPointerHolder *>;
43
44 std::tuple<RResultPtr<std::vector<ColumnTypes>>...> fColumns;
45 const std::vector<std::string> fColNames;
46 const std::map<std::string, std::string> fColTypesMap;
47 // The role of the fPointerHoldersModels is to be initialized with the pack
48 // of arguments in the constrcutor signature at construction time
49 // Once the number of slots is known, the fPointerHolders are initialized
50 // according to the models.
52 std::vector<PointerHolderPtrs_t> fPointerHolders;
53 std::vector<std::pair<ULong64_t, ULong64_t>> fEntryRanges{};
54 unsigned int fNSlots{0};
55
56 Record_t GetColumnReadersImpl(std::string_view colName, const std::type_info &id) final
57 {
58 auto colNameStr = std::string(colName);
59 // This could be optimised and done statically
61 auto it = fColTypesMap.find(colNameStr);
62 if (fColTypesMap.end() == it) {
63 std::string err = "The specified column name, \"" + colNameStr + "\" is not known to the data source.";
64 throw std::runtime_error(err);
65 }
66
67 const auto colIdName = it->second;
68 if (colIdName != idName) {
69 std::string err = "Column " + colNameStr + " has type " + colIdName +
70 " while the id specified is associated to type " + idName;
71 throw std::runtime_error(err);
72 }
73
74 const auto colBegin = fColNames.begin();
75 const auto colEnd = fColNames.end();
76 const auto namesIt = std::find(colBegin, colEnd, colName);
77 const auto index = std::distance(colBegin, namesIt);
78
80 for (auto slot : ROOT::TSeqU(fNSlots)) {
81 ret[slot] = fPointerHolders[index][slot]->GetPointerAddr();
82 }
83 return ret;
84 }
85
86 size_t GetEntriesNumber() { return std::get<0>(fColumns)->size(); }
87 template <std::size_t... S>
88 void SetEntryHelper(unsigned int slot, ULong64_t entry, std::index_sequence<S...>)
89 {
90 std::initializer_list<int> expander{
91 (*static_cast<ColumnTypes *>(fPointerHolders[S][slot]->GetPointer()) = (*std::get<S>(fColumns))[entry], 0)...};
92 (void)expander; // avoid unused variable warnings
93 }
94
95 template <std::size_t... S>
96 void ColLengthChecker(std::index_sequence<S...>)
97 {
98 if (sizeof...(S) < 2)
99 return;
100
101 const std::vector<size_t> colLengths{std::get<S>(fColumns)->size()...};
102 const auto expectedLen = colLengths[0];
103 std::string err;
104 for (auto i : TSeqI(1, colLengths.size())) {
105 if (expectedLen != colLengths[i]) {
106 err += "Column \"" + fColNames[i] + "\" and column \"" + fColNames[0] +
107 "\" have different lengths: " + std::to_string(expectedLen) + " and " +
108 std::to_string(colLengths[i]);
109 }
110 }
111 if (!err.empty()) {
112 throw std::runtime_error(err);
113 }
114 }
115
116protected:
117 std::string AsString() final { return "lazy data source"; };
118
119public:
120 RLazyDS(std::pair<std::string, RResultPtr<std::vector<ColumnTypes>>>... colsNameVals)
121 : fColumns(std::tuple<RResultPtr<std::vector<ColumnTypes>>...>(colsNameVals.second...)),
122 fColNames({colsNameVals.first...}),
125 {
126 }
127
128 // Rule of five
129 RLazyDS(const RLazyDS &) = delete;
130 RLazyDS &operator=(const RLazyDS &) = delete;
131 RLazyDS(RLazyDS &&) = delete;
132 RLazyDS &operator=(RLazyDS &&) = delete;
134 {
135 for (auto &&ptrHolderv : fPointerHolders) {
136 for (auto &&ptrHolder : ptrHolderv) {
137 delete ptrHolder;
138 }
139 }
140 }
141
142 const std::vector<std::string> &GetColumnNames() const final { return fColNames; }
143
144 std::vector<std::pair<ULong64_t, ULong64_t>> GetEntryRanges() final
145 {
146 auto entryRanges(std::move(fEntryRanges)); // empty fEntryRanges
147 return entryRanges;
148 }
149
150 std::string GetTypeName(std::string_view colName) const final
151 {
152 const auto key = std::string(colName);
153 return fColTypesMap.at(key);
154 }
155
156 bool HasColumn(std::string_view colName) const final
157 {
158 const auto key = std::string(colName);
159 const auto endIt = fColTypesMap.end();
160 return endIt != fColTypesMap.find(key);
161 }
162
163 bool SetEntry(unsigned int slot, ULong64_t entry) final
164 {
165 SetEntryHelper(slot, entry, std::index_sequence_for<ColumnTypes...>());
166 return true;
167 }
168
169 void SetNSlots(unsigned int nSlots) final
170 {
171 fNSlots = nSlots;
172 const auto nCols = fColNames.size();
173 fPointerHolders.resize(nCols); // now we need to fill it with the slots, all of the same type
174 auto colIndex = 0U;
175 for (auto &&ptrHolderv : fPointerHolders) {
176 for (auto slot : ROOT::TSeqI(fNSlots)) {
177 auto ptrHolder = fPointerHoldersModels[colIndex]->GetDeepCopy();
178 ptrHolderv.emplace_back(ptrHolder);
179 (void)slot;
180 }
181 colIndex++;
182 }
183 for (auto &&ptrHolder : fPointerHoldersModels)
184 delete ptrHolder;
185 }
186
188 {
189 ColLengthChecker(std::index_sequence_for<ColumnTypes...>());
190 const auto nEntries = GetEntriesNumber();
191 const auto nEntriesInRange = nEntries / fNSlots; // between integers. Should make smaller?
192 auto reminder = 1U == fNSlots ? 0 : nEntries % fNSlots;
193 fEntryRanges.resize(fNSlots);
194 auto init = 0ULL;
195 auto end = 0ULL;
196 for (auto &&range : fEntryRanges) {
197 end = init + nEntriesInRange;
198 if (0 != reminder) { // Distribute the reminder among the first chunks
199 reminder--;
200 end += 1;
201 }
202 range.first = init;
203 range.second = end;
204 init = end;
205 }
206 }
207
208 std::string GetLabel() final { return "LazyDS"; }
209};
210
211} // ns RDF
212
213} // ns ROOT
214
215#endif
unsigned long long ULong64_t
Definition RtypesCore.h:70
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Class to wrap a pointer and delete the memory associated to it correctly.
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
std::vector< void * > Record_t
A RDataSource implementation which is built on top of result proxies.
RLazyDS(RLazyDS &&)=delete
const std::vector< std::string > fColNames
std::vector< PointerHolderPtrs_t > fPointerHolders
RLazyDS(const RLazyDS &)=delete
void SetNSlots(unsigned int nSlots) final
Inform RDataSource of the number of processing slots (i.e.
std::string GetLabel() final
Return a string representation of the datasource type.
std::vector< ROOT::Internal::TDS::TPointerHolder * > PointerHolderPtrs_t
const std::vector< std::string > & GetColumnNames() const final
Returns a reference to the collection of the dataset's column names.
RLazyDS & operator=(RLazyDS &&)=delete
std::string GetTypeName(std::string_view colName) const final
Type of a column as a string, e.g.
unsigned int fNSlots
void SetEntryHelper(unsigned int slot, ULong64_t entry, std::index_sequence< S... >)
const PointerHolderPtrs_t fPointerHoldersModels
const std::map< std::string, std::string > fColTypesMap
void ColLengthChecker(std::index_sequence< S... >)
bool HasColumn(std::string_view colName) const final
Checks if the dataset has a certain column.
std::string AsString() final
void Initialize() final
Convenience method called before starting an event-loop.
RLazyDS & operator=(const RLazyDS &)=delete
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges() final
Return ranges of entries to distribute to tasks.
size_t GetEntriesNumber()
RLazyDS(std::pair< std::string, RResultPtr< std::vector< ColumnTypes > > >... colsNameVals)
Record_t GetColumnReadersImpl(std::string_view colName, const std::type_info &id) final
type-erased vector of pointers to pointers to column values - one per slot
bool SetEntry(unsigned int slot, ULong64_t entry) final
Advance the "cursors" returned by GetColumnReaders to the selected entry for a particular slot.
std::tuple< RResultPtr< std::vector< ColumnTypes > >... > fColumns
std::vector< std::pair< ULong64_t, ULong64_t > > fEntryRanges
Smart pointer for the return type of actions.
const_iterator begin() const
const_iterator end() const
std::string TypeID2TypeName(const std::type_info &id)
Returns the name of a type starting from its type_info An empty string is returned in case of failure...
Definition RDFUtils.cxx:119
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
TSeq< int > TSeqI
Definition TSeq.hxx:203
TSeq< unsigned int > TSeqU
Definition TSeq.hxx:204