Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ntpl005_introspection.C File Reference

Detailed Description

View in nbviewer Open in SWAN Write and read an RNTuple from a user-defined class.

Adapted from tv3.C Illustrates various RNTuple introspection methods.

// NOTE: The RNTuple classes are experimental at this point.
// Functionality, interface, and data format is still subject to changes.
// Do not use for real data!
// Until C++ runtime modules are universally used, we explicitly load the ntuple library. Otherwise
// triggering autoloading from the use of templated types would require an exhaustive enumeration
// of "all" template instances in the LinkDef file.
R__LOAD_LIBRARY(ROOTNTuple)
#include <ROOT/RNTuple.hxx>
#include <Compression.h>
#include <TCanvas.h>
#include <TH1.h>
#include <TRandom.h>
#include <TSystem.h>
#include <cassert>
// Import classes from experimental namespace for the time being
using ENTupleInfo = ROOT::Experimental::ENTupleInfo;
using ENTupleShowFormat = ROOT::Experimental::ENTupleShowFormat;
using RNTupleModel = ROOT::Experimental::RNTupleModel;
using RNTupleReader = ROOT::Experimental::RNTupleReader;
using RNTupleWriter = ROOT::Experimental::RNTupleWriter;
using RNTupleWriteOptions = ROOT::Experimental::RNTupleWriteOptions;
constexpr char const* kNTupleFileName = "ntpl005_introspection.root";
// Store entries of type Vector3 in the ntuple
class Vector3 {
private:
double fX = 0;
double fY = 0;
double fZ = 0;
public:
double x() const { return fX; }
double y() const { return fY; }
double z() const { return fZ; }
void SetXYZ(double x, double y, double z) {
fX = x;
fY = y;
fZ = z;
}
};
void Generate()
{
auto model = RNTupleModel::Create();
auto fldVector3 = model->MakeField<Vector3>("v3");
// Explicitly enforce a certain compression algorithm
RNTupleWriteOptions options;
auto ntuple = RNTupleWriter::Recreate(std::move(model), "Vector3", kNTupleFileName, options);
for (unsigned int i = 0; i < 500000; ++i) {
fldVector3->SetXYZ(r.Gaus(0,1), r.Landau(0,1), r.Gaus(100,10));
ntuple->Fill();
}
}
void ntpl005_introspection() {
Generate();
auto ntuple = RNTupleReader::Open("Vector3", kNTupleFileName);
// Display the schema of the ntuple
ntuple->PrintInfo();
// Display information about the storage layout of the data
ntuple->PrintInfo(ENTupleInfo::kStorageDetails);
// Display the first entry; no model was defined, hence we use kCompleteJSON to force showing all fields
ntuple->Show(0, ENTupleShowFormat::kCompleteJSON);
// Collect I/O runtime counters when processing the data set.
// Maintaining the counters comes with a small performance overhead, so it has to be explicitly enabled
ntuple->EnableMetrics();
// Plot the y components of vector3
TCanvas *c1 = new TCanvas("c1","RNTuple Demo", 10, 10, 600, 800);
c1->Divide(1,2);
c1->cd(1);
TH1F h1("x", "x component of Vector3", 100, -3, 3);
{
/// We enclose viewX in a scope in order to indicate to the RNTuple when we are not
/// anymore interested in v3.fX
auto viewX = ntuple->GetView<double>("v3.fX");
for (auto i : ntuple->GetEntryRange()) {
h1.Fill(viewX(i));
}
}
c1->cd(2);
TH1F h2("y", "y component of Vector3", 100, -5, 20);
auto viewY = ntuple->GetView<double>("v3.fY");
for (auto i : ntuple->GetEntryRange()) {
h2.Fill(viewY(i));
}
h2.DrawCopy();
// Display the I/O operation statistics performed by the RNTuple reader
ntuple->PrintInfo(ENTupleInfo::kMetrics);
// We read 2 out of the 3 Vector3 members and thus should have requested approximately 2/3 of the file
FileStat_t fileStat;
auto retval = gSystem->GetPathInfo(kNTupleFileName, fileStat);
assert(retval == 0);
float fileSize = static_cast<float>(fileStat.fSize);
float nbytesRead = ntuple->GetMetrics().GetCounter("RNTupleReader.RPageSourceFile.szReadPayload")->GetValueAsInt() +
ntuple->GetMetrics().GetCounter("RNTupleReader.RPageSourceFile.szReadOverhead")->GetValueAsInt();
std::cout << "File size: " << fileSize / 1024. / 1024. << " MiB" << std::endl;
std::cout << "Read from file: " << nbytesRead / 1024. / 1024. << " MiB" << std::endl;
std::cout << "Ratio: " << nbytesRead / fileSize << std::endl;
}
ROOT::R::TRInterface & r
Definition Object.C:4
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:472
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
The RNTupleModel encapulates the schema of an ntuple.
An RNTuple that is used to read data from storage.
Definition RNTuple.hxx:102
Common user-tunable settings for storing ntuples.
An RNTuple that gets filled with entries (data) and writes them to storage.
Definition RNTuple.hxx:351
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:575
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3351
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition TH1.cxx:3121
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
int GetPathInfo(const char *path, Long_t *id, Long_t *size, Long_t *flags, Long_t *modtime)
Get info about a file: id, size, flags, modification time.
Definition TSystem.cxx:1398
Definition tv3.C:2
Double_t fZ
Definition tv3.C:5
Double_t x()
Definition tv3.C:10
Double_t z()
Definition tv3.C:12
Double_t fX
Definition tv3.C:3
Double_t y()
Definition tv3.C:11
Double_t fY
Definition tv3.C:4
void SetXYZ(Double_t x, Double_t y, Double_t z)
Definition tv3.C:14
return c1
Definition legend1.C:41
TH1F * h1
Definition legend1.C:5
ENTupleInfo
Listing of the different options that can be printed by RNTupleReader::GetInfo()
Definition RNTuple.hxx:51
ENTupleShowFormat
Listing of the different entry output formats of RNTupleReader::Show()
Definition RNTuple.hxx:60
Long64_t fSize
Definition TSystem.h:130
@ kUseGeneralPurpose
Use the new recommended general-purpose setting; it is a best trade-off between compression ratio/dec...
Definition Compression.h:54
Date
April 2020
Author
The ROOT Team

Definition in file ntpl005_introspection.C.