Logo ROOT  
Reference Guide
RooFoamGenerator.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 RooFoamGenerator.cxx
19 \class RooFoamGenerator
20 \ingroup Roofitcore
21 
22 Class RooFoamGenerator is a generic toy monte carlo generator that implement
23 the TFOAM sampling technique on any positively valued function.
24 The RooFoamGenerator generator is used by the various generator context
25 classes to take care of generation of observables for which p.d.fs
26 do not define internal methods.
27 
28 The foam generator reacts to the following config options:
29 - nCell[123N]D
30 - nSample
31 - chatLevel
32 Access those using:
33  myPdf->specialGeneratorConfig()->getConfigSection("RooFoamGenerator").setRealValue("nSample",1e4);
34 
35 \see rf902_numgenconfig.C
36 **/
37 
38 
39 #include "RooFit.h"
40 #include "Riostream.h"
41 
42 #include "RooFoamGenerator.h"
43 #include "RooAbsReal.h"
44 #include "RooCategory.h"
45 #include "RooRealVar.h"
46 #include "RooDataSet.h"
47 #include "RooRandom.h"
48 #include "RooErrorHandler.h"
49 
50 #include "TString.h"
51 #include "TIterator.h"
52 #include "RooMsgService.h"
53 #include "TClass.h"
54 #include "TFoam.h"
55 #include "RooTFoamBinding.h"
56 #include "RooNumGenFactory.h"
57 #include "RooNumGenConfig.h"
58 
59 #include <assert.h>
60 
61 using namespace std;
62 
64  ;
65 
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 /// Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory
69 
71 {
72  // Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory
73  RooRealVar nSample("nSample","Number of samples per cell",200,0,1e6) ;
74  RooRealVar nCell1D("nCell1D","Number of cells for 1-dim generation",30,0,1e6) ;
75  RooRealVar nCell2D("nCell2D","Number of cells for 2-dim generation",500,0,1e6) ;
76  RooRealVar nCell3D("nCell3D","Number of cells for 3-dim generation",5000,0,1e6) ;
77  RooRealVar nCellND("nCellND","Number of cells for N-dim generation",10000,0,1e6) ;
78  RooRealVar chatLevel("chatLevel","TFOAM 'chat level' (verbosity)",0,0,2) ;
79 
81  fact.storeProtoSampler(proto,RooArgSet(nSample,nCell1D,nCell2D,nCell3D,nCellND,chatLevel)) ;
82 }
83 
84 
85 
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 
89 RooFoamGenerator::RooFoamGenerator(const RooAbsReal &func, const RooArgSet &genVars, const RooNumGenConfig& config, Bool_t verbose, const RooAbsReal* maxFuncVal) :
90  RooAbsNumGenerator(func,genVars,verbose,maxFuncVal)
91 {
93 
94  _tfoam = new TFoam("TFOAM") ;
98  switch(_realVars.getSize()) {
99  case 1:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell1D")) ; break ;
100  case 2:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell2D")) ; break ;
101  case 3:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell3D")) ; break ;
102  default:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCellND")) ; break ;
103  }
104  _tfoam->SetnSampl((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nSample")) ;
106  _tfoam->SetChat((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("chatLevel")) ;
107  _tfoam->Initialize() ;
108 
109  _vec = new Double_t[_realVars.getSize()] ;
110  _xmin = new Double_t[_realVars.getSize()] ;
111  _range = new Double_t[_realVars.getSize()] ;
112 
113  Int_t i(0) ;
114  for (const auto arg : _realVars) {
115  auto var = static_cast<const RooRealVar*>(arg);
116  _xmin[i] = var->getMin() ;
117  _range[i] = var->getMax() - var->getMin() ;
118  i++ ;
119  }
120 }
121 
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Destructor
125 
127 {
128  delete[] _vec ;
129  delete[] _xmin ;
130  delete[] _range ;
131  delete _tfoam ;
132  delete _binding ;
133 }
134 
135 
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// are we actually generating anything? (the cache always contains at least our function value)
139 
140 const RooArgSet *RooFoamGenerator::generateEvent(UInt_t /*remaining*/, Double_t& /*resampleRatio*/)
141 {
142  const RooArgSet *event= _cache->get();
143  if(event->getSize() == 1) return event;
144 
145  _tfoam->MakeEvent() ;
146  _tfoam->GetMCvect(_vec) ;
147 
148  // Transfer contents to dataset
149  Int_t i(0) ;
150  for (auto arg : _realVars) {
151  auto var = static_cast<RooRealVar*>(arg);
152  var->setVal(_xmin[i] + _range[i]*_vec[i]) ;
153  i++ ;
154  }
155  return &_realVars ;
156 }
RooFoamGenerator::_tfoam
TFoam * _tfoam
Definition: RooFoamGenerator.h:54
RooFoamGenerator::_range
Double_t * _range
Definition: RooFoamGenerator.h:56
RooFoamGenerator::_xmin
Double_t * _xmin
Definition: RooFoamGenerator.h:55
RooRealVar::setVal
virtual void setVal(Double_t value)
Set value of variable to 'value'.
Definition: RooRealVar.cxx:226
RooFoamGenerator::registerSampler
static void registerSampler(RooNumGenFactory &fact)
Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory.
Definition: RooFoamGenerator.cxx:70
RooAbsReal.h
RooMsgService.h
RooFit.h
RooArgSet::getRealValue
Double_t getRealValue(const char *name, Double_t defVal=0, Bool_t verbose=kFALSE) const
Get value of a RooAbsReal stored in set with given name.
Definition: RooArgSet.cxx:474
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TFoam::MakeEvent
virtual void MakeEvent()
User method.
Definition: TFoam.cxx:1126
RooFoamGenerator.h
RooNumGenFactory.h
RooAbsNumGenerator::_cache
RooDataSet * _cache
Definition: RooAbsNumGenerator.h:80
TClass.h
RooAbsReal
Definition: RooAbsReal.h:61
RooFoamGenerator::RooFoamGenerator
RooFoamGenerator()
Definition: RooFoamGenerator.h:33
RooNumGenFactory::storeProtoSampler
Bool_t storeProtoSampler(RooAbsNumGenerator *proto, const RooArgSet &defConfig)
Method accepting registration of a prototype numeric integrator along with a RooArgSet of its default...
Definition: RooNumGenFactory.cxx:120
RooDataSet.h
TString.h
RooNumGenFactory
Definition: RooNumGenFactory.h:30
TFoam.h
bool
RooDataSet::get
virtual const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
Definition: RooDataSet.cxx:1038
TFoam::SetChat
virtual void SetChat(Int_t Chat)
Definition: TFoam.h:119
RooAbsNumGenerator::_funcClone
RooAbsReal * _funcClone
Definition: RooAbsNumGenerator.h:74
TFoam
Definition: TFoam.h:21
TIterator.h
RooFoamGenerator::_binding
RooTFoamBinding * _binding
Definition: RooFoamGenerator.h:53
RooRandom.h
TFoam::SetnSampl
virtual void SetnSampl(Long_t nSampl)
Definition: TFoam.h:117
RooAbsNumGenerator
Definition: RooAbsNumGenerator.h:30
TFoam::GetMCvect
virtual void GetMCvect(Double_t *)
User may get generated MC point/vector with help of this method.
Definition: TFoam.cxx:1176
RooCategory.h
RooRealVar.h
RooNumGenConfig::getConfigSection
const RooArgSet & getConfigSection(const char *name) const
Retrieve configuration information specific to integrator with given name.
Definition: RooNumGenConfig.cxx:295
TFoam::Initialize
virtual void Initialize()
Basic initialization of FOAM invoked by the user.
Definition: TFoam.cxx:323
unsigned int
proto
const char * proto
Definition: civetweb.c:16604
Double_t
double Double_t
Definition: RtypesCore.h:59
RooFoamGenerator
Definition: RooFoamGenerator.h:31
RooNumGenConfig
Definition: RooNumGenConfig.h:25
RooAbsNumGenerator::_realVars
RooArgSet _realVars
Definition: RooAbsNumGenerator.h:76
RooNumGenConfig.h
RooTFoamBinding
Definition: RooTFoamBinding.h:24
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
TFoam::SetkDim
virtual void SetkDim(Int_t kDim)
Definition: TFoam.h:115
TFoam::SetnCells
virtual void SetnCells(Long_t nCells)
Definition: TFoam.h:116
RooErrorHandler.h
RooRandom::randomGenerator
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition: RooRandom.cxx:53
RooFoamGenerator::~RooFoamGenerator
virtual ~RooFoamGenerator()
Destructor.
Definition: RooFoamGenerator.cxx:126
RooTFoamBinding.h
TFoam::SetRho
virtual void SetRho(TFoamIntegrand *Rho)
User may use this method to set the distribution object.
Definition: TFoam.cxx:1022
RooRealVar
Definition: RooRealVar.h:35
TFoam::SetPseRan
virtual void SetPseRan(TRandom *PseRan)
Definition: TFoam.h:112
Riostream.h
RooAbsCollection::getSize
Int_t getSize() const
Definition: RooAbsCollection.h:171
RooArgSet
Definition: RooArgSet.h:28
RooFoamGenerator::generateEvent
const RooArgSet * generateEvent(UInt_t remaining, Double_t &resampleRatio)
are we actually generating anything? (the cache always contains at least our function value)
Definition: RooFoamGenerator.cxx:140
RooFoamGenerator::_vec
Double_t * _vec
Definition: RooFoamGenerator.h:57
int