You are here

1. Extracting an object from a TFile

File can be local on your disk or remote accessible for instance through http:

root [0] TFile* file = TFile::Open("http://someserver/somefile.root");

You can list the objects inside the file using .ls:

root [1] .ls
TFile**     hsimple.root    Demo ROOT file with histograms
 TFile*     hsimple.root    Demo ROOT file with histograms
  KEY: TH1F hpx;1   This is the px distribution
  KEY: TH2F hpxpy;1 py vs px
  KEY: TProfile hprof;1 Profile of pz versus px
  KEY: TNtuple  ntuple;1    Demo ntuple

You can access objects inside the files by their names, either by using this name as a variable on the prompt:

root [2] hpx->Draw();

In regular code you should use TFile::GetObject():

root [3] TH1* readThis = 0;
root [4] file->GetObject("hpx", readThis);

Now let's try this out: get the TTree called "MyTree" from a file located at http://root.cern.ch/root/files/tutorials/mockupx.root Note that you don't need to download it with your browser: you just specify the http:// location as an argument to TFile::Open(). Once you have the TTree object, call Print() on it to see its structure.