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 RNTupleProcessor and related 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 kPrimaryNTupleName = "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 primary ntuple will have a fifth of this number.
constexpr int kNEvents = 10000;
void WritePrimary(std::string_view ntupleName, std::string_view ntupleFileName)
{
auto fldI = model->MakeField<std::uint32_t>("i");
auto fldVpx = model->MakeField<float>("vpx");
// The primary ntuple only contains a subset of the entries present in the auxiliary ntuple.
for (int i = 0; i < kNEvents; i += 5) {
*fldI = i;
writer->Fill();
}
std::cout << "Wrote " << writer->GetNEntries() << " to the primary RNTuple" << std::endl;
}
void WriteAux(std::string_view ntupleName, std::string_view ntupleFileName)
{
auto fldI = model->MakeField<std::uint32_t>("i");
auto fldVpy = model->MakeField<float>("vpy");
for (int i = 0; i < kNEvents; ++i) {
*fldI = i;
writer->Fill();
}
std::cout << "Wrote " << writer->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 first specified ntuple is the primary ntuple and will be used to drive the processor loop. The subsequent
// list of ntuples (in this case, only one) are auxiliary and will be joined with the entries from the primary
// ntuple. 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({kPrimaryNTupleName, kMainNTuplePath}, {kAuxNTupleName, kAuxNTuplePath}, {"i"});
// Access to the processor's fields is done by first requesting them through RNTupleProcessor::RequestField(). The
// returned value can be used to read the current entry's value for that particular field. Fields from the primary
// ntuple are requested by their original name.
auto px = processor->RequestField<float>("vpx");
// Fields from auxiliary ntuples are requested by prepending the name of the auxiliary ntuple.
auto py = processor->RequestField<float>(kAuxNTupleName + ".vpy");
// The iterator value is the index of the current entry being processed. In this example, we don't use it.
for (auto _ : *processor) {
hPy.Fill(*px + *py);
}
std::cout << "Processed a total of " << processor->GetNEntriesProcessed() << " entries" << std::endl;
hPy.DrawCopy();
}
{
Read();
}
#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
#define _(A, B)
Definition cfortran.h:108
Specification of the name and location of an RNTuple, used for creating a new RNTupleProcessor.
Interface for iterating over entries of vertically ("chained") and/or horizontally ("joined") combine...
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())
Creates an RNTupleWriter backed by storage, overwriting it if one with the same URI exists.
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
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:274
Date
November 2024
Author
The ROOT Team

Definition in file ntpl015_processor_join.C.