Getting Started

This page is a collection of links to help users get started with programming and the general HEP software packages.

C++

C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises both high-level and low-level language features. (see Wikipedia)

Reference

Tutorials

Books

Java

Java is a general-purpose, concurrent, class-based, object-oriented computer programming language that is specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another. (see Wikipedia)

Reference

Tutorials

Books

Python

Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C. The language provides constructs intended to enable clear programs on both a small and large scale. (see Wikipedia)

Reference

Tutorials

Books

Useful Packages

ROOT

An object oriented data analysis and plotting framework developed specifically for high energy physics analysis.

TMVA

The Toolkit for Multivariate Analysis (TMVA) provides a ROOT-integrated machine learning environment for the processing and parallel evaluation of multivariate classification and regression techniques.

RooFit

The RooFit library provides a toolkit for modeling the expected distribution of events in a physics analysis. Models can be used to perform unbinned maximum likelihood fits, produce plots, and generate "toy Monte Carlo" samples for various studies.

PyROOT

PyROOT is a Python extension module that allows the user to interact with any ROOT class from the Python interpreter.

Examples

Creating and fitting a histogram

from ROOT import TCanvas, TF1, TH1D, TRandom3, kRed, kDashed

# create a canvas for the histogram
canvas = TCanvas( 'aCanvas', 'my Canvas', 800, 700 )
# create the histogram
histogram = TH1D( 'aHistogram', 'my Histogram;x;y', 100, 0., 10. )
# create a random number generator
random = TRandom3()

# loop and fill the histogram
for index in xrange( 10000 ):
    histogram.Fill( random.Gaus( 5.0, 2.0 ) )

# create a fit function
fitFunction = TF1( 'aFunction', 'gaus', 0., 10. )
# change the appearance of the fit
fitFunction.SetLineColor( kRed )
fitFunction.SetLineStyle( kDashed )
# fit the histogram
histogram.Fit( fitFunction, '' )
# draw the histogram (together with its fit)
histogram.Draw()

Creating a graph with error bars

from ROOT import TF1, TGraphErrors, TRandom3, kRed, kDashed
import math

xValues = [ 0.5, 0.7, 0.9, 1.1, 1.4, 1.9, 2.4, 3.1, 3.7 ]
xResolution = 0.05
yResolution = 0.1
# create a random number generator
random = TRandom3()
# create a graph with error bars and set its title
graph = TGraphErrors( len(xValues) )
graph.SetTitle( 'my Graph;x;y' )

for index, xValue in enumerate(xValues):
    # calculate y and smear with the resolution
    x = random.Gaus( xValue, xResolution)
    y = random.Gaus( 0.5 * math.sin( 2.0 * xValue - 0.3 ) + 0.5, yResolution )
    # set the point values and the errors
    graph.SetPoint( index, x, y )
    graph.SetPointError( index, xResolution, yResolution )

# define a fit function and set some starting values for the fit parameters
fitFunction = TF1( 'aFunction', '[0] * sin([1] * x + [2]) + [3]' )
fitFunction.SetParameters( 1., 1., 0., 0. )
# change the appearance
fitFunction.SetLineColor( kRed )
fitFunction.SetLineStyle( kDashed )
# fit the graph and draw the graph (together with its fit)
graph.Fit( fitFunction )
graph.Draw( 'AP' )

Create a graph using arrays

from ROOT import TGraph, kOpenDiamond
from array import array

xValues = array( 'f', [-3, -2, -1, 0, 1, 2, 3, 4] )
yValues = array( 'f', [10, 5, 2, 1, 2, 5, 10, 17] )
# create a graph using array objects and set its title
graph = TGraph( len(xValues), xValues, yValues )
graph.SetTitle( 'my Graph;x;y' )
# change the appearance and draw the graph
graph.SetMarkerStyle( kOpenDiamond )
graph.Draw( 'APC' )

Writing a TTree

from ROOT import TFile, TTree, TRandom3, std
from array import array

# create a file to store the tree
rootFile = TFile( 'myTree.root', 'recreate' )
# create the tree
tree = TTree( 'aTree', 'A test tree' )
# python basic types can not be passed by reference we need to use arrays instead
integerVariable = array( 'i', [0] )
gaussVariable = array( 'f', [0.] )
# the branch address points to the first entry of the array
tree.Branch( 'integer', integerVariable, 'integer/I' )
tree.Branch( 'gauss', gaussVariable, 'gauss/F' )
# we don't need this work around for objects. This line also illustrates how to create C++ template objects
vectorVariable = std.vector( 'double' )()
# we can only create branches of TObjects or other objects with a ROOT dictionary
tree.Branch( 'vector', vectorVariable )
# create a random number generator
random = TRandom3()

# filling the tree
for entry in xrange( 100 ):
    # here we have to set the first entry of the arrays, which corresponds to the branch adresses
    integerVariable[0] = random.Integer( 10 )
    gaussVariable[0] = random.Gaus( 5.0, 3.5 )
    vectorVariable.clear()
    for index in xrange( random.Integer( 5 ) ):
        vectorVariable.push_back( random.Gaus( -7.0, 15.0 ) )
    tree.Fill()

