52 RooCategory samplingMode(
"samplingMode",
"Sampling Mode") ;
53 samplingMode.defineType(
"Importance",RooMCIntegrator::Importance) ;
54 samplingMode.defineType(
"ImportanceOnly",RooMCIntegrator::ImportanceOnly) ;
55 samplingMode.defineType(
"Stratified",RooMCIntegrator::Stratified) ;
56 samplingMode.setIndex(RooMCIntegrator::Importance) ;
59 genType.defineType(
"QuasiRandom",RooMCIntegrator::QuasiRandom) ;
60 genType.defineType(
"PseudoRandom",RooMCIntegrator::PseudoRandom) ;
61 genType.setIndex(RooMCIntegrator::QuasiRandom) ;
68 RooRealVar alpha(
"alpha",
"Grid structure constant",1.5) ;
69 RooRealVar nRefineIter(
"nRefineIter",
"Number of refining iterations",5) ;
70 RooRealVar nRefinePerDim(
"nRefinePerDim",
"Number of refining samples (per dimension)",1000) ;
71 RooRealVar nIntPerDim(
"nIntPerDim",
"Number of integration samples (per dimension)",5000) ;
75 return std::make_unique<RooMCIntegrator>(function,config);
79 std::string
name =
"RooMCIntegrator";
98RooMCIntegrator::RooMCIntegrator(
const RooAbsFunc& function, SamplingMode
mode,
99 GeneratorType genType,
bool verbose) :
101 _alpha(1.5), _mode(
mode), _genType(genType),
102 _nRefineIter(5),_nRefinePerDim(1000),_nIntegratePerDim(5000)
105 if(!(_valid= _grid.isValid()))
return;
106 if(_verbose) _grid.print(std::cout);
121 _mode = (SamplingMode) configSet.
getCatIndex(
"samplingMode",Importance) ;
122 _genType = (GeneratorType) configSet.
getCatIndex(
"genType",QuasiRandom) ;
128 if(!(_valid= _grid.isValid()))
return;
129 if(_verbose) _grid.print(std::cout);
137bool RooMCIntegrator::checkLimits()
const
139 return _grid.initialize(*integrand());
150double RooMCIntegrator::integral(
const double* )
153 vegas(AllStages,_nRefinePerDim*_grid.getDimension(),_nRefineIter);
154 double ret = vegas(ReuseGrid,_nIntegratePerDim*_grid.getDimension(),1);
167double RooMCIntegrator::vegas(Stage stage,
UInt_t calls,
UInt_t iterations,
double *absError)
172 if(stage == AllStages) _grid.initialize(*_function);
175 if(stage <= ReuseGrid) {
184 if(stage <= RefineGrid) {
185 UInt_t bins = RooGrid::maxBins;
187 UInt_t dim(_grid.getDimension());
190 if(_mode != ImportanceOnly) {
193 boxes = (
UInt_t)floor(std::pow(calls/2.0,1.0/dim));
197 if (2*boxes >= RooGrid::maxBins) {
200 Int_t box_per_bin= (boxes > RooGrid::maxBins) ? boxes/RooGrid::maxBins : 1;
201 bins= boxes/box_per_bin;
202 if(bins > RooGrid::maxBins) bins= RooGrid::maxBins;
203 boxes = box_per_bin * bins;
204 oocxcoutD((
TObject*)
nullptr,Integration) <<
"RooMCIntegrator: using stratified sampling with " << bins <<
" bins and "
205 << box_per_bin <<
" boxes/bin" << std::endl;
208 oocxcoutD((
TObject*)
nullptr,Integration) <<
"RooMCIntegrator: using importance sampling with " << bins <<
" bins and "
209 << boxes <<
" boxes" << std::endl;
214 double tot_boxes = std::pow((
double)boxes,(
double)dim);
217 _calls_per_box = (
UInt_t)(calls/tot_boxes);
218 if(_calls_per_box < 2) _calls_per_box= 2;
219 calls= (
UInt_t)(_calls_per_box*tot_boxes);
222 _jac = _grid.getVolume()*std::pow((
double)bins,(
double)dim)/calls;
225 _grid.setNBoxes(boxes);
226 if(bins != _grid.getNBins()) _grid.resize(bins);
230 std::vector<UInt_t>
box(_grid.getDimension());
231 std::vector<UInt_t> bin(_grid.getDimension());
232 std::vector<double>
x(_grid.getDimension());
240 for (
UInt_t it = 0; it < iterations; it++) {
246 _it_num = _it_start + it;
252 _grid.firstBox(
box.data());
257 for(
UInt_t k = 0; k < _calls_per_box; k++) {
260 _grid.generatePoint(
box.data(),
x.data(), bin.data(), bin_vol, _genType == QuasiRandom ?
true : false);
262 double fval= jacbin*bin_vol*integrand(
x.data());
266 q+=
d *
d * (k / (k + 1.0));
268 if (_mode != Stratified) _grid.accumulate(bin.data(), fval*fval);
270 intgrl +=
m * _calls_per_box;
271 double f_sq_sum =
q * _calls_per_box ;
275 if (_mode == Stratified) _grid.accumulate(bin.data(), f_sq_sum);
278 if(_timer.RealTime() > 30) {
279 std::size_t
index = 0;
280 std::size_t sizeOfDim = 1;
282 for (
unsigned int i=0; i < _grid.getDimension(); ++i) {
284 sizeOfDim *= _grid.getNBoxes();
286 oocoutP(
nullptr, Integration) <<
"RooMCIntegrator: still working ... iteration "
287 << it <<
'/' << iterations <<
" box " <<
index <<
"/"<< std::pow(_grid.getNBoxes(), _grid.getDimension()) << std::endl;
294 }
while(_grid.nextBox(
box.data()));
298 sig = sig / (_calls_per_box - 1.0) ;
302 else if (_sum_wgts > 0) {
303 wgt = _sum_wgts / _samples;
308 intgrl_sq = intgrl * intgrl;
315 _wtd_int_sum += intgrl * wgt;
316 _chi_sum += intgrl_sq * wgt;
318 cum_int = _wtd_int_sum / _sum_wgts;
319 cum_sig = sqrt (1 / _sum_wgts);
322 _chisq = (_chi_sum - _wtd_int_sum * cum_int)/(_samples - 1.0);
326 cum_int += (intgrl - cum_int) / (it + 1.0);
329 oocxcoutD((
TObject*)
nullptr,Integration) <<
"=== Iteration " << _it_num <<
" : I = " << intgrl <<
" +/- " << sqrt(sig) << std::endl
330 <<
" Cumulative : I = " << cum_int <<
" +/- " << cum_sig <<
"( chi2 = " << _chisq
334 if(it + 1 == iterations) _grid.print(std::cout,
true);
336 _grid.refine(_alpha);
339 if(absError) *absError = cum_sig;
int Int_t
Signed integer 4 bytes (int)
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char mode
double getRealValue(const char *name, double defVal=0.0, bool verbose=false) const
Get value of a RooAbsReal stored in set with given name.
Int_t getCatIndex(const char *name, Int_t defVal=0, bool verbose=false) const
Get index value of a RooAbsCategory stored in set with given name.
Abstract interface for evaluating a real-valued function of one real variable and performing numerica...
Abstract interface for integrators of real-valued functions that implement the RooAbsFunc interface.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Object to represent discrete states.
bool setLabel(const char *label, bool printError=true) override
Set value by specifying the name of the desired state.
Holds the configuration parameters of the various numeric integrators used by RooRealIntegral.
const RooArgSet & getConfigSection(const char *name) const
Retrieve configuration information specific to integrator with given name.
static RooNumIntConfig & defaultConfig()
Return reference to instance of default numeric integrator configuration object.
Factory to instantiate numeric integrators from a given function binding and a given configuration.
bool registerPlugin(std::string const &name, Creator const &creator, const RooArgSet &defConfig, bool canIntegrate1D, bool canIntegrate2D, bool canIntegrateND, bool canIntegrateOpenEnded, const char *depName="")
Method accepting registration of a prototype numeric integrator along with a RooArgSet of its default...
Variable that can be changed from the outside.
Mother of all ROOT objects.
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)