ROOT
master
Reference Guide
Loading...
Searching...
No Matches
df014_CSVDataSource.C
Go to the documentation of this file.
1
/// \file
2
/// \ingroup tutorial_dataframe
3
/// \notebook -draw
4
/// Process a CSV file with RDataFrame and the CSV data source.
5
///
6
/// This tutorial illustrates how use the RDataFrame in combination with a
7
/// RDataSource. In this case we use a RCsvDS. This data source allows to read
8
/// a CSV file from a RDataFrame.
9
/// As a result of running this tutorial, we will produce plots of the dimuon
10
/// spectrum starting from a subset of the CMS collision events of Run2010B.
11
/// Dataset Reference:
12
/// McCauley, T. (2014). Dimuon event information derived from the Run2010B
13
/// public Mu dataset. CERN Open Data Portal.
14
/// DOI: [10.7483/OPENDATA.CMS.CB8H.MFFA](http://opendata.cern.ch/record/700).
15
///
16
/// \macro_code
17
/// \macro_image
18
///
19
/// \date October 2017
20
/// \author Enric Tejedor (CERN)
21
22
int
df014_CSVDataSource()
23
{
24
// Let's first create a RDF that will read from the CSV file.
25
// The types of the columns will be automatically inferred.
26
auto
fileUrl
=
"http://root.cern/files/tutorials/df014_CsvDataSource_MuRun2010B.csv"
;
27
auto
df =
ROOT::RDF::FromCSV
(
fileUrl);
28
29
// Now we will apply a first filter based on two columns of the CSV,
30
// and we will define a new column that will contain the invariant mass.
31
// Note how the new invariant mass column is defined from several other
32
// columns that already existed in the CSV file.
33
auto
filteredEvents
=
34
df.Filter(
"Q1 * Q2 == -1"
)
35
.Define(
"m"
,
"sqrt(pow(E1 + E2, 2) - (pow(px1 + px2, 2) + pow(py1 + py2, 2) + pow(pz1 + pz2, 2)))"
);
36
37
// Next we create a histogram to hold the invariant mass values and we draw it.
38
auto
invMass
=
39
filteredEvents
.Histo1D({
"invMass"
,
"CMS Opendata: #mu#mu mass;#mu#mu mass [GeV];Events"
, 512, 2, 110},
"m"
);
40
41
auto
c
=
new
TCanvas
();
42
c
->SetLogx();
43
c
->SetLogy();
44
invMass
->DrawClone();
45
46
// We will now produce a plot also for the J/Psi particle. We will plot
47
// on the same canvas the full spectrum and the zoom in on the J/psi particle.
48
// First we will create the full spectrum histogram from the invariant mass
49
// column, using a different histogram model than before.
50
auto
fullSpectrum
=
51
filteredEvents
.Histo1D({
"Spectrum"
,
"Subset of CMS Run 2010B;#mu#mu mass [GeV];Events"
, 1024, 2, 110},
"m"
);
52
53
// Next we will create the histogram for the J/psi particle, applying first
54
// the corresponding cut.
55
double
jpsiLow
= 2.95;
56
double
jpsiHigh
= 3.25;
57
auto
jpsiCut
= [
jpsiLow
,
jpsiHigh
](
double
m
) {
return
m < jpsiHigh && m >
jpsiLow
; };
58
auto
jpsi
=
59
filteredEvents
.Filter(
jpsiCut
, {
"m"
})
60
.Histo1D({
"jpsi"
,
"Subset of CMS Run 2010B: J/#psi window;#mu#mu mass [GeV];Events"
, 128,
jpsiLow
,
jpsiHigh
},
61
"m"
);
62
63
// Finally we draw the two histograms side by side.
64
auto
dualCanvas
=
new
TCanvas
(
"DualCanvas"
,
"DualCanvas"
, 800, 512);
65
dualCanvas
->Divide(2, 1);
66
auto
leftPad
=
dualCanvas
->cd(1);
67
leftPad
->SetLogx();
68
leftPad
->SetLogy();
69
fullSpectrum
->DrawClone(
"Hist"
);
70
dualCanvas
->cd(2);
71
jpsi
->SetMarkerStyle(20);
72
jpsi
->DrawClone(
"HistP"
);
73
74
return
0;
75
}
c
#define c(i)
Definition
RSha256.hxx:101
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:360
ROOT::Detail::TRangeCast
Definition
TCollection.h:313
TCanvas
The Canvas class.
Definition
TCanvas.h:23
ROOT::RDF::FromCSV
RDataFrame FromCSV(std::string_view fileName, const RCsvDS::ROptions &options)
Factory method to create a CSV RDataFrame.
Definition
RCsvDS.cxx:644
df014_CSVDataSource
Definition
df014_CSVDataSource.py:1
df101_h1Analysis.df
df
Definition
df101_h1Analysis.py:115
m
TMarker m
Definition
textangle.C:8
tutorials
analysis
dataframe
df014_CSVDataSource.C
ROOTmaster - Reference Guide Generated on Sat Mar 21 2026 14:08:23 (GVA Time) using Doxygen 1.10.0