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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.pcm
*.so
Nested*.cxx
User*.cxx
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ROOT_EXE := $(shell which root.exe)
ROOT_EXE ?= $(shell which root.exe)
ifeq ($(ROOT_EXE),)
$(error Could not find root.exe)
endif
Expand Down
1 change: 1 addition & 0 deletions types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [`unique_ptr`](unique_ptr): `std::unique_ptr` with different element types
* [`unordered_multiset`](unordered_multiset): `std::unordered_multiset` with all `[Split]Index{32,64}` column types
* [`unordered_set`](unordered_set): `std::unordered_set` with all `[Split]Index{32,64}` column types
* [`user`](user): user-defined types, such as classes and enums
* [`variant`](variant): `std::variant` with `Switch` column type
* [`vector`](vector): `std::vector` with all `[Split]Index{32,64}` column types

Expand Down
24 changes: 15 additions & 9 deletions types/multiset/nested/Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
CXX=g++
CXXFLAGS_ROOT=$(shell root-config --cflags)
ifeq ($(CXXFLAGS_ROOT),)
$(error cannot find root-config: make sure to source thisroot.sh)
ROOT_CONFIG ?= $(shell which root-config)
ifeq ($(ROOT_CONFIG),)
$(error Could not find root-config)
endif
CXXFLAGS=-Wall $(CXXFLAGS_ROOT)
LDFLAGS=$(shell root-config --libs)
ROOTCLING ?= $(shell which rootcling)
ifeq ($(ROOTCLING),)
$(error Could not find rootcling)
endif

CXX := $(shell $(ROOT_CONFIG) --cxx)
CXXFLAGS_ROOT := $(shell $(ROOT_CONFIG) --cflags)
CXXFLAGS := -Wall $(CXXFLAGS_ROOT)
LDFLAGS := $(shell $(ROOT_CONFIG) --libs)

.PHONY: all clean

all: NestedMultiset.cxx libNestedMultiset.so
all: libNestedMultiset.so

NestedMultiset.cxx: NestedMultiset.hxx LinkDef.h
rootcling -f $@ $^
$(ROOTCLING) -f $@ $^

libNestedMultiset.so: NestedMultiset.cxx
$(CXX) -shared -fPIC -o $@ $^ $(CXXFLAGS) $(LDFLAGS)

clean:
rm -f NestedMultiset.cxx NestedMultiset_rdict.pcm libNestedMultiset.so
$(RM) NestedMultiset.cxx NestedMultiset_rdict.pcm libNestedMultiset.so
24 changes: 15 additions & 9 deletions types/set/nested/Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
CXX=g++
CXXFLAGS_ROOT=$(shell root-config --cflags)
ifeq ($(CXXFLAGS_ROOT),)
$(error cannot find root-config: make sure to source thisroot.sh)
ROOT_CONFIG ?= $(shell which root-config)
ifeq ($(ROOT_CONFIG),)
$(error Could not find root-config)
endif
CXXFLAGS=-Wall $(CXXFLAGS_ROOT)
LDFLAGS=$(shell root-config --libs)
ROOTCLING ?= $(shell which rootcling)
ifeq ($(ROOTCLING),)
$(error Could not find rootcling)
endif

CXX := $(shell $(ROOT_CONFIG) --cxx)
CXXFLAGS_ROOT := $(shell $(ROOT_CONFIG) --cflags)
CXXFLAGS := -Wall $(CXXFLAGS_ROOT)
LDFLAGS := $(shell $(ROOT_CONFIG) --libs)

.PHONY: all clean

all: NestedSet.cxx libNestedSet.so
all: libNestedSet.so

NestedSet.cxx: NestedSet.hxx LinkDef.h
rootcling -f $@ $^
$(ROOTCLING) -f $@ $^

libNestedSet.so: NestedSet.cxx
$(CXX) -shared -fPIC -o $@ $^ $(CXXFLAGS) $(LDFLAGS)

