Re: [ROOT] Use of TFolder

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Oct 23 2001 - 10:33:14 MEST


Hi Christian,

The primary role of TFolder is to facilitate the description of the
transient data structures, minimizing strong coupling between classes.
Your folder structure is correct. However, your structure mimics a Unix
directory structure. It is not what a TFolder is designed for.
Why don't you use directly the OS file system to do this?

Anyhow, assuming your existing folder structure, you can automatically
generate a Root Tree from this structure by doing for example:

   TFile *vFile = new TFile("folders.root","recreate");
   TTree T("T","/MyRoot");
   T.Fill();
   T.Print();
   T.Write();

instead of the last 3 statements of your function MyFolders.
The T.Print() will show you the automatic generation of the Tree branches
from the folder structure. There is no need to specify branch addresses.

Currently, Root is not capable of rebuilding the folder structure automatically
when reading back a Tree. This is on my todo list since a few months now.

Rene Brun

cstrato@EUnet.at wrote:
> 
> Dear Rooters
> 
> Maybe the use of TFolder is not quite clear to me. What I would like
> to do is the following:
> Suppose I have the following hierarchy:
>    Runs
>        Run1
>            Sets
>                Set1
>                     Tree1
>                     Tree2
>                Set2
>                    Tree3
>                    Tree4
>        Run2
>            Sets
>                Set3
>                    Tree5
>       etc
> where every Run is stored in its own root file:
>     Run1.root
>     Run2.root
> 
> Now I would like to display this hierachy in TBrowser as tree structure.
> 
> However, I want to display this structure permanently, and when I add
> another RunX.root file, this file should be added automatically.
> 
> My idea was to create a TFolder structure and store this structure
> permanently in another root file, called ?folders.root?
> 
> As the macro (?macroFolder.C?) below shows, this works within one
> root session. However, when I close root, and open it again, I can no
> longer display the RunX.root files within TBrowser, since the
> information
> where these files are stored is lost.
> 
> Does someone have a hint, how I could store the TFolder hierarchy
> permanently, so that I have access to TTrees of the different root
> files?
> 
> If TFolder is not the right way to go, what other possibilities do
> exist?
> 
> Normally, I would create a class TRun and TSet where each class has
> a TList which stores the  list of Runs and Trees, respectively. However,
> 
> the root manual says, that the reason to use TFolders is to reduce class
> 
> dependencies. At the moment it is not quite clear to me how I could
> achieve this in a standalone application?
> 
> Thank you very much in advance.
> 
> Best regards
> Christian
> ----------------------------------
> C.h.r.i.s.t.i.a.n  S.t.r.a.t.o.w.a
> V.i.e.n.n.a,  A.u.s.t.r.i.a
> 
> ----------macroFolder.C-------------------
> TTree *CreateTree(const char *vTreeName, const char *vTreeTitle)
> {
>    vTree = new TTree(vTreeName, vTreeTitle);
>    Int_t id;
>    Float_t x, y;
>    TBranch *br1 = vTree->Branch("br1",&id,"id/I");
>    TBranch *br2 = vTree->Branch("br2",&x,"x/F");
>    TBranch *br3 = vTree->Branch("br2",&y,"y/F");
>    for (Int_t i = 0; i < 100; i++) {
>       id = i;
>       gRandom->Rannor(x,y);
>       vTree->Fill();
>    }
> 
>    return vTree;
> }
> 
> //----------------------------------------------------------------------//
> 
> void AddTrees(TFolder *vSet)
> {
>    TFolder *vTrees = vSet->AddFolder("Trees", "Set Trees");
> 
>    vTrees->Add(CreateTree("Tree1", "Tree 1"));
>    vTrees->Add(CreateTree("Tree2", "Tree 2"));
>    vTrees->Add(CreateTree("Tree3", "Tree 3"));
>    vTrees->Add(CreateTree("Tree4", "Tree 4"));
> }
> 
> //----------------------------------------------------------------------//
> 
> void AddTree(TFolder *vSet, const char *vTreeName)
> {
>    TTree *vTree = CreateTree(vTreeName, vSet->GetName());
> 
>    vSet->Add(vTree);
> }
> 
> //----------------------------------------------------------------------//
> 
> void AddFile(TFolder *vRun, const char *vFileName)
> {
>    TFile *vFile = new TFile(vFileName,"recreate");
>    TTree *vTree1 = CreateTree("T1",vFileName);
>    TTree *vTree2 = CreateTree("T2",vFileName);
> 
>    vFile->Write();
> 
>    vRun->Add(vFile);
> }
> 
> //----------------------------------------------------------------------//
> 
> void MyFolders()
> {
>    TFolder *vMyRoot = gROOT->GetRootFolder()
>                            ->AddFolder("MyRoot", "MyRoot top level");
> 
>    gROOT->GetListOfBrowsables()->Add(vMyRoot, "MyRoot");
> 
>    TFolder *vRuns = vMyRoot->AddFolder("Runs", "Runs");
> 
>    TFolder *vRun1 = vRuns->AddFolder("Run1", "Run 1");
> 
>    TFolder *vSets = vRun1->AddFolder("Sets", "Dataset");
> 
>    TFolder *vSet1 = vSets->AddFolder("Set1", "Dataset 1");
>    vSet1->Add(new TNamed("Data1", "data1 in set 1"));
>    vSet1->Add(new TNamed("Data2", "data2 in set 1"));
>    vSet1->Add(new TNamed("Data3", "data3 in set 1"));
> 
>    TFolder *vSet2 = vSets->AddFolder("Set2", "Dataset 2");
>    AddTrees(vSet2);
> 
>    TFolder *vSet3 = vSets->AddFolder("Set3", "Dataset 3");
>    AddTree(vSet3, "TreeA");
>    AddTree(vSet3, "TreeB");
> 
>    AddFile(vRun1, "/home/christian/rt/rootcode/xps/Run1.root");
> 
>    TFolder *vRun2 = vRuns->AddFolder("vRun2", "Run 2");
>    AddFile(vRun2, "/home/christian/test/Run2.root");
> 
> //   new TBrowser();
> 
>    TFile *vFile = new TFile("folders.root","recreate");
>    vMyRoot->Write();
>    vFile->Delete("");
> }
> 
> ///////////////////////////
> // To run it in root:
> //     > .L macroFolder.C
> //     > MyFolders()
> //
> ///////////////////////////



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:51:03 MET