ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
dirs.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_io
3 /// This macro illustrates how to create a hierarchy of directories
4 /// in a Root file.
5 /// Ten directories called plane0, plane1, ..., plane9 are created.
6 /// Each plane directory contains 200 histograms.
7 /// Note that the macro deletes the TFile object at the end!
8 /// Connect the file again in read mode:
9 /// ~~~{.bash}
10 /// Root [0] TFile top("top.root");
11 /// ~~~
12 /// The hierarchy can be browsed by the Root browser as shown below
13 /// ~~~{.bash}
14 /// Root TBrowser b;
15 /// ~~~
16 /// Click on the left pane on one of the plane directories.
17 /// This shows the list of all histograms in this directory.
18 /// Double click on one histogram to draw it (left mouse button).
19 /// Select different options with the right mouse button.
20 /// Instead of using the browser, you can also do:
21 /// ~~~{.bash}
22 /// Root > tof->cd();
23 /// Root > plane3->cd();
24 /// Root > h3_90N->Draw();
25 /// ~~~
26 /// \macro_code
27 ///
28 /// \author Rene Brun
29 
30 void dirs() {
31  // create a new Root file
32  TFile *top = new TFile("top.root","recreate");
33 
34  // create a subdirectory "tof" in this file
35  TDirectory *cdtof = top->mkdir("tof");
36  cdtof->cd(); // make the "tof" directory the current directory
37 
38  // create a new subdirectory for each plane
39  const Int_t nplanes = 10;
40  const Int_t ncounters = 100;
41  char dirname[50];
42  char hname[20];
43  char htitle[80];
44  Int_t i,j,k;
45  TDirectory *cdplane[nplanes];
46  TH1F *hn[nplanes][ncounters];
47  TH1F *hs[nplanes][ncounters];
48  for (i=0;i<nplanes;i++) {
49  sprintf(dirname,"plane%d",i);
50  cdplane[i] = cdtof->mkdir(dirname);
51  cdplane[i]->cd();
52  // create counter histograms
53  for (j=0;j<ncounters;j++) {
54  sprintf(hname,"h%d_%dN",i,j);
55  sprintf(htitle,"hist for counter:%d in plane:%d North",j,i);
56  hn[i][j] = new TH1F(hname,htitle,100,0,100);
57  sprintf(hname,"h%d_%dS",i,j);
58  sprintf(htitle,"hist for counter:%d in plane:%d South",j,i);
59  hs[i][j] = new TH1F(hname,htitle,100,0,100);
60  }
61  cdtof->cd(); // change current directory to top
62  }
63 
64  // .. fill histograms
65  TRandom r;
66  for (i=0;i<nplanes;i++) {
67  cdplane[i]->cd();
68  for (j=0;j<ncounters;j++) {
69  for (k=0;k<100;k++) {
70  hn[i][j]->Fill(100*r.Rndm(),i+j);
71  hs[i][j]->Fill(100*r.Rndm(),i+j+k);
72  }
73  }
74  }
75 
76  // save histogram hierarchy in the file
77  top->Write();
78  delete top;
79 }
80 
virtual TDirectory * mkdir(const char *name, const char *title="")
Create a sub-directory and return a pointer to the created directory.
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
virtual TDirectory * mkdir(const char *name, const char *title="")
Create a sub-directory and return a pointer to the created directory.
Definition: TDirectory.cxx:955
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
ROOT::R::TRInterface & r
Definition: Object.C:4
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:2248
Describe directory structure in memory.
Definition: TDirectory.h:44
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:433