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 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>
28
29#include <cstddef>
30#include <cstdint>
31#include <memory>
32#include <string_view>
33#include <utility>
34
35class TFile;
36
37namespace ROOT {
38namespace Experimental {
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<RNTupleModel> model, std::unique_ptr<Internal::RPageSink> sink);
46} // namespace Internal
47
48// clang-format off
49/**
50\class ROOT::Experimental::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 CommitCluster() 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<RNTupleModel>, std::unique_ptr<Internal::RPageSink>);
64
65private:
66 /// The page sink's parallel page compression scheduler if IMT is on.
67 /// Needs to be destructed after the page sink (in the fill context) is destructed and so declared before.
68 std::unique_ptr<Internal::RPageStorage::RTaskScheduler> fZipTasks;
71
73
74 RNTupleWriter(std::unique_ptr<RNTupleModel> model, std::unique_ptr<Internal::RPageSink> sink);
75
78
79 // Helper function that is called from CommitCluster() when necessary
80 void CommitClusterGroup();
81
82 /// Create a writer, potentially wrapping the sink in a RPageSinkBuf.
83 static std::unique_ptr<RNTupleWriter> Create(std::unique_ptr<RNTupleModel> model,
84 std::unique_ptr<Internal::RPageSink> sink,
85 const RNTupleWriteOptions &options);
86
87public:
88 /// Throws an exception if the model is null.
89 static std::unique_ptr<RNTupleWriter> Recreate(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName,
90 std::string_view storage,
91 const RNTupleWriteOptions &options = RNTupleWriteOptions());
92 static std::unique_ptr<RNTupleWriter>
93 Recreate(std::initializer_list<std::pair<std::string_view, std::string_view>> fields, std::string_view ntupleName,
94 std::string_view storage, const RNTupleWriteOptions &options = RNTupleWriteOptions());
95 /// Throws an exception if the model is null.
96 static std::unique_ptr<RNTupleWriter> Append(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName,
97 TFile &file,
98 const RNTupleWriteOptions &options = RNTupleWriteOptions());
99 RNTupleWriter(const RNTupleWriter &) = delete;
102
103 /// The simplest user interface if the default entry that comes with the ntuple model is used.
104 /// \return The number of uncompressed bytes written.
105 std::size_t Fill() { return fFillContext.Fill(fFillContext.fModel->GetDefaultEntry()); }
106 /// Multiple entries can have been instantiated from the ntuple model. This method will perform
107 /// a light check whether the entry comes from the ntuple's own model.
108 /// \return The number of uncompressed bytes written.
109 std::size_t Fill(REntry &entry) { return fFillContext.Fill(entry); }
110 /// Fill an entry into this ntuple, but don't commit the cluster. The calling code must pass an RNTupleFillStatus
111 /// and check RNTupleFillStatus::ShouldCommitCluster.
112 void FillNoCommit(REntry &entry, RNTupleFillStatus &status) { fFillContext.FillNoCommit(entry, status); }
113 /// Ensure that the data from the so far seen Fill calls has been written to storage
114 void CommitCluster(bool commitClusterGroup = false)
115 {
117 if (commitClusterGroup)
119 }
120
121 std::unique_ptr<REntry> CreateEntry() { return fFillContext.CreateEntry(); }
122
123 /// Return the entry number that was last committed in a cluster.
125 /// Return the entry number that was last committed in a cluster group.
127 /// Return the number of entries filled so far.
129
131 const Detail::RNTupleMetrics &GetMetrics() const { return fMetrics; }
132
133 const RNTupleModel &GetModel() const { return *fFillContext.fModel; }
134
135 /// Get a `RNTupleModel::RUpdater` that provides limited support for incremental updates to the underlying
136 /// model, e.g. addition of new fields.
137 ///
138 /// **Example: add a new field after the model has been used to construct a `RNTupleWriter` object**
139 /// ~~~ {.cpp}
140 /// #include <ROOT/RNTuple.hxx>
141 /// using ROOT::Experimental::RNTupleModel;
142 /// using ROOT::Experimental::RNTupleWriter;
143 ///
144 /// auto model = RNTupleModel::Create();
145 /// auto fldFloat = model->MakeField<float>("fldFloat");
146 /// auto writer = RNTupleWriter::Recreate(std::move(model), "myNTuple", "some/file.root");
147 /// auto updater = writer->CreateModelUpdater();
148 /// updater->BeginUpdate();
149 /// updater->AddField(std::make_unique<RField<float>>("pt"));
150 /// updater->CommitUpdate();
151 ///
152 /// // ...
153 /// ~~~
154 std::unique_ptr<RNTupleModel::RUpdater> CreateModelUpdater()
155 {
156 return std::make_unique<RNTupleModel::RUpdater>(*this);
157 }
158}; // class RNTupleWriter
159
160} // namespace Experimental
161} // namespace ROOT
162
163#endif // ROOT7_RNTupleWriter
A collection of Counter objects with a name, a unit, and a description.
Abstract interface to write data into an ntuple.
The REntry is a collection of values in an ntuple corresponding to a complete row in the data set.
Definition REntry.hxx:46
A context for filling entries (data) into clusters of an RNTuple.
void FillNoCommit(REntry &entry, RNTupleFillStatus &status)
Fill an entry into this context, but don't commit the cluster.
void CommitCluster()
Ensure that the data from the so far seen Fill calls has been written to storage.
std::size_t Fill(REntry &entry)
Fill an entry into this context.
std::unique_ptr< RNTupleModel > fModel
Needs to be destructed before fSink.
NTupleSize_t GetNEntries() const
Return the number of entries filled so far.
NTupleSize_t GetLastCommitted() const
Return the entry number that was last committed in a cluster.
std::unique_ptr< Internal::RPageSink > fSink
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 ntuple.
Common user-tunable settings for storing ntuples.
An RNTuple that gets filled with entries (data) and writes them to storage.
std::unique_ptr< RNTupleModel::RUpdater > CreateModelUpdater()
Get a RNTupleModel::RUpdater that provides limited support for incremental updates to the underlying ...
Internal::RPageSink & GetSink()
const Detail::RNTupleMetrics & GetMetrics() const
NTupleSize_t GetLastCommittedClusterGroup() const
Return the entry number that was last committed in a cluster group.
std::unique_ptr< Internal::RPageStorage::RTaskScheduler > fZipTasks
The page sink's parallel page compression scheduler if IMT is on.
void FillNoCommit(REntry &entry, RNTupleFillStatus &status)
Fill an entry into this ntuple, but don't commit the cluster.
const RNTupleModel & GetModel() const
std::size_t Fill()
The simplest user interface if the default entry that comes with the ntuple model is used.
std::unique_ptr< REntry > CreateEntry()
static std::unique_ptr< RNTupleWriter > Create(std::unique_ptr< RNTupleModel > model, std::unique_ptr< Internal::RPageSink > sink, const RNTupleWriteOptions &options)
Create a writer, potentially wrapping the sink in a RPageSinkBuf.
NTupleSize_t GetLastCommitted() const
Return the entry number that was last committed in a cluster.
void CommitCluster(bool commitClusterGroup=false)
Ensure that the data from the so far seen Fill calls has been written to storage.
RNTupleWriter(const RNTupleWriter &)=delete
Detail::RNTupleMetrics fMetrics
std::size_t Fill(REntry &entry)
Multiple entries can have been instantiated from the ntuple model.
RNTupleWriter & operator=(const RNTupleWriter &)=delete
static std::unique_ptr< RNTupleWriter > Recreate(std::unique_ptr< RNTupleModel > model, std::string_view ntupleName, std::string_view storage, const RNTupleWriteOptions &options=RNTupleWriteOptions())
Throws an exception if the model is null.
NTupleSize_t GetNEntries() const
Return the number of entries filled so far.
static std::unique_ptr< RNTupleWriter > Append(std::unique_ptr< RNTupleModel > model, std::string_view ntupleName, TFile &file, const RNTupleWriteOptions &options=RNTupleWriteOptions())
Throws an exception if the model is null.
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
std::unique_ptr< RNTupleWriter > CreateRNTupleWriter(std::unique_ptr< 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.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...