Logo ROOT   6.12/07
Reference Guide
TRootDS.cxx
Go to the documentation of this file.
1 #include <ROOT/TDFUtils.hxx>
2 #include <ROOT/TRootDS.hxx>
3 #include <ROOT/TSeq.hxx>
4 #include <TClass.h>
5 #include <TROOT.h> // For the gROOTMutex
6 #include <TVirtualMutex.h> // For the R__LOCKGUARD
7 #include <ROOT/RMakeUnique.hxx>
8 
9 #include <algorithm>
10 #include <vector>
11 
12 namespace ROOT {
13 namespace Experimental {
14 namespace TDF {
15 
16 std::vector<void *> TRootDS::GetColumnReadersImpl(std::string_view name, const std::type_info &id)
17 {
18  const auto colTypeName = GetTypeName(name);
19  const auto &colTypeId = ROOT::Internal::TDF::TypeName2TypeID(colTypeName);
20  if (id != colTypeId) {
21  std::string err = "The type of column \"";
22  err += name;
23  err += "\" is ";
24  err += colTypeName;
25  err += " but a different one has been selected.";
26  throw std::runtime_error(err);
27  }
28 
29  const auto index =
30  std::distance(fListOfBranches.begin(), std::find(fListOfBranches.begin(), fListOfBranches.end(), name));
31  std::vector<void *> ret(fNSlots);
32  for (auto slot : ROOT::TSeqU(fNSlots)) {
33  ret[slot] = (void *)&fBranchAddresses[index][slot];
34  }
35  return ret;
36 }
37 
39  : fTreeName(treeName), fFileNameGlob(fileNameGlob), fModelChain(std::string(treeName).c_str())
40 {
41  fModelChain.Add(fFileNameGlob.c_str());
42 
43  auto &lob = *fModelChain.GetListOfBranches();
44  fListOfBranches.resize(lob.GetEntries());
45  std::transform(lob.begin(), lob.end(), fListOfBranches.begin(), [](TObject *o) { return o->GetName(); });
46 }
47 
49 {
50  for (auto addr : fAddressesToFree) {
51  delete addr;
52  }
53 }
54 
55 std::string TRootDS::GetTypeName(std::string_view colName) const
56 {
57  if (!HasColumn(colName)) {
58  std::string e = "The dataset does not have column ";
59  e += colName;
60  throw std::runtime_error(e);
61  }
62  // TODO: we need to factor out the routine for the branch alone...
63  // Maybe a cache for the names?
64  auto typeName = ROOT::Internal::TDF::ColumnName2ColumnTypeName(std::string(colName).c_str(), &fModelChain,
65  nullptr /*TCustomColumnBase here*/);
66  // We may not have yet loaded the library where the dictionary of this type
67  // is
68  TClass::GetClass(typeName.c_str());
69  return typeName;
70 }
71 
72 const std::vector<std::string> &TRootDS::GetColumnNames() const
73 {
74  return fListOfBranches;
75 }
76 
78 {
79  if (!fListOfBranches.empty())
81  return fListOfBranches.end() != std::find(fListOfBranches.begin(), fListOfBranches.end(), colName);
82 }
83 
84 void TRootDS::InitSlot(unsigned int slot, ULong64_t firstEntry)
85 {
86  TChain *chain;
87  {
89  chain = new TChain(fTreeName.c_str());
90  }
91  chain->ResetBit(kMustCleanup);
92  chain->Add(fFileNameGlob.c_str());
93  chain->GetEntry(firstEntry);
94  TString setBranches;
95  for (auto i : ROOT::TSeqU(fListOfBranches.size())) {
96  auto colName = fListOfBranches[i].c_str();
97  auto &addr = fBranchAddresses[i][slot];
98  auto typeName = GetTypeName(colName);
99  auto typeClass = TClass::GetClass(typeName.c_str());
100  if (typeClass) {
101  chain->SetBranchAddress(colName, &addr, nullptr, typeClass, EDataType(0), true);
102  } else {
103  if (!addr) {
104  addr = new double();
105  fAddressesToFree.emplace_back((double *)addr);
106  }
107  chain->SetBranchAddress(colName, addr);
108  }
109  }
110  fChains[slot].reset(chain);
111 }
112 
113 std::vector<std::pair<ULong64_t, ULong64_t>> TRootDS::GetEntryRanges()
114 {
115  auto entryRanges(std::move(fEntryRanges)); // empty fEntryRanges
116  return entryRanges;
117 }
118 
119 void TRootDS::SetEntry(unsigned int slot, ULong64_t entry)
120 {
121  fChains[slot]->GetEntry(entry);
122 }
123 
124 void TRootDS::SetNSlots(unsigned int nSlots)
125 {
126  assert(0U == fNSlots && "Setting the number of slots even if the number of slots is different from zero.");
127 
128  fNSlots = nSlots;
129 
130  const auto nColumns = fListOfBranches.size();
131  // Initialise the entire set of addresses
132  fBranchAddresses.resize(nColumns, std::vector<void *>(fNSlots, nullptr));
133 
134  fChains.resize(fNSlots);
135 }
136 
138 {
139  const auto nentries = fModelChain.GetEntries();
140  const auto chunkSize = nentries / fNSlots;
141  const auto reminder = 1U == fNSlots ? 0 : nentries % fNSlots;
142  auto start = 0UL;
143  auto end = 0UL;
144  for (auto i : ROOT::TSeqU(fNSlots)) {
145  start = end;
146  end += chunkSize;
147  fEntryRanges.emplace_back(start, end);
148  (void)i;
149  }
150  fEntryRanges.back().second += reminder;
151 }
152 
154 {
155  ROOT::Experimental::TDataFrame tdf(std::make_unique<TRootDS>(treeName, fileNameGlob));
156  return tdf;
157 }
158 
159 } // ns TDF
160 } // ns Experimental
161 } // ns ROOT
const std::type_info & TypeName2TypeID(const std::string &name)
Return the type_info associated to a name.
Definition: TDFUtils.cxx:43
std::vector< std::vector< void * > > fBranchAddresses
Definition: TRootDS.hxx:23
basic_string_view< char > string_view
Definition: RStringView.h:35
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges()
Return ranges of entries to distribute to tasks.
Definition: TRootDS.cxx:113
Basic string class.
Definition: TString.h:125
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:57
const std::vector< std::string > & GetColumnNames() const
Returns a reference to the collection of the dataset&#39;s column names.
Definition: TRootDS.cxx:72
STL namespace.
std::vector< void * > GetColumnReadersImpl(std::string_view, const std::type_info &)
type-erased vector of pointers to pointers to column values - one per slot
Definition: TRootDS.cxx:16
std::vector< std::string > fListOfBranches
Definition: TRootDS.hxx:21
virtual Long64_t GetEntries() const
Return the total number of entries in the chain.
Definition: TChain.cxx:924
std::string GetTypeName(std::string_view colName) const
Type of a column as a string, e.g.
Definition: TRootDS.cxx:55
void InitSlot(unsigned int slot, ULong64_t firstEntry)
Convenience method called at the start of the data processing associated to a slot.
Definition: TRootDS.cxx:84
std::vector< double * > fAddressesToFree
Definition: TRootDS.hxx:20
std::vector< std::unique_ptr< TChain > > fChains
Definition: TRootDS.hxx:24
TRootDS(std::string_view treeName, std::string_view fileNameGlob)
Definition: TRootDS.cxx:38
void SetEntry(unsigned int slot, ULong64_t entry)
Advance the "cursors" returned by GetColumnReaders to the selected entry for a particular slot...
Definition: TRootDS.cxx:119
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Get entry from the file to memory.
Definition: TChain.cxx:948
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
unsigned long long ULong64_t
Definition: RtypesCore.h:70
int nentries
Definition: THbookFile.cxx:89
EDataType
Definition: TDataType.h:28
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
#define R__LOCKGUARD(mutex)
bool HasColumn(std::string_view colName) const
Checks if the dataset has a certain column.
Definition: TRootDS.cxx:77
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:2887
std::vector< std::pair< ULong64_t, ULong64_t > > fEntryRanges
Definition: TRootDS.hxx:22
TDataFrame MakeRootDataFrame(std::string_view treeName, std::string_view fileNameGlob)
Definition: TRootDS.cxx:153
Mother of all ROOT objects.
Definition: TObject.h:37
typedef void((*Func_t)())
std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *tree, TCustomColumnBase *tmpBranch, TDataSource *ds)
Return a string containing the type of the given branch.
Definition: TDFUtils.cxx:123
ROOT&#39;s TDataFrame offers a high level interface for analyses of data stored in TTrees.
Definition: TDataFrame.hxx:39
A chain is a collection of files containing TTree objects.
Definition: TChain.h:33
void SetNSlots(unsigned int nSlots)
Inform TDataSource of the number of processing slots (i.e.
Definition: TRootDS.cxx:124
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Set branch address.
Definition: TChain.cxx:2374
void ResetBit(UInt_t f)
Definition: TObject.h:171
void Initialise()
Convenience method called before starting an event-loop.
Definition: TRootDS.cxx:137
virtual TObjArray * GetListOfBranches()
Return a pointer to the list of branches of the current tree.
Definition: TChain.cxx:1071
char name[80]
Definition: TGX11.cxx:109
virtual Int_t Add(TChain *chain)
Add all files referenced by the passed chain to this chain.
Definition: TChain.cxx:221