Append data to an existing TTree (with Branches)

Hi,
i’m using this program to write some data into a TTree:

void writeDataROOT(int nrunr, int nsegm, int nstream, int ID_riv,
Int_t nfoundb, Float_t xpb_time_vect[], Float_t ypb_sub_vect[], int
idelev, Float_t
 meanback_peak[], Float_t peak_integral[], Float_t time_start, Float_t
beaminfo, char MyDetName[])
{
 sprintf(outnameb,"/castor/cern.ch/user/c/calviani/DST/%s/data_run%d_%d_s%d_IDriv%d.root",MyDetName,nrunr,nsegm,nstream,ID_riv);
 TRFIOFile *f = new TRFIOFile(outnameb,"UPDATE"); 
e' come TFile.....
 Int_t jj=0;
 while(f->IsZombie())  
   {
     if(jj==0) cout << "WAITING....." <<endl>Branch("riv",&data_tree.idelev,"idelev/I");
 t->Branch("xpb_time_vect",&data_tree.xpb_time_vect,"xpb_time_vect");
 t->Branch("ypb_sub_vect",&data_tree.ypb_sub_vect,"ypb_sub_vect");
 t->Branch("meanback_peak",&data_tree.meanback_peak,"meanback_peak");
 t->Branch("peak_integral",&data_tree.peak_integral,"peak_integral");
 t->Branch("time_start",&data_tree.time_start,"time_start");
 t->Branch("beaminfo",&data_tree.beaminfo,"beaminfo");
 for(int i=0; i<nfoundb>Fill();

   }
 t->Write();
 f->Close();


}

The problem is that it creates different trees every time it enters this loop (well this is expected i think even if they are not Autosave), for example:

  KEY: TTree    t;9     DST DATA
  KEY: TTree    t;8     DST DATA
  KEY: TTree    t;7     DST DATA
  KEY: TTree    t;6     DST DATA
  KEY: TTree    t;5     DST DATA
  KEY: TTree    t;4     DST DATA
  KEY: TTree    t;3     DST DATA
  KEY: TTree    t;2     DST DATA
  KEY: TTree    t;1     DST DATA

How is it possible to append data on the same TTree?

Regards,
Marco

I dont think it is every loop. It is autosave feature that writes headers very so often. Headers contain informtaion about tree structure.

To ovewrite all those headers with just latest version of THE header at the end of filling do:

fOutFile->Write(0,TObject::kOverwrite);

Hi,
actually they are indeed not Autosave KEYs since each KEY contains different data as expected. I’ve tried to unify each KEY using

f->Write(0,TObject::kSingleKey);

but i didn’t obtained any good result: it still creates different KEYs every time the function is entered…

m

oh, I see.

I’ve never done that but :

seems logical to add a check if tree already exists then if it does ( after 1-st iteration) dont create new tree but rather fill existing one.
TTree* treePtr = NULL;
while()
{

treePtr = (TTree*)gDirectory->Get(“treename”);
if(treePtr == NULL)
{ make one }

else

{ another loop to fill existing one}

}

I normally open file for writing and keep it open and keep filling tree till the end and then write tree with “overwite” and close file before program exits.

Hi,

As point to ardashev, you need to call with kOverwrite (and not kSingleKey).
You may have to call this directly on the tree:

Also it is not recommended to call Write within the loop. It is problably better to use AutoSave unless your application is such that you can not afford under any circumstance to lose any data (with AutoSave you would only lose the date since the last AutoSave).

Cheers,
Philippe

Hi,
i’ve tried both options: kOverwrite overwrite (!) at each loop the data that has been written the loop that came before, so it is not useful (yes there is one key but only with the data written by the last loop). I’ve tried also the solution of ardashev but the result is the same as the first one.

m

Hi,

Can you please send me a running example (as attachment)?

Thanks,
Philippe.

Hi Philippe,
thanks for your help but i’ve solved my problem, by simply following an old message from Rene found here:

root.cern.ch/root/roottalk/roottalk01/0363.html

Cheers,
Marco