Logo ROOT   6.16/01
Reference Guide
df015_LazyDataSource.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_dataframe
3/// \notebook -draw
4/// This tutorial illustrates how to take advantage of a *lazy data source*
5/// creating a data frame from columns of one or multiple parent dataframe(s),
6/// delaying the creation of the columns to the actual usage of the daughter
7/// data frame.
8/// Dataset Reference:
9/// McCauley, T. (2014). Dimuon event information derived from the Run2010B
10/// public Mu dataset. CERN Open Data Portal.
11/// DOI: [10.7483/OPENDATA.CMS.CB8H.MFFA](http://opendata.cern.ch/record/700).
12/// From the ROOT website: https://root.cern.ch/files/tutorials/tdf014_CsvDataSource_MuRun2010B.csv
13///
14/// \macro_code
15/// \macro_image
16///
17/// \date February 2018
18/// \author Danilo Piparo
19
20int df015_LazyDataSource()
21{
22 using namespace ROOT::RDF;
23
24 // Let's first create a RDF that will read from the CSV file.
25 // See the tutorial relative to CSV data sources for more details!
26 auto fileName = "df014_CsvDataSource_MuRun2010B.csv";
27 auto csv_tdf = MakeCsvDataFrame(fileName);
28
29 // Now we take out four columns: px and py of the first muon in the muon pair
30 std::string px1Name = "px1";
31 auto px1 = csv_tdf.Take<double>(px1Name);
32 std::string py1Name = "py1";
33 auto py1 = csv_tdf.Take<double>(py1Name);
34
35 // Now we create a new dataframe built on top of the columns above. Note that up to now, no event loop
36 // has been carried out!
37 auto tdf = MakeLazyDataFrame(std::make_pair(px1Name, px1), std::make_pair(py1Name, py1));
38
39 // We build a histogram of the transverse momentum the muons.
40 auto ptFormula = [](double px, double py) { return sqrt(px * px + py * py); };
41 auto pt_h = tdf.Define("pt", ptFormula, {"px1", "py1"})
42 .Histo1D<double>({"pt", "Muon p_{T};p_{T} [GeV/c];", 128, 0, 128}, "pt");
43
44 auto can = new TCanvas();
45 can->SetLogy();
46 pt_h->DrawCopy();
47
48 return 0;
49}
double sqrt(double)
The Canvas class.
Definition: TCanvas.h:31
RDataFrame MakeLazyDataFrame(std::pair< std::string, RResultPtr< std::vector< ColumnTypes > > > &&... colNameProxyPairs)
Factory method to create a Lazy RDataFrame.
Definition: RLazyDS.hxx:28
RDataFrame MakeCsvDataFrame(std::string_view fileName, bool readHeaders=true, char delimiter=',', Long64_t linesChunkSize=-1LL)
Factory method to create a CSV RDataFrame.
Definition: RCsvDS.cxx:454