Std::bad_alloc when writing TTree to file

Dear Rooters,

I’m dealing with a already solved ROOT problem, I guess, but I’m getting that problem again. I’m using ROOT 5.28/00d and a class which derives from TSelector, to loop over events in an input TTree. When writing my TTree into a TFile ( in Terminate() ) I get a “std::bad_alloc”. I attached some pieces of my code - I don’t know what parts you exactly you would need to help me out.

Header:

class FastWenuEoverP : public TSelector {
public :
...
private :
 TTree t_etat_tot;
...

Implementation:

void FastWenuEoverP::SlaveBegin(TTree * tree)
{
    t_etat_tot.SetNameTitle("treeAll","Wenu");
    t_etat_tot.Branch("EoverEtruth",    &m_EoverEtruth);
    t_etat_tot.Branch("PtruthoverP",    &m_PtruthoverP);
    t_etat_tot.Branch("EoverP",         &m_EoverP);
    t_etat_tot.Branch("el_Phi",         &m_el_Phi);
....
}

void FastWenuEoverP::Terminate()
{
    std::cout << "---- Terminate()" << std::endl;
    std::cout << "---- create File" << std::endl;
    TFile* file = new TFile("/tmp/fat/test.root","RECREATE");
    t_etat_tot.FlushBaskets();
    std::cout << "---- write TTree " << std::endl;
    t_etat_tot.Write();

    std::cout << "---- write File" << std::endl;
    file->Write();

    std::cout << "---- close TTree" << std::endl;
    file->Close();

    delete file;
    std::cout << "---- Terminate() --- THE END" << std::endl;
}

And the Ouput, after finishing the event loop and calling Terminate()

---- Terminate()
---- create File
---- write TTree 
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

It seems to me a memory problem and depending on the size of the TTree I want to write to the file, the less Branches the Tree has and the less events I loop over the less is the chance this to happen. I am really a bit lost, since I read somewhere that this issue should be solved vor version greater than 5.27.

What could be checked to find a solution ?
Thanks a lot,
Florian

[quote]It seems to me a memory problem and depending on the size of the TTree I want to write to the file, the less Branches the Tree has and the less events I loop over the less is the chance this to happen.[/quote]This is the expected result as you do not associate the TTree with a file, it must keep in memory all the data until the call to Terminate … So it is very likely/plausible that you run out of memory the more data the TTree contains.

Use something like:[code]class FastWenuEoverP : public TSelector {
public :

private :
TFile *t_output_file;
TTree *t_etat_tot;

void FastWenuEoverP::SlaveBegin(TTree * tree)
{
t_output_file = TFile::Open("/tmp/fat/test.root",“RECREATE”);
t_etat_tot = new TTree(“treeAll”,“Wenu”);

}

void FastWenuEoverP::Terminate()
{
std::cout << “---- Terminate()” << std::endl;

std::cout << "---- write File and Tree." << std::endl;
t_output_file->Write();

std::cout << "---- close File" << std::endl;
t_output_file->Close(); // this also deletes the TTree.
t_etat_tot = 0;

delete t_output_file;  
t_output_file = 0;
std::cout << "---- Terminate() --- THE END" << std::endl;

}[/code]

Cheers,
Philippe.

Dear Philippe,

thanks for the fast response and the hint.
Actually the program doesn’t crash anymore but the TTree is not written in the file, i.e. the file is empty.
Within the event loop and in SlaveBegin I am opening and closing other ROOT files.
Does that interfere with the code you sent me ?

Thanks a lot,
Florian

[quote]Within the event loop and in SlaveBegin I am opening and closing other ROOT files.
Does that interfere with the code you sent me ?[/quote]No, it should not.

Cheers,
Philippe.