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
3 changes: 3 additions & 0 deletions types/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Types

* [`array`](array): `std::array`
* [`atomic`](atomic): `std::atomic`
* [`bitset`](bitset): `std::bitset`
* [`fundamental`](fundamental): fundamental column types
* [`multiset`](multiset): `std::multiset` with all `[Split]Index{32,64}` column types
* [`optional`](optional): `std::optional` with different element types
Expand Down
16 changes: 16 additions & 0 deletions types/array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# `std::array`

## Fields

* `Array_Int32`: `std::array<std::int32_t, 2>`
* `Array_Array`: `std::array<std::array<std::int32_t, 2>, 3>>`
* `Array_String`: `std::array<std::string, 2>`
* `Array_Variant`: `std::array<std::variant<std::int32_t, std::string>, 2>`
* `Array_Vector`: `std::array<std::vector<std::int32_t>, 2>`

with the default column types.

## Entries

1. Simple values
2. Zero / empty values
140 changes: 140 additions & 0 deletions types/array/read.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#include <ROOT/REntry.hxx>
#include <ROOT/RNTupleReader.hxx>

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

#include <array>
#include <cstdint>
#include <fstream>
#include <ostream>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>

using ArrayInt32 = std::array<std::int32_t, 2>;
using Variant = std::variant<std::int32_t, std::string>;
using VectorInt32 = std::vector<std::int32_t>;

template <typename T> static void PrintValue(const T &value, std::ostream &os);

template <> void PrintValue(const std::int32_t &value, std::ostream &os) {
os << value;
}

template <> void PrintValue(const std::string &value, std::ostream &os) {
os << "\"" << value << "\"";
}

template <> void PrintValue(const Variant &value, std::ostream &os) {
if (value.index() == 0) {
PrintValue(std::get<std::int32_t>(value), os);
} else if (value.index() == 1) {
PrintValue(std::get<std::string>(value), os);
}
}

template <> void PrintValue(const VectorInt32 &value, std::ostream &os) {
os << "[";
bool first = true;
for (auto element : value) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n " << element;
}
if (!value.empty()) {
os << "\n ";
}
os << "]";
}

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

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

void read(std::string_view input = "types.array.root",
std::string_view output = "types.array.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";

PrintArrayValue<std::int32_t>(entry, "Array_Int32", os);
PrintArrayArrayValue(entry, "Array_Array", os);
PrintArrayValue<std::string>(entry, "Array_String", os);
PrintArrayValue<Variant>(entry, "Array_Variant", os);
PrintArrayValue<VectorInt32>(entry, "Array_Vector", os, /*last=*/true);

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

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

#include <array>
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>

using ArrayInt32 = std::array<std::int32_t, 2>;
using Variant = std::variant<std::int32_t, std::string>;
using VectorInt32 = std::vector<std::int32_t>;

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

auto Array_Int32 = model->MakeField<ArrayInt32>("Array_Int32");
auto Array_Array = model->MakeField<std::array<ArrayInt32, 3>>("Array_Array");
auto Array_String =
model->MakeField<std::array<std::string, 2>>("Array_String");
auto Array_Variant =
model->MakeField<std::array<Variant, 2>>("Array_Variant");
auto Array_Vector =
model->MakeField<std::array<VectorInt32, 2>>("Array_Vector");

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

// First entry: simple values
*Array_Int32 = {1, 2};
*Array_Array = {ArrayInt32{3, 4}, ArrayInt32{5, 6}, ArrayInt32{7, 8}};
*Array_String = {"ijk", "lmn"};
*Array_Vector = {VectorInt32{15, 16, 17}, VectorInt32{17, 18}};
*Array_Variant = {19, "uvw"};
writer->Fill();

// Second entry: zero / empty values
*Array_Int32 = {0, 0};
*Array_Array = {ArrayInt32{0, 0}, ArrayInt32{0, 0}, ArrayInt32{0, 0}};
*Array_String = {"", ""};
*Array_Vector = {VectorInt32{}, VectorInt32{}};
*Array_Variant = {0, ""};
writer->Fill();
}
16 changes: 16 additions & 0 deletions types/atomic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# `std::atomic`

## Fields

* `Bit`
* `Byte`
* `Char`
* `[U]Int{8,16,32,64}`
* `Real{32,64}`

with the corresponding column types.

## Entries

