Basic ROOT Tutorial Collaboration Meeting Jun2019: Difference between revisions
Line 108: | Line 108: | ||
Now we shall create a simple macro to analyse some of the data from the .root file provided. | Now we shall create a simple macro to analyse some of the data from the .root file provided. | ||
===Exercise 4: | ===Exercise 4: Compiling Code === | ||
Code can be compiled a number of ways. | |||
1) Generate an object library from the macro code, inside the interpreter type: | |||
.L ExampleMacro.C+ | |||
Note the use of the "+" here. Once this operation is accomplished, the macro functions are in memory and you will be able to execute them by calling from inside the interpreter: | |||
macro_function() | |||
2)Compile with a c++ compiler e.g. gcc. In order to make the code executable stand-alone, you must have a main function defined, for example: | |||
int main() { | |||
ExampleMacro(); | |||
return 0; | |||
} | |||
Can be added to the end of your macro. Now compile using: | |||
g++ -o ExampleMacro ExampleMacro.C `root-config --cflags --libs` | |||
or similar if you use another platform. | |||
You can now execute the macro by typing: | |||
./ExampleMacro | |||
===Exercise 5: Saving the histograms to a file (.root)=== | ===Exercise 5: Saving the histograms to a file (.root)=== |
Revision as of 18:41, 30 May 2019
Tutorial Session Goal
In this Tutorial you will learn the basics of ROOT. The final aim being to learn how to analyze TTree data produced by the Mu2e Offline software.
Session Prerequisites and Advance Preparation
As this is a basic introduction, few prerequisites are necessary, however, it would be beneficial if attendees:
- Take a look at [1].
- Download and install from this page to your machine.
ROOT: An introduction
ROOT is a modular scientific software toolkit used extensive in High Energy and Particle Physics. ROOT provides a platform for data processing, statistical analyses, visualisation and data storage.
ROOT is an object-orientated framework predominately written in c++.
For more information: https://root.cern.ch/
The Basics
ROOT uses a C++ interpreter, you can use it on command line, no need to use ";" at the end of every line.
Some basic commands:
- -".q" - quits ROOT
- - ".?" - displays special commands
- - ".x Example.C" - executes the macro in Example
- -".L Example.C" - Loads Example and the associated classes within
What is a TTree?
TTrees are used through out particle physics as data containers. They can be form both input and output files in a ROOT macro.
Exercises
The majority of the session time should be spent performing exercises, which you link or embed in the session page.
For these exericses please download this .root file .....
Exercise 1: Open the and look at content with a TBrowser
- Once you have downloaded and installed root, open a new terminal.
- Type "root" - this will open up root in interactive mode (you should see the root logo flash up and you are then in the root environment)
- Open up the file by typing:
TFile::Open("$FullPathToFile/FileName.root")
This file will contain the TTree, this is a data container used by root and by the Mu2e Offline software.
- View the contents of the File by typing:
.ls
You should see a list of the contents of the file.
- You can create a TBrowser called "a" using the following in the command line:
TBrowser a
A GUI browser should appear which lists files in your current directory. You should see the .root file. You can select the file. You will see the TTree and various associated TBranches. Select one - a histogram should appear.
You can project a histogram in the TBrowser.
Exercise 2: Reading a TTree
You can find all the methods which can be applied to a TTree Class object here: https://root.cern.ch/doc/master/classTTree.html
There are a few ways to Read a TTree, for example:
TFile f("ExampleFile.root") myTree->Print()
An alternative approach is to use the TTree::Scan function to loop over the TTree entries and print entries passing given selection:
TTree::Scan("leaf":"leaf":….)
Exercise 3: Plotting from command line
You can use root to plot functions for example:
TF1 f1("f1","sin(x)/x",-10,10) f1.Draw()
This command displays the function in a window which should pop up after you typed the above two lines.
You can also build histograms on the command line:
TF1 func("efunc","exp([0]+[1]*x)",0.,5.) func.SetParameter(0,1) func.SetParameter(1,-1) TH1F h("h","example histogram",100,0.,5.) for (int i=0;i<1000;i++) {h.Fill(efunc.GetRandom())} h.Draw()
Exercise 3: Write a macro to produce histograms
If you have a number of lines which you were able to execute at the ROOT prompt, they can be turned into a ROOT macro by giving them a name which corresponds to the file name without extension, for example, if you create a file ExampleMacro.C which contains:
void ExampleMacro() { ....YOUR FUNCTION.... }
You can execute the macro by typing:
.x ExampleMacro.C
You can load the macro into the root session by typing:
.L ExampleMacro.C
Now we shall create a simple macro to analyse some of the data from the .root file provided.
Exercise 4: Compiling Code
Code can be compiled a number of ways.
1) Generate an object library from the macro code, inside the interpreter type:
.L ExampleMacro.C+
Note the use of the "+" here. Once this operation is accomplished, the macro functions are in memory and you will be able to execute them by calling from inside the interpreter:
macro_function()
2)Compile with a c++ compiler e.g. gcc. In order to make the code executable stand-alone, you must have a main function defined, for example:
int main() { ExampleMacro(); return 0; }
Can be added to the end of your macro. Now compile using:
g++ -o ExampleMacro ExampleMacro.C `root-config --cflags --libs`
or similar if you use another platform.
You can now execute the macro by typing:
./ExampleMacro
Exercise 5: Saving the histograms to a file (.root)
Exercise 6: Saving the histogram as .png, pdf ....
Exercise 7: Chaining multiple input files together
Extension Tasks
- for those already familiar with the ROOT Basics please familiarise from above but once this is done you can skip to these tasks: