Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleDS.hxx
Go to the documentation of this file.
1/// \file RNTupleDS.hxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \author Enrico Guiraud <enrico.guiraud@cern.ch>
5/// \date 2018-10-04
6/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
7/// is welcome!
8
9/*************************************************************************
10 * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. *
11 * All rights reserved. *
12 * *
13 * For the licensing terms see $ROOTSYS/LICENSE. *
14 * For the list of contributors see $ROOTSYS/README/CREDITS. *
15 *************************************************************************/
16
17#ifndef ROOT_RNTupleDS
18#define ROOT_RNTupleDS
19
20#include <ROOT/RDataSource.hxx>
21#include <ROOT/RNTupleUtil.hxx>
23#include <string_view>
24
25#include <condition_variable>
26#include <cstdint>
27#include <memory>
28#include <mutex>
29#include <string>
30#include <thread>
31#include <vector>
32#include <unordered_map>
33
34namespace ROOT {
35class RFieldBase;
36class RDataFrame;
37class RNTuple;
38} // namespace ROOT
39namespace ROOT::Internal::RDF {
40class RNTupleColumnReader;
41}
42namespace ROOT::Internal {
43class RPageSource;
44}
45
46namespace ROOT::RDF {
49
50 /// The PrepareNextRanges() method populates the fNextRanges list with REntryRangeDS records.
51 /// The GetEntryRanges() swaps fNextRanges and fCurrentRanges and uses the list of
52 /// REntryRangeDS records to return the list of ranges ready to use by the RDF loop manager.
54 std::unique_ptr<ROOT::Internal::RPageSource> fSource;
55 ULong64_t fFirstEntry = 0; ///< First entry index in fSource
56 /// End entry index in fSource, e.g. the number of entries in the range is fLastEntry - fFirstEntry
58 };
59
60 /// A clone of the first pages source's descriptor.
62
63 /// The data source may be constructed with an ntuple name and a list of files
64 std::string fNTupleName;
65 std::vector<std::string> fFileNames;
66 /// The staging area is relevant for chains of files, i.e. when fFileNames is not empty. In this case,
67 /// files are opened in the background in batches of size `fNSlots` and kept in the staging area.
68 /// The first file (chains or no chains) is always opened on construction in order to process the schema.
69 /// For all subsequent files, the corresponding page sources in the staging area only executed `LoadStructure()`,
70 /// i.e. they should have a compressed buffer of the meta-data available.
71 /// Concretely:
72 /// 1. We open the first file on construction to read the schema and then move the corresponding page source
73 /// in the staging area.
74 /// 2. On `Initialize()`, we start the I/O background thread, which in turn opens the first batch of files.
75 /// 3. At the beginning of `GetEntryRanges()`, we
76 /// a) wait for the I/O thread to finish,
77 /// b) call `PrepareNextRanges()` in the main thread to move the page sources from the staging area
78 /// into `fNextRanges`; this will also call `Attach()` on the page sources (i.e., deserialize the meta-data),
79 /// and
80 /// c) trigger staging of the next batch of files in the I/O background thread.
81 /// 4. On `Finalize()`, the I/O background thread is stopped.
82 std::vector<std::unique_ptr<ROOT::Internal::RPageSource>> fStagingArea;
83 std::size_t fNextFileIndex = 0; ///< Index into fFileNames to the next file to process
84
85 /// We prepare a prototype field for every column. If a column reader is actually requested
86 /// in GetColumnReaders(), we move a clone of the field into a new column reader for RDataFrame.
87 /// Only the clone connects to the backing page store and acquires I/O resources.
88 /// The field IDs are set in the context of the first source and used as keys in fFieldId2QualifiedName.
89 std::vector<std::unique_ptr<ROOT::RFieldBase>> fProtoFields;
90 /// Columns may be requested with types other than with which they were initially added as proto fields. For example,
91 /// a column with a `ROOT::RVec<float>` proto field may instead be requested as a `std::vector<float>`. In case this
92 /// happens, we create an alternative proto field and store it here, with the original index in `fProtoFields` as
93 /// key. A single column can have more than one alternative proto fields.
94 std::unordered_map<std::size_t, std::vector<std::unique_ptr<ROOT::RFieldBase>>> fAlternativeProtoFields;
95 /// Connects the IDs of active proto fields and their subfields to their fully qualified name (a.b.c.d).
96 /// This enables the column reader to rewire the field IDs when the file changes (chain),
97 /// using the fully qualified name as a search key in the descriptor of the other page sources.
98 std::unordered_map<ROOT::DescriptorId_t, std::string> fFieldId2QualifiedName;
99 std::vector<std::string> fColumnNames;
100 std::vector<std::string> fColumnTypes;
101 /// List of column readers returned by GetColumnReaders() organized by slot. Used to reconnect readers
102 /// to new page sources when the files in the chain change.
103 std::vector<std::vector<ROOT::Internal::RDF::RNTupleColumnReader *>> fActiveColumnReaders;
104
105 ULong64_t fSeenEntries = 0; ///< The number of entries so far returned by GetEntryRanges()
106 std::vector<REntryRangeDS> fCurrentRanges; ///< Basis for the ranges returned by the last GetEntryRanges() call
107 std::vector<REntryRangeDS> fNextRanges; ///< Basis for the ranges populated by the PrepareNextRanges() call
108 /// Maps the first entries from the ranges of the last GetEntryRanges() call to their corresponding index in
109 /// the fCurrentRanges vectors. This is necessary because the returned ranges get distributed arbitrarily
110 /// onto slots. In the InitSlot method, the column readers use this map to find the correct range to connect to.
111 std::unordered_map<ULong64_t, std::size_t> fFirstEntry2RangeIdx;
112
113 /// The background thread that runs StageNextSources()
114 std::thread fThreadStaging;
115 /// Protects the shared state between the main thread and the I/O thread
116 std::mutex fMutexStaging;
117 /// Signal for the state information of fIsReadyForStaging and fHasNextSources
118 std::condition_variable fCvStaging;
119 /// Is true when the staging thread should start working
120 bool fIsReadyForStaging = false;
121 /// Is true when the staging thread has populated the next batch of files to fStagingArea
122 bool fHasNextSources = false;
123 /// Is true when the I/O thread should quit
125
126 /// \brief Holds useful information about fields added to the RNTupleDS
127 struct RFieldInfo {
129 std::size_t fNRepetitions;
130 // Enable `std::vector::emplace_back` for this type
135 };
136
137 /// Provides the RDF column "colName" given the field identified by fieldID. For records and collections,
138 /// AddField recurses into the sub fields. The fieldInfos argument is a list of objects holding info
139 /// about the fields of the outer collection(s) (w.r.t. fieldId). For instance, if fieldId refers to an
140 /// `std::vector<Jet>`, with
141 /// ~~~{.cpp}
142 /// struct Jet {
143 /// float pt;
144 /// float eta;
145 /// };
146 /// ~~~
147 /// AddField will recurse into `Jet.pt` and `Jet.eta` and provide the two inner fields as `ROOT::VecOps::RVec<float>`
148 /// each.
149 ///
150 /// In case the field is a collection of type `ROOT::VecOps::RVec`, `std::vector` or `std::array`, its corresponding
151 /// column is added as a `ROOT::VecOps::RVec`. Otherwise, the collection field's on-disk type is used. Note, however,
152 /// that inner record members of such collections will still be added as `ROOT::VecOps::RVec` (e.g., `std::set<Jet>
153 /// will be added as a `std::set`, but `Jet.[pt|eta] will be added as `ROOT::VecOps::RVec<float>).
154 void AddField(const ROOT::RNTupleDescriptor &desc, std::string_view colName, ROOT::DescriptorId_t fieldId,
155 std::vector<RFieldInfo> fieldInfos, bool convertToRVec = true);
156
157 /// The main function of the fThreadStaging background thread
158 void ExecStaging();
159 /// Starting from `fNextFileIndex`, opens the next `fNSlots` files. Calls `LoadStructure()` on the opened files.
160 /// The very first file is already available from the constructor.
161 void StageNextSources();
162 /// Populates fNextRanges with the next set of entry ranges. Moves files from the staging area as necessary
163 /// and aligns ranges with cluster boundaries for scheduling the tail of files.
164 /// Upon return, the fNextRanges list is ordered. It has usually fNSlots elements; fewer if there
165 /// is not enough work to give at least one cluster to every slot.
166 void PrepareNextRanges();
167
168 explicit RNTupleDS(std::unique_ptr<ROOT::Internal::RPageSource> pageSource);
169
170public:
171 RNTupleDS(std::string_view ntupleName, std::string_view fileName);
172 RNTupleDS(std::string_view ntupleName, const std::vector<std::string> &fileNames);
173 // Rule of five
174 RNTupleDS(const RNTupleDS &) = delete;
175 RNTupleDS &operator=(const RNTupleDS &) = delete;
176 RNTupleDS(RNTupleDS &&) = delete;
179
180 void SetNSlots(unsigned int nSlots) final;
181 std::size_t GetNFiles() const final { return fFileNames.empty() ? 1 : fFileNames.size(); }
182 const std::vector<std::string> &GetColumnNames() const final { return fColumnNames; }
183 bool HasColumn(std::string_view colName) const final;
184 std::string GetTypeName(std::string_view colName) const final;
185 std::vector<std::pair<ULong64_t, ULong64_t>> GetEntryRanges() final;
186 std::string GetLabel() final { return "RNTupleDS"; }
187
188 void Initialize() final;
189 void InitSlot(unsigned int slot, ULong64_t firstEntry) final;
190 void FinalizeSlot(unsigned int slot) final;
191 void Finalize() final;
192
193 std::unique_ptr<ROOT::Detail::RDF::RColumnReaderBase>
194 GetColumnReaders(unsigned int /*slot*/, std::string_view /*name*/, const std::type_info &) final;
195
196 // Old API, unused
197 bool SetEntry(unsigned int, ULong64_t) final { return true; }
198
199protected:
200 Record_t GetColumnReadersImpl(std::string_view name, const std::type_info &) final;
201};
202} // namespace ROOT::RDF
203
204namespace ROOT::RDF {
205RDataFrame FromRNTuple(std::string_view ntupleName, std::string_view fileName);
206RDataFrame FromRNTuple(std::string_view ntupleName, const std::vector<std::string> &fileNames);
207} // namespace ROOT::RDF
208
209#endif
unsigned long long ULong64_t
Definition RtypesCore.h:70
char name[80]
Definition TGX11.cxx:110
Pure virtual base class for all column reader types.
Every RDF column is represented by exactly one RNTuple field.
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
std::vector< void * > Record_t
The RDataSource implementation for RNTuple.
Definition RNTupleDS.hxx:47
bool fHasNextSources
Is true when the staging thread has populated the next batch of files to fStagingArea.
const std::vector< std::string > & GetColumnNames() const final
Returns a reference to the collection of the dataset's column names.
void AddField(const ROOT::RNTupleDescriptor &desc, std::string_view colName, ROOT::DescriptorId_t fieldId, std::vector< RFieldInfo > fieldInfos, bool convertToRVec=true)
Provides the RDF column "colName" given the field identified by fieldID.
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges() final
Return ranges of entries to distribute to tasks.
std::size_t GetNFiles() const final
Returns the number of files from which the dataset is constructed.
std::vector< REntryRangeDS > fNextRanges
Basis for the ranges populated by the PrepareNextRanges() call.
std::unordered_map< ULong64_t, std::size_t > fFirstEntry2RangeIdx
Maps the first entries from the ranges of the last GetEntryRanges() call to their corresponding index...
void ExecStaging()
The main function of the fThreadStaging background thread.
bool fStagingThreadShouldTerminate
Is true when the I/O thread should quit.
RNTupleDS(RNTupleDS &&)=delete
std::vector< std::vector< ROOT::Internal::RDF::RNTupleColumnReader * > > fActiveColumnReaders
List of column readers returned by GetColumnReaders() organized by slot.
std::vector< std::unique_ptr< ROOT::Internal::RPageSource > > fStagingArea
The staging area is relevant for chains of files, i.e.
Definition RNTupleDS.hxx:82
std::unique_ptr< ROOT::Detail::RDF::RColumnReaderBase > GetColumnReaders(unsigned int, std::string_view, const std::type_info &) final
If the other GetColumnReaders overload returns an empty vector, this overload will be called instead.
std::vector< std::unique_ptr< ROOT::RFieldBase > > fProtoFields
We prepare a prototype field for every column.
Definition RNTupleDS.hxx:89
void SetNSlots(unsigned int nSlots) final
Inform RDataSource of the number of processing slots (i.e.
RNTupleDS & operator=(const RNTupleDS &)=delete
std::vector< std::string > fFileNames
Definition RNTupleDS.hxx:65
bool fIsReadyForStaging
Is true when the staging thread should start working.
void InitSlot(unsigned int slot, ULong64_t firstEntry) final
Convenience method called at the start of the data processing associated to a slot.
ROOT::RNTupleDescriptor fPrincipalDescriptor
A clone of the first pages source's descriptor.
Definition RNTupleDS.hxx:61
std::vector< REntryRangeDS > fCurrentRanges
Basis for the ranges returned by the last GetEntryRanges() call.
RNTupleDS(std::unique_ptr< ROOT::Internal::RPageSource > pageSource)
std::string GetTypeName(std::string_view colName) const final
Type of a column as a string, e.g.
std::size_t fNextFileIndex
Index into fFileNames to the next file to process.
Definition RNTupleDS.hxx:83
std::unordered_map< ROOT::DescriptorId_t, std::string > fFieldId2QualifiedName
Connects the IDs of active proto fields and their subfields to their fully qualified name (a....
Definition RNTupleDS.hxx:98
std::string fNTupleName
The data source may be constructed with an ntuple name and a list of files.
Definition RNTupleDS.hxx:64
void PrepareNextRanges()
Populates fNextRanges with the next set of entry ranges.
void StageNextSources()
Starting from fNextFileIndex, opens the next fNSlots files.
std::condition_variable fCvStaging
Signal for the state information of fIsReadyForStaging and fHasNextSources.
RNTupleDS(const RNTupleDS &)=delete
void Finalize() final
Convenience method called after concluding an event-loop.
std::string GetLabel() final
Return a string representation of the datasource type.
RNTupleDS & operator=(RNTupleDS &&)=delete
std::thread fThreadStaging
The background thread that runs StageNextSources()
std::mutex fMutexStaging
Protects the shared state between the main thread and the I/O thread.
std::unordered_map< std::size_t, std::vector< std::unique_ptr< ROOT::RFieldBase > > > fAlternativeProtoFields
Columns may be requested with types other than with which they were initially added as proto fields.
Definition RNTupleDS.hxx:94
bool SetEntry(unsigned int, ULong64_t) final
Advance the "cursors" returned by GetColumnReaders to the selected entry for a particular slot.
std::vector< std::string > fColumnTypes
void Initialize() final
Convenience method called before starting an event-loop.
std::vector< std::string > fColumnNames
Definition RNTupleDS.hxx:99
ULong64_t fSeenEntries
The number of entries so far returned by GetEntryRanges()
bool HasColumn(std::string_view colName) const final
Checks if the dataset has a certain column.
Record_t GetColumnReadersImpl(std::string_view name, const std::type_info &) final
type-erased vector of pointers to pointers to column values - one per slot
void FinalizeSlot(unsigned int slot) final
Convenience method called at the end of the data processing associated to a slot.
The on-storage metadata of an RNTuple.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
std::uint64_t DescriptorId_t
Distriniguishes elements of the same type within a descriptor, e.g. different fields.
The PrepareNextRanges() method populates the fNextRanges list with REntryRangeDS records.
Definition RNTupleDS.hxx:53
ULong64_t fLastEntry
End entry index in fSource, e.g. the number of entries in the range is fLastEntry - fFirstEntry.
Definition RNTupleDS.hxx:57
std::unique_ptr< ROOT::Internal::RPageSource > fSource
Definition RNTupleDS.hxx:54
ULong64_t fFirstEntry
First entry index in fSource.
Definition RNTupleDS.hxx:55
Holds useful information about fields added to the RNTupleDS.
RFieldInfo(ROOT::DescriptorId_t fieldId, std::size_t nRepetitions)
ROOT::DescriptorId_t fFieldId