ROOT  6.06/09
Reference Guide
RooStudyPackage.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 //
19 // BEGIN_HTML
20 // RooStudyPackage is a utility class to manage studies that consist of
21 // repeated applications of generate-and-fit operations on a workspace
22 //
23 // END_HTML
24 //
25 
26 
27 
28 #include "RooFit.h"
29 #include "Riostream.h"
30 
31 #include "RooStudyPackage.h"
32 #include "RooWorkspace.h"
33 #include "RooAbsStudy.h"
34 #include "RooDataSet.h"
35 #include "RooMsgService.h"
36 #include "TProof.h"
37 #include "TTree.h"
38 #include "TDSet.h"
39 #include "TFile.h"
40 #include "TRandom2.h"
41 #include "RooRandom.h"
42 #include "TMath.h"
43 #include "TEnv.h"
44 
45 using namespace std ;
46 
48  ;
49 
50 
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 
55 {
56 }
57 
58 
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 
63 {
64 }
65 
66 
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 
70 RooStudyPackage::RooStudyPackage(const RooStudyPackage& other) : TNamed(other), _ws(new RooWorkspace(*other._ws))
71 {
72  list<RooAbsStudy*>::const_iterator iter = other._studies.begin() ;
73  for (;iter!=other._studies.end() ; ++iter) {
74  _studies.push_back((*iter)->clone()) ;
75  }
76 }
77 
78 
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 
83 {
84  _studies.push_back(&study) ;
85 }
86 
87 
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 
91 void RooStudyPackage::driver(Int_t nExperiments)
92 {
93  initialize() ;
94  run(nExperiments) ;
95  finalize() ;
96 }
97 
98 
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Make iterator over copy of studies attached to workspace
102 
104 {
105  for (list<RooAbsStudy*>::iterator iter=_studies.begin() ; iter!=_studies.end() ; iter++) {
106  (*iter)->attach(*_ws) ;
107  (*iter)->initialize() ;
108  }
109 
110 }
111 
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 
115 void RooStudyPackage::run(Int_t nExperiments)
116 {
117  // Run the requested number of experiments
118  Int_t prescale = nExperiments>100 ? Int_t(nExperiments/100) : 1 ;
119  for (Int_t i=0 ; i<nExperiments ; i++) {
120  if (i%prescale==0) {
121  coutP(Generation) << "RooStudyPackage::run(" << GetName() << ") processing experiment " << i << "/" << nExperiments << endl ;
122  }
123  runOne() ;
124  }
125 }
126 
127 
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 
132 {
133  for (list<RooAbsStudy*>::iterator iter=_studies.begin() ; iter!=_studies.end() ; iter++) {
134  (*iter)->execute() ;
135  }
136 }
137 
138 
139 
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// Finalize all studies
143 
145 {
146  for (list<RooAbsStudy*>::iterator iter=_studies.begin() ; iter!=_studies.end() ; iter++) {
147  (*iter)->finalize() ;
148  }
149 }
150 
151 
152 
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 
157 {
158  for (list<RooAbsStudy*>::iterator iter=_studies.begin() ; iter!=_studies.end() ; iter++) {
159 
160  (*iter)->finalize() ;
161 
162  RooDataSet* summaryData = (*iter)->summaryData() ;
163  if (summaryData) {
164  summaryData->SetName(Form("%s_%d",summaryData->GetName(),seqno)) ;
165  cout << "registering summary dataset: " ; summaryData->Print() ;
166  olist->Add(summaryData) ;
167  }
168 
169  RooLinkedList* detailedData = (*iter)->detailedData() ;
170  if (detailedData && detailedData->GetSize()>0) {
171 
172  detailedData->SetName(Form("%s_%d",detailedData->GetName(),seqno)) ;
173  cout << "registering detailed dataset " << detailedData->IsA()->GetName() << "::"
174  << detailedData->GetName() << " with " << detailedData->GetSize() << " elements" << endl ;
175  TIterator* diter = detailedData->MakeIterator() ;
176  TNamed* dobj ;
177  while((dobj=(TNamed*)diter->Next())) {
178  dobj->SetName(Form("%s_%d",dobj->GetName(),seqno)) ;
179  }
180  delete diter ;
181  olist->Add(detailedData) ;
182  (*iter)->releaseDetailData() ;
183  }
184  }
185 }
186 
187 
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// Choose random seed for this process
191 /// in case pass a definite seed to have it deterministic
192 /// use also worker number
193 
195 {
196  TRandom2 random(0);
197  //gRandom->SetSeed(0) ;
198  Int_t seed = random.Integer(TMath::Limits<Int_t>::Max()) ;
199 
200  // get worker number
201  TString worknumber = gEnv->GetValue("ProofServ.Ordinal","undef");
202  int iworker = -1;
203  if (worknumber != "undef")
204  iworker = int( worknumber.Atof()*10 + 0.1);
205 
206  if (iworker >= 0) {
207  for (int i = 0; i <= iworker; ++i )
208  seed = random.Integer( TMath::Limits<Int_t>::Max() );
209  }
210 
212  gRandom->SetSeed(seed) ;
213 
214  return seed ;
215 }
216 
217 
218 
219 ////////////////////////////////////////////////////////////////////////////////
220 /// Read in study package
221 
222 void RooStudyPackage::processFile(const char* studyName, Int_t nexp)
223 {
224  string name_fin = Form("study_data_%s.root",studyName) ;
225  TFile fin(name_fin.c_str()) ;
226  RooStudyPackage* pkg = dynamic_cast<RooStudyPackage*>(fin.Get("studypack")) ;
227  if (!pkg) {
228  cout << "RooStudyPackage::processFile() ERROR input file " << name_fin << " does not contain a RooStudyPackage named 'studypack'" << endl ;
229  return ;
230  }
231 
232  // Initialize random seed
233  Int_t seqno = pkg->initRandom() ;
234  cout << "RooStudyPackage::processFile() Initial random seed for this run is " << seqno << endl ;
235 
236  // Run study
237  pkg->driver(nexp) ;
238 
239  // Save result
240  TList res ;
241  pkg->exportData(&res,seqno) ;
242  TFile fout(Form("study_result_%s_%d.root",studyName,seqno),"RECREATE") ;
243  res.Write() ;
244  fout.Close() ;
245 }
const char * GetName() const
Returns name of object.
Definition: RooLinkedList.h:87
void SetName(const char *name)
Change the name of this dataset into the given name.
void run(Int_t nExperiments)
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition: TRandom2.h:29
void addStudy(RooAbsStudy &study)
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:157
Double_t Atof() const
Return floating-point value contained in string.
Definition: TString.cxx:2030
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
void finalize()
Finalize all studies.
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
STL namespace.
virtual void SetSeed(UInt_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
void SetName(const char *name)
Definition: RooLinkedList.h:88
Iterator abstract base class.
Definition: TIterator.h:32
#define coutP(a)
Definition: RooMsgService.h:33
void driver(Int_t nExperiments)
RooWorkspace * _ws
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
virtual UInt_t Integer(UInt_t imax)
Returns a random integer on [ 0, imax-1 ].
Definition: TRandom.cxx:320
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition: RooRandom.cxx:53
TIterator * MakeIterator(Bool_t dir=kTRUE) const
Return an iterator over this list.
void exportData(TList *olist, Int_t seqno)
A doubly linked list.
Definition: TList.h:47
return
Definition: TBase64.cxx:62
ClassImp(RooStudyPackage)
virtual Int_t GetValue(const char *name, Int_t dflt)
Returns the integer value for a resource.
Definition: TEnv.cxx:494
char * Form(const char *fmt,...)
std::list< RooAbsStudy * > _studies
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
void initialize()
Make iterator over copy of studies attached to workspace.
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
R__EXTERN TEnv * gEnv
Definition: TEnv.h:174
static void processFile(const char *infile, Int_t nexp)
Read in study package.
virtual void Add(TObject *obj)
Definition: TList.h:81
virtual TObject * Next()=0
Int_t initRandom()
Choose random seed for this process in case pass a definite seed to have it deterministic use also wo...
Int_t GetSize() const
Definition: RooLinkedList.h:60
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write all objects in this collection.
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:898