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
46
47
48////////////////////////////////////////////////////////////////////////////////
49
51
52////////////////////////////////////////////////////////////////////////////////
53
59
60
61////////////////////////////////////////////////////////////////////////////////
62
64{
65 string pwd = gDirectory->GetName() ;
66 std::unique_ptr<TFile> f{TFile::Open(studyPackFileName, "READ")};
67 _pkg = dynamic_cast<RooStudyPackage*>(f->Get("studypack")) ;
68 gDirectory->cd(Form("%s:",pwd.c_str())) ;
69}
70
71
72
73////////////////////////////////////////////////////////////////////////////////
74
79
80
81
82
83////////////////////////////////////////////////////////////////////////////////
84
89
90
91////////////////////////////////////////////////////////////////////////////////
92
94{
95 TFile f(Form("study_data_%s.root",studyName),"RECREATE") ;
96 _pkg->Write("studypack") ;
97 f.Close() ;
98
99 if (unifiedInput) {
100
101 // Write header of driver script
102 ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
103 bdr << "#!/bin/sh" << std::endl
104 << Form("if [ ! -f study_data_%s.root ] ; then",studyName) << std::endl
105 << "uudecode <<EOR" << std::endl ;
106 bdr.close() ;
107
108 // Write uuencoded ROOT file (base64) in driver script
109 gSystem->Exec(Form("cat study_data_%s.root | uuencode -m study_data_%s.root >> study_driver_%s.sh",studyName,studyName,studyName)) ;
110
111 // Write remainder of driver script
112 ofstream bdr2 (Form("study_driver_%s.sh",studyName),ios::app) ;
113 bdr2 << "EOR" << std::endl
114 << "fi" << std::endl
115 << "root -l -b <<EOR" << std::endl
116 << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << std::endl
117 << ".q" << std::endl
118 << "EOR" << std::endl ;
119 // Remove binary input file
120 gSystem->Unlink(Form("study_data_%s.root",studyName)) ;
121
122 coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << std::endl
123 << " input data files is embedded in driver script" << std::endl ;
124
125 } else {
126
127 ofstream bdr(Form("study_driver_%s.sh",studyName)) ;
128 bdr << "#!/bin/sh" << std::endl
129 << "root -l -b <<EOR" << std::endl
130 << Form("RooStudyPackage::processFile(\"%s\",%d) ;",studyName,nExpPerJob) << std::endl
131 << ".q" << std::endl
132 << "EOR" << std::endl ;
133
134 coutI(DataHandling) << "RooStudyManager::prepareBatchInput batch driver file is '" << Form("study_driver_%s.sh",studyName) << "," << std::endl
135 << " input data file is " << Form("study_data_%s.root",studyName) << std::endl ;
136
137 }
138}
139
140
141
142
143////////////////////////////////////////////////////////////////////////////////
144
146{
149
150 TList olist ;
151
152 for (list<string>::iterator iter = flist.begin() ; iter!=flist.end() ; ++iter) {
153 coutP(DataHandling) << "RooStudyManager::processBatchOutput() now reading file " << *iter << std::endl ;
154 TFile f(iter->c_str()) ;
155
156 for(auto * key : static_range_cast<TKey*>(*f.GetListOfKeys())) {
157 TObject * obj = f.Get(key->GetName()) ;
158 olist.Add(obj->Clone(obj->GetName())) ;
159 }
160 }
162 olist.Delete() ;
163}
164
165
166////////////////////////////////////////////////////////////////////////////////
167
169{
170 for (list<RooAbsStudy*>::iterator iter=_pkg->studies().begin() ; iter!=_pkg->studies().end() ; ++iter) {
171 (*iter)->aggregateSummaryOutput(olist) ;
172 }
173}
174
175
176
177
178////////////////////////////////////////////////////////////////////////////////
179/// case with one single file
180
182{
183 if (!TString(name).MaybeWildcard()) {
184 result.push_back(name) ;
185 return ;
186 }
187
188 // wildcarding used in name
190
191 Int_t dotslashpos = -1;
192 {
193 Int_t next_dot = basename.Index(".root");
194 while(next_dot>=0) {
196 next_dot = basename.Index(".root",dotslashpos+1);
197 }
198 if (basename[dotslashpos+5]!='/') {
199 // We found the 'last' .root in the name and it is not followed by
200 // a '/', so the tree name is _not_ specified in the name.
201 dotslashpos = -1;
202 }
203 }
204 //Int_t dotslashpos = basename.Index(".root/");
206 if (dotslashpos>=0) {
207 // Copy the tree name specification
209 // and remove it from basename
210 basename.Remove(dotslashpos+5);
211 }
212
213 Int_t slashpos = basename.Last('/');
215 if (slashpos>=0) {
216 directory = basename(0,slashpos); // Copy the directory name
217 basename.Remove(0,slashpos+1); // and remove it from basename
218 } else {
220 }
221
224 void *dir = gSystem->OpenDirectory(expand_directory.Data());
225
226 if (dir) {
227 //create a TList to store the file names (not yet sorted)
228 TList l;
229 TRegexp re(basename,true);
230 const char *file;
231 while ((file = gSystem->GetDirEntry(dir))) {
232 if (!strcmp(file,".") || !strcmp(file,"..")) continue;
233 TString s = file;
234 if ( (basename!=file) && s.Index(re) == kNPOS) continue;
235 l.Add(new TObjString(file));
236 }
238 //sort the files in alphanumeric order
239 l.Sort();
240 TIter next(&l);
241 TObjString *obj;
242 while ((obj = static_cast<TObjString*>(next()))) {
243 file = obj->GetName();
244 if (behind_dot_root.Length() != 0) {
245 result.push_back(Form("%s/%s/%s",directory.Data(),file,behind_dot_root.Data())) ;
246 } else {
247 result.push_back(Form("%s/%s", directory.Data(), file));
248 }
249 }
250 l.Delete();
251 }
252}
#define f(i)
Definition RSha256.hxx:104
#define coutI(a)
#define coutP(a)
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:117
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: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:4116
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: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:1287
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition TSystem.cxx:857
virtual void * OpenDirectory(const char *name)
Open a directory.
Definition TSystem.cxx:848
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:865
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1075
virtual const char * WorkingDirectory()
Return working directory.
Definition TSystem.cxx:883
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1394
TLine l
Definition textangle.C:4