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#include "Riostream.h"
29
30#include "RooStudyManager.h"
31#include "RooWorkspace.h"
32#include "RooAbsStudy.h"
33#include "RooDataSet.h"
34#include "RooMsgService.h"
35#include "RooStudyPackage.h"
36#include "TFile.h"
37#include "TObjString.h"
38#include "TRegexp.h"
39#include "TKey.h"
40#include <string>
41#include "TROOT.h"
42#include "TSystem.h"
43
44using namespace std ;
45
47
48
49////////////////////////////////////////////////////////////////////////////////
50
52{
53 _pkg = new RooStudyPackage(w) ;
54}
55
56
57
58////////////////////////////////////////////////////////////////////////////////
59
61{
62 _pkg = new RooStudyPackage(w) ;
63 _pkg->addStudy(study) ;
64}
65
66
67////////////////////////////////////////////////////////////////////////////////
68
69RooStudyManager::RooStudyManager(const char* studyPackFileName)
70{
71 string pwd = gDirectory->GetName() ;
72 std::unique_ptr<TFile> f{TFile::Open(studyPackFileName, "READ")};
73 _pkg = dynamic_cast<RooStudyPackage*>(f->Get("studypack")) ;
74 gDirectory->cd(Form("%s:",pwd.c_str())) ;
75}
76
77
78
79////////////////////////////////////////////////////////////////////////////////
80
82{
83 _pkg->addStudy(study) ;
84}
85
86
87
88
89////////////////////////////////////////////////////////////////////////////////
90
91void RooStudyManager::run(Int_t nExperiments)
92{
93 _pkg->driver(nExperiments) ;
94}
95
96
97
98////////////////////////////////////////////////////////////////////////////////
99/// Open PROOF-Lite session
100
101void RooStudyManager::runProof(Int_t nExperiments, const char* proofHost, bool showGui)
102{
103 coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") opening PROOF session" << endl ;
104 void* p = (void*) gROOT->ProcessLineFast(Form("TProof::Open(\"%s\")",proofHost)) ;
105
106 // Check that PROOF initialization actually succeeeded
107 if (p==0) {
108 coutE(Generation) << "RooStudyManager::runProof(" << GetName() << ") ERROR initializing proof, aborting" << endl ;
109 return ;
110 }
111
112 // Suppress GUI if so requested
113 if (!showGui) {
114 gROOT->ProcessLineFast(Form("((TProof*)0x%zx)->SetProgressDialog(0) ;",(size_t)p)) ;
115 }
116
117 // Propagate workspace to proof nodes
118 coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") sending work package to PROOF servers" << endl ;
119 gROOT->ProcessLineFast(Form("((TProof*)0x%zx)->AddInput((TObject*)0x%zx) ;",(size_t)p,(size_t)_pkg) ) ;
120
121 // Run selector in parallel
122 coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") starting PROOF processing of " << nExperiments << " experiments" << endl ;
123
124 gROOT->ProcessLineFast(Form("((TProof*)0x%zx)->Process(\"RooProofDriverSelector\",%d) ;",(size_t)p,nExperiments)) ;
125
126 // Aggregate results data
127 coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") aggregating results data" << endl ;
128 TList* olist = (TList*) gROOT->ProcessLineFast(Form("((TProof*)0x%zx)->GetOutputList()",(size_t)p)) ;
129 aggregateData(olist) ;
130
131 // cleaning up
132 coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") cleaning up input list" << endl ;
133 gROOT->ProcessLineFast(Form("((TProof*)0x%zx)->GetInputList()->Remove((TObject*)0x%zx) ;",(size_t)p,(size_t)_pkg) ) ;
134
135}
136
137
138////////////////////////////////////////////////////////////////////////////////
139/// "Option_t *option" takes the parameters forwarded to gProof->Close(option).
140///
141/// This function is intended for scripts that run in loops
142/// where it is essential to properly close all connections and delete
143/// the TProof instance (frees ports).
144
146{
147 if (gROOT->GetListOfProofs()->LastIndex() != -1 && gROOT->ProcessLineFast("gProof;"))
148 {
149 gROOT->ProcessLineFast(Form("gProof->Close(\"%s\") ;",option)) ;
150 gROOT->ProcessLineFast("gProof->CloseProgressDialog() ;") ;
151
152 // CloseProgressDialog does not do anything when run without GUI. This detects
153 // whether the proof instance is still there and deletes it if that is the case.
154 if (gROOT->GetListOfProofs()->LastIndex() != -1 && gROOT->ProcessLineFast("gProof;")) {
155 gROOT->ProcessLineFast("delete gProof ;") ;
156 }
157 } else {
158 ooccoutI(nullptr,Generation) << "RooStudyManager: No global Proof objects. No connections closed." << endl ;
159 }
160}
161
162
163
164////////////////////////////////////////////////////////////////////////////////
165
166void RooStudyManager::prepareBatchInput(const char* studyName, Int_t nExpPerJob, bool unifiedInput=false)
167{
168 TFile f(Form("study_data_%s.root",studyName),"RECREATE") ;
169 _pkg->Write("studypack") ;
170 f.Close() ;
171
172 if (unifiedInput) {
173
174 // Write header of driver script
175 ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
176 bdr << "#!/bin/sh" << endl
177 << Form("if [ ! -f study_data_%s.root ] ; then",studyName) << endl
178 << "uudecode <<EOR" << endl ;
179 bdr.close() ;
180
181 // Write uuencoded ROOT file (base64) in driver script
182 gSystem->Exec(Form("cat study_data_%s.root | uuencode -m study_data_%s.root >> study_driver_%s.sh",studyName,studyName,studyName)) ;
183
184 // Write remainder of deriver script
185 ofstream bdr2 (Form("study_driver_%s.sh",studyName),ios::app) ;
186 bdr2 << "EOR" << endl
187 << "fi" << endl
188 << "root -l -b <<EOR" << endl
189 << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << endl
190 << ".q" << endl
191 << "EOR" << endl ;
192 // Remove binary input file
193 gSystem->Unlink(Form("study_data_%s.root",studyName)) ;
194
195 coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << endl
196 << " input data files is embedded in driver script" << endl ;
197
198 } else {
199
200 ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
201 bdr << "#!/bin/sh" << endl
202 << "root -l -b <<EOR" << endl
203 << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << endl
204 << ".q" << endl
205 << "EOR" << endl ;
206
207 coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << endl
208 << " input data file is " << Form("study_data_%s.root",studyName) << endl ;
209
210 }
211}
212
213
214
215
216////////////////////////////////////////////////////////////////////////////////
217
218void RooStudyManager::processBatchOutput(const char* filePat)
219{
220 list<string> flist ;
221 expandWildCardSpec(filePat,flist) ;
222
223 TList olist ;
224
225 for (list<string>::iterator iter = flist.begin() ; iter!=flist.end() ; ++iter) {
226 coutP(DataHandling) << "RooStudyManager::processBatchOutput() now reading file " << *iter << endl ;
227 TFile f(iter->c_str()) ;
228
229 for(auto * key : static_range_cast<TKey*>(*f.GetListOfKeys())) {
230 TObject * obj = f.Get(key->GetName()) ;
231 olist.Add(obj->Clone(obj->GetName())) ;
232 }
233 }
234 aggregateData(&olist) ;
235 olist.Delete() ;
236}
237
238
239////////////////////////////////////////////////////////////////////////////////
240
242{
243 for (list<RooAbsStudy*>::iterator iter=_pkg->studies().begin() ; iter!=_pkg->studies().end() ; ++iter) {
244 (*iter)->aggregateSummaryOutput(olist) ;
245 }
246}
247
248
249
250
251////////////////////////////////////////////////////////////////////////////////
252/// case with one single file
253
254void RooStudyManager::expandWildCardSpec(const char* name, list<string>& result)
255{
256 if (!TString(name).MaybeWildcard()) {
257 result.push_back(name) ;
258 return ;
259 }
260
261 // wildcarding used in name
262 TString basename(name);
263
264 Int_t dotslashpos = -1;
265 {
266 Int_t next_dot = basename.Index(".root");
267 while(next_dot>=0) {
268 dotslashpos = next_dot;
269 next_dot = basename.Index(".root",dotslashpos+1);
270 }
271 if (basename[dotslashpos+5]!='/') {
272 // We found the 'last' .root in the name and it is not followed by
273 // a '/', so the tree name is _not_ specified in the name.
274 dotslashpos = -1;
275 }
276 }
277 //Int_t dotslashpos = basename.Index(".root/");
278 TString behind_dot_root;
279 if (dotslashpos>=0) {
280 // Copy the tree name specification
281 behind_dot_root = basename(dotslashpos+6,basename.Length()-dotslashpos+6);
282 // and remove it from basename
283 basename.Remove(dotslashpos+5);
284 }
285
286 Int_t slashpos = basename.Last('/');
287 TString directory;
288 if (slashpos>=0) {
289 directory = basename(0,slashpos); // Copy the directory name
290 basename.Remove(0,slashpos+1); // and remove it from basename
291 } else {
293 }
294
295 TString expand_directory = directory;
296 gSystem->ExpandPathName(expand_directory);
297 void *dir = gSystem->OpenDirectory(expand_directory.Data());
298
299 if (dir) {
300 //create a TList to store the file names (not yet sorted)
301 TList l;
302 TRegexp re(basename,true);
303 const char *file;
304 while ((file = gSystem->GetDirEntry(dir))) {
305 if (!strcmp(file,".") || !strcmp(file,"..")) continue;
306 TString s = file;
307 if ( (basename!=file) && s.Index(re) == kNPOS) continue;
308 l.Add(new TObjString(file));
309 }
311 //sort the files in alphanumeric order
312 l.Sort();
313 TIter next(&l);
314 TObjString *obj;
315 while ((obj = (TObjString*)next())) {
316 file = obj->GetName();
317 if (behind_dot_root.Length() != 0)
318 result.push_back(Form("%s/%s/%s",directory.Data(),file,behind_dot_root.Data())) ;
319 else
320 result.push_back(Form("%s/%s",directory.Data(),file)) ;
321 }
322 l.Delete();
323 }
324}
#define f(i)
Definition RSha256.hxx:104
#define coutI(a)
#define coutP(a)
#define coutE(a)
#define ooccoutI(o, a)
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:124
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
#define gDirectory
Definition TDirectory.h:386
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:405
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
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 showGui=true)
Open PROOF-Lite session.
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).
void prepareBatchInput(const char *studyName, Int_t nExpPerJob, bool unifiedInput)
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:51
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4053
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:470
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Collectable string class.
Definition TObjString.h:28
const char * GetName() const override
Returns name of object.
Definition TObjString.h:38
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:439
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition TObject.cxx:223
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition TObject.cxx:874
virtual void Delete(Option_t *option="")
Delete this object.
Definition TObject.cxx:248
Regular expression class.
Definition TRegexp.h:31
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:421
const char * Data() const
Definition TString.h:380
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition TString.cxx:924
TString & Remove(Ssiz_t pos)
Definition TString.h:685
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1277
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition TSystem.cxx:848
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition TSystem.cxx:839
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:856
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1066
virtual const char * WorkingDirectory()
Return working directory.
Definition TSystem.cxx:874
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1384
Definition file.py:1
TLine l
Definition textangle.C:4