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:
- TTree
1.1 TDirectory
1.1.1 TH1
1.1.2 TH1
…
1.2 TDirectory
… - 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