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