Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ntpl015_processor_join.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ntuple
3/// \notebook
4/// Demonstrate the RNTupleProcessor for horizontal compositions (joins) of RNTuples
5///
6/// \macro_image
7/// \macro_code
8///
9/// \date November 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
29const std::string kMainNTupleName = "mainNTuple";
30const std::string kMainNTuplePath = "main_ntuple.root";
31const std::string kAuxNTupleName = "auxNTuple";
32const std::string kAuxNTuplePath = "aux_ntuple.root";
33
34// Number of events to generate for the auxiliary ntuple. The main ntuple will have a fifth of this number.
35constexpr int kNEvents = 10000;
36
37void WriteMain(std::string_view ntupleName, std::string_view ntupleFileName)
38{
39 auto model = RNTupleModel::Create();
40
41 auto fldI = model->MakeField<std::uint32_t>("i");
42 auto fldVpx = model->MakeField<float>("vpx");
43
44 auto ntuple = RNTupleWriter::Recreate(std::move(model), ntupleName, ntupleFileName);
45
46 // The main ntuple only contains a subset of the entries present in the auxiliary ntuple.
47 for (int i = 0; i < kNEvents; i += 5) {
48 *fldI = i;
49 *fldVpx = gRandom->Gaus();
50
51 ntuple->Fill();
52 }
53
54 std::cout << "Wrote " << ntuple->GetNEntries() << " to the main RNTuple" << std::endl;
55}
56
57void WriteAux(std::string_view ntupleName, std::string_view ntupleFileName)
58{
59 auto model = RNTupleModel::Create();
60
61 auto fldI = model->MakeField<std::uint32_t>("i");
62 auto fldVpy = model->MakeField<float>("vpy");
63
64 auto ntuple = RNTupleWriter::Recreate(std::move(model), ntupleName, ntupleFileName);
65
66 for (int i = 0; i < kNEvents; ++i) {
67 *fldI = i;
68 *fldVpy = gRandom->Gaus();
69
70 ntuple->Fill();
71 }
72
73 std::cout << "Wrote " << ntuple->GetNEntries() << " to the auxiliary RNTuple" << std::endl;
74}
75
76void Read()
77{
78 auto c = new TCanvas("c", "RNTupleJoinProcessor Example", 200, 10, 700, 500);
79 TH1F hPy("h", "This is the px + py distribution", 100, -4, 4);
80 hPy.SetFillColor(48);
81
82 // The ntuples to generate and subsequently process. The first ntuple is the main ntuple and will be used to drive
83 // the processor loop. All subsequent ntuples are auxiliary and will be joined with the entries from the main ntuple.
84 std::vector<RNTupleOpenSpec> ntuples = {{kMainNTupleName, kMainNTuplePath}, {kAuxNTupleName, kAuxNTuplePath}};
85
86 // We specify field "i" as the join field. This field, which should be present in all ntuples specified is used to
87 // identify which entries belong together. Multiple join fields can be specified, in which case the combination of
88 // field values is used. It is possible to specify up to 4 join fields. Providing an empty list of join fields
89 // signals to the processor that all entries are aligned.
90 auto processor = RNTupleProcessor::CreateJoin(ntuples, {"i"});
91
92 float px, py;
93 for (const auto &entry : *processor) {
94 // Fields from the main ntuple are accessed by their original name.
95 px = *entry.GetPtr<float>("vpx");
96 // Fields from auxiliary ntuples are accessed by prepending the name of the auxiliary ntuple.
97 py = *entry.GetPtr<float>(kAuxNTupleName + ".vpy");
98
99 hPy.Fill(px + py);
100 }
101
102 std::cout << "Processed a total of " << processor->GetNEntriesProcessed() << " entries" << std::endl;
103
104 hPy.DrawCopy();
105}
106
107void ntpl015_processor_join()
108{
109 WriteMain(kMainNTupleName, kMainNTuplePath);
110 WriteAux(kAuxNTupleName, kAuxNTuplePath);
111
112 Read();
113}
#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:623
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:275
Used to specify the underlying RNTuples in RNTupleProcessor.