clean:
rm -f NestedSet.cxx NestedSet_rdict.pcm libNestedSet.so
$(RM) NestedSet.cxx NestedSet_rdict.pcm libNestedSet.so
24 changes: 15 additions & 9 deletions types/unordered_multiset/nested/Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
CXX=g++
CXXFLAGS_ROOT=$(shell root-config --cflags)
ifeq ($(CXXFLAGS_ROOT),)
$(error cannot find root-config: make sure to source thisroot.sh)
ROOT_CONFIG ?= $(shell which root-config)
ifeq ($(ROOT_CONFIG),)
$(error Could not find root-config)
endif
CXXFLAGS=-Wall $(CXXFLAGS_ROOT)
LDFLAGS=$(shell root-config --libs)
ROOTCLING ?= $(shell which rootcling)
ifeq ($(ROOTCLING),)
$(error Could not find rootcling)
endif

CXX := $(shell $(ROOT_CONFIG) --cxx)
CXXFLAGS_ROOT := $(shell $(ROOT_CONFIG) --cflags)
CXXFLAGS := -Wall $(CXXFLAGS_ROOT)
LDFLAGS := $(shell $(ROOT_CONFIG) --libs)

.PHONY: all clean

all: NestedUnorderedMultiset.cxx libNestedUnorderedMultiset.so
all: libNestedUnorderedMultiset.so

NestedUnorderedMultiset.cxx: NestedUnorderedMultiset.hxx LinkDef.h
rootcling -f $@ $^
$(ROOTCLING) -f $@ $^

libNestedUnorderedMultiset.so: NestedUnorderedMultiset.cxx
$(CXX) -shared -fPIC -o $@ $^ $(CXXFLAGS) $(LDFLAGS)

clean:
rm -f NestedUnorderedMultiset.cxx NestedUnorderedMultiset_rdict.pcm libNestedUnorderedMultiset.so
$(RM) NestedUnorderedMultiset.cxx NestedUnorderedMultiset_rdict.pcm libNestedUnorderedMultiset.so
24 changes: 15 additions & 9 deletions types/unordered_set/nested/Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
CXX=g++
CXXFLAGS_ROOT=$(shell root-config --cflags)
ifeq ($(CXXFLAGS_ROOT),)
$(error cannot find root-config: make sure to source thisroot.sh)
ROOT_CONFIG ?= $(shell which root-config)
ifeq ($(ROOT_CONFIG),)
$(error Could not find root-config)
endif
CXXFLAGS=-Wall $(CXXFLAGS_ROOT)
LDFLAGS=$(shell root-config --libs)
ROOTCLING ?= $(shell which rootcling)
ifeq ($(ROOTCLING),)
$(error Could not find rootcling)
endif

CXX := $(shell $(ROOT_CONFIG) --cxx)
CXXFLAGS_ROOT := $(shell $(ROOT_CONFIG) --cflags)
CXXFLAGS := -Wall $(CXXFLAGS_ROOT)
LDFLAGS := $(shell $(ROOT_CONFIG) --libs)

.PHONY: all clean

all: NestedUnorderedSet.cxx libNestedUnorderedSet.so
all: libNestedUnorderedSet.so

NestedUnorderedSet.cxx: NestedUnorderedSet.hxx LinkDef.h
rootcling -f $@ $^
$(ROOTCLING) -f $@ $^

libNestedUnorderedSet.so: NestedUnorderedSet.cxx
$(CXX) -shared -fPIC -o $@ $^ $(CXXFLAGS) $(LDFLAGS)

clean:
rm -f NestedUnorderedSet.cxx NestedUnorderedSet_rdict.pcm libNestedUnorderedSet.so
$(RM) NestedUnorderedSet.cxx NestedUnorderedSet_rdict.pcm libNestedUnorderedSet.so
4 changes: 4 additions & 0 deletions types/user/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# User-defined Types

* [`class`]: user-defined class(es)
* [`enum`]: user-defined enum(s)
12 changes: 12 additions & 0 deletions types/user/class/LinkDef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <cstdint>
#include <string>
#include <variant>

