C C omputational Methods in Experimental Particle Physics
KaiFeng Chen
National Taiwan University
PAGE 1
Lecture 04: HEP Tools – ROOT (Part I)
What is ROOT?
Surely we are not talking about this:
ROOT: An Introduction
An analysis framework for large scale data handling, mostly used by highenergy physics, astrophysics, nuclear physics, including both experiments & theory.
It's also an open source project.
Started in 1995, with 8 full time developers at CERN, plus Fermilab, Agilent Tech, Japan, MIT (one each).
Large number of parttime developers:
Users participate its development.
Available (incl. source) under GNU/LGPL.
PAGE 3
ROOT: An Introduction (II)
The ROOT can deal with:
An efficient(?) data storage, access and query system up to PetaBytes level.
Advanced statistical analysis tools and algorithms
(multi dimensional histogramming, fitting, minimization and cluster finding, etc.)
Scientific visualization: 2D and 3D graphics, convert the output to Images (jpeg/png/gif), Postscript, PDF, LateX
Geometrical modeller (to show some 3D detector view)
PROOF parallel query engine (MPI...)
Some Fancy(?) Figures
PAGE 5
A Little Handson Show
I'm going to show you a very easy/little thing that can do with the ROOT framework:
{ TH1F *h1 = new TH1F("h1","A Random Histogram",100,0.,1.);
TF1 *f1 = new TF1("f1","[0]+[1]*x+[2]*gaus(2)");
f1->SetParameters(2.,-1.0,2.5,0.5,0.05);
h1->FillRandom("f1",2000);
h1->Fit("f1");
}
A direct run of this piece of code will generate this plot (with a fit)
[example-01.cc]
A Little Handson Show
PAGE 7
We can already play around with this figure easily:
Some Fancy(?) Stuff
3D views:
TF2 *f2 = new TF2("f2","sin(x)*sin(y)",0,10,0,10);
f2->Draw();
Some Fancy(?) Stuff
PAGE 9
View with OpenGL:
Detector Geometry &
Event Display
Mathematics Utilities
PAGE 11
Mathematics Utilities
Most of the computing tasks we have covered in this
lecture can be actually made with few root commands.
e.g.: our homework #1:
r=
2y=
2−x2 A=∫
01
2−x2dx1
=A×4−2
TF1 *f1 = new TF1("f1","sqrt(2.-x*x)");
double pi = f1->Integral(0.,1.)*4.-2.;
Ref: 3.1415926535897931 Calc: 3.1415926535897940 diff: 0.0000000000000009
[example-02.cc]
Multivariate Analysis Support
PAGE 13
The question: how could we distinguish cats from dogs by a computer program?
Multivariate Analysis Support
The question: how could we distinguish cats from dogs?
Cats are generally smaller then dogs, so we could measure their sizes.
Cats are generally lighter then dogs, so we could also measure their weights.
They have different “barking” sound – so we could analysis their voice.
They have different favorite food – so we could feed them with fish and bone and see if they take them.
Cats can safely fall down from a high place, so we could perform a (cruelly) freefalling test.
Multivariate Analysis Support
PAGE 15
ROOT includes
a tool TMVA to do multivariate analysis.
It supports
Neural Network, Likelihood methods, Boost decision trees, etc.
(size?) (weight?) (food favor?) (free falling test?)
Multi CPU/Cluster Support
Q: I just spend a big memory to buy a multicore cpu, could I use it in my work?
A: Yes, there is some parallel working supports in RROOF.
Multi CPU/Cluster Support
PAGE 17
Surely we can also speed out the work with more and more CPUs...
More features to be discovered...
There are more features to be discovered from the web:
http://root.cern.ch
We are going to show you
how to use this tool to carry out your own work during in following lecture time.
Beginning of Everything:
Get a working copy somewhere
PAGE 19
As you connect to their web, you can already find a big icon showing in the middle:
Just go ahead!
Beginning of Everything:
Get a working copy somewhere
For Linux/Solaris/Mac OSX:
Just download the right version corresponding to your gcc version.
For the people who is not using scientific linux, there may be some missing
libraries, but in principle they can be solved easily.
Get a working copy somewhere
PAGE 21
Unfortunately, I have never
tried a windows version before.
Probably you have to figure it out by yourself...
Startup Under Linux
> cd somewhere/
> tar xfz root_v5.26.00.Linux-slc5-gcc4.3.tar.gz
> ls root/
bin etc geom include LICENSE man test tutorials cint fonts icons lib macros README tmva
Unpack the tar ball:
> setenv ROOTSYS somewhere/root
> setenv PATH "${ROOTSYS}/bin:$PATH"
> setenv LD_LIBRARY_PATH "${ROOTSYS}/lib"
Setup the environment (for tcsh):
For bash, you have to replace setenv by export, and add an “=”,
Then...Launch It!
PAGE 23
> root
*******************************************
* * * W E L C O M E to R O O T * * * * Version 5.26/00 14 December 2009 * * * * You are welcome to visit our Web site * * http://root.cern.ch * * * *******************************************
ROOT 5.26/00 (trunk@31882, Dec 14 2009, 20:18:36 on linux) CINT/ROOT C/C++ Interpreter version 5.17.00, Dec 21, 2008 Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root[0] _
ROOT as a pocket calculator
root [0] exp(10)
(const double)2.20264657948067179e+04 root [1] sqrt(28)
(const double)5.29150262212918143e+00 root [2] double x = 0.54;
root [3] x
(double)5.40000000000000036e-01 root [4] x+x*x+x*x*x
(double)9.89064000000000165e-01 root [5] printf("%f\n",sin(x));
0.514136
root [6] for(int i=0;i<5;i++) cout << i << endl;
0 12 3 4
Simple But Important Operations
PAGE 25
root [0] .q
The most important command: how to quit root:
root [0] .h
A command list; help page
root [0] .csh
Go to csh temporary
root [0] .! <command>
Execute shell command
root [0] gROOT->Reset();
Reset ROOT
ROOT Prompt
Probably you already noticed that, the root prompt is
actually based on C/C++, except those special commands starting with a “.”
This interactive prompt is called CINT (=C/C++ interpreter),
in principle all your codes can be written in C/C++ and execute them directly.
Basically there are some variations (python, ruby, etc) for those who does not like to use C/C++ as a script language.
Why not GUI interface? There are some fine GUI for tuning your figures, fits, others, but it is still recommended to go the work in scripts/codes.
A line from ROOT developers: ROOT is not an office suite, but a programming framework!
PAGE 27
Running under CINT
A minimum example with a function:
void example_03(double x)
{ printf("x*x = %g\n",x*x);
}
[example-03.cc]
root [0] .x example-03.cc(2.5) x*x = 6.25
So, let's run it:
root [0] .L example-03.cc root [1] example_03(2.5) x*x = 6.25
This is equivalent to:
NOTE: the entry point is not main() but example_03()
Running under CINT
A heavier example:
#include <TRandom3.h>
#define M_SIZE 500 void do_some_calc() {
double A[M_SIZE][M_SIZE],B[M_SIZE][M_SIZE],C[M_SIZE][M_SIZE];
TRandom3 rnd;
for(int i=0;i<M_SIZE;i++) for(int j=0;j<M_SIZE;j++) {
A[i][j] = rand(); B[i][j] = rnd.Uniform();
}
for(int i=0;i<M_SIZE;i++) for(int j=0;j<M_SIZE;j++) { C[i][j] = 0.;
for(int k=0;k<M_SIZE;k++)
C[i][j] += A[k][j]*B[i][k];
} }
void example_04() { do_some_calc(); }
[example-04.cc]
PAGE 29
It is a working code, but...
Direct execute, well, it runs, but it also takes as long as forever...
root [0] .x example-04.cc
For a boost in speed, it's recommended to compile it first, and then execute it:
root [0] .x example-04.cc+
(2:22.63)
(0:05.02)
If your code has been compiled already, it just runs!
root [0] .x example-04.cc+ (0:00.42)
This running mode is not called CINT anymore, but named as ACLiC (Automatic Compiler of Libraries for CINT)
CINT versus ACLiC
Interpreter versus (pre)compiled.
Slower versus faster.
Run with partial code versus coding with full headers, C/C++ rules.
Easy object allocation, freely versus
a full tight code (prepare for segment violation!).
No grammar checks versus a full check before execute.
General guide lines:
If your code needs lots of for/while loops, just compile it.
If you just want to write a lazy script, keep it as CINT.
CINT already does a good job for most of plotting/graphics works.
Restriction of ACLiC: need to include full headers.
PAGE 31
Assessment
Get handson trials:
Install/prepare a working ROOT environment.
Try the examples given in the previous lectures here, running under CINT and/or ACLiC modes.
Histogram Basic Operations
Create an empty histogram:
root [0] TH1F* h1 = new TH1F("h1","a histogram",100,-5.,5.);
Object name
(in your code) Object name (root recorded)
Histogram title
# of bins Min. and Max.
Clone a copy:
root [1] TH1F* h2 = (TH1F*)h1->Clone("h2");
List the objects we just created:
root [2] .ls
OBJ: TH1F h1 a histogram : 0 at: 0x8b42fc0
PAGE 33
Histogram Basic Operations
Let's fill it with random numbers:
root [3] TRandom3 rnd(1234);
root [4] for(int i=0;i<10000;i++) h1->Fill(rnd.Uniform(-5.,5.));
root [5] for(int i=0;i<10000;i++) h2->Fill(rnd.Gaus(0.,1.));
Generator class, seed = 1234 (Mersenne Twister)
Uniform generator (min,max) &
Gaussian generator (mean,width) Loop & fill for 10,000 times
Plot them:
root [6] h2->Draw();
root [7] h1->Draw();
root [8] h1->GetYaxis()->SetRangeUser(0.,150.);
root [9] h1->Draw()
Histogram Basic Operations
Direct operations: (+/–/x/÷)
root [10] TH1F* h3 = (TH1F*)h1->Clone("h3");
root [11] h3->Add(h2);
root [12] TH1F* h4 = (TH1F*)h1->Clone("h4");
root [13] h4->Add(h2,-1.);
root [14] TH1F* h5 = (TH1F*)h1->Clone("h5");
root [15] h5->Multiply(h1);
root [16] TH1F* h6 = (TH1F*)h1->Clone("h6");
root [17] h6->Divide(h1);
Normalize it:
root [18] h1->SetNormFactor();
Draw the error bar
PAGE 35
Some Drawing Options
Set line width:
root [20] h3->SetLineWidth(3);
root [21] h3->Draw();
Set line color
root [22] h3->SetLineColor(50);
Set filling color
root [24] h3->SetFillColor(50);
Set line style
root [23] h3->SetLineStyle(50);
2D Histograms
Working with 2D histograms is also very straightforward:
TH2F* h2d = new TH2F("h2d","a 2d histogram",20,-5.,5.,20,-5.,5.);
# of bins (x) Min(x) and Max(x)
# of bins (y) Min(y) and Max(y)
for(int i=0;i<10000;i++) h2d->Fill(rnd.Gaus(0,1),rnd.Gaus(0,1));
Let's fill it with some random numbers:
h2d->Draw();
Just plot it:
PAGE 37
More Drawing Options
h2d->Draw("box"); h2d->Draw("lego"); h2d->Draw("lego2");
h2d->Draw("cont"); h2d->Draw("surf"); h2d->Draw("surf4");
Functions
f x=xx
2 x
3 x
4 sin5 xexp x
TF1* f1 = new TF1("f1","x+x^2+x^3+x^4+sin(5*pi*x)+exp(x)",0.,1.);
free function form min and max function object name
f1->Draw();
How should I do if I just want to plot a function, e.g.:
Just plot it:
f1->Eval(0.5);
Get the function value at x=0.5
PAGE 39
Functions
Then, we could practice those “handson shows” at the beginning of this lecture:
Derivative x = 0.5:
root [30] f1->Derivative(0.5);
(Double_t)4.89872127069923557e+00
Integration in an interval, e.g. [0,1]:
root [31] f1->Integral(0.,1.);
(Double_t)3.12893911626589505e+00
Generate a Random number according to this function:
root [32] f1->GetRandom();
(Double_t)9.99879725496876204e-01
Fill Random Histogram
We could fill a histogram with the random numbers generated according to such a specific function:
Create a 1D histogram and fill:
root [33] TH1F* h1 = new TH1F("h1","a histogram",100,0.,1.);
root [34] for(int i=0;i<10000;i++) h1->Fill(f1->GetRandom());
This is the same as
root [35] h1->FillRandom();
If you draw it:
PAGE 41
2D Functions
The 2D function is not really different from the 1D functions:
(as we already see the example earlier)
root [36] TF2* f2 = new TF2("f2","sin(x)*sin(y)",0,10,0,10);
root [37] f2->SetFillColor(50);
root [38] f2->Draw("lego1");
root [39] f2->Draw("surf4");
Let's put them together!
Step1: Generate some data and save them into a
text file (yes, this step can be replaced by any other program!).
Step 2: Read those data back, and draw it.
void example_05() { TRandom3 rnd;
FILE *fpout = fopen("whatever_data.txt","w");
for(int i=0;i<1000;i++)
fprintf(fpout,"%f\n",rnd.Gaus());
fclose(fpout);
TH1F *h1 = new TH1F("h1","whatever data",100,-5.,5.);
FILE *fpin = fopen("what_ever_data.txt","r");
float val;
while(1) {
int r = fscanf(fpin,"%f",&val);
if (r!=1) break;
h1->Fill(val);
}fclose(fpin);
[example-05.cc]
PAGE 43
Assessment
Read some of the data which is really related to your own work, and produce some nice figures with ROOT.
Practice:
Draw some specific function and generate the corresponding distributions with random numbers.
Try out different drawing options.