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 {
33class REntry;
34class RFieldToken;
35class RNTupleFillContext;
36class RNTupleParallelWriter;
37class TBufferMerger;
38class TBufferMergerFile;
39} // namespace ROOT
40
41namespace ROOT::Internal::RDF {
42
43class R__CLING_PTRCHECK(off) UntypedSnapshotRNTupleHelper final : public RActionImpl<UntypedSnapshotRNTupleHelper> {
44 std::string fFileName;
45 std::string fDirName;
46 std::string fNTupleName;
47
48 std::unique_ptr<TFile> fOutputFile;
49
53 ColumnNames_t fInputFieldNames; // This contains the resolved aliases
55 std::unique_ptr<ROOT::RNTupleParallelWriter> fWriter;
56 std::vector<ROOT::RFieldToken> fFieldTokens;
57
58 unsigned int fNSlots;
59 std::vector<std::shared_ptr<ROOT::RNTupleFillContext>> fFillContexts;
60 std::vector<std::unique_ptr<ROOT::REntry>> fEntries;
61
62 std::vector<const std::type_info *> fInputColumnTypeIDs; // Types for the input columns
63
64public:
65 UntypedSnapshotRNTupleHelper(unsigned int nSlots, std::string_view filename, std::string_view dirname,
66 std::string_view ntuplename, const ColumnNames_t &vfnames, const ColumnNames_t &fnames,
69 const std::vector<const std::type_info *> &colTypeIDs);
70
76
77 void Initialize();
78
79 void Exec(unsigned int slot, const std::vector<void *> &values);
80
81 void InitTask(TTreeReader *, unsigned int slot);
82
83 void FinalizeTask(unsigned int slot);
84
85 void Finalize();
86
87 std::string GetActionName() { return "Snapshot"; }
88
90 {
91 return [](unsigned int, const RSampleInfo &) mutable {};
92 }
93
95};
96
97/// Stores properties of each output branch in a Snapshot.
99 std::string fInputBranchName; // This contains resolved aliases
100 std::string fOutputBranchName;
101 const std::type_info *fInputTypeID = nullptr;
102 TBranch *fOutputBranch = nullptr;
103 void *fBranchAddressForCArrays = nullptr; // Used to detect if branch addresses need to be updated
104
105 std::unique_ptr<void, std::function<void(void *)>> fEmptyInstance;
106 bool fIsCArray = false;
107 bool fIsDefine = false;
108
109 RBranchData(std::string inputBranchName, std::string outputBranchName, bool isDefine, const std::type_info *typeID,
110 TBranch *outputBranch = nullptr)
111 : fInputBranchName{std::move(inputBranchName)},
112 fOutputBranchName{std::move(outputBranchName)},
113 fInputTypeID{typeID},
114 fOutputBranch{outputBranch},
115 fIsDefine(isDefine)
116 {
117 }
119 {
120 fOutputBranch = nullptr;
121 fBranchAddressForCArrays = nullptr;
122 }
123};
124
125class R__CLING_PTRCHECK(off) UntypedSnapshotTTreeHelper final : public RActionImpl<UntypedSnapshotTTreeHelper> {
126 std::string fFileName;
127 std::string fDirName;
128 std::string fTreeName;
130 std::unique_ptr<TFile> fOutputFile;
131 std::unique_ptr<TTree> fOutputTree; // must be a ptr because TTrees are not copy/move constructible
132 bool fBranchAddressesNeedReset{true};
133 TTree *fInputTree = nullptr; // Current input tree. Set at initialization time (`InitTask`)
134 std::vector<RBranchData> fBranchData; // Information for all output branches
137
138public:
139 UntypedSnapshotTTreeHelper(std::string_view filename, std::string_view dirname, std::string_view treename,
141 const RSnapshotOptions &options, std::vector<bool> &&isDefine,
143 const std::vector<const std::type_info *> &colTypeIDs);
144
150
151 void InitTask(TTreeReader *, unsigned int);
152
153 void Exec(unsigned int, const std::vector<void *> &values);
154
155 void UpdateCArraysPtrs(const std::vector<void *> &values);
156
157 void SetBranches(const std::vector<void *> &values);
158
159 void SetEmptyBranches(TTree *inputTree, TTree &outputTree);
160
161 void Initialize();
162
163 void Finalize();
164
165 std::string GetActionName() { return "Snapshot"; }
166
168 {
169 return [this](unsigned int, const RSampleInfo &) mutable { fBranchAddressesNeedReset = true; };
170 }
171
172 UntypedSnapshotTTreeHelper MakeNew(void *newName, std::string_view /*variation*/ = "nominal");
173};
174
175class R__CLING_PTRCHECK(off) UntypedSnapshotTTreeHelperMT final : public RActionImpl<UntypedSnapshotTTreeHelperMT> {
176
177 // IMT-specific data members
178
179 unsigned int fNSlots;
180 std::unique_ptr<ROOT::TBufferMerger> fMerger; // must use a ptr because TBufferMerger is not movable
181 std::vector<std::shared_ptr<ROOT::TBufferMergerFile>> fOutputFiles;
182 std::vector<std::unique_ptr<TTree>> fOutputTrees;
183 std::vector<int> fBranchAddressesNeedReset; // vector<bool> does not allow concurrent writing of different elements
184 std::vector<TTree *> fInputTrees; // Current input trees, one per slot. Set at initialization time (`InitTask`)
185 std::vector<std::vector<RBranchData>> fBranchData; // Information for all output branches of each slot
186
187 // Attributes of the output TTree
188
189 std::string fFileName;
190 std::string fDirName;
191 std::string fTreeName;
192 TFile *fOutputFile; // Non-owning view on the output file
194
195 // Attributes related to the computation graph
196
199
200public:
201 UntypedSnapshotTTreeHelperMT(unsigned int nSlots, std::string_view filename, std::string_view dirname,
202 std::string_view treename, const ColumnNames_t &vbnames, const ColumnNames_t &bnames,
203 const RSnapshotOptions &options, std::vector<bool> &&isDefine,
205 const std::vector<const std::type_info *> &colTypeIDs);
206
212
213 void InitTask(TTreeReader *r, unsigned int slot);
214
215 void FinalizeTask(unsigned int slot);
216
217 void Exec(unsigned int slot, const std::vector<void *> &values);
218
219 void UpdateCArraysPtrs(unsigned int slot, const std::vector<void *> &values);
220
221 void SetBranches(unsigned int slot, const std::vector<void *> &values);
222
223 void SetEmptyBranches(TTree *inputTree, TTree &outputTree);
224
225 void Initialize();
226
227 void Finalize();
228
229 std::string GetActionName() { return "Snapshot"; }
230
232 {
233 return [this](unsigned int slot, const RSampleInfo &) mutable { fBranchAddressesNeedReset[slot] = 1; };
234 }
235
236 UntypedSnapshotTTreeHelperMT MakeNew(void *newName, std::string_view /*variation*/ = "nominal");
237};
238
239} // namespace ROOT::Internal::RDF
240
241#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
Base class for action helpers, see RInterface::Book() for more information.
The head node of a RDF computation graph.
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< std::shared_ptr< ROOT::RNTupleFillContext > > fFillContexts
UntypedSnapshotRNTupleHelper(const UntypedSnapshotRNTupleHelper &)=delete
std::unique_ptr< ROOT::RNTupleParallelWriter > fWriter
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
std::vector< std::vector< RBranchData > > fBranchData
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::unique_ptr< TTree > > fOutputTrees
std::unique_ptr< ROOT::TBufferMerger > fMerger
UntypedSnapshotTTreeHelperMT(const UntypedSnapshotTTreeHelperMT &)=delete
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 ...
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
Stores properties of each output branch in a Snapshot.
RBranchData(std::string inputBranchName, std::string outputBranchName, bool isDefine, const std::type_info *typeID, TBranch *outputBranch=nullptr)
std::unique_ptr< void, std::function< void(void *)> fEmptyInstance)
A collection of options to steer the creation of the dataset on file.