Hadd options for merging files


I am wanting to know if there is an option for hadd so that it adds files together 1 entry per file and then the 2nd entry per file? I have 4 ntuples that I want to merge together but preferably I would like the 1st entry from each file merged and the then 2nd and then the 3rd and so on. This is so that I can run the resultant file through a BDT and train on events frm all of the 4 ntuples equally. Is this possible with hadd or is there another way to do this please?

ROOT Version: 6.26/08
Built for linuxx8664gcc on Oct 18 2022, 08:30:35

hadd can not do it. you will need something like:

std::string firstfilename;
std::vector<string> filenames;
std::unique_ptr<TFile> first{ TFile::Open(firstfilename.c_str() };
std::vector<std::unique_ptr<TFile>> files;
for(const auto &name : filenames)
   files.emplace_back( TFile::Open(name.c_str());

std::vector<TTree*> trees;
TTree *first_tree = first.Get<TTree>(treename.c_str());

std::unique_ptr<TFile> output_file{ TFile::Open( output_filename.c_str(), "RECREATE" ) };
TTree *output_tree = first_tree->CloneTree(0);

for(auto &file : files) {
   TTree *t = file.Get<TTree>(treename.c_str());
   first_tree->CopyAddresses( t );
   trees.push_back( t );

for(Long64_t e = 0; e < nentries; first_tree->GetEntries()) {
   for(auto &t : trees) {
      t.GetEntry( e ) ;


(with a few details to be filled out).

Ok, thank you very much for answering. I will try your suggestion and see if I can get that to work. Thanks