File indexing completed on 2025-08-05 08:10:57
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <fstream>
0011 #include <string.h>
0012 #include "TChain.h"
0013 #include "TFile.h"
0014 #include "TH1.h"
0015 #include "TTree.h"
0016 #include "TKey.h"
0017 #include "Riostream.h"
0018
0019
0020
0021
0022 void MergeRootfile(TDirectory *target, TList *sourcelist);
0023
0024
0025
0026 void MergeFiles(const Int_t nFiles=100, const TString filelist= "file.list", const TString outputrootfiles ="test.root") {
0027
0028 ifstream files;
0029
0030 files.open(filelist);
0031
0032 char file_stm[800];
0033 int line=0;
0034
0035 TList *FileList;
0036 TFile *Target;
0037
0038
0039 TString filename_output;
0040 TString chain_;
0041 Int_t start_num = 0;
0042 Int_t end_num = 4;
0043
0044 Target = TFile::Open(outputrootfiles, "RECREATE");
0045 FileList = new TList();
0046
0047 while(files.good()) {
0048 files >> file_stm;
0049
0050
0051 if ((line + 1) > nFiles) {
0052 cout << "WARNING: Whoah there, tiger! Only need " << nFiles << " files!\n"
0053 << " Moving on to processing files."
0054 << endl;
0055 break;
0056 }
0057
0058
0059
0060 TFile f(file_stm);
0061
0062
0063
0064
0065 Bool_t fOk = f.IsZombie();
0066 cout<<"file# "<<line<<" "<< file_stm<<" zombi= "<<fOk<<endl;
0067
0068 if(!fOk)
0069 {
0070 FileList->Add(TFile::Open(file_stm));
0071 }
0072
0073
0074
0075 if (!files.good()) break;
0076 line++;
0077 }
0078 MergeRootfile( Target, FileList );
0079
0080
0081 }
0082
0083
0084
0085 void MergeRootfile( TDirectory *target, TList *sourcelist ) {
0086
0087
0088 TString path( (char*)strstr( target->GetPath(), ":" ) );
0089 path.Remove( 0, 2 );
0090
0091 TFile *first_source = (TFile*)sourcelist->First();
0092 first_source->cd( path );
0093 TDirectory *current_sourcedir = gDirectory;
0094
0095 Bool_t status = TH1::AddDirectoryStatus();
0096 TH1::AddDirectory(kFALSE);
0097
0098
0099 TChain *globChain = 0;
0100 TIter nextkey( current_sourcedir->GetListOfKeys() );
0101 TKey *key, *oldkey=0;
0102 while ( (key = (TKey*)nextkey())) {
0103
0104
0105 if (oldkey && !strcmp(oldkey->GetName(),key->GetName())) continue;
0106
0107
0108 first_source->cd( path );
0109 TObject *obj = key->ReadObj();
0110
0111 if ( obj->IsA()->InheritsFrom( TH1::Class() ) ) {
0112
0113
0114
0115 TH1 *h1 = (TH1*)obj;
0116
0117
0118
0119 TFile *nextsource = (TFile*)sourcelist->After( first_source );
0120 while ( nextsource ) {
0121
0122
0123 nextsource->cd( path );
0124 TKey *key2 = (TKey*)gDirectory->GetListOfKeys()->FindObject(h1->GetName());
0125 if (key2) {
0126 TH1 *h2 = (TH1*)key2->ReadObj();
0127 h1->Add( h2 );
0128 delete h2;
0129 }
0130
0131 nextsource = (TFile*)sourcelist->After( nextsource );
0132 }
0133 }
0134 else if ( obj->IsA()->InheritsFrom( TTree::Class() ) ) {
0135
0136
0137 const char* obj_name= obj->GetName();
0138
0139 globChain = new TChain(obj_name);
0140 globChain->Add(first_source->GetName());
0141 TFile *nextsource = (TFile*)sourcelist->After( first_source );
0142
0143
0144 while ( nextsource ) {
0145
0146 globChain->Add(nextsource->GetName());
0147 nextsource = (TFile*)sourcelist->After( nextsource );
0148 }
0149
0150 } else if ( obj->IsA()->InheritsFrom( TDirectory::Class() ) ) {
0151
0152
0153 cout << "Found subdirectory " << obj->GetName() << endl;
0154
0155
0156 target->cd();
0157 TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() );
0158
0159
0160
0161
0162 MergeRootfile( newdir, sourcelist );
0163
0164 } else {
0165
0166
0167 cout << "Unknown object type, name: "
0168 << obj->GetName() << " title: " << obj->GetTitle() << endl;
0169 }
0170
0171
0172
0173
0174
0175 if ( obj ) {
0176 target->cd();
0177
0178
0179 if(obj->IsA()->InheritsFrom( TTree::Class() ))
0180 globChain->Merge(target->GetFile(),0,"keep");
0181 else
0182 obj->Write( key->GetName() );
0183 }
0184
0185 }
0186
0187
0188 target->SaveSelf(kTRUE);
0189 TH1::AddDirectory(status);
0190 }
0191
0192