RooRandomizeParamMCSModule.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooRandomizeParamMCSModule.cxx 34138 2010-06-25 21:03:15Z wouter $
00005  * Authors:                                                                  *
00006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
00007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
00008  *                                                                           *
00009  * Copyright (c) 2000-2005, Regents of the University of California          *
00010  *                          and Stanford University. All rights reserved.    *
00011  *                                                                           *
00012  * Redistribution and use in source and binary forms,                        *
00013  * with or without modification, are permitted according to the terms        *
00014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
00015  *****************************************************************************/
00016 
00017 //////////////////////////////////////////////////////////////////////////////
00018 //
00019 // BEGIN_HTML
00020 // RooRandomizeParamMCSModule is an add-on modules to RooMCStudy that
00021 // allows you to randomize input generation parameters. Randomized generation
00022 // parameters can be sampled from a uniform or Gaussian distribution.
00023 // For every randomized parameter, an extra variable is added to 
00024 // RooMCStudy::fitParDataSet() named <tt><parname>_gen</tt> that indicates the actual
00025 // value used for generation for each trial. 
00026 // <p>
00027 // You can also choose to randomize the sum of N parameters, rather
00028 // than a single parameter. In that case common multiplicative scale
00029 // factor is applied to each component to bring the sum to the desired
00030 // target value taken from either uniform or Gaussian sampling. This
00031 // latter option is for example useful if you want to change the total
00032 // number of expected events of an extended p.d.f
00033 // END_HTML
00034 //
00035 
00036 
00037 #include "Riostream.h"
00038 #include "RooDataSet.h"
00039 #include "RooRealVar.h"
00040 #include "RooRandom.h"
00041 #include "TString.h"
00042 #include "RooFit.h"
00043 #include "RooFitResult.h"
00044 #include "RooAddition.h"
00045 #include "RooMsgService.h"
00046 #include "RooRandomizeParamMCSModule.h"
00047 
00048 using namespace std ;
00049 
00050 ClassImp(RooRandomizeParamMCSModule)
00051   ;
00052 
00053 
00054 
00055 //_____________________________________________________________________________
00056 RooRandomizeParamMCSModule::RooRandomizeParamMCSModule() : 
00057   RooAbsMCStudyModule("RooRandomizeParamMCSModule","RooRandomizeParamMCSModule"), _data(0)
00058 {
00059   // Constructor
00060 }
00061 
00062 
00063 
00064 //_____________________________________________________________________________
00065 RooRandomizeParamMCSModule::RooRandomizeParamMCSModule(const RooRandomizeParamMCSModule& other) : 
00066   RooAbsMCStudyModule(other), 
00067   _unifParams(other._unifParams),
00068   _gausParams(other._gausParams), 
00069   _data(0)
00070 {
00071   // Copy constructor
00072 }
00073 
00074 
00075 
00076 //_____________________________________________________________________________
00077 RooRandomizeParamMCSModule:: ~RooRandomizeParamMCSModule() 
00078 {
00079   // Destructor
00080 
00081   if (_data) {
00082     delete _data ;
00083   }
00084 }
00085 
00086 
00087 
00088 //_____________________________________________________________________________
00089 void RooRandomizeParamMCSModule::sampleUniform(RooRealVar& param, Double_t lo, Double_t hi) 
00090 {  
00091   // Request uniform smearing of param in range [lo,hi] in RooMCStudy
00092   // generation cycle
00093 
00094   // If we're already attached to a RooMCStudy, check that given param is actual generator model parameter
00095   // If not attached, this check is repeated at the attachment moment
00096   if (genParams()) {
00097     RooRealVar* actualPar = static_cast<RooRealVar*>(genParams()->find(param.GetName())) ;
00098     if (!actualPar) {
00099       oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << param.GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
00100       return ;
00101     }
00102   }
00103 
00104   _unifParams.push_back(UniParam(&param,lo,hi)) ;
00105 }
00106 
00107 
00108 
00109 //_____________________________________________________________________________
00110 void RooRandomizeParamMCSModule::sampleGaussian(RooRealVar& param, Double_t mean, Double_t sigma) 
00111 {
00112   // Request Gaussian smearing of param in with mean 'mean' and width
00113   // 'sigma' in RooMCStudy generation cycle
00114 
00115   // If we're already attached to a RooMCStudy, check that given param is actual generator model parameter
00116   // If not attached, this check is repeated at the attachment moment
00117   if (genParams()) {
00118     RooRealVar* actualPar = static_cast<RooRealVar*>(genParams()->find(param.GetName())) ;
00119     if (!actualPar) {
00120       oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << param.GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
00121       return ;
00122     }
00123   }
00124 
00125   _gausParams.push_back(GausParam(&param,mean,sigma)) ;
00126 }
00127 
00128 
00129 
00130 
00131 //_____________________________________________________________________________
00132 void RooRandomizeParamMCSModule::sampleSumUniform(const RooArgSet& paramSet, Double_t lo, Double_t hi) 
00133 {
00134   // Request uniform smearing of sum of parameters in paramSet uniform
00135   // smearing in range [lo,hi] in RooMCStudy generation cycle.  This
00136   // option applies a common multiplicative factor to each parameter
00137   // in paramSet to make the sum of the parameters add up to the
00138   // sampled value in the range [lo,hi]
00139 
00140   // Check that all args are RooRealVars
00141   RooArgSet okset ;
00142   TIterator* iter = paramSet.createIterator() ;
00143   RooAbsArg* arg ;
00144   while((arg=(RooAbsArg*)iter->Next())) {
00145     // Check that arg is a RooRealVar
00146     RooRealVar* rrv = dynamic_cast<RooRealVar*>(arg) ;
00147     if (!rrv) {
00148       oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::sampleSumUniform() ERROR: input parameter " << arg->GetName() << " is not a RooRealVar and is ignored" << endl ;
00149       continue;
00150     }
00151     okset.add(*rrv) ;
00152   }
00153   delete iter ;
00154 
00155   // If we're already attached to a RooMCStudy, check that given param is actual generator model parameter
00156   // If not attached, this check is repeated at the attachment moment
00157   RooArgSet okset2 ;
00158   if (genParams()) {
00159     TIterator* psiter = okset.createIterator() ;
00160     RooAbsArg* arg2 ;
00161     while ((arg2=(RooAbsArg*)psiter->Next())) {
00162       RooRealVar* actualVar= static_cast<RooRealVar*>(genParams()->find(arg2->GetName())) ;
00163       if (!actualVar) {
00164         oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::sampleSumUniform: variable " << arg2->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;      
00165       } else {
00166         okset2.add(*actualVar) ;
00167       }
00168     }    
00169     delete psiter ;
00170   } else {
00171 
00172    // If genParams() are not available, skip this check for now
00173    okset2.add(okset) ;
00174 
00175   }
00176 
00177 
00178   _unifParamSets.push_back(UniParamSet(okset2,lo,hi)) ;
00179 
00180 }
00181 
00182 
00183 
00184 
00185 //_____________________________________________________________________________
00186 void RooRandomizeParamMCSModule::sampleSumGauss(const RooArgSet& paramSet, Double_t mean, Double_t sigma) 
00187 {
00188   // Request gaussian smearing of sum of parameters in paramSet
00189   // uniform smearing with mean 'mean' and width 'sigma' in RooMCStudy
00190   // generation cycle.  This option applies a common multiplicative
00191   // factor to each parameter in paramSet to make the sum of the
00192   // parameters add up to the sampled value from the
00193   // gaussian(mean,sigma)
00194 
00195   // Check that all args are RooRealVars
00196   RooArgSet okset ;
00197   TIterator* iter = paramSet.createIterator() ;
00198   RooAbsArg* arg ;
00199   while((arg=(RooAbsArg*)iter->Next())) {
00200     // Check that arg is a RooRealVar
00201     RooRealVar* rrv = dynamic_cast<RooRealVar*>(arg) ;
00202     if (!rrv) {
00203       oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::sampleSumGauss() ERROR: input parameter " << arg->GetName() << " is not a RooRealVar and is ignored" << endl ;
00204       continue;
00205     }
00206     okset.add(*rrv) ;
00207   }
00208 
00209   // If we're already attached to a RooMCStudy, check that given param is actual generator model parameter
00210   // If not attached, this check is repeated at the attachment moment
00211   RooArgSet okset2 ;
00212   if (genParams()) {
00213     TIterator* psiter = okset.createIterator() ;
00214     RooAbsArg* arg2 ;
00215     while ((arg2=(RooAbsArg*)psiter->Next())) {
00216       RooRealVar* actualVar= static_cast<RooRealVar*>(genParams()->find(arg2->GetName())) ;
00217       if (!actualVar) {
00218         oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::sampleSumUniform: variable " << arg2->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;      
00219       } else {
00220         okset2.add(*actualVar) ;
00221       }
00222     }    
00223     delete psiter ;
00224   } else {
00225 
00226    // If genParams() are not available, skip this check for now
00227    okset2.add(okset) ;
00228 
00229   }
00230 
00231   _gausParamSets.push_back(GausParamSet(okset,mean,sigma)) ;
00232   
00233 }
00234 
00235 
00236 
00237 
00238 //_____________________________________________________________________________
00239 Bool_t RooRandomizeParamMCSModule::initializeInstance()
00240 {
00241   // Initialize module after attachment to RooMCStudy object
00242 
00243   // Loop over all uniform smearing parameters
00244   std::list<UniParam>::iterator uiter ;
00245   for (uiter= _unifParams.begin() ; uiter!= _unifParams.end() ; ++uiter) {
00246 
00247     // Check that listed variable is actual generator model parameter
00248     RooRealVar* actualPar = static_cast<RooRealVar*>(genParams()->find(uiter->_param->GetName())) ;
00249     if (!actualPar) {
00250       oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << uiter->_param->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
00251       uiter = _unifParams.erase(uiter) ;
00252       continue ;
00253     }
00254     uiter->_param = actualPar ;
00255 
00256     // Add variable to summary dataset to hold generator value
00257     TString parName = Form("%s_gen",uiter->_param->GetName()) ;
00258     TString parTitle = Form("%s as generated",uiter->_param->GetTitle()) ;
00259     RooRealVar* par_gen = new RooRealVar(parName.Data(),parTitle.Data(),0) ;    
00260     _genParSet.addOwned(*par_gen) ;
00261   }
00262   
00263   // Loop over all gaussian smearing parameters
00264   std::list<UniParam>::iterator giter ;
00265   for (giter= _unifParams.begin() ; giter!= _unifParams.end() ; ++giter) {
00266 
00267     // Check that listed variable is actual generator model parameter
00268     RooRealVar* actualPar = static_cast<RooRealVar*>(genParams()->find(giter->_param->GetName())) ;
00269     if (!actualPar) {
00270       oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << giter->_param->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
00271       giter = _unifParams.erase(giter) ;
00272       continue ;
00273     }
00274     giter->_param = actualPar ;
00275 
00276     // Add variable to summary dataset to hold generator value
00277     TString parName = Form("%s_gen",giter->_param->GetName()) ;
00278     TString parTitle = Form("%s as generated",giter->_param->GetTitle()) ;
00279     RooRealVar* par_gen = new RooRealVar(parName.Data(),parTitle.Data(),0) ;    
00280     _genParSet.addOwned(*par_gen) ;
00281   }
00282 
00283 
00284   // Loop over all uniform smearing set of parameters
00285   std::list<UniParamSet>::iterator usiter ;
00286   for (usiter= _unifParamSets.begin() ; usiter!= _unifParamSets.end() ; ++usiter) {
00287     
00288     // Check that all listed variables are actual generator model parameters
00289     RooArgSet actualPSet ;
00290     TIterator* psiter = usiter->_pset.createIterator() ;
00291     RooAbsArg* arg ;
00292     while ((arg=(RooAbsArg*)psiter->Next())) {
00293       RooRealVar* actualVar= static_cast<RooRealVar*>(genParams()->find(arg->GetName())) ;
00294       if (!actualVar) {
00295         oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << arg->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;     
00296       } else {
00297         actualPSet.add(*actualVar) ;
00298       }
00299     }    
00300     delete psiter ;
00301     usiter->_pset.removeAll() ;
00302     usiter->_pset.add(actualPSet) ;
00303 
00304     // Add variables to summary dataset to hold generator values
00305     TIterator* iter = usiter->_pset.createIterator() ;
00306     RooRealVar* param ;
00307     while((param=(RooRealVar*)iter->Next())) {
00308       TString parName = Form("%s_gen",param->GetName()) ;
00309       TString parTitle = Form("%s as generated",param->GetTitle()) ;
00310       RooRealVar* par_gen = new RooRealVar(parName.Data(),parTitle.Data(),0) ;    
00311       _genParSet.addOwned(*par_gen) ;          
00312     }
00313     delete iter ;
00314   }
00315   
00316   // Loop over all gaussian smearing set of parameters
00317   std::list<GausParamSet>::iterator ugiter ;
00318   for (ugiter= _gausParamSets.begin() ; ugiter!= _gausParamSets.end() ; ++ugiter) {
00319 
00320     // Check that all listed variables are actual generator model parameters
00321     RooArgSet actualPSet ;
00322     TIterator* psiter = ugiter->_pset.createIterator() ;
00323     RooAbsArg* arg ;
00324     while ((arg=(RooAbsArg*)psiter->Next())) {
00325       RooRealVar* actualVar= static_cast<RooRealVar*>(genParams()->find(arg->GetName())) ;
00326       if (!actualVar) {
00327         oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << arg->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;     
00328       } else {
00329         actualPSet.add(*actualVar) ;
00330       }
00331     }    
00332     ugiter->_pset.removeAll() ;
00333     ugiter->_pset.add(actualPSet) ;
00334 
00335     // Add variables to summary dataset to hold generator values
00336     TIterator* iter = ugiter->_pset.createIterator() ;
00337     RooRealVar* param ;
00338     while((param=(RooRealVar*)iter->Next())) {
00339       TString parName = Form("%s_gen",param->GetName()) ;
00340       TString parTitle = Form("%s as generated",param->GetTitle()) ;
00341       RooRealVar* par_gen = new RooRealVar(parName.Data(),parTitle.Data(),0) ;    
00342       _genParSet.addOwned(*par_gen) ;          
00343     }
00344   }
00345   
00346   // Create new dataset to be merged with RooMCStudy::fitParDataSet
00347   _data = new RooDataSet("DeltaLLSigData","Additional data for Delta(-log(L)) study",_genParSet) ;
00348 
00349   return kTRUE ;
00350 }
00351 
00352 
00353 
00354 //_____________________________________________________________________________
00355 Bool_t RooRandomizeParamMCSModule::initializeRun(Int_t /*numSamples*/) 
00356 {
00357   // Initialize module at beginning of RooCMStudy run
00358 
00359   // Clear dataset at beginning of run
00360   _data->reset() ;
00361   return kTRUE ;
00362 }
00363 
00364 
00365 
00366 //_____________________________________________________________________________
00367 Bool_t RooRandomizeParamMCSModule::processBeforeGen(Int_t /*sampleNum*/) 
00368 {
00369   // Apply all smearings to generator parameters
00370 
00371   // Apply uniform smearing to all generator parameters for which it is requested
00372   std::list<UniParam>::iterator uiter ;
00373   for (uiter= _unifParams.begin() ; uiter!= _unifParams.end() ; ++uiter) {
00374     Double_t newVal = RooRandom::randomGenerator()->Uniform(uiter->_lo,uiter->_hi) ;        
00375     oocoutE((TObject*)0,Generation) << "RooRandomizeParamMCSModule::processBeforeGen: applying uniform smearing to generator parameter " 
00376          << uiter->_param->GetName() << " in range [" << uiter->_lo << "," << uiter->_hi << "], chosen value for this sample is " << newVal << endl ;
00377     uiter->_param->setVal(newVal) ;
00378 
00379     RooRealVar* genpar = static_cast<RooRealVar*>(_genParSet.find(Form("%s_gen",uiter->_param->GetName()))) ;
00380     genpar->setVal(newVal) ;
00381   }
00382 
00383   // Apply gaussian smearing to all generator parameters for which it is requested
00384   std::list<GausParam>::iterator giter ;
00385   for (giter= _gausParams.begin() ; giter!= _gausParams.end() ; ++giter) {
00386     Double_t newVal = RooRandom::randomGenerator()->Gaus(giter->_mean,giter->_sigma) ;
00387     oocoutI((TObject*)0,Generation) << "RooRandomizeParamMCSModule::processBeforeGen: applying gaussian smearing to generator parameter " 
00388          << giter->_param->GetName() << " with a mean of " << giter->_mean << " and a width of " << giter->_sigma << ", chosen value for this sample is " << newVal << endl ;
00389     giter->_param->setVal(newVal) ;
00390 
00391     RooRealVar* genpar = static_cast<RooRealVar*>(_genParSet.find(Form("%s_gen",giter->_param->GetName()))) ;
00392     genpar->setVal(newVal) ;
00393   }
00394 
00395   // Apply uniform smearing to all sets of generator parameters for which it is requested
00396   std::list<UniParamSet>::iterator usiter ;
00397   for (usiter= _unifParamSets.begin() ; usiter!= _unifParamSets.end() ; ++usiter) {
00398 
00399     // Calculate new value for sum 
00400     Double_t newVal = RooRandom::randomGenerator()->Uniform(usiter->_lo,usiter->_hi) ;        
00401     oocoutI((TObject*)0,Generation) << "RooRandomizeParamMCSModule::processBeforeGen: applying uniform smearing to sum of set of generator parameters " 
00402                                     <<  usiter->_pset
00403                                     << " in range [" << usiter->_lo << "," << usiter->_hi << "], chosen sum value for this sample is " << newVal << endl ;
00404 
00405     // Determine original value of sum and calculate per-component scale factor to obtain new valye for sum
00406     RooAddition sumVal("sumVal","sumVal",usiter->_pset) ;
00407     Double_t compScaleFactor = newVal/sumVal.getVal() ;
00408 
00409     // Apply multiplicative correction to each term of the sum
00410     TIterator* iter = usiter->_pset.createIterator() ;
00411     RooRealVar* param ;
00412     while((param=(RooRealVar*)iter->Next())) {
00413       param->setVal(param->getVal()*compScaleFactor) ;
00414       RooRealVar* genpar = static_cast<RooRealVar*>(_genParSet.find(Form("%s_gen",param->GetName()))) ;
00415       genpar->setVal(param->getVal()) ;
00416     }
00417     delete iter ;
00418   }
00419 
00420   // Apply gaussian smearing to all sets of generator parameters for which it is requested
00421   std::list<GausParamSet>::iterator gsiter ;
00422   for (gsiter= _gausParamSets.begin() ; gsiter!= _gausParamSets.end() ; ++gsiter) {
00423     
00424     // Calculate new value for sum 
00425     Double_t newVal = RooRandom::randomGenerator()->Gaus(gsiter->_mean,gsiter->_sigma) ;        
00426     oocoutI((TObject*)0,Generation) << "RooRandomizeParamMCSModule::processBeforeGen: applying gaussian smearing to sum of set of generator parameters " 
00427                                     << gsiter->_pset
00428                                     << " with a mean of " << gsiter->_mean << " and a width of " << gsiter->_sigma 
00429                                     << ", chosen value for this sample is " << newVal << endl ;
00430 
00431     // Determine original value of sum and calculate per-component scale factor to obtain new valye for sum
00432     RooAddition sumVal("sumVal","sumVal",gsiter->_pset) ;
00433     Double_t compScaleFactor = newVal/sumVal.getVal() ;
00434 
00435     // Apply multiplicative correction to each term of the sum
00436     TIterator* iter = gsiter->_pset.createIterator() ;
00437     RooRealVar* param ;
00438     while((param=(RooRealVar*)iter->Next())) {
00439       param->setVal(param->getVal()*compScaleFactor) ;
00440       RooRealVar* genpar = static_cast<RooRealVar*>(_genParSet.find(Form("%s_gen",param->GetName()))) ;
00441       genpar->setVal(param->getVal()) ;
00442     }
00443   }
00444   
00445   // Store generator values for all modified parameters
00446   _data->add(_genParSet) ;
00447   
00448   return kTRUE ;
00449 }
00450 
00451 
00452 
00453 //_____________________________________________________________________________
00454 RooDataSet* RooRandomizeParamMCSModule::finalizeRun() 
00455 {
00456   // Return auxiliary data of this module so that it is merged with
00457   // RooMCStudy::fitParDataSet()
00458   return _data ;
00459 }
00460 
00461 

Generated on Tue Jul 5 15:07:08 2011 for ROOT_528-00b_version by  doxygen 1.5.1