Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ntpl015_processor_join.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Demonstrate the RNTupleProcessor for horizontal compositions (joins) of RNTuples

// NOTE: The RNTuple classes are experimental at this point.
// Functionality and interface are still subject to changes.
#include <TCanvas.h>
#include <TH1F.h>
#include <TRandom.h>
// Import classes from the `Experimental` namespace for the time being.
const std::string kMainNTupleName = "mainNTuple";
const std::string kMainNTuplePath = "main_ntuple.root";
const std::string kAuxNTupleName = "auxNTuple";
const std::string kAuxNTuplePath = "aux_ntuple.root";
// Number of events to generate for the auxiliary ntuple. The main ntuple will have a fifth of this number.
constexpr int kNEvents = 10000;
void WriteMain(std::string_view ntupleName, std::string_view ntupleFileName)
{
auto model = RNTupleModel::Create();
auto fldI = model->MakeField<std::uint32_t>("i");
auto fldVpx = model->MakeField<float>("vpx");
auto ntuple = RNTupleWriter::Recreate(std::move(model), ntupleName, ntupleFileName);
// The main ntuple only contains a subset of the entries present in the auxiliary ntuple.
for (int i = 0; i < kNEvents; i += 5) {
*fldI = i;
*fldVpx = gRandom->Gaus();
ntuple->Fill();
}
std::cout << "Wrote " << ntuple->GetNEntries() << " to the main RNTuple" << std::endl;
}
void WriteAux(std::string_view ntupleName, std::string_view ntupleFileName)
{
auto model = RNTupleModel::Create();
auto fldI = model->MakeField<std::uint32_t>("i");
auto fldVpy = model->MakeField<float>("vpy");
auto ntuple = RNTupleWriter::Recreate(std::move(model), ntupleName, ntupleFileName);
for (int i = 0; i < kNEvents; ++i) {
*fldI = i;
*fldVpy = gRandom->Gaus();
ntuple->Fill();
}
std::cout << "Wrote " << ntuple->GetNEntries() << " to the auxiliary RNTuple" << std::endl;
}
void Read()
{
auto c = new TCanvas("c", "RNTupleJoinProcessor Example", 200, 10, 700, 500);
TH1F hPy("h", "This is the px + py distribution", 100, -4, 4);
hPy.SetFillColor(48);
// The ntuples to generate and subsequently process. The first ntuple is the main ntuple and will be used to drive
// the processor loop. All subsequent ntuples are auxiliary and will be joined with the entries from the main ntuple.
std::vector<RNTupleOpenSpec> ntuples = {{kMainNTupleName, kMainNTuplePath}, {kAuxNTupleName, kAuxNTuplePath}};
// We specify field "i" as the join field. This field, which should be present in all ntuples specified is used to
// identify which entries belong together. Multiple join fields can be specified, in which case the combination of
// field values is used. It is possible to specify up to 4 join fields. Providing an empty list of join fields
// signals to the processor that all entries are aligned.
auto processor = RNTupleProcessor::CreateJoin(ntuples, {"i"});
float px, py;
for (const auto &entry : *processor) {
// Fields from the main ntuple are accessed by their original name.
px = *entry.GetPtr<float>("vpx");
// Fields from auxiliary ntuples are accessed by prepending the name of the auxiliary ntuple.
py = *entry.GetPtr<float>(kAuxNTupleName + ".vpy");
hPy.Fill(px + py);
}
std::cout << "Processed a total of " << processor->GetNEntriesProcessed() << " entries" << std::endl;
hPy.DrawCopy();
}
void ntpl015_processor_join()
{
WriteMain(kMainNTupleName, kMainNTuplePath);
WriteAux(kAuxNTupleName, kAuxNTuplePath);
Read();
}
#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.
Date
November 2024
Author
The ROOT Team

Definition in file ntpl015_processor_join.C.