How to use TChain to combine many .root files

I have recently started using root and I cant figure out how to use TChain command. I have a bunch of “L1 Ntuple_.root" that are consisted of many trees ("” stands for the number on the file and goes like 1,2,3,4…) and I am trying to use TChain to combine the entries from these trees into one tree, I proceed as following:

TChain* chain = new TChain (“L1EventTree”)

/* where L1EventTree is one of the trees inside the file*/

chain->Add(“path/to/file/L1Ntuple_1.root/l1EventTree/L1EventTree”)
chain->Add(“path/to/file/L1Ntuple_2.root/l1EventTree/L1EventTree”)
chain->Add(“path/to/file/L1Ntuple_3.root/l1EventTree/L1EventTree”)
.
.
.
.
.
TFile* f = new TFile(“output”,“create”);
f->Write();
f->Close();

but when I use TBrowser it shows an empty output file. I am not sure what I am missing here. Any help is appreciated.

Try to execute:
rootls --help
rootcp --help
hadd --help

1 Like

Inside root I did .x hadd --help and .x rootcp --help and it gives me an error. is that what you were suggesting?

These are standalone executables.

Thank you Wile_E_Coyote this is much better than TChain and merging each tree one by one, but I am still wondering why TChain didn’t work in my case.

Instead of

TFile* f = new TFile("output","create");

you should have tried

chain.Merge("outputname.root")
2 Likes

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