Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
22Generic Monte Carlo toy generator that implement
23the TFOAM sampling technique on any positively valued function.
24The RooFoamGenerator generator is used by the various generator context
25classes to take care of generation of observables for which p.d.fs
26do not define internal methods.
27
28The foam generator reacts to the following config options:
29- nCell[123N]D
30- nSample
31- chatLevel
32Access those using:
33 myPdf->specialGeneratorConfig()->getConfigSection("RooFoamGenerator").setRealValue("nSample",1e4);
34
35\see rf902_numgenconfig.C
36**/
37
38#include "RooFoamGenerator.h"
39
40#include <RooAbsReal.h>
41#include <RooArgSet.h>
42#include <RooCategory.h>
43#include <RooDataSet.h>
44#include <RooErrorHandler.h>
45#include <RooMsgService.h>
46#include <RooNumGenConfig.h>
47#include <RooRandom.h>
48#include <RooRealBinding.h>
49#include <RooRealVar.h>
50
51#include "RooNumGenFactory.h"
52
53namespace {
54
55// Lightweight interface adaptor that binds a RooAbsPdf to TFOAM.
56class RooTFoamBinding : public TFoamIntegrand {
57public:
58 RooTFoamBinding(const RooAbsReal &pdf, const RooArgSet &observables)
59 : _binding(std::make_unique<RooRealBinding>(pdf, observables, &_nset, false, nullptr))
60 {
61 _nset.add(observables);
62 }
63
64 double Density(Int_t ndim, double *xvec) override
65 {
66 double x[10];
67 for (int i = 0; i < ndim; i++) {
68 x[i] = xvec[i] * (_binding->getMaxLimit(i) - _binding->getMinLimit(i)) + _binding->getMinLimit(i);
69 }
70 double ret = (*_binding)(x);
71 return ret < 0 ? 0 : ret;
72 }
73
74 RooRealBinding &binding() { return *_binding; }
75
76private:
77 RooArgSet _nset;
78 std::unique_ptr<RooRealBinding> _binding;
79};
80
81} // namespace
82
83////////////////////////////////////////////////////////////////////////////////
84/// Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory
85
87{
88 // Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory
89 RooRealVar nSample("nSample","Number of samples per cell",200,0,1e6) ;
90 RooRealVar nCell1D("nCell1D","Number of cells for 1-dim generation",30,0,1e6) ;
91 RooRealVar nCell2D("nCell2D","Number of cells for 2-dim generation",500,0,1e6) ;
92 RooRealVar nCell3D("nCell3D","Number of cells for 3-dim generation",5000,0,1e6) ;
93 RooRealVar nCellND("nCellND","Number of cells for N-dim generation",10000,0,1e6) ;
94 RooRealVar chatLevel("chatLevel","TFOAM 'chat level' (verbosity)",0,0,2) ;
95
97 fact.storeProtoSampler(proto,RooArgSet(nSample,nCell1D,nCell2D,nCell3D,nCellND,chatLevel)) ;
98}
99
100
101
102
103////////////////////////////////////////////////////////////////////////////////
104
105RooFoamGenerator::RooFoamGenerator(const RooAbsReal &func, const RooArgSet &genVars, const RooNumGenConfig& config, bool verbose, const RooAbsReal* maxFuncVal) :
106 RooAbsNumGenerator(func,genVars,verbose,maxFuncVal)
107{
108 _binding = std::make_unique<RooTFoamBinding>(*_funcClone,_realVars) ;
109
110 _tfoam = std::make_unique<TFoam>("TFOAM") ;
111 _tfoam->SetkDim(_realVars.size()) ;
112 _tfoam->SetRho(_binding.get()) ;
113 _tfoam->SetPseRan(RooRandom::randomGenerator()) ;
114 switch(_realVars.size()) {
115 case 1:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell1D")) ; break ;
116 case 2:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell2D")) ; break ;
117 case 3:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell3D")) ; break ;
118 default:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCellND")) ; break ;
119 }
120 _tfoam->SetnSampl((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nSample")) ;
121 _tfoam->SetPseRan(RooRandom::randomGenerator()) ;
122 _tfoam->SetChat((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("chatLevel")) ;
123 _tfoam->Initialize() ;
124
125 _vec.resize(_realVars.size());
126 _xmin.resize(_realVars.size());
127 _range.resize(_realVars.size());
128
129 Int_t i(0) ;
130 for (auto *var : static_range_cast<RooRealVar const*>(_realVars)) {
131 _xmin[i] = var->getMin() ;
132 _range[i] = var->getMax() - var->getMin() ;
133 i++ ;
134 }
135}
136
137////////////////////////////////////////////////////////////////////////////////
138/// are we actually generating anything? (the cache always contains at least our function value)
139
140const RooArgSet *RooFoamGenerator::generateEvent(UInt_t /*remaining*/, double& /*resampleRatio*/)
141{
142 const RooArgSet *event= _cache->get();
143 if(event->size() == 1) return event;
144
145 _tfoam->MakeEvent() ;
146 _tfoam->GetMCvect(_vec.data()) ;
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}
157
158std::string const& RooFoamGenerator::generatorName() const {
159 static const std::string name = "RooFoamGenerator";
160 return name;
161}
char name[80]
Definition TGX11.cxx:110
const char * proto
Definition civetweb.c:17536
double getRealValue(const char *name, double defVal=0.0, bool verbose=false) const
Get value of a RooAbsReal stored in set with given name.
Storage_t const & get() const
Const access to the underlying stl container.
Storage_t::size_type size() const
Abstract base class for MC event generator implementations like RooAcceptReject and RooFoam.
RooArgSet _realVars
Set of real valued observabeles.
RooAbsReal * _funcClone
Pointer to top level node of cloned function.
std::unique_ptr< RooDataSet > _cache
Dataset holding generared values of observables.
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:55
Generic Monte Carlo toy generator that implement the TFOAM sampling technique on any positively value...
const RooArgSet * generateEvent(UInt_t remaining, double &resampleRatio) override
are we actually generating anything? (the cache always contains at least our function value)
std::vector< double > _xmin
Lower bound of observables to be generated ;.
RooFoamGenerator()=default
std::vector< double > _range
Range of observables to be generated ;.
std::unique_ptr< TFoam > _tfoam
Instance of TFOAM generator.
std::unique_ptr< TFoamIntegrand > _binding
Binding of RooAbsReal to TFoam function interface.
std::string const & generatorName() const override
Return unique name of generator implementation.
std::vector< double > _vec
Transfer array for FOAM output.
static void registerSampler(RooNumGenFactory &fact)
Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory.
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.
Factory to instantiate numeric integrators from a given function binding and a given configuration.
bool storeProtoSampler(RooAbsNumGenerator *proto, const RooArgSet &defConfig)
Method accepting registration of a prototype numeric integrator along with a RooArgSet of its default...
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition RooRandom.cxx:48
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.
virtual Double_t Density(Int_t ndim, Double_t *)=0
Double_t x[n]
Definition legend1.C:17