20 void hadd(
const char* list,
const char* out);
23 int main(
int argc ,
char* argv[]){
28 std::cout<<
" Usage: ./hadd_wcsim.exe wcsim.list wcsim_hadd.root " << std::endl;
29 std::cout<<
" Should provide names of input list and output ROOT file" << std::endl;
30 std::cout<<
" Using default names wcsim.list and wcsim_hadd.root" << std::endl;
33 list = argv[1]; std::cout<<
"Input list: "<< list << std::endl;
34 out = argv[2]; std::cout<<
"Output ROOT file:: "<< out << std::endl;
38 wcsimdirenv = getenv (
"WCSIMDIR");
39 if(wcsimdirenv != NULL){
40 gSystem->Load(
"${WCSIMDIR}/libWCSimRoot.so");
43 cout<<
"Must set WCSIMDIR" << std::endl;
46 std::cout<<
"Run hadd function"<< std::endl;
48 std::cout<<
"Completed hadd function"<< std::endl;
52 void hadd(
const char* list,
const char* out){
54 Target = TFile::Open( out ,
"RECREATE" );
58 std::ifstream w_file( list );
59 if (w_file.is_open()) {
61 while (getline(w_file, w_line)) {
62 FileList->Add( TFile::Open( w_line.c_str() ));
73 TString path( (
char*)strstr( target->GetPath(),
":" ) );
76 TFile *first_source = (TFile*)sourcelist->First();
77 first_source->cd( path );
78 TDirectory *current_sourcedir = gDirectory;
80 Bool_t status = TH1::AddDirectoryStatus();
81 TH1::AddDirectory(kFALSE);
84 TChain *globChain = 0;
85 TIter nextkey( current_sourcedir->GetListOfKeys() );
87 while ( (key = (TKey*)nextkey())) {
90 if (oldkey && !strcmp(oldkey->GetName(),key->GetName()))
continue;
93 first_source->cd( path );
96 if ( obj->IsA()->InheritsFrom( TH1::Class() ) ) {
104 TFile *nextsource = (TFile*)sourcelist->After( first_source );
105 while ( nextsource ) {
108 nextsource->cd( path );
109 TKey *key2 = (TKey*)gDirectory->GetListOfKeys()->FindObject(h1->GetName());
111 TH1 *h2 = (TH1*)key2->ReadObj();
116 nextsource = (TFile*)sourcelist->After( nextsource );
119 else if ( obj->IsA()->InheritsFrom( TTree::Class() ) ) {
122 const char* obj_name= obj->GetName();
124 globChain =
new TChain(obj_name);
125 globChain->Add(first_source->GetName());
126 TFile *nextsource = (TFile*)sourcelist->After( first_source );
129 while ( nextsource ) {
131 globChain->Add(nextsource->GetName());
132 nextsource = (TFile*)sourcelist->After( nextsource );
135 }
else if ( obj->IsA()->InheritsFrom( TDirectory::Class() ) ) {
138 cout <<
"Found subdirectory " << obj->GetName() << endl;
142 TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() );
152 cout <<
"Unknown object type, name: " 153 << obj->GetName() <<
" title: " << obj->GetTitle() << endl;
164 if(obj->IsA()->InheritsFrom( TTree::Class() ))
165 globChain->Merge(target->GetFile(),0,
"keep");
167 obj->Write( key->GetName() );
173 target->SaveSelf(kTRUE);
174 TH1::AddDirectory(status);
int main(int argc, char *argv[])
void MergeRootfile(TDirectory *target, TList *sourcelist)
void hadd(const char *list, const char *out)