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.

Introduction

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

events = Events ('ZmumuPatTuple.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 = ("selectedLayer1Muons")

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 muons < 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:
                continue
            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()
zmassHist.Draw()
c1.Print ("zmass_py.png")

References

Python introductions:

-- CharlesPlager - 13-Jan-2010 -- PetarMaksimovic - 08 Jul 2009


This topic: CMSPublic > CMSCommunicationsGroup > CMSCommunicationsProjects > WebHome > SWGuide > WorkBook > WorkBookFWLitePython
Topic revision: r6 - 2010-01-30 - BenediktHegner
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback