GaudiPython Tutorial

This tutorial is focused on how to use GaudiPython. It uses the J/psi -> Mu Mu example to illustrate how to access the LHCb software from within Python and fit two muons to a J/psi vertex, as well as how to acces the information stored on a microDST.

Please refer to UK Software course Jan 2010 for a more recent version.


This tutorial refers to the slides shown here.


The GaudiPython tutorial uses two scripts: One to initialise Gaudi ( and one to run the actual analysis ( or

This is done to exploit a feature of the Python language: scripts can be reloaded after editing using the command reload(MyScript) which reloads the script MyScript (but not the ones loaded from within the script). Since the initialisation of the Gaudi part needs to be done only once, this part can be handled by a separate script.

Environment setup

As we will use tools and methods from DaVinci using Python, you need to setup the appropriate environment:
  • Set environment for the latest DaVinci release:
SetupProject DaVinci
  • Copy the files,, to your area
  • Start an interactive python session:

Using GaudiPython to fit a J/psi -> Mu Mu vertex

This exercise is very similar to the second exercise of the DaVinci tutorials: Two muons are fitted to a common J/psi vertex. Instead of writing C++ code and use it with DaVinci, we do the same thing using Python.
  • Load the script at the Python command prompt:
>>> import Analysis
  • Run the EventLoop function. The function takes two integer arguments: the first specifies the number of events to process (e.g. 10), the second one defines the level of verbosity
>>> Analysis.EventLoop(10,1)
  • The script fills a histogram with the mass of each J/psi candidate, plot the histogram:
>>> Analysis.hMass.Draw()
  • Now run over a couple of more events and then plot the histogram again
>>> Analysis.EventLoop(100)
>>> Analysis.hMass.Draw()
  • The script also creates a plain ROOT ntuple with the mass of the J/psi candidate and the chi2 of the vertex fit. Close the ntuple and have a look in ROOT
>>> Analysis.CloseRootNtuple()
>>> exit()

  • Now start extending the script by filling more histograms, applying cuts, etc.
>>> import Analysis
  • If you edit the Analysis script while the python session, you can use
>>> reload(Analysis)
  • The following command will jump back to the beginning of the input file
>>> Analysis.sel.rewind()

Using GaudiPython to access a microDST

Create a microDST

The job-options file JPsiMuMuMicroDST.opts from the tutorial package can be used to create a microDST using DaVinci. Again, it fits two muons to a common J/psi vertex. The J/psi candidate is then stored on the microDST, together with all its children (i.e. the muons, the associated proto-partices, tracks and corresponding truth information). To create the microDST, execute :
SetupProject DaVinci
and then
DaVinci JPsiMuMuMicroDST.opts
which creates the file microDST.dst in the current working directory.

Prepare the script initialising GaudiPython

We will use the same script as in the previous exercise. Howver, as we now access the microDST from the local area, we need to edit it:
  • Comment out the data read from CASTOR
  • Comment out the options file setting up the full DaVinci environment
  • Instruct Gaudi to look in the right places on the TES by un-commenting
,'ApplicationMgr.TopAlg.RootInTES = "/Event/microDST" '

  • open the microDST by un-commenting['PFN:microDST.dst'])

Analyse the microDST

Now analyse the microDST using the script It will fill the same histogram, ntuple, etc as in the previous exercise. However, instead of fitting two muons to a common J/psi vertex, we now use the J/psi candidate created previously running DaVinci.
>>> import Analysis
>>> Analysis.EventLoop(100)
>>> Analysis.CloseRootNtuple()
>>> exit()

-- UlrichKerzel - 31 Mar 2008

Edit | Attach | Watch | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r4 - 2010-03-09 - unknown
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    LHCb All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2020 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