00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00100 coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") opening PROOF session" << endl ;
00101 void* p = (void*) gROOT->ProcessLineFast(Form("TProof::Open(\"%s\")",proofHost)) ;
00102
00103
00104 if (!showGui) {
00105 gROOT->ProcessLineFast(Form("((TProof*)%p)->SetProgressDialog(0) ;",p)) ;
00106 }
00107
00108
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
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
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
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
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
00150 gSystem->Exec(Form("cat study_data_%s.root | uuencode -m study_data_%s.root >> study_driver_%s.sh",studyName,studyName,studyName)) ;
00151
00152
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
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
00228 if (!TString(name).MaybeWildcard()) {
00229 result.push_back(name) ;
00230 return ;
00231 }
00232
00233
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
00245
00246 dotslashpos = -1;
00247 }
00248 }
00249
00250 TString behind_dot_root;
00251 if (dotslashpos>=0) {
00252
00253 behind_dot_root = basename(dotslashpos+6,basename.Length()-dotslashpos+6);
00254
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);
00262 basename.Remove(0,slashpos+1);
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
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
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 }