forked from ekfriis/farmout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeFiles.C
281 lines (221 loc) · 7.73 KB
/
mergeFiles.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
This macro will add histograms from a list of root files and write them
to a target root file. The target file is newly created and must not be
identical to one of the source files.
Author: Sven A. Schmidt, sven.schmidt@cern.ch
Date: 13.2.2001
This code is based on the hadd.C example by Rene Brun and Dirk Geppert,
which had a problem with directories more than one level deep.
(see macro hadd_old.C for this previous implementation).
The macro from Sven has been enhanced by
Anne-Sylvie Nicollerat <Anne-Sylvie.Nicollerat@cern.ch>
to automatically add Trees (via a chain of trees).
Further "enhanced" by S. Dasu <dasu@hep.wisc.edu>
NB: This macro was provided as a tutorial.
Used to be $ROOTSYS/bin/hadd to merge many histogram files
Modified now to take directory with root files as an argument
*/
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <string>
using std::string;
#include <string.h>
#include "TSystem.h"
#include "TChain.h"
#include "TFile.h"
#include "TH1.h"
#include "TTree.h"
#include "TKey.h"
#include "Riostream.h"
TFile *Target = NULL;
TFile *FirstSource = NULL;
bool InitializedMergeObjects = false;
TMap MergeObjects;
TMap MergeChains;
void MergeRootfile( TDirectory *target, TList *sourcelist );
void mergeFiles(const char* outputFile, const char* inputFilesDirectory) {
// In an interactive ROOT session
// Do the following
// root > .L mergeFiles.C
// root > mergeFiles("<outputFile>", "<inputFilesDirectory>")
Target = TFile::Open(outputFile, "RECREATE");
if(strncmp(inputFilesDirectory,"list-in-file:",13)==0) {
// Read input file list from a file
char const *list_in_file = inputFilesDirectory+13;
FILE *fp = fopen(list_in_file,"r");
if( !fp ) {
int the_errno = errno;
cerr << "Failed to open " << list_in_file << ": errno=" << the_errno << " " << strerror(errno) << endl;
return;
}
char fname[2048];
while( fgets(fname,sizeof(fname),fp)!=NULL ) {
char *newline = strchr(fname,'\n');
if( newline ) *newline = '\0';
if( !fname[0] ) continue;
cout << "Merging root file: " << fname << endl;
MergeRootfile( Target, fname );
}
fclose(fp);
}
else {
void* dirp = gSystem->OpenDirectory(inputFilesDirectory);
const char *entry = gSystem->GetDirEntry(dirp);
while(entry != 0)
{
int len = strlen(entry);
if(len >= 5 && strcmp(&entry[len - 5], ".root") == 0)
{
TString fileName;
if(strncmp(inputFilesDirectory, "/pnfs", 5) == 0)
{
fileName = "dcap://";
}
fileName += inputFilesDirectory;
if( !fileName.EndsWith("/") ) fileName += "/";
fileName += entry;
cout << "Merging root file: " << fileName << endl;
MergeRootfile( Target, fileName );
}
entry = gSystem->GetDirEntry(dirp);
}
}
// save modifications to target file
WriteMergeObjects( Target );
delete Target;
Target = NULL;
}
void SplitPathName(TString pathname,TString *path,TString *name) {
int slash = pathname.Last('/');
*path = pathname(0,slash);
*name = pathname(slash+1,pathname.Length()-slash-1);
}
void WriteMergeObjects( TFile *target ) {
cout << "Writing the merged data." << endl;
TIterator *nextobj = MergeObjects.MakeIterator();
TObjString *pathname_obj;
while( (pathname_obj = (TObjString *)nextobj->Next()) ) {
TString path,name;
SplitPathName(pathname_obj->String(),&path,&name);
TObject *obj = MergeObjects.GetValue(pathname_obj);
target->cd(path);
obj->Write( name );
delete obj;
}
MergeObjects.Clear();
target->Write();
// Temporarily let multiple root files remain if > 2GB
// Prevent Target_1.root Target_2.root, ... from happening.
// long long max_tree_size = 200000000000LL; // 200 GB
// if(TTree::GetMaxTreeSize() < max_tree_size ) {
// TTree::SetMaxTreeSize(max_tree_size);
// }
nextobj = MergeChains.MakeIterator();
TObjString *pathname_obj;
while( (pathname_obj = (TObjString *)nextobj->Next()) ) {
TString path,name;
SplitPathName(pathname_obj->String(),&path,&name);
TChain *ch = (TChain *)MergeChains.GetValue(pathname_obj);
target->cd(path);
ch->Merge(target,0,"KEEP");
delete ch;
// in case of multiple objects with same pathname, must remove
// this one from the list so we don't get the same (deleted)
// one next time we look up the same name
MergeChains.Remove(pathname_obj);
}
MergeChains.Clear();
InitializedMergeObjects = false;
}
void InitMergeObjects( TDirectory *target, TFile *source ) {
TString path( (char*)strstr( target->GetPath(), ":" ) );
path.Remove( 0, 1 );
source->cd( path );
TDirectory *current_sourcedir = gDirectory;
// loop over all keys in this directory
TIter nextkey( current_sourcedir->GetListOfKeys() );
TKey *key;
while ( (key = (TKey*)nextkey())) {
// read object from source file
source->cd( path );
TObject *obj = key->ReadObj();
const char* obj_name= obj->GetName();
TString pathname = path + TString("/") + key->GetName();
if ( obj->IsA()->InheritsFrom( "TH1" ) ) {
target->cd( path );
TObject *h2 = obj->Clone();
MergeObjects.Add(new TObjString(pathname),h2);
}
else if ( obj->IsA()->InheritsFrom( "TTree" ) ) {
//TChain *ch = new TChain(obj_name); // Not quite right. Mike Anderson Julyl 1 2008
TChain *ch = new TChain(pathname);
MergeChains.Add(new TObjString(pathname),ch);
ch->Add(source->GetName());
} else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) {
// it's a subdirectory
cout << "Found subdirectory " << obj->GetName() << endl;
// create a new subdir of same name and title in the target file
target->cd();
TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() );
// newdir is now the starting point of another round of merging
// newdir still knows its depth within the target file via
// GetPath(), so we can still figure out where we are in the recursion
InitMergeObjects( newdir, source );
} else {
// object is of no type that we know or can handle
cout << "Unknown object type, name: "
<< obj->GetName() << " title: " << obj->GetTitle() << endl;
}
} // while ( ( TKey *key = (TKey*)nextkey() ) )
}
void MergeRootfile( TDirectory *target, TString source_name ) {
TFile *source = NULL;
if( !InitializedMergeObjects ) {
InitializedMergeObjects = true;
source = TFile::Open(source_name);
cout << "Initializing merge objects from " << source_name << endl;
InitMergeObjects( target, source );
delete source;
return;
}
// loop over all objects to be merged
TIterator *nextobj = MergeObjects.MakeIterator();
TObjString *pathname_obj;
while( (pathname_obj = (TObjString *)nextobj->Next()) ) {
TString path,name;
SplitPathName(pathname_obj->String(),&path,&name);
TObject *obj = MergeObjects.GetValue(pathname_obj);
if ( obj->IsA()->InheritsFrom( "TH1" ) ) {
// descendant of TH1 -> merge it
TH1 *h1 = (TH1*)obj;
if( !source ) {
source = TFile::Open(source_name);
}
// make sure we are at the correct directory level by cd'ing to path
source->cd( path );
TH1 *h2 = (TH1*)gDirectory->Get( h1->GetName() );
if ( h2 ) {
h1->Add( h2 );
delete h2;
}
}
}
delete nextobj;
nextobj = NULL;
// loop over all chains to be merged
nextobj = MergeChains.MakeIterator();
TObjString *pathname_obj;
while( (pathname_obj = (TObjString *)nextobj->Next()) ) {
TString path,name;
SplitPathName(pathname_obj->String(),&path,&name);
TChain *ch = (TChain *)MergeChains.GetValue(pathname_obj);
ch->Add(source_name);
}
delete nextobj;
nextobj = NULL;
if( source ) {
delete source;
}
}