Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RTTreeDS.hxx
Go to the documentation of this file.
1/**
2 \file ROOT/RTTreeDS.hxx
3 \ingroup dataframe
4 \author Vincenzo Eduardo Padulano
5 \date 2024-12
6*/
7
8/*************************************************************************
9 * Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#ifndef ROOT_INTERNAL_RDF_RTTREEDS
17#define ROOT_INTERNAL_RDF_RTTREEDS
18
19#include "ROOT/RDataSource.hxx"
20
21#include <memory>
22#include <string>
23#include <vector>
24#include <stdexcept>
25#include <string_view>
26
27// Begin forward decls
28
29namespace ROOT {
30class RDataFrame;
31}
32
33namespace ROOT::Detail::RDF {
34class RLoopManager;
35}
36
37namespace ROOT::RDF {
38class RSampleInfo;
39}
40
42class RSample;
43}
44
45namespace ROOT::TreeUtils {
46struct RFriendInfo;
47}
48
52
53class TChain;
54class TDirectory;
55class TTree;
56class TTreeReader;
57
58// End forward decls
59
60namespace ROOT::Internal::RDF {
61
63 std::vector<std::string> fBranchNamesWithDuplicates{};
64 std::vector<std::string> fBranchNamesWithoutDuplicates{};
65 std::vector<std::string> fTopLevelBranchNames{};
66
67 std::shared_ptr<TTree> fTree;
68
69 std::unique_ptr<TTreeReader> fTreeReader;
70
71 std::vector<std::unique_ptr<TChain>> fFriends;
72
73 // Should be needed mostly for MT runs, but we keep it here to document and align the existing functionality
74 // from RLoopManager. See https://github.com/root-project/root/pull/10729
75 std::unique_ptr<ROOT::Internal::TreeUtils::RNoCleanupNotifier> fNoCleanupNotifier;
76
78 CreateSampleInfo(unsigned int,
79 const std::unordered_map<std::string, ROOT::RDF::Experimental::RSample *> &sampleMap) const final;
80
81 void RunFinalChecks(bool nodesLeftNotRun) const final;
82
83 void Setup(std::shared_ptr<TTree> &&tree, const ROOT::TreeUtils::RFriendInfo *friendInfo = nullptr);
84
85 std::vector<std::pair<ULong64_t, ULong64_t>> GetTTreeEntryRange(TTree &tree);
86 std::vector<std::pair<ULong64_t, ULong64_t>> GetTChainEntryRange(TChain &chain);
87
88public:
89 RTTreeDS(std::shared_ptr<TTree> tree);
90 RTTreeDS(std::shared_ptr<TTree> tree, const ROOT::TreeUtils::RFriendInfo &friendInfo);
91 RTTreeDS(std::string_view treeName, TDirectory *dirPtr);
92 RTTreeDS(std::string_view treeName, std::string_view fileNameGlob);
93 RTTreeDS(std::string_view treeName, const std::vector<std::string> &fileNameGlobs);
94
95 // Rule of five
96 RTTreeDS(const RTTreeDS &) = delete;
97 RTTreeDS &operator=(const RTTreeDS &) = delete;
98 RTTreeDS(RTTreeDS &&) = delete;
100 ~RTTreeDS() final; // Define destructor where data member types are defined
101
102 void Initialize() final;
103
104 void Finalize() final;
105
106 std::vector<std::pair<ULong64_t, ULong64_t>> GetEntryRanges() final;
107
108 const std::vector<std::string> &GetColumnNames() const final { return fBranchNamesWithDuplicates; }
109
110 bool HasColumn(std::string_view colName) const final
111 {
112 return std::find(fBranchNamesWithDuplicates.begin(), fBranchNamesWithDuplicates.end(), colName) !=
114 }
115
116 std::string GetTypeName(std::string_view colName) const final;
117
118 std::string GetTypeNameWithOpts(std::string_view colName, bool vector2RVec) const final;
119
120 bool SetEntry(unsigned int, ULong64_t entry) final;
121
122 Record_t GetColumnReadersImpl(std::string_view /* name */, const std::type_info & /* ti */) final
123 {
124 // This datasource uses the newer GetColumnReaders() API
125 return {};
126 }
127
128 std::unique_ptr<ROOT::Detail::RDF::RColumnReaderBase>
129 GetColumnReaders(unsigned int, std::string_view, const std::type_info &) final
130 {
131 // This data source creates column readers via CreateColumnReader
132 throw std::runtime_error("GetColumnReaders should not be called on this data source, something wrong happened!");
133 }
134
135 std::unique_ptr<ROOT::Detail::RDF::RColumnReaderBase> CreateColumnReader(unsigned int slot, std::string_view col,
136 const std::type_info &tid,
137 TTreeReader *treeReader) final;
138
139 std::string GetLabel() final { return "TTreeDS"; }
140
141 TTree *GetTree();
142
143 const std::vector<std::string> &GetTopLevelFieldNames() const final { return fTopLevelBranchNames; }
144
145 const std::vector<std::string> &GetColumnNamesNoDuplicates() const final { return fBranchNamesWithoutDuplicates; }
146
147 void InitializeWithOpts(const std::set<std::string> &suppressErrorsForMissingBranches) final;
148
149 std::string DescribeDataset() final;
150
151 std::string AsString() final { return "TTree data source"; }
152
153 std::size_t GetNFiles() const final;
154
155 void ProcessMT(ROOT::Detail::RDF::RLoopManager &lm) final;
156};
157
158ROOT::RDataFrame FromTTree(std::string_view treeName, std::string_view fileNameGlob);
159ROOT::RDataFrame FromTTree(std::string_view treeName, const std::vector<std::string> &fileNameGlobs);
160
161} // namespace ROOT::Internal::RDF
162
163#endif
unsigned long long ULong64_t
Portable unsigned long integer 8 bytes.
Definition RtypesCore.h:84
The head node of a RDF computation graph.
std::vector< std::pair< ULong64_t, ULong64_t > > GetTTreeEntryRange(TTree &tree)
Definition RTTreeDS.cxx:554
std::vector< std::string > fBranchNamesWithoutDuplicates
Definition RTTreeDS.hxx:64
RTTreeDS & operator=(RTTreeDS &&)=delete
std::size_t GetNFiles() const final
Returns the number of files from which the dataset is constructed.
Definition RTTreeDS.cxx:421
std::string DescribeDataset() final
Definition RTTreeDS.cxx:430
std::string GetLabel() final
Return a string representation of the datasource type.
Definition RTTreeDS.hxx:139
std::unique_ptr< ROOT::Internal::TreeUtils::RNoCleanupNotifier > fNoCleanupNotifier
Definition RTTreeDS.hxx:75
const std::vector< std::string > & GetColumnNamesNoDuplicates() const final
Definition RTTreeDS.hxx:145
const std::vector< std::string > & GetColumnNames() const final
Returns a reference to the collection of the dataset's column names.
Definition RTTreeDS.hxx:108
void Setup(std::shared_ptr< TTree > &&tree, const ROOT::TreeUtils::RFriendInfo *friendInfo=nullptr)
Definition RTTreeDS.cxx:254
std::vector< std::unique_ptr< TChain > > fFriends
Definition RTTreeDS.hxx:71
void Initialize() final
Convenience method called before starting an event-loop.
Definition RTTreeDS.cxx:616
const std::vector< std::string > & GetTopLevelFieldNames() const final
Definition RTTreeDS.hxx:143
RTTreeDS & operator=(const RTTreeDS &)=delete
bool HasColumn(std::string_view colName) const final
Checks if the dataset has a certain column.
Definition RTTreeDS.hxx:110
std::unique_ptr< TTreeReader > fTreeReader
Definition RTTreeDS.hxx:69
std::unique_ptr< ROOT::Detail::RDF::RColumnReaderBase > GetColumnReaders(unsigned int, std::string_view, const std::type_info &) final
If the other GetColumnReaders overload returns an empty vector, this overload will be called instead.
Definition RTTreeDS.hxx:129
void Finalize() final
Convenience method called after concluding an event-loop.
Definition RTTreeDS.cxx:608
RTTreeDS(RTTreeDS &&)=delete
RTTreeDS(const RTTreeDS &)=delete
bool SetEntry(unsigned int, ULong64_t entry) final
Advance the "cursors" returned by GetColumnReaders to the selected entry for a particular slot.
Definition RTTreeDS.cxx:520
void InitializeWithOpts(const std::set< std::string > &suppressErrorsForMissingBranches) final
Definition RTTreeDS.cxx:630
std::vector< std::string > fTopLevelBranchNames
Definition RTTreeDS.hxx:65
std::vector< std::pair< ULong64_t, ULong64_t > > GetTChainEntryRange(TChain &chain)
Definition RTTreeDS.cxx:564
Record_t GetColumnReadersImpl(std::string_view, const std::type_info &) final
type-erased vector of pointers to pointers to column values - one per slot
Definition RTTreeDS.hxx:122
std::vector< std::string > fBranchNamesWithDuplicates
Definition RTTreeDS.hxx:63
RTTreeDS(std::shared_ptr< TTree > tree)
Definition RTTreeDS.cxx:274
std::vector< std::pair< ULong64_t, ULong64_t > > GetEntryRanges() final
Return ranges of entries to distribute to tasks.
Definition RTTreeDS.cxx:585
std::string AsString() final
Definition RTTreeDS.hxx:151
std::shared_ptr< TTree > fTree
Definition RTTreeDS.hxx:67
std::string GetTypeName(std::string_view colName) const final
Type of a column as a string, e.g.
Definition RTTreeDS.cxx:541
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
friend std::string ROOT::Internal::RDF::GetTypeNameWithOpts(const RDataSource &, std::string_view, bool)
friend ROOT::RDF::RSampleInfo ROOT::Internal::RDF::CreateSampleInfo(const ROOT::RDF::RDataSource &, unsigned int, const std::unordered_map< std::string, ROOT::RDF::Experimental::RSample * > &)
std::vector< void * > Record_t
friend std::unique_ptr< ROOT::Detail::RDF::RColumnReaderBase > ROOT::Internal::RDF::CreateColumnReader(ROOT::RDF::RDataSource &, unsigned int, std::string_view, const std::type_info &, TTreeReader *)
friend void ROOT::Internal::RDF::ProcessMT(RDataSource &, ROOT::Detail::RDF::RLoopManager &)
friend void ROOT::Internal::RDF::RunFinalChecks(const ROOT::RDF::RDataSource &, bool)
This type represents a sample identifier, to be used in conjunction with RDataFrame features such as ...
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
A chain is a collection of files containing TTree objects.
Definition TChain.h:33
Describe directory structure in memory.
Definition TDirectory.h:45
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:46
A TTree represents a columnar dataset.
Definition TTree.h:89
ROOT::RDataFrame FromTTree(std::string_view treeName, std::string_view fileNameGlob)
Definition RTTreeDS.cxx:349
Namespace hosting functions and classes to retrieve tree information for internal use.
Definition RTTreeDS.hxx:49
Namespace for new ROOT classes and functions.
Information about friend trees of a certain TTree or TChain object.