Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
RNTupleWriter.hxx
Go to the documentation of this file.
1/// \file ROOT/RNTupleWriter.hxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2024-02-20
5/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6/// is welcome!
7
8/*************************************************************************
9 * Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#ifndef ROOT7_RNTupleWriter
17#define ROOT7_RNTupleWriter
18
19#include <ROOT/RConfig.hxx> // for R__unlikely
20#include <ROOT/REntry.hxx>
21#include <ROOT/RError.hxx>
25#include <ROOT/RNTupleModel.hxx>
26#include <ROOT/RNTupleUtil.hxx>
27#include <ROOT/RPageStorage.hxx>
29
30#include <cstddef>
31#include <cstdint>
32#include <memory>
33#include <string_view>
34#include <utility>
35
36class TDirectory;
37
38namespace ROOT {
39
40class RNTupleWriteOptions;
41
42namespace Internal {
43// Non-public factory method for an RNTuple writer that uses an already constructed page sink
44std::unique_ptr<RNTupleWriter>
45CreateRNTupleWriter(std::unique_ptr<ROOT::RNTupleModel> model, std::unique_ptr<Experimental::Internal::RPageSink> sink);
46} // namespace Internal
47
48// clang-format off
49/**
50\class ROOT::RNTupleWriter
51\ingroup NTuple
52\brief An RNTuple that gets filled with entries (data) and writes them to storage
53
54An output ntuple can be filled with entries. The caller has to make sure that the data that gets filled into an ntuple
55is not modified for the time of the Fill() call. The fill call serializes the C++ object into the column format and
56writes data into the corresponding column page buffers. Writing of the buffers to storage is deferred and can be
57triggered by FlushCluster() or by destructing the writer. On I/O errors, an exception is thrown.
58*/
59// clang-format on
62 friend std::unique_ptr<RNTupleWriter>
63 Internal::CreateRNTupleWriter(std::unique_ptr<ROOT::RNTupleModel>,
64 std::unique_ptr<Experimental::Internal::RPageSink>);
65
66private:
67 /// The page sink's parallel page compression scheduler if IMT is on.
68 /// Needs to be destructed after the page sink (in the fill context) is destructed and so declared before.
69 std::unique_ptr<Experimental::Internal::RPageStorage::RTaskScheduler> fZipTasks;
72
74
75 RNTupleWriter(std::unique_ptr<ROOT::RNTupleModel> model, std::unique_ptr<Experimental::Internal::RPageSink> sink);
76
79
80 // Helper function that is called from CommitCluster() when necessary
81 void CommitClusterGroup();
82
83 /// Create a writer, potentially wrapping the sink in a RPageSinkBuf.
84 static std::unique_ptr<RNTupleWriter> Create(std::unique_ptr<ROOT::RNTupleModel> model,
85 std::unique_ptr<Experimental::Internal::RPageSink> sink,
86 const ROOT::RNTupleWriteOptions &options);
87
88public:
89 /// Throws an exception if the model is null.
90 static std::unique_ptr<RNTupleWriter>
91 Recreate(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view ntupleName, std::string_view storage,
93 static std::unique_ptr<RNTupleWriter>
94 Recreate(std::initializer_list<std::pair<std::string_view, std::string_view>> fields, std::string_view ntupleName,
95 std::string_view storage, const ROOT::RNTupleWriteOptions &options = ROOT::RNTupleWriteOptions());
96 /// Throws an exception if the model is null.
97 static std::unique_ptr<RNTupleWriter> Append(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view ntupleName,
100 RNTupleWriter(const RNTupleWriter &) = delete;
103
104 /// The simplest user interface if the default entry that comes with the ntuple model is used.
105 /// \return The number of uncompressed bytes written.
106 std::size_t Fill() { return fFillContext.Fill(fFillContext.fModel->GetDefaultEntry()); }
107 /// Multiple entries can have been instantiated from the ntuple model. This method will check the entry's model ID
108 /// to ensure it comes from the writer's own model or throw an exception otherwise.
109 /// \return The number of uncompressed bytes written.
110 std::size_t Fill(ROOT::REntry &entry) { return fFillContext.Fill(entry); }
111 /// Fill an entry into this ntuple, but don't commit the cluster. The calling code must pass an RNTupleFillStatus
112 /// and check RNTupleFillStatus::ShouldFlushCluster.
114
115 /// Fill an RRawPtrWriteEntry into this ntuple. This method will check the entry's model ID to ensure it comes from
116 /// the writer's own model or throw an exception otherwise.
117 /// \return The number of uncompressed bytes written.
119 /// Fill an RRawPtrWriteEntry into this ntuple, but don't commit the cluster. The calling code must pass an
120 /// RNTupleFillStatus and check RNTupleFillStatus::ShouldFlushCluster.
125
126 /// Flush column data, preparing for CommitCluster or to reduce memory usage. This will trigger compression of pages,
127 /// but not actually write to storage (unless buffered writing is turned off).
129 /// Flush so far filled entries to storage
131 /// Ensure that the data from the so far seen Fill calls has been written to storage
138 /// Closes the underlying file (page sink) and expires the model. Automatically called on destruct.
139 /// Once the dataset is committed, calls to Fill(), [Commit|Flush]Cluster(), FlushColumns(), CreateEntry(),
140 /// and model updating fail.
141 void CommitDataset();
142
143 std::unique_ptr<ROOT::REntry> CreateEntry() const { return fFillContext.CreateEntry(); }
144 std::unique_ptr<Experimental::Detail::RRawPtrWriteEntry> CreateRawPtrWriteEntry() const
145 {
147 }
148
149 /// Return the entry number that was last flushed in a cluster.
151 /// Return the entry number that was last committed in a cluster.
153 /// Return the entry number that was last committed in a cluster group.
155 /// Return the number of entries filled so far.
157
160
161 const ROOT::RNTupleModel &GetModel() const { return *fFillContext.fModel; }
162
163 /// Get a RNTupleModel::RUpdater that provides limited support for incremental updates to the underlying
164 /// model, e.g. addition of new fields.
165 ///
166 /// **Example: add a new field after the model has been used to construct a `RNTupleWriter` object**
167 /// ~~~ {.cpp}
168 /// #include <ROOT/RNTuple.hxx>
169 ///
170 /// auto model = ROOT::RNTupleModel::Create();
171 /// auto fldFloat = model->MakeField<float>("fldFloat");
172 /// auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), "myNTuple", "some/file.root");
173 /// auto updater = writer->CreateModelUpdater();
174 /// updater->BeginUpdate();
175 /// updater->AddField(std::make_unique<RField<float>>("pt"));
176 /// updater->CommitUpdate();
177 ///
178 /// // ...
179 /// ~~~
180 std::unique_ptr<ROOT::RNTupleModel::RUpdater> CreateModelUpdater()
181 {
182 return std::make_unique<ROOT::RNTupleModel::RUpdater>(*this);
183 }
184}; // class RNTupleWriter
185
186namespace Experimental {
187// TODO(gparolini): remove before branching ROOT v6.36
188using RNTupleWriter [[deprecated("ROOT::Experimental::RNTupleWriter moved to ROOT::RNTupleWriter")]] =
190} // namespace Experimental
191} // namespace ROOT
192
193#endif // ROOT7_RNTupleWriter
A collection of Counter objects with a name, a unit, and a description.
A container of const raw pointers, corresponding to a row in the data set.
Abstract interface to write data into an ntuple.
A context for filling entries (data) into clusters of an RNTuple.
ROOT::NTupleSize_t GetNEntries() const
Return the number of entries filled so far.
void FillNoFlush(ROOT::REntry &entry, ROOT::RNTupleFillStatus &status)
Fill an entry into this context, but don't commit the cluster.
std::unique_ptr< ROOT::RNTupleModel > fModel
Needs to be destructed before fSink.
void FlushCluster()
Flush so far filled entries to storage.
std::size_t Fill(ROOT::REntry &entry)
Fill an entry into this context.
ROOT::NTupleSize_t GetLastFlushed() const
Return the entry number that was last flushed in a cluster.
void FlushColumns()
Flush column data, preparing for CommitCluster or to reduce memory usage.
std::unique_ptr< ROOT::REntry > CreateEntry() const
std::unique_ptr< Internal::RPageSink > fSink
std::unique_ptr< Detail::RRawPtrWriteEntry > CreateRawPtrWriteEntry() const
The REntry is a collection of values in an ntuple corresponding to a complete row in the data set.
Definition REntry.hxx:56
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
std::size_t Fill(Experimental::Detail::RRawPtrWriteEntry &entry)
Fill an RRawPtrWriteEntry into this ntuple.
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())
Throws an exception if the model is null.
RNTupleWriter(const RNTupleWriter &)=delete
Experimental::Detail::RNTupleMetrics fMetrics
const Experimental::Detail::RNTupleMetrics & GetMetrics() const
ROOT::RNTupleModel & GetUpdatableModel()
void FillNoFlush(Experimental::Detail::RRawPtrWriteEntry &entry, RNTupleFillStatus &status)
Fill an RRawPtrWriteEntry into this ntuple, but don't commit the cluster.
static std::unique_ptr< RNTupleWriter > Create(std::unique_ptr< ROOT::RNTupleModel > model, std::unique_ptr< Experimental::Internal::RPageSink > sink, const ROOT::RNTupleWriteOptions &options)
Create a writer, potentially wrapping the sink in a RPageSinkBuf.
static std::unique_ptr< RNTupleWriter > Append(std::unique_ptr< ROOT::RNTupleModel > model, std::string_view ntupleName, TDirectory &fileOrDirectory, const ROOT::RNTupleWriteOptions &options=ROOT::RNTupleWriteOptions())
Throws an exception if the model is null.
std::size_t Fill(ROOT::REntry &entry)
Multiple entries can have been instantiated from the ntuple model.
void FillNoFlush(ROOT::REntry &entry, RNTupleFillStatus &status)
Fill an entry into this ntuple, but don't commit the cluster.
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(std::unique_ptr< ROOT::RNTupleModel > model, std::unique_ptr< Experimental::Internal::RPageSink > sink)
RNTupleWriter & operator=(const RNTupleWriter &)=delete
ROOT::NTupleSize_t GetLastFlushed() const
Return the entry number that was last flushed in a cluster.
std::unique_ptr< Experimental::Detail::RRawPtrWriteEntry > CreateRawPtrWriteEntry() const
std::unique_ptr< ROOT::RNTupleModel::RUpdater > CreateModelUpdater()
Get a RNTupleModel::RUpdater that provides limited support for incremental updates to the underlying ...
std::unique_ptr< Experimental::Internal::RPageStorage::RTaskScheduler > fZipTasks
The page sink's parallel page compression scheduler if IMT is on.
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.
Experimental::Internal::RPageSink & GetSink()
Experimental::RNTupleFillContext fFillContext
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 > CreateRNTupleWriter(std::unique_ptr< ROOT::RNTupleModel > model, std::unique_ptr< Experimental::Internal::RPageSink > sink)
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.