RE: creating trees

From: Philippe Canal <pcanal_at_fnal.gov>
Date: Tue, 17 Jan 2006 08:49:47 -0600


Hi,

You can simply SetOutputFile:

void SetOutputFile( const char* filename ) {

    if( fFile ) {

        fFile->Write();
        delete fFile;
        fFile = 0;
        fTree = 0;

}

    if( !filename.empty() ) {
        fFile = new TFile( filename, "recreate" );
        fTree = new TTree( "T", "tree" );
        fTree->Branch( "event", "TreeEvent", &e, 16000, 2 );

}

}

Also you have:

    for( int i=0; i<100000; ++i ) {

       SetOutputFile( "fileXY.root" );

I suppose this is an artefact of your sample code (this seems to overwrite the same file 100000 times).

In the default contructor of TreeEvent did you make sure to initialize __every__ data member of TreeEvent to a valid value?

What is the stack trace of the crashes?

Did you try to run your program with valgrind (http://valgrind.kde.org)?

Cheers,
Philippe
-----Original Message-----
From: owner-roottalk_at_pcroot.cern.ch [mailto:owner-roottalk_at_pcroot.cern.ch] On Behalf Of Alexander Bürger
Sent: Tuesday, January 17, 2006 6:51 AM
To: roottalk_at_pcroot.cern.ch
Subject: [ROOT] creating trees

Dear ROOTers,

I would like to create several trees in a program neither leaking memory nor causing segfaults. Unfortunately I do not manage to get rid of the segfaults...

The following is not the program I want to run to create the trees, just the calls in the same order. For now I just would like to know if: -this is the right order of calls?
-there is anything missing or too much? (like SetAutoDelete or so)?

// =================================================================
TFile* fFile = 0;

TTree* fTree = 0;
TreeEvent* e = 0;

void SetOutputFile( const char* filename ) {

    if( fFile ) {

        fFile->Write();
        delete fTree;
        fTree = 0;
        fFile->Close();
        delete fFile;
        fFile = 0;

}

    if( !filename.empty() ) {
        fFile = new TFile( filename, "recreate" );
        fTree = new TTree( "T", "tree" );
        fTree->Branch( "event", "TreeEvent", &e, 16000, 2 );

}

}

int main()
{

    e = new TreeEvent();
    for( int i=0; i<100000; ++i ) {

       SetOutputFile( "fileXY.root" );
       for( int j=0; j<10; ++j ) {
           /* fill event in 'e'... */
           fTree->Fill();
       }

}

    SetOutputFile("");
    delete e;
}
// =================================================================

Thanks for your help,

Alexander Received on Tue Jan 17 2006 - 15:54:20 MET

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:31:56 MET