A Brief ROOT Tutorial
Author: Conor Henderson, University of Alabama (June 2012)
This is intended to be a simple introduction to the ROOT software framework for high-energy physics. It focuses on the most immediately useful aspects of ROOT for a beginner in the field. This brief introduction should be supplemented by the tutorials and manuals provided by the ROOT team themselves. And of course there is no substitute for doing real-life actual HEP applications with ROOT...
This is intended to be a simple introduction to the ROOT software framework for high-energy physics. It focuses on the most immediately useful aspects of ROOT for a beginner in the field. This brief introduction should be supplemented by the tutorials and manuals provided by the ROOT team themselves. And of course there is no substitute for doing real-life actual HEP applications with ROOT...
Introduction
ROOT is a software framework originally designed for use in high-energy physics (although it now has applications in other fields as well). It provides a lot of features that are directly useful for HEP applications, such as analysing, saving and visualising data.
ROOT is written in the C++ programming language. It provides a framework consisting of many pre-written classes that users can make use of. These classes implement many useful features for HEP analysis, and save users from having to develop these tools independently. ROOT also provides an interface for programs which make use of these libraries of classes, and and it also provides formats for storing data to files (and accessing such data back again from the files).
ROOT is written in the C++ programming language. It provides a framework consisting of many pre-written classes that users can make use of. These classes implement many useful features for HEP analysis, and save users from having to develop these tools independently. ROOT also provides an interface for programs which make use of these libraries of classes, and and it also provides formats for storing data to files (and accessing such data back again from the files).
Useful Resources
- The ROOT website is of course the best source of information on ROOT.
- In particular, beginners will want to consult the User's Guide to help them get started, and to get background information on the useful features of ROOT.
- Also useful is the Reference Guide, which provides extensive information for each of the many specific classes within ROOT. For example, if you wanted to know what options you can pass to a specific function for a certain class, you would find that information within this Reference Guide. Note that because such information can vary from one ROOT version to another, there is a separate Reference Guide for each of the currently supported versions.
- If you have a specific issue (such as a certain task that you can't figure out how to accomplish, or an error you are trying to understand), you may find that searching the contents of the ROOTTalk forum can be very helpful. This is where ROOT experts respond to inquiries from ROOT users; the solution to your problem will often already be found there. A general google search for your problem or error message is typically quite helpful.
Download and Install ROOT
The latest ROOT production version can be downloaded from the ROOT website. If you are using one of the supported platforms, you can find pre-compiled binaries to install. If not, you can install ROOT yourself from the source code, following the instructions provided there.
Note: Unfortunately this can sometimes be a challenging operation for first-timers to install ROOT, depending on your operating system. Running it tends to be easier, though!
Note: Unfortunately this can sometimes be a challenging operation for first-timers to install ROOT, depending on your operating system. Running it tends to be easier, though!
Getting Started
Once you have ROOT installed, you start it by typing 'root' on your command line. ROOT runs inside the terminal window. ROOT provides a new command line interface, into which you can directly type commands, and it will execute them. Usually, however, we collect a sequence of commands in a separate file called a "macro", and then we ask ROOT to execute the macro by typing:
> .x macro.C
As in our C++ introduction, you can edit the ROOT macros with any editor such as emacs.
I suggest having a terminal open running ROOT and beside it a separate window for your editor, so you can quickly switch back and forth/
> .x macro.C
As in our C++ introduction, you can edit the ROOT macros with any editor such as emacs.
I suggest having a terminal open running ROOT and beside it a separate window for your editor, so you can quickly switch back and forth/
ROOT Histograms
Histograms are one of the two most-used features of ROOT. They are ideal for the way we work in HEP - we tend to process lots of events, one at a time. We can store information from these events in a histogram, updating it each event. For example, suppose we have many events which contain a photon, and we would like to know what is the distribution of momentum that these photons have? We would define a histogram, then loop over the events, and for each event, fill the photon momentum in that event into its appropriate bin of the histogram. At the end, we could see the distribution of the momentum of all the events, and then do analysis such as: "What fraction of the photons had momentum larger than a certain value?"
Below is a simple example of how to create a histogram and save it to a file. You run this example by starting a ROOT session, then typing .x program_name.C to execute the example.
A second example shows how to read an existing histogram from a file, display it, then save the resulting plot as a pdf or gif for use in a presentation.
Below is a simple example of how to create a histogram and save it to a file. You run this example by starting a ROOT session, then typing .x program_name.C to execute the example.
A second example shows how to read an existing histogram from a file, display it, then save the resulting plot as a pdf or gif for use in a presentation.
|
|
ROOT Trees
Trees are the other most-used feature in ROOT. They are ideal for situations where you are processing lots of events, and you want to store the same type of information for each event. Let's continue our example of events containing photons, and being interested in the momentum of each one. With a tree, we could store the px,py,pz components of the momentum for every photon. You can think of this like a table, where the columns comprise the momentum components, and each row corresponds to a different event/photon, with the values appropriate for that event. Storing the information this way allows for more sophisticated analysis later than can be done on a histogram. For example, we could study correlations between the variables stored, or we could choose to look only at a subset of events which pass a particular selection.
Here is a example of how to create a very simple tree, and save it to a file
|
|
And here is another macro which reads in the tree created in the previous macro,
and shows some examples of how we can analyse the data contained in it,
such as by drawing functions of variables, and making cuts to select
only certain entries with specific properties
|
|
The previous examples were very basic trees. Here is a more realistic example for our purposes. This example assumes we are processing a set of events containing a
photon, and that for every event we want to store the event identifier
(Run Number + Event Number) and the momentum components of the photon.
|
|
Suggested Exercises with Trees
- Play with TTree::Draw(). Look it up in the ROOT Reference Guide to
get an explanation of the features it offers. Draw a variety of
variables and functions of variables from the tree. Try also doing
different cut selections on the entries.
- Suppose we are studying events which contain two photons. Add a second branch to the tree with the momentum components of the second photon. Fill it with random numbers and save the tree. In a second macro, read the tree from the file, and plot the transverse momentum of Photon 2 for events where transverse momentum of Photon1 is greater than 10 GeV.
- For real events, we also want to know the position of the
proton-proton collision. We call this the event 'vertex'. Add a new
branch to the tree to store information on the Vertex. It has 3 position
components vx,vy and vz. You can set vx=0=vy, and fill vz using a
Gaussian of mean 0 and width 10 (centimetres).
Reading a Tree Contents with TTree::MakeClass()
TTree::Draw() offers a lot of functionality, but there are still many situations where it is not enough for what we need to do. We might want to loop over all the entries in the tree, do a complicated selection, and then do some complicated analysis on the selected events. TTree::MakeClass() offers us a really good way to accomplish this.
ROOT provides this functionality to create a 'skeleton' class that will loop over all entries in the tree, and for each entry, copy the information to a set of local variables that you can access just like in regular code. Inside the created .C file, you can see where to edit the code to do event selection, fill histograms, etc.
Exercise: Read about TTree::MakeClass() in the ROOT Tree documentation, and construct an example using the above example trees.
ROOT provides this functionality to create a 'skeleton' class that will loop over all entries in the tree, and for each entry, copy the information to a set of local variables that you can access just like in regular code. Inside the created .C file, you can see where to edit the code to do event selection, fill histograms, etc.
Exercise: Read about TTree::MakeClass() in the ROOT Tree documentation, and construct an example using the above example trees.
More Realistic CMS Analysis Trees
Here are copies of some realistic CMS analysis trees
These are created from simulated diphoton events, from the decay of a hypothetical RS graviton.
Because it is simulation, we have access to the 'truth' information about the generated particles, in addition to what is obtained from running reconstruction on the information after detector simulation, as if it were real data.
Exercise: The energy resolution of photons in CMS is excellent, but no detector is perfect. From these simulated events, we can compare the Generated value of the photon pt (GenPhoton branch) with the value obtained from the reconstruction algorithms (Photon branch). Set up a MakeClass for these trees, and study the difference between the GenPhoton and Photon pt values. Usually we express the resolution as a percentage of the true value, so divide the difference by the GenPhoton.pt value. Study this separately for photons that are in the Barrel (Photon.isEB) and Endcap (Photon.isEE) subdetectors.
Exercise 2: The energy resolution of a calorimeter actually varies depending on the particle energy itself. So, divide your plots above into different pt bins, and study how the width of the resolution varies with the pt of the photon (again in EB and EE separately).
These are created from simulated diphoton events, from the decay of a hypothetical RS graviton.
Because it is simulation, we have access to the 'truth' information about the generated particles, in addition to what is obtained from running reconstruction on the information after detector simulation, as if it were real data.
Exercise: The energy resolution of photons in CMS is excellent, but no detector is perfect. From these simulated events, we can compare the Generated value of the photon pt (GenPhoton branch) with the value obtained from the reconstruction algorithms (Photon branch). Set up a MakeClass for these trees, and study the difference between the GenPhoton and Photon pt values. Usually we express the resolution as a percentage of the true value, so divide the difference by the GenPhoton.pt value. Study this separately for photons that are in the Barrel (Photon.isEB) and Endcap (Photon.isEE) subdetectors.
Exercise 2: The energy resolution of a calorimeter actually varies depending on the particle energy itself. So, divide your plots above into different pt bins, and study how the width of the resolution varies with the pt of the photon (again in EB and EE separately).