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