Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooFoamGenerator.cxx
Go to the documentation of this file.
1/// \cond ROOFIT_INTERNAL
2
3/*****************************************************************************
4 * Project: RooFit *
5 * Package: RooFitCore *
6 * @(#)root/roofitcore:$Id$
7 * Authors: *
8 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
9 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
10 * *
11 * Copyright (c) 2000-2005, Regents of the University of California *
12 * and Stanford University. All rights reserved. *
13 * *
14 * Redistribution and use in source and binary forms, *
15 * with or without modification, are permitted according to the terms *
16 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
17 *****************************************************************************/
18
19/**
20\file RooFoamGenerator.cxx
21\class RooFoamGenerator
22\ingroup Roofitcore
23
24Generic Monte Carlo toy generator that implement
25the TFOAM sampling technique on any positively valued function.
26The RooFoamGenerator generator is used by the various generator context
27classes to take care of generation of observables for which p.d.fs
28do not define internal methods.
29
30The foam generator reacts to the following config options:
31- nCell[123N]D
32- nSample
33- chatLevel
34Access those using:
35 myPdf->specialGeneratorConfig()->getConfigSection("RooFoamGenerator").setRealValue("nSample",1e4);
36
37\see rf902_numgenconfig.C
38**/
39
40#include "RooFoamGenerator.h"
41
42#include <RooAbsReal.h>
43#include <RooArgSet.h>
44#include <RooCategory.h>
45#include <RooDataSet.h>
46#include <RooErrorHandler.h>
47#include <RooMsgService.h>
48#include <RooNumGenConfig.h>
49#include <RooRandom.h>
50#include <RooRealBinding.h>
51#include <RooRealVar.h>
52
53#include "RooNumGenFactory.h"
54
55namespace {
56
57// Lightweight interface adaptor that binds a RooAbsPdf to TFOAM.
58class RooTFoamBinding : public TFoamIntegrand {
59public:
60 RooTFoamBinding(const RooAbsReal &pdf, const RooArgSet &observables)
61 : _binding(std::make_unique<RooRealBinding>(pdf, observables, &_nset, false, nullptr))
62 {
63 _nset.add(observables);
64 }
65
66 double Density(Int_t ndim, double *xvec) override
67 {
68 double x[10];
69 for (int i = 0; i < ndim; i++) {
70 x[i] = xvec[i] * (_binding->getMaxLimit(i) - _binding->getMinLimit(i)) + _binding->getMinLimit(i);
71 }
72 double ret = (*_binding)(x);
73 return ret < 0 ? 0 : ret;
74 }
75
76 RooRealBinding &binding() { return *_binding; }
77
78private:
79 RooArgSet _nset;
80 std::unique_ptr<RooRealBinding> _binding;
81};
82
83} // namespace
84
85////////////////////////////////////////////////////////////////////////////////
86/// Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory
87
88void RooFoamGenerator::registerSampler(RooNumGenFactory& fact)
89{
90 // Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory
91 RooRealVar nSample("nSample","Number of samples per cell",200,0,1e6) ;
92 RooRealVar nCell1D("nCell1D","Number of cells for 1-dim generation",30,0,1e6) ;
93 RooRealVar nCell2D("nCell2D","Number of cells for 2-dim generation",500,0,1e6) ;
94 RooRealVar nCell3D("nCell3D","Number of cells for 3-dim generation",5000,0,1e6) ;
95 RooRealVar nCellND("nCellND","Number of cells for N-dim generation",10000,0,1e6) ;
96 RooRealVar chatLevel("chatLevel","TFOAM 'chat level' (verbosity)",0,0,2) ;
97
100}
101
102
103
104
105////////////////////////////////////////////////////////////////////////////////
106
107RooFoamGenerator::RooFoamGenerator(const RooAbsReal &func, const RooArgSet &genVars, const RooNumGenConfig& config, bool verbose, const RooAbsReal* maxFuncVal) :
109{
110 _binding = std::make_unique<RooTFoamBinding>(*_funcClone,_realVars) ;
111
112 _tfoam = std::make_unique<TFoam>("TFOAM") ;
113 _tfoam->SetkDim(_realVars.size()) ;
114 _tfoam->SetRho(_binding.get()) ;
115 _tfoam->SetPseRan(RooRandom::randomGenerator()) ;
116 switch(_realVars.size()) {
117 case 1:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell1D")) ; break ;
118 case 2:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell2D")) ; break ;
119 case 3:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell3D")) ; break ;
120 default:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCellND")) ; break ;
121 }
122 _tfoam->SetnSampl((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nSample")) ;
123 _tfoam->SetPseRan(RooRandom::randomGenerator()) ;
124 _tfoam->SetChat((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("chatLevel")) ;
125 _tfoam->Initialize() ;
126
127 _vec.resize(_realVars.size());
128 _xmin.resize(_realVars.size());
129 _range.resize(_realVars.size());
130
131 Int_t i(0) ;
132 for (auto *var : static_range_cast<RooRealVar const*>(_realVars)) {
133 _xmin[i] = var->getMin() ;
134 _range[i] = var->getMax() - var->getMin() ;
135 i++ ;
136 }
137}
138
139////////////////////////////////////////////////////////////////////////////////
140/// are we actually generating anything? (the cache always contains at least our function value)
141
142const RooArgSet *RooFoamGenerator::generateEvent(UInt_t /*remaining*/, double& /*resampleRatio*/)
143{
144 const RooArgSet *event= _cache->get();
145 if(event->size() == 1) return event;
146
147 _tfoam->MakeEvent() ;
148 _tfoam->GetMCvect(_vec.data()) ;
149
150 // Transfer contents to dataset
151 Int_t i(0) ;
152 for (auto arg : _realVars) {
153 auto var = static_cast<RooRealVar*>(arg);
154 var->setVal(_xmin[i] + _range[i]*_vec[i]) ;
155 i++ ;
156 }
157 return &_realVars ;
158}
159
160std::string const& RooFoamGenerator::generatorName() const {
161 static const std::string name = "RooFoamGenerator";
162 return name;
163}
164
165/// \endcond
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
std::vector< double > _vec
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char name[80]
Definition TGX11.cxx:110
const char * proto
Definition civetweb.c:17535
Storage_t const & get() const
Const access to the underlying stl container.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
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 TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition RooRandom.cxx:47
Lightweight interface adaptor that binds a RooAbsReal object to a subset of its servers and present i...
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void setVal(double value) override
Set value of variable to 'value'.
Abstract class representing n-dimensional real positive integrand function.
Double_t x[n]
Definition legend1.C:17