Re: disabling TNamed characteristics of TNamed inherited objects

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Apr 15 1998 - 11:22:24 MEST


William J Deninger wrote:
> Rene,
> I don't believe that this method works.  It was tried with both TObjArray
> and TClonesArray.  The following does produces a listing of objects in the
> TFile directory. Would you have any other suggestions?  And all you help is
> greatly appreciated!  --William J Deninger
> 




William,
I suggest the following simplifications in your code:
 1- In your class TDriftSpectrum, implement a constructor:
    TDriftSpectrun::TDriftSpectrum(const char*name, const char*title,
                                Int_t nbins, Float_t xlow, Float_t xup)
                  :TH1F(name,title,nbins,xlow,xup)
   {
      SetDirectory(0);
   }

 2- Do not derive TDriftSpectrumInfo from TDriftSpectrum but
from            TNamed.

 3- In your main program, simply do:
    d1->Write();

By default, TH1 objects are automatically added to the list of objects
in memory (you see them with gDirectory->ls();
The statement SetDirectory(0) will remove the object from this list.
d1->Write() will write one single key.
In your old code below, the statement f->Write() writes all objects
in the list in memory associated to the file f.

Rene Brun


> //__________________________________________________
> CINT/ROOT C/C++ Interpreter version 5.13.45, Mar 17 1998
> Type ? for help. Commands must be C++ statements.
> Enclose multiple statements between { }.
> root [0] TFile *f = new TFile("f:/test;3.root","recreate");
> root [1] TDriftSpectrumInfo *d1 = new TDriftSpectrumInfo("d1");
> root [2] Int_t i;
> root [3] for (i=1; i<3; i++)  d1->Add(i);
> root [4] d1->Build();
> root [5] f.ls()
> TFile**         f:/test;3.root
>  TFile*         f:/test;3.root
>   OBJ: TDriftSpectrumInfo       d1      d1 : 0
>   OBJ: TDriftSpectrum   drift1  drift1 : 0
>   OBJ: TDriftSpectrum   drift2  drift2 : 0
>   OBJ: TDriftSpectrum   drift3  drift3 : 0root [6] d1->Write()
> root [7] f->Write()
> TFile Writing Name=f:/test;3.root Title=
> root [8] f.Close()
> root [9]
> 
> CINT/ROOT C/C++ Interpreter version 5.13.45, Mar 17 1998
> Type ? for help. Commands must be C++ statements.
> Enclose multiple statements between { }.
> root [0] TFile *f = new TFile("f:/test;3.root");
> root [1] f.ls()
> TFile**         f:/test;3.root
>  TFile*         f:/test;3.root
>   KEY: TDriftSpectrumInfo       d1;2    d1
>   KEY: TDriftSpectrumInfo       d1;1    d1
>   KEY: TDriftSpectrum   drift1;1        drift1
>   KEY: TDriftSpectrum   drift2;1        drift2
>   KEY: TDriftSpectrum   drift3;1        drift3
>   root [2]
> 
> //__________________________________________________
> class TDriftSpectrum  : public TH1F
> {
> public:
>  TDriftSpectrum();
>  TDriftSpectrum(Text_t* title);
>  virtual ~TDriftSpectrum();
> 
>  ClassDef(TDriftSpectrum,1)
> };
> 
> //__________________________________________________
> class TDriftSpectrumInfo  : public TDriftSpectrum
> {
> public:
>  TDriftSpectrumInfo();
>  TDriftSpectrumInfo(Text_t *tname);
>  virtual ~TDriftSpectrumInfo();
> 
>  TDriftSpectrum * Add(Int_t index);
>  TDriftSpectrum * Add(Text_t *title);
> 
>  TObjArray *drift;
> 
> protected:
>  ClassDef(TDriftSpectrumInfo,1)
> };
> 
> TDriftSpectrumInfo::TDriftSpectrumInfo(Text_t *tname) :
> TDriftSpectrum(tname)
> {
>  drift = new TObjArray(500);
>  drift->Clear();
>  size = 0;
> }
> 
> TDriftSpectrum * TDriftSpectrumInfo::Add(Int_t index)
> {
>  Char_t title[256];
>  sprintf(title,"drift%i",index);
>  return Add(title);
> }
> 
> TDriftSpectrum * TDriftSpectrumInfo::Add(Text_t *title)
> {
>  if ((*drift)[size]) delete (*drift)[size];
> 
>  (*drift)[size] = new TDriftSpectrum(title);
>  return (TDriftSpectrum *) (*drift)[size++];
> }



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:32 MET