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>
24#include <ROOT/RNTupleModel.hxx>
25#include <ROOT/RNTupleUtil.hxx>
26#include <ROOT/RPageStorage.hxx>
27
28#include <cstddef>
29#include <cstdint>
30#include <memory>
31#include <string_view>
32#include <utility>
33
34class TFile;
35
36namespace ROOT {
37namespace Experimental {
38
39class RNTupleWriteOptions;
40
41namespace Internal {
42// Non-public factory method for an RNTuple writer that uses an already constructed page sink
43std::unique_ptr<RNTupleWriter>
44CreateRNTupleWriter(std::unique_ptr<RNTupleModel> model, std::unique_ptr<Internal::RPageSink> sink);
45} // namespace Internal
46
47// clang-format off
48/**
49\class ROOT::Experimental::RNTupleWriter
50\ingroup NTuple
51\brief An RNTuple that gets filled with entries (data) and writes them to storage
52
53An output ntuple can be filled with entries. The caller has to make sure that the data that gets filled into an ntuple
54is not modified for the time of the Fill() call. The fill call serializes the C++ object into the column format and
55writes data into the corresponding column page buffers. Writing of the buffers to storage is deferred and can be
56triggered by CommitCluster() or by destructing the writer. On I/O errors, an exception is thrown.
57*/
58// clang-format on
61 friend std::unique_ptr<RNTupleWriter>
62 Internal::CreateRNTupleWriter(std::unique_ptr<RNTupleModel>, std::unique_ptr<Internal::RPageSink>);
63
64private:
65 /// The page sink's parallel page compression scheduler if IMT is on.
66 /// Needs to be destructed after the page sink (in the fill context) is destructed and so declared before.
67 std::unique_ptr<Internal::RPageStorage::RTaskScheduler> fZipTasks;
70
72
73 RNTupleWriter(std::unique_ptr<RNTupleModel> model, std::unique_ptr<Internal::RPageSink> sink);
74
77
78 // Helper function that is called from CommitCluster() when necessary
79 void CommitClusterGroup();
80
81 /// Create a writer, potentially wrapping the sink in a RPageSinkBuf.
82 static std::unique_ptr<RNTupleWriter> Create(std::unique_ptr<RNTupleModel> model,
83 std::unique_ptr<Internal::RPageSink> sink,
84 const RNTupleWriteOptions &options);
85
86public:
87 /// Throws an exception if the model is null.
88 static std::unique_ptr<RNTupleWriter> Recreate(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName,
89 std::string_view storage,
90 const RNTupleWriteOptions &options = RNTupleWriteOptions());
91 static std::unique_ptr<RNTupleWriter>
92 Recreate(std::initializer_list<std::pair<std::string_view, std::string_view>> fields, std::string_view ntupleName,
93 std::string_view storage, const RNTupleWriteOptions &options = RNTupleWriteOptions());
94 /// Throws an exception if the model is null.
95 static std::unique_ptr<RNTupleWriter> Append(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName,
96 TFile &file,
97 const RNTupleWriteOptions &options = RNTupleWriteOptions());
98 RNTupleWriter(const RNTupleWriter &) = delete;
101
102 /// The simplest user interface if the default entry that comes with the ntuple model is used.
103 /// \return The number of uncompressed bytes written.
104 std::size_t Fill() { return fFillContext.Fill(fFillContext.fModel->GetDefaultEntry()); }
105 /// Multiple entries can have been instantiated from the ntuple model. This method will perform
106 /// a light check whether the entry comes from the ntuple's own model.
107 /// \return The number of uncompressed bytes written.
108 std::size_t Fill(REntry &entry) { return fFillContext.Fill(entry); }
109 /// Ensure that the data from the so far seen Fill calls has been written to storage
110 void CommitCluster(bool commitClusterGroup = false)
111 {
113 if (commitClusterGroup)
115 }
116
117 std::unique_ptr<REntry> CreateEntry() { return fFillContext.CreateEntry(); }
118
119 /// Return the entry number that was last committed in a cluster.
121 /// Return the entry number that was last committed in a cluster group.
123 /// Return the number of entries filled so far.
125
127 const Detail::RNTupleMetrics &GetMetrics() const { return fMetrics; }
128
129 const RNTupleModel &GetModel() const { return *fFillContext.fModel; }
130
131 /// Get a `RNTupleModel::RUpdater` that provides limited support for incremental updates to the underlying
132 /// model, e.g. addition of new fields.
133 ///
134 /// **Example: add a new field after the model has been used to construct a `RNTupleWriter` object**
135 /// ~~~ {.cpp}
136 /// #include <ROOT/RNTuple.hxx>
137 /// using ROOT::Experimental::RNTupleModel;
138 /// using ROOT::Experimental::RNTupleWriter;
139 ///
140 /// auto model = RNTupleModel::Create();
141 /// auto fldFloat = model->MakeField<float>("fldFloat");
142 /// auto writer = RNTupleWriter::Recreate(std::move(model), "myNTuple", "some/file.root");
143 /// auto updater = writer->CreateModelUpdater();
144 /// updater->BeginUpdate();
145 /// updater->AddField(std::make_unique<RField<float>>("pt"));
146 /// updater->CommitUpdate();
147 ///
148 /// // ...
149 /// ~~~
150 std::unique_ptr<RNTupleModel::RUpdater> CreateModelUpdater()
151 {
152 return std::make_unique<RNTupleModel::RUpdater>(*this);
153 }
154}; // class RNTupleWriter
155
156} // namespace Experimental
157} // namespace ROOT
158
159#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:45
A context for filling entries (data) into clusters of an RNTuple.
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 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.
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...