Root Tips (Memo for me)

TH1F
TH2F
TNtuple
TCanvas,TPad
TFile
TString
TPostScript
TF1,TF2
TText
TGraph
TMath

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

 TH2F


TNtuple

How to `manual' loop

  1. N=GetEntries()
  2. GetEvent( 0..N-1 ) =(alias?) GetEntry( 0..N-1 )
  3. n=GetNvar()
  4. 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

         +-----------------+
         |       |         |
         |   +---------+   |
         |   |         |<->|
         |   +---------+   |
         +-----------------+

How to obtain the objects on the Canvas:

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

  1. TFile* f = new TFile("filename")
  2. f->cd("dir")
  3. gDirectory->pwd()
  4. list->GetSize() : return number of items
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 v
v="Take"
v=="Take"
(unsigned char)1

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

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

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