Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
22Utility class to manage studies that consist of
23repeated applications of generate-and-fit operations on a workspace
24
25**/
26
27
28
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 "TFile.h"
37#include "TRandom2.h"
38#include "RooRandom.h"
39#include "TMath.h"
40#include "TEnv.h"
41
42using std::list, std::cout, std::endl, std::string;
43
45
46////////////////////////////////////////////////////////////////////////////////
47
49{
50}
51
52
53
54////////////////////////////////////////////////////////////////////////////////
55
56RooStudyPackage::RooStudyPackage(const RooStudyPackage& other) : TNamed(other), _ws(new RooWorkspace(*other._ws))
57{
58 list<RooAbsStudy*>::const_iterator iter = other._studies.begin() ;
59 for (;iter!=other._studies.end() ; ++iter) {
60 _studies.push_back((*iter)->clone()) ;
61 }
62}
63
64
65
66////////////////////////////////////////////////////////////////////////////////
67
69{
70 _studies.push_back(&study) ;
71}
72
73
74
75////////////////////////////////////////////////////////////////////////////////
76
78{
79 initialize() ;
80 run(nExperiments) ;
81 finalize() ;
82}
83
84
85
86////////////////////////////////////////////////////////////////////////////////
87/// Make iterator over copy of studies attached to workspace
88
90{
91 for (list<RooAbsStudy*>::iterator iter=_studies.begin() ; iter!=_studies.end() ; ++iter) {
92 (*iter)->attach(*_ws) ;
93 (*iter)->initialize() ;
94 }
95
96}
97
98
99////////////////////////////////////////////////////////////////////////////////
100
101void RooStudyPackage::run(Int_t nExperiments)
102{
103 // Run the requested number of experiments
104 Int_t prescale = nExperiments>100 ? Int_t(nExperiments/100) : 1 ;
105 for (Int_t i=0 ; i<nExperiments ; i++) {
106 if (i%prescale==0) {
107 coutP(Generation) << "RooStudyPackage::run(" << GetName() << ") processing experiment " << i << "/" << nExperiments << endl ;
108 }
109 runOne() ;
110 }
111}
112
113
114
115////////////////////////////////////////////////////////////////////////////////
116
118{
119 for (list<RooAbsStudy*>::iterator iter=_studies.begin() ; iter!=_studies.end() ; ++iter) {
120 (*iter)->execute() ;
121 }
122}
123
124
125
126
127////////////////////////////////////////////////////////////////////////////////
128/// Finalize all studies
129
131{
132 for (list<RooAbsStudy*>::iterator iter=_studies.begin() ; iter!=_studies.end() ; ++iter) {
133 (*iter)->finalize() ;
134 }
135}
136
137
138
139
140////////////////////////////////////////////////////////////////////////////////
141
143{
144 for (list<RooAbsStudy*>::iterator iter=_studies.begin() ; iter!=_studies.end() ; ++iter) {
145
146 (*iter)->finalize() ;
147
148 RooDataSet* summaryData = (*iter)->summaryData() ;
149 if (summaryData) {
150 summaryData->SetName(Form("%s_%d",summaryData->GetName(),seqno)) ;
151 cout << "registering summary dataset: " ; summaryData->Print() ;
152 olist->Add(summaryData) ;
153 }
154
155 RooLinkedList* detailedData = (*iter)->detailedData() ;
156 if (detailedData && detailedData->GetSize()>0) {
157
158 detailedData->SetName(Form("%s_%d",detailedData->GetName(),seqno)) ;
159 cout << "registering detailed dataset " << detailedData->ClassName() << "::"
160 << detailedData->GetName() << " with " << detailedData->GetSize() << " elements" << endl ;
161 for(auto dobj : static_range_cast<TNamed*>(*detailedData)) {
162 dobj->SetName(Form("%s_%d",dobj->GetName(),seqno)) ;
163 }
164 olist->Add(detailedData) ;
165 (*iter)->releaseDetailData() ;
166 }
167 }
168}
169
170
171
172////////////////////////////////////////////////////////////////////////////////
173/// Choose random seed for this process
174/// in case pass a definite seed to have it deterministic
175/// use also worker number
176
178{
179 TRandom2 random(0);
180 //gRandom->SetSeed(0) ;
181 Int_t seed = random.Integer(TMath::Limits<Int_t>::Max()) ;
182
183 // get worker number
184 TString worknumber = gEnv->GetValue("ProofServ.Ordinal","undef");
185 int iworker = -1;
186 if (worknumber != "undef")
187 iworker = int( worknumber.Atof()*10 + 0.1);
188
189 if (iworker >= 0) {
190 for (int i = 0; i <= iworker; ++i )
191 seed = random.Integer( TMath::Limits<Int_t>::Max() );
192 }
193
195 gRandom->SetSeed(seed) ;
196
197 return seed ;
198}
199
200
201
202////////////////////////////////////////////////////////////////////////////////
203/// Read in study package
204
205void RooStudyPackage::processFile(const char* studyName, Int_t nexp)
206{
207 string name_fin = Form("study_data_%s.root",studyName) ;
208 TFile fin(name_fin.c_str()) ;
209 RooStudyPackage* pkg = dynamic_cast<RooStudyPackage*>(fin.Get("studypack")) ;
210 if (!pkg) {
211 cout << "RooStudyPackage::processFile() ERROR input file " << name_fin << " does not contain a RooStudyPackage named 'studypack'" << endl ;
212 return ;
213 }
214
215 // Initialize random seed
216 Int_t seqno = pkg->initRandom() ;
217 cout << "RooStudyPackage::processFile() Initial random seed for this run is " << seqno << endl ;
218
219 // Run study
220 pkg->driver(nexp) ;
221
222 // Save result
223 TList res ;
224 pkg->exportData(&res,seqno) ;
225 TFile fout(Form("study_result_%s_%d.root",studyName,seqno),"RECREATE") ;
226 res.Write() ;
227 fout.Close() ;
228}
#define coutP(a)
int Int_t
Definition RtypesCore.h:45
#define ClassImp(name)
Definition Rtypes.h:377
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
void Print(Option_t *options=nullptr) const override
This method must be overridden when a class wants to print itself.
Definition RooAbsData.h:225
Abstract base class for RooStudyManager modules.
Definition RooAbsStudy.h:33
Container class to hold unbinned data.
Definition RooDataSet.h:57
void SetName(const char *name) override
Change the name of this dataset into the given name.
Collection class for internal use, storing a collection of RooAbsArg pointers in a doubly linked list...
Int_t GetSize() const
void SetName(const char *name)
const char * GetName() const override
Returns name of object.
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition RooRandom.cxx:48
Utility class to manage studies that consist of repeated applications of generate-and-fit operations ...
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)
RooStudyPackage()=default
RooWorkspace * _ws
void run(Int_t nExperiments)
Persistable container for RooFit projects.
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override
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 an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
void Close(Option_t *option="") override
Close a file.
Definition TFile.cxx:943
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:83
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:207
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:615
virtual UInt_t Integer(UInt_t imax)
Returns a random integer uniformly distributed on the interval [ 0, imax-1 ].
Definition TRandom.cxx:361
Basic string class.
Definition TString.h:139
Double_t Atof() const
Return floating-point value contained in string.
Definition TString.cxx:2054