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 RNTupleWriter;
34class REntry;
35class TBufferMerger;
36class TBufferMergerFile;
37} // namespace ROOT
38
39namespace ROOT::Internal::RDF {
40
42 std::vector<TBranch *> fBranches;
43 std::vector<std::string> fNames;
44 std::vector<bool> fIsCArray;
45
46public:
47 TBranch *Get(const std::string &name) const;
48 bool IsCArray(const std::string &name) const;
49 void Insert(const std::string &name, TBranch *address, bool isCArray = false);
50 void Clear();
52};
53
54class R__CLING_PTRCHECK(off) UntypedSnapshotRNTupleHelper final : public RActionImpl<UntypedSnapshotRNTupleHelper> {
55 std::string fFileName;
56 std::string fDirName;
57 std::string fNTupleName;
58
59 std::unique_ptr<TFile> fOutputFile;
60
64 ColumnNames_t fInputFieldNames; // This contains the resolved aliases
66 std::unique_ptr<ROOT::RNTupleWriter> fWriter;
67
69
70 std::vector<bool> fIsDefine;
71
72 std::vector<const std::type_info *> fInputColumnTypeIDs; // Types for the input columns
73
74public:
75 UntypedSnapshotRNTupleHelper(std::string_view filename, std::string_view dirname, std::string_view ntuplename,
79 const std::vector<const std::type_info *> &colTypeIDs);
80
86
87 void InitTask(TTreeReader *, unsigned int /* slot */) {}
88
89 void Exec(unsigned int /* slot */, const std::vector<void *> &values);
90
91 void Initialize();
92
93 void Finalize();
94
95 std::string GetActionName() { return "Snapshot"; }
96
98 {
99 return [](unsigned int, const RSampleInfo &) mutable {};
100 }
101
103};
104
105class R__CLING_PTRCHECK(off) UntypedSnapshotTTreeHelper final : public RActionImpl<UntypedSnapshotTTreeHelper> {
106 std::string fFileName;
107 std::string fDirName;
108 std::string fTreeName;
110 std::unique_ptr<TFile> fOutputFile;
111 std::unique_ptr<TTree> fOutputTree; // must be a ptr because TTrees are not copy/move constructible
112 bool fBranchAddressesNeedReset{true};
113 ColumnNames_t fInputBranchNames; // This contains the resolved aliases
115 TTree *fInputTree = nullptr; // Current input tree. Set at initialization time (`InitTask`)
116 // TODO we might be able to unify fBranches, fBranchAddresses and fOutputBranches
117 std::vector<TBranch *> fBranches; // Addresses of branches in output, non-null only for the ones holding C arrays
118 std::vector<void *> fBranchAddresses; // Addresses of objects associated to output branches
120 std::vector<bool> fIsDefine;
123 std::vector<const std::type_info *> fInputColumnTypeIDs; // Types for the input columns
124
125public:
126 UntypedSnapshotTTreeHelper(std::string_view filename, std::string_view dirname, std::string_view treename,
128 const RSnapshotOptions &options, std::vector<bool> &&isDefine,
130 const std::vector<const std::type_info *> &colTypeIDs);
131
137
138 void InitTask(TTreeReader *, unsigned int);
139
140 void Exec(unsigned int, const std::vector<void *> &values);
141
142 void UpdateCArraysPtrs(const std::vector<void *> &values);
143
144 void SetBranches(const std::vector<void *> &values);
145
146 void SetEmptyBranches(TTree *inputTree, TTree &outputTree);
147
148 void Initialize();
149
150 void Finalize();
151
152 std::string GetActionName() { return "Snapshot"; }
153
155 {
156 return [this](unsigned int, const RSampleInfo &) mutable { fBranchAddressesNeedReset = true; };
157 }
158
159 UntypedSnapshotTTreeHelper MakeNew(void *newName, std::string_view /*variation*/ = "nominal");
160};
161
162class R__CLING_PTRCHECK(off) UntypedSnapshotTTreeHelperMT final : public RActionImpl<UntypedSnapshotTTreeHelperMT> {
163
164 // IMT-specific data members
165
166 unsigned int fNSlots;
167 std::unique_ptr<ROOT::TBufferMerger> fMerger; // must use a ptr because TBufferMerger is not movable
168 std::vector<std::shared_ptr<ROOT::TBufferMergerFile>> fOutputFiles;
169 std::vector<std::unique_ptr<TTree>> fOutputTrees;
170 std::vector<int> fBranchAddressesNeedReset; // vector<bool> does not allow concurrent writing of different elements
171 std::vector<TTree *> fInputTrees; // Current input trees, one per slot. Set at initialization time (`InitTask`)
172 // Addresses of branches in output per slot, non-null only for the ones holding C arrays
173 std::vector<std::vector<TBranch *>> fBranches;
174 // Addresses of objects associated to output branches per slot, non-null only for the ones holding C arrays
175 std::vector<std::vector<void *>> fBranchAddresses;
176 std::vector<RBranchSet> fOutputBranches; // Unique set of output branches, one per slot.
177
178 // Attributes of the output TTree
179
180 std::string fFileName;
181 std::string fDirName;
182 std::string fTreeName;
183 TFile *fOutputFile; // Non-owning view on the output file
185 std::vector<std::string> fOutputBranchNames;
186
187 // Attributes related to the computation graph
188
191 std::vector<std::string> fInputBranchNames; // This contains the resolved aliases
192 std::vector<const std::type_info *> fInputColumnTypeIDs; // Types for the input columns
193
194 std::vector<bool> fIsDefine;
195
196public:
197 UntypedSnapshotTTreeHelperMT(unsigned int nSlots, std::string_view filename, std::string_view dirname,
198 std::string_view treename, const ColumnNames_t &vbnames, const ColumnNames_t &bnames,
199 const RSnapshotOptions &options, std::vector<bool> &&isDefine,
201 const std::vector<const std::type_info *> &colTypeIDs);
202
208
209 void InitTask(TTreeReader *r, unsigned int slot);
210
211 void FinalizeTask(unsigned int slot);
212
213 void Exec(unsigned int slot, const std::vector<void *> &values);
214
215 void UpdateCArraysPtrs(unsigned int slot, const std::vector<void *> &values);
216
217 void SetBranches(unsigned int slot, const std::vector<void *> &values);
218
219 void SetEmptyBranches(TTree *inputTree, TTree &outputTree);
220
221 void Initialize();
222
223 void Finalize();
224
225 std::string GetActionName() { return "Snapshot"; }
226
228 {
229 return [this](unsigned int slot, const RSampleInfo &) mutable { fBranchAddressesNeedReset[slot] = 1; };
230 }
231
232 UntypedSnapshotTTreeHelperMT MakeNew(void *newName, std::string_view /*variation*/ = "nominal");
233};
234
235} // namespace ROOT::Internal::RDF
236
237#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::RNTupleWriter > fWriter
UntypedSnapshotRNTupleHelper & operator=(const UntypedSnapshotRNTupleHelper &)=delete
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 ...
The REntry is a collection of values in an RNTuple corresponding to a complete row in the data set.
Definition REntry.hxx:54
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
Namespace for new ROOT classes and functions.
A collection of options to steer the creation of the dataset on file.