Logo ROOT  
Reference Guide
RRootDS.cxx
Go to the documentation of this file.
1#include <ROOT/RDF/Utils.hxx>
2#include <ROOT/RRootDS.hxx>
3#include <ROOT/TSeq.hxx>
4#include <TClass.h>
5#include <TError.h>
6#include <TROOT.h> // For the gROOTMutex
7#include <TVirtualMutex.h> // For the R__LOCKGUARD
9
10#include <algorithm>
11#include <vector>
12
13namespace ROOT {
14
15namespace RDF {
16
17std::vector<void *> RRootDS::GetColumnReadersImpl(std::string_view name, const std::type_info &id)
18{
19 const auto colTypeName = GetTypeName(name);
20 const auto &colTypeId = ROOT::Internal::RDF::TypeName2TypeID(colTypeName);
21 if (id != colTypeId) {
22 std::string err = "The type of column \"";
23 err += name;
24 err += "\" is ";
25 err += colTypeName;
26 err += " but a different one has been selected.";
27 throw std::runtime_error(err);
28 }
29
30 const auto index =
31 std::distance(fListOfBranches.begin(), std::find(fListOfBranches.begin(), fListOfBranches.end(), name));
32 std::vector<void *> ret(fNSlots);
33 for (auto slot : ROOT::TSeqU(fNSlots)) {
34 ret[slot] = (void *)&fBranchAddresses[index][slot];
35 }
36 return ret;
37}
38
39RRootDS::RRootDS(std::string_view treeName, std::string_view fileNameGlob)
40 : fTreeName(treeName), fFileNameGlob(fileNameGlob), fModelChain(std::string(treeName).c_str())
41{
42 fModelChain.Add(fFileNameGlob.c_str());
43
44 const TObjArray &lob = *fModelChain.GetListOfBranches();
45 fListOfBranches.resize(lob.GetEntries());
46
47 TIterCategory<TObjArray> iter(&lob);
48 std::transform(iter.Begin(), iter.End(), fListOfBranches.begin(), [](TObject *o) { return o->GetName(); });
49}
50
51RRootDS::~RRootDS()
52{
53 for (auto addr : fAddressesToFree) {
54 delete addr;
55 }
56}
57
58std::string RRootDS::GetTypeName(std::string_view colName) const
59{
60 if (!HasColumn(colName)) {
61 std::string e = "The dataset does not have column ";
62 e += colName;
63 throw std::runtime_error(e);
64 }
65 // TODO: we need to factor out the routine for the branch alone...
66 // Maybe a cache for the names?
67 auto typeName = ROOT::Internal::RDF::ColumnName2ColumnTypeName(std::string(colName), &fModelChain, /*ds=*/nullptr,
68 /*customCol=*/nullptr);
69 // We may not have yet loaded the library where the dictionary of this type is
70 TClass::GetClass(typeName.c_str());
71 return typeName;
72}
73
74const std::vector<std::string> &RRootDS::GetColumnNames() const
75{
76 return fListOfBranches;
77}
78
79bool RRootDS::HasColumn(std::string_view colName) const
80{
81 if (!fListOfBranches.empty())
82 GetColumnNames();
83 return fListOfBranches.end() != std::find(fListOfBranches.begin(), fListOfBranches.end(), colName);
84}
85
86void RRootDS::InitSlot(unsigned int slot, ULong64_t firstEntry)
87{
88 auto chain = new TChain(fTreeName.c_str());
89 chain->ResetBit(kMustCleanup);
90 chain->Add(fFileNameGlob.c_str());
91 chain->GetEntry(firstEntry);
92 TString setBranches;
93 for (auto i : ROOT::TSeqU(fListOfBranches.size())) {
94 auto colName = fListOfBranches[i].c_str();
95 auto &addr = fBranchAddresses[i][slot];
96 auto typeName = GetTypeName(colName);
97 auto typeClass = TClass::GetClass(typeName.c_str());
98 if (typeClass) {
99 chain->SetBranchAddress(colName, &addr, nullptr, typeClass, EDataType(0), true);
100 } else {
101 if (!addr) {
102 addr = new double();
103 fAddressesToFree.emplace_back((double *)addr);
104 }
105 chain->SetBranchAddress(colName, addr);
106 }
107 }
108 fChains[slot].reset(chain);
109}
110
111void RRootDS::FinaliseSlot(unsigned int slot)
112{
113 fChains[slot].reset(nullptr);
114}
115
116std::vector<std::pair<ULong64_t, ULong64_t>> RRootDS::GetEntryRanges()
117{
118 auto entryRanges(std::move(fEntryRanges)); // empty fEntryRanges
119 return entryRanges;
120}
121
122bool RRootDS::SetEntry(unsigned int slot, ULong64_t entry)
123{
124 fChains[slot]->GetEntry(entry);
125 return true;
126}
127
128void RRootDS::SetNSlots(unsigned int nSlots)
129{
130 R__ASSERT(0U == fNSlots && "Setting the number of slots even if the number of slots is different from zero.");
131
132 fNSlots = nSlots;
133
134 const auto nColumns = fListOfBranches.size();
135 // Initialise the entire set of addresses
136 fBranchAddresses.resize(nColumns, std::vector<void *>(fNSlots, nullptr));
137
138 fChains.resize(fNSlots);
139}
140
141void RRootDS::Initialise()
142{
143 const auto nentries = fModelChain.GetEntries();
144 const auto chunkSize = nentries / fNSlots;
145 const auto reminder = 1U == fNSlots ? 0 : nentries % fNSlots;
146 auto start = 0UL;
147 auto end = 0UL;
148 for (auto i : ROOT::TSeqU(fNSlots)) {
149 start = end;
150 end += chunkSize;
151 fEntryRanges.emplace_back(start, end);
152 (void)i;
153 }
154 fEntryRanges.back().second += reminder;
155}
156
157std::string RRootDS::GetLabel()
158{
159 return "Root";
160}
161
163{
164 return ROOT::RDataFrame(treeName, fileNameGlob);
165}
166
167} // ns RDF
168
169} // ns ROOT
double
Definition: Converters.cxx:921
#define e(i)
Definition: RSha256.hxx:103
unsigned long long ULong64_t
Definition: RtypesCore.h:72
EDataType
Definition: TDataType.h:28
#define R__ASSERT(e)
Definition: TError.h:96
char name[80]
Definition: TGX11.cxx:109
int nentries
Definition: THbookFile.cxx:89
@ kMustCleanup
Definition: TObject.h:355
typedef void((*Func_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
A chain is a collection of files containing TTree objects.
Definition: TChain.h:34
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:2948
An array of TObjects.
Definition: TObjArray.h:37
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:523
Mother of all ROOT objects.
Definition: TObject.h:37
Basic string class.
Definition: TString.h:131
basic_string_view< char > string_view
const std::type_info & TypeName2TypeID(const std::string &name)
Return the type_info associated to a name.
Definition: RDFUtils.cxx:42
std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *tree, RDataSource *ds, RCustomColumnBase *customColumn, bool vector2rvec)
Return a string containing the type of the given branch.
Definition: RDFUtils.cxx:211
class R__DEPRECATED(6, 24, "RRootDS will be removed from the public namespace. Please use standard RDataFrame constructors " "and interfaces instead") RRootDS final RDataFrame MakeRootDataFrame(std::string_view treeName, std::string_view fileNameGlob)
Definition: RRootDS.cxx:162
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21