RooStudyManager.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooStudyManager.cxx 37534 2010-12-10 22:30:05Z wouter $
00005  * Authors:                                                                  *
00006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
00007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
00008  *                                                                           *
00009  * Copyright (c) 2000-2005, Regents of the University of California          *
00010  *                          and Stanford University. All rights reserved.    *
00011  *                                                                           *
00012  * Redistribution and use in source and binary forms,                        *
00013  * with or without modification, are permitted according to the terms        *
00014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
00015  *****************************************************************************/
00016 
00017 //////////////////////////////////////////////////////////////////////////////
00018 //
00019 // BEGIN_HTML
00020 // RooStudyManager is a utility class to manage studies that consist of
00021 // repeated applications of generate-and-fit operations on a workspace
00022 //
00023 // END_HTML
00024 //
00025 
00026 
00027 
00028 #include "RooFit.h"
00029 #include "Riostream.h"
00030 
00031 #include "RooStudyManager.h"
00032 #include "RooWorkspace.h"
00033 #include "RooAbsStudy.h"
00034 #include "RooDataSet.h"
00035 #include "RooMsgService.h"
00036 #include "RooStudyPackage.h"
00037 #include "TTree.h"
00038 #include "TFile.h"
00039 #include "TRegexp.h"
00040 #include "TKey.h"
00041 #include <string>
00042 #include "TROOT.h"
00043 #include "TSystem.h"
00044 
00045 using namespace std ;
00046 
00047 ClassImp(RooStudyManager)
00048   ;
00049 
00050 
00051 
00052 //_____________________________________________________________________________
00053 RooStudyManager::RooStudyManager(RooWorkspace& w)
00054 {  
00055   _pkg = new RooStudyPackage(w) ;
00056 }
00057 
00058 
00059 
00060 //_____________________________________________________________________________
00061 RooStudyManager::RooStudyManager(RooWorkspace& w, RooAbsStudy& study)
00062 {  
00063   _pkg = new RooStudyPackage(w) ;
00064   _pkg->addStudy(study) ;
00065 }
00066 
00067 
00068 //_____________________________________________________________________________
00069 RooStudyManager::RooStudyManager(const char* studyPackFileName) 
00070 {
00071   string pwd = gDirectory->GetName() ;
00072   TFile *f = new TFile(studyPackFileName) ;
00073   _pkg = dynamic_cast<RooStudyPackage*>(f->Get("studypack")) ;
00074   gDirectory->cd(Form("%s:",pwd.c_str())) ;
00075 }
00076 
00077 
00078 
00079 //_____________________________________________________________________________
00080 void RooStudyManager::addStudy(RooAbsStudy& study) 
00081 {
00082   _pkg->addStudy(study) ;
00083 }
00084 
00085 
00086 
00087 
00088 //_____________________________________________________________________________
00089 void RooStudyManager::run(Int_t nExperiments) 
00090 {
00091   _pkg->driver(nExperiments) ;
00092 }
00093 
00094 
00095 
00096 //_____________________________________________________________________________
00097 void RooStudyManager::runProof(Int_t nExperiments, const char* proofHost, Bool_t showGui) 
00098 {
00099   // Open PROOF-Lite session
00100   coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") opening PROOF session" << endl ;
00101   void* p = (void*) gROOT->ProcessLineFast(Form("TProof::Open(\"%s\")",proofHost)) ;
00102 
00103   // Suppress GUI if so requested
00104   if (!showGui) {
00105     gROOT->ProcessLineFast(Form("((TProof*)%p)->SetProgressDialog(0) ;",p)) ;
00106   }
00107 
00108   // Propagate workspace to proof nodes
00109   coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") sending work package to PROOF servers" << endl ;
00110   gROOT->ProcessLineFast(Form("((TProof*)%p)->AddInput((TObject*)%p) ;",p,(void*)_pkg) ) ;
00111 
00112   // Run selector in parallel
00113   coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") starting PROOF processing of " << nExperiments << " experiments" << endl ;
00114                          
00115   gROOT->ProcessLineFast(Form("((TProof*)%p)->Process(\"RooProofDriverSelector\",%d) ;",p,nExperiments)) ;
00116 
00117   // Aggregate results data
00118   coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") aggregating results data" << endl ;
00119   TList* olist = (TList*) gROOT->ProcessLineFast(Form("((TProof*)%p)->GetOutputList()",p)) ;
00120   aggregateData(olist) ;
00121 
00122 
00123   gROOT->ProcessLineFast(Form("((TProof*)%p)->Close(\"s\") ;",p)) ;
00124   // close proof session
00125   if (!showGui) {
00126     gROOT->ProcessLineFast(Form("delete ((TProof*)%p) ;",p)) ;
00127   }
00128 }
00129 
00130 
00131 
00132 
00133 //_____________________________________________________________________________
00134 void RooStudyManager::prepareBatchInput(const char* studyName, Int_t nExpPerJob, Bool_t unifiedInput=kFALSE) 
00135 {
00136   TFile f(Form("study_data_%s.root",studyName),"RECREATE") ;
00137   _pkg->Write("studypack") ;
00138   f.Close() ;
00139 
00140   if (unifiedInput) {
00141 
00142     // Write header of driver script
00143     ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
00144     bdr << "#!/bin/sh" << endl 
00145         << Form("if [ ! -f study_data_%s.root ] ; then",studyName) << endl 
00146         << "uudecode <<EOR" << endl ;
00147     bdr.close() ;
00148 
00149     // Write uuencoded ROOT file (base64) in driver script
00150     gSystem->Exec(Form("cat study_data_%s.root | uuencode -m study_data_%s.root >> study_driver_%s.sh",studyName,studyName,studyName)) ;
00151 
00152     // Write remainder of deriver script
00153     ofstream bdr2 (Form("study_driver_%s.sh",studyName),ios::app) ;
00154     bdr2 << "EOR" << endl 
00155          << "fi" << endl 
00156          << "root -l -b <<EOR" << endl 
00157          << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << endl 
00158          << ".q" << endl 
00159          << "EOR" << endl ;  
00160     // Remove binary input file
00161     gSystem->Unlink(Form("study_data_%s.root",studyName)) ;    
00162 
00163     coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << endl
00164                         << "     input data files is embedded in driver script" << endl ;
00165 
00166   } else {
00167 
00168     ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
00169     bdr << "#!/bin/sh" << endl 
00170         << "root -l -b <<EOR" << endl 
00171         << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << endl 
00172         << ".q" << endl 
00173         << "EOR" << endl ;  
00174 
00175     coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << endl
00176                         << "     input data file is " << Form("study_data_%s.root",studyName) << endl ;
00177 
00178   }
00179 }
00180 
00181 
00182 
00183 
00184 //_____________________________________________________________________________
00185 void RooStudyManager::processBatchOutput(const char* filePat) 
00186 {
00187   list<string> flist ;
00188   expandWildCardSpec(filePat,flist) ;
00189 
00190   TList olist ;
00191 
00192   for (list<string>::iterator iter = flist.begin() ; iter!=flist.end() ; ++iter) {
00193     coutP(DataHandling) << "RooStudyManager::processBatchOutput() now reading file " << *iter << endl ;
00194     TFile f(iter->c_str()) ;
00195 
00196     TList* list = f.GetListOfKeys() ;
00197     TIterator* kiter = list->MakeIterator();
00198     
00199     TObject* obj ;
00200     TKey* key ;
00201     while((key=(TKey*)kiter->Next())) {      
00202       obj = f.Get(key->GetName()) ;
00203       TObject* clone = obj->Clone(obj->GetName()) ;
00204       olist.Add(clone) ;
00205     }
00206     delete kiter ;
00207   }
00208   aggregateData(&olist) ;
00209   olist.Delete() ;
00210 }
00211 
00212 
00213 //_____________________________________________________________________________
00214 void RooStudyManager::aggregateData(TList* olist) 
00215 {
00216   for (list<RooAbsStudy*>::iterator iter=_pkg->studies().begin() ; iter!=_pkg->studies().end() ; iter++) {
00217     (*iter)->aggregateSummaryOutput(olist) ;
00218   }  
00219 }
00220 
00221 
00222 
00223 
00224 //_____________________________________________________________________________
00225 void RooStudyManager::expandWildCardSpec(const char* name, list<string>& result)
00226 {
00227   // case with one single file
00228   if (!TString(name).MaybeWildcard()) {
00229     result.push_back(name) ;
00230     return ;
00231   }
00232 
00233    // wildcarding used in name
00234    TString basename(name);
00235 
00236    Int_t dotslashpos = -1;
00237    {
00238       Int_t next_dot = basename.Index(".root");
00239       while(next_dot>=0) {
00240          dotslashpos = next_dot;
00241          next_dot = basename.Index(".root",dotslashpos+1);
00242       }
00243       if (basename[dotslashpos+5]!='/') {
00244          // We found the 'last' .root in the name and it is not followed by
00245          // a '/', so the tree name is _not_ specified in the name.
00246          dotslashpos = -1;
00247       }
00248    }
00249    //Int_t dotslashpos = basename.Index(".root/");
00250    TString behind_dot_root;
00251    if (dotslashpos>=0) {
00252       // Copy the tree name specification
00253       behind_dot_root = basename(dotslashpos+6,basename.Length()-dotslashpos+6);
00254       // and remove it from basename
00255       basename.Remove(dotslashpos+5);
00256    }
00257 
00258    Int_t slashpos = basename.Last('/');
00259    TString directory;
00260    if (slashpos>=0) {
00261       directory = basename(0,slashpos); // Copy the directory name
00262       basename.Remove(0,slashpos+1);      // and remove it from basename
00263    } else {
00264       directory = gSystem->UnixPathName(gSystem->WorkingDirectory());
00265    }
00266 
00267    const char *file;
00268    void *dir = gSystem->OpenDirectory(gSystem->ExpandPathName(directory.Data()));
00269 
00270    if (dir) {
00271       //create a TList to store the file names (not yet sorted)
00272       TList l;
00273       TRegexp re(basename,kTRUE);
00274       while ((file = gSystem->GetDirEntry(dir))) {
00275          if (!strcmp(file,".") || !strcmp(file,"..")) continue;
00276          TString s = file;
00277          if ( (basename!=file) && s.Index(re) == kNPOS) continue;
00278          l.Add(new TObjString(file));
00279       }
00280       gSystem->FreeDirectory(dir);
00281       //sort the files in alphanumeric order
00282       l.Sort();
00283       TIter next(&l);
00284       TObjString *obj;
00285       while ((obj = (TObjString*)next())) {
00286          file = obj->GetName();
00287          if (behind_dot_root.Length() != 0)
00288            result.push_back(Form("%s/%s/%s",directory.Data(),file,behind_dot_root.Data())) ;
00289          else
00290            result.push_back(Form("%s/%s",directory.Data(),file)) ;
00291       }
00292       l.Delete();
00293    }
00294 }

Generated on Tue Jul 5 15:07:34 2011 for ROOT_528-00b_version by  doxygen 1.5.1