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
22Class RooFoamGenerator is a generic toy monte carlo 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 "Riostream.h"
39
40#include "RooFoamGenerator.h"
41#include "RooAbsReal.h"
42#include "RooCategory.h"
43#include "RooRealVar.h"
44#include "RooDataSet.h"
45#include "RooRandom.h"
46#include "RooErrorHandler.h"
47
48#include "RooMsgService.h"
49#include "TFoam.h"
50#include "RooTFoamBinding.h"
51#include "RooNumGenFactory.h"
52#include "RooNumGenConfig.h"
53
54#include <cassert>
55
56////////////////////////////////////////////////////////////////////////////////
57/// Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory
58
60{
61 // Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory
62 RooRealVar nSample("nSample","Number of samples per cell",200,0,1e6) ;
63 RooRealVar nCell1D("nCell1D","Number of cells for 1-dim generation",30,0,1e6) ;
64 RooRealVar nCell2D("nCell2D","Number of cells for 2-dim generation",500,0,1e6) ;
65 RooRealVar nCell3D("nCell3D","Number of cells for 3-dim generation",5000,0,1e6) ;
66 RooRealVar nCellND("nCellND","Number of cells for N-dim generation",10000,0,1e6) ;
67 RooRealVar chatLevel("chatLevel","TFOAM 'chat level' (verbosity)",0,0,2) ;
68
70 fact.storeProtoSampler(proto,RooArgSet(nSample,nCell1D,nCell2D,nCell3D,nCellND,chatLevel)) ;
71}
72
73
74
75
76////////////////////////////////////////////////////////////////////////////////
77
78RooFoamGenerator::RooFoamGenerator(const RooAbsReal &func, const RooArgSet &genVars, const RooNumGenConfig& config, bool verbose, const RooAbsReal* maxFuncVal) :
79 RooAbsNumGenerator(func,genVars,verbose,maxFuncVal)
80{
81 _binding = std::make_unique<RooTFoamBinding>(*_funcClone,_realVars) ;
82
83 _tfoam = std::make_unique<TFoam>("TFOAM") ;
84 _tfoam->SetkDim(_realVars.size()) ;
85 _tfoam->SetRho(_binding.get()) ;
87 switch(_realVars.size()) {
88 case 1:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell1D")) ; break ;
89 case 2:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell2D")) ; break ;
90 case 3:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCell3D")) ; break ;
91 default:_tfoam->SetnCells((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nCellND")) ; break ;
92 }
93 _tfoam->SetnSampl((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("nSample")) ;
95 _tfoam->SetChat((Int_t)config.getConfigSection("RooFoamGenerator").getRealValue("chatLevel")) ;
96 _tfoam->Initialize() ;
97
98 _vec.resize(_realVars.size());
99 _xmin.resize(_realVars.size());
100 _range.resize(_realVars.size());
101
102 Int_t i(0) ;
103 for (const auto arg : _realVars) {
104 auto var = static_cast<const RooRealVar*>(arg);
105 _xmin[i] = var->getMin() ;
106 _range[i] = var->getMax() - var->getMin() ;
107 i++ ;
108 }
109}
110
112
113////////////////////////////////////////////////////////////////////////////////
114/// are we actually generating anything? (the cache always contains at least our function value)
115
116const RooArgSet *RooFoamGenerator::generateEvent(UInt_t /*remaining*/, double& /*resampleRatio*/)
117{
118 const RooArgSet *event= _cache->get();
119 if(event->size() == 1) return event;
120
121 _tfoam->MakeEvent() ;
122 _tfoam->GetMCvect(_vec.data()) ;
123
124 // Transfer contents to dataset
125 Int_t i(0) ;
126 for (auto arg : _realVars) {
127 auto var = static_cast<RooRealVar*>(arg);
128 var->setVal(_xmin[i] + _range[i]*_vec[i]) ;
129 i++ ;
130 }
131 return &_realVars ;
132}
133
134std::string const& RooFoamGenerator::generatorName() const {
135 static const std::string name = "RooFoamGenerator";
136 return name;
137}
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
Class RooAbsNumGenerator is the abstract base class for MC event generator implementations like RooAc...
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.
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
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
Class RooFoamGenerator is a generic toy monte carlo generator that implement the TFOAM sampling techn...
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
~RooFoamGenerator() override
std::vector< double > _range
Range of observables to be generated ;.
std::unique_ptr< TFoam > _tfoam
Instance of TFOAM generator.
std::string const & generatorName() const override
Return unique name of generator implementation.
std::unique_ptr< RooTFoamBinding > _binding
Binding of RooAbsReal to TFoam function interface.
std::vector< double > _vec
Transfer array for FOAM output.
static void registerSampler(RooNumGenFactory &fact)
Register RooIntegrator1D, is parameters and capabilities with RooNumIntFactory.
RooNumGenConfig holds the configuration parameters of the various numeric integrators used by RooReal...
const RooArgSet & getConfigSection(const char *name) const
Retrieve configuration information specific to integrator with given name.
RooNumGenFactory is a factory to instantiate numeric integrators from a given function binding and a ...
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:51
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void setVal(double value) override
Set value of variable to 'value'.