Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [`multiset`](multiset): `std::multiset` with all `[Split]Index{32,64}` column types
* [`optional`](optional): `std::optional` with different element types
* [`pair`](pair): `std::pair` with different element types
* [`RVec`](RVec): `ROOT::RVec` with all `[Split]Index{32,64}` column types
* [`set`](set): `std::set` with all `[Split]Index{32,64}` column types
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types
* [`tuple`](tuple): `std::tuple` with different element types
Expand Down
7 changes: 7 additions & 0 deletions types/RVec/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# `ROOT::RVec`

* [`fundamental`](fundamental): `ROOT::RVec<std::int32_t>`
* [`nested`](nested): `ROOT::RVec<ROOT::RVec<std::int32_t>>`

__Missing:__
Field with the shorter alias `ROOT::RVec<T>`; ROOT always stores the fully qualified type name `ROOT::VecOps::RVec<T>`.
15 changes: 15 additions & 0 deletions types/RVec/fundamental/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# `ROOT::RVec<std::int32_t>`

## Fields

* `[Split]Index{32,64}`

with the corresponding column type for offset column of the collection parent field.
All child fields use the default column encoding `Int32`.

## Entries

1. Single-element vectors, with ascending values
2. Empty vectors
3. Increasing number of elements in the vector:
one element in the first field, two elements in the second field, etc.
68 changes: 68 additions & 0 deletions types/RVec/fundamental/read.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <ROOT/REntry.hxx>
#include <ROOT/RNTupleReader.hxx>
#include <ROOT/RVec.hxx>

using ROOT::Experimental::REntry;
using ROOT::Experimental::RNTupleReader;

#include <cstdint>
#include <fstream>
#include <ostream>
#include <string>
#include <string_view>

using Vector = ROOT::RVec<std::int32_t>;

static void PrintVectorValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
Vector &value = *entry.GetPtr<Vector>(name);
os << " \"" << name << "\": [";
bool first = true;
for (auto element : value) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n " << element;
}
if (!value.empty()) {
os << "\n ";
}
os << "]";
if (!last) {
os << ",";
}
os << "\n";
}

void read(std::string_view input = "types.RVec.fundamental.root",
std::string_view output = "types.RVec.fundamental.json") {
std::ofstream os(std::string{output});
os << "[\n";

auto reader = RNTupleReader::Open("ntpl", input);
auto &entry = reader->GetModel().GetDefaultEntry();
bool first = true;
for (auto index : *reader) {
reader->LoadEntry(index);

if (first) {
first = false;
} else {
os << ",\n";
}
os << " {\n";

PrintVectorValue(entry, "Index32", os);
PrintVectorValue(entry, "Index64", os);
PrintVectorValue(entry, "SplitIndex32", os);
PrintVectorValue(entry, "SplitIndex64", os, /*last=*/true);

os << " }";
// Newline is intentionally missing, may need to print a comma before the
// next entry.
}
os << "\n";
os << "]\n";
}
67 changes: 67 additions & 0 deletions types/RVec/fundamental/write.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <ROOT/RField.hxx>
#include <ROOT/RNTupleModel.hxx>
#include <ROOT/RNTupleUtil.hxx>
#include <ROOT/RNTupleWriteOptions.hxx>
#include <ROOT/RNTupleWriter.hxx>
#include <ROOT/RVec.hxx>

using ROOT::Experimental::EColumnType;
using ROOT::Experimental::RField;
using ROOT::Experimental::RNTupleModel;
using ROOT::Experimental::RNTupleWriteOptions;
using ROOT::Experimental::RNTupleWriter;

#include <cstdint>
#include <memory>
#include <string_view>

using Vector = ROOT::RVec<std::int32_t>;

static std::shared_ptr<Vector> MakeVectorField(RNTupleModel &model,
std::string_view name,
EColumnType indexType) {
auto field = std::make_unique<RField<Vector>>(name);
field->SetColumnRepresentatives({{indexType}});
model.AddField(std::move(field));
return model.GetDefaultEntry().GetPtr<Vector>(name);
}

void write(std::string_view filename = "types.RVec.fundamental.root") {
auto model = RNTupleModel::Create();

// Non-split index encoding
auto Index32 = MakeVectorField(*model, "Index32", EColumnType::kIndex32);
auto Index64 = MakeVectorField(*model, "Index64", EColumnType::kIndex64);

// Split index encoding
auto SplitIndex32 =
MakeVectorField(*model, "SplitIndex32", EColumnType::kSplitIndex32);
auto SplitIndex64 =
MakeVectorField(*model, "SplitIndex64", EColumnType::kSplitIndex64);

RNTupleWriteOptions options;
options.SetCompression(0);
auto writer =
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);

// First entry: single-element vectors, with ascending values
*Index32 = {1};
*Index64 = {2};
*SplitIndex32 = {3};
*SplitIndex64 = {4};
writer->Fill();

// Second entry: empty vectors
*Index32 = {};
*Index64 = {};
*SplitIndex32 = {};
*SplitIndex64 = {};
writer->Fill();

// Third entry: increasing number of elements in the vector
*Index32 = {1};
*Index64 = {2, 3};
*SplitIndex32 = {4, 5, 6};
*SplitIndex64 = {7, 8, 9, 10};
writer->Fill();
}
14 changes: 14 additions & 0 deletions types/RVec/nested/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# `ROOT::RVec<ROOT::RVec<std::int32_t>>`