#ifdef __CLING__
#pragma link C++ class Base+;
#pragma link C++ class Nested+;
// ROOT does not (yet) support std::variant, but this line silences a warning
// that fVariant would not be saved (which is not true for RNTuple).
#pragma link C++ class std::variant<std::int32_t, std::string>;
#pragma link C++ class User+;
#endif
26 changes: 26 additions & 0 deletions types/user/class/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ROOT_CONFIG ?= $(shell which root-config)
ifeq ($(ROOT_CONFIG),)
$(error Could not find root-config)
endif
ROOTCLING ?= $(shell which rootcling)
ifeq ($(ROOTCLING),)
$(error Could not find rootcling)
endif

CXX := $(shell $(ROOT_CONFIG) --cxx)
CXXFLAGS_ROOT := $(shell $(ROOT_CONFIG) --cflags)
CXXFLAGS := -Wall $(CXXFLAGS_ROOT)
LDFLAGS := $(shell $(ROOT_CONFIG) --libs)

.PHONY: all clean

all: libUserClass.so

UserClass.cxx: UserClass.hxx LinkDef.h
$(ROOTCLING) -f $@ $^

libUserClass.so: UserClass.cxx
$(CXX) -shared -fPIC -o $@ $^ $(CXXFLAGS) $(LDFLAGS)

clean:
$(RM) UserClass.cxx UserClass_rdict.pcm libUserClass.so
20 changes: 20 additions & 0 deletions types/user/class/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# User-defined class(es)

## Fields

* `f` of type `User` with the following subfields:
* `:_0` of type `Base`
* `fInt` of type `std::int32_t`
* `fString` of type `std::string`
* `fVariant` of type `std::variant<std::int32_t, std::string>`
* `fVector` of type `std::vector<std::int32_t>`
* `fNested` of type `Nested`
* `fInt` of type `std::int32_t`
* `VectorUser` of type `std::vector<User>`

with the default column types.

## Entries

1. Simple values
2. Zero / empty values
27 changes: 27 additions & 0 deletions types/user/class/UserClass.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <cstdint>
#include <string>
#include <variant>
#include <vector>

using Variant = std::variant<std::int32_t, std::string>;
using Vector = std::vector<std::int32_t>;

class Base {
public:
std::int32_t fInt;
};

class Nested {
public:
std::int32_t fInt;
};

class User : public Base {
public:
std::string fString;
Variant fVariant;
Vector fVector;
Nested fNested;
};
122 changes: 122 additions & 0 deletions types/user/class/read.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <ROOT/REntry.hxx>
#include <ROOT/RNTupleReader.hxx>

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

#include <TSystem.h>

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

#include "UserClass.hxx"

static void PrintUserMembers(const User &value, std::ostream &os,
std::string_view indent) {
os << indent << "\":_0\": {\n";
os << indent << " \"fInt\": " << value.fInt << "\n";
os << indent << "},\n";
os << indent << "\"fString\": \"" << value.fString << "\",\n";

os << indent << "\"fVariant\": ";
if (value.fVariant.index() == 0) {
os << std::get<std::int32_t>(value.fVariant);
} else if (value.fVariant.index() == 1) {
os << "\"" << std::get<std::string>(value.fVariant) << "\"";
}
os << ",\n";

os << indent << "\"fVector\": [";
bool first = true;
for (auto element : value.fVector) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n" << indent << " " << element;
}
if (!value.fVector.empty()) {
os << "\n" << indent;
}
os << "],\n";

os << indent << "\"fNested\": {\n";
os << indent << " \"fInt\": " << value.fNested.fInt << "\n";
os << indent << "}\n";
}

static void PrintUserValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
auto &value = *entry.GetPtr<User>(name);
os << " \"" << name << "\": {\n";
PrintUserMembers(value, os, " ");
os << " }";
if (!last) {
os << ",";
}
os << "\n";
}

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

void read(std::string_view input = "types.user.class.root",
std::string_view output = "types.user.class.json") {
if (gSystem->Load("libUserClass") == -1)
throw std::runtime_error("could not find the required ROOT dictionaries, "
"please make sure to run `make` first");

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";

PrintUserValue(entry, "f", os);
PrintUserVector(entry, "Vector", os, /*last=*/true);

os << " }";
// Newline is intentionally missing, may need to print a comma before the
// next entry.
}
os << "\n";
os << "]\n";
}
Loading