ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/Analysis.cc
Revision: 1.9
Committed: Mon Jun 23 10:54:20 2008 UTC (16 years, 10 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.8: +6 -5 lines
Log Message:
Use Selector.

File Contents

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