Institut f¨ur Experimentelle Kernphysik (EKP) Prof. Dr. M. Feindt, Dr. T. Kuhr
M. R¨ohrken, B. Kronenbitter, Dr. A. Zupanc
21. October 2010
Ubungen zu Moderne Methoden der Datenanalyse ¨ Some useful ROOT commands
1D histograms:
Create histogram TH1F* h1 = new TH1F(“name”,”title”,nbin, xmin, xmax);
Fill histogram h1→ Fill(x, weight = 1);
Set entry of i-th bin h1→ SetBinContent(i,x);
Set error of i-th bin h1→ SetBinError(i,x);
Get entry of i-th bin h1→ GetBinContent(i,x);
Draw histogram h1→ Draw();
Reset histogram h1→ Reset();
Write histogram content
into an array Float t* content = h1→ GetArray();
Set maximum of y-axis h1→ SetMaximum(ymax);
Set minimum of y-axis h1→ SetMaximum(ymin);
2
2D histograms:
Create histogram TH2F* h2
= new TH2F(“name”,”title”,nbinx, xmin, xmax,nbiny, ymin, ymax);
Fill histogram h2→ Fill(x,y, weight = 1);
Draw histogram h2→ Draw();
Useful draw options h2→ Draw(“BOX”), h2 →Draw(“SAME”);
Profiles of scatter plots are sometimes more demonstrative Create profile TProfile* tp
= new TProfile(“name”,”title”,nbinx, xmin, xmax,nbiny, ymin, ymax);
Filling and drawing analog to TH2F
3
Canvas:
Create canvas TCanvas* c1 = new TCanvas(“c1”,”c1”,800,600);
Draw histogram h1 on canvas c1 →cd(); h1 → Draw(); c1 →Update();
Divide canvas c1 →Divide(3,2);
Draw h1 in pad c1 →cd(1); h1 → Draw(); c1 → Update();
Functions:
Create functions TF1* f1 = new TF1(“f1”,”sin(x)/x”,xmin, xmax);
Create parameterized TF1* f2
functions [0],[1],[2] = new TF1(“f2”,”[0]*sin([1]*x)/x + [2]”,xmin, xmax);
Set parameter Double t par[3];
f2 → SetParameter(par);
Get i-th parameter Double t pari = f2 → GetParameter(i);
Get error Double t erri = f2 → GetParError(i);
Fit histogram with function h1 →Fit(“f1”);
Fit within the definiton range of the fct. h1 →Fit(“f1”,”R”);
Fit within the range x1,x2 h1 →Fit(“f1”,””,””,x1,x2);
Random generators:
Preset generator is already initialized
with time-depended start value (seed = 0) Change preset generator delete gRandom;
(faster and higher periodicity) gRandom = new TRandom3(seed = 0);
predefined random distribution gaus(E=0, σ = 1), uniform(x1, x2),
landau(E = 0, σ = 1), poisson(E), binomial(n,p) Fill histogram with gaus distributed
random numbers h1 →FillRandom(“gaus”,n);
Fill histogram with random numbers
according to fct. f1 (TH1F) h1 →FillRandom(“f1”,n);
gStyle / gSystem:
Show fitparamter in statistic box gStyle → SetOptFit(11....1);
Additional paramter in statistic box gStyle → SetOptStat(11....1);
Include pause (t in ms) gSystem → Sleep(t);
4
Ntuple:
Create ntuple (x1, x2, ...xn(n <15)) TNtuple* nt
= new TNtuple(“name”,”title”,x1 :x2 :...:xn);
Fill ntuple nt → Fill(x1, x2, ...xn);
Draw a variable of the ntuple nt → Draw(“x1”);
Project a variable of the ntuple
into a histogram nt → Project(“h1”, “x1”);
Draw a variable with cuts nt → Draw(“x1”,”x2 <3”);
Draw two variables nt → Draw(“x1 :x2”);
Tree:
As far as TTree is used in the exercises, they can be used analog to TNtuple (draw, project, ...)
File:
Create file TFile* file = new TFile(“file.root”);
Write histogram h1 into file file → cd();
h1→ Write();
Close file file → Close();
Read histogram from file TH1F* h1 = (TH1F*) file → Get(“histoname”);
Read tree from file TTree* t1 = (TTree*) file →Get(“treename”);