How to merge ROOT files containing TDirectory and TTree?

I was trying to merge some ROOT files and i stumbled upon a program(link below)

root.cern.ch/root/html/examples/hadd.C.html


#include
#include “TChain.h”
#include “TFile.h”
#include “TH1.h”
#include “TTree.h”
#include “TKey.h”
#include “Riostream.h”

TList *FileList;
TFile *Target;

void MergeRootfile( TDirectory *target, TList *sourcelist );

void hadd() {
// in an interactive ROOT session, edit the file names
// Target and FileList, then
// root > .L hadd.C
// root > hadd()

Target = TFile::Open( “result.root”, “RECREATE” );

FileList = new TList();
FileList->Add( TFile::Open(“hsimple1.root”) );
FileList->Add( TFile::Open(“hsimple2.root”) );

MergeRootfile( Target, FileList );

}

void MergeRootfile( TDirectory *target, TList *sourcelist ) {

// cout <Write()" below
if ( obj ) {
target->cd();

  //!!if the object is a tree, it is stored in globChain...
if(obj->IsA()->InheritsFrom( "TTree" ))
  globChain->Write( key->GetName() );
else
obj->Write( key->GetName() );
}

} // while ( ( TKey key = (TKey)nextkey() ) )

// save modifications to target file
target->Write();

}

which claims to be capable of merging also TTree and derived classes in
addition to TDirectory and TH1, in difference from the usual HADD
routine which doesnt work for TTrees.

I ran it on dataset files which the the following structure:

DATASET:

  1. TTree
    1.1 TDirectory
    1.1.1 TH1
    1.1.2 TH1

    1.2 TDirectory
  2. TDirectory
    2.1 TH1
    2.2 TH1

it is a modified version of standard HADD. It worked and merged the root TDirectory and all TH1 under it, but it only created a TChain with the name of the root TTree and left it empty.

I looked through the code, but as I have no big experience with working
with TTree, I couldnt see what is wrong.

I was wondering someone on this forum could provide me some info as to how to correct the code or if there is any other code capable of merging also TTree structures.

Thanks a lot in advance,

Best Regards,
Hayk Hakobyan

Use $ROOTSYS/bin/hadd to merge ROOT files containing histograms and Trees. hadd support the case of directories

Rene

Dear Rene,

I already tried, before even stumbling upon the modified version of HADD as previously posted, the standard HADD available on the location you have pointed out.

And it crashed when i ran it on my two sample ROOT files, trying to merge them into the third. Although it has crashed, there was nonetheless a merged root file produced which included the merged root TDirectory and all TH1s (also merged) under it but it didnt include (nor have) the TTree of the two ROOT files and its underlying TBrachElements.

For the sake of clarity and in order to find out what went wrong i will attach my original two ROOT files and I will ask you (or whoever who can) to take a look at them and try to apply the HADD and see why it isnt working.

I would also like to know if there is any other alternative merging tool that i could use.

Thanks a lot in advance,

Hayk
histo2.root (57.1 KB)
histo1.root (56.9 KB)

I do not see any problem when using hadd with these two files

hadd -f junk.root histo1.root histo2.root Target file: junk.root Source file 1: histo1.root Source file 2: histo2.root Target path: junk.root:/ Found subdirectory Electron Target path: junk.root:/Electron

Rene

1 Like