Logo ROOT   6.14/05
Reference Guide
df015_LazyDataSource.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_dataframe
3 /// \notebook
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 ///
16 /// \date February 2018
17 /// \author Danilo Piparo
18 
19 int df015_LazyDataSource()
20 {
21  using namespace ROOT::RDF;
22 
23  // Let's first create a RDF that will read from the CSV file.
24  // See the tutorial relative to CSV data sources for more details!
25  auto fileName = "df014_CsvDataSource_MuRun2010B.csv";
26  auto csv_tdf = MakeCsvDataFrame(fileName);
27 
28  // Now we take out four columns: px and py of the first muon in the muon pair
29  std::string px1Name = "px1";
30  auto px1 = csv_tdf.Take<double>(px1Name);
31  std::string py1Name = "py1";
32  auto py1 = csv_tdf.Take<double>(py1Name);
33 
34  // Now we create a new dataframe built on top of the columns above. Note that up to now, no event loop
35  // has been carried out!
36  auto tdf = MakeLazyDataFrame(std::make_pair(px1Name, px1), std::make_pair(py1Name, py1));
37 
38  // We build a histogram of the transverse momentum the muons.
39  auto ptFormula = [](double px, double py) { return sqrt(px * px + py * py); };
40  auto pt_h = tdf.Define("pt", ptFormula, {"px1", "py1"})
41  .Histo1D<double>({"pt", "Muon p_{T};p_{T} [GeV/c];", 128, 0, 128}, "pt");
42 
43  auto can = new TCanvas();
44  can->SetLogy();
45  pt_h->DrawCopy();
46 
47  return 0;
48 }
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:439
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