Example of a streaming vector: a special purpose container that reads large vectors piece-wise.
#include <cstdint>
#include <iostream>
#include <vector>
#include <utility>
constexpr char const *kFileName = "ntpl016_streaming_vector.root";
constexpr char const *kNTupleName = "ntpl";
constexpr char const *kFieldName = "LargeVector";
constexpr unsigned int kNEvents = 10;
constexpr unsigned int kNElementsPerVector = 1000000;
void CreateRNTuple()
{
auto ptrLargeVector = model->MakeField<std::vector<std::uint32_t>>(kFieldName);
auto prng = std::make_unique<TRandom3>();
prng->SetSeed();
ptrLargeVector->clear();
for (std::size_t j = 0; j < kNElementsPerVector; j++)
ptrLargeVector->emplace_back(prng->Integer(-1));
}
std::cout << "RNTuple written" << std::endl;
}
void ReadRNTupleSimple()
{
const auto nEntries = reader->GetNEntries();
std::cout << "Simple reading, found " << nEntries << " entries" << std::endl;
auto ptrLargeVector = reader->GetModel().GetDefaultEntry().GetPtr<std::vector<std::uint32_t>>(kFieldName);
reader->LoadEntry(i);
const auto vectorSize = ptrLargeVector->size();
for (auto val : *ptrLargeVector)
std::cout <<
"Size and sum of vector: " << vectorSize <<
" " <<
sum << std::endl;
}
std::cout << "RNTuple simple read" << std::endl;
}
template <class T>
class StreamingVectorView {
ROOT::RNTupleCollectionView fVectorView;
ROOT::RNTupleView<T> fItemView;
public:
class Iterator {
ROOT::RNTupleLocalRange::RIterator fRangeItr;
ROOT::RNTupleView<T> &fView;
public:
using iterator = Iterator;
using iterator_category = std::input_iterator_tag;
using pointer =
const T *;
using reference =
const T &;
Iterator(ROOT::RNTupleLocalRange::RIterator rangeItr, ROOT::RNTupleView<T> &view)
: fRangeItr(rangeItr), fView(view)
{
}
iterator operator++(int)
{
++(*this);
}
iterator &operator++()
{
++fRangeItr;
return *this;
}
reference
operator*() {
return fView.operator()(*fRangeItr); }
pointer operator->() { return &fView.operator()(*fRangeItr); }
bool operator==(
const iterator &rh)
const {
return fRangeItr == rh.fRangeItr; }
bool operator!=(
const iterator &rh)
const {
return fRangeItr != rh.fRangeItr; }
};
explicit StreamingVectorView(ROOT::RNTupleCollectionView vectorView)
: fVectorView(std::move(vectorView)), fItemView(fVectorView.GetView<
T>(
"_0"))
{
}
Iterator begin() {
return Iterator(fRange.
begin(), fItemView); }
Iterator end() {
return Iterator(fRange.
end(), fItemView); }
{
fEntry = entry;
fSize = fVectorView.operator()(fEntry);
}
};
void ReadRNTupleStreamingVector()
{
const auto nEntries = reader->GetNEntries();
std::cout << "Streamed reading, found " << nEntries << " entries" << std::endl;
StreamingVectorView<std::uint32_t> streamingVector(reader->GetCollectionView(kFieldName));
streamingVector.LoadEntry(i);
const auto vectorSize = streamingVector.size();
for (auto val : streamingVector)
std::cout <<
"Size and sum of vector: " << vectorSize <<
" " <<
sum << std::endl;
}
std::cout << "RNTuple streaming read" << std::endl;
}
void ntpl016_streaming_vector()
{
CreateRNTuple();
ReadRNTupleSimple();
ReadRNTupleStreamingVector();
}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Bool_t operator!=(const TDatime &d1, const TDatime &d2)
Bool_t operator==(const TDatime &d1, const TDatime &d2)
TTime operator*(const TTime &t1, const TTime &t2)
ROOT::RNTupleLocalRange GetCollectionRange(ROOT::NTupleSize_t globalIndex)
static std::unique_ptr< RNTupleModel > Create()
Common user-tunable settings for reading RNTuples.
void SetClusterCache(EClusterCache val)
static std::unique_ptr< RNTupleReader > Open(std::string_view ntupleName, std::string_view storage, const ROOT::RNTupleReadOptions &options=ROOT::RNTupleReadOptions())
Open an RNTuple for reading.
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.
constexpr NTupleSize_t kInvalidNTupleIndex
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
constexpr DescriptorId_t kInvalidDescriptorId
static uint64_t sum(uint64_t i)