ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/Analysis.cc
Revision: 1.18
Committed: Wed Nov 19 15:30:26 2008 UTC (16 years, 5 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.17: +10 -1 lines
Log Message:
Added general ana fwk module. Currently only used to give an overall event counter. Could be extended for other purposes.

File Contents

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