Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
RNTupleWriter.cxx
Go to the documentation of this file.
1/// \file RNTupleReader.cxx
2/// \author Jakob Blomer <jblomer@cern.ch>
3/// \date 2024-02-20
4
5/*************************************************************************
6 * Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
14
15#include <ROOT/RLogger.hxx>
20#include <ROOT/RNTupleModel.hxx>
21#include <ROOT/RNTupleUtils.hxx>
23#include <ROOT/RPageSinkBuf.hxx>
24#include <ROOT/RPageStorage.hxx>
26#include <ROOT/RFile.hxx>
27
28#include <TFile.h>
29#include <TROOT.h>
30
31#include <utility>
32
33static bool IsReservedRNTupleAttrSetName(std::string_view name)
34{
35 return ROOT::StartsWith(name, "__");
36}
37
38ROOT::RNTupleWriter::RNTupleWriter(std::unique_ptr<ROOT::RNTupleModel> model,
39 std::unique_ptr<ROOT::Internal::RPageSink> sink)
40 : fFillContext(std::move(model), std::move(sink)), fMetrics("RNTupleWriter")
41{
42#ifdef R__USE_IMT
43 if (IsImplicitMTEnabled() &&
44 fFillContext.fSink->GetWriteOptions().GetUseImplicitMT() != ROOT::RNTupleWriteOptions::EImplicitMT::kOff) {
45 fFillContext.fZipTasks = std::make_unique<ROOT::Experimental::Internal::RNTupleImtTaskScheduler>();
46 fFillContext.fSink->SetTaskScheduler(fFillContext.fZipTasks.get());
47 }
48#endif
49 // Observe directly the sink's metrics to avoid an additional prefix from the fill context.
50 fMetrics.ObserveMetrics(fFillContext.fSink->GetMetrics());
51}
52
54{
55 try {
57 } catch (const RException &err) {
58 R__LOG_ERROR(ROOT::Internal::NTupleLog()) << "failure committing ntuple: " << err.GetError().GetReport();
59 }
60}
61
62std::unique_ptr<ROOT::RNTupleWriter> ROOT::RNTupleWriter::Create(std::unique_ptr<ROOT::RNTupleModel> model,
63 std::unique_ptr<Internal::RPageSink> sink,
64 const ROOT::RNTupleWriteOptions &options)
65{
66 if (model->GetRegisteredSubfieldNames().size() > 0) {
67 throw RException(R__FAIL("cannot create an RNTupleWriter from a model with registered subfields"));
68 }
69 for (const auto &field : model->GetConstFieldZero()) {
70 if (field.GetTraits() & ROOT::RFieldBase::kTraitEmulatedField)
71 throw RException(
72 R__FAIL("creating a RNTupleWriter from a model containing emulated fields is currently unsupported."));
73 }
74 if (options.GetUseBufferedWrite()) {
75 sink = std::make_unique<Internal::RPageSinkBuf>(std::move(sink));
76 }
77 return std::unique_ptr<RNTupleWriter>(new RNTupleWriter(std::move(model), std::move(sink)));
78}
79
80std::unique_ptr<ROOT::RNTupleWriter>
81ROOT::RNTupleWriter::Recreate(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view ntupleName,
82 std::string_view storage, const ROOT::RNTupleWriteOptions &options)
83{
84 auto sink = Internal::RPagePersistentSink::Create(ntupleName, storage, options);
85 return Create(std::move(model), std::move(sink), options);
86}
87
88std::unique_ptr<ROOT::RNTupleWriter>
89ROOT::RNTupleWriter::Recreate(std::initializer_list<std::pair<std::string_view, std::string_view>> fields,
90 std::string_view ntupleName, std::string_view storage,
91 const ROOT::RNTupleWriteOptions &options)
92{
93 auto sink = Internal::RPagePersistentSink::Create(ntupleName, storage, options);
94 auto model = ROOT::RNTupleModel::Create();
95 for (const auto &fieldDesc : fields) {
96 std::string typeName(fieldDesc.first);
97 std::string fieldName(fieldDesc.second);
98 auto field = ROOT::RFieldBase::Create(fieldName, typeName);
99 model->AddField(field.Unwrap());
100 }
101 return Create(std::move(model), std::move(sink), options);
102}
103
104std::unique_ptr<ROOT::RNTupleWriter>
105ROOT::RNTupleWriter::Append(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view ntupleName,
106 TDirectory &fileOrDirectory, const ROOT::RNTupleWriteOptions &options)
107{
108 auto file = fileOrDirectory.GetFile();
109 if (!file) {
110 throw RException(R__FAIL("RNTupleWriter only supports writing to a ROOT file. Cannot write into a directory "
111 "that is not backed by a file"));
112 }
113 if (!file->IsBinary()) {
114 throw RException(R__FAIL("RNTupleWriter only supports writing to a ROOT file. Cannot write into " +
115 std::string(file->GetName())));
116 }
117 if (!file->IsWritable()) {
118 throw RException(R__FAIL("The file '" + std::string(file->GetName()) +
119 "' given to RNTupleWriter is not writable. Open it with 'UPDATE' or 'RECREATE' "
120 "if you want to write into it."));
121 }
122
123 auto sink = std::make_unique<Internal::RPageSinkFile>(ntupleName, fileOrDirectory, options);
124 return Create(std::move(model), std::move(sink), options);
125}
126
128{
130 return;
131 fFillContext.fSink->CommitClusterGroup();
133}
134
136{
137 if (fFillContext.fModel->IsExpired()) {
138 throw RException(R__FAIL("invalid attempt to update expired model"));
139 }
140 return *fFillContext.fModel;
141}
142
144{
145 if (fFillContext.GetModel().IsExpired())
146 return;
147
148 CommitCluster(true /* commitClusterGroup */);
149
150 // Commit attributes
151 for (auto &attrSet : fAttributeSets) {
152 CloseAttributeSetImpl(*attrSet);
153 }
154
155 fFillContext.fSink->CommitDataset();
156 fFillContext.fModel->Expire();
157}
158
159std::unique_ptr<ROOT::RNTupleWriter>
160ROOT::Internal::CreateRNTupleWriter(std::unique_ptr<ROOT::RNTupleModel> model,
161 std::unique_ptr<ROOT::Internal::RPageSink> sink)
162{
163 return std::unique_ptr<ROOT::RNTupleWriter>(new ROOT::RNTupleWriter(std::move(model), std::move(sink)));
164}
165
166std::unique_ptr<ROOT::RNTupleWriter>
167ROOT::Experimental::RNTupleWriter_Append(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view ntupleName,
169{
170 auto [ntupleDir, ntupleBasename] = ROOT::Experimental::Detail::DecomposePath(ntupleName);
171 auto sink = std::make_unique<ROOT::Internal::RPageSinkFile>(ntupleBasename, file, ntupleDir, options);
172 return ROOT::RNTupleWriter::Create(std::move(model), std::move(sink), options);
173}
174
176ROOT::RNTupleWriter::CreateAttributeSet(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view name,
177 const ROOT::RNTupleWriteOptions *optsPtr)
178{
180 throw ROOT::RException(R__FAIL("Cannot create Attribute Set named \"" + std::string(name) +
181 "\": names starting with '__' are reserved for internal use."));
182 }
183
184 if (name.empty())
185 throw ROOT::RException(R__FAIL("cannot create an Attribute Set with an empty name"));
186
187 const RNTupleWriteOptions &opts = optsPtr != nullptr ? *optsPtr : fFillContext.fSink->GetWriteOptions();
188 auto attrSink = fFillContext.fSink->CloneAsHidden(name, opts);
189
190 std::string nameStr{name};
191 auto attrSet = Experimental::RNTupleAttrSetWriter::Create(fFillContext, std::move(attrSink), std::move(model));
192
193 // check for duplicates
194 auto existingIt = std::find_if(fAttributeSets.begin(), fAttributeSets.end(),
195 [&nameStr](const auto &set) { return set->GetDescriptor().GetName() == nameStr; });
196 if (existingIt != fAttributeSets.end())
197 throw ROOT::RException(R__FAIL(std::string("attempted to create an Attribute Set named '") + nameStr +
198 "', but one already exists with that name"));
199
200 auto &addedSet = fAttributeSets.emplace_back(std::move(attrSet));
202}
203
205{
206 auto attrAnchorInfo = attrSet.Commit();
207 fFillContext.fSink->CommitAttributeSet(attrSet.GetDescriptor().GetName(), attrAnchorInfo);
208}
209
211{
212 auto writer = handle.fWriter.lock();
213 if (writer) {
214 throw ROOT::RException(R__FAIL("Tried to close an invalid AttributeSetWriter"));
215 }
216
218
219 auto &attrSets = fAttributeSets;
220 for (auto it = attrSets.begin(), end = attrSets.end(); it != end; ++it) {
221 if (it->get() == writer.get()) {
222 attrSets.erase(it);
223 return;
224 }
225 }
226 // We must have erased the attribute set.
227 R__ASSERT(false);
228}
#define R__FAIL(msg)
Short-hand to return an RResult<T> in an error state; the RError is implicitly converted into RResult...
Definition RError.hxx:299
#define R__LOG_ERROR(...)
Definition RLogger.hxx:356
static bool IsReservedRNTupleAttrSetName(std::string_view name)
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
char name[80]
Definition TGX11.cxx:148
Double_t err
An interface to read from, or write to, a ROOT file, as well as performing other common operations.
Definition RFile.hxx:252
Non-owning handle to an RNTupleAttrSetWriter.
std::weak_ptr< RNTupleAttrSetWriter > fWriter
Class used to write an RNTupleAttrSet in the context of an RNTupleWriter.
static std::unique_ptr< RNTupleAttrSetWriter > Create(const RNTupleFillContext &mainFillContext, std::unique_ptr< ROOT::Internal::RPageSink > sink, std::unique_ptr< RNTupleModel > userModel)
Creates an RNTupleAttrSetWriter associated to the RNTupleWriter owning mainFillContext and writing us...
ROOT::Internal::RNTupleLink Commit()
Commits the attributes written so far to disk and disables writing any new ones.
const ROOT::RNTupleDescriptor & GetDescriptor() const
Returns the descriptor of the underlying attribute RNTuple.
static std::unique_ptr< RPageSink > Create(std::string_view ntupleName, std::string_view location, const ROOT::RNTupleWriteOptions &options=ROOT::RNTupleWriteOptions())
Guess the concrete derived page source from the location.
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
@ kTraitEmulatedField
This field is a user defined type that was missing dictionaries and was reconstructed from the on-dis...
static RResult< std::unique_ptr< RFieldBase > > Create(const std::string &fieldName, const std::string &typeName, const ROOT::RCreateFieldOptions &options, const ROOT::RNTupleDescriptor *desc, ROOT::DescriptorId_t fieldId)
Factory method to resurrect a field from the stored on-disk type information.
const std::string & GetName() const
The RNTupleModel encapulates the schema of an RNTuple.
static std::unique_ptr< RNTupleModel > Create()
Common user-tunable settings for storing RNTuples.
An RNTuple that gets filled with entries (data) and writes them to storage.
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.
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.
Experimental::Detail::RNTupleMetrics fMetrics
RNTupleFillContext fFillContext
void CloseAttributeSet(ROOT::Experimental::RNTupleAttrSetWriterHandle handle)
Writes the given AttributeSet to the underlying storage and closes it.
ROOT::RNTupleModel & GetUpdatableModel()
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...
RNTupleWriter(std::unique_ptr< ROOT::RNTupleModel > model, std::unique_ptr< Internal::RPageSink > sink)
void CommitDataset()
Closes the underlying file (page sink) and expires the model.
ROOT::NTupleSize_t fLastCommittedClusterGroup
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)
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TFile * GetFile() const
Definition TDirectory.h:221
STL class.
std::pair< std::string_view, std::string_view > DecomposePath(std::string_view path)
Given a "path-like" string (like foo/bar/baz), returns a pair { dirName, baseName }...
Definition RFile.cxx:193
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.
ROOT::RLogChannel & NTupleLog()
Log channel for RNTuple diagnostics.
std::unique_ptr< RNTupleWriter > CreateRNTupleWriter(std::unique_ptr< ROOT::RNTupleModel > model, std::unique_ptr< Internal::RPageSink > sink)
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:669
bool StartsWith(std::string_view string, std::string_view prefix)