## Fields

* `[Split]Index{32,64}`

with the corresponding column type for offset columns of the two collection parent fields.
All child fields use the default column encoding `Int32`.

## Entries

1. Single-element vectors, with ascending values
2. Empty vectors
3. Increasing number of elements in the outer vector, with arbitrary lengths of the inner vectors
81 changes: 81 additions & 0 deletions types/RVec/nested/read.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <ROOT/REntry.hxx>
#include <ROOT/RNTupleReader.hxx>
#include <ROOT/RVec.hxx>

using ROOT::Experimental::REntry;
using ROOT::Experimental::RNTupleReader;

#include <cstdint>
#include <fstream>
#include <ostream>
#include <string>
#include <string_view>

using Vector = ROOT::RVec<ROOT::RVec<std::int32_t>>;

static void PrintNestedVectorValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
Vector &value = *entry.GetPtr<Vector>(name);
os << " \"" << name << "\": [";
bool outerFirst = true;
for (auto inner : value) {
if (outerFirst) {
outerFirst = false;
} else {
os << ",";
}
os << "\n [";
bool innerFirst = true;
for (auto element : inner) {
if (innerFirst) {
innerFirst = false;
} else {
os << ",";
}
os << "\n " << element;
}
if (!inner.empty()) {
os << "\n ";
}
os << "]";
}
if (!value.empty()) {
os << "\n ";
}
os << "]";
if (!last) {
os << ",";
}
os << "\n";
}

void read(std::string_view input = "types.RVec.nested.root",
std::string_view output = "types.RVec.nested.json") {
std::ofstream os(std::string{output});
os << "[\n";

auto reader = RNTupleReader::Open("ntpl", input);
auto &entry = reader->GetModel().GetDefaultEntry();
bool first = true;
for (auto index : *reader) {
reader->LoadEntry(index);

if (first) {
first = false;
} else {
os << ",\n";
}
os << " {\n";

PrintNestedVectorValue(entry, "Index32", os);
PrintNestedVectorValue(entry, "Index64", os);
PrintNestedVectorValue(entry, "SplitIndex32", os);
PrintNestedVectorValue(entry, "SplitIndex64", os, /*last=*/true);

os << " }";
// Newline is intentionally missing, may need to print a comma before the
// next entry.
}
os << "\n";
os << "]\n";
}
76 changes: 76 additions & 0 deletions types/RVec/nested/write.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <ROOT/RField.hxx>
#include <ROOT/RNTupleModel.hxx>
#include <ROOT/RNTupleUtil.hxx>
#include <ROOT/RNTupleWriteOptions.hxx>
#include <ROOT/RNTupleWriter.hxx>
#include <ROOT/RVec.hxx>

using ROOT::Experimental::EColumnType;
using ROOT::Experimental::RField;
using ROOT::Experimental::RNTupleModel;
using ROOT::Experimental::RNTupleWriteOptions;
using ROOT::Experimental::RNTupleWriter;

#include <cstdint>
#include <memory>
#include <string_view>

using Inner = ROOT::RVec<std::int32_t>;
using Vector = ROOT::RVec<Inner>;

static std::shared_ptr<Vector> MakeVectorField(RNTupleModel &model,
std::string_view name,
EColumnType indexType) {
auto field = std::make_unique<RField<Vector>>(name);
field->SetColumnRepresentatives({{indexType}});
field->GetSubFields()[0]->SetColumnRepresentatives({{indexType}});
model.AddField(std::move(field));
return model.GetDefaultEntry().GetPtr<Vector>(name);
}

void write(std::string_view filename = "types.RVec.nested.root") {
auto model = RNTupleModel::Create();

// Non-split index encoding
auto Index32 = MakeVectorField(*model, "Index32", EColumnType::kIndex32);
auto Index64 = MakeVectorField(*model, "Index64", EColumnType::kIndex64);

// Split index encoding
auto SplitIndex32 =
MakeVectorField(*model, "SplitIndex32", EColumnType::kSplitIndex32);
auto SplitIndex64 =
MakeVectorField(*model, "SplitIndex64", EColumnType::kSplitIndex64);

RNTupleWriteOptions options;
options.SetCompression(0);
auto writer =
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);

// First entry: single-element vectors, with ascending values
*Index32 = {{1}};
*Index64 = {{2}};
*SplitIndex32 = {{3}};
*SplitIndex64 = {{4}};
writer->Fill();

// Second entry: empty vectors
*Index32 = {};
*Index64 = {};
*SplitIndex32 = {};
*SplitIndex64 = {};
writer->Fill();

// Third entry: increasing number of elements in the outer vector
*Index32 = {{1}};
*Index64 = {{}, {2, 3}};
*SplitIndex32 = {{4}, {}, {5, 6}};
*SplitIndex64 = {{}, {7, 8, 9}, {}, {10}};
writer->Fill();

// Work around bug in 6.34 for destroying RVec's, later fixed by commit
// https://github.com/root-project/root/commit/996cac359d for 6.36.
Index32->clear();
Index64->clear();
SplitIndex32->clear();
SplitIndex64->clear();
}