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