ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/Analysis.cc
Revision: 1.10
Committed: Tue Jun 24 14:07:21 2008 UTC (16 years, 10 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.9: +1 -2 lines
Log Message:
Cosmetics.

File Contents

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