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 <TROOT.h>
29
30#include <utility>
31
32ROOT::Experimental::RNTupleWriter::RNTupleWriter(std::unique_ptr<ROOT::Experimental::RNTupleModel> model,
33 std::unique_ptr<ROOT::Experimental::Internal::RPageSink> sink)
34 : fFillContext(std::move(model), std::move(sink)), fMetrics("RNTupleWriter")
35{
36#ifdef R__USE_IMT
37 if (IsImplicitMTEnabled() &&
38 fFillContext.fSink->GetWriteOptions().GetUseImplicitMT() == RNTupleWriteOptions::EImplicitMT::kDefault) {
39 fZipTasks = std::make_unique<Internal::RNTupleImtTaskScheduler>();
40 fFillContext.fSink->SetTaskScheduler(fZipTasks.get());
41 }
42#endif
43 // Observe directly the sink's metrics to avoid an additional prefix from the fill context.
45}
46
48{
49 try {
50 CommitCluster(true /* commitClusterGroup */);
51 fFillContext.fSink->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 (options.GetUseBufferedWrite()) {
62 sink = std::make_unique<Internal::RPageSinkBuf>(std::move(sink));
63 }
64 return std::unique_ptr<RNTupleWriter>(new RNTupleWriter(std::move(model), std::move(sink)));
65}
66
67std::unique_ptr<ROOT::Experimental::RNTupleWriter>
68ROOT::Experimental::RNTupleWriter::Recreate(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName,
69 std::string_view storage, const RNTupleWriteOptions &options)
70{
71 auto sink = Internal::RPagePersistentSink::Create(ntupleName, storage, options);
72 return Create(std::move(model), std::move(sink), options);
73}
74
75std::unique_ptr<ROOT::Experimental::RNTupleWriter>
76ROOT::Experimental::RNTupleWriter::Recreate(std::initializer_list<std::pair<std::string_view, std::string_view>> fields,
77 std::string_view ntupleName, std::string_view storage,
78 const RNTupleWriteOptions &options)
79{
80 auto sink = Internal::RPagePersistentSink::Create(ntupleName, storage, options);
81 auto model = RNTupleModel::Create();
82 for (const auto &fieldDesc : fields) {
83 std::string typeName(fieldDesc.first);
84 std::string fieldName(fieldDesc.second);
85 auto field = RFieldBase::Create(fieldName, typeName);
86 model->AddField(field.Unwrap());
87 }
88 return Create(std::move(model), std::move(sink), options);
89}
90
91std::unique_ptr<ROOT::Experimental::RNTupleWriter>
92ROOT::Experimental::RNTupleWriter::Append(std::unique_ptr<RNTupleModel> model, std::string_view ntupleName, TFile &file,
93 const RNTupleWriteOptions &options)
94{
95 auto sink = std::make_unique<Internal::RPageSinkFile>(ntupleName, file, options);
96 return Create(std::move(model), std::move(sink), options);
97}
98
100{
101 if (GetNEntries() == fLastCommittedClusterGroup)
102 return;
103 fFillContext.fSink->CommitClusterGroup();
104 fLastCommittedClusterGroup = GetNEntries();
105}
106
107std::unique_ptr<ROOT::Experimental::RNTupleWriter>
108ROOT::Experimental::Internal::CreateRNTupleWriter(std::unique_ptr<ROOT::Experimental::RNTupleModel> model,
109 std::unique_ptr<ROOT::Experimental::Internal::RPageSink> sink)
110{
111 return std::unique_ptr<ROOT::Experimental::RNTupleWriter>(
112 new ROOT::Experimental::RNTupleWriter(std::move(model), std::move(sink)));
113}
#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.
std::string GetReport() const
Format a dignostics report, e.g. for an exception message.
Definition RError.cxx:25
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
const RError & GetError() const
Definition RError.hxx:82
static RResult< std::unique_ptr< RFieldBase > > Create(const std::string &fieldName, const std::string &canonicalType, const std::string &typeAlias, bool fContinueOnError=false)
Factory method to resurrect a field from the stored on-disk type information.
Definition RField.cxx:566
std::unique_ptr< Internal::RPageSink > fSink
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)
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.
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.
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)
RLogChannel & NTupleLog()
Log channel for RNTuple diagnostics.
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:568