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
44////////////////////////////////////////////////////////////////////////////////
45
47
48////////////////////////////////////////////////////////////////////////////////
49
55
56
57////////////////////////////////////////////////////////////////////////////////
58
60{
61 std::string pwd = gDirectory->GetName();
62 std::unique_ptr<TFile> f{TFile::Open(studyPackFileName, "READ")};
63 _pkg = dynamic_cast<RooStudyPackage *>(f->Get("studypack"));
64 gDirectory->cd((pwd + ":").c_str());
65}
66
67
68
69////////////////////////////////////////////////////////////////////////////////
70
75
76
77
78
79////////////////////////////////////////////////////////////////////////////////
80
85
86
87////////////////////////////////////////////////////////////////////////////////
88
90{
91 TFile f(Form("study_data_%s.root",studyName),"RECREATE") ;
92 _pkg->Write("studypack") ;
93 f.Close() ;
94
95 if (unifiedInput) {
96
97 // Write header of driver script
98 std::ofstream bdr(Form("study_driver_%s.sh", studyName));
99 bdr << "#!/bin/sh" << std::endl
100 << Form("if [ ! -f study_data_%s.root ] ; then",studyName) << std::endl
101 << "uudecode <<EOR" << std::endl ;
102 bdr.close() ;
103
104 // Write uuencoded ROOT file (base64) in driver script
105 gSystem->Exec(Form("cat study_data_%s.root | uuencode -m study_data_%s.root >> study_driver_%s.sh",studyName,studyName,studyName)) ;
106
107 // Write remainder of driver script
108 std::ofstream bdr2(Form("study_driver_%s.sh", studyName), std::ios::app);
109 bdr2 << "EOR" << std::endl
110 << "fi" << std::endl
111 << "root -l -b <<EOR" << std::endl
112 << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << std::endl
113 << ".q" << std::endl
114 << "EOR" << std::endl ;
115 // Remove binary input file
116 gSystem->Unlink(Form("study_data_%s.root",studyName)) ;
117
118 coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << std::endl
119 << " input data files is embedded in driver script" << std::endl ;
120
121 } else {
122
123 std::ofstream bdr(Form("study_driver_%s.sh", studyName));
124 bdr << "#!/bin/sh" << std::endl
125 << "root -l -b <<EOR" << std::endl
126 << Form("RooStudyPackage::processFile(\"%s\",%d) ;", studyName, nExpPerJob) << std::endl
127 << ".q" << std::endl
128 << "EOR" << std::endl;
129
130 coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '"
131 << Form("study_driver_%s.sh", studyName) << "," << std::endl
132 << " input data file is " << Form("study_data_%s.root", studyName) << std::endl;
133
134 }
135}
136
137
138
139
140////////////////////////////////////////////////////////////////////////////////
141
143{
144 std::list<std::string> flist;
146
147 TList olist;
148
149 for (auto iter = flist.begin(); iter != flist.end(); ++iter) {
150 coutP(DataHandling) << "RooStudyManager::processBatchOutput() now reading file " << *iter << std::endl;
151 TFile f(iter->c_str());
152
153 for (auto *key : static_range_cast<TKey *>(*f.GetListOfKeys())) {
154 TObject *obj = f.Get(key->GetName());
155 olist.Add(obj->Clone(obj->GetName()));
156 }
157 }
159 olist.Delete() ;
160}
161
162
163////////////////////////////////////////////////////////////////////////////////
164
166{
167 for (auto study : _pkg->studies()) {
168 study->aggregateSummaryOutput(olist);
169 }
170}
171
172
173
174
175////////////////////////////////////////////////////////////////////////////////
176/// case with one single file
177
178void RooStudyManager::expandWildCardSpec(const char *name, std::list<std::string> &result)
179{
180 if (!TString(name).MaybeWildcard()) {
181 result.push_back(name) ;
182 return ;
183 }
184
185 // wildcarding used in name
187
188 Int_t dotslashpos = -1;
189 {
190 Int_t next_dot = basename.Index(".root");
191 while(next_dot>=0) {
193 next_dot = basename.Index(".root",dotslashpos+1);
194 }
195 if (basename[dotslashpos+5]!='/') {
196 // We found the 'last' .root in the name and it is not followed by
197 // a '/', so the tree name is _not_ specified in the name.
198 dotslashpos = -1;
199 }
200 }
201 //Int_t dotslashpos = basename.Index(".root/");
203 if (dotslashpos>=0) {
204 // Copy the tree name specification
206 // and remove it from basename
207 basename.Remove(dotslashpos+5);
208 }
209
210 Int_t slashpos = basename.Last('/');
212 if (slashpos>=0) {
213 directory = basename(0,slashpos); // Copy the directory name
214 basename.Remove(0,slashpos+1); // and remove it from basename
215 } else {
217 }
218
221 void *dir = gSystem->OpenDirectory(expand_directory.Data());
222
223 if (dir) {
224 //create a TList to store the file names (not yet sorted)
225 TList l;
226 TRegexp re(basename,true);
227 const char *file;
228 while ((file = gSystem->GetDirEntry(dir))) {
229 if (!strcmp(file,".") || !strcmp(file,"..")) continue;
230 TString s = file;
231 if ( (basename!=file) && s.Index(re) == kNPOS) continue;
232 l.Add(new TObjString(file));
233 }
235 //sort the files in alphanumeric order
236 l.Sort();
237 TIter next(&l);
238 TObjString *obj;
239 while ((obj = static_cast<TObjString*>(next()))) {
240 file = obj->GetName();
241 if (behind_dot_root.Length() != 0) {
242 result.push_back(Form("%s/%s/%s",directory.Data(),file,behind_dot_root.Data())) ;
243 } else {
244 result.push_back(Form("%s/%s", directory.Data(), file));
245 }
246 }
247 l.Delete();
248 }
249}
#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:131
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:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2495
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
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 ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
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:3764
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:41
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:457
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:964
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:659
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1285
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition TSystem.cxx:855
virtual void * OpenDirectory(const char *name)
Open a directory.
Definition TSystem.cxx:846
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:863
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1073
virtual const char * WorkingDirectory()
Return working directory.
Definition TSystem.cxx:881
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1392
TLine l
Definition textangle.C:4