Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ntpl012_processor.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ntuple
3/// \notebook
4/// Demonstrate the RNTupleProcessor using multiple RNTuples
5///
6/// \macro_image
7/// \macro_code
8///
9/// \date April 2024
10/// \author The ROOT Team
11
12// NOTE: The RNTuple classes are experimental at this point.
13// Functionality and interface are still subject to changes.
14
15#include <ROOT/RNTupleModel.hxx>
18
19#include <TCanvas.h>
20#include <TH1F.h>
21#include <TRandom.h>
22
23// Import classes from the `Experimental` namespace for the time being.
28
29// Number of events to generate for each ntuple.
30constexpr int kNEvents = 10000;
31
32void Write(std::string_view ntupleName, std::string_view ntupleFileName)
33{
34 auto model = RNTupleModel::Create();
35
36 auto fldVpx = model->MakeField<std::vector<float>>("vpx");
37 auto fldVpy = model->MakeField<std::vector<float>>("vpy");
38 auto fldVpz = model->MakeField<std::vector<float>>("vpz");
39 auto fldN = model->MakeField<std::uint64_t>("vn");
40
41 auto ntuple = RNTupleWriter::Recreate(std::move(model), ntupleName, ntupleFileName);
42
43 for (int i = 0; i < kNEvents; ++i) {
44 fldVpx->clear();
45 fldVpy->clear();
46 fldVpz->clear();
47
48 *fldN = gRandom->Integer(15);
49 for (int j = 0; j < *fldN; ++j) {
50 float px, py, pz;
51 gRandom->Rannor(px, py);
52 pz = px * px + py * py;
53
54 fldVpx->emplace_back(px);
55 fldVpy->emplace_back(py);
56 fldVpz->emplace_back(pz);
57 }
58
59 ntuple->Fill();
60 }
61}
62
63void Read(const std::vector<RNTupleOpenSpec> &ntuples)
64{
65 auto c = new TCanvas("c", "RNTupleProcessor Example", 200, 10, 700, 500);
66 TH1F hPx("h", "This is the px distribution", 100, -4, 4);
67 hPx.SetFillColor(48);
68
69 auto model = RNTupleModel::Create();
70 auto ptrPx = model->MakeField<std::vector<float>>("vpx");
71
72 // By passing a model to the processor, we can use the pointers to field values created upon model creation during
73 // processing. When no model is provided, a default model is created based on the first ntuple specified.
74 // Access to the entry values in this case can be achieved through RNTupleProcessor::GetEntry() or through its
75 // iterator.
76 auto processor = RNTupleProcessor::CreateChain(ntuples, std::move(model));
77
78 for (const auto &entry : *processor) {
79 // The RNTupleProcessor provides some additional bookkeeping information. The local entry number is reset each
80 // a new ntuple in the chain is opened for processing.
81 if (processor->GetLocalEntryNumber() == 0) {
82 std::cout << "Processing " << ntuples.at(processor->GetCurrentNTupleNumber()).fNTupleName << " ("
83 << processor->GetNEntriesProcessed() << " total entries processed so far)" << std::endl;
84 }
85
86 // We can use the pointer to the field obtained while creating our model to read the field's data for the current
87 // entry.
88 for (auto x : *ptrPx) {
89 hPx.Fill(x);
90 }
91 }
92
93 hPx.DrawCopy();
94}
95
96void ntpl012_processor()
97{
98 // The ntuples to generate and subsequently process. The model of the first ntuple will be used to construct the
99 // entry used by the processor.
100 std::vector<RNTupleOpenSpec> ntuples = {
101 {"ntuple1", "ntuple1.root"}, {"ntuple2", "ntuple2.root"}, {"ntuple3", "ntuple3.root"}};
102
103 for (const auto &ntuple : ntuples) {
104 Write(ntuple.fNTupleName, ntuple.fStorage);
105 }
106
107 Read(ntuples);
108}
#define c(i)
Definition RSha256.hxx:101
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
The RNTupleModel encapulates the schema of an ntuple.
Interface for iterating over entries of RNTuples and vertically concatenated RNTuples (chains).
An RNTuple that gets filled with entries (data) and writes them to storage.
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:622
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition TRandom.cxx:507
virtual UInt_t Integer(UInt_t imax)
Returns a random integer uniformly distributed on the interval [ 0, imax-1 ].
Definition TRandom.cxx:361
Double_t x[n]
Definition legend1.C:17
Used to specify the underlying RNTuples in RNTupleProcessor and RNTupleReader::OpenFriends()