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