Logo ROOT   6.18/05
Reference Guide
ntpl004_dimuon.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ntuple
3/// \notebook
4/// Convert CMS open data from a TTree to RNTuple.
5/// This tutorial illustrates data conversion and data processing with RNTuple and RDataFrame. In contrast to the
6/// LHCb open data tutorial, the data model in this tutorial is not tabular but entries have variable lengths vectors
7/// Based on RDataFrame's df102_NanoAODDimuonAnalysis.C
8///
9/// \macro_image
10/// \macro_code
11///
12/// \date April 2019
13/// \author The ROOT Team
14
15// NOTE: The RNTuple classes are experimental at this point.
16// Functionality, interface, and data format is still subject to changes.
17// Do not use for real data!
18
19#include <ROOT/RDataFrame.hxx>
20#include <ROOT/RNTuple.hxx>
21#include <ROOT/RNTupleDS.hxx>
22#include <ROOT/RVec.hxx>
23
24#include <TCanvas.h>
25#include <TH1D.h>
26#include <TLatex.h>
27#include <TStyle.h>
28#include <TSystem.h>
29
30#include <cassert>
31#include <cmath>
32#include <iostream>
33#include <memory>
34#include <string>
35#include <vector>
36#include <utility>
37
38// Import classes from experimental namespace for the time being
39using RNTupleReader = ROOT::Experimental::RNTupleReader;
40using RNTupleWriter = ROOT::Experimental::RNTupleWriter;
41using RNTupleDS = ROOT::Experimental::RNTupleDS;
42
43constexpr char const* kTreeFileName = "http://root.cern.ch/files/NanoAOD_DoubleMuon_CMS2011OpenData.root";
44constexpr char const* kNTupleFileName = "ntpl004_dimuon.root";
45
46
47using ColNames_t = std::vector<std::string>;
48
49// This is a custom action for RDataFrame. It does not support parallelism!
50// This action writes data from an RDataFrame entry into an ntuple. It is templated on the
51// types of the columns to be written and can be used as a generic file format converter.
52template <typename... ColumnTypes_t>
53class RNTupleHelper : public ROOT::Detail::RDF::RActionImpl<RNTupleHelper<ColumnTypes_t...>> {
54public:
55 using Result_t = RNTupleWriter;
56private:
57 using ColumnValues_t = std::tuple<std::shared_ptr<ColumnTypes_t>...>;
58
59 std::string fNTupleName;
60 std::string fRootFile;
61 ColNames_t fColNames;
62 ColumnValues_t fColumnValues;
63 static constexpr const auto fNColumns = std::tuple_size<ColumnValues_t>::value;
64 std::shared_ptr<RNTupleWriter> fNTuple;
65 int fCounter;
66
67 template<std::size_t... S>
68 void InitializeImpl(std::index_sequence<S...>) {
70 // Create the fields and the shared pointers to the connected values
71 std::initializer_list<int> expander{
72 (std::get<S>(fColumnValues) = eventModel->MakeField<ColumnTypes_t>(fColNames[S]), 0)...};
73 fNTuple = std::move(RNTupleWriter::Recreate(std::move(eventModel), fNTupleName, fRootFile));
74 }
75
76 template<std::size_t... S>
77 void ExecImpl(std::index_sequence<S...>, ColumnTypes_t... values) {
78 // For every entry, set the destination of the ntuple's default entry's shared pointers to the given values,
79 // which are provided by RDataFrame
80 std::initializer_list<int> expander{(*std::get<S>(fColumnValues) = values, 0)...};
81 }
82
83public:
84 RNTupleHelper(std::string_view ntupleName, std::string_view rootFile, const ColNames_t& colNames)
85 : fNTupleName(ntupleName), fRootFile(rootFile), fColNames(colNames)
86 {
87 InitializeImpl(std::make_index_sequence<fNColumns>());
88 }
89
90 RNTupleHelper(RNTupleHelper&&) = default;
91 RNTupleHelper(const RNTupleHelper&) = delete;
92 std::shared_ptr<RNTupleWriter> GetResultPtr() const { return fNTuple; }
93
94 void Initialize()
95 {
96 fCounter = 0;
97 }
98
99 void InitTask(TTreeReader *, unsigned int) {}
100
101 /// This is a method executed at every entry
102 void Exec(unsigned int slot, ColumnTypes_t... values)
103 {
104 // Populate the ntuple's fields data locations with the provided values, then write to disk
105 ExecImpl(std::make_index_sequence<fNColumns>(), values...);
106 fNTuple->Fill();
107 if (++fCounter % 100000 == 0)
108 std::cout << "Wrote " << fCounter << " entries" << std::endl;
109 }
110
111 void Finalize()
112 {
113 fNTuple->CommitCluster();
114 }
115
116 std::string GetActionName() { return "RNTuple Writer"; }
117};
118
119
120/// A wrapper for ROOT's InvariantMass function that takes std::vector instead of RVecs
121template <typename T>
122T InvariantMassStdVector(std::vector<T>& pt, std::vector<T>& eta, std::vector<T>& phi, std::vector<T>& mass)
123{
124 assert(pt.size() == 2 && eta.size() == 2 && phi.size() == 2 && mass.size() == 2);
125
126 // We adopt the memory here, no copy
127 ROOT::RVec<float> rvPt(pt);
128 ROOT::RVec<float> rvEta(eta);
129 ROOT::RVec<float> rvPhi(phi);
130 ROOT::RVec<float> rvMass(mass);
131
132 return InvariantMass(rvPt, rvEta, rvPhi, rvMass);
133}
134
135// We use an RDataFrame custom snapshotter to convert between TTree and RNTuple.
136// The snapshotter is templated; we construct the conversion C++ code as a string and hand it over to Cling
137void Convert() {
138 // Use df to list the branch types and names of the input tree
139 ROOT::RDataFrame df("Events", kTreeFileName);
140
141 // Construct the types for the template instantiation and the column names from the dataframe
142 std::string typeList = "<";
143 std::string columnList = "{";
144 auto columnNames = df.GetColumnNames();
145 for (auto name : columnNames) {
146 auto typeName = df.GetColumnType(name);
147 // Skip ULong64_t for the time being, RNTuple support will be added at a later point
148 if (typeName == "ULong64_t") continue;
149 columnList += "\"" + name + "\",";
150 typeList += typeName + ",";
151 }
152 *columnList.rbegin() = '}';
153 *typeList.rbegin() = '>';
154
155 std::string code = "{";
156 // Convert the first 4 million events
157 code += "auto df = std::make_unique<ROOT::RDataFrame>(\"Events\", \"" + std::string(kTreeFileName)
158 + "\")->Range(0, 4000000);";
159 code += "ColNames_t colNames = " + columnList + ";";
160 code += "RNTupleHelper" + typeList + " helper{\"Events\", \"" + std::string(kNTupleFileName) + "\", colNames};";
161 code += "*df.Book" + typeList + "(std::move(helper), colNames);";
162 code += "}";
163
164 gInterpreter->ProcessLine(code.c_str());
165}
166
167
168void ntpl004_dimuon() {
169 // Support for multi-threading comes at a later point, for the time being do not enable
170 // ROOT::EnableImplicitMT();
171
172 if (gSystem->AccessPathName(kNTupleFileName))
173 Convert();
174
175 auto df = ROOT::Experimental::MakeNTupleDataFrame("Events", kNTupleFileName);
176
177 // As of this point, the tutorial is identical to df102_NanoAODDimuonAnalysis except the use of
178 // InvariantMassStdVector instead of InvariantMass
179
180 // For simplicity, select only events with exactly two muons and require opposite charge
181 auto df_2mu = df.Filter("nMuon == 2", "Events with exactly two muons");
182 auto df_os = df_2mu.Filter("Muon_charge[0] != Muon_charge[1]", "Muons with opposite charge");
183
184 // Compute invariant mass of the dimuon system
185 auto df_mass = df_os.Define("Dimuon_mass", InvariantMassStdVector<float>, {"Muon_pt", "Muon_eta", "Muon_phi", "Muon_mass"});
186 auto df_size = df_os.Define("eta_size", "Muon_mass.size()");
187
188 // Make histogram of dimuon mass spectrum
189 auto h = df_mass.Histo1D({"Dimuon_mass", "Dimuon_mass", 30000, 0.25, 300}, "Dimuon_mass");
190
191 // Request cut-flow report
192 auto report = df_mass.Report();
193
194 // Produce plot
196 auto c = new TCanvas("c", "", 800, 700);
197 c->SetLogx(); c->SetLogy();
198
199 h->SetTitle("");
200 h->GetXaxis()->SetTitle("m_{#mu#mu} (GeV)"); h->GetXaxis()->SetTitleSize(0.04);
201 h->GetYaxis()->SetTitle("N_{Events}"); h->GetYaxis()->SetTitleSize(0.04);
202 h->DrawCopy();
203
204 TLatex label; label.SetNDC(true);
205 label.DrawLatex(0.175, 0.740, "#eta");
206 label.DrawLatex(0.205, 0.775, "#rho,#omega");
207 label.DrawLatex(0.270, 0.740, "#phi");
208 label.DrawLatex(0.400, 0.800, "J/#psi");
209 label.DrawLatex(0.415, 0.670, "#psi'");
210 label.DrawLatex(0.485, 0.700, "Y(1,2,3S)");
211 label.DrawLatex(0.755, 0.680, "Z");
212 label.SetTextSize(0.040); label.DrawLatex(0.100, 0.920, "#bf{CMS Open Data}");
213 label.SetTextSize(0.030); label.DrawLatex(0.630, 0.920, "#sqrt{s} = 8 TeV, L_{int} = 11.6 fb^{-1}");
214
215 // Print cut-flow report
216 report->Print();
217}
#define c(i)
Definition: RSha256.hxx:101
#define h(i)
Definition: RSha256.hxx:106
char name[80]
Definition: TGX11.cxx:109
#define gInterpreter
Definition: TInterpreter.h:553
R__EXTERN TStyle * gStyle
Definition: TStyle.h:406
R__EXTERN TSystem * gSystem
Definition: TSystem.h:560
static std::unique_ptr< RNTupleModel > Create()
An RNTuple that is used to read data from storage.
Definition: RNTuple.hxx:94
An RNTuple that gets filled with entries (data) and writes them to storage.
Definition: RNTuple.hxx:170
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTrees,...
Definition: RDataFrame.hxx:42
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition: RVec.hxx:272
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition: TAttText.h:45
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:46
The Canvas class.
Definition: TCanvas.h:31
To draw Mathematical Formula.
Definition: TLatex.h:18
TLatex * DrawLatex(Double_t x, Double_t y, const char *text)
Make a copy of this object with the new parameters And copy object attributes.
Definition: TLatex.cxx:1914
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1444
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1286
virtual void Print(Option_t *option="") const
Dump this text with its attributes.
Definition: TText.cxx:779
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition: TText.cxx:812
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition: TTreeReader.h:44
TPaveText * pt
basic_string_view< char > string_view
RDataFrame MakeNTupleDataFrame(std::string_view ntupleName, std::string_view fileName)
Definition: RNTupleDS.cxx:123
double T(double x)
Definition: ChebyshevPol.h:34
Vector1::Scalar InvariantMass(const Vector1 &v1, const Vector2 &v2)
return the invariant mass of two LorentzVector The only requirement on the LorentzVector is that they...
Definition: VectorUtil.h:215
RooArgSet S(const RooAbsArg &v1)
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176