Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
df014_CSVDataSource.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_dataframe
3 ## \notebook -draw
4 ## This tutorial illustrates how use the RDataFrame in combination with a
5 ## RDataSource. In this case we use a TCsvDS. This data source allows to read
6 ## a CSV file from a RDataFrame.
7 ## As a result of running this tutorial, we will produce plots of the dimuon
8 ## spectrum starting from a subset of the CMS collision events of Run2010B.
9 ## Dataset Reference:
10 ## McCauley, T. (2014). Dimuon event information derived from the Run2010B
11 ## public Mu dataset. CERN Open Data Portal.
12 ## DOI: [10.7483/OPENDATA.CMS.CB8H.MFFA](http://opendata.cern.ch/record/700).
13 ##
14 ## \macro_code
15 ## \macro_image
16 ##
17 ## \date October 2017
18 ## \author Enric Tejedor
19 
20 import ROOT
21 import os
22 
23 # Let's first create a RDF that will read from the CSV file.
24 # The types of the columns will be automatically inferred.
25 fileNameUrl = "http://root.cern.ch/files/tutorials/df014_CsvDataSource_MuRun2010B.csv"
26 fileName = "df014_CsvDataSource_MuRun2010B_py.csv"
27 if not os.path.isfile(fileName):
28  ROOT.TFile.Cp(fileNameUrl, fileName)
29 
30 MakeCsvDataFrame = ROOT.ROOT.RDF.MakeCsvDataFrame
31 tdf = MakeCsvDataFrame(fileName)
32 
33 # Now we will apply a first filter based on two columns of the CSV,
34 # and we will define a new column that will contain the invariant mass.
35 # Note how the new invariant mass column is defined from several other
36 # columns that already existed in the CSV file.
37 filteredEvents = tdf.Filter("Q1 * Q2 == -1") \
38  .Define("m", "sqrt(pow(E1 + E2, 2) - (pow(px1 + px2, 2) + pow(py1 + py2, 2) + pow(pz1 + pz2, 2)))")
39 
40 # Next we create a histogram to hold the invariant mass values and we draw it.
41 invMass = filteredEvents.Histo1D(("invMass", "CMS Opendata: #mu#mu mass;#mu#mu mass [GeV];Events", 512, 2, 110), "m")
42 
43 c = ROOT.TCanvas()
44 c.SetLogx()
45 c.SetLogy()
46 invMass.Draw()
47 
48 # We will now produce a plot also for the J/Psi particle. We will plot
49 # on the same canvas the full spectrum and the zoom in the J/psi particle.
50 # First we will create the full spectrum histogram from the invariant mass
51 # column, using a different histogram model than before.
52 fullSpectrum = filteredEvents.Histo1D(("Spectrum", "Subset of CMS Run 2010B;#mu#mu mass [GeV];Events", 1024, 2, 110), "m")
53 
54 # Next we will create the histogram for the J/psi particle, applying first
55 # the corresponding cut.
56 jpsiLow = 2.95
57 jpsiHigh = 3.25
58 jpsiCut = 'm < %s && m > %s' % (jpsiHigh, jpsiLow)
59 jpsi = filteredEvents.Filter(jpsiCut) \
60  .Histo1D(("jpsi", "Subset of CMS Run 2010B: J/#psi window;#mu#mu mass [GeV];Events", 128, jpsiLow, jpsiHigh), "m")
61 
62 # Finally we draw the two histograms side by side.
63 dualCanvas = ROOT.TCanvas("DualCanvas", "DualCanvas", 800, 512)
64 dualCanvas.Divide(2, 1)
65 leftPad = dualCanvas.cd(1)
66 leftPad.SetLogx()
67 leftPad.SetLogy()
68 fullSpectrum.Draw("Hist")
69 dualCanvas.cd(2)
70 jpsi.Draw("HistP")