Logo ROOT  
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 "TFile.h"
39 #include "TObjString.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 
54 {
55  _pkg = new RooStudyPackage(w) ;
56 }
57 
58 
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 
63 {
64  _pkg = new RooStudyPackage(w) ;
65  _pkg->addStudy(study) ;
66 }
67 
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 
71 RooStudyManager::RooStudyManager(const char* studyPackFileName)
72 {
73  string pwd = gDirectory->GetName() ;
74  TFile *f = new TFile(studyPackFileName) ;
75  _pkg = dynamic_cast<RooStudyPackage*>(f->Get("studypack")) ;
76  gDirectory->cd(Form("%s:",pwd.c_str())) ;
77 }
78 
79 
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 
84 {
85  _pkg->addStudy(study) ;
86 }
87 
88 
89 
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 
93 void RooStudyManager::run(Int_t nExperiments)
94 {
95  _pkg->driver(nExperiments) ;
96 }
97 
98 
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Open PROOF-Lite session
102 
103 void RooStudyManager::runProof(Int_t nExperiments, const char* proofHost, Bool_t showGui)
104 {
105  coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") opening PROOF session" << endl ;
106  void* p = (void*) gROOT->ProcessLineFast(Form("TProof::Open(\"%s\")",proofHost)) ;
107 
108  // Check that PROOF initialization actually succeeeded
109  if (p==0) {
110  coutE(Generation) << "RooStudyManager::runProof(" << GetName() << ") ERROR initializing proof, aborting" << endl ;
111  return ;
112  }
113 
114  // Suppress GUI if so requested
115  if (!showGui) {
116  gROOT->ProcessLineFast(Form("((TProof*)0x%lx)->SetProgressDialog(0) ;",(ULong_t)p)) ;
117  }
118 
119  // Propagate workspace to proof nodes
120  coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") sending work package to PROOF servers" << endl ;
121  gROOT->ProcessLineFast(Form("((TProof*)0x%lx)->AddInput((TObject*)0x%lx) ;",(ULong_t)p,(ULong_t)_pkg) ) ;
122 
123  // Run selector in parallel
124  coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") starting PROOF processing of " << nExperiments << " experiments" << endl ;
125 
126  gROOT->ProcessLineFast(Form("((TProof*)0x%lx)->Process(\"RooProofDriverSelector\",%d) ;",(ULong_t)p,nExperiments)) ;
127 
128  // Aggregate results data
129  coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") aggregating results data" << endl ;
130  TList* olist = (TList*) gROOT->ProcessLineFast(Form("((TProof*)0x%lx)->GetOutputList()",(ULong_t)p)) ;
131  aggregateData(olist) ;
132 
133  // cleaning up
134  coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") cleaning up input list" << endl ;
135  gROOT->ProcessLineFast(Form("((TProof*)0x%lx)->GetInputList()->Remove((TObject*)0x%lx) ;",(ULong_t)p,(ULong_t)_pkg) ) ;
136 
137 }
138 
139 
140 ////////////////////////////////////////////////////////////////////////////////
141 /// "Option_t *option" takes the parameters forwarded to gProof->Close(option).
142 ///
143 /// This function is intended for scripts that run in loops
144 /// where it is essential to properly close all connections and delete
145 /// the TProof instance (frees ports).
146 
148 {
149  if (gROOT->GetListOfProofs()->LastIndex() != -1 && gROOT->ProcessLineFast("gProof;"))
150  {
151  gROOT->ProcessLineFast(Form("gProof->Close(\"%s\") ;",option)) ;
152  gROOT->ProcessLineFast("gProof->CloseProgressDialog() ;") ;
153 
154  // CloseProgressDialog does not do anything when run without GUI. This detects
155  // whether the proof instance is still there and deletes it if that is the case.
156  if (gROOT->GetListOfProofs()->LastIndex() != -1 && gROOT->ProcessLineFast("gProof;")) {
157  gROOT->ProcessLineFast("delete gProof ;") ;
158  }
159  } else {
160  ooccoutI((TObject*)NULL,Generation) << "RooStudyManager: No global Proof objects. No connections closed." << endl ;
161  }
162 }
163 
164 
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 
168 void RooStudyManager::prepareBatchInput(const char* studyName, Int_t nExpPerJob, Bool_t unifiedInput=kFALSE)
169 {
170  TFile f(Form("study_data_%s.root",studyName),"RECREATE") ;
171  _pkg->Write("studypack") ;
172  f.Close() ;
173 
174  if (unifiedInput) {
175 
176  // Write header of driver script
177  ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
178  bdr << "#!/bin/sh" << endl
179  << Form("if [ ! -f study_data_%s.root ] ; then",studyName) << endl
180  << "uudecode <<EOR" << endl ;
181  bdr.close() ;
182 
183  // Write uuencoded ROOT file (base64) in driver script
184  gSystem->Exec(Form("cat study_data_%s.root | uuencode -m study_data_%s.root >> study_driver_%s.sh",studyName,studyName,studyName)) ;
185 
186  // Write remainder of deriver script
187  ofstream bdr2 (Form("study_driver_%s.sh",studyName),ios::app) ;
188  bdr2 << "EOR" << endl
189  << "fi" << endl
190  << "root -l -b <<EOR" << endl
191  << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << endl
192  << ".q" << endl
193  << "EOR" << endl ;
194  // Remove binary input file
195  gSystem->Unlink(Form("study_data_%s.root",studyName)) ;
196 
197  coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << endl
198  << " input data files is embedded in driver script" << endl ;
199 
200  } else {
201 
202  ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
203  bdr << "#!/bin/sh" << endl
204  << "root -l -b <<EOR" << endl
205  << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << endl
206  << ".q" << endl
207  << "EOR" << endl ;
208 
209  coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << endl
210  << " input data file is " << Form("study_data_%s.root",studyName) << endl ;
211 
212  }
213 }
214 
215 
216 
217 
218 ////////////////////////////////////////////////////////////////////////////////
219 
220 void RooStudyManager::processBatchOutput(const char* filePat)
221 {
222  list<string> flist ;
223  expandWildCardSpec(filePat,flist) ;
224 
225  TList olist ;
226 
227  for (list<string>::iterator iter = flist.begin() ; iter!=flist.end() ; ++iter) {
228  coutP(DataHandling) << "RooStudyManager::processBatchOutput() now reading file " << *iter << endl ;
229  TFile f(iter->c_str()) ;
230 
231  TList* list = f.GetListOfKeys() ;
232  TIterator* kiter = list->MakeIterator();
233 
234  TObject* obj ;
235  TKey* key ;
236  while((key=(TKey*)kiter->Next())) {
237  obj = f.Get(key->GetName()) ;
238  TObject* clone = obj->Clone(obj->GetName()) ;
239  olist.Add(clone) ;
240  }
241  delete kiter ;
242  }
243  aggregateData(&olist) ;
244  olist.Delete() ;
245 }
246 
247 
248 ////////////////////////////////////////////////////////////////////////////////
249 
251 {
252  for (list<RooAbsStudy*>::iterator iter=_pkg->studies().begin() ; iter!=_pkg->studies().end() ; ++iter) {
253  (*iter)->aggregateSummaryOutput(olist) ;
254  }
255 }
256 
257 
258 
259 
260 ////////////////////////////////////////////////////////////////////////////////
261 /// case with one single file
262 
263 void RooStudyManager::expandWildCardSpec(const char* name, list<string>& result)
264 {
265  if (!TString(name).MaybeWildcard()) {
266  result.push_back(name) ;
267  return ;
268  }
269 
270  // wildcarding used in name
271  TString basename(name);
272 
273  Int_t dotslashpos = -1;
274  {
275  Int_t next_dot = basename.Index(".root");
276  while(next_dot>=0) {
277  dotslashpos = next_dot;
278  next_dot = basename.Index(".root",dotslashpos+1);
279  }
280  if (basename[dotslashpos+5]!='/') {
281  // We found the 'last' .root in the name and it is not followed by
282  // a '/', so the tree name is _not_ specified in the name.
283  dotslashpos = -1;
284  }
285  }
286  //Int_t dotslashpos = basename.Index(".root/");
287  TString behind_dot_root;
288  if (dotslashpos>=0) {
289  // Copy the tree name specification
290  behind_dot_root = basename(dotslashpos+6,basename.Length()-dotslashpos+6);
291  // and remove it from basename
292  basename.Remove(dotslashpos+5);
293  }
294 
295  Int_t slashpos = basename.Last('/');
296  TString directory;
297  if (slashpos>=0) {
298  directory = basename(0,slashpos); // Copy the directory name
299  basename.Remove(0,slashpos+1); // and remove it from basename
300  } else {
301  directory = gSystem->UnixPathName(gSystem->WorkingDirectory());
302  }
303 
304  TString expand_directory = directory;
305  gSystem->ExpandPathName(expand_directory);
306  void *dir = gSystem->OpenDirectory(expand_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  const char *file;
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 }
l
auto * l
Definition: textangle.C:4
ooccoutI
#define ooccoutI(o, a)
Definition: RooMsgService.h:53
TSystem::Unlink
virtual int Unlink(const char *name)
Unlink, i.e.
Definition: TSystem.cxx:1379
RooWorkspace.h
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooStudyManager.h
RooMsgService.h
f
#define f(i)
Definition: RSha256.hxx:122
RooFit.h
kNPOS
const Ssiz_t kNPOS
Definition: RtypesCore.h:115
RooStudyManager::closeProof
static void closeProof(Option_t *option="s")
"Option_t *option" takes the parameters forwarded to gProof->Close(option).
Definition: RooStudyManager.cxx:147
TList::Delete
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:469
TString::Data
const char * Data() const
Definition: TString.h:369
coutP
#define coutP(a)
Definition: RooMsgService.h:31
TSystem::FreeDirectory
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition: TSystem.cxx:844
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
Form
char * Form(const char *fmt,...)
TObjString.h
RooStudyPackage
Definition: RooStudyPackage.h:31
coutE
#define coutE(a)
Definition: RooMsgService.h:33
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
RooStudyManager::expandWildCardSpec
void expandWildCardSpec(const char *spec, std::list< std::string > &result)
case with one single file
Definition: RooStudyManager.cxx:263
RooStudyManager::addStudy
void addStudy(RooAbsStudy &study)
Definition: RooStudyManager.cxx:83
TString::Length
Ssiz_t Length() const
Definition: TString.h:410
coutI
#define coutI(a)
Definition: RooMsgService.h:30
TString
Definition: TString.h:136
RooDataSet.h
TFile.h
TObjString::GetName
const char * GetName() const
Returns name of object.
Definition: TObjString.h:44
bool
TSystem::WorkingDirectory
virtual const char * WorkingDirectory()
Return working directory.
Definition: TSystem.cxx:870
TIterator
Definition: TIterator.h:30
TString::Last
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition: TString.cxx:892
TROOT.h
TObjString
Definition: TObjString.h:28
TList::MakeIterator
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const
Return a list iterator.
Definition: TList.cxx:721
TObject::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
TRegexp.h
RooStudyManager::processBatchOutput
void processBatchOutput(const char *filePat)
Definition: RooStudyManager.cxx:220
RooFit::DataHandling
@ DataHandling
Definition: RooGlobalFunc.h:69
RooStudyManager
Definition: RooStudyManager.h:33
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
TSystem.h
RooStudyManager::runProof
void runProof(Int_t nExperiments, const char *proofHost="", Bool_t showGui=kTRUE)
Open PROOF-Lite session.
Definition: RooStudyManager.cxx:103
RooAbsStudy.h
TString::Remove
TString & Remove(Ssiz_t pos)
Definition: TString.h:673
RooFit::Generation
@ Generation
Definition: RooGlobalFunc.h:67
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TSystem::Exec
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition: TSystem.cxx:654
TObject::Clone
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TObject.cxx:146
gDirectory
#define gDirectory
Definition: TDirectory.h:236
ULong_t
unsigned long ULong_t
Definition: RtypesCore.h:55
TSystem::ExpandPathName
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1272
TFile
Definition: TFile.h:54
TIterator::Next
virtual TObject * Next()=0
TRegexp
Definition: TRegexp.h:31
TString::Index
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:639
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TKey
Definition: TKey.h:28
RooStudyManager::prepareBatchInput
void prepareBatchInput(const char *studyName, Int_t nExpPerJob, Bool_t unifiedInput)
Definition: RooStudyManager.cxx:168
RooWorkspace
Definition: RooWorkspace.h:43
RooStudyManager::run
void run(Int_t nExperiments)
Definition: RooStudyManager.cxx:93
TSystem::OpenDirectory
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition: TSystem.cxx:835
RooStudyManager::RooStudyManager
RooStudyManager(RooWorkspace &w)
Definition: RooStudyManager.cxx:53
TSystem::UnixPathName
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition: TSystem.cxx:1061
file
Definition: file.py:1
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TSystem::GetDirEntry
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition: TSystem.cxx:852
TObject
Definition: TObject.h:37
RooStudyPackage.h
name
char name[80]
Definition: TGX11.cxx:110
TIter
Definition: TCollection.h:233
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
TKey.h
Riostream.h
TList
Definition: TList.h:44
RooAbsStudy
Definition: RooAbsStudy.h:33
gROOT
#define gROOT
Definition: TROOT.h:406
int
RooStudyManager::aggregateData
void aggregateData(TList *olist)
Definition: RooStudyManager.cxx:250