Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RBatchSink.hxx
Go to the documentation of this file.
1// Author: Silia Taider, CERN 08/2026
2
3/*************************************************************************
4 * Copyright (C) 1995-2026, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#ifndef ROOT_INTERNAL_ML_RBATCHSINK
12#define ROOT_INTERNAL_ML_RBATCHSINK
13
14#include <cstddef>
15#include <memory>
16#include <string>
17#include <string_view>
18#include <vector>
19
20class TFile;
21class TTree;
22namespace ROOT {
23class RNTupleWriter;
24}
25
27
28struct RFlat2DMatrix;
29
30/**
31 * \struct RColumnLayout
32 * \brief Where one of the loader's columns lives inside a batch-tensor row and how it's shaped.
33 *
34 * A batch row is a flat span of floats in which a vector column occupies \p fWidth consecutive
35 * slots (padded to its specified maximum size). This describes the inverse mapping, so a sink can
36 * write each column back under its original name (unexpanded).
37 */
39 std::string fName; // the column's original name
40 std::size_t fOffset; // index of the column's first float within a row
41 std::size_t fWidth; // number of floats the column occupies
42 bool fIsVector; // write as a vector of \p fWidth floats rather than a single float
43};
44
45/**
46 * \class RBatchSink
47 * \brief Writes RFlat2DMatrix batches to disk letting RDataLoaderEngine::Save() stay agnostic to
48 * the output format
49 */
51protected:
52 std::vector<RColumnLayout> fLayout;
53 std::size_t fRowWidth{};
54
55 explicit RBatchSink(std::vector<RColumnLayout> layout) : fLayout(std::move(layout))
56 {
57 for (const auto &col : fLayout)
58 fRowWidth += col.fWidth;
59 }
60
61 /// \brief Copy one row of \p fRowWidth floats into the writer's buffers and write the entry
62 virtual void FillRow(const float *row) = 0;
63
64public:
65 RBatchSink(const RBatchSink &) = delete;
66 RBatchSink &operator=(const RBatchSink &) = delete;
67 RBatchSink(RBatchSink &&) = delete;
69 virtual ~RBatchSink() = default;
70
71 /// \brief Write every row of \p batch.
72 void FillBatch(const RFlat2DMatrix &batch);
73
74 /// \brief Flush everything to disk once after the last batch
75 virtual void Commit() = 0;
76};
77
78/// \brief Writes batches into a TTree, as one float leaf per scalar column and one
79/// std::vector<float> branch per vector column, matching the RNTuple sink's schema.
81 std::unique_ptr<TFile> fFile;
83 std::vector<float> fRow; // scalar branches are bound into this buffer
84 std::vector<std::vector<float>> fVectorBuffers; // one buffer per vector column, in fLayout order
85
86public:
87 RTTreeBatchSink(std::string_view dataset_name, std::string_view filename, std::vector<RColumnLayout> layout);
92 ~RTTreeBatchSink() override;
93
94 void FillRow(const float *row) override;
95 void Commit() override;
96};
97
98/// \brief Writes batches into an RNTuple, as one float field per scalar column and one
99/// std::vector<float> field per vector column.
101 std::vector<std::shared_ptr<float>> fScalarFields;
102 std::vector<std::shared_ptr<std::vector<float>>> fVectorFields;
103 std::vector<float *> fDestinations; // where each column's floats go
104 std::unique_ptr<ROOT::RNTupleWriter> fWriter;
105
106public:
107 RNTupleBatchSink(std::string_view dataset_name, std::string_view filename, std::vector<RColumnLayout> layout);
113
114 void FillRow(const float *row) override;
115 void Commit() override;
116};
117
118//////////////////////////////////////////////////////////////////////////
119/// \brief Create the sink matching \p format.
120/// \param dataset_name Name of the output TTree or RNTuple.
121/// \param filename Path of the output file, overwritten if it exists.
122/// \param layout Where each of the loader's columns sits in a batch row, see RColumnLayout.
123/// \param format Either "ttree" or "rntuple".
124std::unique_ptr<RBatchSink> CreateBatchSink(std::string_view dataset_name, std::string_view filename,
125 std::vector<RColumnLayout> layout, std::string_view format);
126
127} // namespace ROOT::Experimental::Internal::ML
128#endif // ROOT_INTERNAL_ML_RBATCHSINK
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 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 Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
Writes RFlat2DMatrix batches to disk letting RDataLoaderEngine::Save() stay agnostic to the output fo...
RBatchSink & operator=(const RBatchSink &)=delete
std::vector< RColumnLayout > fLayout
void FillBatch(const RFlat2DMatrix &batch)
Write every row of batch.
virtual void Commit()=0
Flush everything to disk once after the last batch.
virtual void FillRow(const float *row)=0
Copy one row of fRowWidth floats into the writer's buffers and write the entry.
RBatchSink(std::vector< RColumnLayout > layout)
RBatchSink & operator=(RBatchSink &&)=delete
Writes batches into an RNTuple, as one float field per scalar column and one std::vector<float> field...
std::vector< std::shared_ptr< std::vector< float > > > fVectorFields
void FillRow(const float *row) override
Copy one row of fRowWidth floats into the writer's buffers and write the entry.
RNTupleBatchSink(const RNTupleBatchSink &)=delete
RNTupleBatchSink & operator=(RNTupleBatchSink &&)=delete
void Commit() override
Flush everything to disk once after the last batch.
std::unique_ptr< ROOT::RNTupleWriter > fWriter
RNTupleBatchSink & operator=(const RNTupleBatchSink &)=delete
RNTupleBatchSink(std::string_view dataset_name, std::string_view filename, std::vector< RColumnLayout > layout)
std::vector< std::shared_ptr< float > > fScalarFields
Writes batches into a TTree, as one float leaf per scalar column and one std::vector<float> branch pe...
RTTreeBatchSink & operator=(const RTTreeBatchSink &)=delete
void Commit() override
Flush everything to disk once after the last batch.
RTTreeBatchSink(std::string_view dataset_name, std::string_view filename, std::vector< RColumnLayout > layout)
RTTreeBatchSink(const RTTreeBatchSink &)=delete
RTTreeBatchSink & operator=(RTTreeBatchSink &&)=delete
void FillRow(const float *row) override
Copy one row of fRowWidth floats into the writer's buffers and write the entry.
std::vector< std::vector< float > > fVectorBuffers
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
A TTree represents a columnar dataset.
Definition TTree.h:89
std::unique_ptr< RBatchSink > CreateBatchSink(std::string_view dataset_name, std::string_view filename, std::vector< RColumnLayout > layout, std::string_view format)
Create the sink matching format.
Where one of the loader's columns lives inside a batch-tensor row and how it's shaped.
Wrapper around ROOT::RVec<float> representing a 2D matrix.