Skip to content
Snippets Groups Projects
Commit 1f8bc2be authored by Alaettin Serhan Mete's avatar Alaettin Serhan Mete 🦅
Browse files

Refine the example and add a TTree version

parent 73de1c55
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,8 @@
* Author: Alaettin Serhan Mete <amete@anl.gov>
*/
#include <vector>
// Dummy Class to define the Enum
class MyEnumClass {
public:
......@@ -21,3 +23,10 @@ class MyPersClass {
float m_float;
MyEnumClass::MyEnum m_enum;
};
// Instantiate all necessary types for the dictionary
namespace {
struct GCCXML_DUMMY_INSTANTIATION {
std::vector<MyPersClass> dummy;
};
}
......@@ -2,8 +2,12 @@
* Author: Alaettin Serhan Mete <amete@anl.gov>
*/
#define VERSION 0 // 0: RNTuple, 1: TTree
#include <ROOT/RNTuple.hxx>
#include <ROOT/RNTupleModel.hxx>
#include <TFile.h>
#include <TTree.h>
#include "MyPersClass.h"
......@@ -26,21 +30,44 @@ int main(int argc, char *argv[]) {
// Write Here
{
auto model = RNTupleModel::Create();
auto field = model->MakeField<std::vector<MyPersClass>>("foo");
auto ntuple = RNTupleWriter::Recreate(std::move(model), ntupleName, outputName);
for (unsigned i = 0; i < 10; ++i) {
for (unsigned j = 0; j < 10; ++j) {
field->emplace_back(i*j, j<5 ? MyEnumClass::ZERO : MyEnumClass::ONE);
if(VERSION == 0) { // RNTuple version
auto model = RNTupleModel::Create();
auto field = model->MakeField<std::vector<MyPersClass>>("foo");
auto ntuple = RNTupleWriter::Recreate(std::move(model), ntupleName, outputName);
for (unsigned i = 0; i < 10; ++i) {
field->clear();
for (unsigned j = 0; j < 10; ++j) {
field->emplace_back(i*j, j<5 ? MyEnumClass::ZERO : MyEnumClass::ONE);
}
ntuple->Fill();
}
} else if(VERSION == 1) { // TTree version
TFile* f = TFile::Open(outputName.c_str(), "RECREATE");
TTree* t = new TTree(ntupleName.c_str(), "");
std::vector<MyPersClass> result;
t->Branch("foo", &result);
for (unsigned i = 0; i < 10; ++i) {
result.clear();
for (unsigned j = 0; j < 10; ++j) {
result.emplace_back(i*j, j<5 ? MyEnumClass::ZERO : MyEnumClass::ONE);
}
t->Fill();
}
ntuple->Fill();
f->Write();
f->Close();
}
}
// Read Here
{
auto ntuple = RNTupleReader::Open(ntupleName, outputName);
ntuple->Show(0);
if(VERSION == 0) { // RNTuple version
auto ntuple = RNTupleReader::Open(ntupleName, outputName);
ntuple->Show(9);
} else if (VERSION == 1) { // TTree version
TFile* f = TFile::Open(outputName.c_str(), "OPEN");
TTree* t = f->Get<TTree>(ntupleName.c_str());
t->Show(9);
}
}
return 0;
......
<lcgdict>
<!-- These two are probably redundant in this context -->
<class name="MyEnumClass" />
<enum name="MyEnumClass::MyEnum" />
<!-- These are the important ones for this example -->
<class name="MyPersClass" />
<class name="std::vector<MyPersClass>" />
</lcgdict>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment