Logo ROOT   6.18/05
Reference Guide
RNTupleDS.cxx
Go to the documentation of this file.
1/// \file RNTupleDS.cxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-10-04
5/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6/// is welcome!
7
8/*************************************************************************
9 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#include <ROOT/RNTuple.hxx>
17#include <ROOT/RNTupleDS.hxx>
18#include <ROOT/RStringView.hxx>
19
20#include <TError.h>
21
22#include <string>
23#include <vector>
24#include <typeinfo>
25#include <utility>
26
27namespace ROOT {
28namespace Experimental {
29
30RNTupleDS::RNTupleDS(std::unique_ptr<ROOT::Experimental::RNTupleReader> ntuple)
31 : fNTuple(std::move(ntuple)), fEntry(fNTuple->GetModel()->CreateEntry()), fNSlots(1), fHasSeenAllRanges(false)
32{
33 auto rootField = fNTuple->GetModel()->GetRootField();
34 for (auto& f : *rootField) {
35 if (f.GetParent() != rootField)
36 continue;
37 fColumnNames.push_back(f.GetName());
38 fColumnTypes.push_back(f.GetType());
39 fValuePtrs.push_back(fEntry->GetValue(f.GetName()).GetRawPtr());
40 }
41}
42
43
45{
46}
47
48
49const std::vector<std::string>& RNTupleDS::GetColumnNames() const
50{
51 return fColumnNames;
52}
53
54
56{
57 const auto index = std::distance(
58 fColumnNames.begin(), std::find(fColumnNames.begin(), fColumnNames.end(), name));
59 // TODO(jblomer): check expected type info like in, e.g., RRootDS.cxx
60 // There is a problem extracting the type info for std::int32_t and company though
61
62 std::vector<void*> ptrs;
63 R__ASSERT(fNSlots == 1);
64 ptrs.push_back(&fValuePtrs[index]);
65
66 return ptrs;
67}
68
69bool RNTupleDS::SetEntry(unsigned int /*slot*/, ULong64_t entryIndex) {
70 fNTuple->LoadEntry(entryIndex, fEntry.get());
71 return true;
72}
73
74std::vector<std::pair<ULong64_t, ULong64_t>> RNTupleDS::GetEntryRanges()
75{
76 std::vector<std::pair<ULong64_t, ULong64_t>> ranges;
77 if (fHasSeenAllRanges) return ranges;
78
79 auto nEntries = fNTuple->GetNEntries();
80 const auto chunkSize = nEntries / fNSlots;
81 const auto reminder = 1U == fNSlots ? 0 : nEntries % fNSlots;
82 auto start = 0UL;
83 auto end = 0UL;
84 for (auto i : ROOT::TSeqU(fNSlots)) {
85 start = end;
86 end += chunkSize;
87 ranges.emplace_back(start, end);
88 (void)i;
89 }
90 ranges.back().second += reminder;
91 fHasSeenAllRanges = true;
92 return ranges;
93}
94
95
96std::string RNTupleDS::GetTypeName(std::string_view colName) const
97{
98 const auto index = std::distance(
99 fColumnNames.begin(), std::find(fColumnNames.begin(), fColumnNames.end(), colName));
100 return fColumnTypes[index];
101}
102
103
105{
106 return std::find(fColumnNames.begin(), fColumnNames.end(), colName) !=
107 fColumnNames.end();
108}
109
110
112{
113 fHasSeenAllRanges = false;
114}
115
116
117void RNTupleDS::SetNSlots(unsigned int nSlots)
118{
119 fNSlots = nSlots;
120}
121
122
124 auto ntuple = RNTupleReader::Open(ntupleName, fileName);
125 ROOT::RDataFrame rdf(std::make_unique<RNTupleDS>(std::move(ntuple)));
126 return rdf;
127}
128
129} // ns Experimental
130} // ns ROOT
#define f(i)
Definition: RSha256.hxx:104
unsigned long long ULong64_t
Definition: RtypesCore.h:70
#define R__ASSERT(e)
Definition: TError.h:96
char name[80]
Definition: TGX11.cxx:109
typedef void((*Func_t)())
std::unique_ptr< ROOT::Experimental::REntry > fEntry
Definition: RNTupleDS.hxx:37
const std::vector< std::string > & GetColumnNames() const final
Returns a reference to the collection of the dataset's column names.
Definition: RNTupleDS.cxx:49
void SetNSlots(unsigned int nSlots) final
Inform RDataSource of the number of processing slots (i.e.
Definition: RNTupleDS.cxx:117
bool SetEntry(unsigned int slot, ULong64_t entry) final
Advance the "cursors" returned by GetColumnReaders to the selected entry for a particular slot.
Definition: RNTupleDS.cxx:69
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
Definition: RNTupleDS.cxx:55
std::unique_ptr< ROOT::Experimental::RNTupleReader > fNTuple
Definition: RNTupleDS.hxx:36
std::vector< std::string > fColumnNames
Definition: RNTupleDS.hxx:40
std::string GetTypeName(std::string_view colName) const final
Type of a column as a string, e.g.
Definition: RNTupleDS.cxx:96
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges() final
Return ranges of entries to distribute to tasks.
Definition: RNTupleDS.cxx:74
RNTupleDS(std::unique_ptr< ROOT::Experimental::RNTupleReader > ntuple)
Definition: RNTupleDS.cxx:30
bool HasColumn(std::string_view colName) const final
Checks if the dataset has a certain column.
Definition: RNTupleDS.cxx:104
std::vector< std::string > fColumnTypes
Definition: RNTupleDS.hxx:41
std::vector< void * > fValuePtrs
Definition: RNTupleDS.hxx:42
void Initialise() final
Convenience method called before starting an event-loop.
Definition: RNTupleDS.cxx:111
static std::unique_ptr< RNTupleReader > Open(std::unique_ptr< RNTupleModel > model, std::string_view ntupleName, std::string_view storage)
Definition: RNTuple.cxx:68
std::vector< void * > Record_t
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
Definition: RDataFrame.hxx:42
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
basic_string_view< char > string_view
RDataFrame MakeNTupleDataFrame(std::string_view ntupleName, std::string_view fileName)
Definition: RNTupleDS.cxx:123
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21