Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleWriter.hxx
Go to the documentation of this file.
1/// \file ROOT/RNTupleWriter.hxx
2/// \ingroup NTuple
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2024-02-20
5
6/*************************************************************************
7 * Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. *
8 * All rights reserved. *
9 * *
10 * For the licensing terms see $ROOTSYS/LICENSE. *
11 * For the list of contributors see $ROOTSYS/README/CREDITS. *
12 *************************************************************************/
13
14#ifndef ROOT_RNTupleWriter
15#define ROOT_RNTupleWriter
16
17#include <ROOT/RConfig.hxx> // for R__unlikely
18#include <ROOT/REntry.hxx>
19#include <ROOT/RError.hxx>
23#include <ROOT/RNTupleModel.hxx>
24#include <ROOT/RNTupleTypes.hxx>
25#include <ROOT/RPageStorage.hxx>
27
28#include <cstddef>
29#include <cstdint>
30#include <memory>
31#include <string_view>
32#include <utility>
33
34class TDirectory;
35
36namespace ROOT {
37
38class RNTupleWriteOptions;
39
40namespace Experimental {
41class RNTupleAttrSetWriterHandle;
42class RFile;
43
44/// Creates an RNTupleWriter that writes into the given `file`, appending to it. The RNTuple is written under the
45/// path `ntuplePath`.
46/// `ntuplePath` may have the form `"path/to/ntuple"`, in which case the ntuple's name will be `"ntuple"` and it will
47/// be stored under the given `ntuplePath` in the RFile.
48/// Throws an exception if the model is null.
49/// NOTE: this is a temporary, experimental API that will be replaced by an overload of RNTupleWriter::Append in the
50/// future.
51std::unique_ptr<RNTupleWriter>
52RNTupleWriter_Append(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view ntuplePath,
55} // namespace Experimental
56
57namespace Internal {
58// Non-public factory method for an RNTuple writer that uses an already constructed page sink
59std::unique_ptr<RNTupleWriter>
60CreateRNTupleWriter(std::unique_ptr<ROOT::RNTupleModel> model, std::unique_ptr<Internal::RPageSink> sink);
61} // namespace Internal
62
63// clang-format off
64/**
65\class ROOT::RNTupleWriter
66\ingroup NTuple
67\brief An RNTuple that gets filled with entries (data) and writes them to storage
68
69RNTupleWriter is an interface for writing RNTuples to storage. It can be instantiated using the static functions
70Append() and Recreate(), providing an RNTupleModel that defines the schema of the data to be written.
71
72An RNTuple can be thought of as a table, whose columns are defined by its schema (i.e. by its associated RNTupleModel,
73whose Fields map to 0 or more columns).
74Writing into an RNTuple happens by filling *entries* into the RNTupleWriter, which make up the rows of the table.
75The simplest way to do so is by:
76
77- retrieving a (shared) pointer to each Field's value;
78- writing a value into each pointer;
79- calling `writer->Fill()` to commit the entry with all the current pointer values.
80
81~~~ {.cpp}
82#include <ROOT/RNTupleWriter.hxx>
83
84/// 1. Create the model.
85auto model = ROOT::RNTupleModel::Create();
86// Define the schema by adding Fields to the model.
87// MakeField returns a shared_ptr to the value to be written (in this case, a shared_ptr<int>)
88auto pFoo = model->MakeField<int>("foo");
89
90/// 2. Create writer from the model.
91auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), "myNTuple", "some/file.root");
92
93/// 3. Write into it.
94for (int i = 0; i < 10; ++i) {
95 // Assign the value you want to each RNTuple Field (in this case there is only one Field "foo").
96 *pFoo = i;
97
98 // Fill() writes the entire entry to the RNTuple.
99 // After calling Fill() you can safely write another value into `pFoo` knowing that the previous one was
100 // already saved.
101 writer->Fill();
102}
103
104// On destruction, the writer will flush the written data to disk.
105~~~
106
107The caller has to make sure that the data that gets filled into an RNTuple is not modified for the time of the
108Fill() call. The Fill call serializes the C++ object into the column format and
109writes data into the corresponding column page buffers.
110
111The actual writing of the buffers to storage is deferred and can be triggered by FlushCluster() or by
112destructing the writer.
113
114On I/O errors, a ROOT::RException is thrown.
115
116*/
117// clang-format on
120 friend std::unique_ptr<RNTupleWriter>
121 Internal::CreateRNTupleWriter(std::unique_ptr<ROOT::RNTupleModel>, std::unique_ptr<Internal::RPageSink>);
122 friend std::unique_ptr<RNTupleWriter>
123 Experimental::RNTupleWriter_Append(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view ntuplePath,
125
126private:
129 /// All the Attribute Sets created from this Writer.
130 std::vector<std::shared_ptr<Experimental::RNTupleAttrSetWriter>> fAttributeSets;
131
133
134 RNTupleWriter(std::unique_ptr<ROOT::RNTupleModel> model, std::unique_ptr<Internal::RPageSink> sink);
135
138
139 // Helper function that is called from CommitCluster() when necessary
140 void CommitClusterGroup();
141
143
144 /// Create a writer, potentially wrapping the sink in a RPageSinkBuf.
145 static std::unique_ptr<RNTupleWriter> Create(std::unique_ptr<ROOT::RNTupleModel> model,
146 std::unique_ptr<Internal::RPageSink> sink,
147 const ROOT::RNTupleWriteOptions &options);
148
149public:
150 /// Creates an RNTupleWriter backed by `storage`, overwriting it if one with the same URI exists.
151 /// The format of the backing storage is determined by `storage`: in the simplest case it will be a local file, but
152 /// a different backend may be selected via the URI prefix.
153 ///
154 /// The RNTupleWriter will create an RNTuple with the schema determined by `model` (which must not be null) and
155 /// with name `ntupleName`. This same name can later be used to read back the RNTuple via RNTupleReader.
156 ///
157 /// \param model The RNTupleModel describing the schema of the RNTuple written by this writer
158 /// \param ntupleName The name of the RNTuple to be written
159 /// \param storage The URI where the RNTuple will be stored (usually just a file name or path)
160 /// \param options May be passed to customize the behavior of the RNTupleWriter (see also RNTupleWriteOptions).
161 ///
162 /// Throws a ROOT::RException if the model is null.
163 static std::unique_ptr<RNTupleWriter>
164 Recreate(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view ntupleName, std::string_view storage,
166
167 /// Convenience function allowing to call Recreate() with an inline-defined model.
168 static std::unique_ptr<RNTupleWriter>
169 Recreate(std::initializer_list<std::pair<std::string_view, std::string_view>> fields, std::string_view ntupleName,
170 std::string_view storage, const ROOT::RNTupleWriteOptions &options = ROOT::RNTupleWriteOptions());
171
172 /// Creates an RNTupleWriter that writes into an existing TFile or TDirectory, without overwriting its content.
173 /// `fileOrDirectory` may be an empty TFile and its reference **must remain valid** for the lifetime of the
174 /// RNTupleWriter (which means the TDirectory object must not be moved, destroyed or replaced during that time).
175 /// \see Recreate()
176 static std::unique_ptr<RNTupleWriter> Append(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view ntupleName,
179
180 RNTupleWriter(const RNTupleWriter &) = delete;
185
186 /// The simplest user interface if the default entry that comes with the ntuple model is used.
187 /// \return The number of uncompressed bytes written.
188 std::size_t Fill() { return fFillContext.Fill(fFillContext.fModel->GetDefaultEntry()); }
189 /// Multiple entries can have been instantiated from the ntuple model. This method will check the entry's model ID
190 /// to ensure it comes from the writer's own model or throw an exception otherwise.
191 /// \return The number of uncompressed bytes written.
192 std::size_t Fill(ROOT::REntry &entry) { return fFillContext.Fill(entry); }
193 /// Fill an entry into this ntuple, but don't commit the cluster. The calling code must pass an RNTupleFillStatus
194 /// and check RNTupleFillStatus::ShouldFlushCluster.
196
197 /// Fill an RRawPtrWriteEntry into this ntuple. This method will check the entry's model ID to ensure it comes from
198 /// the writer's own model or throw an exception otherwise.
199 /// \return The number of uncompressed bytes written.
201 /// Fill an RRawPtrWriteEntry into this ntuple, but don't commit the cluster. The calling code must pass an
202 /// RNTupleFillStatus and check RNTupleFillStatus::ShouldFlushCluster.
207
208 /// Flush column data, preparing for CommitCluster or to reduce memory usage. This will trigger compression of pages,
209 /// but not actually write to storage (unless buffered writing is turned off).
211 /// Flush so far filled entries to storage
213 /// Ensure that the data from the so far seen Fill calls has been written to storage
220 /// Closes the underlying file (page sink) and expires the model. Automatically called on destruct.
221 /// Once the dataset is committed, calls to Fill(), [Commit|Flush]Cluster(), FlushColumns(), CreateEntry(),
222 /// and model updating fail.
223 void CommitDataset();
224
225 std::unique_ptr<ROOT::REntry> CreateEntry() const { return fFillContext.CreateEntry(); }
226 std::unique_ptr<ROOT::Detail::RRawPtrWriteEntry> CreateRawPtrWriteEntry() const
227 {
229 }
230
231 /// Return the entry number that was last flushed in a cluster.
233 /// Return the entry number that was last committed in a cluster.
235 /// Return the entry number that was last committed in a cluster group.
237 /// Return the number of entries filled so far.
239
242
243 const ROOT::RNTupleModel &GetModel() const { return *fFillContext.fModel; }
244
245 /// Get a RNTupleModel::RUpdater that provides limited support for incremental updates to the underlying
246 /// model, e.g. addition of new fields.
247 ///
248 /// Note that a Model may not be extended with Streamer fields.
249 ///
250 /// **Example: add a new field after the model has been used to construct a `RNTupleWriter` object**
251 /// ~~~ {.cpp}
252 /// #include <ROOT/RNTuple.hxx>
253 ///
254 /// auto model = ROOT::RNTupleModel::Create();
255 /// auto fldFloat = model->MakeField<float>("fldFloat");
256 /// auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), "myNTuple", "some/file.root");
257 /// auto updater = writer->CreateModelUpdater();
258 /// updater->BeginUpdate();
259 /// updater->AddField(std::make_unique<RField<float>>("pt"));
260 /// updater->CommitUpdate();
261 ///
262 /// // ...
263 /// ~~~
264 std::unique_ptr<ROOT::RNTupleModel::RUpdater> CreateModelUpdater()
265 {
266 return std::make_unique<ROOT::RNTupleModel::RUpdater>(*this);
267 }
268
269 /// Creates a new Attribute Set called `name` associated to this Writer and returns a non-owning pointer to it.
270 /// The lifetime of the Attribute Set ends at the same time as the Writer's.
271 /// If `options` are passed, they will be used for the attribute set writer; otherwise, they will be derived from
272 /// the main writer.
274 CreateAttributeSet(std::unique_ptr<RNTupleModel> model, std::string_view name,
275 const ROOT::RNTupleWriteOptions *options = nullptr);
276
277 /// Writes the given AttributeSet to the underlying storage and closes it. This method is only useful if you
278 /// want to close the AttributeSet early: otherwise it will automatically closed when the RNTupleWriter gets
279 /// destroyed.
281}; // class RNTupleWriter
282
283} // namespace ROOT
284
285#endif // ROOT_RNTupleWriter
char name[80]
Definition TGX11.cxx:145
A container of const raw pointers, corresponding to a row in the data set.
A collection of Counter objects with a name, a unit, and a description.
An interface to read from, or write to, a ROOT file, as well as performing other common operations.
Definition RFile.hxx:253
Non-owning handle to an RNTupleAttrSetWriter.
Class used to write an RNTupleAttrSet in the context of an RNTupleWriter.
Abstract interface to write data into an ntuple.
The REntry is a collection of values in an RNTuple corresponding to a complete row in the data set.
Definition REntry.hxx:55
A context for filling entries (data) into clusters of an RNTuple.
void FlushCluster()
Flush so far filled entries to storage.
ROOT::NTupleSize_t GetLastFlushed() const
Return the entry number that was last flushed in a cluster.
ROOT::NTupleSize_t GetNEntries() const
Return the number of entries filled so far.
void FlushColumns()
Flush column data, preparing for CommitCluster or to reduce memory usage.
std::unique_ptr< ROOT::REntry > CreateEntry() const
std::unique_ptr< ROOT::Detail::RRawPtrWriteEntry > CreateRawPtrWriteEntry() const
std::unique_ptr< ROOT::RNTupleModel > fModel
Needs to be destructed before fSink.
std::unique_ptr< ROOT::Internal::RPageSink > fSink
void FillNoFlush(ROOT::REntry &entry, ROOT::RNTupleFillStatus &status)
Fill an entry into this context, but don't commit the cluster.
std::size_t Fill(ROOT::REntry &entry)
Fill an entry into this context.
A status object after filling an entry.
A model is usually immutable after passing it to an RNTupleWriter.
The RNTupleModel encapulates the schema of an RNTuple.
Common user-tunable settings for storing RNTuples.
An RNTuple that gets filled with entries (data) and writes them to storage.
std::unique_ptr< ROOT::REntry > CreateEntry() const
static std::unique_ptr< RNTupleWriter > Create(std::unique_ptr< ROOT::RNTupleModel > model, std::unique_ptr< Internal::RPageSink > sink, const ROOT::RNTupleWriteOptions &options)
Create a writer, potentially wrapping the sink in a RPageSinkBuf.
RNTupleWriter(RNTupleWriter &&)=delete
static std::unique_ptr< RNTupleWriter > Recreate(std::unique_ptr< ROOT::RNTupleModel > model, std::string_view ntupleName, std::string_view storage, const ROOT::RNTupleWriteOptions &options=ROOT::RNTupleWriteOptions())
Creates an RNTupleWriter backed by storage, overwriting it if one with the same URI exists.
std::vector< std::shared_ptr< Experimental::RNTupleAttrSetWriter > > fAttributeSets
All the Attribute Sets created from this Writer.
RNTupleWriter(const RNTupleWriter &)=delete
std::unique_ptr< ROOT::Detail::RRawPtrWriteEntry > CreateRawPtrWriteEntry() const
Experimental::Detail::RNTupleMetrics fMetrics
RNTupleWriter & operator=(RNTupleWriter &&)=delete
const Experimental::Detail::RNTupleMetrics & GetMetrics() const
RNTupleFillContext fFillContext
void CloseAttributeSet(ROOT::Experimental::RNTupleAttrSetWriterHandle handle)
Writes the given AttributeSet to the underlying storage and closes it.
ROOT::RNTupleModel & GetUpdatableModel()
Internal::RPageSink & GetSink()
ROOT::Experimental::RNTupleAttrSetWriterHandle CreateAttributeSet(std::unique_ptr< RNTupleModel > model, std::string_view name, const ROOT::RNTupleWriteOptions *options=nullptr)
Creates a new Attribute Set called name associated to this Writer and returns a non-owning pointer to...
static std::unique_ptr< RNTupleWriter > Append(std::unique_ptr< ROOT::RNTupleModel > model, std::string_view ntupleName, TDirectory &fileOrDirectory, const ROOT::RNTupleWriteOptions &options=ROOT::RNTupleWriteOptions())
Creates an RNTupleWriter that writes into an existing TFile or TDirectory, without overwriting its co...
std::size_t Fill(ROOT::REntry &entry)
Multiple entries can have been instantiated from the ntuple model.
std::size_t Fill(ROOT::Detail::RRawPtrWriteEntry &entry)
Fill an RRawPtrWriteEntry into this ntuple.
void FillNoFlush(ROOT::REntry &entry, RNTupleFillStatus &status)
Fill an entry into this ntuple, but don't commit the cluster.
RNTupleWriter(std::unique_ptr< ROOT::RNTupleModel > model, std::unique_ptr< Internal::RPageSink > sink)
ROOT::NTupleSize_t GetLastCommitted() const
Return the entry number that was last committed in a cluster.
ROOT::NTupleSize_t GetLastCommittedClusterGroup() const
Return the entry number that was last committed in a cluster group.
void CommitDataset()
Closes the underlying file (page sink) and expires the model.
const ROOT::RNTupleModel & GetModel() const
ROOT::NTupleSize_t fLastCommittedClusterGroup
void FlushColumns()
Flush column data, preparing for CommitCluster or to reduce memory usage.
RNTupleWriter & operator=(const RNTupleWriter &)=delete
ROOT::NTupleSize_t GetLastFlushed() const
Return the entry number that was last flushed in a cluster.
void FillNoFlush(ROOT::Detail::RRawPtrWriteEntry &entry, RNTupleFillStatus &status)
Fill an RRawPtrWriteEntry into this ntuple, but don't commit the cluster.
std::unique_ptr< ROOT::RNTupleModel::RUpdater > CreateModelUpdater()
Get a RNTupleModel::RUpdater that provides limited support for incremental updates to the underlying ...
void FlushCluster()
Flush so far filled entries to storage.
ROOT::NTupleSize_t GetNEntries() const
Return the number of entries filled so far.
void CommitCluster(bool commitClusterGroup=false)
Ensure that the data from the so far seen Fill calls has been written to storage.
void CloseAttributeSetImpl(ROOT::Experimental::RNTupleAttrSetWriter &attrSet)
std::size_t Fill()
The simplest user interface if the default entry that comes with the ntuple model is used.
Describe directory structure in memory.
Definition TDirectory.h:45
std::unique_ptr< RNTupleWriter > RNTupleWriter_Append(std::unique_ptr< ROOT::RNTupleModel > model, std::string_view ntuplePath, ROOT::Experimental::RFile &file, const ROOT::RNTupleWriteOptions &options=ROOT::RNTupleWriteOptions())
Creates an RNTupleWriter that writes into the given file, appending to it.
std::unique_ptr< RNTupleWriter > CreateRNTupleWriter(std::unique_ptr< ROOT::RNTupleModel > model, std::unique_ptr< Internal::RPageSink > sink)
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.