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.17 by loizides, Wed Nov 5 17:23:47 2008 UTC vs.
Revision 1.41 by paus, Sun Oct 3 02:26:30 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 29 | Line 33 | Analysis::Analysis(Bool_t useproof) :
33    fUseProof(useproof),
34    fUseHLT(kTRUE),
35    fHierarchy(kTRUE),
36 <  fDoProxy(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),
45 <  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 69 | 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 = kTRUE;
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 86 | Line 117 | Bool_t Analysis::AddFile(const char *pna
117      return kFALSE;
118    }
119  
120 <  TString pnamestr(pname);
120 >  //================================================================================================
121 >  // Please do not ask me why I need to do this but I have to?! (CP - Oct02, 2010)
122 >  char   fileName[4096];
123 >  strcpy(fileName,pname);
124 >  //================================================================================================
125 >
126 >  TString pnamestr(fileName);
127 >
128 >  if (pnamestr.IsNull())
129 >    return kFALSE;
130 >
131    TString tok("|");
132    TObjArray *arr = pnamestr.Tokenize(tok);
133 <  TString msg;
133 >  if (!arr)
134 >    return kFALSE;
135  
136 <  for(Int_t i=0; i<arr->GetEntries(); i++){
137 <      
136 >  TString msg;
137 >  for (Int_t i=0; i<arr->GetEntries(); ++i) {
138 >  
139      TObjString *dummy = dynamic_cast<TObjString*>(arr->At(i));
140 <    if(!dummy) continue;
140 >    if (!dummy)
141 >      continue;
142  
143      AddFile(dummy->GetName(),i);      
144 <    if(i==0) msg=dummy->GetName();
145 <    else {
146 <      Info("AddFile", "Add file %s as friend to %s",
147 <           dummy->GetName(), msg.Data());
104 <    }
144 >    if (i==0)
145 >      msg=dummy->GetName();
146 >    else
147 >      Info("AddFile", "Add file %s as friend to %s", dummy->GetName(), msg.Data());
148    }
149    delete arr;
150  
# Line 131 | Line 174 | void Analysis::AddFile(const char *pname
174      l = new TList;
175      l->SetOwner();
176      fList->Add(l);
177 <    fNFriends++;
177 >    ++fNFriends;
178  
179    } else if (eventlist < 0 || eventlist > fNFriends) {
180      Error("AddFile", "Specified list %d not in [0,%d]", eventlist, fNFriends);
181      return;
182    }
183    
184 <  if(!IsValidName(pname)) return;
184 >  if (! IsValidName(pname))
185 >    return;
186  
187 <  l->Add(new TObjString(pname));
187 >  TString theFile(pname);
188 >  if ( theFile.BeginsWith("castor:") )
189 >    theFile.ReplaceAll("castor:","root://castorcms/");
190 >  if ( theFile.BeginsWith("/castor/"))
191 >    theFile.Prepend("root://castorcms/");
192 >  printf("Adding file with path: %s\n",theFile.Data());
193 >  l->Add(new TObjString(theFile.Data()));
194  
195    MDB(kAnalysis, 2)
196      Info("AddFile", "Added %s to list of files.", pname);
# Line 168 | Line 218 | Bool_t Analysis::AddFiles(const char *pn
218    ifstream in;
219    in.open(pname);
220    if (!in) {
221 <    Error("AddFiles", "Can not open file with name %s", pname);
221 >    Error("AddFiles", "Cannot open file with name %s", pname);
222      return kFALSE;
223    }
224  
# Line 176 | Line 226 | Bool_t Analysis::AddFiles(const char *pn
226    while (in) {
227      TString line;
228      line.ReadLine(in);
179    cout << line << endl;
229      if (!line.EndsWith(".root"))
230        continue;
182    cout << line << endl;
231  
232      if (!AddFile(line)) {
233        Error("AddFiles", "Error adding file with name %s", line.Data());
# Line 187 | Line 235 | Bool_t Analysis::AddFiles(const char *pn
235      }
236  
237      ++fc;
238 <    if(nmax>0 && fc>=nmax) {
238 >    if (nmax>0 && fc>=nmax) {
239        Info("AddFiles", "Maximal number (%d) of files added", nmax);
240        break;
241      }
# Line 197 | Line 245 | Bool_t Analysis::AddFiles(const char *pn
245   }
246  
247   //--------------------------------------------------------------------------------------------------
200 Bool_t Analysis::AddDataset(const Dataset *dataset)
201 {
202  // Add a full dataset to the analysis.
203
204  Bool_t status = true;
205
206  for (UInt_t i=0; i<dataset->NFiles(); i++)
207    status = (status && AddFile(dataset->FileUrl(i)));
208
209  return status;
210 }
211
212 //--------------------------------------------------------------------------------------------------
248   void Analysis::AddList(TList *list, Int_t eventlist)
249   {
250    // Add file name to the event list specified by eventlist. The lists are used to hold filenames of
# Line 254 | Line 289 | void Analysis::AddPackages(TList *list)
289   }
290  
291   //--------------------------------------------------------------------------------------------------
292 + void Analysis::AddSuperModule(TAModule *mod)
293 + {
294 +  // Add a top-level module to list of top-level (super) modules.
295 +
296 +  fSuperMods->Add(mod);      
297 + }
298 +
299 + //--------------------------------------------------------------------------------------------------
300 + void Analysis::FileInputFromEnv()
301 + {
302 +  // Attempt to get list of filesets/files from environment.
303 +
304 +  TString catalog (gSystem->Getenv("MIT_CATALOG"));
305 +  TString book    (gSystem->Getenv("MIT_BOOK"));
306 +  TString dataset (gSystem->Getenv("MIT_DATASET"));
307 +  TString filesets(gSystem->Getenv("MIT_FILESETS"));
308 +  TString files   (gSystem->Getenv("MIT_FILES"));
309 +
310 +  if ((catalog.IsNull() || book.IsNull() || dataset.IsNull()) && files.IsNull()) {
311 +      Warning("FileInputFromEnv", "Called to get file info from environment, but did not get"
312 +              " consistent set of variables:\n\tMIT_CATALOG=%s\n\tMIT_BOOK=%s\n\t"
313 +              "MIT_DATASET=%s\n\tMIT_FILESETS=%s\n\tMIT_FILES=%s\n",
314 +              catalog.Data(), book.Data(), dataset.Data(), filesets.Data(), files.Data());
315 +      return;
316 +  }
317 +
318 +  if (!files.IsNull()) { // add local files
319 +    Info("FileInputFromEnv", "Got from environment:\n"
320 +         "\n\tMIT_FILES=%s\n", files.Data());
321 +    TString tok(";");
322 +    TObjArray *arr = files.Tokenize(tok);
323 +    if (arr) {
324 +      for (Int_t i=0; i<arr->GetEntries(); ++i) {
325 +        TObjString *dummy = dynamic_cast<TObjString*>(arr->At(i));
326 +        if (!dummy) continue;
327 +        AddFile(dummy->GetName(),0);      
328 +      }
329 +      delete arr;
330 +    }
331 +    return;
332 +  }
333 +
334 +  Info("FileInputFromEnv", "Got from environment:\n"
335 +       "\tMIT_CATALOG=%s\n\tMIT_BOOK=%s\n\tMIT_DATASET=%s\n\tMIT_FILESETS=%s\n",
336 +       catalog.Data(), book.Data(), dataset.Data(), filesets.Data());
337 +
338 +  Catalog cat(catalog);
339 +  if (filesets.IsNull()) {
340 +    Dataset *d = cat.FindDataset(book, dataset);
341 +    if (d) {
342 +      AddDataset(d);
343 +      delete d;
344 +    }
345 +  } else {
346 +    TString tok(";");
347 +    TObjArray *arr = filesets.Tokenize(tok);
348 +    if (arr) {
349 +      for (Int_t i=0; i<arr->GetEntries(); ++i) {
350 +        TObjString *fileset = dynamic_cast<TObjString*>(arr->At(i));
351 +        if (!fileset) continue;
352 +        Dataset *d = cat.FindDataset(book, dataset, fileset->String());
353 +        if (!d)
354 +          continue;
355 +        AddDataset(d);
356 +        delete d;
357 +      }
358 +      delete arr;
359 +    }
360 +  }
361 + }
362 +
363 + //--------------------------------------------------------------------------------------------------
364   Bool_t Analysis::Init()
365   {
366    // Setup the TDSet and TChain to be used for the analysis with or without PROOF. If more than one
# Line 265 | Line 372 | Bool_t Analysis::Init()
372      return kFALSE;
373    }
374  
375 +  // check if we should attempt to get filesets/filenames from environment
376 +  if (fNFriends == 0)
377 +    FileInputFromEnv();
378 +
379    if (fNFriends <= 0) {
380      Error("Init", "List of friend lists is empty!");
381      return kFALSE;
382    }
383  
384 <  if (!fSuperMod) {
384 >  if (!fSuperMods->First()) {
385      Error("Init", "Top-level TAM module is NULL!");
386      return kFALSE;
387    }
388  
389    if (fUseProof) { // first init our PROOF session
390 <    if (!InitProof()) return kFALSE;
390 >    if (!InitProof())
391 >      return kFALSE;
392    }
393  
394    // we do this here instead in Terminate() so that
# Line 287 | Line 399 | Bool_t Analysis::Init()
399    fChain = new TChain(fTreeName);
400    fSet   = new TDSet("TTree",fTreeName);
401  
402 <  for(Int_t i=0; i<fNFriends; i++){
291 <
402 >  for (Int_t i=0; i<fNFriends; ++i) {
403      TList *l = dynamic_cast<TList*>(fList->At(i));
404      if (!l) {
405        Fatal("Init", "List %d not found!", i);
# Line 297 | Line 408 | Bool_t Analysis::Init()
408  
409      if (i == 0) {
410        TIter next(l);                          
411 <      while ( TObjString *obj = dynamic_cast<TObjString*>(next()) ) {
411 >      while (TObjString *obj = dynamic_cast<TObjString*>(next())) {
412          fChain->Add(obj->GetName());
413          fSet->Add(obj->GetName());
414 +        if (fCacheSize<0 && obj->GetString().Contains("/castor/"))
415 +          fCacheSize = 64*1024*1024;
416        }
304
417      } else {
306
418        TChain *chain = new TChain(fTreeName);
419        TDSet *set    = new TDSet("TTree",fTreeName);
420  
# Line 311 | Line 422 | Bool_t Analysis::Init()
422        while (TObjString *obj = dynamic_cast<TObjString*>(next())) {
423          chain->Add(obj->GetName());
424          set->Add(obj->GetName());
425 +        if (fCacheSize<0 && obj->GetString().Contains("/castor/"))
426 +          fCacheSize = 64*1024*1024;
427        }
428  
429        TString alias("TAMTREE_"); // aliases currently not used
# Line 322 | Line 435 | Bool_t Analysis::Init()
435        fDeleteList->Add(chain);
436        fDeleteList->Add(set);
437      }
325
438    }
439  
440 +  if (fParallel)
441 +    TTreeCacheUnzip::SetParallelUnzip(TTreeCacheUnzip::kEnable);
442 +
443 +  if (fCacheSize>=0)
444 +    fChain->SetCacheSize(fCacheSize);
445 +
446    // create our customized loader plugin for TAM
447    TreeLoader *bl = new TreeLoader;
448    fLoaders->Add(bl);
449    fDeleteList->Add(bl);
450  
451 +  // create our ana framework module
452 +  AnaFwkMod *anamod = new AnaFwkMod;
453 +  anamod->SetSkipNEvents(fSkipNEvents);
454 +  anamod->SetPrintScale(fPrintScale);
455 +  fDeleteList->Add(anamod);
456 +
457    // create our HLT framework module
458    HLTFwkMod *hltmod = 0;
459    if (fUseHLT) {
460      hltmod = new HLTFwkMod;
461 +    hltmod->SetHLTObjsName(GetHLTObjsName());
462 +    hltmod->SetHLTTreeName(GetHLTTreeName());
463      fDeleteList->Add(hltmod);
464    }
465  
466    if (fUseProof) {
467  
468 +    fProof->AddInput(anamod);
469      if (hltmod)
470        fProof->AddInput(hltmod);
471  
472 <    fProof->AddInput(fSuperMod);
472 >    TIter iter(fSuperMods->MakeIterator());
473 >    while (1) {
474 >      TAModule *next = dynamic_cast<TAModule*>(iter.Next());
475 >      if (!next) break;
476 >      fProof->AddInput(next);
477 >    }
478 >
479      fLoaders->SetName("TAM_LOADERS");
480      fProof->AddInput(fLoaders);      
481  
482    } else {
483  
484      // when not running Proof, we must make a selector
485 <    fSelector = new Selector;
486 <    fSelector->SetDoProxy(fDoProxy);
485 >    Selector *sel = new Selector;
486 >    sel->SetDoProxy(fDoProxy);
487 >    sel->SetDoObjTabClean(fDoObjTabClean);
488 >    sel->SetDoRunInfo(kTRUE);
489 >    sel->SetAllEvtHdrBrn(GetAllEvtHdrBrn());
490 >    sel->SetAllEvtTreeName(GetAllEvtTreeName());
491 >    sel->SetEvtHdrName(GetEvtHdrName());
492 >    sel->SetLAHdrName(GetLAHdrName());
493 >    sel->SetLATreeName(GetLATreeName());
494 >    sel->SetRunInfoName(GetRunInfoName());
495 >    sel->SetRunTreeName(GetRunTreeName());
496 >    sel->AddInput(anamod);
497 >    fSelector = sel;
498 >
499      if (hltmod)
500        fSelector->AddInput(hltmod);
501 <    fSelector->AddInput(fSuperMod);
501 >
502 >    TIter iter(fSuperMods->MakeIterator());
503 >    while (1) {
504 >      TAModule *next = dynamic_cast<TAModule*>(iter.Next());
505 >      if (!next) break;
506 >      fSelector->AddInput(next);
507 >    }
508 >
509      MDB(kAnalysis, 2)
510        fSelector->SetVerbosity(1);
511  
512      // pass loaders to selector
513      TIter next(fLoaders);                          
514 <    while ( TAMVirtualLoader *l = dynamic_cast<TAMVirtualLoader*>(next()) )
514 >    while (TAMVirtualLoader *l = dynamic_cast<TAMVirtualLoader*>(next()))
515        fSelector->AddLoader(l);
516    }
517  
# Line 372 | Line 524 | Bool_t Analysis::InitProof()
524   {
525    // Initialize PROOF connection.
526  
527 <  if(fProof && fProof->IsValid())
527 >  if (fProof && fProof->IsValid())
528      return kTRUE;
529  
530    delete fProof;
531  
532    if (fMaster.Contains("rcf.bnl.gov")) {
533 <    for(Int_t i=0;i<5;i++) {
533 >    for (Int_t i=0; i<5; ++i) {
534        Warning("InitProof", "*** DID YOU RUN PROOF_KINIT? %d (5) ***", i);
535        gSystem->Sleep(1000);
536      }
# Line 458 | Line 610 | Bool_t Analysis::Run(Bool_t browse)
610   }
611  
612   //--------------------------------------------------------------------------------------------------
613 + void Analysis::SetSuperModule(TAModule *mod)
614 + {
615 +  // Set the first top-level module in the list of top-level (super) modules.
616 +
617 +  fSuperMods->AddFirst(mod);      
618 + }
619 +
620 + //--------------------------------------------------------------------------------------------------
621   void Analysis::Terminate()
622   {
623    // Terminate current analysis run.
# Line 486 | Line 646 | void Analysis::Terminate()
646        fOutput = fSelector->GetModOutput();
647      }
648  
649 +    
650 +
651      if (fOutput && !fAnaOutput.IsNull()) {
652        TDirectory::TContext context(0); // automatically restore gDirectory
653  
# Line 497 | Line 659 | void Analysis::Terminate()
659            Info("Terminate", "Saving output to %s!", fAnaOutput.Data());
660  
661          if (fHierarchy)
662 <          fOutput->Write(fOutput->GetName(),TObject::kSingleKey);      
662 >          fOutput->Write(0,-99);
663          else
664            fOutput->Write();
503              
665        }
666      }
667      // set state to terminate
# Line 519 | Line 680 | Bool_t Analysis::UploadPackages(TList *p
680  
681    MitAssert("UploadPackages", packages != 0);
682  
683 <  for (Int_t i=0; i<packages->GetEntries(); i++) {
683 >  for (Int_t i=0; i<packages->GetEntries(); ++i) {
684  
685      TObject* objstr = packages->At(i);
686      if (!objstr) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines