First Steps: Files

Overview

Creating and opening ROOT files. Writing and reading objects.

The Lesson

Start by reading about TFile and TKey in the ROOT Crib which describes files and the keys used to access the objects they contain. One very important point to take note of: we cannot write out our Quad object to a TFile As was explained in the Classes lesson, if you want to do that you must follow the instructions given in Adding Your own Classes to ROOT

Now we will put it into practice. You may find it helpful to open a second browser window looking at TFile and TKey. Create a file called:-

file_write.cxx containing:- { gROOT->Reset(); TFile *MyFile = new TFile("hist.root","RECREATE"); if ( MyFile->IsOpen() ) cout << "File opened successfully" << endl; TH1F *hist = new TH1F("hist","A histogram",100,0.,10.); hist->Fill(1.); hist->Fill(2.); hist->Fill(2.); hist->Fill(3.); hist->Draw(); hist->Write(); MyFile->Print(); } This creates, or recreates if already existing, the file hist.root and checks that is was opened properly. Next a 1D histogram is created, as explained in Histograms lesson. The histogram is asked to Draw itself. This is another interesting shortcut. ROOT needs a TCanvas to display the histogram but we have omitted to create one. So ROOT kindly produces a default one but warns us. Next the histogram is told to Write itself, which it does to the current TFile. Finally the TFile object MyFile is asked for a summary of what it contains.

Start up ROOT and run this macro. Now close the file:-

MyFile->Close(); When you do this the histogram will disappear from the TCanvas. Our histogram was "owned" by the TFile once it was written to it, and closing the TFile deleted it. Strictly speaking we did not need to close the TFile; ROOT would do it automatically when it exited, but we should be tidy! Now and exit ROOT and create the file:- file_read.cxx containing:- { gROOT->Reset(); TFile *MyFile = new TFile("hist.root","OLD"); MyFile->GetListOfKeys()->Print(); TH1F *hist = (TH1F*) MyFile->Get("hist"); hist->Draw(); } This time we create a TFile object to connect to an existing file and ask it for its list of keys which is then asked to print itself. This produces something like:- TKey Name = hist, Title = A histogram, Cycle = 1 showing that there is a TKey called hist. Note the cycle number, which follows the VMS concept of version numbers. If you use the same name to write multiple objects to a file, you don't loose any, they each get their own version number. The TFile is asked to retrieve our histogram object by name. Recall that the concept of access by name was discussed in the Inheritance lesson. Finally, to demonstrate that this really is our histogram, we ask it to Draw itself. Of course there is nothing to stop us adding more data to it and writing back to a TFile, a nice demonstration of how autonomous the parts of an OO program are!

Summary


Go Back to the The First Steps Top Page


If you have any comments about this page please send them to Nick West