Re: [ROOT] Classes derived from TFile or TFolder?

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Jan 05 2001 - 21:00:40 MET


Hi Christian,
The TFolder class is to manage objects in memory not on disk.
Use file directories for disk files.



On Fri, 5 Jan 2001 cstrato@EUnet.at wrote:

> Dear Rooters
> 
> I would like to create a Class B which stores a list of trees, and a
> Class A,
> which stores a list of Class B and another list of histograms.
> 
> In this respect I have the following questions on how to deal best with
> this problem:
> 
> 1, I understand that TFile is stored in machine independent format,
> which is great.
> This means that everything in TFile can be accessed independent of the
> machine type,
> including class TFolder when it is stored in TFile.
> Could I also use TFolder as the highest hierarchy and store different
> TFiles and
> histograms in TFolder? Are histograms then accessible from different
> machines or
> do I still need to store historgrams in TFile first, which is itself
> stored in
> TFolder to access the histograms?
> 
> 2, Can I create class A in the following way?
> ClassA: public TNamed {
>    private
>       TFile *fFile;
>       TFolder *fFolder;
> };
> How can I store this class A itself in its member variable fFile or
> fFolder?
> Is this possible?


You can store the class A into fFile. You must declare fFile to be non
persistent with
  TFile *fFile;   //!
In the default constructor, set fFile to 0.

> 
> I would like to be able to write a simple macro
> {
>    ClassA *classA = new Class("NameA");
>    classA->DoSomething();
> }
> Is it possible to code the constructor such that it opens its TFile
> *fFile when it
> exists and then accesses the information stored in itself, i.e. the
> classA stored
> earlier in fFile?

yes

> 
> 3, Would it be better to create class A in one of the following ways?
> a,ClassA: public TFile {
>    private
>       TFolder *fFolder;
> }

No, you should not do this.

> Would ClassA then also be machine
independent?
> Would this be a feasible idea?
> 
> b,ClassA: public TFolder {
>    private
>       TFile *fFile;
> }
> 

no

> 4, TFolder has a function TFolder::Add(TObject *obj) and the
explanation
> says
> that "obj must be a TObject or a TFolder"
> a, Does this also include classes derived from TObject such as TFile,
> histograms?


 It means any object derived from TObject, including TFile, TCollections, 
etc

> b, The code of Add() is fFolders->Add(obj), however, fFolders is
> TCollection, which
> is an abstract base class with TCollection::Add() = 0. Does this mean
> that in  the
> constructor TFolder() I have to define the type of collection I want to
> use?
> 


No, it is preferable to call TFolder::AddFolder and specify the type
of collection you want in the folder. The doc says;
  // Create a new folder and add it to the list of folders of this folder
  // return a pointer to the created folder
  // Note that a folder can be added to several folders
  //
  // if (collection is non NULL, the pointer fFolders is set to the
existing
  // collection, otherwise a default collection (Tlist) is created

>
5,
Can I somewhere find a simple code fragment how to use TFolder class?

  
See in the attachment, an example macro "folders.C"

> 
> 6, Is there a better way to deal with the problem described at the
> beginning?
> Probably, the simplest way would be to create a TFile and store the
> trees and
> histograms in different subdirectories. However, this is not sufficient
> for me
> since both ClassA and ClassB will contain additional variables and
> member functions.

yes, use subdirectories. I cannot tell you more without a better
description of what you want to do. I suggest you look at the example
in the attachment and also the way TFolders are used in Root itself.
See the TROOT constructor.

Rene Brun
-> 
> Thank you in advance
> 
> Best regards
> Christian Stratowa
> Vienna, Austria
> 
> 





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