ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/Analysis.cc
Revision: 1.1
Committed: Tue May 27 19:50:16 2008 UTC (16 years, 11 months ago) by loizides
Content type: text/plain
Branch: MAIN
Log Message:
First import of TreeModules.

File Contents

# Content
1 // $Id: Analysis.cxx 4604 2007-11-01 15:21:50Z loizides $
2
3 #include "MitAna/TreeMod/interface/Analysis.h"
4 #include <Riostream.h>
5 #include <TFile.h>
6 #include <TList.h>
7 #include <TDSet.h>
8 #include <TChain.h>
9 #include <TObjString.h>
10 #include <TSystem.h>
11 #include <TProof.h>
12 #include <TROOT.h>
13 #include "MitAna/DataUtil/interface/Debug.h"
14 #include "MitAna/TAM/interface/TAMVirtualLoader.h"
15 #include "MitAna/TAM/interface/TAModule.h"
16 #include "MitAna/TAM/interface/TAMSelector.h"
17
18 ClassImp(mithep::Analysis)
19
20 using namespace mithep;
21
22 //__________________________________________________________________________________________________
23 Analysis::Analysis(Bool_t up) :
24 fUseProof(up),
25 fHierachy(kFALSE),
26 fState(kPristine),
27 fNFriends(0),
28 fList(new TList),
29 fOutput(0),
30 fPackages(new TList),
31 fLoaders(new TList),
32 fSuperMod(0),
33 fSelector(0),
34 fChain(0),
35 fSet(0),
36 fDeleteList(new TList),
37 fTreeName("todo"),
38 fCompLevel(2),
39 fProof(0)
40 {
41 // Default constructor.
42
43 fList->SetOwner();
44 fDeleteList->SetOwner();
45
46 /* default packages for PROOF upload */
47 fPackages->SetOwner();
48 // nothing to be done since we do not use par files (yet?)
49 }
50
51 //__________________________________________________________________________________________________
52 Analysis::~Analysis()
53 {
54 // Destructor.
55
56 if (fState == kInit || fState == kRun)
57 Terminate();
58
59 delete fList;
60 delete fDeleteList;
61 delete fLoaders;
62 delete fSelector;
63 delete fPackages;
64 fOutput = 0; // owned by TAM
65 fSuperMod = 0; // owned by user
66
67 delete fProof;
68 }
69
70 //__________________________________________________________________________________________________
71 Bool_t Analysis::AddFile(const char *pname)
72 {
73 // Add file with given name to the list of files to be processed.
74 // Using the token "|", you can specify an arbritray number
75 // of paths to tree files that will be concatenated as friend
76 // trees.
77
78 if (fState != kPristine) {
79 Error("AddFile", "Analysis already initialized");
80 return kFALSE;
81 }
82
83 TString pnamestr(pname);
84 TString tok("|");
85 TObjArray *arr = pnamestr.Tokenize(tok);
86 TString msg;
87
88 for(Int_t i=0; i<arr->GetEntries(); i++){
89
90 TObjString *dummy = dynamic_cast<TObjString*>(arr->At(i));
91 if(!dummy) continue;
92
93 AddFile(dummy->GetName(),i);
94 if(i==0) msg=dummy->GetName();
95 else {
96 Info("AddFile", "Add file %s as friend to %s",
97 dummy->GetName(), msg.Data());
98 }
99 }
100 delete arr;
101
102 return kTRUE;
103 }
104
105 //________________________________________________________________________
106 void Analysis::AddFile(const char *pname, Int_t eventlist)
107 {
108 // Add file name to the event list specified by eventlist. The lists
109 // are used to hold filenames of different types of events. In case
110 // you dont want friend trees, just give no eventlist argument (default 0).
111
112 MitAssert("AddFile", pname != 0);
113
114 TList *l = 0;
115 if (eventlist >= 0 && eventlist < fNFriends) {
116
117 l = dynamic_cast<TList*>(fList->At(eventlist));
118 if (!l) {
119 Fatal("AddFile", "Requested list %d not found!", eventlist);
120 return;
121 }
122
123 } else if (eventlist == fNFriends) {
124
125 l = new TList;
126 l->SetOwner();
127 fList->Add(l);
128 fNFriends++;
129
130 } else if (eventlist < 0 || eventlist > fNFriends) {
131 Error("AddFile", "Specified list %d not in [0,%d]", eventlist, fNFriends);
132 return;
133 }
134
135 if(!IsValidName(pname)) return;
136
137 l->Add(new TObjString(pname));
138
139 MDB(kAnalysis, 2)
140 Info("AddFile", "Added %s to list of files.", pname);
141 }
142
143 //__________________________________________________________________________________________________
144 void Analysis::AddFile(const TObject *oname, Int_t eventlist)
145 {
146 // Add file name to the event list specified by eventlist. The lists
147 // are used to hold filenames of different types of events. In case
148 // you dont want mixing, just give no eventlist argument (default 0).
149
150 MitAssert("AddFile", oname != 0);
151
152 return AddFile(oname->GetName(), eventlist);
153 }
154
155 //__________________________________________________________________________________________________
156 void Analysis::AddList(TList *list, Int_t eventlist)
157 {
158 // Add file name to the event list specified by eventlist. The lists
159 // are used to hold filenames of different types of events. In case
160 // you dont want mixing, just give no eventlist argument (default 0).
161
162 MitAssert("AddList", list != 0);
163
164 TIter next(list);
165 while (TObject *obj = next())
166 AddFile(obj->GetName(), eventlist);
167 }
168
169 //__________________________________________________________________________________________________
170 void Analysis::AddLoader(TAMVirtualLoader *l)
171 {
172 // Add loader to the list of loaders.
173
174 fLoaders->Add(l);
175 }
176
177 //__________________________________________________________________________________________________
178 void Analysis::AddPackage(const char* name)
179 {
180 // Add package to the list of uploaded packages.
181
182 MitAssert("AddPackage", name != 0);
183 fPackages->Add(new TObjString(name));
184 }
185
186 //__________________________________________________________________________________________________
187 void Analysis::AddPackages(TList *list)
188 {
189 // Add list of packages to the list of uploaded packages.
190
191 MitAssert("AddPackage", list != 0);
192
193 TIter next(list);
194 while (TObject *obj = next()) {
195 fPackages->Add(new TObjString(obj->GetName()));
196 }
197 }
198
199 //__________________________________________________________________________________________________
200 Bool_t Analysis::Init()
201 {
202 // Setup the TDSet and TChain to be used for the analysis
203 // with or without PROOF. If more than one list of
204 // file names was given, friend trees are supported.
205
206 if (fState == kRun || fState == kInit) {
207 Error("Init", "Init in state %d is not possible! Call Terminate() first.",
208 Int_t(fState));
209 return kFALSE;
210 }
211
212 if (fNFriends <= 0) {
213 Error("Init", "List of friend lists is empty!");
214 return kFALSE;
215 }
216
217 if (!fSuperMod) {
218 Error("Init", "Top-level TAM module is NULL!");
219 return kFALSE;
220 }
221
222 if (fUseProof) { // first init our PROOF session
223 if (!InitProof()) return kFALSE;
224 }
225
226 // we do this here instead in Terminate() so that
227 // we can browse the output even after Terminate()
228 delete fSelector;
229 fSelector = 0;
230
231 fChain = new TChain(fTreeName);
232 fSet = new TDSet("TTree",fTreeName);
233
234 for(Int_t i=0; i<fNFriends; i++){
235
236 TList *l = dynamic_cast<TList*>(fList->At(i));
237 if (!l) {
238 Fatal("Init", "List %d not found!", i);
239 return kFALSE;
240 }
241
242 if (i == 0) {
243
244 TIter next(l);
245 while ( TObjString *obj = dynamic_cast<TObjString*>(next()) ) {
246 fChain->Add(obj->GetName());
247 fSet->Add(obj->GetName());
248 }
249
250 } else {
251
252 TChain *chain = new TChain(fTreeName);
253 TDSet *set = new TDSet("TTree",fTreeName);
254
255 TIter next(l);
256 while (TObjString *obj = dynamic_cast<TObjString*>(next())) {
257 chain->Add(obj->GetName());
258 set->Add(obj->GetName());
259 }
260
261 TString alias("TAMTREE_"); // aliases currently not used
262 alias+=i;
263
264 fChain->AddFriend(chain,alias.Data());
265 fSet->AddFriend(set,alias.Data());
266
267 fDeleteList->Add(chain);
268 fDeleteList->Add(set);
269 }
270
271 }
272
273 // if we had our default TAM plugin we would create it here
274 // TreeLoader *bl = new TreeLoader;
275 // fLoaders->Add(bl);
276 // fDeleteList->Add(bl);
277
278 if (fUseProof) {
279
280 fProof->AddInput(fSuperMod);
281 fLoaders->SetName("TAM_LOADERS");
282 fProof->AddInput(fLoaders);
283
284 } else {
285
286 // when not running Proof, we must make a selector
287 fSelector = new TAMSelector;
288 fSelector->AddInput(fSuperMod);
289 MDB(kAnalysis, 2)
290 fSelector->SetVerbosity(1);
291
292 // pass loaders to selector
293 TIter next(fLoaders);
294 while ( TAMVirtualLoader *l = dynamic_cast<TAMVirtualLoader*>(next()) )
295 fSelector->AddLoader(l);
296 }
297
298 fState = kInit;
299 return kTRUE;
300 }
301
302 //__________________________________________________________________________________________________
303 Bool_t Analysis::InitProof()
304 {
305 // Initialize PROOF connection.
306
307 if(fProof && fProof->IsValid())
308 return kTRUE;
309
310 delete fProof;
311
312 if (fMaster.Contains("rcf.bnl.gov")) {
313 for(Int_t i=0;i<5;i++) {
314 Warning("InitProof", "*** DID YOU RUN PROOF_KINIT? %d (5) ***", i);
315 gSystem->Sleep(1000);
316 }
317 }
318
319 MDB(kAnalysis, 1)
320 Info("InitProof", "Starting PROOF on master %s with config %s",
321 fMaster.Data(), fConfig.Data());
322
323 fProof = dynamic_cast<TProof*>(TProof::Open(fMaster, fConfig));
324 if (!fProof) {
325 Error("InitProof", "Could not start PROOF!");
326 return kFALSE;
327 }
328
329 MDB(kAnalysis, 3)
330 gROOT->Print();
331
332 //fProof->AddInput(new TNamed("PROOF_NewPacketizer",""));
333
334 Bool_t ret=kTRUE;
335 if (fPackages) {
336 // tell Proof what additional libraries we will need on each worker computer
337 ret = UploadPackages(fPackages);
338 }
339
340 return ret;
341 }
342
343 //__________________________________________________________________________________________________
344 void Analysis::Run()
345 {
346 // Run the analysis on the created file set.
347
348 if (fState == kPristine || fState == kRun) {
349 Error("Run", "Run in state %d is not possible! Call Init() first.",
350 Int_t(fState));
351 }
352
353 if (fUseProof) {
354
355 MDB(kAnalysis, 1)
356 Info("Run", "Start processing with PROOF...");
357
358 fSet->Process("TAMSelector");
359
360 } else {
361
362 MDB(kAnalysis, 1)
363 Info("Run", "Start processing (no PROOF)...");
364
365 fChain->Process(fSelector);
366 }
367
368 MDB(kAnalysis, 1)
369 Info("Run", "Processing complete!");
370
371 fState = kRun;
372 }
373
374 //__________________________________________________________________________________________________
375 void Analysis::Terminate()
376 {
377 // Terminate current analysis run.
378
379 if (fState == kPristine || fState == kTerminate) {
380 Error("Terminate", "Terminate in state %d is not possible! Call Init() first.",
381 Int_t(fState));
382 return;
383 }
384
385 if (fState == kRun) {
386
387 if (fUseProof) {
388 // the output list from Proof can (in principal) contain other objects
389 // besides the module output hierarchy.
390 TList* outputlist = fProof->GetOutputList();
391 TIter nextOut(outputlist);
392 while (TObject *obj = nextOut()) {
393 if (obj->InheritsFrom(TAMOutput::Class())) {
394 fOutput = dynamic_cast<TList*>(obj);
395 break;
396 }
397 }
398
399 } else {
400 fOutput = fSelector->GetModOutput();
401 }
402
403 if (fOutput && !fAnaOutput.IsNull()) {
404 TDirectory::TContext context(0); // automatically restore gDirectory
405
406 std::auto_ptr<TFile> outf(TFile::Open(fAnaOutput,"recreate","", fCompLevel));
407 if (outf.get() == 0) {
408 Error("Terminate", "Could not open file %s for output!", fAnaOutput.Data());
409 } else {
410 MDB(kAnalysis, 1)
411 Info("Terminate", "Saving output to %s!", fAnaOutput.Data());
412
413 if (fHierachy)
414 fOutput->Write(fOutput->GetName(),TObject::kSingleKey);
415 else
416 fOutput->Write();
417
418 }
419 }
420 }
421
422 delete fChain;
423 delete fSet;
424 fDeleteList->Delete();
425
426 fState = kTerminate;
427 }
428
429 //__________________________________________________________________________________________________
430 Bool_t Analysis::UploadPackages(TList *packages)
431 {
432 // Upload list of par files to the server.
433
434 MitAssert("UploadPackages", packages != 0);
435
436 for(Int_t i=0; i < packages->GetEntries(); i++) {
437
438 TObject* objstr = packages->At(i);
439 if (!objstr){
440 Error("InitProof", "Problem at package number %d!", i);
441 return kFALSE;
442 }
443
444 TString packname = objstr->GetName();
445 Int_t en = 0;
446 if (packname.EndsWith("+")) {
447 en=1;
448 packname.Resize(packname.Length()-1);
449 }
450
451 ifstream ftest(gSystem->ExpandPathName(packname.Data()),ios_base::binary);
452 if (!ftest.good()){
453 Error("InitProof", "Could not open %s for upload!", packname.Data());
454 return kFALSE;
455 }
456
457 if(fProof->UploadPackage(packname)<0) {
458 Error("UploadPackage", "Upload for %s failed!", packname.Data());
459 return kFALSE;
460 }
461
462 if (en == 1) {
463 Int_t pos=packname.Last('/')+1;
464 if (pos) packname.Remove(0,pos);
465 if(fProof->EnablePackage(packname)<0) {
466 Error("UploadPackage", "Enabling for %s failed!", packname.Data());
467 return kFALSE;
468 }
469 }
470 }
471
472 return kTRUE;
473 }