Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTuple.cxx
Go to the documentation of this file.
1/// \file RNTuple.cxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2023-09-19
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-2023, 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#include <ROOT/RError.hxx>
17#include <ROOT/RNTuple.hxx>
18#include <ROOT/RPageStorage.hxx>
20
21#include <TBuffer.h>
22#include <TError.h>
23#include <TFile.h>
24
25#include <xxhash.h>
26
28{
29 if (buf.IsReading()) {
30 UInt_t offClassBuf;
31 UInt_t bcnt;
32 auto classVersion = buf.ReadVersion(&offClassBuf, &bcnt);
33 if (classVersion < 4)
34 throw RException(R__FAIL("unsupported RNTuple pre-release"));
35
36 // Strip class version from checksum calculation
37 UInt_t lenStrip = sizeof(Version_t);
38 // TEMP(version4): In version 4 checksum is embedded in the on disk representation,
39 // so we need to strip that as well from the byte count.
40 // Support for version 4 will be dropped before the class moves out of experimental.
41 lenStrip += (classVersion == 4) * sizeof(std::uint64_t);
42
43 if (bcnt < lenStrip)
44 throw RException(R__FAIL("invalid anchor byte count: " + std::to_string(bcnt)));
45
46 auto lenCkData = bcnt - lenStrip;
47 // Skip byte count and class version
48 auto offCkData = offClassBuf + sizeof(UInt_t) + sizeof(Version_t);
49 auto expectedChecksum = XXH3_64bits(buf.Buffer() + offCkData, lenCkData);
50
51 std::uint64_t onDiskChecksum;
52 if (classVersion == 4) {
53 // TEMP(version4): Version 5 of the anchor breaks backward compat, but we still want to support version 4
54 // for a while. Support for version 4, as well as this code, will be removed before the RNTuple stabilization.
55 // For version 4 we need to manually read all the known members as we cannot rely on ReadClassBuffer.
56 constexpr std::size_t expectedBytes = 66;
57 if (bcnt != expectedBytes)
58 throw RException(R__FAIL("byte count mismatch in RNTuple anchor v4: expected=" +
59 std::to_string(expectedBytes) + ", got=" + std::to_string(bcnt)));
60 buf >> fVersionEpoch;
61 buf >> fVersionMajor;
62 buf >> fVersionMinor;
63 buf >> fVersionPatch;
64 buf >> fSeekHeader;
65 buf >> fNBytesHeader;
66 buf >> fLenHeader;
67 buf >> fSeekFooter;
68 buf >> fNBytesFooter;
69 buf >> fLenFooter;
70 buf >> onDiskChecksum;
71 } else {
72 // Rewind the version bytes, as ReadClassBuffer needs to read the version again.
73 buf.SetBufferOffset(offClassBuf);
75 if (static_cast<std::size_t>(buf.BufferSize()) < buf.Length() + sizeof(onDiskChecksum))
76 throw RException(R__FAIL("the buffer containing RNTuple is too small to contain the checksum!"));
77 buf >> onDiskChecksum;
78 }
79
80 if (expectedChecksum != onDiskChecksum)
81 throw RException(R__FAIL("checksum mismatch in RNTuple anchor"));
82
83 R__ASSERT(buf.GetParent() && buf.GetParent()->InheritsFrom("TFile"));
84 fFile = static_cast<TFile *>(buf.GetParent());
85 } else {
86 auto offCkData = buf.Length() + sizeof(UInt_t) + sizeof(Version_t);
88 std::uint64_t checksum = XXH3_64bits(buf.Buffer() + offCkData, buf.Length() - offCkData);
89 buf << checksum;
90 }
91}
#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
short Version_t
Definition RtypesCore.h:65
unsigned int UInt_t
Definition RtypesCore.h:46
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
std::uint16_t fVersionMajor
Changing the major version indicates forward incompatible changes; such changes should correspond to ...
Definition RNTuple.hxx:79
std::uint64_t fSeekFooter
The file offset of the footer excluding the TKey part.
Definition RNTuple.hxx:91
std::uint16_t fVersionMinor
Changing the minor version indicates new optional fields added to the RNTuple meta-data.
Definition RNTuple.hxx:81
std::uint16_t fVersionEpoch
Version of the RNTuple binary format that the writer supports (see specification).
Definition RNTuple.hxx:75
std::uint64_t fNBytesFooter
The size of the compressed ntuple footer.
Definition RNTuple.hxx:93
std::uint64_t fLenFooter
The size of the uncompressed ntuple footer.
Definition RNTuple.hxx:95
void Streamer(TBuffer &)
Definition RNTuple.cxx:27
TFile * fFile
! The file from which the ntuple was streamed, registered in the custom streamer
Definition RNTuple.hxx:99
std::uint64_t fLenHeader
The size of the uncompressed ntuple header.
Definition RNTuple.hxx:89
std::uint64_t fNBytesHeader
The size of the compressed ntuple header.
Definition RNTuple.hxx:87
std::uint16_t fVersionPatch
Changing the patch version indicates new backported features from newer binary format versions.
Definition RNTuple.hxx:83
static TClass * Class()
std::uint64_t fSeekHeader
The file offset of the header excluding the TKey part.
Definition RNTuple.hxx:85
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Version_t ReadVersion(UInt_t *start=nullptr, UInt_t *bcnt=nullptr, const TClass *cl=nullptr)=0
TObject * GetParent() const
Return pointer to parent of this buffer.
Definition TBuffer.cxx:262
Int_t BufferSize() const
Definition TBuffer.h:98
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=nullptr)=0
Bool_t IsReading() const
Definition TBuffer.h:86
void SetBufferOffset(Int_t offset=0)
Definition TBuffer.h:93
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
Int_t Length() const
Definition TBuffer.h:100
char * Buffer() const
Definition TBuffer.h:96
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:530