Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
SnapshotHelpers.hxx
Go to the documentation of this file.
1/**
2 \file ROOT/RDF/SnapshotHelpers.hxx
3 \ingroup dataframe
4 \author Enrico Guiraud, CERN
5 \author Danilo Piparo, CERN
6 \date 2016-12
7 \author Vincenzo Eduardo Padulano
8 \author Stephan Hageboeck
9 \date 2025-06
10*/
11
12/*************************************************************************
13 * Copyright (C) 1995-2025, Rene Brun and Fons Rademakers. *
14 * All rights reserved. *
15 * *
16 * For the licensing terms see $ROOTSYS/LICENSE. *
17 * For the list of contributors see $ROOTSYS/README/CREDITS. *
18 *************************************************************************/
19
20#ifndef RDF_SNAPSHOTHELPERS
21#define RDF_SNAPSHOTHELPERS
22
24
27#include <ROOT/RDF/Utils.hxx>
28
29class TBranch;
30class TFile;
31
32namespace ROOT {
33namespace Experimental {
34class RNTupleFillContext;
35class RNTupleParallelWriter;
36} // namespace Experimental
37class REntry;
38class RFieldToken;
39class TBufferMerger;
40class TBufferMergerFile;
41} // namespace ROOT
42
43namespace ROOT::Internal::RDF {
44
46 std::vector<TBranch *> fBranches;
47 std::vector<std::string> fNames;
48 std::vector<bool> fIsCArray;
49
50public:
51 TBranch *Get(const std::string &name) const;
52 bool IsCArray(const std::string &name) const;
53 void Insert(const std::string &name, TBranch *address, bool isCArray = false);
54 void Clear();
56};
57
58class R__CLING_PTRCHECK(off) UntypedSnapshotRNTupleHelper final : public RActionImpl<UntypedSnapshotRNTupleHelper> {
59 std::string fFileName;
60 std::string fDirName;
61 std::string fNTupleName;
62
63 std::unique_ptr<TFile> fOutputFile;
64
68 ColumnNames_t fInputFieldNames; // This contains the resolved aliases
70 std::unique_ptr<ROOT::Experimental::RNTupleParallelWriter> fWriter;
71 std::vector<ROOT::RFieldToken> fFieldTokens;
72
73 unsigned int fNSlots;
74 std::vector<std::shared_ptr<ROOT::Experimental::RNTupleFillContext>> fFillContexts;
75 std::vector<std::unique_ptr<ROOT::REntry>> fEntries;
76
77 std::vector<const std::type_info *> fInputColumnTypeIDs; // Types for the input columns
78
79public:
80 UntypedSnapshotRNTupleHelper(unsigned int nSlots, std::string_view filename, std::string_view dirname,
81 std::string_view ntuplename, const ColumnNames_t &vfnames, const ColumnNames_t &fnames,
84 const std::vector<const std::type_info *> &colTypeIDs);
85
91
92 void Initialize();
93
94 void Exec(unsigned int slot, const std::vector<void *> &values);
95
96 void InitTask(TTreeReader *, unsigned int slot);
97
98 void FinalizeTask(unsigned int slot);
99
100 void Finalize();
101
102 std::string GetActionName() { return "Snapshot"; }
103
105 {
106 return [](unsigned int, const RSampleInfo &) mutable {};
107 }
108
110};
111
112class R__CLING_PTRCHECK(off) UntypedSnapshotTTreeHelper final : public RActionImpl<UntypedSnapshotTTreeHelper> {
113 std::string fFileName;
114 std::string fDirName;
115 std::string fTreeName;
117 std::unique_ptr<TFile> fOutputFile;
118 std::unique_ptr<TTree> fOutputTree; // must be a ptr because TTrees are not copy/move constructible
119 bool fBranchAddressesNeedReset{true};
120 ColumnNames_t fInputBranchNames; // This contains the resolved aliases
122 TTree *fInputTree = nullptr; // Current input tree. Set at initialization time (`InitTask`)
123 // TODO we might be able to unify fBranches, fBranchAddresses and fOutputBranches
124 std::vector<TBranch *> fBranches; // Addresses of branches in output, non-null only for the ones holding C arrays
125 std::vector<void *> fBranchAddresses; // Addresses of objects associated to output branches
127 std::vector<bool> fIsDefine;
130 std::vector<const std::type_info *> fInputColumnTypeIDs; // Types for the input columns
131
132public:
133 UntypedSnapshotTTreeHelper(std::string_view filename, std::string_view dirname, std::string_view treename,
135 const RSnapshotOptions &options, std::vector<bool> &&isDefine,
137 const std::vector<const std::type_info *> &colTypeIDs);
138
144
145 void InitTask(TTreeReader *, unsigned int);
146
147 void Exec(unsigned int, const std::vector<void *> &values);
148
149 void UpdateCArraysPtrs(const std::vector<void *> &values);
150
151 void SetBranches(const std::vector<void *> &values);
152
153 void SetEmptyBranches(TTree *inputTree, TTree &outputTree);
154
155 void Initialize();
156
157 void Finalize();
158
159 std::string GetActionName() { return "Snapshot"; }
160
162 {
163 return [this](unsigned int, const RSampleInfo &) mutable { fBranchAddressesNeedReset = true; };
164 }
165
166 UntypedSnapshotTTreeHelper MakeNew(void *newName, std::string_view /*variation*/ = "nominal");
167};
168
169class R__CLING_PTRCHECK(off) UntypedSnapshotTTreeHelperMT final : public RActionImpl<UntypedSnapshotTTreeHelperMT> {
170
171 // IMT-specific data members
172
173 unsigned int fNSlots;
174 std::unique_ptr<ROOT::TBufferMerger> fMerger; // must use a ptr because TBufferMerger is not movable
175 std::vector<std::shared_ptr<ROOT::TBufferMergerFile>> fOutputFiles;
176 std::vector<std::unique_ptr<TTree>> fOutputTrees;
177 std::vector<int> fBranchAddressesNeedReset; // vector<bool> does not allow concurrent writing of different elements
178 std::vector<TTree *> fInputTrees; // Current input trees, one per slot. Set at initialization time (`InitTask`)
179 // Addresses of branches in output per slot, non-null only for the ones holding C arrays
180 std::vector<std::vector<TBranch *>> fBranches;
181 // Addresses of objects associated to output branches per slot, non-null only for the ones holding C arrays
182 std::vector<std::vector<void *>> fBranchAddresses;
183 std::vector<RBranchSet> fOutputBranches; // Unique set of output branches, one per slot.
184
185 // Attributes of the output TTree
186
187 std::string fFileName;
188 std::string fDirName;
189 std::string fTreeName;
190 TFile *fOutputFile; // Non-owning view on the output file
192 std::vector<std::string> fOutputBranchNames;
193
194 // Attributes related to the computation graph
195
198 std::vector<std::string> fInputBranchNames; // This contains the resolved aliases
199 std::vector<const std::type_info *> fInputColumnTypeIDs; // Types for the input columns
200
201 std::vector<bool> fIsDefine;
202
203public:
204 UntypedSnapshotTTreeHelperMT(unsigned int nSlots, std::string_view filename, std::string_view dirname,
205 std::string_view treename, const ColumnNames_t &vbnames, const ColumnNames_t &bnames,
206 const RSnapshotOptions &options, std::vector<bool> &&isDefine,
208 const std::vector<const std::type_info *> &colTypeIDs);
209
215
216 void InitTask(TTreeReader *r, unsigned int slot);
217
218 void FinalizeTask(unsigned int slot);
219
220 void Exec(unsigned int slot, const std::vector<void *> &values);
221
222 void UpdateCArraysPtrs(unsigned int slot, const std::vector<void *> &values);
223
224 void SetBranches(unsigned int slot, const std::vector<void *> &values);
225
226 void SetEmptyBranches(TTree *inputTree, TTree &outputTree);
227
228 void Initialize();
229
230 void Finalize();
231
232 std::string GetActionName() { return "Snapshot"; }
233
235 {
236 return [this](unsigned int slot, const RSampleInfo &) mutable { fBranchAddressesNeedReset[slot] = 1; };
237 }
238
239 UntypedSnapshotTTreeHelperMT MakeNew(void *newName, std::string_view /*variation*/ = "nominal");
240};
241
242} // namespace ROOT::Internal::RDF
243
244#endif
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
char name[80]
Definition TGX11.cxx:110
Base class for action helpers, see RInterface::Book() for more information.
The head node of a RDF computation graph.
void Insert(const std::string &name, TBranch *address, bool isCArray=false)
std::vector< TBranch * > fBranches
std::vector< std::string > fNames
bool IsCArray(const std::string &name) const
TBranch * Get(const std::string &name) const
ROOT::RDF::SampleCallback_t GetSampleCallback() final
Override this method to register a callback that is executed before the processing a new data sample ...
UntypedSnapshotRNTupleHelper(const UntypedSnapshotRNTupleHelper &)=delete
std::unique_ptr< ROOT::Experimental::RNTupleParallelWriter > fWriter
std::vector< std::shared_ptr< ROOT::Experimental::RNTupleFillContext > > fFillContexts
UntypedSnapshotRNTupleHelper & operator=(const UntypedSnapshotRNTupleHelper &)=delete
std::vector< std::unique_ptr< ROOT::REntry > > fEntries
std::vector< const std::type_info * > fInputColumnTypeIDs
ROOT::Detail::RDF::RLoopManager * fOutputLoopManager
UntypedSnapshotRNTupleHelper(UntypedSnapshotRNTupleHelper &&) noexcept
ROOT::Detail::RDF::RLoopManager * fInputLoopManager
ROOT::Detail::RDF::RLoopManager * fInputLoopManager
UntypedSnapshotTTreeHelperMT(UntypedSnapshotTTreeHelperMT &&) noexcept
ROOT::Detail::RDF::RLoopManager * fOutputLoopManager
std::vector< std::shared_ptr< ROOT::TBufferMergerFile > > fOutputFiles
ROOT::RDF::SampleCallback_t GetSampleCallback() final
Override this method to register a callback that is executed before the processing a new data sample ...
UntypedSnapshotTTreeHelperMT & operator=(const UntypedSnapshotTTreeHelperMT &)=delete
std::vector< std::vector< TBranch * > > fBranches
std::vector< std::unique_ptr< TTree > > fOutputTrees
std::vector< const std::type_info * > fInputColumnTypeIDs
std::unique_ptr< ROOT::TBufferMerger > fMerger
UntypedSnapshotTTreeHelperMT(const UntypedSnapshotTTreeHelperMT &)=delete
std::vector< std::vector< void * > > fBranchAddresses
ROOT::Detail::RDF::RLoopManager * fOutputLoopManager
ROOT::Detail::RDF::RLoopManager * fInputLoopManager
UntypedSnapshotTTreeHelper(UntypedSnapshotTTreeHelper &&) noexcept
UntypedSnapshotTTreeHelper(const UntypedSnapshotTTreeHelper &)=delete
UntypedSnapshotTTreeHelper & operator=(const UntypedSnapshotTTreeHelper &)=delete
ROOT::RDF::SampleCallback_t GetSampleCallback() final
Override this method to register a callback that is executed before the processing a new data sample ...
std::vector< const std::type_info * > fInputColumnTypeIDs
This type represents a sample identifier, to be used in conjunction with RDataFrame features such as ...
A TTree is a list of TBranches.
Definition TBranch.h:93
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
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
std::function< void(unsigned int, const ROOT::RDF::RSampleInfo &)> SampleCallback_t
The type of a data-block callback, registered with an RDataFrame computation graph via e....
std::vector< std::string > ColumnNames_t
A collection of options to steer the creation of the dataset on file.