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::string RRootDS::GetTypeName(std::string_view colName) const
65{
66 if (!HasColumn(colName)) {
67 std::string e = "The dataset does not have column ";
68 e += colName;
69 throw std::runtime_error(e);
70 }
71 // TODO: we need to factor out the routine for the branch alone...
72 // Maybe a cache for the names?
73 auto typeName = ROOT::Internal::RDF::ColumnName2ColumnTypeName(std::string(colName), &fModelChain, /*ds=*/nullptr,
74 /*define=*/nullptr);
75 // We may not have yet loaded the library where the dictionary of this type is
76 TClass::GetClass(typeName.c_str());
77 return typeName;
78}
79
80const std::vector<std::string> &RRootDS::GetColumnNames() const
81{
82 return fListOfBranches;
83}
84
85bool RRootDS::HasColumn(std::string_view colName) const
86{
87 if (!fListOfBranches.empty())
89 return fListOfBranches.end() != std::find(fListOfBranches.begin(), fListOfBranches.end(), colName);
90}
91
92void RRootDS::InitSlot(unsigned int slot, ULong64_t firstEntry)
93{
94 auto chain = new TChain(fTreeName.c_str());
95 chain->ResetBit(kMustCleanup);
96 chain->Add(fFileNameGlob.c_str());
97 chain->GetEntry(firstEntry);
98 for (auto i : ROOT::TSeqU(fListOfBranches.size())) {
99 auto colName = fListOfBranches[i].c_str();
100 auto &addr = fBranchAddresses[i][slot];
101 auto typeName = GetTypeName(colName);
102 auto typeClass = TClass::GetClass(typeName.c_str());
103 if (typeClass) {
104 chain->SetBranchAddress(colName, &addr, nullptr, typeClass, EDataType(0), true);
105 } else {
106 if (!addr) {
107 addr = new double();
108 fAddressesToFree.emplace_back((double *)addr);
109 }
110 chain->SetBranchAddress(colName, addr);
111 }
112 }
113 fChains[slot].reset(chain);
114}
115
116void RRootDS::FinalizeSlot(unsigned int slot)
117{
118 fChains[slot].reset(nullptr);
119}
120
121std::vector<std::pair<ULong64_t, ULong64_t>> RRootDS::GetEntryRanges()
122{
123 auto entryRanges(std::move(fEntryRanges)); // empty fEntryRanges
124 return entryRanges;
125}
126
127bool RRootDS::SetEntry(unsigned int slot, ULong64_t entry)
128{
129 fChains[slot]->GetEntry(entry);
130 return true;
131}
132
133void RRootDS::SetNSlots(unsigned int nSlots)
134{
135 assert(0U == fNSlots && "Setting the number of slots even if the number of slots is different from zero.");
136
137 fNSlots = nSlots;
138
139 const auto nColumns = fListOfBranches.size();
140 // Initialize the entire set of addresses
141 fBranchAddresses.resize(nColumns, std::vector<void *>(fNSlots, nullptr));
142
143 fChains.resize(fNSlots);
144}
145
147{
148 const auto nentries = fModelChain.GetEntries();
149 const auto chunkSize = nentries / fNSlots;
150 const auto reminder = 1U == fNSlots ? 0 : nentries % fNSlots;
151 auto start = 0UL;
152 auto end = 0UL;
153 for (auto i : ROOT::TSeqU(fNSlots)) {
154 start = end;
155 end += chunkSize;
156 fEntryRanges.emplace_back(start, end);
157 (void)i;
158 }
159 fEntryRanges.back().second += reminder;
160}
161
162std::string RRootDS::GetLabel()
163{
164 return "Root";
165}
166
167} // ns RDF
168
169} // ns Internal
170
171} // ns ROOT
#define e(i)
Definition RSha256.hxx:103
unsigned long long ULong64_t
Definition RtypesCore.h:81
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:127
void SetNSlots(unsigned int nSlots) final
Inform RDataSource of the number of processing slots (i.e.
Definition RRootDS.cxx:133
const std::vector< std::string > & GetColumnNames() const final
Returns a reference to the collection of the dataset's column names.
Definition RRootDS.cxx:80
void FinalizeSlot(unsigned int slot) final
Convenience method called at the end of the data processing associated to a slot.
Definition RRootDS.cxx:116
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges() final
Return ranges of entries to distribute to tasks.
Definition RRootDS.cxx:121
std::string GetTypeName(std::string_view colName) const final
Type of a column as a string, e.g.
Definition RRootDS.cxx:64
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:85
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:92
std::vector< std::string > fListOfBranches
Definition RRootDS.hxx:35
void Initialize() final
Convenience method called before starting an event-loop.
Definition RRootDS.cxx:146
std::vector< double * > fAddressesToFree
Definition RRootDS.hxx:34
std::string GetLabel() final
Return a string representation of the datasource type.
Definition RRootDS.cxx:162
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:1097
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:950
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:2968
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:51
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:222
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
TSeq< unsigned int > TSeqU
Definition TSeq.hxx:204