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
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 "TTree.h"
39#include "TFile.h"
40#include "TObjString.h"
41#include "TRegexp.h"
42#include "TKey.h"
43#include <string>
44#include "TROOT.h"
45#include "TSystem.h"
46
47using namespace std ;
48
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
72RooStudyManager::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
94void RooStudyManager::run(Int_t nExperiments)
95{
96 _pkg->driver(nExperiments) ;
97}
98
99
100
101////////////////////////////////////////////////////////////////////////////////
102/// Open PROOF-Lite session
103
104void 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
169void 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
221void 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
264void 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 {
303 }
304
305 TString expand_directory = directory;
306 gSystem->ExpandPathName(expand_directory);
307 void *dir = gSystem->OpenDirectory(expand_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 const char *file;
314 while ((file = gSystem->GetDirEntry(dir))) {
315 if (!strcmp(file,".") || !strcmp(file,"..")) continue;
316 TString s = file;
317 if ( (basename!=file) && s.Index(re) == kNPOS) continue;
318 l.Add(new TObjString(file));
319 }
321 //sort the files in alphanumeric order
322 l.Sort();
323 TIter next(&l);
324 TObjString *obj;
325 while ((obj = (TObjString*)next())) {
326 file = obj->GetName();
327 if (behind_dot_root.Length() != 0)
328 result.push_back(Form("%s/%s/%s",directory.Data(),file,behind_dot_root.Data())) ;
329 else
330 result.push_back(Form("%s/%s",directory.Data(),file)) ;
331 }
332 l.Delete();
333 }
334}
#define f(i)
Definition: RSha256.hxx:104
#define coutI(a)
Definition: RooMsgService.h:30
#define coutP(a)
Definition: RooMsgService.h:31
#define coutE(a)
Definition: RooMsgService.h:33
#define ooccoutI(o, a)
Definition: RooMsgService.h:53
const Ssiz_t kNPOS
Definition: RtypesCore.h:113
const Bool_t kFALSE
Definition: RtypesCore.h:90
unsigned long ULong_t
Definition: RtypesCore.h:53
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
#define gDirectory
Definition: TDirectory.h:229
char name[80]
Definition: TGX11.cxx:109
#define gROOT
Definition: TROOT.h:406
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Definition: TSystem.h:556
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.
Definition: RooWorkspace.h:43
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:53
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:44
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const
Return a list iterator.
Definition: TList.cxx:721
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:469
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:37
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:796
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TObject.cxx:144
Regular expression class.
Definition: TRegexp.h:31
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
const char * Data() const
Definition: TString.h:364
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition: TString.cxx:892
TString & Remove(Ssiz_t pos)
Definition: TString.h:668
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:634
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1269
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition: TSystem.cxx:841
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition: TSystem.cxx:832
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition: TSystem.cxx:651
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition: TSystem.cxx:849
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition: TSystem.cxx:1058
virtual const char * WorkingDirectory()
Return working directory.
Definition: TSystem.cxx:867
virtual int Unlink(const char *name)
Unlink, i.e.
Definition: TSystem.cxx:1376
@ Generation
Definition: RooGlobalFunc.h:67
@ DataHandling
Definition: RooGlobalFunc.h:69
static constexpr double s
Definition: file.py:1
auto * l
Definition: textangle.C:4