Problems with writing TTree

I have a TTree, which I fill with data, which I read in pieces from an other source. After each piece, I want to save the tree to a file, so I do:

		TFile *fntp = new TFile("frames_tree.root", "RECREATE");
		stars_tree->Write();
		fntp->Close();

The problem is, that this works properly only if I do it once, for example after reading all the pieces and finishing the task. If I do it more than once, I got mess in my TTree. Is it a normal behaviour?

Could you post a concrete piece of code showing what you do?
Anyhow the first thing to do is to replace

fntp->Close(); by

delete fntp;
Rene

It’s working better in this sample than in my real case, but still bad. The attached file is the wrong case by default - although tframe should change from 1800 to 1802, it is 1802 for all entries. When you save the tree only after both loops (the commented out part), it is saved properly - all 3 values of “tframe”.

In my real case, which is difficult to reproduce, sometimes there are also values of tframe that seem to be random and I cannot scan/draw other variables - root gives some strange errors with includes.
tt.C (849 Bytes)

You had many problems in your script. see my changes below.

Rene

[code]{

Float_t tccdx, tccdy, tra, tdec, tmag;
UShort_t tpval[15][15], tframe;
    TFile *fntp;
    
TTree *stars_tree = new TTree("stars_tree", "Stars Tree");
stars_tree->Branch("frame", &tframe, "frame/s");
stars_tree->Branch("ccdx", &tccdx, "ccdx/F");
stars_tree->Branch("ccdy", &tccdy, "ccdy/F");
stars_tree->Branch("ra", &tra, "ra/F");
stars_tree->Branch("dec", &tdec, "dec/F");
stars_tree->Branch("mag", &tmag, "mag/F");


for(int i=1800; i<1803; i++) {
	fntp = new TFile(Form("frames_tree_%d.root",i), "RECREATE");
            stars_tree->SetDirectory(fntp);
	for(int j=0; j<14000; j++)
	{
		tframe=(UShort_t)i;
		tmag=0.1*j; tccdx=j/5.; tccdy=j/7.;
		tra = tccdx*3.5; tdec=tccdy*5.5;
		stars_tree->Fill();
	}
	stars_tree->Write();
            //disconnect tree from the file, otherwise deleting the file
            //will also delete the tree.
            stars_tree->SetDirectory(0);
	delete fntp;

}

}
[/code]

I guess the main thing was the “disconnect” thing - is it documented anywhere? For sure it workded for me that way with ntuple.

However, I have some questions:

Is it important to open file before filling the tree?

I wrote all the iterations to the same file by intent - when filling a tree takes long time, I like to have intermediate results stored on HD. Is there any simpler way to “flush” contents of TTree to disk?

[quote]Is it important to open file before filling the tree?
[/quote]
If you create the Tree before creating the file, you have to tell the Tree later on that you want to have the tree buffers written to the file (SetDirectory call)

[quote]I wrote all the iterations to the same file by intent - when filling a tree takes long time, I like to have intermediate results stored on HD. Is there any simpler way to “flush” contents of TTree to disk?
[/quote]
see TTree::AutoSave

Rene

Thanks!

I have no idea how all this worked with ntuple…