Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
22RooStudyManager is a utility class to manage studies that consist of
23repeated 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
46using 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
71RooStudyManager::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
93void RooStudyManager::run(Int_t nExperiments)
94{
95 _pkg->driver(nExperiments) ;
96}
97
98
99
100////////////////////////////////////////////////////////////////////////////////
101/// Open PROOF-Lite session
102
103void 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%zx)->SetProgressDialog(0) ;",(size_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%zx)->AddInput((TObject*)0x%zx) ;",(size_t)p,(size_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%zx)->Process(\"RooProofDriverSelector\",%d) ;",(size_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%zx)->GetOutputList()",(size_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%zx)->GetInputList()->Remove((TObject*)0x%zx) ;",(size_t)p,(size_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
168void 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
220void 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
263void 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 {
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 }
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}
#define f(i)
Definition RSha256.hxx:104
#define coutI(a)
#define coutP(a)
#define coutE(a)
#define ooccoutI(o, a)
const Ssiz_t kNPOS
Definition RtypesCore.h:124
const Bool_t kFALSE
Definition RtypesCore.h:101
const Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
#define gDirectory
Definition TDirectory.h:385
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:404
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
RooAbsStudy is an abstract base class for RooStudyManager modules.
Definition RooAbsStudy.h:33
RooStudyManager is a utility class to manage studies that consist of repeated applications of generat...
void runProof(Int_t nExperiments, const char *proofHost="", Bool_t showGui=kTRUE)
Open PROOF-Lite session.
void prepareBatchInput(const char *studyName, Int_t nExpPerJob, Bool_t unifiedInput)
void expandWildCardSpec(const char *spec, std::list< std::string > &result)
case with one single file
void run(Int_t nExperiments)
void processBatchOutput(const char *filePat)
void addStudy(RooAbsStudy &study)
static void closeProof(Option_t *option="s")
"Option_t *option" takes the parameters forwarded to gProof->Close(option).
RooStudyPackage * _pkg
RooStudyManager(RooWorkspace &w)
void aggregateData(TList *olist)
RooStudyPackage is a utility class to manage studies that consist of repeated applications of generat...
void driver(Int_t nExperiments)
void addStudy(RooAbsStudy &study)
std::list< RooAbsStudy * > & studies()
The RooWorkspace is a persistable container for RooFit projects.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
Iterator abstract base class.
Definition TIterator.h:30
virtual TObject * Next()=0
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
A doubly linked list.
Definition TList.h:38
virtual void Add(TObject *obj)
Definition TList.h:81
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const
Return a list iterator.
Definition TList.cxx:722
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:470
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
Collectable string class.
Definition TObjString.h:28
const char * GetName() const
Returns name of object.
Definition TObjString.h:38
Mother of all ROOT objects.
Definition TObject.h:41
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition TObject.cxx:868
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:429
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition TObject.cxx:216
Regular expression class.
Definition TRegexp.h:31
Basic string class.
Definition TString.h:136
Ssiz_t Length() const
Definition TString.h:410
const char * Data() const
Definition TString.h:369
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition TString.cxx:916
TString & Remove(Ssiz_t pos)
Definition TString.h:673
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:639
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1274
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition TSystem.cxx:846
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition TSystem.cxx:837
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition TSystem.cxx:656
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition TSystem.cxx:854
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1063
virtual const char * WorkingDirectory()
Return working directory.
Definition TSystem.cxx:872
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1381
Definition file.py:1
auto * l
Definition textangle.C:4