1. Ascending values
2. Zero values
82 changes: 82 additions & 0 deletions types/atomic/read.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <ROOT/REntry.hxx>
#include <ROOT/RNTupleReader.hxx>

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

#include <atomic>
#include <cstddef> // for std::byte
#include <cstdint>
#include <fstream>
#include <ostream>
#include <string>
#include <string_view>

template <typename T>
static void PrintIntegerValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
T value = *entry.GetPtr<std::atomic<T>>(name);
os << " \"" << name << "\": ";
// We want to print the integer value even if it is a character; use the unary
// + operator (https://stackoverflow.com/a/28414758).
os << +value;
if (!last) {
os << ",";
}
os << "\n";
}

template <typename T>
static void PrintRealValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
T value = *entry.GetPtr<std::atomic<T>>(name);
os << " \"" << name << "\": \"" << value << "\"";
if (!last) {
os << ",";
}
os << "\n";
}

void read(std::string_view input = "types.atomic.root",
std::string_view output = "types.atomic.json") {
std::ofstream os(std::string{output});
// Print floating-point numbers as hexadecimal literals.
os << std::hexfloat;
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";

PrintIntegerValue<bool>(entry, "Bit", os);
os << " \"Byte\": ";
os << std::to_integer<int>(*entry.GetPtr<std::atomic<std::byte>>("Byte"));
os << ",\n";
PrintIntegerValue<char>(entry, "Char", os);
PrintIntegerValue<std::int8_t>(entry, "Int8", os);
PrintIntegerValue<std::uint8_t>(entry, "UInt8", os);
PrintIntegerValue<std::int16_t>(entry, "Int16", os);
PrintIntegerValue<std::uint16_t>(entry, "UInt16", os);
PrintIntegerValue<std::int32_t>(entry, "Int32", os);
PrintIntegerValue<std::uint32_t>(entry, "UInt32", os);
PrintIntegerValue<std::int64_t>(entry, "Int64", os);
PrintIntegerValue<std::uint64_t>(entry, "UInt64", os);
PrintRealValue<float>(entry, "Real32", os);
PrintRealValue<double>(entry, "Real64", 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/atomic/write.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <ROOT/RNTupleModel.hxx>
#include <ROOT/RNTupleWriteOptions.hxx>
#include <ROOT/RNTupleWriter.hxx>

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

#include <atomic>
#include <cstddef> // for std::byte
#include <cstdint>
#include <memory>
#include <string_view>

template <typename T>
static std::shared_ptr<std::atomic<T>> MakeAtomicField(RNTupleModel &model,
std::string_view name) {
return model.MakeField<std::atomic<T>>(name);
}

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

auto Bit = MakeAtomicField<bool>(*model, "Bit");
auto Byte = MakeAtomicField<std::byte>(*model, "Byte");
auto Char = MakeAtomicField<char>(*model, "Char");

auto Int8 = MakeAtomicField<std::int8_t>(*model, "Int8");
auto UInt8 = MakeAtomicField<std::uint8_t>(*model, "UInt8");
auto Int16 = MakeAtomicField<std::int16_t>(*model, "Int16");
auto UInt16 = MakeAtomicField<std::uint16_t>(*model, "UInt16");
auto Int32 = MakeAtomicField<std::int32_t>(*model, "Int32");
auto UInt32 = MakeAtomicField<std::uint32_t>(*model, "UInt32");
auto Int64 = MakeAtomicField<std::int64_t>(*model, "Int64");
auto UInt64 = MakeAtomicField<std::uint64_t>(*model, "UInt64");

auto Real32 = MakeAtomicField<float>(*model, "Real32");
auto Real64 = MakeAtomicField<double>(*model, "Real64");

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

// First entry: ascending values
*Bit = 1;
*Byte = std::byte{2};
*Char = 3;
*Int8 = 4;
*UInt8 = 5;
*Int16 = 6;
*UInt16 = 7;
*Int32 = 8;
*UInt32 = 9;
*Int64 = 10;
*UInt64 = 11;
*Real32 = 12.0f;
*Real64 = 13.0;
writer->Fill();

// Second entry: zero values
*Bit = 0;
*Byte = std::byte{0};
*Char = 0;
*Int8 = 0;
*UInt8 = 0;
*Int16 = 0;
*UInt16 = 0;
*Int32 = 0;
*UInt32 = 0;
*Int64 = 0;
*UInt64 = 0;
*Real32 = 0;
*Real64 = 0;
writer->Fill();
}
Loading