Python Analysis

Complete: 3

! Warning: This page is out of date. Please visit FWLite.Python Workbook page instead.

Introduction

The FWLite environment for CMSSW can be used either with CINT (the C command line interpreter) or with the Python language. This page describes how to set your environment to use the Python implementation and some python specific issues and hints.

Getting the package

The package ist part of the standard CMSSW release. If you want to use the latest version you can get the package using the following command:
$ cvs co PhysicsTools/PythonAnalysis
After checkout you need to to scramv1 b

Starting an interactive session

The startup command to use python and FWLite interactively is
$ python -i start.py
Preparing CMS tab completer tool...
Loading FWLite dictionary...
>>>
The file start.py (in example directory) loads the needed ROOT libraries, some helper tools (cmstools) and starts the auto loading mechanism:
from ROOT import *
from CMS.PhysicsTools.PythonAnalysis import *
gSystem.Load("libFWCoreFWLite.so")
AutoLibraryLoader.enable()

Now you can open a ROOT file and bring up a browser:

>>> theFile = TFile("reco.root")
>>> myBrowser = TBrowser()
Feel free to explore the content of the file

Looping over events

To access the data inside the opened ROOT first do:
events = EventTree(theFile.Get("Events"))
After that you access the branches. For example for the monte-carlo branch (i.e. product) just do:
sourceBranch = events.branch("VtxSmeared")
As you can see you don't have to specify the type of the obect you want to access. This is quite useful if you want to learn about the information inside an event. You get the branch by using it's name or alias (see below) and then see what you've got. For that you can use the build-in help command of python. E.g. you want to know what a HepMC.GenParticle is and how it's interface looks like:

>>> 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()
... 

If you now want to loop over all events use the syntax:

for event in events:
    genEvent = event.VtxSmeared.GetEvent()

Python examples

Using container like objects

For python objects and most of the ROOT objects:

for item in iterable:
    doSomething

If you don't have direct access to the container itself but to iterators there is a slightly different syntax. This is for example the case with HepMC.GenEvent and particles_begin() and particles_end() / vertices_begin() and vertices_end(). Then it would look like:

begin = genEvent.particles_begin()
end = genEvent.particles_end()
for particle in loop(begin, end):
    doSomething
The helper function loop(a, b) iterates over all entries in the container between a and b.

Using your own C++ classes with Python

If you want to include routines you have written in C++ for e.g. kinematic fits you can follow a simple recipe. Given you have a class Fitter defined in Fitter.cc. The first step is to compile this class with ROOT ACLiC:
$ echo .L Fitter.cc+ | root.exe -b
This creates a library you then can load in Python with:
gSystem.Load( "Fitter_cc")

If you use templates in your code and want to have access to these objects in Python you need to define dictionaries for the instantiation you use. This is done by adding some statements to the top of your header file before compilation. For instance if you like to use a std::vector you have to write:

#ifdef __CINT__
using namespace std;
using namespace ROOT;
#pragma link C++ class vector< TLorentzVector >;
#pragma link C++ class vector< TLorentzVector >::iterator;
#else
template std::vector< TLorentzVector >;
#endif
Don't forget to define the iterator as well if you are using container type objects. Otherwise you wouldn"t be able to loop through them. For most of the useful containers the dictionaries are already defined in the framework.

References

More details on:

Python introductions:

Review Status

Reviewer/Editor and Date (copy from screen) Comments
BenediktHegner - 19 Jun 2007 updated started instructions
BenediktHegner - 29 Jan 2007 page content last edited
JennyWilliams - 07 Feb 2007 editing to include in SWGuide

Responsible: BenediktHegner
Last reviewed by: Reviewer

Edit | Attach | Watch | Print version | History: r12 < r11 < r10 < r9 < r8 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r12 - 2010-01-13 - CharlesPlager



 
    • 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