dirs.C

Go to the documentation of this file.
00001 void dirs() {
00002 // .........................macro dirs.C............................
00003 // This macro illustrates how to create a hierarchy of directories
00004 // in a Root file.
00005 // 10 directories called plane0, plane1, plane9 are created.
00006 // Each plane directory contains 200 histograms.
00007 //
00008 // Run this macro (Note that the macro delete the TFile object at the end!)
00009 // Connect the file again in read mode:
00010 //   Root > TFile *top = new TFile("top.root");
00011 // The hierarchy can be browsed by the Root browser as shown below
00012 //   Root > TBrowser b;
00013 //    click on the left pane on one of the plane directories.
00014 //    this shows the list of all histograms in this directory.
00015 //    Double click on one histogram to draw it (left mouse button).
00016 //    Select different options with the right mouse button.
00017 //
00018 //  You can see the begin_html <a href="gif/dirs.gif" >picture of the browser </a> end_html
00019 //
00020 //    Instead of using the browser, you can also do:
00021 //   Root > tof->cd();
00022 //   Root > plane3->cd();
00023 //   Root > h3_90N->Draw();
00024 //Author: Rene Brun
00025 
00026     // create a new Root file
00027    TFile *top = new TFile("top.root","recreate");
00028 
00029    // create a subdirectory "tof" in this file
00030    TDirectory *cdtof = top->mkdir("tof");
00031    cdtof->cd();    // make the "tof" directory the current directory
00032 
00033    // create a new subdirectory for each plane
00034    const Int_t nplanes = 10;
00035    const Int_t ncounters = 100;
00036    char dirname[50];
00037    char hname[20];
00038    char htitle[80];
00039    Int_t i,j,k;
00040    TDirectory *cdplane[nplanes];
00041    TH1F *hn[nplanes][ncounters];
00042    TH1F *hs[nplanes][ncounters];
00043    for (i=0;i<nplanes;i++) {
00044       sprintf(dirname,"plane%d",i);
00045       cdplane[i] = cdtof->mkdir(dirname);
00046       cdplane[i]->cd();
00047       // create counter histograms
00048       for (j=0;j<ncounters;j++) {
00049          sprintf(hname,"h%d_%dN",i,j);
00050          sprintf(htitle,"hist for counter:%d in plane:%d North",j,i);
00051          hn[i][j] = new TH1F(hname,htitle,100,0,100);
00052          sprintf(hname,"h%d_%dS",i,j);
00053          sprintf(htitle,"hist for counter:%d in plane:%d South",j,i);
00054          hs[i][j] = new TH1F(hname,htitle,100,0,100);
00055       }
00056       cdtof->cd();    // change current directory to top
00057    }
00058 
00059      // .. fill histograms
00060    TRandom r;
00061    for (i=0;i<nplanes;i++) {
00062       cdplane[i]->cd();
00063       for (j=0;j<ncounters;j++) {
00064          for (k=0;k<100;k++) {
00065             hn[i][j]->Fill(100*r.Rndm(),i+j);
00066             hs[i][j]->Fill(100*r.Rndm(),i+j+k);
00067          }
00068       }
00069    }
00070 
00071      // save histogram hierarchy in the file
00072    top->Write();
00073    delete top;
00074 }
00075 

Generated on Tue Jul 5 15:44:48 2011 for ROOT_528-00b_version by  doxygen 1.5.1