Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RRootDS.cxx
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
3 * All rights reserved. *
4 * *
5 * For the licensing terms see $ROOTSYS/LICENSE. *
6 * For the list of contributors see $ROOTSYS/README/CREDITS. *
7 *************************************************************************/
8
9#include <ROOT/RDF/Utils.hxx>
10#include <ROOT/RRootDS.hxx>
11#include <ROOT/TSeq.hxx>
12#include <TClass.h>
13
14#include <algorithm>
15#include <vector>
16
17namespace ROOT {
18
19namespace Internal {
20
21namespace RDF {
22
23std::vector<void *> RRootDS::GetColumnReadersImpl(std::string_view name, const std::type_info &id)
24{
25 const auto colTypeName = GetTypeName(name);
26 const auto &colTypeId = ROOT::Internal::RDF::TypeName2TypeID(colTypeName);
27 if (id != colTypeId) {
28 std::string err = "The type of column \"";
29 err += name;
30 err += "\" is ";
31 err += colTypeName;
32 err += " but a different one has been selected.";
33 throw std::runtime_error(err);
34 }
35
36 const auto index =
37 std::distance(fListOfBranches.begin(), std::find(fListOfBranches.begin(), fListOfBranches.end(), name));
38 std::vector<void *> ret(fNSlots);
39 for (auto slot : ROOT::TSeqU(fNSlots)) {
40 ret[slot] = (void *)&fBranchAddresses[index][slot];
41 }
42 return ret;
43}
44
45RRootDS::RRootDS(std::string_view treeName, std::string_view fileNameGlob)
46 : fTreeName(treeName), fFileNameGlob(fileNameGlob), fModelChain(std::string(treeName).c_str())
47{
49
52
53 TIterCategory<TObjArray> iter(&lob);
54 std::transform(iter.Begin(), iter.End(), fListOfBranches.begin(), [](TObject *o) { return o->GetName(); });
55}
56
58{
59 for (auto addr : fAddressesToFree) {
60 delete addr;
61 }
62}
63
64std::size_t RRootDS::GetNFiles() const
65{
66 if (auto files = fModelChain.GetListOfFiles())
67 return files->GetEntries();
68 return 0;
69}
70
71std::string RRootDS::GetTypeName(std::string_view colName) const
72{
73 if (!HasColumn(colName)) {
74 std::string e = "The dataset does not have column ";
75 e += colName;
76 throw std::runtime_error(e);
77 }
78 // TODO: we need to factor out the routine for the branch alone...
79 // Maybe a cache for the names?
80 auto typeName = ROOT::Internal::RDF::ColumnName2ColumnTypeName(std::string(colName), &fModelChain, /*ds=*/nullptr,
81 /*define=*/nullptr);
82 // We may not have yet loaded the library where the dictionary of this type is
83 TClass::GetClass(typeName.c_str());
84 return typeName;
85}
86
87const std::vector<std::string> &RRootDS::GetColumnNames() const
88{
89 return fListOfBranches;
90}
91
92bool RRootDS::HasColumn(std::string_view colName) const
93{
94 if (!fListOfBranches.empty())
96 return fListOfBranches.end() != std::find(fListOfBranches.begin(), fListOfBranches.end(), colName);
97}
98
99void RRootDS::InitSlot(unsigned int slot, ULong64_t firstEntry)
100{
101 auto chain = new TChain(fTreeName.c_str());
102 chain->ResetBit(kMustCleanup);
103 chain->Add(fFileNameGlob.c_str());
104 chain->GetEntry(firstEntry);
105 for (auto i : ROOT::TSeqU(fListOfBranches.size())) {
106 auto colName = fListOfBranches[i].c_str();
107 auto &addr = fBranchAddresses[i][slot];
108 auto typeName = GetTypeName(colName);
109 auto typeClass = TClass::GetClass(typeName.c_str());
110 if (typeClass) {
111 chain->SetBranchAddress(colName, &addr, nullptr, typeClass, EDataType(0), true);
112 } else {
113 if (!addr) {
114 addr = new double();
115 fAddressesToFree.emplace_back((double *)addr);
116 }
117 chain->SetBranchAddress(colName, addr);
118 }
119 }
120 fChains[slot].reset(chain);
121}
122
123void RRootDS::FinalizeSlot(unsigned int slot)
124{
125 fChains[slot].reset(nullptr);
126}
127
128std::vector<std::pair<ULong64_t, ULong64_t>> RRootDS::GetEntryRanges()
129{
130 auto entryRanges(std::move(fEntryRanges)); // empty fEntryRanges
131 return entryRanges;
132}
133
134bool RRootDS::SetEntry(unsigned int slot, ULong64_t entry)
135{
136 fChains[slot]->GetEntry(entry);
137 return true;
138}
139
140void RRootDS::SetNSlots(unsigned int nSlots)
141{
142 assert(0U == fNSlots && "Setting the number of slots even if the number of slots is different from zero.");
143
144 fNSlots = nSlots;
145
146 const auto nColumns = fListOfBranches.size();
147 // Initialize the entire set of addresses
148 fBranchAddresses.resize(nColumns, std::vector<void *>(fNSlots, nullptr));
149
150 fChains.resize(fNSlots);
151}
152
154{
155 const auto nentries = fModelChain.GetEntries();
156 const auto chunkSize = nentries / fNSlots;
157 const auto reminder = 1U == fNSlots ? 0 : nentries % fNSlots;
158 auto start = 0UL;
159 auto end = 0UL;
160 for (auto i : ROOT::TSeqU(fNSlots)) {
161 start = end;
162 end += chunkSize;
163 fEntryRanges.emplace_back(start, end);
164 (void)i;
165 }
166 fEntryRanges.back().second += reminder;
167}
168
169std::string RRootDS::GetLabel()
170{
171 return "Root";
172}
173
174} // ns RDF
175
176} // ns Internal
177
178} // ns ROOT
#define e(i)
Definition RSha256.hxx:103
unsigned long long ULong64_t
Definition RtypesCore.h:70
EDataType
Definition TDataType.h:28
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
char name[80]
Definition TGX11.cxx:110
int nentries
@ kMustCleanup
Definition TObject.h:368
bool SetEntry(unsigned int slot, ULong64_t entry) final
Advance the "cursors" returned by GetColumnReaders to the selected entry for a particular slot.
Definition RRootDS.cxx:134
void SetNSlots(unsigned int nSlots) final
Inform RDataSource of the number of processing slots (i.e.
Definition RRootDS.cxx:140
const std::vector< std::string > & GetColumnNames() const final
Returns a reference to the collection of the dataset's column names.
Definition RRootDS.cxx:87
void FinalizeSlot(unsigned int slot) final
Convenience method called at the end of the data processing associated to a slot.
Definition RRootDS.cxx:123
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges() final
Return ranges of entries to distribute to tasks.
Definition RRootDS.cxx:128
std::string GetTypeName(std::string_view colName) const final
Type of a column as a string, e.g.
Definition RRootDS.cxx:71
RRootDS(std::string_view treeName, std::string_view fileNameGlob)
Definition RRootDS.cxx:45
std::vector< std::vector< void * > > fBranchAddresses
Definition RRootDS.hxx:37
std::vector< void * > GetColumnReadersImpl(std::string_view, const std::type_info &) final
type-erased vector of pointers to pointers to column values - one per slot
Definition RRootDS.cxx:23
std::vector< std::unique_ptr< TChain > > fChains
Definition RRootDS.hxx:38
bool HasColumn(std::string_view colName) const final
Checks if the dataset has a certain column.
Definition RRootDS.cxx:92
void InitSlot(unsigned int slot, ULong64_t firstEntry) final
Convenience method called at the start of the data processing associated to a slot.
Definition RRootDS.cxx:99
std::vector< std::string > fListOfBranches
Definition RRootDS.hxx:35
void Initialize() final
Convenience method called before starting an event-loop.
Definition RRootDS.cxx:153
std::vector< double * > fAddressesToFree
Definition RRootDS.hxx:34
std::string GetLabel() final
Return a string representation of the datasource type.
Definition RRootDS.cxx:169
std::size_t GetNFiles() const final
Returns the number of files from which the dataset is constructed.
Definition RRootDS.cxx:64
std::vector< std::pair< ULong64_t, ULong64_t > > fEntryRanges
Definition RRootDS.hxx:36
A chain is a collection of files containing TTree objects.
Definition TChain.h:33
TObjArray * GetListOfBranches() override
Return a pointer to the list of branches of the current tree.
Definition TChain.cxx:1125
TObjArray * GetListOfFiles() const
Definition TChain.h:111
virtual Int_t Add(TChain *chain)
Add all files referenced by the passed chain to this chain.
Definition TChain.cxx:219
Long64_t GetEntries() const override
Return the total number of entries in the chain.
Definition TChain.cxx:978
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:3035
TIterCategory & Begin()
static TIterCategory End()
An array of TObjects.
Definition TObjArray.h:31
Int_t GetEntriesUnsafe() const
Return the number of objects in array (i.e.
Mother of all ROOT objects.
Definition TObject.h:41
const std::type_info & TypeName2TypeID(const std::string &name)
Return the type_info associated to a name.
Definition RDFUtils.cxx:62
std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *, RDataSource *, RDefineBase *, bool vector2RVec=true)
Return a string containing the type of the given branch.
Definition RDFUtils.cxx:229
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
TSeq< unsigned int > TSeqU
Definition TSeq.hxx:204