Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleWriter.cxx
Go to the documentation of this file.
1/// \file RNTupleReader.cxx
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
17
18#include <ROOT/RLogger.hxx>
22#include <ROOT/RNTupleModel.hxx>
24#include <ROOT/RPageSinkBuf.hxx>
25#include <ROOT/RPageStorage.hxx>
27
28#include <TFile.h>
29#include <TROOT.h>
30
31#include <utility>
32
33ROOT::Experimental::RNTupleWriter::RNTupleWriter(std::unique_ptr<ROOT::Experimental::RNTupleModel> model,
34 std::unique_ptr<ROOT::Experimental::Internal::RPageSink> sink)
35 : fFillContext(std::move(model), std::move(sink)), fMetrics("RNTupleWriter")
36{
37#ifdef R__USE_IMT
38 if (IsImplicitMTEnabled() &&
39 fFillContext.fSink->GetWriteOptions().GetUseImplicitMT() == RNTupleWriteOptions::EImplicitMT::kDefault) {
40 fZipTasks = std::make_unique<Internal::RNTupleImtTaskScheduler>();
41 fFillContext.fSink->SetTaskScheduler(fZipTasks.get());
42 }
43#endif
44 // Observe directly the sink's metrics to avoid an additional prefix from the fill context.
46}
47
49{
50 try {
51 CommitDataset();
52 } catch (const RException &err) {
53 R__LOG_ERROR(NTupleLog()) << "failure committing ntuple: " << err.GetError().GetReport();
54 }
55}
56
57std::unique_ptr<ROOT::Experimental::RNTupleWriter>
58ROOT::Experimental::RNTupleWriter::Create(std::unique_ptr<RNTupleModel> model,
59 std::unique_ptr<Internal::RPageSink> sink, const RNTupleWriteOptions &options)
60{
61 if (model->GetRegisteredSubfields().size() > 0) {
62 throw RException(R__FAIL("cannot create an RNTupleWriter from a model with registered subfields"));
63 }
64 if (options.GetUseBufferedWrite()) {
65 sink = std::make_unique<Internal::RPageSinkBuf>(std::move(sink));
66 }
67 return std::unique_ptr<RNTupleWriter>(new RNTupleWriter(std::move(model), std::move(sink)));
68}
69
70std::unique_ptr<ROOT::Experimental::RNTupleWriter>
71ROOT::Experimental::RNTupleWriter::Recreate(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName,
72 std::string_view storage, const RNTupleWriteOptions &options)
73{
74 auto sink = Internal::RPagePersistentSink::Create(ntupleName, storage, options);
75 return Create(std::move(model), std::move(sink), options);
76}
77
78std::unique_ptr<ROOT::Experimental::RNTupleWriter>
79ROOT::Experimental::RNTupleWriter::Recreate(std::initializer_list<std::pair<std::string_view, std::string_view>> fields,
80 std::string_view ntupleName, std::string_view storage,
81 const RNTupleWriteOptions &options)
82{
83 auto sink = Internal::RPagePersistentSink::Create(ntupleName, storage, options);
84 auto model = RNTupleModel::Create();
85 for (const auto &fieldDesc : fields) {
86 std::string typeName(fieldDesc.first);
87 std::string fieldName(fieldDesc.second);
88 auto field = RFieldBase::Create(fieldName, typeName);
89 model->AddField(field.Unwrap());
90 }
91 return Create(std::move(model), std::move(sink), options);
92}
93
94std::unique_ptr<ROOT::Experimental::RNTupleWriter>
95ROOT::Experimental::RNTupleWriter::Append(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName,
96 TDirectory &fileOrDirectory, const RNTupleWriteOptions &options)
97{
98 auto file = fileOrDirectory.GetFile();
99 if (!file) {
100 throw RException(R__FAIL("RNTupleWriter only supports writing to a ROOT file. Cannot write into a directory "
101 "that is not backed by a file"));
102 }
103 if (!file->IsBinary()) {
104 throw RException(R__FAIL("RNTupleWriter only supports writing to a ROOT file. Cannot write into " +
105 std::string(file->GetName())));
106 }
107
108 auto sink = std::make_unique<Internal::RPageSinkFile>(ntupleName, fileOrDirectory, options);
109 return Create(std::move(model), std::move(sink), options);
110}
111
113{
114 if (GetNEntries() == fLastCommittedClusterGroup)
115 return;
116 fFillContext.fSink->CommitClusterGroup();
117 fLastCommittedClusterGroup = GetNEntries();
118}
119
121{
122 if (fFillContext.fModel->IsExpired()) {
123 throw RException(R__FAIL("invalid attempt to update expired model"));
124 }
125 return *fFillContext.fModel;
126}
127
129{
130 if (fFillContext.GetModel().IsExpired())
131 return;
132
133 CommitCluster(true /* commitClusterGroup */);
134 fFillContext.fSink->CommitDataset();
135 fFillContext.fModel->Expire();
136}
137
138std::unique_ptr<ROOT::Experimental::RNTupleWriter>
139ROOT::Experimental::Internal::CreateRNTupleWriter(std::unique_ptr<ROOT::Experimental::RNTupleModel> model,
140 std::unique_ptr<ROOT::Experimental::Internal::RPageSink> sink)
141{
142 return std::unique_ptr<ROOT::Experimental::RNTupleWriter>(
143 new ROOT::Experimental::RNTupleWriter(std::move(model), std::move(sink)));
144}
#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:362
void ObserveMetrics(RNTupleMetrics &observee)
static std::unique_ptr< RPageSink > Create(std::string_view ntupleName, std::string_view location, const RNTupleWriteOptions &options=RNTupleWriteOptions())
Guess the concrete derived page source from the location.
static RResult< std::unique_ptr< RFieldBase > > Create(const std::string &fieldName, const std::string &canonicalType, const std::string &typeAlias, bool continueOnError=false)
Factory method to resurrect a field from the stored on-disk type information.
std::unique_ptr< Internal::RPageSink > fSink
The RNTupleModel encapulates the schema of an ntuple.
static std::unique_ptr< RNTupleModel > Create()
Common user-tunable settings for storing ntuples.
An RNTuple that gets filled with entries (data) and writes them to storage.
RNTupleWriter(std::unique_ptr< RNTupleModel > model, std::unique_ptr< Internal::RPageSink > sink)
static std::unique_ptr< RNTupleWriter > Append(std::unique_ptr< RNTupleModel > model, std::string_view ntupleName, TDirectory &fileOrDirectory, const RNTupleWriteOptions &options=RNTupleWriteOptions())
Throws an exception if the model is null.
std::unique_ptr< Internal::RPageStorage::RTaskScheduler > fZipTasks
The page sink's parallel page compression scheduler if IMT is on.
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.
void CommitDataset()
Closes the underlying file (page sink) and expires the model.
Detail::RNTupleMetrics fMetrics
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.
std::string GetReport() const
Format a dignostics report, e.g. for an exception message.
Definition RError.cxx:22
Base class for all ROOT issued exceptions.
Definition RError.hxx:79
const RError & GetError() const
Definition RError.hxx:84
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TFile * GetFile() const
Definition TDirectory.h:220
std::unique_ptr< RNTupleWriter > CreateRNTupleWriter(std::unique_ptr< RNTupleModel > model, std::unique_ptr< Internal::RPageSink > sink)
RLogChannel & NTupleLog()
Log channel for RNTuple diagnostics.
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:570