How to write a tree in the file

Hello,

I have a program that creates a tree first, than fills the tree, opens a file with the name of the current date, writes the tree and closes the file. In a minute procedure repeats. If the date has changed tree->Reset() is called and file is created with the new name.

I create tree before opening a file and do not use SetDirectory method. It seems not to be right thefore I thought to use the following code

tree=new tree();
today=current_date;
while(1){
       tree->Fill();
       if(today!=current_date){
               tree->Reset();
               today=current_date
       }
       file=new TFile("today.root",);
       tree->SetDirectory(file,"RECREATE");
       tree->Write("","TObject::kOverwrite");
       tree->SetDirectory(0); //I do not want tree to disappear after file is closed
       file->Close();
       usleep(1000000);               //sleep for a minute
}

But is it right? Should I use AutoSave(“current date”) or is there more correct way?
This program is memory resident. There is another program-- viewer that reads the file and does some plotting therefore the file needs to be closed.

With respect,
Anton

Anton,

The logic in your example is badly wrong. You should change it
with something like shown below. However, in case you do not know,
ROOT supports reading of a Tree while the Tree is filled by
another process. I give an example in the two short files
in the attachements.

Rene

tree=new tree();
today=current_date;
while(1){
tree->Fill();
if(today!=current_date){
file=new TFile(“today.root”,“RECREATE”);
tree->Write("",“TObject::kOverwrite”);
delete file;
tree->Reset();
today=current_date
}
usleep(1000000); //sleep for a minute
}
treer.C (417 Bytes)
treew.C (370 Bytes)

Thank you Rene for reply,

I do not understand why it is ok not to use SetDirectory method.
It is written in manual that tree belongs to current directory, if I created a tree before creating TFile than tree should belong to the global directory, the open file becomes the current directory and I need to call tree->SetDirectory(TFile) in order to write the tree in this file.

I do not understand the logic of it.

Anton

Calling setDirectory is perfectly OK. Your code snippet was totally wrong.
When sending mails to roottalk or this Forum, we always ask for a concrete running short piece of code, NOT A KIND OF METALANGUAGE
like your code above.

Rene