CMSDAS Pre-Exercise 3: ROOT and python basics

Overview

Teaching: 0 min
Exercises: 60 min
Questions
  • How do I set up ROOT and python on cmslpc?

  • How do I inspect a NanoAOD file?

  • How do I make a quick plot with pyROOT?

Objectives
  • Learn how to use ROOT and python

  • Use ROOT and python to inspect a CMS NanoAOD file

Submit your answers

Work through the Question 3.x boxes on this page, then enter all your answers in the CMSDAS@LPC2027 answer form for Episode 3. You can resubmit at any time; we keep your latest answers. Stuck? Ask on Mattermost.

Python and ROOT

Python and ROOT are two of the most important software tools in HEP. Before starting:

Inspect a NanoAOD file with ROOT

Now let’s look at real CMS data on cmslpc. Log in as in Episode 2.

A fresh cmslpc login has no ROOT and an old python. The LCG releases provide both; run this once per login:

source /cvmfs/sft.cern.ch/lcg/views/LCG_106a/x86_64-el9-gcc14-opt/setup.sh

Open the NanoAOD file you copied in Episode 2 with ROOT’s C++ interpreter (-l skips the slow splash screen; type .q to quit):

cd ~/nobackup/cmsdas
root -l DYJetsToLL_M50_NANOAOD.root

At the ROOT prompt, list the file’s contents:

root [1] _file0->ls()
TFile**		DYJetsToLL_M50_NANOAOD.root
 TFile*		DYJetsToLL_M50_NANOAOD.root
  KEY: TTree	Events;1	Events

The file contains one TTree, Events. Count its entries:

root [2] Events->GetEntries()
(long long) 10000

Events->Print() lists every branch, but NanoAOD has a branch for every CMS trigger, so the list is very long (over 1600 branches). Filter it instead, for example to muon branches only:

root [3] Events->Print("Muon*")
...
*Br    0 :Muon_dxy  : Float_t dxy (with sign) wrt first PV, in cm            *
*Br    1 :Muon_dxyErr : Float_t dxy uncertainty, in cm                       *
...

Question 3.1

TTree::GetEntries(const char *selection) returns the number of events passing a selection (written in C++). Count the events with at least two reconstructed muons:

root [4] Events->GetEntries("nMuon >= 2")

Enter the number in the answer form.

Plotting with pyROOT

pyROOT lets you use ROOT from python with almost the same commands as in C++. On cmslpc, with the LCG setup above, start python3 (ctrl-d or exit() to quit) and reopen the file:

import ROOT
f = ROOT.TFile("DYJetsToLL_M50_NANOAOD.root", "READ")
f.ls()

TTree::Draw(varexp, selection) is the fastest way to plot from a TTree: varexp is what to plot (a branch or an expression) and selection filters the events. Let’s plot the mass of the generator-level Z bosons, i.e. the “truth” particles from the event generator, before detector simulation. GenPart_mass holds the mass of each generated particle, and GenPart_pdgId its PDG ID (23 for a Z boson):

events = f.Get("Events")
c = ROOT.TCanvas()
events.Draw("GenPart_mass", "GenPart_pdgId==23")
c.Draw()

A plot should appear with a mean close to the Z mass, 91.1876 GeV. No window? Check that you have an X server on your computer and logged in with ssh -Y (see the cmslpc instructions).

Question 3.2: Z mass plot

The plot has a “stat box” with basic information about the histogram. Enter the mean of the distribution in the answer form.

Key Points

  • ROOT and python are two key software tools in HEP.

  • Many CMS analyses use the NanoAOD format, which are simple ROOT ntuples that can be analyzed with standalone ROOT or pyROOT.

  • There are numerous ways to use ROOT, including the built-in C++ interpreter (Cling), pyROOT, Jupyter notebooks, compiled C++, and more.