Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleFillContext.cxx
Go to the documentation of this file.
1/// \file RNTupleFillContext.cxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2024-02-22
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/RError.hxx>
19#include <ROOT/RField.hxx>
20#include <ROOT/RLogger.hxx>
22#include <ROOT/RNTupleModel.hxx>
24#include <ROOT/RPageStorage.hxx>
25
26#include <algorithm>
27#include <utility>
28
30 std::unique_ptr<Internal::RPageSink> sink)
31 : fSink(std::move(sink)), fModel(std::move(model)), fMetrics("RNTupleFillContext")
32{
33 fModel->Freeze();
34 fSink->Init(*fModel.get());
35 fMetrics.ObserveMetrics(fSink->GetMetrics());
36
37 const auto &writeOpts = fSink->GetWriteOptions();
38 fMaxUnzippedClusterSize = writeOpts.GetMaxUnzippedClusterSize();
39 // First estimate is a factor 2 compression if compression is used at all
40 const int scale = writeOpts.GetCompression() ? 2 : 1;
41 fUnzippedClusterSizeEst = scale * writeOpts.GetApproxZippedClusterSize();
42}
43
45{
46 try {
47 CommitCluster();
48 } catch (const RException &err) {
49 R__LOG_ERROR(NTupleLog()) << "failure committing ntuple: " << err.GetError().GetReport();
50 }
51}
52
54{
55 if (fNEntries == fLastCommitted) {
56 return;
57 }
58 if (fSink->GetWriteOptions().GetHasSmallClusters() &&
59 (fUnzippedClusterSize > RNTupleWriteOptions::kMaxSmallClusterSize)) {
60 throw RException(R__FAIL("invalid attempt to write a cluster > 512MiB with 'small clusters' option enabled"));
61 }
62 for (auto &field : fModel->GetFieldZero()) {
64 }
65 auto nEntriesInCluster = fNEntries - fLastCommitted;
66 fNBytesCommitted += fSink->CommitCluster(nEntriesInCluster);
67 fNBytesFilled += fUnzippedClusterSize;
68
69 // Cap the compression factor at 1000 to prevent overflow of fUnzippedClusterSizeEst
70 const float compressionFactor =
71 std::min(1000.f, static_cast<float>(fNBytesFilled) / static_cast<float>(fNBytesCommitted));
72 fUnzippedClusterSizeEst =
73 compressionFactor * static_cast<float>(fSink->GetWriteOptions().GetApproxZippedClusterSize());
74
75 fLastCommitted = fNEntries;
76 fUnzippedClusterSize = 0;
77}
#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:290
#define R__LOG_ERROR(...)
Definition RLogger.hxx:362
void ObserveMetrics(RNTupleMetrics &observee)
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
std::size_t fUnzippedClusterSizeEst
Estimator of uncompressed cluster size, taking into account the estimated compression ratio.
void CommitCluster()
Ensure that the data from the so far seen Fill calls has been written to storage.
std::unique_ptr< RNTupleModel > fModel
Needs to be destructed before fSink.
std::size_t fMaxUnzippedClusterSize
Limit for committing cluster no matter the other tunables.
RNTupleFillContext(std::unique_ptr< RNTupleModel > model, std::unique_ptr< Internal::RPageSink > sink)
std::unique_ptr< Internal::RPageSink > fSink
static constexpr std::uint64_t kMaxSmallClusterSize
A maximum size of 512MB still allows for a vector of bool to be stored in a small cluster.
void CallCommitClusterOnField(RFieldBase &)
Definition RField.cxx:354
RLogChannel & NTupleLog()
Log channel for RNTuple diagnostics.