ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/Analysis.cc
Revision: 1.15
Committed: Sun Sep 28 02:36:23 2008 UTC (16 years, 7 months ago) by loizides
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_004
Changes since 1.14: +17 -3 lines
Log Message:
Use HLTFwkMod

File Contents

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