Root Tips (Memo for me)
Web pages
Misc.
To run (csh/tcsh)
> setenv ROOTSYS /usr/local/root
> setenv LD_LIBRARY_PATH $ROOTSYS/lib:$LD_LIBRARY_PATH
> setenv PATH $ROOTSYS/bin:$PATH
> root
To exit from root
ROOT> .q
~/.rootrc, rootlogon.C, rootlogoff.C
ROOT> .h
All commands must be preceded by . except {}, and ?.
> [file] output redirect
2> [file] error redirect
?, .help help
.x evaluate {} in a file
.X execute function in a file.
".X ex.c" means to execute function "ex" in ex.C.
.L load file
.g show global variable
Coordinate System
1 +-------------+ ( X1, Y1, X2, Y2 )
| |
Y | |
| |
0 +-------------+
0 X 1
color
0 No filled
|
8 dark green
|
1 Black
|
9 dark blue
|
2 Red
|
10 white
|
3 Green
|
:
|
4 Blue
|
19 Gray
|
5 Yellow
|
|
6 Magenta
|
|
7 Cyan
|
|
How to make frame transparent when print out.
gStyle->SetStatColor(0);
gStyle->SetTitleColor(0);
gStyle->SetCanvasColor(0);
gStyle->SetPadColor(0);
gStyle->SetPadBorderMode(0);
gStyle->SetCanvasBorderMode(0);
gStyle->SetFrameBorderMode(0);
Make eps file
c1->Print("filename.eps", "eps");
TH1F
- hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
- Reset(); // clear all contents
- GetNbinsX()
- SetStats(kFALSE|kTRUE) # if display statistics
- gStyle->SetOptFit(1111) # prob chi-sq/dof errors values
- SetFillColor(color), SetLineColor(color)
- GetBinContent(1..nbin)
- SetBinContent(1..nbin, float?) #
float seems to work well.
- FindBin(x) # Return bin number
corresponding to x (0.. underflow, NBIN+1.. overflow)
- GetBinCenter(1..nbin), GetBinLowEdge(1..nbin) # x on
x-axis corresponding to given bin number
- Draw("option")
- "same" : same as option s of PAW
- "e1p" : error bar, ?, polymark?
- DrawCopy("option") # You want to use
this instead of Draw
- GetXaxis()->GetXmin() # GetXaxis return TAxis* object.
- SetTitle("title") # if "", then No title
- SetXTitle("x axis label")
- GetMaximum(), SetMaximum(Float_t)
- ex.)
hp11->SetMaximum(TMath::Max(hp11->GetMaximum(),
hp12->GetMaximum())*1.05);
- SetTitleSize( Float_t size, "Y" )
- SetLabelSize( Float_t size, "X" ) -> number around x(y)-axis
- fArray[1..nbin] # can access bin content as an array
- Fill( x-value ) # will return bin number
- Fill(Axis_t x, Stat_t w)
- GetEntries()
- GetMean()
- Fit
hist->Fit("gaus", "Q");
TF1 *fit= hist->GetFunction("gaus");
float mean=fit->GetParameter(1);
float sigma=fit->GetParameter(2);
- Logscale
- (TCanvas*)c1 -> SetLogy(1);
Linear scale: SetLogy(0)
- GetLogy(); Return true if Pad is set to Logy(1).
- Grid
- (TCanvas*)c1->SetGrid(1,1); grid off -> SetGrid(0,0);
- Operate
- h1->Add(h2, c2) : h1=h1+c2*h2
- h3->Add(h1, h2, c1, c2) : h3=c1*h1+c2*h2 : Can use if h3=h1
or h2?
- h1->Divide(h2) : h1=h1/h2
- h1->Multiply(h2) : h1=h1*h2
TH2F
- TH2F* hpx = new TH2F("h","test",6,0,6, 12,0,12);
- hpx->SetBinContent(Int_t binx, Int_t biny, Stat_t content);
- gStyle->SetPalette(1);
- hpx->DrawCopy("colz");
TNtuple
- ntuple = new TNtuple("ntuple", "Demo", "px:py:pz:random:i");
- Get("directory") -> (TNtuple*)f->Get("dir/path?")
- Draw("py:px", "condition", "same" )
How to `manual' loop
- N=GetEntries()
- GetEvent( 0..N-1 ) =(alias?) GetEntry( 0..N-1 )
- n=GetNvar()
- GetArgs()[ 0..n-1 ]
Branch
nt->Print()
Br 0 :var : x/F:y/F ...
struct { float x,y } var;
nt->SetBranchAddress("var", &var);
for( Int_t idx=0; idx<N; ++idx ) {
nt->GetEntry( idx );
cout << var.x << " " << var.y
<< " " << endl;
}
============================================================sample========
ana(char* fname)
{
TFile* f = new TFile(fname);
// f->ls() will help you find the path name.
TNtuple* ntpl = (TNtuple*)f->Get("ntuple");
// How to `manual' loop
Int_t n=ntpl->GetEntries();
// ntpl->Print() will help you find what variables are
cotained.
Float_t p, thb1, thb2, phb2, thd1, thd2;
ntpl->SetBranchAddress("v01", &p);
ntpl->SetBranchAddress("v02", &thb1);
ntpl->SetBranchAddress("v03", &thb2);
ntpl->SetBranchAddress("v04", &phb2);
ntpl->SetBranchAddress("v05", &thd1);
ntpl->SetBranchAddress("v05", &thd2);
// main loop
for( Int_t idx=0; idx<10; ++idx ) {
ntpl->GetEntry( idx );
cout << p << " " << thb1 <<
" " << thb2 << endl;
}
f.Close();
}
// EOF
============================================================sample========
Filling to a histogram
TH1F* hp = new TH1F("hp", "hoge", 50, 0.0, 10.0);
nt->Draw("pathlength>>hp","pathlength>0");
nt->Draw("pathlength>>hp(50, 0.0, 10.0)","pathlength>0");
TCanvas, TPad
- TCanvas* c1=new TCanvas( "c1", "HOGE", 700, 500);
- TPad* pad1 = new TPad("pad1","The pad",0.03,0.62,0.50,0.92,21);
- Clear()
- ls()
-> after TH1F* hp->Draw()
- GetPrimitive("title") .... will return TPaveText* object
- SetRightMargin(0.05)
+-----------------+
| |
|
| +---------+ |
| |
|<->|
| +---------+ |
+-----------------+
How to obtain the objects on the Canvas:
- TList* l=c1->GetListOfPrimitives();
- l->Print();
- TLine X1=0.026210 Y1=0.913690 X2=0.691532 Y2=0.577381
- Ellipse: X1= 0.385081 Y1=0.462798 R1=0.233871 R2=0.102679
- TEllipse* e1=l->At(1);
How to make Staticstics box bigger: <-- Obsolete!
c1 = new TCanvas( "c1", "HOGE",
500, 700);
pad1 = new TPad("pad1","The
pad",0.03,0.62,0.50,0.92,21)
pad2 = new TPad("pad2","The
pad",0.03,0.32,0.50,0.60,20)
pad1->Draw()
pad2->Draw()
pad1->cd()
hpx = new TH1F("hpx","This is the px
distribution",100,-4,4)
hpx->Draw()
pad2->cd()
hpy = new TH1F("hpy","This is the py
distribution",100,-4,4)
hpy->Draw()
TPaveStats
*stat1=(TPaveStats*)pad1->GetPrimitive("stats")
// 'pad1->ls()' tells what objects
are in pad1.
// 'stats', 'title' will be available
as an argument
stat1->SetX1NDC(0.5)
stat1->SetY1NDC(0.5)
// in NDC coordinates
// +--------------------+(1,1)
// |
(X2,Y2)|
// |
+---------+ |
// |
| | |
// |
+---------+ |
// | (X1,Y1)
|
// |
|
// |(0,0)
|
// +--------------------+
pad1->cd()
stat1->Draw()
TPaveStats
*stat2=(TPaveStats*)pad2->GetPrimitive("stats")
stat2->SetX1NDC(0.5)
stat2->SetY1NDC(0.5)
pad2->cd()
stat2->Draw()
--> gStyle->SetStatH(0.4);
gStyle->SetStatW(0.4);
How to divide a Canvas into Sub-Pads:
TCanvas* c1=new TCanvas( "c1", "HOGE", 700, 500);
c1->Draw();
c1->Divide(2, 3);
TPad* c1_1=(TPad*)(c1->GetPrimitive("c1_1"));
c1_1->cd();
c1_1->SetGrid(kTRUE);
hp11->Draw();
TFile
How to iterative action over items in a directory
- TFile* f = new TFile("filename")
- f->cd("dir")
- gDirectory->pwd()
- ->ls()
- ->GetNkeys(): obtain # of keys in the current dir
- ->GetListOfKeys()
- (TList*)list = gDirectory->GetListOfKey()
- list->GetSize() : return number of items
- ->At( Int_t 0..GetSize()-1 )
Create new ROOT file
TFile *tf = new TFile("output.root", "recreate");
...
hp = new TH1F("hp", "Mjj", 50, 0.0, 300.0);
...
tf->Write();
tf->Close();
TString
- TString* str = new TString()
- TString* str = new TString("Test")
- str->Data()
- (const char* 0xb4c3a0c)"Test"
TString v
v="Take"
v=="Take"
(unsigned char)1
- str->Append("XXX")
- *str+"DDDD"
TString str=TString("hoge")+"foo"
str[1]
(char 111)'o'
str.Length()
(const Int_t)7
Remove
TString v=TString("0123456")
v.Remove(1,1)
v.Data()
(const char* 0xb4c3844)"023456"
TString v=TString("0123456")
TString s=v(1,2)
s.Data()
(const char* 0xb86ee54)"12
TPostScript
111
|
ps
|
Portrait
|
112
|
ps
|
Landscape
|
113
|
eps
|
|
- TPostScript* ps =new TPostScript("map.ps", 111);
TCanvas* c1=new TCanvas( "c1",
"HOGE", 700, 500);
TPostScript* ps =new TPostScript("map.ps", 112);
...
c1->Update();
ps->Clear();
...
ps->Close();
Easy way to plot function
- (new TF1("fun1","(1-x)*sqrt(1-x*x)",-1,1))->Draw();
- (new TF1("fun1","1/sqrt(2*TMath::Pi())*exp(-x*x/2)",-3,3))->Draw();
- (new
TF2("fun2","(175/80)**2*(1-y)*(1-x**2)+(1+y)*(1-x)**2",-1,1,-1,1))->Draw("lego")
TF1* f=new
TF1("fun1","[0]*(2-[1]+[1]*x*x)/(2-[1]+[1]/3)",0,1);
f->SetParameters(val0,val1);
.. or ..
f->SetParameter(0, val0);
f->SetParameter(1, val1);
...
f->Draw(); DrawCopy("same");
Combination of pre-implemented functions
TF1 * f=new TF1("myfunc", "expo*pol1(2)", 0, 1);
f->SetParameters(1,2,3,4);
f->Draw();
pol1(2) means that the index of pol1 starts from [2], since expo has two
parameters [0] and [1]. This is equivalent to
TF1 * f2=new TF1("myfunc", "exp(1+2*x)*(3+4*x)", 0, 1);
f2->Draw();
TText, etc
TText* txt=new TText(0.5, 0.5,
"hoge");
txt->SetNDC(kTRUE); // <- use NDC coordinate
txt->SetTextSize(0.04);
txt->Draw();
TLatex* tlx=new TLatex(0.5, 0.5,
"#[]{#frac{x^{2}_{i}}{#sqrt{#hat{y}}}}");
tlx->SetNDC(kTRUE); // <- use NDC coordinate
tlx->SetTextSize(0.04);
tlx->Draw();
TLine* l=new TLine(xpos, 0.0 , xpos, hp->GetMaximum());
l->Draw();
TMath
IsNaN(x) ... check if x is NaN such as sqrt(-1).
Finite(x) ... check if x is finite, not inf such as
1e+999
TGraph
//
XY Graph // float xp[n], yp[n]
c1 = new TCanvas("c1", "foo",200,10,600,400);
c1->SetGrid();
TH2F *hr = new TH2F("hr", "hoge", 2, XMIN-XMARGIN, XMAX+XMARGIN, 2,
YMIN-YMARGIN, YMAX+YMARGIN);
hr->SetStats(kFALSE);
hr->Draw();
TGraph* g = new TGraph(n, xp, yp);
g->SetMarkerColor(kBlue);
g->SetMarkerStyle(2);
g->Draw("P");
Histograme as a function of time
hpx = new TH1F("hpx","hist", 31, 52149600.0, 54828000.0);
hpx->SetXTitle("Date");
TAxis *ax=hpx->GetXaxis();
ax->SetTitleColor(1); // should be by default!
gStyle->SetTimeOffset(0);
ax->SetTimeDisplay(kTRUE);
ax->SetTimeFormat("%b %d,%y");
hpx->Draw();
Cosmetics
gStyle->SetHistLineWidth(3); //
Default:1(GetHistLineWidth) --- Line width of histogram
gStyle->SetLineWidth(2); //
Default:1 --- Line width of axis.
gStyle->SetTitleH(0.09);
gStyle->SetTitleW(0.65); // Default: 0.0, 0.0 ---
Make title box larger.
gStyle->SetStatH(0.4);
gStyle->SetStatW(0.4); // Default: 0.16 Make stat box larger.
TH1F* h;
h->SetTitle("title");
h->SetXTitle("x axis label"); h->SetYTitle("y axis label");
h->SetTitleSize( 0.08, "X" ); h->SetTitleOffset(0.01, "X");
h->SetTitleSize( 0.08, "Y" ); h->SetTitleOffset(0.01, "Y");
h->SetLabelSize( 0.08, "X" ); h->SetLabelOffset(0.01, "X");
h->SetLabelSize( 0.08, "Y" ); h->SetLabelOffset(0.01, "Y");
h->SetNdivisions( 505, "X" ); // Default: 510 ---
change the intervals of ticks on an axis.
gStyle->SetOptStat(ourmen)
(default = 001111)
n = 1; name of histogram is printed
e = 1; number of entries printed
m = 1; mean value printed
r = 1; rms printed
u = 1; number of underflows printed
o = 1; number of overflows printed
gStyle->SetOptFit(pcev)
(default = 0111)
v = 1; print name/values of parameters
e = 1; print errors (if e=1, v must be 1)
c = 1; print Chisquare/Number of degress of
freedom
p = 1; print Probability
Back
Last modified at Sat Sep 28 02:21:26 JST 2002 by yuji@fnal.gov