Logo ROOT   6.18/05
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 fileNameUrl = "http://root.cern.ch/files/tutorials/df014_CsvDataSource_MuRun2010B.csv";
27 auto fileName = "df015_CsvDataSource_MuRun2010B.csv";
28 TFile::Cp(fileNameUrl, fileName);
29
30 auto csv_rdf = MakeCsvDataFrame(fileName);
31
32 // Now we take out four columns: px and py of the first muon in the muon pair
33 std::string px1Name = "px1";
34 auto px1 = csv_rdf.Take<double>(px1Name);
35 std::string py1Name = "py1";
36 auto py1 = csv_rdf.Take<double>(py1Name);
37
38 // Now we create a new dataframe built on top of the columns above. Note that up to now, no event loop
39 // has been carried out!
40 auto tdf = MakeLazyDataFrame(std::make_pair(px1Name, px1), std::make_pair(py1Name, py1));
41
42 // We build a histogram of the transverse momentum the muons.
43 auto ptFormula = [](double px, double py) { return sqrt(px * px + py * py); };
44 auto pt_h = tdf.Define("pt", ptFormula, {"px1", "py1"})
45 .Histo1D<double>({"pt", "Muon p_{T};p_{T} [GeV/c];", 128, 0, 128}, "pt");
46
47 auto can = new TCanvas();
48 can->SetLogy();
49 pt_h->DrawCopy();
50
51 return 0;
52}
double sqrt(double)
The Canvas class.
Definition: TCanvas.h:31
virtual Bool_t Cp(const char *dst, Bool_t progressbar=kTRUE, UInt_t buffersize=1000000)
Allows to copy this file to the dst URL.
Definition: TFile.cxx:4876
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:462