Memory error writting a tree

Dear Rooters,
I have a problem of memory when filling a TTree and saving it to a file when processing a large number of events.(It just happens when i fill a large number of events)
The error happens at the TTree writting to a file stage and reads:
“terminate called after throwing an instance of 'std::bad_alloc’
what(): St9bad_alloc
Abort

I have tried to solve it by using AutoSave TTree’s method unsuccesfully to periodically save to file the processed data in the tree.
The pseudo-code i use is the following:


TFile *Output_file=new TFile("myoutputfile.root","recreate");

TTree *Run=new TTree("Run","Mytree");
Run->SetDirectory(0);

//make branchs and all stuff here
/*
...
*/

TDirectory* Currentdir = gDirectory;

for(int i=0;i<1000000;i++){           //loop over events
//here we fill the branches
/*
...
*/

  Run->Fill();
    if (i%10000 == 1){  
    Output_file->cd();
    Run->AutoSave("SaveSelf");
    Output_dst->SaveSelf();
    Currentdir->cd();
    }
}//eofloop
Output_file->cd();
Run->Write();
Output_file->Close();

I would appreciate any hint pointing the problem and a possible solution.
Thank you
I.

Hi,

Calling

 Run->SetDirectory(0)

detaches the tree from the file and keeps it in memory.

Try:

TFile *Output_file=new TFile("myoutputfile.root","recreate");

TTree *Run=new TTree("Run","Mytree");
Run->SetDirectory(Output_file);

//make branchs and all stuff here
/*
...
*/

TDirectory* Currentdir = gDirectory;
Run->AutoSave();

for(int i=0;i<1000000;i++){           //loop over events
//here we fill the branches
/*
...
*/

  Run->Fill();

}//eofloop
Output_file->cd();
Run->Write();
Output_file->Close();

G. Ganis

It worked!
Thank you!