Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ntpl006_friends.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ntuple
3/// \notebook
4/// Work with befriended RNTuples. Adapted from tree3.C
5///
6/// \macro_image
7/// \macro_code
8///
9/// \date April 2020
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 <TMath.h>
22#include <TRandom.h>
23
24#include <vector>
25
26constexpr char const* kNTupleMainFileName = "ntpl006_data.root";
27constexpr char const* kNTupleFriendFileName = "ntpl006_reco.root";
28
29using RNTupleModel = ROOT::Experimental::RNTupleModel;
30using RNTupleOpenSpec = ROOT::Experimental::RNTupleOpenSpec;
31using RNTupleReader = ROOT::Experimental::RNTupleReader;
32using RNTupleWriter = ROOT::Experimental::RNTupleWriter;
33
34void Generate()
35{
36 constexpr int kMaxTrack = 500;
37
38 auto modelData = RNTupleModel::Create();
39 auto fldPx = modelData->MakeField<std::vector<float>>("px");
40 auto fldPy = modelData->MakeField<std::vector<float>>("py");
41 auto fldPz = modelData->MakeField<std::vector<float>>("pz");
42
43 auto modelFriend = RNTupleModel::Create();
44 auto fldPt = modelFriend->MakeField<std::vector<float>>("pt");
45
46 auto ntupleData = RNTupleWriter::Recreate(std::move(modelData), "data", kNTupleMainFileName);
47 auto ntupleReco = RNTupleWriter::Recreate(std::move(modelFriend), "reco", kNTupleFriendFileName);
48
49 for (int i=0; i < 1000; i++) {
50 int ntracks = gRandom->Rndm() * (kMaxTrack - 1);
51 for (int n = 0; n < ntracks; n++) {
52 fldPx->emplace_back(gRandom->Gaus( 0, 1));
53 fldPy->emplace_back(gRandom->Gaus( 0, 2));
54 fldPz->emplace_back(gRandom->Gaus(10, 5));
55 fldPt->emplace_back(TMath::Sqrt(fldPx->at(n) * fldPx->at(n) + fldPy->at(n) * fldPy->at(n)));
56 }
57 ntupleData->Fill();
58 ntupleReco->Fill();
59
60 fldPx->clear();
61 fldPy->clear();
62 fldPz->clear();
63 fldPt->clear();
64 }
65}
66
67
68void ntpl006_friends()
69{
70 Generate();
71
72 std::vector<RNTupleOpenSpec> friends{{"data", kNTupleMainFileName}, {"reco", kNTupleFriendFileName}};
73 auto ntuple = RNTupleReader::OpenFriends(friends);
74
75 auto c = new TCanvas("c", "", 200, 10, 700, 500);
76 TH1F h("h", "pz {pt > 3.}", 100, -15, 35);
77
78 auto viewPz = ntuple->GetView<float>("data.pz._0");
79 auto viewPt = ntuple->GetView<float>("reco.pt._0");
80 for (auto i : viewPt.GetFieldRange()) {
81 if (viewPt(i) > 3.)
82 h.Fill(viewPz(i));
83 }
84
85 h.SetFillColor(48);
86 h.DrawCopy();
87}
#define c(i)
Definition RSha256.hxx:101
#define h(i)
Definition RSha256.hxx:106
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
The RNTupleModel encapulates the schema of an ntuple.
An RNTuple that is used to read data from storage.
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 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
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
const Int_t n
Definition legend1.C:16
RNTupleGlobalRange GetFieldRange(const RFieldBase &field, const RPageSource &pageSource)
Helper to get the iteration space of the given field that needs to be connected to the given page sou...
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:662
Used to specify the underlying RNTuples in RNTupleProcessor and RNTupleReader::OpenFriends()