Ubuntu 14.04 + ROOT v.5.34
Goal: learn the basic I/O ( Tfile class)
we will mostly use CINT to demonstrate.
----------------------------------------------------------------
//--------create a pointer that can reference to TFile class---
TFile* f = new TFile("demo.root")
f->close()
//--------create instance from TFile class-----
TFile f("demo.root","recreate"); // to overwrite
TFile f("demo.root",); // to read
TFile f("demo.root", "update"); // to append
f.Close()
f.ls() // to list what in side this TFile object “f”
tip: use tab to auto complete the input.
tip: ROOT file is independent of .root file extension.
----------------------------------------------------------------
Example 1: save objects to root file
TFile* f = new TFile ("test.root","recreate");
f->ls()
TH1F h0("h0","histro0",100,-10,10)
TH1F h1("h1","histro1",100,-10,10)
h0.FillRandom("gaus",100)
h1.FillRandom("gaus",100)
h0.Write()
h1.Write()
f->ls()
// you can see two new histograms in the test.root file.
// let update h0
h0.FillRandom("gaus",200)
h0.Draw()
h0.Write()
f->ls()
f->close()
CINT retrieves the one with the highest cycle number.
----------------------------------------------------------------
TFile:: mkdir("char") // to mkdir
TFile:: cd() // to change dir
TFile:: delete() // to delete element or dir
TFile:: ls("-m") // to see objects in memory.
gDirectory->pwd() // to show the current path.
Example 2, to make a dir
TFile* f1 = new TFile ("test1.root","recreate");
TFile* f2 = new TFile ("test2.root","recreate");
gDirectory->pwd()
f1->cd()
f1->ls()
f1->mkdir("new_dir")
f1->ls()
f1->cd("new_dir”)
TH1F h0("h0","histro0",100,-10,10)
h0.FillRandom("gaus",100)
h0.Write()
f1->ls()
new TBrowser
f1->Close()
All histograms and trees are created in the current director
ROOT File provides sequential access and direct access.
Example 3, to retrieve objects.
TFile f("demo.root")
f.GetListOfKeys()->Print()
f.ls()