From $ROOTSYS/tutorials/io/dirs.C

void dirs() {
// .........................macro dirs.C............................
// This macro illustrates how to create a hierarchy of directories
// in a Root file.
// 10 directories called plane0, plane1, plane9 are created.
// Each plane directory contains 200 histograms.
//
// Run this macro (Note that the macro delete the TFile object at the end!)
// Connect the file again in read mode:
//   Root > TFile *top = new TFile("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.
//
//  You can see the begin_html <a href="gif/dirs.gif" >picture of the browser </a> end_html
//
//    Instead of using the browser, you can also do:
//   Root > tof->cd();
//   Root > plane3->cd();
//   Root > h3_90N->Draw();
//Author: Rene Brun

    // 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
   TRandom r;
   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;
}

 dirs.C:1
 dirs.C:2
 dirs.C:3
 dirs.C:4
 dirs.C:5
 dirs.C:6
 dirs.C:7
 dirs.C:8
 dirs.C:9
 dirs.C:10
 dirs.C:11
 dirs.C:12
 dirs.C:13
 dirs.C:14
 dirs.C:15
 dirs.C:16
 dirs.C:17
 dirs.C:18
 dirs.C:19
 dirs.C:20
 dirs.C:21
 dirs.C:22
 dirs.C:23
 dirs.C:24
 dirs.C:25
 dirs.C:26
 dirs.C:27
 dirs.C:28
 dirs.C:29
 dirs.C:30
 dirs.C:31
 dirs.C:32
 dirs.C:33
 dirs.C:34
 dirs.C:35
 dirs.C:36
 dirs.C:37
 dirs.C:38
 dirs.C:39
 dirs.C:40
 dirs.C:41
 dirs.C:42
 dirs.C:43
 dirs.C:44
 dirs.C:45
 dirs.C:46
 dirs.C:47
 dirs.C:48
 dirs.C:49
 dirs.C:50
 dirs.C:51
 dirs.C:52
 dirs.C:53
 dirs.C:54
 dirs.C:55
 dirs.C:56
 dirs.C:57
 dirs.C:58
 dirs.C:59
 dirs.C:60
 dirs.C:61
 dirs.C:62
 dirs.C:63
 dirs.C:64
 dirs.C:65
 dirs.C:66
 dirs.C:67
 dirs.C:68
 dirs.C:69
 dirs.C:70
 dirs.C:71
 dirs.C:72
 dirs.C:73
 dirs.C:74
 dirs.C:75
 dirs.C:76