17 std::vector<void *> RRootDS::GetColumnReadersImpl(std::string_view name,
const std::type_info &
id)
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 \"";
26 err +=
" but a different one has been selected.";
27 throw std::runtime_error(err);
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];
39 RRootDS::RRootDS(std::string_view treeName, std::string_view fileNameGlob)
40 : fTreeName(treeName), fFileNameGlob(fileNameGlob), fModelChain(std::string(treeName).c_str())
42 fModelChain.Add(fFileNameGlob.c_str());
44 const TObjArray &lob = *fModelChain.GetListOfBranches();
45 fListOfBranches.resize(lob.GetEntries());
47 TIterCategory<TObjArray> iter(&lob);
48 std::transform(iter.Begin(), iter.End(), fListOfBranches.begin(), [](TObject *o) {
return o->GetName(); });
53 for (
auto addr : fAddressesToFree) {
58 std::string RRootDS::GetTypeName(std::string_view colName)
const
60 if (!HasColumn(colName)) {
61 std::string e =
"The dataset does not have column ";
63 throw std::runtime_error(e);
68 ROOT::Internal::RDF::ColumnName2ColumnTypeName(std::string(colName), 0, &fModelChain,
nullptr,
71 TClass::GetClass(typeName.c_str());
75 const std::vector<std::string> &RRootDS::GetColumnNames()
const
77 return fListOfBranches;
80 bool RRootDS::HasColumn(std::string_view colName)
const
82 if (!fListOfBranches.empty())
84 return fListOfBranches.end() != std::find(fListOfBranches.begin(), fListOfBranches.end(), colName);
87 void RRootDS::InitSlot(
unsigned int slot, ULong64_t firstEntry)
89 auto chain =
new TChain(fTreeName.c_str());
90 chain->ResetBit(kMustCleanup);
91 chain->Add(fFileNameGlob.c_str());
92 chain->GetEntry(firstEntry);
94 for (
auto i : ROOT::TSeqU(fListOfBranches.size())) {
95 auto colName = fListOfBranches[i].c_str();
96 auto &addr = fBranchAddresses[i][slot];
97 auto typeName = GetTypeName(colName);
98 auto typeClass = TClass::GetClass(typeName.c_str());
100 chain->SetBranchAddress(colName, &addr,
nullptr, typeClass, EDataType(0),
true);
104 fAddressesToFree.emplace_back((
double *)addr);
106 chain->SetBranchAddress(colName, addr);
109 fChains[slot].reset(chain);
112 void RRootDS::FinaliseSlot(
unsigned int slot)
114 fChains[slot].reset(
nullptr);
117 std::vector<std::pair<ULong64_t, ULong64_t>> RRootDS::GetEntryRanges()
119 auto entryRanges(std::move(fEntryRanges));
123 bool RRootDS::SetEntry(
unsigned int slot, ULong64_t entry)
125 fChains[slot]->GetEntry(entry);
129 void RRootDS::SetNSlots(
unsigned int nSlots)
131 R__ASSERT(0U == fNSlots &&
"Setting the number of slots even if the number of slots is different from zero.");
135 const auto nColumns = fListOfBranches.size();
137 fBranchAddresses.resize(nColumns, std::vector<void *>(fNSlots,
nullptr));
139 fChains.resize(fNSlots);
142 void RRootDS::Initialise()
144 const auto nentries = fModelChain.GetEntries();
145 const auto chunkSize = nentries / fNSlots;
146 const auto reminder = 1U == fNSlots ? 0 : nentries % fNSlots;
149 for (
auto i : ROOT::TSeqU(fNSlots)) {
152 fEntryRanges.emplace_back(start, end);
155 fEntryRanges.back().second += reminder;
158 std::string RRootDS::GetLabel()
163 RDataFrame MakeRootDataFrame(std::string_view treeName, std::string_view fileNameGlob)
165 ROOT::RDataFrame tdf(std::make_unique<RRootDS>(treeName, fileNameGlob));