Merge filtered events from multiple files

Hi all,

My macro is intended to take multiple TFiles, all with the same tree structure, and write only the interesting events to an output file. For a single file, this works fine. However, when I start including more files, rather than get a single tree in the output file, I get one tree (each of which has the same name) for every input file. I have tried numerous methods to get these output trees to merge, but have not yet been successful. I think the CloneTree may be doing something I don’t expect. The macro is attached to this post. Any help you can provide would be much appreciated.

thinGrid.C (2.34 KB)

Do something like

TChain ch("treename"); ch.Add(...); //add all your files ch.LoadTree(0); //force loading first tree TFile fnew("newfile.root","recreate"); TTree *Tnew = (TTree*)ch.GetTree()->CloneTree(0); //then proceed like for one single file, looping on all entries in teh TChain

Hi Rene,

That works quite well, thanks. However, there is one small piece of strange behavior. When I run this code for a small number of files/events, everything runs as expected. For example, when I run on a file that is a simple merge of all all of the input files, I get 1242 events in a single tree. But when I increase the number of files (there were 47 individual files that contributed to the merge), I get two problems: first, I get an error (“Error in TFile::Open: no url specified”) during running; second, I end up with a “second tree.” This is the output of ls():

root [3]
TFile** _thin.root
TFile* _thin.root
KEY: TTree physics;2 physics
KEY: TTree physics;1 physics

When I look at the trees via the command line (physics->Draw(“EventNumber”) I get the proper number of entries (1242) and all the data looks correct. When I use the TBrowser, I can look at the 2 trees separately. One has 1242, and the other has 980. However, the size of this file is only ~10% larger than when using the merged file. So this “second tree” is not real. Any ideas as to why this would occur?

thinGrid_chain.C (2.21 KB)

It is likely that you are using an old version of ROOT where the MaxTreeSize was set to 1.9 GBytes. See TTree::SetMaxTreeSize. With 5.26 or newer this limit has been set to Terabytes. When reaching the MaxTreeSize ROOT automatically creates a new file.

Concerning the second point (2 cycles of the Tree Header) see TTree::SetAutoSave, TTree::AutoSave for more details.