Re: [ROOT] Appending To NTuple

From: Jon Gans (gans@star.physics.yale.edu)
Date: Sat Jul 08 2000 - 17:31:12 MEST


Rene,
When I first create the ntuple, then use code like append2 to append,
it not only overwrites the entries, but i only get the last
entry saved. Is that code right? Using the append method I get all the
data, but the "factorial" type saving makes the file HUGE. Below is my
minimal code segment, for the append 2 type save:

void StRichPadMonitor::doResiduals(){
    
  TFile * residFile = new TFile("resid.root","NEW");
  TNtuple * residNtuple = 0;
  int appending = 0;
  
  float tempArray = {1,2,3,4,5,6,7,8,9,10,11};
  
  if( !residFile->IsOpen() ){
    
    delete residFile;
    residFile = new TFile("resid.root","UPDATE");
    residNtuple = (TNtuple*)residFile->Get("residNtuple");
    appending = 1;
    cout << "appending" << endl;
  }
  else{
    residNtuple =
      new TNtuple("residNtuple","Residual Info","run:evt:hitX:hitY:hitCharge:mipX:mipY:mipPx:mipPy:mipPz:zVert");	
    cout << "creating new" << endl;
  }
  
     
  residNtuple->Fill(tempArray);
    
    //should keep latest header file only...may not
    if(appending)
	residNtuple->Write("",TObject::kOverwrite);
    else
	residNtuple->Write();
    
    residFile->Close();
    delete residFile;
}

thanks alot
 jon



On Wed, 5 Jul 2000, Rene Brun wrote:

> Hi Jonathan,
> 
> a small example showing how to create an ntuple (ntwrite) and appending
> new data to an existing ntuple (ntappend, ntappend2). see comments in code
> 
> Rene Brun
> 
> void ntwrite()
> {
> 
>   TFile *file = new TFile("ntuple.root","RECREATE");
> 
>   TNtuple *ntuple = new TNtuple("ntuple","Demo ntuple","px:py:pz:random:i");
> 
>   Float_t px, py, pz;
>   for (Int_t i = 0; i < 25000; i++) {
>      gRandom->Rannor(px,py);
>      pz = px*px + py*py;
>      Float_t random = gRandom->Rndm(1);
>      ntuple->Fill(px,py,pz,random,i);
>   }
> 
>   ntuple->Write();
>   file->Close();
>   delete file;
> }
> 
> void ntappend()
> {
> 
>   TFile *file = new TFile("ntuple.root","UPDATE");
> 
>   TNtuple *ntuple = (TNtuple*)file->Get("ntuple");
> 
>   Float_t px, py, pz;
>   for (Int_t i = 0; i < 25000; i++) {
>      gRandom->Rannor(px,py);
>      pz = px*px + py*py;
>      Float_t random = gRandom->Rndm(1);
>      ntuple->Fill(px,py,pz,random,i);
>   }
> 
>   //each ntuple->Write saves a new ntuple header. You will get the
>   // keys ntuple1;1 ntuple;2 ntuple;3 ,etc
>   // Assuming 3 cycles (1 call to ntwrite and 2 calls to ntappend),
>   // in an interactive session, you can do:
>   //   TNtuple *ntuple1 = (TNtuple*) file.Get("ntuple;1");
>   //   ntuple1->Draw("px>>hpx1"); // hpx1 will have 25000 entries
>   //   TNtuple *ntuple2 = (TNtuple*) file.Get("ntuple;2");
>   //   ntuple2->Draw("px>>hpx2"); // hpx2 will have 50000 entries
>   //   TNtuple *ntuple3 = (TNtuple*) file.Get("ntuple;3");
>   //   ntuple3->Draw("px>>hpx3"); // hpx3 will have 75000 entries
>   
>   ntuple->Write();
>   file->Close();
>   delete file;
> }
> 
> void ntappend2()
> {
> 
>   TFile *file = new TFile("ntuple.root","UPDATE");
> 
>   TNtuple *ntuple = (TNtuple*)file->Get("ntuple");
> 
>   Float_t px, py, pz;
>   for (Int_t i = 0; i < 25000; i++) {
>      gRandom->Rannor(px,py);
>      pz = px*px + py*py;
>      Float_t random = gRandom->Rndm(1);
>      ntuple->Fill(px,py,pz,random,i);
>   }
> 
>   // keep the latest ntuple header only
>   ntuple->Write("",TObject::kOverwrite);
>   file->Close();
>   delete file;
> }
> 
> 
> Jon Gans wrote:
> > 
> > Dear Rooters...
> > 
> > I want to append to a saved NTuple in a TFile. However doing a
> > file->write() merely adds another entry to the current file with the same
> > name. How can I get it to append?
> >  Thanks
> >  jon
> > 
> > ______________________________________________________________________________
> > 
> > Jonathan Gans
> > Yale University
> > Physics Department
> > PO BOX 208120
> > New Haven, CT 06520-8120                             http://www.jongans.com
> 

______________________________________________________________________________

Jonathan Gans		                             Home: (203) 789 - 8029
Yale University                         Office (305 WNSL): (203) 432 - 5637
Physics Department                               Cellular: (203) 676 - 9963  
PO BOX 208120                                         Fax: (516) 213 - 4967    
New Haven, CT 06520-8120     	                     http://www.jongans.com



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:29 MET