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