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
29#include <array>
30#include <memory>
31#include <variant>
32
33class TBranch;
34class TFile;
35
36namespace ROOT {
37class REntry;
38class RFieldToken;
39class RNTupleFillContext;
40class RNTupleParallelWriter;
41class TBufferMerger;
42class TBufferMergerFile;
43} // namespace ROOT
44
45namespace ROOT::Internal::RDF {
46
47class R__CLING_PTRCHECK(off) UntypedSnapshotRNTupleHelper final : public RActionImpl<UntypedSnapshotRNTupleHelper> {
48 std::string fFileName;
49 std::string fDirName;
50 std::string fNTupleName;
51
52 std::unique_ptr<TFile> fOutputFile;
53
57 ColumnNames_t fInputFieldNames; // This contains the resolved aliases
59 std::unique_ptr<ROOT::RNTupleParallelWriter> fWriter;
60 std::vector<ROOT::RFieldToken> fFieldTokens;
61
62 unsigned int fNSlots;
63 std::vector<std::shared_ptr<ROOT::RNTupleFillContext>> fFillContexts;
64 std::vector<std::unique_ptr<ROOT::REntry>> fEntries;
65
66 std::vector<const std::type_info *> fInputColumnTypeIDs; // Types for the input columns
67
68public:
69 UntypedSnapshotRNTupleHelper(unsigned int nSlots, std::string_view filename, std::string_view dirname,
70 std::string_view ntuplename, const ColumnNames_t &vfnames, const ColumnNames_t &fnames,
73 const std::vector<const std::type_info *> &colTypeIDs);
74
80
81 void Initialize();
82
83 void Exec(unsigned int slot, const std::vector<void *> &values);
84
85 void InitTask(TTreeReader *, unsigned int slot);
86
87 void FinalizeTask(unsigned int slot);
88
89 void Finalize();
90
91 std::string GetActionName() { return "Snapshot"; }
92
94 {
95 return [](unsigned int, const RSampleInfo &) mutable {};
96 }
97
99};
100
101/// Stores properties of each output branch in a Snapshot.
103 /// Stores variations of a fundamental type.
104 /// The bytes hold anything up to double or 64-bit numbers, and are cleared for every event.
105 /// This allows for binding the branches directly to these bytes.
107 static constexpr std::size_t fNBytes = 8;
108 alignas(8) std::array<std::byte, fNBytes> fBytes{std::byte{0}}; // 8 bytes to store any fundamental type
109 unsigned short fSize = 0;
110 FundamentalType(unsigned short size) : fSize(size) { assert(size <= fNBytes); }
111 };
112 /// Stores empty instances of classes, so a dummy object can be written when a systematic variation
113 /// doesn't pass a selection cut.
115 const TClass *fTClass = nullptr;
116 std::shared_ptr<void> fEmptyInstance = nullptr;
117 void *fRawPtrToEmptyInstance = nullptr; // Needed because TTree expects pointer to pointer
118 };
119
120 std::string fInputBranchName; // This contains resolved aliases
121 std::string fOutputBranchName;
122 const std::type_info *fInputTypeID = nullptr;
123 TBranch *fOutputBranch = nullptr;
124 void *fBranchAddressForCArrays = nullptr; // Used to detect if branch addresses need to be updated
125
126 int fVariationIndex = -1; // For branches that are only valid if a specific filter passed
127 std::variant<FundamentalType, EmptyDynamicType> fTypeData = FundamentalType{0};
128 bool fIsCArray = false;
129 bool fIsDefine = false;
130
131 RBranchData() = default;
132 RBranchData(std::string inputBranchName, std::string outputBranchName, bool isDefine, const std::type_info *typeID);
133
135 {
136 fOutputBranch = nullptr;
137 fBranchAddressForCArrays = nullptr;
138 }
139 void *EmptyInstance(bool pointerToPointer);
140 void ClearBranchContents();
141 /// For fundamental types represented by TDataType, fetch a value from the pointer into the local branch buffer.
142 /// If the branch holds a class type, nothing happens.
143 /// \return true if the branch holds a fundamental type, false if it holds a class type.
145 {
146 if (auto fundamentalType = std::get_if<FundamentalType>(&fTypeData); fundamentalType) {
147 std::memcpy(fundamentalType->fBytes.data(), valuePtr, fundamentalType->fSize);
148 return true;
149 }
150 return false;
151 }
152};
153
154class R__CLING_PTRCHECK(off) UntypedSnapshotTTreeHelper final : public RActionImpl<UntypedSnapshotTTreeHelper> {
155 std::string fFileName;
156 std::string fDirName;
157 std::string fTreeName;
159 std::unique_ptr<TFile> fOutputFile;
160 std::unique_ptr<TTree> fOutputTree; // must be a ptr because TTrees are not copy/move constructible
161 bool fBranchAddressesNeedReset{true};
162 TTree *fInputTree = nullptr; // Current input tree. Set at initialization time (`InitTask`)
163 std::vector<RBranchData> fBranchData; // Information for all output branches
166
167public:
168 UntypedSnapshotTTreeHelper(std::string_view filename, std::string_view dirname, std::string_view treename,
170 const RSnapshotOptions &options, std::vector<bool> &&isDefine,
172 const std::vector<const std::type_info *> &colTypeIDs);
173
179
180 void InitTask(TTreeReader *, unsigned int);
181
182 void Exec(unsigned int, const std::vector<void *> &values);
183
184 void UpdateCArraysPtrs(const std::vector<void *> &values);
185
186 void SetBranches(const std::vector<void *> &values);
187
188 void SetEmptyBranches(TTree *inputTree, TTree &outputTree);
189
190 void Initialize();
191
192 void Finalize();
193
194 std::string GetActionName() { return "Snapshot"; }
195
197 {
198 return [this](unsigned int, const RSampleInfo &) mutable { fBranchAddressesNeedReset = true; };
199 }
200
201 UntypedSnapshotTTreeHelper MakeNew(void *newName, std::string_view /*variation*/ = "nominal");
202};
203
204class R__CLING_PTRCHECK(off) UntypedSnapshotTTreeHelperMT final : public RActionImpl<UntypedSnapshotTTreeHelperMT> {
205
206 // IMT-specific data members
207
208 unsigned int fNSlots;
209 std::unique_ptr<ROOT::TBufferMerger> fMerger; // must use a ptr because TBufferMerger is not movable
210 std::vector<std::shared_ptr<ROOT::TBufferMergerFile>> fOutputFiles;
211 std::vector<std::unique_ptr<TTree>> fOutputTrees;
212 std::vector<int> fBranchAddressesNeedReset; // vector<bool> does not allow concurrent writing of different elements
213 std::vector<TTree *> fInputTrees; // Current input trees, one per slot. Set at initialization time (`InitTask`)
214 std::vector<std::vector<RBranchData>> fBranchData; // Information for all output branches of each slot
215
216 // Attributes of the output TTree
217
218 std::string fFileName;
219 std::string fDirName;
220 std::string fTreeName;
221 TFile *fOutputFile; // Non-owning view on the output file
223
224 // Attributes related to the computation graph
225
228
229public:
230 UntypedSnapshotTTreeHelperMT(unsigned int nSlots, std::string_view filename, std::string_view dirname,
231 std::string_view treename, const ColumnNames_t &vbnames, const ColumnNames_t &bnames,
232 const RSnapshotOptions &options, std::vector<bool> &&isDefine,
234 const std::vector<const std::type_info *> &colTypeIDs);
235
241
242 void InitTask(TTreeReader *r, unsigned int slot);
243
244 void FinalizeTask(unsigned int slot);
245
246 void Exec(unsigned int slot, const std::vector<void *> &values);
247
248 void UpdateCArraysPtrs(unsigned int slot, const std::vector<void *> &values);
249
250 void SetBranches(unsigned int slot, const std::vector<void *> &values);
251
252 void SetEmptyBranches(TTree *inputTree, TTree &outputTree);
253
254 void Initialize();
255
256 void Finalize();
257
258 std::string GetActionName() { return "Snapshot"; }
259
261 {
262 return [this](unsigned int slot, const RSampleInfo &) mutable { fBranchAddressesNeedReset[slot] = 1; };
263 }
264
265 UntypedSnapshotTTreeHelperMT MakeNew(void *newName, std::string_view /*variation*/ = "nominal");
266};
267
268struct SnapshotOutputWriter;
269
270/// TTree snapshot helper with systematic variations.
272 : public ROOT::Detail::RDF::RActionImpl<SnapshotHelperWithVariations> {
274 std::shared_ptr<SnapshotOutputWriter> fOutputHandle;
275 TTree *fInputTree = nullptr; // Current input tree. Set at initialization time (`InitTask`)
276 std::vector<RBranchData> fBranchData;
277 ROOT::Detail::RDF::RLoopManager *fInputLoopManager = nullptr;
278 ROOT::Detail::RDF::RLoopManager *fOutputLoopManager = nullptr;
279
281
282public:
283 SnapshotHelperWithVariations(std::string_view filename, std::string_view dirname, std::string_view treename,
284 const ColumnNames_t & /*vbnames*/, const ColumnNames_t &bnames,
285 const RSnapshotOptions &options, std::vector<bool> && /*isDefine*/,
288 const std::vector<const std::type_info *> &colTypeIDs);
289
295
296 void RegisterVariedColumn(unsigned int slot, unsigned int columnIndex, unsigned int originalColumnIndex,
297 unsigned int varationIndex, std::string const &variationName);
298
299 void InitTask(TTreeReader *, unsigned int slot);
300
301 void Exec(unsigned int /*slot*/, const std::vector<void *> &values, std::vector<bool> const &filterPassed);
302
303 /// Nothing to do. All initialisations run in the constructor or InitTask().
304 void Initialize() {}
305
306 void Finalize();
307
308 std::string GetActionName() { return "SnapshotWithVariations"; }
309};
310
311} // namespace ROOT::Internal::RDF
312
313#endif
dim_t fSize
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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.
TTree snapshot helper with systematic variations.
std::shared_ptr< SnapshotOutputWriter > fOutputHandle
SnapshotHelperWithVariations(SnapshotHelperWithVariations const &) noexcept=delete
SnapshotHelperWithVariations(SnapshotHelperWithVariations &&) noexcept=default
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
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
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 empty instances of classes, so a dummy object can be written when a systematic variation doesn...
Stores variations of a fundamental type.
Stores properties of each output branch in a Snapshot.
bool WriteValueIfFundamental(void *valuePtr)
For fundamental types represented by TDataType, fetch a value from the pointer into the local branch ...
A collection of options to steer the creation of the dataset on file.