ROOT  6.06/09
Reference Guide
RooStudyManager.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 //////////////////////////////////////////////////////////////////////////////
18 //
19 // BEGIN_HTML
20 // RooStudyManager is a utility class to manage studies that consist of
21 // repeated applications of generate-and-fit operations on a workspace
22 //
23 // END_HTML
24 //
25 
26 
27 
28 #include "RooFit.h"
29 #include "Riostream.h"
30 
31 #include "RooStudyManager.h"
32 #include "RooWorkspace.h"
33 #include "RooAbsStudy.h"
34 #include "RooDataSet.h"
35 #include "RooMsgService.h"
36 #include "RooStudyPackage.h"
37 #include "TTree.h"
38 #include "TFile.h"
39 #include "TRegexp.h"
40 #include "TKey.h"
41 #include <string>
42 #include "TROOT.h"
43 #include "TSystem.h"
44 
45 using namespace std ;
46 
48  ;
49 
50 
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 
55 {
56  _pkg = new RooStudyPackage(w) ;
57 }
58 
59 
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 
64 {
65  _pkg = new RooStudyPackage(w) ;
66  _pkg->addStudy(study) ;
67 }
68 
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 
72 RooStudyManager::RooStudyManager(const char* studyPackFileName)
73 {
74  string pwd = gDirectory->GetName() ;
75  TFile *f = new TFile(studyPackFileName) ;
76  _pkg = dynamic_cast<RooStudyPackage*>(f->Get("studypack")) ;
77  gDirectory->cd(Form("%s:",pwd.c_str())) ;
78 }
79 
80 
81 
82 ////////////////////////////////////////////////////////////////////////////////
83 
85 {
86  _pkg->addStudy(study) ;
87 }
88 
89 
90 
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 
94 void RooStudyManager::run(Int_t nExperiments)
95 {
96  _pkg->driver(nExperiments) ;
97 }
98 
99 
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 /// Open PROOF-Lite session
103 
104 void RooStudyManager::runProof(Int_t nExperiments, const char* proofHost, Bool_t showGui)
105 {
106  coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") opening PROOF session" << endl ;
107  void* p = (void*) gROOT->ProcessLineFast(Form("TProof::Open(\"%s\")",proofHost)) ;
108 
109  // Check that PROOF initialization actually succeeeded
110  if (p==0) {
111  coutE(Generation) << "RooStudyManager::runProof(" << GetName() << ") ERROR initializing proof, aborting" << endl ;
112  return ;
113  }
114 
115  // Suppress GUI if so requested
116  if (!showGui) {
117  gROOT->ProcessLineFast(Form("((TProof*)0x%lx)->SetProgressDialog(0) ;",(ULong_t)p)) ;
118  }
119 
120  // Propagate workspace to proof nodes
121  coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") sending work package to PROOF servers" << endl ;
122  gROOT->ProcessLineFast(Form("((TProof*)0x%lx)->AddInput((TObject*)0x%lx) ;",(ULong_t)p,(ULong_t)_pkg) ) ;
123 
124  // Run selector in parallel
125  coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") starting PROOF processing of " << nExperiments << " experiments" << endl ;
126 
127  gROOT->ProcessLineFast(Form("((TProof*)0x%lx)->Process(\"RooProofDriverSelector\",%d) ;",(ULong_t)p,nExperiments)) ;
128 
129  // Aggregate results data
130  coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") aggregating results data" << endl ;
131  TList* olist = (TList*) gROOT->ProcessLineFast(Form("((TProof*)0x%lx)->GetOutputList()",(ULong_t)p)) ;
132  aggregateData(olist) ;
133 
134  // cleaning up
135  coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") cleaning up input list" << endl ;
136  gROOT->ProcessLineFast(Form("((TProof*)0x%lx)->GetInputList()->Remove((TObject*)0x%lx) ;",(ULong_t)p,(ULong_t)_pkg) ) ;
137 
138 }
139 
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// "Option_t *option" takes the parameters forwarded to gProof->Close(option).
143 ///
144 /// This function is intended for scripts that run in loops
145 /// where it is essential to properly close all connections and delete
146 /// the TProof instance (frees ports).
147 
149 {
150  if (gROOT->GetListOfProofs()->LastIndex() != -1 && gROOT->ProcessLineFast("gProof;"))
151  {
152  gROOT->ProcessLineFast(Form("gProof->Close(\"%s\") ;",option)) ;
153  gROOT->ProcessLineFast("gProof->CloseProgressDialog() ;") ;
154 
155  // CloseProgressDialog does not do anything when run without GUI. This detects
156  // whether the proof instance is still there and deletes it if that is the case.
157  if (gROOT->GetListOfProofs()->LastIndex() != -1 && gROOT->ProcessLineFast("gProof;")) {
158  gROOT->ProcessLineFast("delete gProof ;") ;
159  }
160  } else {
161  ooccoutI((TObject*)NULL,Generation) << "RooStudyManager: No global Proof objects. No connections closed." << endl ;
162  }
163 }
164 
165 
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 
169 void RooStudyManager::prepareBatchInput(const char* studyName, Int_t nExpPerJob, Bool_t unifiedInput=kFALSE)
170 {
171  TFile f(Form("study_data_%s.root",studyName),"RECREATE") ;
172  _pkg->Write("studypack") ;
173  f.Close() ;
174 
175  if (unifiedInput) {
176 
177  // Write header of driver script
178  ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
179  bdr << "#!/bin/sh" << endl
180  << Form("if [ ! -f study_data_%s.root ] ; then",studyName) << endl
181  << "uudecode <<EOR" << endl ;
182  bdr.close() ;
183 
184  // Write uuencoded ROOT file (base64) in driver script
185  gSystem->Exec(Form("cat study_data_%s.root | uuencode -m study_data_%s.root >> study_driver_%s.sh",studyName,studyName,studyName)) ;
186 
187  // Write remainder of deriver script
188  ofstream bdr2 (Form("study_driver_%s.sh",studyName),ios::app) ;
189  bdr2 << "EOR" << endl
190  << "fi" << endl
191  << "root -l -b <<EOR" << endl
192  << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << endl
193  << ".q" << endl
194  << "EOR" << endl ;
195  // Remove binary input file
196  gSystem->Unlink(Form("study_data_%s.root",studyName)) ;
197 
198  coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << endl
199  << " input data files is embedded in driver script" << endl ;
200 
201  } else {
202 
203  ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
204  bdr << "#!/bin/sh" << endl
205  << "root -l -b <<EOR" << endl
206  << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << endl
207  << ".q" << endl
208  << "EOR" << endl ;
209 
210  coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << endl
211  << " input data file is " << Form("study_data_%s.root",studyName) << endl ;
212 
213  }
214 }
215 
216 
217 
218 
219 ////////////////////////////////////////////////////////////////////////////////
220 
221 void RooStudyManager::processBatchOutput(const char* filePat)
222 {
223  list<string> flist ;
224  expandWildCardSpec(filePat,flist) ;
225 
226  TList olist ;
227 
228  for (list<string>::iterator iter = flist.begin() ; iter!=flist.end() ; ++iter) {
229  coutP(DataHandling) << "RooStudyManager::processBatchOutput() now reading file " << *iter << endl ;
230  TFile f(iter->c_str()) ;
231 
232  TList* list = f.GetListOfKeys() ;
233  TIterator* kiter = list->MakeIterator();
234 
235  TObject* obj ;
236  TKey* key ;
237  while((key=(TKey*)kiter->Next())) {
238  obj = f.Get(key->GetName()) ;
239  TObject* clone = obj->Clone(obj->GetName()) ;
240  olist.Add(clone) ;
241  }
242  delete kiter ;
243  }
244  aggregateData(&olist) ;
245  olist.Delete() ;
246 }
247 
248 
249 ////////////////////////////////////////////////////////////////////////////////
250 
252 {
253  for (list<RooAbsStudy*>::iterator iter=_pkg->studies().begin() ; iter!=_pkg->studies().end() ; iter++) {
254  (*iter)->aggregateSummaryOutput(olist) ;
255  }
256 }
257 
258 
259 
260 
261 ////////////////////////////////////////////////////////////////////////////////
262 /// case with one single file
263 
264 void RooStudyManager::expandWildCardSpec(const char* name, list<string>& result)
265 {
266  if (!TString(name).MaybeWildcard()) {
267  result.push_back(name) ;
268  return ;
269  }
270 
271  // wildcarding used in name
272  TString basename(name);
273 
274  Int_t dotslashpos = -1;
275  {
276  Int_t next_dot = basename.Index(".root");
277  while(next_dot>=0) {
278  dotslashpos = next_dot;
279  next_dot = basename.Index(".root",dotslashpos+1);
280  }
281  if (basename[dotslashpos+5]!='/') {
282  // We found the 'last' .root in the name and it is not followed by
283  // a '/', so the tree name is _not_ specified in the name.
284  dotslashpos = -1;
285  }
286  }
287  //Int_t dotslashpos = basename.Index(".root/");
288  TString behind_dot_root;
289  if (dotslashpos>=0) {
290  // Copy the tree name specification
291  behind_dot_root = basename(dotslashpos+6,basename.Length()-dotslashpos+6);
292  // and remove it from basename
293  basename.Remove(dotslashpos+5);
294  }
295 
296  Int_t slashpos = basename.Last('/');
297  TString directory;
298  if (slashpos>=0) {
299  directory = basename(0,slashpos); // Copy the directory name
300  basename.Remove(0,slashpos+1); // and remove it from basename
301  } else {
302  directory = gSystem->UnixPathName(gSystem->WorkingDirectory());
303  }
304 
305  const char *file;
306  void *dir = gSystem->OpenDirectory(gSystem->ExpandPathName(directory.Data()));
307 
308  if (dir) {
309  //create a TList to store the file names (not yet sorted)
310  TList l;
311  TRegexp re(basename,kTRUE);
312  while ((file = gSystem->GetDirEntry(dir))) {
313  if (!strcmp(file,".") || !strcmp(file,"..")) continue;
314  TString s = file;
315  if ( (basename!=file) && s.Index(re) == kNPOS) continue;
316  l.Add(new TObjString(file));
317  }
318  gSystem->FreeDirectory(dir);
319  //sort the files in alphanumeric order
320  l.Sort();
321  TIter next(&l);
322  TObjString *obj;
323  while ((obj = (TObjString*)next())) {
324  file = obj->GetName();
325  if (behind_dot_root.Length() != 0)
326  result.push_back(Form("%s/%s/%s",directory.Data(),file,behind_dot_root.Data())) ;
327  else
328  result.push_back(Form("%s/%s",directory.Data(),file)) ;
329  }
330  l.Delete();
331  }
332 }
const char * GetName() const
Returns name of object.
Definition: TObjString.h:42
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition: TString.cxx:864
void run(Int_t nExperiments)
#define coutE(a)
Definition: RooMsgService.h:35
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:404
#define ooccoutI(o, a)
Definition: RooMsgService.h:52
static void closeProof(Option_t *option="s")
"Option_t *option" takes the parameters forwarded to gProof->Close(option).
virtual const char * WorkingDirectory()
Return working directory.
Definition: TSystem.cxx:865
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
Ssiz_t Length() const
Definition: TString.h:390
Collectable string class.
Definition: TObjString.h:32
const char Option_t
Definition: RtypesCore.h:62
#define coutI(a)
Definition: RooMsgService.h:32
#define gDirectory
Definition: TDirectory.h:218
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
Regular expression class.
Definition: TRegexp.h:35
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
#define gROOT
Definition: TROOT.h:340
void addStudy(RooAbsStudy &study)
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
Iterator abstract base class.
Definition: TIterator.h:32
#define coutP(a)
Definition: RooMsgService.h:33
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1036
virtual void Sort(Bool_t order=kSortAscending)
Sort linked list.
Definition: TList.cxx:770
const char * Data() const
Definition: TString.h:349
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition: TSystem.cxx:847
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1346
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TObject.cxx:203
ClassImp(RooStudyManager)
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:30
RooStudyManager(RooWorkspace &w)
A doubly linked list.
Definition: TList.h:47
void processBatchOutput(const char *filePat)
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
return
Definition: TBase64.cxx:62
char * Form(const char *fmt,...)
void expandWildCardSpec(const char *spec, std::list< std::string > &result)
case with one single file
TLine * l
Definition: textangle.C:4
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition: TSystem.cxx:657
void aggregateData(TList *olist)
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition: TSystem.cxx:839
TString & Remove(Ssiz_t pos)
Definition: TString.h:616
void runProof(Int_t nExperiments, const char *proofHost="", Bool_t showGui=kTRUE)
Open PROOF-Lite session.
double f(double x)
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:415
unsigned long ULong_t
Definition: RtypesCore.h:51
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
virtual void Add(TObject *obj)
Definition: TList.h:81
const Ssiz_t kNPOS
Definition: Rtypes.h:115
virtual TObject * Next()=0
#define NULL
Definition: Rtypes.h:82
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition: TSystem.cxx:830
double result[121]
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1243
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:582
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
void prepareBatchInput(const char *studyName, Int_t nExpPerJob, Bool_t unifiedInput)
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:898