Re: [ROOT] Unexpected reading of TH1 objects from a tree

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Jun 17 2002 - 08:28:26 MEST


Hi Nesterov,

When calling whell->GetEntry, you request to read all the branches
of your Tree. If you want to read only one single branch, get
the pointer to the branch and call branch->GetEntry.

In your case, you should do something like:

TBranch *b_Spec = wheel->GetBranch("Spec");
TH1 *Spec=0;
b_Spec->SetAddress(&Spec);
b_Spec->GetEntry(22);
delete Spec; Spec=0; //to delete the current hist
Spec=0; // if you want to keep the current hist in memory
b_Spec->GetEntry(23); /etc..

Rene Brun

On Mon, 17 Jun 2002, Nesterov Stanislav wrote:

> Hi All,
> 
> I found a strange problem arising when you are trying to read TH1F objects 
> from a TTree. We have a tree with several TH1F objects as TBranches (under 
> different names). I understand clearly that this kind of object is not 
> suitable for keeping inside a tree, but ROOT behavior in getting these 
> histograms is very strange. Below I dump simple ROOT session where I opened 
> TFile, created new TH1F object, set Branch Address, and then got several 
> Entries from opened tree.
> >>>>>>>>>>>>>>>>>>>>Session start<<<<<<<<<<<<<<<<<<<<<<<<<<<
>  *******************************************
>   *                                        *
>   *        W E L C O M E  to  R O O T       *
>   *                                         *
>   *   Version   3.03/05       31 May 2002   *
>   *                                         *
>   *  You are welcome to visit our Web site  *
>   *          http://root.cern.ch            *
>   *                                         *
>   *******************************************
> root [0] TFile fl("res/run06.root")
> root [1] fl.cd()                        
> (Bool_t)1
> root [2] pht = new TH1F;                              
> root [3] .ls                                    
> TFile**         res/run06.root
>  TFile*          res/run06.root
>   KEY: TTree    wheel;1 Measurement of TRT Wheel
> root [4] wheel->Print()
> >>>>>>>>>>>>>>>bla-bla-bla<<<<<<<<<<<<<<<<<<<<<<<<<<
> *.........................................................................*
> *Br   41 :RBckgr    : RBckg[3]/F                                          *
> *Entries :       54 : Total  Size=        714 bytes  File Size  =       0 *
> *Baskets :        0 : Basket Size=      32000 bytes  Compression=1.00     *
> *.........................................................................*
> *Br  42 :Spec      :                                                      *
> *Entries :       54 : Total  Size=     323848 bytes  File Size  =   92614 *
> *Baskets :       27 : Basket Size=      16000 bytes  Compression=3.50     *
> *.........................................................................*
> *Br  43 :RSpec     :                                                      *
> *Entries :       54 : Total  Size=     324416 bytes  File Size  =  103221 *
> *Baskets :       27 : Basket Size=      16000 bytes  Compression= 3.14    *
> *.........................................................................*
> root [5] wheel->SetBranchAddress("Spec",&pht);
> root [6] .ls
> TFile**         ../../fedin/wts_offline/res/run06.root
>  TFile*         ../../fedin/wts_offline/res/run06.root
>   OBJ: TTree    wheel   Measurement of TRT Wheel : 0
>   KEY: TTree    wheel;1 Measurement of TRT Wheel
> root [7] wheel->GetEntry(13)
> (Int_t)12134
> root [8] .ls
> TFile**         ../../fedin/wts_offline/res/run06.root
>  TFile*         ../../fedin/wts_offline/res/run06.root
>   OBJ: TTree    wheel   Measurement of TRT Wheel : 0
>   OBJ: TH1F     s1_3_2  Spectrum L==1  C==3  R==2 : 0
>   OBJ: TH1F     r1_3_2  Reference spectrum L==1  C==3  R==2 : 0
>   KEY: TTree    wheel;1 Measurement of TRT Wheel
> root [9] wheel->GetEntry(24)
> (Int_t)12134
> root [10] .ls                
> TFile**         ../../fedin/wts_offline/res/run06.root
>  TFile*         ../../fedin/wts_offline/res/run06.root
>   OBJ: TTree    wheel   Measurement of TRT Wheel : 0
>   OBJ: TH1F     s1_5_1  Spectrum L==1  C==5  R==1 : 0
>   OBJ: TH1F     r1_5_1  Reference spectrum L==1  C==5  R==1 : 0
>   OBJ: TH1F     s1_5_1  Spectrum L==1  C==5  R==1 : 0
>   OBJ: TH1F     r1_5_1  Reference spectrum L==1  C==5  R==1 : 0
>   KEY: TTree    wheel;1 Measurement of TRT Wheel
> root [11] wheel->GetEntry(2) 
> (Int_t)12134
> root [12] .ls               
> TFile**         ../../fedin/wts_offline/res/run06.root
>  TFile*         ../../fedin/wts_offline/res/run06.root
>   OBJ: TTree    wheel   Measurement of TRT Wheel : 0
>   OBJ: TH1F     s1_1_3  Spectrum L==1  C==1  R==3 : 0
>   OBJ: TH1F     r1_1_3  Reference spectrum L==1  C==1  R==3 : 0
>   OBJ: TH1F     s1_1_3  Spectrum L==1  C==1  R==3 : 0
>   OBJ: TH1F     r1_1_3  Reference spectrum L==1  C==1  R==3 : 0
>   OBJ: TH1F     s1_1_3  Spectrum L==1  C==1  R==3 : 0
>   OBJ: TH1F     r1_1_3  Reference spectrum L==1  C==1  R==3 : 0
>   KEY: TTree    wheel;1 Measurement of TRT Wheel
> root [13] delete pht
> root [14] .ls
> TFile**         ../../fedin/wts_offline/res/run06.root
>  TFile*         ../../fedin/wts_offline/res/run06.root
>   OBJ: TTree    wheel   Measurement of TRT Wheel : 0
>   OBJ: TH1F     r1_1_3  Reference spectrum L==1  C==1  R==3 : 0
>   OBJ: TObject  TObject Basic ROOT object : 0
>   OBJ: TH1F     r1_1_3  Reference spectrum L==1  C==1  R==3 : 0
>   OBJ: TObject  TObject Basic ROOT object : 0
>   OBJ: TH1F     r1_1_3  Reference spectrum L==1  C==1  R==3 : 0
>   KEY: TTree    wheel;1 Measurement of TRT Wheel
> root [15] r1_1_3->Delete()
> root [16] .ls
> TFile**         ../../fedin/wts_offline/res/run06.root
>  TFile*         ../../fedin/wts_offline/res/run06.root
>   OBJ: TTree    wheel   Measurement of TRT Wheel : 0
>   OBJ: TObject  TObject Basic ROOT object : 0
>   OBJ: TObject  TObject Basic ROOT object : 0
>   OBJ: TObject  TObject Basic ROOT object : 0
>   OBJ: TObject  TObject Basic ROOT object : 0
>   KEY: TTree    wheel;1 Measurement of TRT Wheel
> root [17] .q
> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<End of Session>>>>>>>>>>>>>>>>>>
> This is getting very odd. I just tried to get ONE histogram, but have got 
> ALL of them in memory. And this is not all: histograms are duplicated with 
> the same names. Deleting is not good for them: strange object still in 
> memory. 
> 
> Yes I know: TH1 and TTree objects are reading into a memory by default, but 
> for tree branch it is not very good default I think.
> 			   Best regards,
> 					Stanislav Nesterov.
> 
> 
> 
> 



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:57 MET