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, interface, and data format is still subject to changes.
14// Do not use for real data!
15
16// Until C++ runtime modules are universally used, we explicitly load the ntuple library. Otherwise
17// triggering autoloading from the use of templated types would require an exhaustive enumeration
18// of "all" template instances in the LinkDef file.
19R__LOAD_LIBRARY(ROOTNTuple)
20
21#include <ROOT/RNTuple.hxx>
22#include <ROOT/RNTupleModel.hxx>
23
24#include <TCanvas.h>
25#include <TH1F.h>
26#include <TMath.h>
27#include <TRandom.h>
28
29#include <vector>
30
31constexpr char const* kNTupleMainFileName = "ntpl006_data.root";
32constexpr char const* kNTupleFriendFileName = "ntpl006_reco.root";
33
34using RNTupleModel = ROOT::Experimental::RNTupleModel;
35using RNTupleReader = ROOT::Experimental::RNTupleReader;
36using RNTupleWriter = ROOT::Experimental::RNTupleWriter;
37
38void Generate()
39{
40 constexpr int kMaxTrack = 500;
41
42 auto modelData = RNTupleModel::Create();
43 auto fldPx = modelData->MakeField<std::vector<float>>("px");
44 auto fldPy = modelData->MakeField<std::vector<float>>("py");
45 auto fldPz = modelData->MakeField<std::vector<float>>("pz");
46
47 auto modelFriend = RNTupleModel::Create();
48 auto fldPt = modelFriend->MakeField<std::vector<float>>("pt");
49
50 auto ntupleData = RNTupleWriter::Recreate(std::move(modelData), "data", kNTupleMainFileName);
51 auto ntupleReco = RNTupleWriter::Recreate(std::move(modelFriend), "reco", kNTupleFriendFileName);
52
53 for (int i=0; i < 1000; i++) {
54 int ntracks = gRandom->Rndm() * (kMaxTrack - 1);
55 for (int n = 0; n < ntracks; n++) {
56 fldPx->emplace_back(gRandom->Gaus( 0, 1));
57 fldPy->emplace_back(gRandom->Gaus( 0, 2));
58 fldPz->emplace_back(gRandom->Gaus(10, 5));
59 fldPt->emplace_back(TMath::Sqrt(fldPx->at(n) * fldPx->at(n) + fldPy->at(n) * fldPy->at(n)));
60 }
61 ntupleData->Fill();
62 ntupleReco->Fill();
63
64 fldPx->clear();
65 fldPy->clear();
66 fldPz->clear();
67 fldPt->clear();
68 }
69}
70
71
72void ntpl006_friends()
73{
74 Generate();
75
76 std::vector<RNTupleReader::ROpenSpec> friends{
77 {"data", kNTupleMainFileName},
78 {"reco", kNTupleFriendFileName} };
79 auto ntuple = RNTupleReader::OpenFriends(friends);
80
81 auto c = new TCanvas("c", "", 200, 10, 700, 500);
82 TH1F h("h", "pz {pt > 3.}", 100, -15, 35);
83
84 auto viewPz = ntuple->GetView<float>("data.pz._0");
85 auto viewPt = ntuple->GetView<float>("reco.pt._0");
86 for (auto i : viewPt.GetFieldRange()) {
87 if (viewPt(i) > 3.)
88 h.Fill(viewPz(i));
89 }
90
91 h.SetFillColor(48);
92 h.DrawCopy();
93}
#define c(i)
Definition RSha256.hxx:101
#define h(i)
Definition RSha256.hxx:106
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:472
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.
Definition RNTuple.hxx:102
An RNTuple that gets filled with entries (data) and writes them to storage.
Definition RNTuple.hxx:351
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:575
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
virtual Double_t Rndm()
Machine independent random number generator.
Definition TRandom.cxx:552
const Int_t n
Definition legend1.C:16
Double_t Sqrt(Double_t x)
Definition TMath.h:641