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 RNTupleProcessor and related 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.
26
27const std::string kMainNTupleName = "mainNTuple";
28const std::string kMainNTuplePath = "main_ntuple.root";
29const std::string kAuxNTupleName = "auxNTuple";
30const std::string kAuxNTuplePath = "aux_ntuple.root";
31
32// Number of events to generate for the auxiliary ntuple. The main ntuple will have a fifth of this number.
33constexpr int kNEvents = 10000;
34
35void WriteMain(std::string_view ntupleName, std::string_view ntupleFileName)
36{
37 auto model = ROOT::RNTupleModel::Create();
38
39 auto fldI = model->MakeField<std::uint32_t>("i");
40 auto fldVpx = model->MakeField<float>("vpx");
41
43
44 // The main ntuple only contains a subset of the entries present in the auxiliary ntuple.
45 for (int i = 0; i < kNEvents; i += 5) {
46 *fldI = i;
47 *fldVpx = gRandom->Gaus();
48
49 writer->Fill();
50 }
51
52 std::cout << "Wrote " << writer->GetNEntries() << " to the main RNTuple" << std::endl;
53}
54
55void WriteAux(std::string_view ntupleName, std::string_view ntupleFileName)
56{
57 auto model = ROOT::RNTupleModel::Create();
58
59 auto fldI = model->MakeField<std::uint32_t>("i");
60 auto fldVpy = model->MakeField<float>("vpy");
61
63
64 for (int i = 0; i < kNEvents; ++i) {
65 *fldI = i;
66 *fldVpy = gRandom->Gaus();
67
68 writer->Fill();
69 }
70
71 std::cout << "Wrote " << writer->GetNEntries() << " to the auxiliary RNTuple" << std::endl;
72}
73
74void Read()
75{
76 auto c = new TCanvas("c", "RNTupleJoinProcessor Example", 200, 10, 700, 500);
77 TH1F hPy("h", "This is the px + py distribution", 100, -4, 4);
78 hPy.SetFillColor(48);
79
80 // The first specified ntuple is the main ntuple and will be used to drive the processor loop. The subsequent
81 // list of ntuples (in this case, only one) are auxiliary and will be joined with the entries from the main ntuple.
82 // We specify field "i" as the join field. This field, which should be present in all ntuples specified is used to
83 // identify which entries belong together. Multiple join fields can be specified, in which case the combination of
84 // field values is used. It is possible to specify up to 4 join fields. Providing an empty list of join fields
85 // signals to the processor that all entries are aligned.
86 auto processor =
87 RNTupleProcessor::CreateJoin({kMainNTupleName, kMainNTuplePath}, {{kAuxNTupleName, kAuxNTuplePath}}, {"i"});
88
89 float px, py;
90 for (const auto &entry : *processor) {
91 // Fields from the main ntuple are accessed by their original name.
92 px = *entry.GetPtr<float>("vpx");
93 // Fields from auxiliary ntuples are accessed by prepending the name of the auxiliary ntuple.
94 py = *entry.GetPtr<float>(kAuxNTupleName + ".vpy");
95
96 hPy.Fill(px + py);
97 }
98
99 std::cout << "Processed a total of " << processor->GetNEntriesProcessed() << " entries" << std::endl;
100
101 hPy.DrawCopy();
102}
103
105{
108
109 Read();
110}
#define c(i)
Definition RSha256.hxx:101
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
Specification of the name and location of an RNTuple, used for creating a new RNTupleProcessor.
Interface for iterating over entries of RNTuples and vertically concatenated RNTuples (chains).
static std::unique_ptr< RNTupleModel > Create()
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 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