// RooRandomizeParamMCSModule is an add-on modules to RooMCStudy that
// allows you to randomize input generation parameters. Randomized generation
// parameters can be sampled from a uniform or Gaussian distribution.
// For every randomized parameter, an extra variable is added to
// RooMCStudy::fitParDataSet() named <tt><parname>_gen</tt> that indicates the actual
// value used for generation for each trial.
// <p>
// You can also choose to randomize the sum of N parameters, rather
// than a single parameter. In that case common multiplicative scale
// factor is applied to each component to bring the sum to the desired
// target value taken from either uniform or Gaussian sampling. This
// latter option is for example useful if you want to change the total
// number of expected events of an extended p.d.f
// END_HTML
#include "Riostream.h"
#include "RooDataSet.h"
#include "RooRealVar.h"
#include "RooRandom.h"
#include "TString.h"
#include "RooFit.h"
#include "RooFitResult.h"
#include "RooAddition.h"
#include "RooMsgService.h"
#include "RooRandomizeParamMCSModule.h"
using namespace std ;
ClassImp(RooRandomizeParamMCSModule)
;
RooRandomizeParamMCSModule::RooRandomizeParamMCSModule() :
RooAbsMCStudyModule("RooRandomizeParamMCSModule","RooRandomizeParamMCSModule"), _data(0)
{
}
RooRandomizeParamMCSModule::RooRandomizeParamMCSModule(const RooRandomizeParamMCSModule& other) :
RooAbsMCStudyModule(other),
_unifParams(other._unifParams),
_gausParams(other._gausParams),
_data(0)
{
}
RooRandomizeParamMCSModule:: ~RooRandomizeParamMCSModule()
{
if (_data) {
delete _data ;
}
}
void RooRandomizeParamMCSModule::sampleUniform(RooRealVar& param, Double_t lo, Double_t hi)
{
if (genParams()) {
RooRealVar* actualPar = static_cast<RooRealVar*>(genParams()->find(param.GetName())) ;
if (!actualPar) {
oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << param.GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
return ;
}
}
_unifParams.push_back(UniParam(¶m,lo,hi)) ;
}
void RooRandomizeParamMCSModule::sampleGaussian(RooRealVar& param, Double_t mean, Double_t sigma)
{
if (genParams()) {
RooRealVar* actualPar = static_cast<RooRealVar*>(genParams()->find(param.GetName())) ;
if (!actualPar) {
oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << param.GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
return ;
}
}
_gausParams.push_back(GausParam(¶m,mean,sigma)) ;
}
void RooRandomizeParamMCSModule::sampleSumUniform(const RooArgSet& paramSet, Double_t lo, Double_t hi)
{
RooArgSet okset ;
TIterator* iter = paramSet.createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
RooRealVar* rrv = dynamic_cast<RooRealVar*>(arg) ;
if (!rrv) {
oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::sampleSumUniform() ERROR: input parameter " << arg->GetName() << " is not a RooRealVar and is ignored" << endl ;
continue;
}
okset.add(*rrv) ;
}
delete iter ;
RooArgSet okset2 ;
if (genParams()) {
TIterator* psiter = okset.createIterator() ;
RooAbsArg* arg2 ;
while ((arg2=(RooAbsArg*)psiter->Next())) {
RooRealVar* actualVar= static_cast<RooRealVar*>(genParams()->find(arg2->GetName())) ;
if (!actualVar) {
oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::sampleSumUniform: variable " << arg2->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
} else {
okset2.add(*actualVar) ;
}
}
delete psiter ;
} else {
okset2.add(okset) ;
}
_unifParamSets.push_back(UniParamSet(okset2,lo,hi)) ;
}
void RooRandomizeParamMCSModule::sampleSumGauss(const RooArgSet& paramSet, Double_t mean, Double_t sigma)
{
RooArgSet okset ;
TIterator* iter = paramSet.createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
RooRealVar* rrv = dynamic_cast<RooRealVar*>(arg) ;
if (!rrv) {
oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::sampleSumGauss() ERROR: input parameter " << arg->GetName() << " is not a RooRealVar and is ignored" << endl ;
continue;
}
okset.add(*rrv) ;
}
RooArgSet okset2 ;
if (genParams()) {
TIterator* psiter = okset.createIterator() ;
RooAbsArg* arg2 ;
while ((arg2=(RooAbsArg*)psiter->Next())) {
RooRealVar* actualVar= static_cast<RooRealVar*>(genParams()->find(arg2->GetName())) ;
if (!actualVar) {
oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::sampleSumUniform: variable " << arg2->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
} else {
okset2.add(*actualVar) ;
}
}
delete psiter ;
} else {
okset2.add(okset) ;
}
_gausParamSets.push_back(GausParamSet(okset,mean,sigma)) ;
}
Bool_t RooRandomizeParamMCSModule::initializeInstance()
{
std::list<UniParam>::iterator uiter ;
for (uiter= _unifParams.begin() ; uiter!= _unifParams.end() ; ++uiter) {
RooRealVar* actualPar = static_cast<RooRealVar*>(genParams()->find(uiter->_param->GetName())) ;
if (!actualPar) {
oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << uiter->_param->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
uiter = _unifParams.erase(uiter) ;
continue ;
}
uiter->_param = actualPar ;
TString parName = Form("%s_gen",uiter->_param->GetName()) ;
TString parTitle = Form("%s as generated",uiter->_param->GetTitle()) ;
RooRealVar* par_gen = new RooRealVar(parName.Data(),parTitle.Data(),0) ;
_genParSet.addOwned(*par_gen) ;
}
std::list<UniParam>::iterator giter ;
for (giter= _unifParams.begin() ; giter!= _unifParams.end() ; ++giter) {
RooRealVar* actualPar = static_cast<RooRealVar*>(genParams()->find(giter->_param->GetName())) ;
if (!actualPar) {
oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << giter->_param->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
giter = _unifParams.erase(giter) ;
continue ;
}
giter->_param = actualPar ;
TString parName = Form("%s_gen",giter->_param->GetName()) ;
TString parTitle = Form("%s as generated",giter->_param->GetTitle()) ;
RooRealVar* par_gen = new RooRealVar(parName.Data(),parTitle.Data(),0) ;
_genParSet.addOwned(*par_gen) ;
}
std::list<UniParamSet>::iterator usiter ;
for (usiter= _unifParamSets.begin() ; usiter!= _unifParamSets.end() ; ++usiter) {
RooArgSet actualPSet ;
TIterator* psiter = usiter->_pset.createIterator() ;
RooAbsArg* arg ;
while ((arg=(RooAbsArg*)psiter->Next())) {
RooRealVar* actualVar= static_cast<RooRealVar*>(genParams()->find(arg->GetName())) ;
if (!actualVar) {
oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << arg->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
} else {
actualPSet.add(*actualVar) ;
}
}
delete psiter ;
usiter->_pset.removeAll() ;
usiter->_pset.add(actualPSet) ;
TIterator* iter = usiter->_pset.createIterator() ;
RooRealVar* param ;
while((param=(RooRealVar*)iter->Next())) {
TString parName = Form("%s_gen",param->GetName()) ;
TString parTitle = Form("%s as generated",param->GetTitle()) ;
RooRealVar* par_gen = new RooRealVar(parName.Data(),parTitle.Data(),0) ;
_genParSet.addOwned(*par_gen) ;
}
}
std::list<GausParamSet>::iterator ugiter ;
for (ugiter= _gausParamSets.begin() ; ugiter!= _gausParamSets.end() ; ++ugiter) {
RooArgSet actualPSet ;
TIterator* psiter = ugiter->_pset.createIterator() ;
RooAbsArg* arg ;
while ((arg=(RooAbsArg*)psiter->Next())) {
RooRealVar* actualVar= static_cast<RooRealVar*>(genParams()->find(arg->GetName())) ;
if (!actualVar) {
oocoutW((TObject*)0,InputArguments) << "RooRandomizeParamMCSModule::initializeInstance: variable " << arg->GetName() << " is not a parameter of RooMCStudy model and is ignored!" << endl ;
} else {
actualPSet.add(*actualVar) ;
}
}
ugiter->_pset.removeAll() ;
ugiter->_pset.add(actualPSet) ;
TIterator* iter = ugiter->_pset.createIterator() ;
RooRealVar* param ;
while((param=(RooRealVar*)iter->Next())) {
TString parName = Form("%s_gen",param->GetName()) ;
TString parTitle = Form("%s as generated",param->GetTitle()) ;
RooRealVar* par_gen = new RooRealVar(parName.Data(),parTitle.Data(),0) ;
_genParSet.addOwned(*par_gen) ;
}
}
_data = new RooDataSet("DeltaLLSigData","Additional data for Delta(-log(L)) study",_genParSet) ;
return kTRUE ;
}
Bool_t RooRandomizeParamMCSModule::initializeRun(Int_t )
{
_data->reset() ;
return kTRUE ;
}
Bool_t RooRandomizeParamMCSModule::processBeforeGen(Int_t )
{
std::list<UniParam>::iterator uiter ;
for (uiter= _unifParams.begin() ; uiter!= _unifParams.end() ; ++uiter) {
Double_t newVal = RooRandom::randomGenerator()->Uniform(uiter->_lo,uiter->_hi) ;
oocoutE((TObject*)0,Generation) << "RooRandomizeParamMCSModule::processBeforeGen: applying uniform smearing to generator parameter "
<< uiter->_param->GetName() << " in range [" << uiter->_lo << "," << uiter->_hi << "], chosen value for this sample is " << newVal << endl ;
uiter->_param->setVal(newVal) ;
RooRealVar* genpar = static_cast<RooRealVar*>(_genParSet.find(Form("%s_gen",uiter->_param->GetName()))) ;
genpar->setVal(newVal) ;
}
std::list<GausParam>::iterator giter ;
for (giter= _gausParams.begin() ; giter!= _gausParams.end() ; ++giter) {
Double_t newVal = RooRandom::randomGenerator()->Gaus(giter->_mean,giter->_sigma) ;
oocoutI((TObject*)0,Generation) << "RooRandomizeParamMCSModule::processBeforeGen: applying gaussian smearing to generator parameter "
<< giter->_param->GetName() << " with a mean of " << giter->_mean << " and a width of " << giter->_sigma << ", chosen value for this sample is " << newVal << endl ;
giter->_param->setVal(newVal) ;
RooRealVar* genpar = static_cast<RooRealVar*>(_genParSet.find(Form("%s_gen",giter->_param->GetName()))) ;
genpar->setVal(newVal) ;
}
std::list<UniParamSet>::iterator usiter ;
for (usiter= _unifParamSets.begin() ; usiter!= _unifParamSets.end() ; ++usiter) {
Double_t newVal = RooRandom::randomGenerator()->Uniform(usiter->_lo,usiter->_hi) ;
oocoutI((TObject*)0,Generation) << "RooRandomizeParamMCSModule::processBeforeGen: applying uniform smearing to sum of set of generator parameters "
<< usiter->_pset
<< " in range [" << usiter->_lo << "," << usiter->_hi << "], chosen sum value for this sample is " << newVal << endl ;
RooAddition sumVal("sumVal","sumVal",usiter->_pset) ;
Double_t compScaleFactor = newVal/sumVal.getVal() ;
TIterator* iter = usiter->_pset.createIterator() ;
RooRealVar* param ;
while((param=(RooRealVar*)iter->Next())) {
param->setVal(param->getVal()*compScaleFactor) ;
RooRealVar* genpar = static_cast<RooRealVar*>(_genParSet.find(Form("%s_gen",param->GetName()))) ;
genpar->setVal(param->getVal()) ;
}
delete iter ;
}
std::list<GausParamSet>::iterator gsiter ;
for (gsiter= _gausParamSets.begin() ; gsiter!= _gausParamSets.end() ; ++gsiter) {
Double_t newVal = RooRandom::randomGenerator()->Gaus(gsiter->_mean,gsiter->_sigma) ;
oocoutI((TObject*)0,Generation) << "RooRandomizeParamMCSModule::processBeforeGen: applying gaussian smearing to sum of set of generator parameters "
<< gsiter->_pset
<< " with a mean of " << gsiter->_mean << " and a width of " << gsiter->_sigma
<< ", chosen value for this sample is " << newVal << endl ;
RooAddition sumVal("sumVal","sumVal",gsiter->_pset) ;
Double_t compScaleFactor = newVal/sumVal.getVal() ;
TIterator* iter = gsiter->_pset.createIterator() ;
RooRealVar* param ;
while((param=(RooRealVar*)iter->Next())) {
param->setVal(param->getVal()*compScaleFactor) ;
RooRealVar* genpar = static_cast<RooRealVar*>(_genParSet.find(Form("%s_gen",param->GetName()))) ;
genpar->setVal(param->getVal()) ;
}
}
_data->add(_genParSet) ;
return kTRUE ;
}
RooDataSet* RooRandomizeParamMCSModule::finalizeRun()
{
return _data ;
}
Last change: Thu Nov 13 09:42:46 2008
Last generated: 2008-11-13 09:42
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.