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 | #include <stdlib.h> |
26 | |
27 | #include <string> |
28 | #include <iostream> |
29 | #include <iomanip> |
30 | using namespace std; |
31 | |
32 | #include <TChain.h> |
33 | #include <TFile.h> |
34 | #include <TH1.h> |
35 | #include <TTree.h> |
36 | #include <TKey.h> |
37 | #include <Riostream.h> |
38 | |
39 | TList *FileList; |
40 | TFile *Target; |
41 | |
42 | const char *OUTPUT_FILENAME = "merged.root"; |
43 | |
44 | void Usage(void); |
45 | void ParseCommandLineArguments(int narg, char* argv[]); |
46 | void MergeRootfile( TDirectory *target, TList *sourcelist ); |
47 | |
48 | |
49 | |
50 | |
51 | int main(int narg, char* argv[]) |
52 | { |
53 | |
54 | FileList = new TList(); |
55 | |
56 | |
57 | ParseCommandLineArguments(narg, argv); |
58 | |
59 | |
60 | Target = new TFile(OUTPUT_FILENAME,"RECREATE","Produced by root_merge"); |
61 | cout<<"Opened ROOT file \""<<OUTPUT_FILENAME<<"\" ..."<<endl; |
62 | |
63 | |
64 | MergeRootfile( Target, FileList ); |
| |
65 | |
66 | |
67 | Target->Write(); |
68 | delete Target; |
69 | cout<<endl<<"Closed ROOT file: "<<OUTPUT_FILENAME<<endl; |
70 | |
71 | return 0; |
72 | } |
73 | |
74 | |
75 | |
76 | |
77 | void Usage(void) |
78 | { |
79 | cout<<endl; |
80 | cout<<" root_merge [options] infput_file1 input_file2 input_file3 ..."<<endl; |
81 | cout<<endl; |
82 | cout<<"options:"<<endl; |
83 | cout<<" -o filename set output filename (def:"<<OUTPUT_FILENAME<<")"<<endl; |
84 | cout<<" -h Print this usage statement"<<endl; |
85 | cout<<endl; |
86 | cout<<endl; |
87 | |
88 | exit(0); |
89 | } |
90 | |
91 | |
92 | |
93 | |
94 | void ParseCommandLineArguments(int narg, char* argv[]) |
95 | { |
96 | if(narg==1)Usage(); |
97 | |
98 | for(int i=1; i<narg; i++){ |
99 | if(argv[i][0] == '-'){ |
100 | switch(argv[i][1]){ |
101 | case 'o': |
102 | i++; |
103 | OUTPUT_FILENAME = argv[i]; |
104 | break; |
105 | case 'h': |
106 | Usage(); |
107 | break; |
108 | default: |
109 | cout<<"Unknown option \""<<argv[i]<<"\". Aborting."<<endl; |
110 | exit(-1); |
111 | } |
112 | }else{ |
113 | FileList->Add( TFile::Open(argv[i])); |
114 | } |
115 | } |
116 | } |
117 | |
118 | #if 0 |
119 | void hadd() { |
120 | |
121 | |
122 | |
123 | |
124 | |
125 | Target = TFile::Open( "result.root", "RECREATE" ); |
126 | |
127 | FileList = new TList(); |
128 | FileList->Add( TFile::Open("hsimple1.root") ); |
129 | FileList->Add( TFile::Open("hsimple2.root") ); |
130 | |
131 | MergeRootfile( Target, FileList ); |
132 | |
133 | } |
134 | #endif |
135 | |
136 | |
137 | |
138 | |
139 | void MergeRootfile( TDirectory *target, TList *sourcelist ) { |
140 | |
141 | |
142 | TString path( (char*)strstr( target->GetPath(), ":" ) ); |
143 | path.Remove( 0, 2 ); |
144 | |
145 | TFile *first_source = (TFile*)sourcelist->First(); |
146 | first_source->cd( path ); |
147 | TDirectory *current_sourcedir = gDirectory(TDirectory::CurrentDirectory()); |
148 | |
149 | |
150 | TChain *globChain = 0; |
| 2 | | 'globChain' initialized to a null pointer value | |
|
151 | TIter nextkey( current_sourcedir->GetListOfKeys() ); |
152 | TKey *key; |
153 | while ( (key = (TKey*)nextkey())) { |
| 3 | | Loop condition is true. Entering loop body | |
|
| 9 | | Loop condition is true. Entering loop body | |
|
| 15 | | Loop condition is true. Entering loop body | |
|
| 21 | | Loop condition is true. Entering loop body | |
|
154 | |
155 | |
156 | first_source->cd( path ); |
157 | TObject *obj = key->ReadObj(); |
158 | |
159 | if ( obj->IsA()->InheritsFrom( "TH1" ) ) { |
| |
| |
| |
| |
160 | |
161 | |
162 | |
163 | TH1 *h1 = (TH1*)obj; |
164 | |
165 | |
166 | |
167 | TFile *nextsource = (TFile*)sourcelist->After( first_source ); |
168 | while ( nextsource ) { |
| 23 | | Loop condition is false. Execution continues on line 223 | |
|
169 | |
170 | |
171 | nextsource->cd( path ); |
172 | TH1 *h2 = (TH1*)gDirectory(TDirectory::CurrentDirectory())->Get( h1->GetName() ); |
173 | if ( h2 ) { |
174 | h1->Add( h2 ); |
175 | delete h2; |
176 | |
177 | } |
178 | |
179 | nextsource = (TFile*)sourcelist->After( nextsource ); |
180 | } |
181 | } |
182 | else if ( obj->IsA()->InheritsFrom( "TTree" ) ) { |
| |
| |
| |
183 | |
184 | |
185 | const char* obj_name= obj->GetName(); |
186 | |
187 | globChain = new TChain(obj_name); |
188 | globChain->Add(first_source->GetName()); |
189 | TFile *nextsource = (TFile*)sourcelist->After( first_source ); |
190 | |
191 | |
192 | while ( nextsource ) { |
193 | |
194 | globChain->Add(nextsource->GetName()); |
195 | nextsource = (TFile*)sourcelist->After( nextsource ); |
196 | } |
197 | |
198 | } else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) { |
| |
| |
| |
199 | |
200 | |
201 | cout << "Found subdirectory " << obj->GetName() << endl; |
202 | |
203 | |
204 | target->cd(); |
205 | TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() ); |
206 | |
207 | |
208 | |
209 | |
210 | MergeRootfile( newdir, sourcelist ); |
211 | |
212 | } else { |
213 | |
214 | |
215 | cout << "Unknown object type, name: " |
216 | << obj->GetName() << " title: " << obj->GetTitle() << endl; |
217 | } |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | if ( obj ) { |
| |
| |
| |
| |
224 | target->cd(); |
225 | |
226 | |
227 | if(obj->IsA()->InheritsFrom( "TTree" )) |
| |
| |
| |
| |
228 | globChain->Write( key->GetName() ); |
| 26 | | Called C++ object pointer is null |
|
229 | else |
230 | obj->Write( key->GetName() ); |
231 | } |
232 | |
233 | } |
234 | |
235 | |
236 | target->Write(); |
237 | |
238 | } |
239 | |
240 | |