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#include "RooStudyManager.h"
28#include "RooWorkspace.h"
29#include "RooAbsStudy.h"
30#include "RooDataSet.h"
31#include "RooMsgService.h"
32#include "RooStudyPackage.h"
33#include "TFile.h"
34#include "TObjString.h"
35#include "TRegexp.h"
36#include "TKey.h"
37#include "TROOT.h"
38#include "TSystem.h"
39
40#include <string>
41#include <fstream>
42
43////////////////////////////////////////////////////////////////////////////////
44
46
47////////////////////////////////////////////////////////////////////////////////
48
54
55
56////////////////////////////////////////////////////////////////////////////////
57
59{
60 std::string pwd = gDirectory->GetName();
61 std::unique_ptr<TFile> f{TFile::Open(studyPackFileName, "READ")};
62 _pkg = dynamic_cast<RooStudyPackage *>(f->Get("studypack"));
63 gDirectory->cd((pwd + ":").c_str());
64}
65
66
67
68////////////////////////////////////////////////////////////////////////////////
69
74
75
76
77
78////////////////////////////////////////////////////////////////////////////////
79
84
85
86////////////////////////////////////////////////////////////////////////////////
87
89{
90 TFile f(Form("study_data_%s.root",studyName),"RECREATE") ;
91 _pkg->Write("studypack") ;
92 f.Close() ;
93
94 if (unifiedInput) {
95
96 // Write header of driver script
97 std::ofstream bdr(Form("study_driver_%s.sh", studyName));
98 bdr << "#!/bin/sh" << std::endl
99 << Form("if [ ! -f study_data_%s.root ] ; then",studyName) << std::endl
100 << "uudecode <<EOR" << std::endl ;
101 bdr.close() ;
102
103 // Write uuencoded ROOT file (base64) in driver script
104 gSystem->Exec(Form("cat study_data_%s.root | uuencode -m study_data_%s.root >> study_driver_%s.sh",studyName,studyName,studyName)) ;
105
106 // Write remainder of driver script
107 std::ofstream bdr2(Form("study_driver_%s.sh", studyName), std::ios::app);
108 bdr2 << "EOR" << std::endl
109 << "fi" << std::endl
110 << "root -l -b <<EOR" << std::endl
111 << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << std::endl
112 << ".q" << std::endl
113 << "EOR" << std::endl ;
114 // Remove binary input file
115 gSystem->Unlink(Form("study_data_%s.root",studyName)) ;
116
117 coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << std::endl
118 << " input data files is embedded in driver script" << std::endl ;
119
120 } else {
121
122 std::ofstream bdr(Form("study_driver_%s.sh", studyName));
123 bdr << "#!/bin/sh" << std::endl
124 << "root -l -b <<EOR" << std::endl
125 << Form("RooStudyPackage::processFile(\"%s\",%d) ;", studyName, nExpPerJob) << std::endl
126 << ".q" << std::endl
127 << "EOR" << std::endl;
128
129 coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '"
130 << Form("study_driver_%s.sh", studyName) << "," << std::endl
131 << " input data file is " << Form("study_data_%s.root", studyName) << std::endl;
132
133 }
134}
135
136
137
138
139////////////////////////////////////////////////////////////////////////////////
140
142{
143 std::list<std::string> flist;
145
146 TList olist;
147
148 for (auto iter = flist.begin(); iter != flist.end(); ++iter) {
149 coutP(DataHandling) << "RooStudyManager::processBatchOutput() now reading file " << *iter << std::endl;
150 TFile f(iter->c_str());
151
152 for (auto *key : static_range_cast<TKey *>(*f.GetListOfKeys())) {
153 TObject *obj = f.Get(key->GetName());
154 olist.Add(obj->Clone(obj->GetName()));
155 }
156 }
158 olist.Delete() ;
159}
160
161
162////////////////////////////////////////////////////////////////////////////////
163
165{
166 for (auto study : _pkg->studies()) {
167 study->aggregateSummaryOutput(olist);
168 }
169}
170
171
172
173
174////////////////////////////////////////////////////////////////////////////////
175/// case with one single file
176
177void RooStudyManager::expandWildCardSpec(const char *name, std::list<std::string> &result)
178{
179 if (!TString(name).MaybeWildcard()) {
180 result.push_back(name) ;
181 return ;
182 }
183
184 // wildcarding used in name
186
187 Int_t dotslashpos = -1;
188 {
189 Int_t next_dot = basename.Index(".root");
190 while(next_dot>=0) {
192 next_dot = basename.Index(".root",dotslashpos+1);
193 }
194 if (basename[dotslashpos+5]!='/') {
195 // We found the 'last' .root in the name and it is not followed by
196 // a '/', so the tree name is _not_ specified in the name.
197 dotslashpos = -1;
198 }
199 }
200 //Int_t dotslashpos = basename.Index(".root/");
202 if (dotslashpos>=0) {
203 // Copy the tree name specification
205 // and remove it from basename
206 basename.Remove(dotslashpos+5);
207 }
208
209 Int_t slashpos = basename.Last('/');
211 if (slashpos>=0) {
212 directory = basename(0,slashpos); // Copy the directory name
213 basename.Remove(0,slashpos+1); // and remove it from basename
214 } else {
216 }
217
220 void *dir = gSystem->OpenDirectory(expand_directory.Data());
221
222 if (dir) {
223 //create a TList to store the file names (not yet sorted)
224 TList l;
225 TRegexp re(basename,true);
226 const char *file;
227 while ((file = gSystem->GetDirEntry(dir))) {
228 if (!strcmp(file,".") || !strcmp(file,"..")) continue;
229 TString s = file;
230 if ( (basename!=file) && s.Index(re) == kNPOS) continue;
231 l.Add(new TObjString(file));
232 }
234 //sort the files in alphanumeric order
235 l.Sort();
236 TIter next(&l);
237 TObjString *obj;
238 while ((obj = static_cast<TObjString*>(next()))) {
239 file = obj->GetName();
240 if (behind_dot_root.Length() != 0) {
241 result.push_back(Form("%s/%s/%s",directory.Data(),file,behind_dot_root.Data())) ;
242 } else {
243 result.push_back(Form("%s/%s", directory.Data(), file));
244 }
245 }
246 l.Delete();
247 }
248}
#define f(i)
Definition RSha256.hxx:104
#define coutI(a)
#define coutP(a)
constexpr Ssiz_t kNPOS
The equivalent of std::string::npos for the ROOT class TString.
Definition RtypesCore.h:132
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gDirectory
Definition TDirectory.h:385
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:142
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2570
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
const_iterator begin() const
const_iterator end() const
Abstract base class for RooStudyManager modules.
Definition RooAbsStudy.h:33
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)
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 file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
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:3800
A doubly linked list.
Definition TList.h:38
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:42
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:461
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition TObject.cxx:242
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:987
virtual void Delete(Option_t *option="")
Delete this object.
Definition TObject.cxx:267
Regular expression class.
Definition TRegexp.h:31
Basic string class.
Definition TString.h:138
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:662
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1289
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition TSystem.cxx:859
virtual void * OpenDirectory(const char *name)
Open a directory.
Definition TSystem.cxx:850
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition TSystem.cxx:655
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition TSystem.cxx:867
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1077
virtual const char * WorkingDirectory()
Return working directory.
Definition TSystem.cxx:885
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1396
TLine l
Definition textangle.C:4