C++ programming for physicists ROOT: An object-orientated analysis framework PD Dr H Kroha, Dr J Dubbert, Dr M Flowerdew 1 Kroha, Dubbert, Flowerdew 14/04/11
What is ROOT? An object-orientated framework aimed at solving the data analysis challenges of high-energy physics Project started in the 1990's particularly aimed at the Large Hadron Collider experiments Designed for large-scale data analysis parallel processing of multi- Terabyte datasets Main components: C++ core classes CINT command-line interpreter PROOF, the Parallel ROOT Facility PyROOT, a python interface (recommended, but learn python first!) ROOT User's Guide 2 Kroha, Dubbert, Flowerdew 14/04/11
Links ROOT home page: http://root.cern.ch User's guide (pdf): http://root.cern.ch/drupal/content/users-guide ROOT reference guide: http://root.cern.ch/root/html/index.html Type a class or method name into the search box RootTalk forum (user help): http://root.cern.ch/phpbb3 Extra statistics packages: https://twiki.cern.ch/twiki/bin/view/roostats/webhome Some alternatives to ROOT (for non-hep physicists) are listed here: http://www.insectnation.org/howto/academic-software 3 Kroha, Dubbert, Flowerdew 14/04/11
Core C++ classes The main data types (classes) you will deal with are: TFile: database-like file, with.root suffix TTree and TNtuple: event-by-event record of variables TH1: binned histogram summary of data TF1: representation of a mathematical function TBrowser and TCanvas: Interactive graphical interfaces to the above TVector3 and TLorentzVector: 3- and 4-dimensional vectors (Nearly) any ROOT object can be saved in a TFile ROOT classes have a complex (and controversial) inheritance structure All histograms inherit from TH1 THnX = n-dimensional histogram of type X (eg TH1D, TH2I, TH3F, ) Functions TF2 and TF3 similarly inherit from TF1 The ROOT libraries can be used in your C++ program, but we will use ROOT interactively today 4 Kroha, Dubbert, Flowerdew 14/04/11
Initial setup Try to run ROOT: > root If you get this output, ROOT is not fully set up on your system: > root bash: root: command not found To fix it, use the alias command: > alias root=/opt/cern/root/bin/root Note: This is not the best solution, just the fastest 5 Kroha, Dubbert, Flowerdew 14/04/11
CINT, the C/C++ Interpreter Commands you run are in yellow Open a ROOT file on the command line: > root -l block_test.root root [0] Attaching file block_test.root as _file0... root [1] _ -l (ell, not one!) avoids splash screen CINT takes commands that look like C++ Not all valid C++ statements are supported Many illegal C++ statements are allowed Eg semi-colons at end of line is optional Some non-c++ commands exist to make your life easier root [1].ls TFile** block_test.root TFile* block_test.root KEY: TNtuple Ntuple;1 Block-N-Tuple root [2].q > _ List contents of TDirectory Exit ROOT 6 Kroha, Dubbert, Flowerdew 14/04/11
What's in a name? Look again at the file listing: root [1].ls TFile** block_test.root TFile* block_test.root KEY: TNtuple Ntuple;1 Block-N-Tuple These give the class names This is an instance name This is an instance title Most ROOT objects have a name (a C-string). In terms of class structure, this is a private class variable, and it is set when the object was first created. CINT uses the name as an identifier Pseudo-code: TNtuple* Ntuple = new TNtuple(...) Titles are human-readable descriptions of a particular instance 7 Kroha, Dubbert, Flowerdew 14/04/11
The interactive browser > root -l block_test.root root [0] Attaching file block_test.root as _file0... root [1] groot->setstyle( Plain ) root [2] TBrowser *b = new TBrowser Gets rid of nasty grey canvas background Note: Screenshots use ROOT version 5.28 + ATLAS style Older versions (eg 5.26) look different: try File New Browser if you want the new one 8 Kroha, Dubbert, Flowerdew 14/04/11
An ntuple In the browser, double-click on the file name (under ROOT Files ) and then the ntuple name This shows the structure of your saved TNtuple object The TFile object The TNtuple object Branches (or leaves ) of the ntuple The branches are filled for each event A TNtuple only holds float variables. A TTree can hold other types, including classes 9 Kroha, Dubbert, Flowerdew 14/04/11
First histogram On the CINT command line, type root [1] Ntuple->Draw( x_out ) Alternatively, double-click the variable in an open TBrowser Frequency Variable name Tip: The canvas is interactive try dragging and right-clicking different things 10 Kroha, Dubbert, Flowerdew 14/04/11
The draw panel Right click on the histogram itself Make sure to get the histogram, not the canvas! The first menu line says the name of the class and instance to help you In the menu, select the Draw Panel Here, you can easily change the display properties of the histogram Colour and size of the line and markers Display statistical errors on each point Modify the binning and axis range 11 Kroha, Dubbert, Flowerdew 14/04/11
Controlling the plot To choose a specific histogram binning: root [1] Ntuple->Draw( x_out>>h(50,-100,100) ) Two dimensional (scatter) plot: root [1] Ntuple->Draw( x_out:z_out ) Two dimensional histogram: Improve the colour palette: Histogram name Number of bins Minimum x x var Maximum x root [1] Ntuple->Draw( x_out:z_out>>h2(50,0,40, 50,-3,3,, COLZ ) root [2] gstyle->setpalette(1) root [3] h2->draw( COLZ ) 12 Kroha, Dubbert, Flowerdew 14/04/11 y var y var x var Histogram name Number of x bins Minimum x Etc... Invalid C++! (Why?)
Advanced plotting Functions of variables can also be drawn: root [1] Ntuple->Draw( E_out - E_in ) Apply a selection using the second argument: root [1] Ntuple->Draw( x_out, abs(z_out-40.0) < 0.001 ) Some obvious functions are implemented: abs, sqrt, cos, sin, Set some histogram properties: root [1] Ntuple->Draw( x_out:z_out>>h2(50,0,40, 50,- 3,3,, COLZ ) root [2] h2->getxaxis()->settitle( z position [mm] ) root [3] h2->getyaxis()->settitle( x position [mm] ) root [4] h2->draw( COLZ ) 13 Kroha, Dubbert, Flowerdew 14/04/11
The result Go to File Save As to save the image Output formats:.ps (postscript).eps.pdf.gif.jpg.png.c (CINT script).root 14 Kroha, Dubbert, Flowerdew 14/04/11
One more for fun 3- or even 4-dimensional plots are possible (note: your ntuples don't have y_out) root [1] Ntuple->Draw( x_out:y_out:z_out:e_in ) Colour is the fourth dimension (E_in) 15 Kroha, Dubbert, Flowerdew 14/04/11
TTree::Draw() TNtuple is derived from TTree, which defines this method: virtual long int TTree::Draw(const char * varexp, const char * selection =, const char * option =, long int nentries = 1000000000, long int firstentry = 0); NB: This is simplified for clarity ROOT has its own types for portable integers and C-strings Note that this means we can control which entries we plot too: root [1] Ntuple->Draw( x_out,,, 500, 1000) (Plot entries from 1000-1499 inclusive) 16 Kroha, Dubbert, Flowerdew 14/04/11
Fitting a histogram If we think a histogram has a certain shape (eg a Gaussian), we can try fitting it with a test function Right click on the histogram line and select Fit Panel Select the fit function to try Default gaus is a Gaussian More details on this and other options at http://root.cern.ch/root/html/tf1.html The fit range is usually important too A similar effect can be achieved on the command line: root [1] h1->fit( gaus,,, -1.5, 1.5) Gaussian fit for -1.5 x < 1.5 17 Kroha, Dubbert, Flowerdew 14/04/11
More on fitting Here is shown a Gaussian fit to this histogram: root [1] Ntuple->Draw( x_out, abs(z_out-40.0) < 0.001 ) The thick black line represents a TF1 object, which you can also manipulate in the canvas, like a histogram Always remember to ask if the fit looks reasonable, and if not, why not 18 Kroha, Dubbert, Flowerdew 14/04/11
Now over to you... What energy do the particles have... At the start? (z = 0) At the exit of each layer of material? What is the distribution of x at each point? Can you see the finite steps in z? Does the step size vary? Try plotting z_out with a cut on z_out Can you plot the path of just one particle? Hint: Remember you can control the first and last event plotted On a sample with lots of particles: can you understand your results in terms of the multiple scattering equations? S 1 2 d θ E, where θ E has a mean of zero and a standard deviation of θ 0 = 13.6 MeV E q d X 0 19 Kroha, Dubbert, Flowerdew 14/04/11