Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TFoamSampler.cxx
Go to the documentation of this file.
1// @(#)root/unuran:$Id$
2// Authors: L. Moneta, Dec 2010
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2010 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11// Implementation file for class TFoamSampler
12
13#include "TFoamSampler.h"
15
16#include "TFoam.h"
17#include "TFoamIntegrand.h"
19#include "Math/IOptions.h"
20#include "Fit/DataRange.h"
21
22#include "TRandom.h"
23#include "TError.h"
24
25#include "TF1.h"
26#include <cassert>
27#include <cmath>
28
30
31public:
32
34 fFunc(f),
35 fX(std::vector<double>(f.NDim() ) ),
36 fMinX(std::vector<double>(f.NDim() ) ),
37 fDeltaX(std::vector<double>(f.NDim() ) )
38 {
39 assert(f.NDim() == range.NDim() );
40 std::vector<double> xmax(f.NDim() );
41 for (unsigned int i = 0; i < range.NDim(); ++i) {
42 if (range.Size(i) == 0)
43 Error("FoamDistribution","Range is not set for coordinate dim %d",i);
44 else if (range.Size(i)>1)
45 Warning("FoamDistribution","Using only first range in coordinate dim %d",i);
46
47 std::pair<double,double> r = range(i);
48 fMinX[i] = r.first;
49 fDeltaX[i] = r.second - r.first;
50 }
51 }
52 // in principle function does not need to be cloned
53
54 double Density(int ndim, double * x) override {
55 assert(ndim == (int) fFunc.NDim() );
56 for (int i = 0; i < ndim; ++i)
57 fX[i] = fMinX[i] + x[i] * fDeltaX[i];
58
59 return (fFunc)(&fX[0]);
60 }
61
62 double MinX(unsigned int i) { return fMinX[i]; }
63 double DeltaX(unsigned int i) { return fDeltaX[i]; }
64
65private:
66
68 std::vector<double> fX;
69 std::vector<double> fMinX;
70 std::vector<double> fDeltaX;
71
72};
73
74/** \class TFoamSampler
75 class implementing the ROOT::Math::DistSampler interface using FOAM
76 for sampling arbitrary distributions.
77*/
78
79
80
82// fOneDim(false)
83// fDiscrete(false),
84// fHasMode(false), fHasArea(false),
85// fMode(0), fArea(0),
86 fFunc1D(nullptr),
87 fFoam(new TFoam("FOAM") ),
88 fFoamDist(nullptr)
89{}
90
92 assert(fFoam != nullptr);
93 delete fFoam;
94 if (fFoamDist) delete fFoamDist;
95}
96
97bool TFoamSampler::Init(const char *) {
98
99 // initialize using default options
103 return Init(opt);
104}
105
107 // initialize foam classes using the given algorithm
108 assert (fFoam != nullptr );
109 if (NDim() == 0) {
110 Error("TFoamSampler::Init","Distribution function has not been set ! Need to call SetFunction first.");
111 return false;
112 }
113
114 // initialize the foam
115 fFoam->SetkDim(NDim() );
116
117 // initialize random number
118 if (!GetRandom()) SetRandom(gRandom);
119
120 // create TFoamIntegrand class
121 if (fFoamDist) delete fFoamDist;
123
125 // set print level
126 fFoam->SetChat(opt.PrintLevel());
127
128 // get extra options
129 ROOT::Math::IOptions * fopt = opt.ExtraOptions();
130 if (fopt) {
131 int nval = 0;
132 double fval = 0;
133 if (fopt->GetIntValue("nCells", nval) ) fFoam->SetnCells(nval);
134 if (fopt->GetIntValue("nCell1D", nval) && NDim() ==1) fFoam->SetnCells(nval);
135 if (fopt->GetIntValue("nCellND", nval) && NDim() >1) fFoam->SetnCells(nval);
136 if (fopt->GetIntValue("nCell2D", nval) && NDim() ==2) fFoam->SetnCells(nval);
137 if (fopt->GetIntValue("nCell3D", nval) && NDim() ==3) fFoam->SetnCells(nval);
138
139 if (fopt->GetIntValue("nSample", nval) ) fFoam->SetnSampl(nval);
140 if (fopt->GetIntValue("nBin", nval) ) fFoam->SetnBin(nval);
141 if (fopt->GetIntValue("OptDrive",nval) ) fFoam->SetOptDrive(nval);
142 if (fopt->GetIntValue("OptRej",nval) ) fFoam->SetOptRej(nval);
143 if (fopt->GetRealValue("MaxWtRej",fval) ) fFoam->SetMaxWtRej(fval);
144
145
146 if (fopt->GetIntValue("chatLevel", nval) ) fFoam->SetChat(nval);
147 }
148 fFoam->Initialize();
149
150 return true;
151
152}
153
154
156 // set function from a TF1 pointer
157 SetFunction<TF1>(*pdf, pdf->GetNdim());
158}
159
161 // set random generator (must be called before Init to have effect)
162 fFoam->SetPseRan(r);
163}
164
165void TFoamSampler::SetSeed(unsigned int seed) {
166 // set random generator seed (must be called before Init to have effect)
167 TRandom * r = fFoam->GetPseRan();
168 if (r) r->SetSeed(seed);
169}
170
172 // get random generator used
173 return fFoam->GetPseRan();
174}
175
176// double TFoamSampler::Sample1D() {
177// // sample 1D distributions
178// return (fDiscrete) ? (double) fFoam->SampleDiscr() : fFoam->Sample();
179// }
180
181bool TFoamSampler::Sample(double * x) {
182 // sample multi-dim distributions
183
184 fFoam->MakeEvent();
185 fFoam->GetMCvect(x);
186 // adjust for the range
187 for (unsigned int i = 0; i < NDim(); ++i)
188 x[i] = ( (FoamDistribution*)fFoamDist)->MinX(i) + ( ( (FoamDistribution*) fFoamDist)->DeltaX(i))*x[i];
189
190 return true;
191}
192
193
194bool TFoamSampler::SampleBin(double prob, double & value, double *error) {
195 // sample a bin according to Poisson statistics
196
197 TRandom * r = GetRandom();
198 if (!r) return false;
199 value = r->Poisson(prob);
200 if (error) *error = std::sqrt(value);
201 return true;
202}
#define f(i)
Definition RSha256.hxx:104
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
float xmax
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
std::vector< double > fMinX
double Density(int ndim, double *x) override
std::vector< double > fDeltaX
FoamDistribution(const ROOT::Math::IMultiGenFunction &f, const ROOT::Fit::DataRange &range)
const ROOT::Math::IMultiGenFunction & fFunc
double MinX(unsigned int i)
std::vector< double > fX
double DeltaX(unsigned int i)
class describing the range in the coordinates it supports multiple range in a coordinate.
Definition DataRange.h:35
DistSampler options class.
int PrintLevel() const
non-static methods for retrieving options
void SetExtraOptions(const IOptions &opt)
set extra options (in this case pointer is cloned)
IOptions * ExtraOptions() const
return extra options (NULL pointer if they are not present)
static ROOT::Math::IOptions * FindDefault(const char *name)
const double * Sample()
Sample one event and return an array x with sample coordinates values.
const ROOT::Math::IMultiGenFunction & ParentPdf() const
Get the parent distribution function (must be called after setting the function).
unsigned int NDim() const
return the dimension of the parent distribution (and the data)
Definition DistSampler.h:92
const ROOT::Fit::DataRange & PdfRange() const
return the data range of the Pdf . Must be called after setting the function
Documentation for the abstract class IBaseFunctionMultiDim.
Definition IFunction.h:63
Generic interface for defining configuration options of a numerical algorithm.
Definition IOptions.h:28
virtual bool GetIntValue(const char *, int &) const
Definition IOptions.h:64
virtual bool GetRealValue(const char *, double &) const
Definition IOptions.h:63
1-Dim function class
Definition TF1.h:182
virtual Int_t GetNdim() const
Definition TF1.h:465
Abstract class representing n-dimensional real positive integrand function.
void SetFunction(const ROOT::Math::IGenFunction &func) override
set the parent function distribution to use for random sampling (one dim case)
bool Init(const char *="") override
initialize the generators with the default options
void SetSeed(unsigned int seed) override
Set the random seed for the TRandom instances used by the sampler classes Needs to be called before I...
TFoamSampler()
default constructor
bool SampleBin(double prob, double &value, double *error=nullptr) override
sample one bin given an estimated of the pdf in the bin (this can be function value at the center or ...
TFoamIntegrand * fFoamDist
TRandom * GetRandom() override
Get the random engine used by the sampler.
~TFoamSampler() override
virtual destructor
void SetRandom(TRandom *r) override
Set the random engine to be used Needs to be called before Init to have effect.
TFoam is the main class of the multi-dimensional general purpose Monte Carlo event generator (integra...
Definition TFoam.h:21
virtual void GetMCvect(Double_t *)
User may get generated MC point/vector with help of this method.
Definition TFoam.cxx:1171
virtual void MakeEvent()
User method.
Definition TFoam.cxx:1121
virtual void Initialize()
Basic initialization of FOAM invoked by the user.
Definition TFoam.cxx:320
virtual void SetnSampl(Long_t nSampl)
Definition TFoam.h:117
virtual void SetMaxWtRej(Double_t MaxWtRej)
Definition TFoam.h:123
virtual TRandom * GetPseRan() const
Definition TFoam.h:108
virtual void SetChat(Int_t Chat)
Definition TFoam.h:119
virtual void SetOptDrive(Int_t OptDrive)
Definition TFoam.h:121
virtual void SetnCells(Long_t nCells)
Definition TFoam.h:116
virtual void SetRho(TFoamIntegrand *Rho)
User may use this method to set the distribution object.
Definition TFoam.cxx:1017
virtual void SetOptRej(Int_t OptRej)
Definition TFoam.h:120
virtual void SetPseRan(TRandom *PseRan)
Definition TFoam.h:112
virtual void SetnBin(Int_t nBin)
Definition TFoam.h:118
virtual void SetkDim(Int_t kDim)
Definition TFoam.h:115
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1057
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
Double_t x[n]
Definition legend1.C:17