Logo ROOT  
Reference Guide
RooRandomizeParamMCSModule.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 RooRandomizeParamMCSModule.cxx
19\class RooRandomizeParamMCSModule
20\ingroup Roofitcore
21
22RooRandomizeParamMCSModule is an add-on modules to RooMCStudy that
23allows you to randomize input generation parameters. Randomized generation
24parameters can be sampled from a uniform or Gaussian distribution.
25For every randomized parameter, an extra variable is added to
26RooMCStudy::fitParDataSet() named <tt>`<parname>`_gen</tt> that indicates the actual
27value used for generation for each trial.
28You can also choose to randomize the sum of N parameters, rather
29than a single parameter. In that case common multiplicative scale
30factor is applied to each component to bring the sum to the desired
31target value taken from either uniform or Gaussian sampling. This
32latter option is for example useful if you want to change the total
33number of expected events of an extended p.d.f
34**/
35
36
37#include "Riostream.h"
38#include "RooDataSet.h"
39#include "RooRealVar.h"
40#include "RooRandom.h"
41#include "TString.h"
42#include "RooFitResult.h"
43#include "RooAddition.h"
44#include "RooMsgService.h"
46
47using namespace std ;
48
50 ;
51
52
53
54////////////////////////////////////////////////////////////////////////////////
55/// Constructor
56
58 RooAbsMCStudyModule("RooRandomizeParamMCSModule","RooRandomizeParamMCSModule"), _data(0)
59{
60}
61
62
63
64////////////////////////////////////////////////////////////////////////////////
65/// Copy constructor
66
69 _unifParams(other._unifParams),
70 _gausParams(other._gausParams),
71 _data(0)
72{
73}
74
75
76
77////////////////////////////////////////////////////////////////////////////////
78/// Destructor
79
81{
82 if (_data) {
83 delete _data ;
84 }
85}
86
87
88
89////////////////////////////////////////////////////////////////////////////////
90/// Request uniform smearing of param in range [lo,hi] in RooMCStudy
91/// generation cycle
92
94{
95 // If we're already attached to a RooMCStudy, check that given param is actual generator model parameter
96 // If not attached, this check is repeated at the attachment moment
97 if (genParams()) {
98 RooRealVar* actualPar = static_cast<RooRealVar*>(genParams()->find(param.GetName())) ;
99 if (!actualPar) {
100 oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << param.GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
101 return ;
102 }
103 }
104
105 _unifParams.push_back(UniParam(&param,lo,hi)) ;
106}
107
108
109
110////////////////////////////////////////////////////////////////////////////////
111/// Request Gaussian smearing of param in with mean 'mean' and width
112/// 'sigma' in RooMCStudy generation cycle
113
115{
116 // If we're already attached to a RooMCStudy, check that given param is actual generator model parameter
117 // If not attached, this check is repeated at the attachment moment
118 if (genParams()) {
119 RooRealVar* actualPar = static_cast<RooRealVar*>(genParams()->find(param.GetName())) ;
120 if (!actualPar) {
121 oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << param.GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
122 return ;
123 }
124 }
125
126 _gausParams.push_back(GausParam(&param,mean,sigma)) ;
127}
128
129
130
131
132////////////////////////////////////////////////////////////////////////////////
133/// Request uniform smearing of sum of parameters in paramSet uniform
134/// smearing in range [lo,hi] in RooMCStudy generation cycle. This
135/// option applies a common multiplicative factor to each parameter
136/// in paramSet to make the sum of the parameters add up to the
137/// sampled value in the range [lo,hi]
138
140{
141 // Check that all args are RooRealVars
142 RooArgSet okset ;
143 TIterator* iter = paramSet.createIterator() ;
144 RooAbsArg* arg ;
145 while((arg=(RooAbsArg*)iter->Next())) {
146 // Check that arg is a RooRealVar
147 RooRealVar* rrv = dynamic_cast<RooRealVar*>(arg) ;
148 if (!rrv) {
149 oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::sampleSumUniform() ERROR: input parameter " << arg->GetName() << " is not a RooRealVar and is ignored" << endl ;
150 continue;
151 }
152 okset.add(*rrv) ;
153 }
154 delete iter ;
155
156 // If we're already attached to a RooMCStudy, check that given param is actual generator model parameter
157 // If not attached, this check is repeated at the attachment moment
158 RooArgSet okset2 ;
159 if (genParams()) {
160 TIterator* psiter = okset.createIterator() ;
161 RooAbsArg* arg2 ;
162 while ((arg2=(RooAbsArg*)psiter->Next())) {
163 RooRealVar* actualVar= static_cast<RooRealVar*>(genParams()->find(arg2->GetName())) ;
164 if (!actualVar) {
165 oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::sampleSumUniform: variable " << arg2->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
166 } else {
167 okset2.add(*actualVar) ;
168 }
169 }
170 delete psiter ;
171 } else {
172
173 // If genParams() are not available, skip this check for now
174 okset2.add(okset) ;
175
176 }
177
178
179 _unifParamSets.push_back(UniParamSet(okset2,lo,hi)) ;
180
181}
182
183
184
185
186////////////////////////////////////////////////////////////////////////////////
187/// Request gaussian smearing of sum of parameters in paramSet
188/// uniform smearing with mean 'mean' and width 'sigma' in RooMCStudy
189/// generation cycle. This option applies a common multiplicative
190/// factor to each parameter in paramSet to make the sum of the
191/// parameters add up to the sampled value from the
192/// gaussian(mean,sigma)
193
195{
196 // Check that all args are RooRealVars
197 RooArgSet okset ;
198 TIter iter = paramSet.createIterator() ;
199 RooAbsArg* arg ;
200 while((arg=(RooAbsArg*)iter.Next())) {
201 // Check that arg is a RooRealVar
202 RooRealVar* rrv = dynamic_cast<RooRealVar*>(arg) ;
203 if (!rrv) {
204 oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::sampleSumGauss() ERROR: input parameter " << arg->GetName() << " is not a RooRealVar and is ignored" << endl ;
205 continue;
206 }
207 okset.add(*rrv) ;
208 }
209
210 // If we're already attached to a RooMCStudy, check that given param is actual generator model parameter
211 // If not attached, this check is repeated at the attachment moment
212 RooArgSet okset2 ;
213 if (genParams()) {
214 TIter psiter = okset.createIterator() ;
215 RooAbsArg* arg2 ;
216 while ((arg2=(RooAbsArg*)psiter.Next())) {
217 RooRealVar* actualVar= static_cast<RooRealVar*>(genParams()->find(arg2->GetName())) ;
218 if (!actualVar) {
219 oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::sampleSumUniform: variable " << arg2->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
220 } else {
221 okset2.add(*actualVar) ;
222 }
223 }
224 } else {
225
226 // If genParams() are not available, skip this check for now
227 okset2.add(okset) ;
228
229 }
230
231 _gausParamSets.push_back(GausParamSet(okset,mean,sigma)) ;
232
233}
234
235
236
237
238////////////////////////////////////////////////////////////////////////////////
239/// Initialize module after attachment to RooMCStudy object
240
242{
243 // Loop over all uniform smearing parameters
244 std::list<UniParam>::iterator uiter ;
245 for (uiter= _unifParams.begin() ; uiter!= _unifParams.end() ; ++uiter) {
246
247 // Check that listed variable is actual generator model parameter
248 RooRealVar* actualPar = static_cast<RooRealVar*>(genParams()->find(uiter->_param->GetName())) ;
249 if (!actualPar) {
250 oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << uiter->_param->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
251 uiter = _unifParams.erase(uiter) ;
252 continue ;
253 }
254 uiter->_param = actualPar ;
255
256 // Add variable to summary dataset to hold generator value
257 TString parName = Form("%s_gen",uiter->_param->GetName()) ;
258 TString parTitle = Form("%s as generated",uiter->_param->GetTitle()) ;
259 RooRealVar* par_gen = new RooRealVar(parName.Data(),parTitle.Data(),0) ;
260 _genParSet.addOwned(*par_gen) ;
261 }
262
263 // Loop over all gaussian smearing parameters
264 std::list<GausParam>::iterator giter ;
265 for (giter= _gausParams.begin() ; giter!= _gausParams.end() ; ++giter) {
266
267 // Check that listed variable is actual generator model parameter
268 RooRealVar* actualPar = static_cast<RooRealVar*>(genParams()->find(giter->_param->GetName())) ;
269 if (!actualPar) {
270 oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << giter->_param->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
271 giter = _gausParams.erase(giter) ;
272 continue ;
273 }
274 giter->_param = actualPar ;
275
276 // Add variable to summary dataset to hold generator value
277 TString parName = Form("%s_gen",giter->_param->GetName()) ;
278 TString parTitle = Form("%s as generated",giter->_param->GetTitle()) ;
279 RooRealVar* par_gen = new RooRealVar(parName.Data(),parTitle.Data(),0) ;
280 _genParSet.addOwned(*par_gen) ;
281 }
282
283
284 // Loop over all uniform smearing set of parameters
285 std::list<UniParamSet>::iterator usiter ;
286 for (usiter= _unifParamSets.begin() ; usiter!= _unifParamSets.end() ; ++usiter) {
287
288 // Check that all listed variables are actual generator model parameters
289 RooArgSet actualPSet ;
290 TIterator* psiter = usiter->_pset.createIterator() ;
291 RooAbsArg* arg ;
292 while ((arg=(RooAbsArg*)psiter->Next())) {
293 RooRealVar* actualVar= static_cast<RooRealVar*>(genParams()->find(arg->GetName())) ;
294 if (!actualVar) {
295 oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << arg->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
296 } else {
297 actualPSet.add(*actualVar) ;
298 }
299 }
300 delete psiter ;
301 usiter->_pset.removeAll() ;
302 usiter->_pset.add(actualPSet) ;
303
304 // Add variables to summary dataset to hold generator values
305 TIterator* iter = usiter->_pset.createIterator() ;
306 RooRealVar* param ;
307 while((param=(RooRealVar*)iter->Next())) {
308 TString parName = Form("%s_gen",param->GetName()) ;
309 TString parTitle = Form("%s as generated",param->GetTitle()) ;
310 RooRealVar* par_gen = new RooRealVar(parName.Data(),parTitle.Data(),0) ;
311 _genParSet.addOwned(*par_gen) ;
312 }
313 delete iter ;
314 }
315
316 // Loop over all gaussian smearing set of parameters
317 std::list<GausParamSet>::iterator ugiter ;
318 for (ugiter= _gausParamSets.begin() ; ugiter!= _gausParamSets.end() ; ++ugiter) {
319
320 // Check that all listed variables are actual generator model parameters
321 RooArgSet actualPSet ;
322 TIter psiter = ugiter->_pset.createIterator() ;
323 RooAbsArg* arg ;
324 while ((arg=(RooAbsArg*)psiter.Next())) {
325 RooRealVar* actualVar= static_cast<RooRealVar*>(genParams()->find(arg->GetName())) ;
326 if (!actualVar) {
327 oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << arg->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
328 } else {
329 actualPSet.add(*actualVar) ;
330 }
331 }
332
333 ugiter->_pset.removeAll() ;
334 ugiter->_pset.add(actualPSet) ;
335
336 // Add variables to summary dataset to hold generator values
337 TIter iter = ugiter->_pset.createIterator() ;
338 RooRealVar* param ;
339 while((param=(RooRealVar*)iter.Next())) {
340 TString parName = Form("%s_gen",param->GetName()) ;
341 TString parTitle = Form("%s as generated",param->GetTitle()) ;
342 RooRealVar* par_gen = new RooRealVar(parName.Data(),parTitle.Data(),0) ;
343 _genParSet.addOwned(*par_gen) ;
344 }
345 }
346
347 // Create new dataset to be merged with RooMCStudy::fitParDataSet
348 _data = new RooDataSet("DeltaLLSigData","Additional data for Delta(-log(L)) study",_genParSet) ;
349
350 return kTRUE ;
351}
352
353
354
355////////////////////////////////////////////////////////////////////////////////
356/// Initialize module at beginning of RooCMStudy run
357
359{
360 // Clear dataset at beginning of run
361 _data->reset() ;
362 return kTRUE ;
363}
364
365
366
367////////////////////////////////////////////////////////////////////////////////
368/// Apply all smearings to generator parameters
369
371{
372 // Apply uniform smearing to all generator parameters for which it is requested
373 std::list<UniParam>::iterator uiter ;
374 for (uiter= _unifParams.begin() ; uiter!= _unifParams.end() ; ++uiter) {
375 Double_t newVal = RooRandom::randomGenerator()->Uniform(uiter->_lo,uiter->_hi) ;
376 oocoutE((TObject*)0,Generation) << "RooRandomizeParamMCSModule::processBeforeGen: applying uniform smearing to generator parameter "
377 << uiter->_param->GetName() << " in range [" << uiter->_lo << "," << uiter->_hi << "], chosen value for this sample is " << newVal << endl ;
378 uiter->_param->setVal(newVal) ;
379
380 RooRealVar* genpar = static_cast<RooRealVar*>(_genParSet.find(Form("%s_gen",uiter->_param->GetName()))) ;
381 genpar->setVal(newVal) ;
382 }
383
384 // Apply gaussian smearing to all generator parameters for which it is requested
385 std::list<GausParam>::iterator giter ;
386 for (giter= _gausParams.begin() ; giter!= _gausParams.end() ; ++giter) {
387 Double_t newVal = RooRandom::randomGenerator()->Gaus(giter->_mean,giter->_sigma) ;
388 oocoutI((TObject*)0,Generation) << "RooRandomizeParamMCSModule::processBeforeGen: applying gaussian smearing to generator parameter "
389 << giter->_param->GetName() << " with a mean of " << giter->_mean << " and a width of " << giter->_sigma << ", chosen value for this sample is " << newVal << endl ;
390 giter->_param->setVal(newVal) ;
391
392 RooRealVar* genpar = static_cast<RooRealVar*>(_genParSet.find(Form("%s_gen",giter->_param->GetName()))) ;
393 genpar->setVal(newVal) ;
394 }
395
396 // Apply uniform smearing to all sets of generator parameters for which it is requested
397 std::list<UniParamSet>::iterator usiter ;
398 for (usiter= _unifParamSets.begin() ; usiter!= _unifParamSets.end() ; ++usiter) {
399
400 // Calculate new value for sum
401 Double_t newVal = RooRandom::randomGenerator()->Uniform(usiter->_lo,usiter->_hi) ;
402 oocoutI((TObject*)0,Generation) << "RooRandomizeParamMCSModule::processBeforeGen: applying uniform smearing to sum of set of generator parameters "
403 << usiter->_pset
404 << " in range [" << usiter->_lo << "," << usiter->_hi << "], chosen sum value for this sample is " << newVal << endl ;
405
406 // Determine original value of sum and calculate per-component scale factor to obtain new valye for sum
407 RooAddition sumVal("sumVal","sumVal",usiter->_pset) ;
408 Double_t compScaleFactor = newVal/sumVal.getVal() ;
409
410 // Apply multiplicative correction to each term of the sum
411 TIterator* iter = usiter->_pset.createIterator() ;
412 RooRealVar* param ;
413 while((param=(RooRealVar*)iter->Next())) {
414 param->setVal(param->getVal()*compScaleFactor) ;
415 RooRealVar* genpar = static_cast<RooRealVar*>(_genParSet.find(Form("%s_gen",param->GetName()))) ;
416 genpar->setVal(param->getVal()) ;
417 }
418 delete iter ;
419 }
420
421 // Apply gaussian smearing to all sets of generator parameters for which it is requested
422 std::list<GausParamSet>::iterator gsiter ;
423 for (gsiter= _gausParamSets.begin() ; gsiter!= _gausParamSets.end() ; ++gsiter) {
424
425 // Calculate new value for sum
426 Double_t newVal = RooRandom::randomGenerator()->Gaus(gsiter->_mean,gsiter->_sigma) ;
427 oocoutI((TObject*)0,Generation) << "RooRandomizeParamMCSModule::processBeforeGen: applying gaussian smearing to sum of set of generator parameters "
428 << gsiter->_pset
429 << " with a mean of " << gsiter->_mean << " and a width of " << gsiter->_sigma
430 << ", chosen value for this sample is " << newVal << endl ;
431
432 // Determine original value of sum and calculate per-component scale factor to obtain new valye for sum
433 RooAddition sumVal("sumVal","sumVal",gsiter->_pset) ;
434 Double_t compScaleFactor = newVal/sumVal.getVal() ;
435
436 // Apply multiplicative correction to each term of the sum
437 TIter iter = gsiter->_pset.createIterator() ;
438 RooRealVar* param ;
439 while((param=(RooRealVar*)iter.Next())) {
440 param->setVal(param->getVal()*compScaleFactor) ;
441 RooRealVar* genpar = static_cast<RooRealVar*>(_genParSet.find(Form("%s_gen",param->GetName()))) ;
442 genpar->setVal(param->getVal()) ;
443 }
444 }
445
446 // Store generator values for all modified parameters
448
449 return kTRUE ;
450}
451
452
453
454////////////////////////////////////////////////////////////////////////////////
455/// Return auxiliary data of this module so that it is merged with
456/// RooMCStudy::fitParDataSet()
457
459{
460 return _data ;
461}
462
463
#define oocoutW(o, a)
Definition: RooMsgService.h:47
#define oocoutE(o, a)
Definition: RooMsgService.h:48
#define oocoutI(o, a)
Definition: RooMsgService.h:45
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassImp(name)
Definition: Rtypes.h:375
#define hi
Definition: THbookFile.cxx:128
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2447
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:78
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
virtual Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE)
Add an argument and transfer the ownership to the collection.
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
RooAbsArg * find(const char *name) const
Find object with given name in list.
virtual void reset()
Definition: RooAbsData.cxx:388
RooAbsMCStudyModule is a base class for add-on modules to RooMCStudy that can perform additional calc...
RooArgSet * genParams()
Return current value of generator model parameters.
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:93
RooAddition calculates the sum of a set of RooAbsReal terms, or when constructed with two sets,...
Definition: RooAddition.h:27
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:57
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:55
void add(const RooArgSet &row, Double_t weight=1.0, Double_t weightError=0) override
Add one ore more rows of data.
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition: RooRandom.cxx:51
RooRandomizeParamMCSModule is an add-on modules to RooMCStudy that allows you to randomize input gene...
void sampleUniform(RooRealVar &param, Double_t lo, Double_t hi)
Request uniform smearing of param in range [lo,hi] in RooMCStudy generation cycle.
std::list< GausParam > _gausParams
!
void sampleSumGauss(const RooArgSet &paramSet, Double_t lo, Double_t hi)
Request gaussian smearing of sum of parameters in paramSet uniform smearing with mean 'mean' and widt...
void sampleGaussian(RooRealVar &param, Double_t mean, Double_t sigma)
Request Gaussian smearing of param in with mean 'mean' and width 'sigma' in RooMCStudy generation cyc...
std::list< UniParamSet > _unifParamSets
!
Bool_t initializeInstance() override
Initialize module after attachment to RooMCStudy object.
~RooRandomizeParamMCSModule() override
Destructor.
Bool_t initializeRun(Int_t) override
Initialize module at beginning of RooCMStudy run.
Bool_t processBeforeGen(Int_t) override
Apply all smearings to generator parameters.
void sampleSumUniform(const RooArgSet &paramSet, Double_t lo, Double_t hi)
Request uniform smearing of sum of parameters in paramSet uniform smearing in range [lo,...
RooDataSet * finalizeRun() override
Return auxiliary data of this module so that it is merged with RooMCStudy::fitParDataSet()
std::list< GausParamSet > _gausParamSets
!
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
void setVal(Double_t value) override
Set value of variable to 'value'.
Definition: RooRealVar.cxx:281
TObject * Next()
Definition: TCollection.h:251
Iterator abstract base class.
Definition: TIterator.h:30
virtual TObject * Next()=0
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition: TNamed.h:48
Mother of all ROOT objects.
Definition: TObject.h:37
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:274
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:672
Basic string class.
Definition: TString.h:136
const char * Data() const
Definition: TString.h:369
const Double_t sigma
@ Generation
Definition: RooGlobalFunc.h:63
@ InputArguments
Definition: RooGlobalFunc.h:64