3.5.4 FW Lite in Python (PyROOT)

* Warning, important WARNING: This implementation of accessing EDM files is buggy and should be avoided. Starting in 3_3_1, there is a new FWLite.Python implementation. We do not yet have full documentation, but you can view an example here

Complete: 4


Detailed Review status

Goals of this page:

In this section you will learn how to use FW Lite in PyROOT.

Contents

FWLite in Python

If you prefer Python to CINT (the C interpreter in ROOT) you can use PyROOT instead. The commands for startup are quite close to the CINT version.

Invoke python with:

python

Then load the libraries:

>>> from ROOT import *
>>> gSystem.Load("libFWCoreFWLite.so")
>>> AutoLibraryLoader.enable()

To load a file and to open a browser, type:

>>> myFile = TFile("reco.root")
>>> myBrowser = TBrowser()
This brings up the standard ROOT Object browser.

Now you can use some of the advantages of the Python interpreter. If you want to know something more about a class, use the help command. For example (command followed by output is shown):

>>> help(HepMC.GenParticle)

Help on class HepMC::GenParticle in module __main__:

class HepMC::GenParticle(ROOT.ObjectProxy)
 |  Method resolution order:
 |      HepMC::GenParticle
 |      ROOT.ObjectProxy
 |      __builtin__.object
 |  
 |  Methods defined here:
 |  
 |  BeginDaughters(...)
 |      int GenParticle::BeginDaughters()
 |  
 |  CollisionNumber(...)
 |      int GenParticle::CollisionNumber()
 |  
 |  CreationVertex(...)
 |      CLHEP::HepLorentzVector GenParticle::CreationVertex()
 |  
 |  DecayVertex(...)
 |      CLHEP::HepLorentzVector GenParticle::DecayVertex()

... 

This way you can explore all objects inside the ROOT file and learn step-by-step what information you can get. Just take an object, use help and get used to the interface of the class.

To quit the interactive python session, press Ctrl-D or .q as in ROOT.

Event loop in Python

You could use a similar syntax as above for a Python event loop. But there is a much smarter way. First you create an EventTree and then access the products inside the event by EventTree.branch. Inside the loop there is no GetEntry necessary. The point here is that you don't need to define the variable beforehand and thus you don't have to know the type of the object you want to access. This makes the coding much faster. Here an example showing how it works:
from ROOT import * 
from PhysicsTools.PythonAnalysis.cmstools import *

### prepare the FWLite autoloading mechanism 
gSystem.Load("libFWCoreFWLite.so") 
AutoLibraryLoader.enable() 

# open the file and access the event tree 
events = EventTree("reco.root") 

# loop over the events 
for event in events:
    muons = event.getProduct("blah") #with 'blah' being the branch name.
    for muon in muons:
        if muon.pt() > 30 and abs(muon.eta()) < 2.1:
            hist_muPt.Fill(muon.pt()) 
    ... 

Review status

Reviewer/Editor and Date (copy from screen) Comments
PetarMaksimovic - 08 Jul 2009 Created.

Last reviewed by: PetarMaksimovic - 08 Jul 2009

-- PetarMaksimovic - 08 Jul 2009

Edit | Attach | Watch | Print version | History: r14 | r6 < r5 < r4 < r3 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r4 - 2010-01-01 - KatiLassilaPerini


ESSENTIALS

ADVANCED TOPICS


 
    • 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-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