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
60
61
62////////////////////////////////////////////////////////////////////////////////
63
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
80
81
82
83
84////////////////////////////////////////////////////////////////////////////////
85
90
91
92////////////////////////////////////////////////////////////////////////////////
93
95{
96 TFile f(Form("study_data_%s.root",studyName),"RECREATE") ;
97 _pkg->Write("studypack") ;
98 f.Close() ;
99
100 if (unifiedInput) {
101
102 // Write header of driver script
103 ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
104 bdr << "#!/bin/sh" << std::endl
105 << Form("if [ ! -f study_data_%s.root ] ; then",studyName) << std::endl
106 << "uudecode <<EOR" << std::endl ;
107 bdr.close() ;
108
109 // Write uuencoded ROOT file (base64) in driver script
110 gSystem->Exec(Form("cat study_data_%s.root | uuencode -m study_data_%s.root >> study_driver_%s.sh",studyName,studyName,studyName)) ;
111
112 // Write remainder of driver script
113 ofstream bdr2 (Form("study_driver_%s.sh",studyName),ios::app) ;
114 bdr2 << "EOR" << std::endl
115 << "fi" << std::endl
116 << "root -l -b <<EOR" << std::endl
117 << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << std::endl
118 << ".q" << std::endl
119 << "EOR" << std::endl ;
120 // Remove binary input file
121 gSystem->Unlink(Form("study_data_%s.root",studyName)) ;
122
123 coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << std::endl
124 << " input data files is embedded in driver script" << std::endl ;
125
126 } else {
127
128 ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
129 bdr << "#!/bin/sh" << std::endl
130 << "root -l -b <<EOR" << std::endl
131 << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << std::endl
132 << ".q" << std::endl
133 << "EOR" << std::endl ;
134
135 coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << std::endl
136 << " input data file is " << Form("study_data_%s.root",studyName) << std::endl ;
137
138 }
139}
140
141
142
143
144////////////////////////////////////////////////////////////////////////////////
145
147{
150
151 TList olist ;
152
153 for (list<string>::iterator iter = flist.begin() ; iter!=flist.end() ; ++iter) {
154 coutP(DataHandling) << "RooStudyManager::processBatchOutput() now reading file " << *iter << std::endl ;
155 TFile f(iter->c_str()) ;
156
157 for(auto * key : static_range_cast<TKey*>(*f.GetListOfKeys())) {
158 TObject * obj = f.Get(key->GetName()) ;
159 olist.Add(obj->Clone(obj->GetName())) ;
160 }
161 }
163 olist.Delete() ;
164}
165
166
167////////////////////////////////////////////////////////////////////////////////
168
170{
171 for (list<RooAbsStudy*>::iterator iter=_pkg->studies().begin() ; iter!=_pkg->studies().end() ; ++iter) {
172 (*iter)->aggregateSummaryOutput(olist) ;
173 }
174}
175
176
177
178
179////////////////////////////////////////////////////////////////////////////////
180/// case with one single file
181
183{
184 if (!TString(name).MaybeWildcard()) {
185 result.push_back(name) ;
186 return ;
187 }
188
189 // wildcarding used in name
191
192 Int_t dotslashpos = -1;
193 {
194 Int_t next_dot = basename.Index(".root");
195 while(next_dot>=0) {
197 next_dot = basename.Index(".root",dotslashpos+1);
198 }
199 if (basename[dotslashpos+5]!='/') {
200 // We found the 'last' .root in the name and it is not followed by
201 // a '/', so the tree name is _not_ specified in the name.
202 dotslashpos = -1;
203 }
204 }
205 //Int_t dotslashpos = basename.Index(".root/");
207 if (dotslashpos>=0) {
208 // Copy the tree name specification
210 // and remove it from basename
211 basename.Remove(dotslashpos+5);
212 }
213
214 Int_t slashpos = basename.Last('/');
216 if (slashpos>=0) {
217 directory = basename(0,slashpos); // Copy the directory name
218 basename.Remove(0,slashpos+1); // and remove it from basename
219 } else {
221 }
222
225 void *dir = gSystem->OpenDirectory(expand_directory.Data());
226
227 if (dir) {
228 //create a TList to store the file names (not yet sorted)
229 TList l;
230 TRegexp re(basename,true);
231 const char *file;
232 while ((file = gSystem->GetDirEntry(dir))) {
233 if (!strcmp(file,".") || !strcmp(file,"..")) continue;
234 TString s = file;
235 if ( (basename!=file) && s.Index(re) == kNPOS) continue;
236 l.Add(new TObjString(file));
237 }
239 //sort the files in alphanumeric order
240 l.Sort();
241 TIter next(&l);
242 TObjString *obj;
243 while ((obj = static_cast<TObjString*>(next()))) {
244 file = obj->GetName();
245 if (behind_dot_root.Length() != 0) {
246 result.push_back(Form("%s/%s/%s",directory.Data(),file,behind_dot_root.Data())) ;
247 } else {
248 result.push_back(Form("%s/%s", directory.Data(), file));
249 }
250 }
251 l.Delete();
252 }
253}
#define f(i)
Definition RSha256.hxx:104
#define coutI(a)
#define coutP(a)
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:117
#define ClassImp(name)
Definition Rtypes.h:382
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gDirectory
Definition TDirectory.h:384
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:2489
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
const_iterator begin() const
const_iterator end() const
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 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: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:4088
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:456
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition TObject.cxx:241
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:898
virtual void Delete(Option_t *option="")
Delete this object.
Definition TObject.cxx:266
Regular expression class.
Definition TRegexp.h:31
Basic string class.
Definition TString.h:139
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