GaudiPython Tutorial

This tutorial focuses on how to use GaudiPython to explore a DST or MicroDST, and make some simple plots of quantities derived from their contents. Ths site is under construction and at the moment most of the material is in the Tutorial/GaudiPythonTutor package and on the slides (last updated 2010-10-01).


We will focus on how to use GaudiPython to explore a DST or MicroDST, and make some simple plots of quantities derived from their contents. For this reason, we will try to avoid ready-built modules and existing example scripts as much as possible, and try to do as much as possible ourselves, starting from the basics. This means that we will end up with a bunch of very similar scripts, and you may indulge in some cutting and pasting. Please note that once a coding pattern is used in more than one place, it is better to factor it out ant put it somewhere where it can be used by everyone. But today, we will not worry about that.

The tutorial is structured as a series of exercises of increasing complexity. By the end of the tutorial you should know how to obtain LHCb Event Model objects from the transient event store (TES), how to make histograms, how to create and use LHCb-specific Gaud tools, how to use relations tables associating Particles to MCParticles or Vertices. tables. You may also build on what you learnt earlier by either using your own Bs->Jpsi Phi selection, or re-using one of those used in the stripping, to write a MicroDST and analyse it. Depending on your knowledge of python, you may also experiment with making reasonably complex calculations using the contents of the (Micro)DST with your own pure python functions.


You should have followed all the LHCb software tutorials up to and including the DaVinci tutorial, or be confident that you know everything involved. You should also be reasonably familiar with the python programming language, hopefully with a version higher than 2.3. I recommend following the online tutorial up to and including section 5, but it is not strictly necessary that you do it all before this tutorial, it just will help a lot. You should be in a position where the following set of exercises should not pose any problems to you.

Python exercises

  1. Play with a list:
# print x after each step
x = range(10)
x[2:4] = [999,999]
  1. Try the folwing. What do you expect y to be?
x = [0,1,2,3,4]
y = x*3
print y
  1. Create a list containing the integers -100 to 100. (hint: help(range), help(xrange))
  2. Create a list containing numbers -10., -9.9, ..., 9.9, 10
  3. Create a list containing the sin of each entry in the previous list (hint: import math, dir(math), see list comprehensions )
  4. Find the maximum and minumin values in that list
  5. Sort the entries in the list
  6. Create a dictionary that returns the first name of someone given their last name. If you are in company of people that you talk to, use it for your four nearest neighbours.
  7. Write a helloWorld function such that the following prints "Hello World":
  1. What will this print?
def fun(x) :
   x += 500.

n = 4
print n
  1. Write a factorial function factorial such that
factorial(4) == 24
factorial(9) == 362880
  1. Create a functor Adder that adds a user-defined number to any number passed to it. A functor is a class with an operator instance(args), defined in python as def __call__(self, ... args...) :
add5 = Adder(5)
assert add5(4) == 9
  1. Use the functor to add 3 to each element of the -100 to 100 list
  2. If you find all of the above easy, carry on with the tutorial, or have a look at this.

Setting up

You will need the DaVinci v26r1 envoronment, plus your own copy of Tutorial/GaudiPythonTutor v1r2. From the previous LHCb and DaVinci tutorials you should know how to get the package from the repository, set the environment, and build. Since Tutorial/GaudiPythonTutor is not known by DaVinci, you need to run source setup.(sh, csh) from your =Tutorial/GaudiPythonTutor/cmt directory to set variables like $GAUDIPYTHONTUTORROOT.


You will find plain text descriptions of each exercise in $GAUDIPYTHONTUTORROOT/exercises. We shall be attempting
  1. GaudiPythonTutorialUnits: A bit of playing around to see how units are defined and what the defaults are.
  2. GaudiPythonTutorialPVs: Use a basic event loop to look at the PVs
  3. tracks0.txt
  4. exercises/microDST0.txt: make a MicroDST. See Creating a MicroDST.
  5. exercises/particles0.txt: a simple loop on Particles
  6. exercises/p2pv0.txt: Use tools to get "best" primary vertices for each particle.
  7. exercises/particles1.txt: a recursive version of exercises/particles0.txt. If time permits.
  8. exercises/particles2.txt: use tools to get associated MC particles to make resolution plots
  9. exercises/particles3.txt: a recursive version of exercises/particles2.txt. If time permits.

In each case, write a suitable named script in scripts. We will be running the scripts with python -i such that we can play with histograms and so on interactively.


There are two examples in the package: scripte/ and scripts/ The former is probably the simplest loop imaginable, and just serves to count events. The latter accesses some data and plots some histogram. We will build on top of that to make more complex examples.

Helping yourself

Besides using the in-built python documentation functions help and dir, there is the Davinci doxygen, which should contain adequate documentation for our C++ classes. If you find any deficiencies there, please complain to the authors! We will use a combination of python help and doxygen.
# these are some histogramming helpers that we will be using extensively. Check them out:  
from AnalysisPython import HistoUtils

Useful links


We will be using some LHCb event model types, plus some tools to perform operations on them. The full doxygen documentation for classes seen by DaVinci are here.These are the ones you're most likely to meet:

We shall also be directly using the following tools:

  • ILifetimeFitter. Fits the propet time of an LHCb::Particle.
  • IP2MCP: obtain the related LHCb::MCParticle decay line for a given LHCb::Particle.
  • IRelatedPVFinder: Used to find the "best" primary vertex for a given particle.


Use as much as possible the python in-built documentation (help, dir, type). Use also the python tutorial pages. See also the Google Python Class.

Other links

Other tutorials and examples.

-- JuanPalacios - 01-Oct-2010

Edit | Attach | Watch | Print version | History: r17 < r16 < r15 < r14 < r13 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r17 - 2013-09-04 - NathanaelFarley
    • 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-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