Logo ROOT   6.18/05
Reference Guide
dirs.C File Reference

Detailed Description

View in nbviewer Open in SWAN This macro illustrates how to create a hierarchy of directories in a Root file.

Ten directories called plane0, plane1, ..., plane9 are created. Each plane directory contains 200 histograms. Note that the macro deletes the TFile object at the end! Connect the file again in read mode:

Root [0] TFile top("top.root");

The hierarchy can be browsed by the Root browser as shown below

Root TBrowser b;

Click on the left pane on one of the plane directories. This shows the list of all histograms in this directory. Double click on one histogram to draw it (left mouse button). Select different options with the right mouse button. Instead of using the browser, you can also do:

Root > tof->cd();
Root > plane3->cd();
Root > h3_90N->Draw();
void dirs() {
// create a new Root file
TFile *top = new TFile("top.root","recreate");
// create a subdirectory "tof" in this file
TDirectory *cdtof = top->mkdir("tof");
cdtof->cd(); // make the "tof" directory the current directory
// create a new subdirectory for each plane
const Int_t nplanes = 10;
const Int_t ncounters = 100;
char dirname[50];
char hname[20];
char htitle[80];
Int_t i,j,k;
TDirectory *cdplane[nplanes];
TH1F *hn[nplanes][ncounters];
TH1F *hs[nplanes][ncounters];
for (i=0;i<nplanes;i++) {
sprintf(dirname,"plane%d",i);
cdplane[i] = cdtof->mkdir(dirname);
cdplane[i]->cd();
// create counter histograms
for (j=0;j<ncounters;j++) {
sprintf(hname,"h%d_%dN",i,j);
sprintf(htitle,"hist for counter:%d in plane:%d North",j,i);
hn[i][j] = new TH1F(hname,htitle,100,0,100);
sprintf(hname,"h%d_%dS",i,j);
sprintf(htitle,"hist for counter:%d in plane:%d South",j,i);
hs[i][j] = new TH1F(hname,htitle,100,0,100);
}
cdtof->cd(); // change current directory to top
}
// Fill histograms
for (i=0;i<nplanes;i++) {
cdplane[i]->cd();
for (j=0;j<ncounters;j++) {
for (k=0;k<100;k++) {
hn[i][j]->Fill(100*r.Rndm(),i+j);
hs[i][j]->Fill(100*r.Rndm(),i+j+k);
}
}
}
// save histogram hierarchy in the file
top->Write();
delete top;
}
ROOT::R::TRInterface & r
Definition: Object.C:4
int Int_t
Definition: RtypesCore.h:41
virtual TDirectory * mkdir(const char *name, const char *title="")
Create a sub-directory "a" or a hierarchy of sub-directories "a/b/c/...".
Describe directory structure in memory.
Definition: TDirectory.h:34
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:497
virtual TDirectory * mkdir(const char *name, const char *title="")
Create a sub-directory "a" or a hierarchy of sub-directories "a/b/c/...".
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
Write memory objects to this file.
Definition: TFile.cxx:2337
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3258
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
Author
Rene Brun

Definition in file dirs.C.