How to save a big tree

Dear Experts,

I am trying to save a tree with 100k events.

  1. If the code is like below
TFile rootfile(rootfilename, "recreate");
TTree* tree = new TTree("tree", "tree");
(Fill the tree)
rootfile.Close();

Some events will not be saved in the file.

  1. If the code is like below
TFile rootfile(rootfilename, "recreate");
TTree* tree = new TTree("tree", "tree");
(Fill the tree)
tree->Write();
rootfile.Close();

There will be 2 trees saved in the file with different size.

  1. If the code is like below
TFile rootfile(rootfilename, "recreate");
TTree* tree = new TTree("tree", "tree");
(Fill the tree)
tree->Write();
rootfile.Close();

There will be 2 trees saved in the file with different size.

What should I do to make the tree saved properly?

_ROOT Version: 6.26/10
_Platform: WSL2
Compiler: Not Provided

TFile rootfile(rootfilename, "recreate");
TTree* tree = new TTree("tree", "tree");
(Fill the tree)
tree->Write();
rootfile.Close();

works and

TFile rootfile(rootfilename, "recreate");
TTree* tree = new TTree("tree", "tree");
(Fill the tree)
rootfile.Write();
// The call to Close is implicit is the destruction of the local `TFile` object

works better.

In either case, there will appear in the file listing 2 entries for the TTree, one is the complete version and the other is a backup version that was written during one the call to Fill to be used in case the process dies unexpected before the final call to Write to be able to recover some of the data.

But It seems that the cycle number of the complete tree is unpredictable. This makes it complex to get the complete tree.
I’ve found an ugly solution, which consumes much more memory, to get a file with only one complete tree:

TTree* tree = new TTree("tree", "tree");
(Fill the tree)
TFile rootfile(rootfilename, "recreate");
tree->Write();
rootfile.Close();

I wonder if there are better solutions

It should be very predictable. How is it not? (The highest cycle number should be the latest version and this version should be the one returned unless you specify another cycle explicitly)

which consumes much more memory,

And it limits the size of the output tree to the size the installed RAM (+ swap space).

I see, so the smaller tree doesn’t make the ROOT file size bigger. And the function

file.Get<Tree>("name_without_cyclenumber")

returns the tree with a bigger cycle number by default.
Actually, I still feel it is not “clean”. But it should be the best way.
Thank you for your help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.