Logo ROOT  
Reference Guide
 
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#include <ROOT/RNTupleModel.hxx>
17
18#include <Compression.h>
19#include <TCanvas.h>
20#include <TH1.h>
21#include <TRandom.h>
22#include <TSystem.h>
23
24#include <cassert>
25
26constexpr char const* kNTupleFileName = "ntpl005_introspection.root";
27
28// Store entries of type Vector3 in the ntuple
29class Vector3 {
30private:
31 double fX = 0;
32 double fY = 0;
33 double fZ = 0;
34
35public:
36 double x() const { return fX; }
37 double y() const { return fY; }
38 double z() const { return fZ; }
39
40 void SetXYZ(double x, double y, double z) {
41 fX = x;
42 fY = y;
43 fZ = z;
44 }
45};
46
47
48void Generate()
49{
50 auto model = ROOT::RNTupleModel::Create();
51 auto fldVector3 = model->MakeField<Vector3>("v3");
52
53 // Explicitly enforce a certain compression algorithm
56
57 auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), "Vector3", kNTupleFileName, options);
58 TRandom r;
59 for (unsigned int i = 0; i < 500000; ++i) {
60 fldVector3->SetXYZ(r.Gaus(0,1), r.Landau(0,1), r.Gaus(100,10));
61 writer->Fill();
62 }
63}
64
65
67 Generate();
68
70
71 // Display the schema of the ntuple
72 reader->PrintInfo();
73
74 // Display information about the storage layout of the data
76
77 // Display the first entry
78 reader->Show(0);
79
80 // Collect I/O runtime counters when processing the data set.
81 // Maintaining the counters comes with a small performance overhead, so it has to be explicitly enabled
82 reader->EnableMetrics();
83
84 // Plot the y components of vector3
85 TCanvas *c1 = new TCanvas("c1","RNTuple Demo", 10, 10, 600, 800);
86 c1->Divide(1,2);
87 c1->cd(1);
88 TH1F h1("x", "x component of Vector3", 100, -3, 3);
89 {
90 /// We enclose viewX in a scope in order to indicate to the RNTuple when we are not
91 /// anymore interested in v3.fX
92 auto viewX = reader->GetView<double>("v3.fX");
93 for (auto i : reader->GetEntryRange()) {
94 h1.Fill(viewX(i));
95 }
96 }
97 h1.DrawCopy();
98
99 c1->cd(2);
100 h2("y", "y component of Vector3", 100, -5, 20);
101 auto viewY = reader->GetView<double>("v3.fY");
102 for (auto i : reader->GetEntryRange()) {
103 viewY(i));
104 }
105 h2.DrawCopy();
106
107 // Display the I/O operation statistics performed by the RNTuple reader
109
110 // We read 2 out of the 3 Vector3 members and thus should have requested approximately 2/3 of the file
113 assert(retval == 0);
114 float fileSize = static_cast<float>(fileStat.fSize);
115 float nbytesRead = reader->GetMetrics().GetCounter("RNTupleReader.RPageSourceFile.szReadPayload")->GetValueAsInt() +
116 reader->GetMetrics().GetCounter("RNTupleReader.RPageSourceFile.szReadOverhead")->GetValueAsInt();
117
118 std::cout << "File size: " << fileSize / 1024. / 1024. << " MiB" << std::endl;
119 std::cout << "Read from file: " << nbytesRead / 1024. / 1024. << " MiB" << std::endl;
120 std::cout << "Ratio: " << nbytesRead / fileSize << std::endl;
121}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:572
static std::unique_ptr< RNTupleModel > Create()
static std::unique_ptr< RNTupleReader > Open(std::string_view ntupleName, std::string_view storage, const ROOT::RNTupleReadOptions &options=ROOT::RNTupleReadOptions())
Open an RNTuple for reading.
Common user-tunable settings for storing RNTuples.
void SetCompression(std::uint32_t val)
static std::unique_ptr< RNTupleWriter > Recreate(std::unique_ptr< ROOT::RNTupleModel > model, std::string_view ntupleName, std::string_view storage, const ROOT::RNTupleWriteOptions &options=ROOT::RNTupleWriteOptions())
Throws an exception if the model is null.
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:877
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3316
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition TH1.cxx:3085
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:1410
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
TH1F * h1
Definition legend1.C:5
@ kUseGeneralPurpose
Use the new recommended general-purpose setting; it is a best trade-off between compression ratio/dec...
Definition Compression.h:58