ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitAna/TreeMod/src/Analysis.cc
(Generate patch)

Comparing UserCode/MitAna/TreeMod/src/Analysis.cc (file contents):
Revision 1.14 by loizides, Tue Jul 8 14:42:09 2008 UTC vs.
Revision 1.38 by bendavid, Sun Jan 24 21:00:19 2010 UTC

# Line 1 | Line 1
1   // $Id$
2  
3   #include "MitAna/TreeMod/interface/Analysis.h"
4 + #include <memory>
5   #include <Riostream.h>
6   #include <TFile.h>
7   #include <TList.h>
# Line 11 | Line 12
12   #include <TProof.h>
13   #include <TROOT.h>
14   #include <TBrowser.h>
15 + #include <TTreeCacheUnzip.h>
16   #include "MitAna/DataUtil/interface/Debug.h"
17   #include "MitAna/DataTree/interface/Names.h"
18   #include "MitAna/TAM/interface/TAMVirtualLoader.h"
19   #include "MitAna/TAM/interface/TAModule.h"
20   #include "MitAna/TreeMod/interface/Selector.h"
21   #include "MitAna/TreeMod/interface/TreeLoader.h"
22 + #include "MitAna/TreeMod/interface/AnaFwkMod.h"
23 + #include "MitAna/TreeMod/interface/HLTFwkMod.h"
24 + #include "MitAna/Catalog/interface/Catalog.h"
25   #include "MitAna/Catalog/interface/Dataset.h"
26  
27   ClassImp(mithep::Analysis)
# Line 26 | Line 31 | using namespace mithep;
31   //--------------------------------------------------------------------------------------------------
32   Analysis::Analysis(Bool_t useproof) :
33    fUseProof(useproof),
34 <  fHierachy(kTRUE),
34 >  fUseHLT(kTRUE),
35 >  fHierarchy(kTRUE),
36 >  fDoProxy(kFALSE),
37 >  fDoObjTabClean(kTRUE),
38 >  fParallel(kFALSE),
39    fState(kPristine),
40    fNFriends(0),
41    fList(new TList),
42    fOutput(0),
43    fPackages(new TList),
44    fLoaders(new TList),
45 <  fSuperMod(0),
45 >  fSuperMods(new TList),
46    fSelector(0),
47    fChain(0),
48    fSet(0),
49    fDeleteList(new TList),
50 <  fTreeName(Names::gkEvtTreeName),
42 <  fCompLevel(2),
50 >  fCompLevel(7),
51    fProof(0),
52 <  fDoNEvents(TChain::kBigNumber)
52 >  fDoNEvents(TChain::kBigNumber),
53 >  fSkipNEvents(0),
54 >  fPrintScale(1),
55 >  fCacheSize(-1),
56 >  fTreeName(Names::gkEvtTreeName),
57 >  fEvtHdrName(Names::gkEvtHeaderBrn),
58 >  fRunTreeName(Names::gkRunTreeName),
59 >  fRunInfoName(Names::gkRunInfoBrn),
60 >  fAllEvtHdrBrn(Names::gkAllEvtHeaderBrn),
61 >  fLATreeName(Names::gkLATreeName),
62 >  fLAHdrName(Names::gkLAHeaderBrn),
63 >  fHLTTreeName(Names::gkHltTreeName),
64 >  fAllEvtTreeName(Names::gkAllEvtTreeName),
65 >  fHLTObjsName(Names::gkHltObjBrn)
66   {
67    // Default constructor.
68  
# Line 66 | Line 87 | Analysis::~Analysis()
87    delete fLoaders;
88    delete fDeleteList;
89    delete fSelector;
90 <  fOutput   = 0;   // owned by TAM
91 <  fSuperMod = 0;   // owned by user
90 >  delete fSuperMods;
91 >  fOutput = 0;   // owned by TAM
92  
93    delete fProof;
94   }
95  
96   //--------------------------------------------------------------------------------------------------
97 + Bool_t Analysis::AddDataset(const Dataset *dataset)
98 + {
99 +  // Add a full dataset to the analysis.
100 +
101 +  Bool_t status = true;
102 +
103 +  for (UInt_t i=0; i<dataset->NFiles(); ++i)
104 +    status = (status && AddFile(dataset->FileUrl(i)));
105 +
106 +  return status;
107 + }
108 +
109 + //--------------------------------------------------------------------------------------------------
110   Bool_t Analysis::AddFile(const char *pname)
111   {
112    // Add file with given name to the list of files to be processed. Using the token "|", you can
# Line 84 | Line 118 | Bool_t Analysis::AddFile(const char *pna
118    }
119  
120    TString pnamestr(pname);
121 +  if (pnamestr.IsNull())
122 +    return kFALSE;
123 +
124    TString tok("|");
125    TObjArray *arr = pnamestr.Tokenize(tok);
126 <  TString msg;
126 >  if (!arr)
127 >    return kFALSE;
128  
129 <  for(Int_t i=0; i<arr->GetEntries(); i++){
130 <      
129 >  TString msg;
130 >  for (Int_t i=0; i<arr->GetEntries(); ++i) {
131 >  
132      TObjString *dummy = dynamic_cast<TObjString*>(arr->At(i));
133 <    if(!dummy) continue;
133 >    if (!dummy)
134 >      continue;
135  
136      AddFile(dummy->GetName(),i);      
137 <    if(i==0) msg=dummy->GetName();
138 <    else {
139 <      Info("AddFile", "Add file %s as friend to %s",
140 <           dummy->GetName(), msg.Data());
101 <    }
137 >    if (i==0)
138 >      msg=dummy->GetName();
139 >    else
140 >      Info("AddFile", "Add file %s as friend to %s", dummy->GetName(), msg.Data());
141    }
142    delete arr;
143  
# Line 128 | Line 167 | void Analysis::AddFile(const char *pname
167      l = new TList;
168      l->SetOwner();
169      fList->Add(l);
170 <    fNFriends++;
170 >    ++fNFriends;
171  
172    } else if (eventlist < 0 || eventlist > fNFriends) {
173      Error("AddFile", "Specified list %d not in [0,%d]", eventlist, fNFriends);
174      return;
175    }
176    
177 <  if(!IsValidName(pname)) return;
177 >  if (!IsValidName(pname)) return;
178  
179    l->Add(new TObjString(pname));
180  
# Line 165 | Line 204 | Bool_t Analysis::AddFiles(const char *pn
204    ifstream in;
205    in.open(pname);
206    if (!in) {
207 <    Error("AddFiles", "Can not open file with name %s", pname);
207 >    Error("AddFiles", "Cannot open file with name %s", pname);
208      return kFALSE;
209    }
210  
# Line 173 | Line 212 | Bool_t Analysis::AddFiles(const char *pn
212    while (in) {
213      TString line;
214      line.ReadLine(in);
176    cout << line << endl;
215      if (!line.EndsWith(".root"))
216        continue;
179    cout << line << endl;
217  
218      if (!AddFile(line)) {
219        Error("AddFiles", "Error adding file with name %s", line.Data());
# Line 184 | Line 221 | Bool_t Analysis::AddFiles(const char *pn
221      }
222  
223      ++fc;
224 <    if(nmax>0 && fc>=nmax) {
224 >    if (nmax>0 && fc>=nmax) {
225        Info("AddFiles", "Maximal number (%d) of files added", nmax);
226        break;
227      }
# Line 194 | Line 231 | Bool_t Analysis::AddFiles(const char *pn
231   }
232  
233   //--------------------------------------------------------------------------------------------------
197 Bool_t Analysis::AddDataset(const Dataset *dataset)
198 {
199  // Add a full dataset to the analysis.
200
201  Bool_t status = true;
202
203  for (UInt_t i=0; i<dataset->NFiles(); i++)
204    status = (status && AddFile(dataset->FileUrl(i)));
205
206  return status;
207 }
208
209 //--------------------------------------------------------------------------------------------------
234   void Analysis::AddList(TList *list, Int_t eventlist)
235   {
236    // Add file name to the event list specified by eventlist. The lists are used to hold filenames of
# Line 251 | Line 275 | void Analysis::AddPackages(TList *list)
275   }
276  
277   //--------------------------------------------------------------------------------------------------
278 + void Analysis::AddSuperModule(TAModule *mod)
279 + {
280 +  // Add a top-level module to list of top-level (super) modules.
281 +
282 +  fSuperMods->Add(mod);      
283 + }
284 +
285 + //--------------------------------------------------------------------------------------------------
286 + void Analysis::FileInputFromEnv()
287 + {
288 +  // Attempt to get list of filesets/files from environment.
289 +
290 +  TString catalog(gSystem->Getenv("MIT_CATALOG"));
291 +  TString book(gSystem->Getenv("MIT_BOOK"));
292 +  TString dataset(gSystem->Getenv("MIT_DATASET"));
293 +  TString filesets(gSystem->Getenv("MIT_FILESETS"));
294 +  TString files(gSystem->Getenv("MIT_FILES"));
295 +
296 +  if ((catalog.IsNull() || book.IsNull() || dataset.IsNull()) && files.IsNull()) {
297 +      Warning("FileInputFromEnv", "Called to get file info from environment, but did not get"
298 +              " consistent set of variables:\n\tMIT_CATALOG=%s\n\tMIT_BOOK=%s\n\t"
299 +              "MIT_DATASET=%s\n\tMIT_FILESETS=%s\n\tMIT_FILES=%s\n",
300 +              catalog.Data(), book.Data(), dataset.Data(), filesets.Data(), files.Data());
301 +      return;
302 +  }
303 +
304 +  if (!files.IsNull()) { // add local files
305 +    Info("FileInputFromEnv", "Got from environment:\n"
306 +         "\n\tMIT_FILES=%s\n", files.Data());
307 +    TString tok(";");
308 +    TObjArray *arr = files.Tokenize(tok);
309 +    if (arr) {
310 +      for (Int_t i=0; i<arr->GetEntries(); ++i) {
311 +        TObjString *dummy = dynamic_cast<TObjString*>(arr->At(i));
312 +        if (!dummy) continue;
313 +        AddFile(dummy->GetName(),0);      
314 +      }
315 +      delete arr;
316 +    }
317 +    return;
318 +  }
319 +
320 +  Info("FileInputFromEnv", "Got from environment:\n"
321 +       "\tMIT_CATALOG=%s\n\tMIT_BOOK=%s\n\tMIT_DATASET=%s\n\tMIT_FILESETS=%s\n",
322 +       catalog.Data(), book.Data(), dataset.Data(), filesets.Data());
323 +
324 +  Catalog cat(catalog);
325 +  if (filesets.IsNull()) {
326 +    Dataset *d = cat.FindDataset(book, dataset);
327 +    if (d) {
328 +      AddDataset(d);
329 +      delete d;
330 +    }
331 +  } else {
332 +    TString tok(";");
333 +    TObjArray *arr = filesets.Tokenize(tok);
334 +    if (arr) {
335 +      for (Int_t i=0; i<arr->GetEntries(); ++i) {
336 +        TObjString *fileset = dynamic_cast<TObjString*>(arr->At(i));
337 +        if (!fileset) continue;
338 +        Dataset *d = cat.FindDataset(book, dataset, fileset->String());
339 +        if (!d)
340 +          continue;
341 +        AddDataset(d);
342 +        delete d;
343 +      }
344 +      delete arr;
345 +    }
346 +  }
347 + }
348 +
349 + //--------------------------------------------------------------------------------------------------
350   Bool_t Analysis::Init()
351   {
352    // Setup the TDSet and TChain to be used for the analysis with or without PROOF. If more than one
# Line 262 | Line 358 | Bool_t Analysis::Init()
358      return kFALSE;
359    }
360  
361 +  // check if we should attempt to get filesets/filenames from environment
362 +  if (fNFriends == 0)
363 +    FileInputFromEnv();
364 +
365    if (fNFriends <= 0) {
366      Error("Init", "List of friend lists is empty!");
367      return kFALSE;
368    }
369  
370 <  if (!fSuperMod) {
370 >  if (!fSuperMods->First()) {
371      Error("Init", "Top-level TAM module is NULL!");
372      return kFALSE;
373    }
374  
375    if (fUseProof) { // first init our PROOF session
376 <    if (!InitProof()) return kFALSE;
376 >    if (!InitProof())
377 >      return kFALSE;
378    }
379  
380    // we do this here instead in Terminate() so that
# Line 284 | Line 385 | Bool_t Analysis::Init()
385    fChain = new TChain(fTreeName);
386    fSet   = new TDSet("TTree",fTreeName);
387  
388 <  for(Int_t i=0; i<fNFriends; i++){
288 <
388 >  for (Int_t i=0; i<fNFriends; ++i) {
389      TList *l = dynamic_cast<TList*>(fList->At(i));
390      if (!l) {
391        Fatal("Init", "List %d not found!", i);
# Line 294 | Line 394 | Bool_t Analysis::Init()
394  
395      if (i == 0) {
396        TIter next(l);                          
397 <      while ( TObjString *obj = dynamic_cast<TObjString*>(next()) ) {
397 >      while (TObjString *obj = dynamic_cast<TObjString*>(next())) {
398          fChain->Add(obj->GetName());
399          fSet->Add(obj->GetName());
400 +        if (fCacheSize<0 && obj->GetString().Contains("/castor/"))
401 +          fCacheSize = 64*1024*1024;
402        }
301
403      } else {
303
404        TChain *chain = new TChain(fTreeName);
405        TDSet *set    = new TDSet("TTree",fTreeName);
406  
# Line 308 | Line 408 | Bool_t Analysis::Init()
408        while (TObjString *obj = dynamic_cast<TObjString*>(next())) {
409          chain->Add(obj->GetName());
410          set->Add(obj->GetName());
411 +        if (fCacheSize<0 && obj->GetString().Contains("/castor/"))
412 +          fCacheSize = 64*1024*1024;
413        }
414  
415        TString alias("TAMTREE_"); // aliases currently not used
# Line 319 | Line 421 | Bool_t Analysis::Init()
421        fDeleteList->Add(chain);
422        fDeleteList->Add(set);
423      }
322
424    }
425  
426 +  if (fParallel)
427 +    TTreeCacheUnzip::SetParallelUnzip(TTreeCacheUnzip::kEnable);
428 +
429 +  if (fCacheSize>=0)
430 +    fChain->SetCacheSize(fCacheSize);
431 +
432    // create our customized loader plugin for TAM
433    TreeLoader *bl = new TreeLoader;
434    fLoaders->Add(bl);
435    fDeleteList->Add(bl);
436  
437 +  // create our ana framework module
438 +  AnaFwkMod *anamod = new AnaFwkMod;
439 +  anamod->SetSkipNEvents(fSkipNEvents);
440 +  anamod->SetPrintScale(fPrintScale);
441 +  fDeleteList->Add(anamod);
442 +
443 +  // create our HLT framework module
444 +  HLTFwkMod *hltmod = 0;
445 +  if (fUseHLT) {
446 +    hltmod = new HLTFwkMod;
447 +    hltmod->SetHLTObjsName(GetHLTObjsName());
448 +    hltmod->SetHLTTreeName(GetHLTTreeName());
449 +    fDeleteList->Add(hltmod);
450 +  }
451 +
452    if (fUseProof) {
453  
454 <    fProof->AddInput(fSuperMod);
454 >    fProof->AddInput(anamod);
455 >    if (hltmod)
456 >      fProof->AddInput(hltmod);
457 >
458 >    TIter iter(fSuperMods->MakeIterator());
459 >    while (1) {
460 >      TAModule *next = dynamic_cast<TAModule*>(iter.Next());
461 >      if (!next) break;
462 >      fProof->AddInput(next);
463 >    }
464 >
465      fLoaders->SetName("TAM_LOADERS");
466      fProof->AddInput(fLoaders);      
467  
468    } else {
469  
470      // when not running Proof, we must make a selector
471 <    fSelector = new Selector;
472 <    fSelector->AddInput(fSuperMod);
471 >    Selector *sel = new Selector;
472 >    sel->SetDoProxy(fDoProxy);
473 >    sel->SetDoObjTabClean(fDoObjTabClean);
474 >    sel->SetDoRunInfo(kTRUE);
475 >    sel->SetAllEvtHdrBrn(GetAllEvtHdrBrn());
476 >    sel->SetAllEvtTreeName(GetAllEvtTreeName());
477 >    sel->SetEvtHdrName(GetEvtHdrName());
478 >    sel->SetLAHdrName(GetLAHdrName());
479 >    sel->SetLATreeName(GetLATreeName());
480 >    sel->SetRunInfoName(GetRunInfoName());
481 >    sel->SetRunTreeName(GetRunTreeName());
482 >    sel->AddInput(anamod);
483 >    fSelector = sel;
484 >
485 >    if (hltmod)
486 >      fSelector->AddInput(hltmod);
487 >
488 >    TIter iter(fSuperMods->MakeIterator());
489 >    while (1) {
490 >      TAModule *next = dynamic_cast<TAModule*>(iter.Next());
491 >      if (!next) break;
492 >      fSelector->AddInput(next);
493 >    }
494 >
495      MDB(kAnalysis, 2)
496        fSelector->SetVerbosity(1);
497  
498      // pass loaders to selector
499      TIter next(fLoaders);                          
500 <    while ( TAMVirtualLoader *l = dynamic_cast<TAMVirtualLoader*>(next()) )
500 >    while (TAMVirtualLoader *l = dynamic_cast<TAMVirtualLoader*>(next()))
501        fSelector->AddLoader(l);
502    }
503  
# Line 356 | Line 510 | Bool_t Analysis::InitProof()
510   {
511    // Initialize PROOF connection.
512  
513 <  if(fProof && fProof->IsValid())
513 >  if (fProof && fProof->IsValid())
514      return kTRUE;
515  
516    delete fProof;
517  
518    if (fMaster.Contains("rcf.bnl.gov")) {
519 <    for(Int_t i=0;i<5;i++) {
519 >    for (Int_t i=0; i<5; ++i) {
520        Warning("InitProof", "*** DID YOU RUN PROOF_KINIT? %d (5) ***", i);
521        gSystem->Sleep(1000);
522      }
# Line 442 | Line 596 | Bool_t Analysis::Run(Bool_t browse)
596   }
597  
598   //--------------------------------------------------------------------------------------------------
599 + void Analysis::SetSuperModule(TAModule *mod)
600 + {
601 +  // Set the first top-level module in the list of top-level (super) modules.
602 +
603 +  fSuperMods->AddFirst(mod);      
604 + }
605 +
606 + //--------------------------------------------------------------------------------------------------
607   void Analysis::Terminate()
608   {
609    // Terminate current analysis run.
# Line 470 | Line 632 | void Analysis::Terminate()
632        fOutput = fSelector->GetModOutput();
633      }
634  
635 +    
636 +
637      if (fOutput && !fAnaOutput.IsNull()) {
638        TDirectory::TContext context(0); // automatically restore gDirectory
639  
# Line 480 | Line 644 | void Analysis::Terminate()
644          MDB(kAnalysis, 1)
645            Info("Terminate", "Saving output to %s!", fAnaOutput.Data());
646  
647 <        if (fHierachy)
648 <          fOutput->Write(fOutput->GetName(),TObject::kSingleKey);      
647 >        if (fHierarchy)
648 >          fOutput->Write(0,-99);
649          else
650            fOutput->Write();
487              
651        }
652      }
653 +    // set state to terminate
654 +    fState = kTerminate;
655    }
656  
657    delete fChain;
658    delete fSet;
659    fDeleteList->Delete();
495
496  fState = kTerminate;
660   }
661  
662   //--------------------------------------------------------------------------------------------------
# Line 503 | Line 666 | Bool_t Analysis::UploadPackages(TList *p
666  
667    MitAssert("UploadPackages", packages != 0);
668  
669 <  for (Int_t i=0; i<packages->GetEntries(); i++) {
669 >  for (Int_t i=0; i<packages->GetEntries(); ++i) {
670  
671      TObject* objstr = packages->At(i);
672      if (!objstr) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines