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
22Utility 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 std::string, std::endl, std::ios, std::list, std::ofstream;
45
47
48
49////////////////////////////////////////////////////////////////////////////////
50
52
53////////////////////////////////////////////////////////////////////////////////
54
56{
57
58 _pkg->addStudy(study) ;
59}
60
61
62////////////////////////////////////////////////////////////////////////////////
63
64RooStudyManager::RooStudyManager(const char* studyPackFileName)
65{
66 string pwd = gDirectory->GetName() ;
67 std::unique_ptr<TFile> f{TFile::Open(studyPackFileName, "READ")};
68 _pkg = dynamic_cast<RooStudyPackage*>(f->Get("studypack")) ;
69 gDirectory->cd(Form("%s:",pwd.c_str())) ;
70}
71
72
73
74////////////////////////////////////////////////////////////////////////////////
75
77{
78 _pkg->addStudy(study) ;
79}
80
81
82
83
84////////////////////////////////////////////////////////////////////////////////
85
86void RooStudyManager::run(Int_t nExperiments)
87{
88 _pkg->driver(nExperiments) ;
89}
90
91
92
93////////////////////////////////////////////////////////////////////////////////
94/// Open PROOF-Lite session
95
96void RooStudyManager::runProof(Int_t nExperiments, const char* proofHost, bool showGui)
97{
98 coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") opening PROOF session" << endl ;
99 void* p = reinterpret_cast<void*>(gROOT->ProcessLineFast(Form("TProof::Open(\"%s\")",proofHost)));
100
101 // Check that PROOF initialization actually succeeded
102 if (p==nullptr) {
103 coutE(Generation) << "RooStudyManager::runProof(" << GetName() << ") ERROR initializing proof, aborting" << endl ;
104 return ;
105 }
106
107 // Suppress GUI if so requested
108 if (!showGui) {
109 gROOT->ProcessLineFast(Form("((TProof*)0x%zx)->SetProgressDialog(0) ;",reinterpret_cast<size_t>(p))) ;
110 }
111
112 // Propagate workspace to proof nodes
113 coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") sending work package to PROOF servers" << endl ;
114 gROOT->ProcessLineFast(Form("((TProof*)0x%zx)->AddInput((TObject*)0x%zx) ;",reinterpret_cast<size_t>(p),reinterpret_cast<size_t>(_pkg)) ) ;
115
116 // Run selector in parallel
117 coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") starting PROOF processing of " << nExperiments << " experiments" << endl ;
118
119 gROOT->ProcessLineFast(Form("((TProof*)0x%zx)->Process(\"RooProofDriverSelector\",%d) ;",reinterpret_cast<size_t>(p),nExperiments)) ;
120
121 // Aggregate results data
122 coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") aggregating results data" << endl ;
123 TList* olist = reinterpret_cast<TList*>(gROOT->ProcessLineFast(Form("((TProof*)0x%zx)->GetOutputList()",reinterpret_cast<size_t>(p))));
124 aggregateData(olist) ;
125
126 // cleaning up
127 coutP(Generation) << "RooStudyManager::runProof(" << GetName() << ") cleaning up input list" << endl ;
128 gROOT->ProcessLineFast(Form("((TProof*)0x%zx)->GetInputList()->Remove((TObject*)0x%zx) ;",reinterpret_cast<size_t>(p),reinterpret_cast<size_t>(_pkg)) ) ;
129
130}
131
132
133////////////////////////////////////////////////////////////////////////////////
134/// "Option_t *option" takes the parameters forwarded to gProof->Close(option).
135///
136/// This function is intended for scripts that run in loops
137/// where it is essential to properly close all connections and delete
138/// the TProof instance (frees ports).
139
141{
142 if (gROOT->GetListOfProofs()->LastIndex() != -1 && gROOT->ProcessLineFast("gProof;"))
143 {
144 gROOT->ProcessLineFast(Form("gProof->Close(\"%s\") ;",option)) ;
145 gROOT->ProcessLineFast("gProof->CloseProgressDialog() ;") ;
146
147 // CloseProgressDialog does not do anything when run without GUI. This detects
148 // whether the proof instance is still there and deletes it if that is the case.
149 if (gROOT->GetListOfProofs()->LastIndex() != -1 && gROOT->ProcessLineFast("gProof;")) {
150 gROOT->ProcessLineFast("delete gProof ;") ;
151 }
152 } else {
153 ooccoutI(nullptr,Generation) << "RooStudyManager: No global Proof objects. No connections closed." << endl ;
154 }
155}
156
157
158
159////////////////////////////////////////////////////////////////////////////////
160
161void RooStudyManager::prepareBatchInput(const char* studyName, Int_t nExpPerJob, bool unifiedInput=false)
162{
163 TFile f(Form("study_data_%s.root",studyName),"RECREATE") ;
164 _pkg->Write("studypack") ;
165 f.Close() ;
166
167 if (unifiedInput) {
168
169 // Write header of driver script
170 ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
171 bdr << "#!/bin/sh" << endl
172 << Form("if [ ! -f study_data_%s.root ] ; then",studyName) << endl
173 << "uudecode <<EOR" << endl ;
174 bdr.close() ;
175
176 // Write uuencoded ROOT file (base64) in driver script
177 gSystem->Exec(Form("cat study_data_%s.root | uuencode -m study_data_%s.root >> study_driver_%s.sh",studyName,studyName,studyName)) ;
178
179 // Write remainder of driver script
180 ofstream bdr2 (Form("study_driver_%s.sh",studyName),ios::app) ;
181 bdr2 << "EOR" << endl
182 << "fi" << endl
183 << "root -l -b <<EOR" << endl
184 << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << endl
185 << ".q" << endl
186 << "EOR" << endl ;
187 // Remove binary input file
188 gSystem->Unlink(Form("study_data_%s.root",studyName)) ;
189
190 coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << endl
191 << " input data files is embedded in driver script" << endl ;
192
193 } else {
194
195 ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
196 bdr << "#!/bin/sh" << endl
197 << "root -l -b <<EOR" << endl
198 << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << endl
199 << ".q" << endl
200 << "EOR" << endl ;
201
202 coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << endl
203 << " input data file is " << Form("study_data_%s.root",studyName) << endl ;
204
205 }
206}
207
208
209
210
211////////////////////////////////////////////////////////////////////////////////
212
213void RooStudyManager::processBatchOutput(const char* filePat)
214{
215 list<string> flist ;
216 expandWildCardSpec(filePat,flist) ;
217
218 TList olist ;
219
220 for (list<string>::iterator iter = flist.begin() ; iter!=flist.end() ; ++iter) {
221 coutP(DataHandling) << "RooStudyManager::processBatchOutput() now reading file " << *iter << endl ;
222 TFile f(iter->c_str()) ;
223
224 for(auto * key : static_range_cast<TKey*>(*f.GetListOfKeys())) {
225 TObject * obj = f.Get(key->GetName()) ;
226 olist.Add(obj->Clone(obj->GetName())) ;
227 }
228 }
229 aggregateData(&olist) ;
230 olist.Delete() ;
231}
232
233
234////////////////////////////////////////////////////////////////////////////////
235
237{
238 for (list<RooAbsStudy*>::iterator iter=_pkg->studies().begin() ; iter!=_pkg->studies().end() ; ++iter) {
239 (*iter)->aggregateSummaryOutput(olist) ;
240 }
241}
242
243
244
245
246////////////////////////////////////////////////////////////////////////////////
247/// case with one single file
248
249void RooStudyManager::expandWildCardSpec(const char* name, list<string>& result)
250{
251 if (!TString(name).MaybeWildcard()) {
252 result.push_back(name) ;
253 return ;
254 }
255
256 // wildcarding used in name
257 TString basename(name);
258
259 Int_t dotslashpos = -1;
260 {
261 Int_t next_dot = basename.Index(".root");
262 while(next_dot>=0) {
263 dotslashpos = next_dot;
264 next_dot = basename.Index(".root",dotslashpos+1);
265 }
266 if (basename[dotslashpos+5]!='/') {
267 // We found the 'last' .root in the name and it is not followed by
268 // a '/', so the tree name is _not_ specified in the name.
269 dotslashpos = -1;
270 }
271 }
272 //Int_t dotslashpos = basename.Index(".root/");
273 TString behind_dot_root;
274 if (dotslashpos>=0) {
275 // Copy the tree name specification
276 behind_dot_root = basename(dotslashpos+6,basename.Length()-dotslashpos+6);
277 // and remove it from basename
278 basename.Remove(dotslashpos+5);
279 }
280
281 Int_t slashpos = basename.Last('/');
282 TString directory;
283 if (slashpos>=0) {
284 directory = basename(0,slashpos); // Copy the directory name
285 basename.Remove(0,slashpos+1); // and remove it from basename
286 } else {
288 }
289
290 TString expand_directory = directory;
291 gSystem->ExpandPathName(expand_directory);
292 void *dir = gSystem->OpenDirectory(expand_directory.Data());
293
294 if (dir) {
295 //create a TList to store the file names (not yet sorted)
296 TList l;
297 TRegexp re(basename,true);
298 const char *file;
299 while ((file = gSystem->GetDirEntry(dir))) {
300 if (!strcmp(file,".") || !strcmp(file,"..")) continue;
301 TString s = file;
302 if ( (basename!=file) && s.Index(re) == kNPOS) continue;
303 l.Add(new TObjString(file));
304 }
306 //sort the files in alphanumeric order
307 l.Sort();
308 TIter next(&l);
309 TObjString *obj;
310 while ((obj = static_cast<TObjString*>(next()))) {
311 file = obj->GetName();
312 if (behind_dot_root.Length() != 0) {
313 result.push_back(Form("%s/%s/%s",directory.Data(),file,behind_dot_root.Data())) ;
314 } else {
315 result.push_back(Form("%s/%s", directory.Data(), file));
316 }
317 }
318 l.Delete();
319 }
320}
#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:384
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:407
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
Abstract base class for RooStudyManager modules.
Definition RooAbsStudy.h:33
Utility class to manage studies that consist of repeated applications of generate-and-fit operations ...
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)
Utility class to manage studies that consist of repeated applications of generate-and-fit operations ...
void driver(Int_t nExperiments)
void addStudy(RooAbsStudy &study)
std::list< RooAbsStudy * > & studies()
Persistable container for RooFit projects.
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
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:4082
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:83
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:468
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:880
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:417
const char * Data() const
Definition TString.h:376
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition TString.cxx:931
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:1274
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition TSystem.cxx:845
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition TSystem.cxx:836
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition TSystem.cxx:653
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition TSystem.cxx:853
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:871
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1381
TLine l
Definition textangle.C:4