3.5.4 FWLite.Python (using PyROOT)

Detailed Review status

Goals of this page:

In this section you will learn how to use FWLite.Python.

For the impatient: you can also jump directly to instructions how to Run ROOT macros that analyze MiniAOD CMS data directly in MyFWLite_12


This document assumes that you already know python (although you may be able to learn a fair bit just following the examples). Please see references for some links for learning Python.

Why Python? CMS already uses it for its configuration files. It is a powerful language that many consider both much easier to read and easier to write than C++. FWLite.Python also provides a possibility to interactively play with CMS data.

Note: Many people are already using Root's Cint to interactively access CMS data. This is fine if one is only using the TBrowser or Draw() command, but for anything more complicated, Cint is very unreliable (meaning it crashes if you are lucky or runs without any error messages but gives you the wrong answer if you are not).

FWLite.Python is designed to be intuitive if you are both experienced with Python and FWLite or the full framework.

Simple Annotated Example

For a complete example, see patZpeak.py. This can be used either as a script or typed in interactively.

To start, we want to import ROOT and the needed pieces of FWLite.Python:

import ROOT
from DataFormats.FWLite import Events, Handle

You then create an Events object by giving it either:

  • VarParsing options object (see Command line option parsing twiki for details)
  • A string containing an input file
  • A list of strings containing input files
In this case, I'll use a single input file  (see other options in MyFWLite_12)
events = Events ('MuMu_miniaod-230-1.root')

We then create a handle. Unlike cmsRun and FWLite in C++, this is a cpu-intensive operation so it is recommend to do this outside of the event loop.

# create handle outside of loop
handle  = Handle ('std::vector<pat::Muon>')

Now we make a label

# a label is just a tuple of strings that is initialized just
# like and edm::InputTag
label = ("slimmedMuons")

By default, PyRoot will open windows. Make sure you tell Root if you don't want to do this:

ROOT.gROOT.SetBatch()        # don't pop up canvases

Create any histograms you want to fill:

# Create histograms, etc.
ROOT.gROOT.SetStyle('Plain') # white background
zmassHist = ROOT.TH1F ("zmass", "Z Candidate Mass", 50, 20, 220)

And now the event loop:

# loop over events
for event in events:

Let's get the data as we would in FWLite and cmsRun:

    # use getByLabel, just like in cmsRun
    event.getByLabel (label, handle)

Now use what you got:

    # get the product
    muons = handle.product()

Since I'm running over Z → μμ, let's make a Z peak:

    # use muons to make Z peak
    numMuons = len (muons)
    if numMuons < 2: continue
    for outer in xrange (numMuons - 1):
        outerMuon = muons[outer]
        for inner in xrange (outer + 1, numMuons):
            innerMuon = muons[inner]
            if outerMuon.charge() * innerMuon.charge() >= 0:
            inner4v = ROOT.TLorentzVector (innerMuon.px(), innerMuon.py(),
                                           innerMuon.pz(), innerMuon.energy())
            outer4v = ROOT.TLorentzVector (outerMuon.px(), outerMuon.py(),
                                           outerMuon.pz(), outerMuon.energy())
            zmassHist.Fill( (inner4v + outer4v).M() )

Groovy. Let's make a plot and quit:

# make a canvas, draw, and save it
c1 = ROOT.TCanvas()
c1.Print ("zmass_py.png")


Python introductions:

Review status

Reviewer/Editor anda date Comments
JhovannyMejia - 27 Oct 2021 Update to pythin3 and MiniAOD
-- CharlesPlager - 13-Jan-2010 -- PetarMaksimovic - 08 Jul 2009
Edit | Attach | Watch | Print version | History: r16 < r15 < r14 < r13 < r12 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r16 - 2022-02-24 - JhovannyMejia



    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    CMSPublic All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2023 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback