forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTRootDS.cxx
More file actions
131 lines (114 loc) · 3.86 KB
/
TRootDS.cxx
File metadata and controls
131 lines (114 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <TClass.h>
#include <ROOT/TDFUtils.hxx>
#include <ROOT/TRootDS.hxx>
#include <ROOT/TSeq.hxx>
#include <algorithm>
#include <vector>
namespace ROOT {
namespace Experimental {
namespace TDF {
std::vector<void *> TRootDS::GetColumnReadersImpl(std::string_view name, const std::type_info &)
{
const auto &colNames = GetColumnNames();
if (fBranchAddresses.empty()) {
auto nColumns = colNames.size();
// Initialise the entire set of addresses
fBranchAddresses.resize(nColumns, std::vector<void *>(fNSlots));
}
const auto index = std::distance(colNames.begin(), std::find(colNames.begin(), colNames.end(), name));
std::vector<void *> ret(fNSlots);
for (auto slot : ROOT::TSeqU(fNSlots)) {
ret[slot] = (void *)&fBranchAddresses[index][slot];
}
return ret;
}
TRootDS::TRootDS(std::string_view treeName, std::string_view fileNameGlob)
: fTreeName(treeName), fFileNameGlob(fileNameGlob), fModelChain(std::string(treeName).c_str())
{
fModelChain.Add(fFileNameGlob.c_str());
auto &lob = *fModelChain.GetListOfBranches();
fListOfBranches.resize(lob.GetEntries());
std::transform(lob.begin(), lob.end(), fListOfBranches.begin(), [](TObject *o) { return o->GetName(); });
}
TRootDS::~TRootDS()
{
}
std::string TRootDS::GetTypeName(std::string_view colName) const
{
if (!HasColumn(colName)) {
std::string e = "The dataset does not have column ";
e += colName;
throw std::runtime_error(e);
}
// TODO: we need to factor out the routine for the branch alone...
// Maybe a cache for the names?
auto typeName = ROOT::Internal::TDF::ColumnName2ColumnTypeName(std::string(colName).c_str(), &fModelChain,
nullptr /*TCustomColumnBase here*/);
// We may not have yet loaded the library where the dictionary of this type
// is
TClass::GetClass(typeName.c_str());
return typeName;
}
const std::vector<std::string> &TRootDS::GetColumnNames() const
{
return fListOfBranches;
}
bool TRootDS::HasColumn(std::string_view colName) const
{
if (!fListOfBranches.empty())
GetColumnNames();
return fListOfBranches.end() != std::find(fListOfBranches.begin(), fListOfBranches.end(), colName);
}
void TRootDS::InitSlot(unsigned int slot, ULong64_t firstEntry)
{
auto chain = new TChain(fTreeName.c_str());
fChains[slot].reset(chain);
chain->Add(fFileNameGlob.c_str());
chain->GetEntry(firstEntry);
for (auto i : ROOT::TSeqU(fListOfBranches.size())) {
auto colName = fListOfBranches[i].c_str();
auto &addr = fBranchAddresses[i][slot];
auto typeName = GetTypeName(colName);
auto isClass = nullptr != TClass::GetClass(typeName.c_str());
if (isClass) {
chain->SetBranchAddress(colName, &addr);
} else {
if (!addr) {
addr = new double(); // who frees this :) ?
}
chain->SetBranchAddress(colName, addr);
}
}
}
const std::vector<std::pair<ULong64_t, ULong64_t>> &TRootDS::GetEntryRanges() const
{
if (fEntryRanges.empty()) {
throw std::runtime_error("No ranges are available. Did you set the number of slots?");
}
return fEntryRanges;
}
void TRootDS::SetEntry(unsigned int slot, ULong64_t entry)
{
fChains[slot]->GetEntry(entry);
}
void TRootDS::SetNSlots(unsigned int nSlots)
{
assert(0U == fNSlots && "Setting the number of slots even if the number of slots is different from zero.");
fNSlots = nSlots;
fChains.resize(fNSlots);
auto nentries = fModelChain.GetEntries();
auto chunkSize = nentries / fNSlots;
auto reminder = 1U == fNSlots ? 0 : nentries % fNSlots;
auto start = 0UL;
auto end = 0UL;
for (auto i : ROOT::TSeqU(fNSlots)) {
start = end;
end += chunkSize;
fEntryRanges.emplace_back(start, end);
(void)i;
}
fEntryRanges.back().second += reminder;
}
} // ns TDF
} // ns Experimental
} // ns ROOT