Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
ntpl005_introspection.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ntuple
3/// \notebook
4/// Write and read an RNTuple from a user-defined class. Adapted from tv3.C
5/// Illustrates various RNTuple introspection methods.
6///
7/// \macro_image
8/// \macro_code
9///
10/// \date April 2020
11/// \author The ROOT Team
12
13// NOTE: The RNTuple classes are experimental at this point.
14// Functionality, interface, and data format is still subject to changes.
15// Do not use for real data!
16
17// Until C++ runtime modules are universally used, we explicitly load the ntuple library. Otherwise
18// triggering autoloading from the use of templated types would require an exhaustive enumeration
19// of "all" template instances in the LinkDef file.
20R__LOAD_LIBRARY(ROOTNTuple)
21
22#include <ROOT/RNTuple.hxx>
23#include <ROOT/RNTupleModel.hxx>
25
26#include <Compression.h>
27#include <TCanvas.h>
28#include <TH1.h>
29#include <TRandom.h>
30#include <TSystem.h>
31
32#include <cassert>
33
34// Import classes from experimental namespace for the time being
36using RNTupleModel = ROOT::Experimental::RNTupleModel;
37using RNTupleReader = ROOT::Experimental::RNTupleReader;
38using RNTupleWriter = ROOT::Experimental::RNTupleWriter;
39using RNTupleWriteOptions = ROOT::Experimental::RNTupleWriteOptions;
40
41constexpr char const* kNTupleFileName = "ntpl005_introspection.root";
42
43// Store entries of type Vector3 in the ntuple
44class Vector3 {
45private:
46 double fX = 0;
47 double fY = 0;
48 double fZ = 0;
49
50public:
51 double x() const { return fX; }
52 double y() const { return fY; }
53 double z() const { return fZ; }
54
55 void SetXYZ(double x, double y, double z) {
56 fX = x;
57 fY = y;
58 fZ = z;
59 }
60};
61
62
63void Generate()
64{
65 auto model = RNTupleModel::Create();
66 auto fldVector3 = model->MakeField<Vector3>("v3");
67
68 // Explicitly enforce a certain compression algorithm
69 RNTupleWriteOptions options;
71
72 auto ntuple = RNTupleWriter::Recreate(std::move(model), "Vector3", kNTupleFileName, options);
73 TRandom r;
74 for (unsigned int i = 0; i < 500000; ++i) {
75 fldVector3->SetXYZ(r.Gaus(0,1), r.Landau(0,1), r.Gaus(100,10));
76 ntuple->Fill();
77 }
78}
79
80
81void ntpl005_introspection() {
82 Generate();
83
84 auto ntuple = RNTupleReader::Open("Vector3", kNTupleFileName);
85
86 // Display the schema of the ntuple
87 ntuple->PrintInfo();
88
89 // Display information about the storage layout of the data
90 ntuple->PrintInfo(ENTupleInfo::kStorageDetails);
91
92 // Display the first entry
93 ntuple->Show(0);
94
95 // Collect I/O runtime counters when processing the data set.
96 // Maintaining the counters comes with a small performance overhead, so it has to be explicitly enabled
97 ntuple->EnableMetrics();
98
99 // Plot the y components of vector3
100 TCanvas *c1 = new TCanvas("c1","RNTuple Demo", 10, 10, 600, 800);
101 c1->Divide(1,2);
102 c1->cd(1);
103 TH1F h1("x", "x component of Vector3", 100, -3, 3);
104 {
105 /// We enclose viewX in a scope in order to indicate to the RNTuple when we are not
106 /// anymore interested in v3.fX
107 auto viewX = ntuple->GetView<double>("v3.fX");
108 for (auto i : ntuple->GetEntryRange()) {
109 h1.Fill(viewX(i));
110 }
111 }
112 h1.DrawCopy();
113
114 c1->cd(2);
115 TH1F h2("y", "y component of Vector3", 100, -5, 20);
116 auto viewY = ntuple->GetView<double>("v3.fY");
117 for (auto i : ntuple->GetEntryRange()) {
118 h2.Fill(viewY(i));
119 }
120 h2.DrawCopy();
121
122 // Display the I/O operation statistics performed by the RNTuple reader
123 ntuple->PrintInfo(ENTupleInfo::kMetrics);
124
125 // We read 2 out of the 3 Vector3 members and thus should have requested approximately 2/3 of the file
126 FileStat_t fileStat;
127 auto retval = gSystem->GetPathInfo(kNTupleFileName, fileStat);
128 assert(retval == 0);
129 float fileSize = static_cast<float>(fileStat.fSize);
130 float nbytesRead = ntuple->GetMetrics().GetCounter("RNTupleReader.RPageSourceFile.szReadPayload")->GetValueAsInt() +
131 ntuple->GetMetrics().GetCounter("RNTupleReader.RPageSourceFile.szReadOverhead")->GetValueAsInt();
132
133 std::cout << "File size: " << fileSize / 1024. / 1024. << " MiB" << std::endl;
134 std::cout << "Read from file: " << nbytesRead / 1024. / 1024. << " MiB" << std::endl;
135 std::cout << "Ratio: " << nbytesRead / fileSize << std::endl;
136}
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:491
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
The RNTupleModel encapulates the schema of an ntuple.
An RNTuple that is used to read data from storage.
Definition RNTuple.hxx:101
Common user-tunable settings for storing ntuples.
An RNTuple that gets filled with entries (data) and writes them to storage.
Definition RNTuple.hxx:358
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:577
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3345
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition TH1.cxx:3114
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:1385
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:59
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:56