# write and close the file
rootFile.Write()
rootFile.Close()

Reading a TTree

from ROOT import TFile

# open the ROOT file containing the tree
rootFile = TFile( 'myTree.root' )
# get the tree object
tree = rootFile.Get( 'aTree' )

# loop over the tree and print its content
for entry in tree:
    vectorAsList = []
    # loop over the std::vector
    for value in tree.vector:
        vectorAsList.append( value )
    # branches are accessible as member variables of the tree
    print 'integer = %d, gauss = %g, vector = %s' % ( tree.integer, tree.gauss, vectorAsList )

# close the file
rootFile.Close()

LCIO

LCIO is a persistency framework that defines a data model for linear collider detector studies. It is intended to be used in both simulation studies and analysis frameworks. Its light weight and portability makes it also suitable for use in detector R&D applications. It provides a C++ and a Java implementation with a common interface (API) - a Fortran interface to the C++ implementation also exists.

pyLCIO

PyLCIO is a python module that allows access to LCIO using pyROOT and the LCIO ROOT dictionaries. It enhances the basic LCIO interface to allow "pythonic" coding.

Examples

Test file for the examples: test.slcio

Reading LCIO files and their collections

from pyLCIO import IOIMPL

# create a reader and open an LCIO file
reader = IOIMPL.LCFactory.getInstance().createLCReader()
reader.open( 'test.slcio' )

# loop over all events in the file
for event in reader:
    print 'Content of event number %s' % ( event.getEventNumber() )
    # loop over all the collections in the event
    for collectionName, collection in event:
        print '\t%s of type %s with %d elements' % ( collectionName, collection.getTypeName(), collection.getNumberOfElements() )
reader.close()

Reading from an LCIO collection

from pyLCIO import IOIMPL
from ROOT import TCanvas, TH1D, TF1, kRed, kDashed

# create a reader and open an LCIO file
reader = IOIMPL.LCFactory.getInstance().createLCReader()
reader.open( 'test.slcio' )
# create a canvas for the histogram
canvas = TCanvas( 'aCanvas', 'my Canvas', 800, 700 )
# create a histogram for the hit energies
hitEnergyHistogram = TH1D( 'aHistogram', 'Hit Energy;ECAL Barrel Hit Energy [MeV];Entries', 100, 0., 1. )

# loop over all events in the file
for event in reader:
    # get the collection from the event
    hitCollection = event.getCollection( 'EcalBarrHits' )
    # loop over all hits in the collection and fill the histogram
    for hit in hitCollection:
        hitEnergyHistogram.Fill( 1000. * hit.getEnergy() )
reader.close()
# define a fit function
fitFunction = TF1( 'aFunction', 'landau', 0.05, 1. )
# change styling of the fit function
fitFunction.SetLineColor( kRed )
fitFunction.SetLineStyle( kDashed )
# fit the histogram
hitEnergyHistogram.Fit( fitFunction, 'R' )
# draw the histogram
hitEnergyHistogram.Draw()

Decoding cell IDs

Usually the class UTIL::CellIDDecoder is used to decode the cell IDs in LCIO. This class overloads the ()-operator to set the hit, which can not be mapped to a python method. A workaround is to use the underlying BitField64 class directly to decode the cell IDs.

from pyLCIO.io.LcioReader import LcioReader
from pyLCIO import EVENT, UTIL

# create a reader
reader = LcioReader( 'test.slcio' )

# loop over the events
for event in reader:
    # get a hit collection
    hcalHits = event.getCollection( 'HcalBarrHits' )
    # get the cell ID encoding string from the collection parameters
    cellIdEncoding = hcalHits.getParameters().getStringVal( EVENT.LCIO.CellIDEncoding )
    # define a cell ID decoder for the collection
    idDecoder = UTIL.BitField64( cellIdEncoding )
    # loop over all hits in the collection
    for caloHit in hcalHits:
        # combine the two 32 bit cell IDs of the hit into one 64 bit integer
        cellID = long( caloHit.getCellID0() & 0xffffffff ) | ( long( caloHit.getCellID1() ) << 32 )
        # set up the ID decoder for this cell ID
        idDecoder.setValue( cellID )
        # access the field information using a valid field from the cell ID encoding string
        print 'layer:', idDecoder['layer'].value()

Bash

Bash is a command line interface to run most of the software programs.

Bash Completion

Also install the bash completion package to simplify your life considerably. It provides a context sensitive completion for many tools. I.e. acroread will only complete for pdf files and folders, subversion will give a list of possible commands after typing svn and hitting tab, or give a list of options after typing --, make will complete for a list of targets, etc.

You probably also need to have this

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi
in your ${HOME}/.bashrc file
Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatslcio test.slcio r1 manage 594.5 K 2013-07-03 - 13:49 ChristianGrefe Test file for LCIO examples
Edit | Attach | Watch | Print version | History: r11 < r10 < r9 < r8 < r7 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r11 - 2014-10-20 - ChristianGrefe
 
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    CLIC All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback