Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TUnuranSampler.h
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Author: L. Moneta Fri Sep 22 15:06:47 2006
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10// Header file for class TUnuranSampler
11
12#ifndef ROOT_TUnuranSampler
13#define ROOT_TUnuranSampler
14
15
16#include "Math/DistSampler.h"
17
18// needed by the ClassDef
19#include "Rtypes.h"
20
21namespace ROOT {
22
23 namespace Fit {
24
25 class DataRange;
26 class BinData;
27 class UnBinData;
28 }
29
30 namespace Math {
31 }
32}
33
34
35
36//_______________________________________________________________________________
37/**
38 \class TUnuranSampler
39 \ingroup Unuran
40
41 TUnuranSampler class
42 class implementing the ROOT::Math::DistSampler interface using the UNU.RAN
43 package for sampling distributions.
44
45*/
46
47class TRandom;
48class TF1;
49class TUnuran;
50
52
53public:
54
55 /// default constructor
57
58
59 /// virtual destructor
60 virtual ~TUnuranSampler();
61
62
63 using DistSampler::SetFunction;
64
65 /// Set the parent function distribution to use for random sampling (one dim case).
67 fFunc1D = &func;
68 SetFunction<const ROOT::Math::IGenFunction>(func, 1);
69 }
70
71 /// Set the Function using a TF1 pointer.
72 void SetFunction(TF1 * pdf);
73
74 /// set the cumulative distribution function of the PDF used for random sampling (one dim case)
75 void SetCdf(const ROOT::Math::IGenFunction &cdf);
76
77 /// set the Derivative of the PDF used for random sampling (one dim continous case)
78 void SetDPdf(const ROOT::Math::IGenFunction &dpdf);
79
80 /**
81 initialize the generators with the given algorithm
82 If no algorithm is passed used the default one for the type of distribution
83 */
84 bool Init(const char * algo ="");
85
86
87 /**
88 initialize the generators with the given algorithm
89 If no algorithm is passed used the default one for the type of distribution
90 */
91 bool Init(const ROOT::Math::DistSamplerOptions & opt );
92
93 /**
94 Set the random engine to be used
95 Needs to be called before Init to have effect
96 */
97 void SetRandom(TRandom * r);
98
99 /**
100 Set the random seed for the TRandom instances used by the sampler
101 classes
102 Needs to be called before Init to have effect
103 */
104 void SetSeed(unsigned int seed);
105
106 /**
107 Set the print level
108 (if level=-1 use default)
109 */
110 void SetPrintLevel(int level) {fLevel = level;}
111
112 /*
113 set the mode (1D distribution)
114 */
115 void SetMode(double mode) {
116 fMode = mode;
117 fHasMode = true;
118 }
119
120 /*
121 set the mode (Multidim distribution)
122 */
123 void SetMode(const std::vector<double> &modes);
124
125
126 /*
127 set the area
128 */
129 void SetArea(double area) {
130 fArea = area;
131 fHasArea = true;
132 }
133
134 /// Set using of logarithm of PDF (only for 1D continous case)
135 void SetUseLogPdf(bool on = true) { fUseLogPdf = on; }
136
137 /**
138 Get the random engine used by the sampler
139 */
140 TRandom * GetRandom();
141
142 /**
143 sample one event in one dimension
144 better implementation could be provided by the derived classes
145 */
146 double Sample1D();// {
147// return fUnuran->Sample();
148// }
149
150 /**
151 sample one event in multi-dimension by filling the given array
152 return false if sampling failed
153 */
154 bool Sample(double * x);
155// {
156// if (!fOneDim) return fUnuran->SampleMulti(x);
157// x[0] = Sample1D();
158// return true;
159// }
160
161 /**
162 sample one bin given an estimated of the pdf in the bin
163 (this can be function value at the center or its integral in the bin
164 divided by the bin width)
165 By default do not do random sample, just return the function values
166 */
167 bool SampleBin(double prob, double & value, double *error = 0);
168
169
170
171protected:
172
173 /// Initialization for 1D distributions.
174 bool DoInit1D(const char * algo);
175 /// Initialization for 1D discrete distributions.
176 bool DoInitDiscrete1D(const char * algo);
177 /// Initialization for multi-dim distributions.
178 bool DoInitND(const char * algo);
179
180
181private:
182
183 // private member
184 bool fOneDim = false; /// flag to indicate if the function is 1 dimension
185 bool fDiscrete = false; /// flag to indicate if the function is discrete
186 bool fHasMode = false; /// flag to indicate if a mode is set
187 bool fHasArea = false; /// flag to indicate if a area is set
188 bool fUseLogPdf = false; /// flag to indicate if we use the log of the PDF
189 int fLevel; /// debug level
190 double fMode; /// mode of dist (1D)
191 double fArea; /// area of dist
192 std::vector<double> fNDMode; /// mode of the multi-dim distribution
193 const ROOT::Math::IGenFunction * fFunc1D = nullptr; /// 1D function pointer (pdf)
194 const ROOT::Math::IGenFunction * fCDF = nullptr; /// CDF function pointer
195 const ROOT::Math::IGenFunction * fDPDF = nullptr; /// 1D Derivative function pointer
196 TUnuran * fUnuran = nullptr; /// unuran engine class
197
198 ClassDef(TUnuranSampler, 2); /// Distribution sampler class based on UNU.RAN
199};
200
201
202
203#endif /* ROOT_TUnuranSampler */
ROOT::R::TRInterface & r
Definition Object.C:4
#define ClassDef(name, id)
Definition Rtypes.h:325
DistSampler options class.
Interface class for generic sampling of a distribution, i.e.
Definition DistSampler.h:57
const double * Sample()
Sample one event and return an array x with sample coordinates values.
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:135
1-Dim function class
Definition TF1.h:213
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
TUnuranSampler class class implementing the ROOT::Math::DistSampler interface using the UNU....
const ROOT::Math::IGenFunction * fDPDF
CDF function pointer.
double fArea
mode of dist (1D)
TRandom * GetRandom()
Get the random engine used by the sampler.
const ROOT::Math::IGenFunction * fCDF
1D function pointer (pdf)
void SetRandom(TRandom *r)
Set the random engine to be used Needs to be called before Init to have effect.
virtual ~TUnuranSampler()
virtual destructor
bool fHasMode
flag to indicate if the function is discrete
bool fHasArea
flag to indicate if a mode is set
bool SampleBin(double prob, double &value, double *error=0)
sample one bin given an estimated of the pdf in the bin (this can be function value at the center or ...
void SetPrintLevel(int level)
Set the print level (if level=-1 use default)
bool DoInitND(const char *algo)
Initialization for multi-dim distributions.
void SetSeed(unsigned int seed)
Set the random seed for the TRandom instances used by the sampler classes Needs to be called before I...
TUnuran * fUnuran
1D Derivative function pointer
const ROOT::Math::IGenFunction * fFunc1D
mode of the multi-dim distribution
void SetArea(double area)
Set the normalization area of distribution.
void SetCdf(const ROOT::Math::IGenFunction &cdf)
set the cumulative distribution function of the PDF used for random sampling (one dim case)
bool DoInit1D(const char *algo)
Initialization for 1D distributions.
void SetUseLogPdf(bool on=true)
Set using of logarithm of PDF (only for 1D continous case)
bool fUseLogPdf
flag to indicate if a area is set
void SetMode(double mode)
Set the mode of the distribution (1D case).
bool Init(const char *algo="")
initialize the generators with the given algorithm If no algorithm is passed used the default one for...
TUnuranSampler()
default constructor
bool fDiscrete
flag to indicate if the function is 1 dimension
void SetFunction(const ROOT::Math::IGenFunction &func)
Set the parent function distribution to use for random sampling (one dim case).
double Sample1D()
sample one event in one dimension better implementation could be provided by the derived classes
double fMode
debug level
std::vector< double > fNDMode
area of dist
int fLevel
flag to indicate if we use the log of the PDF
bool DoInitDiscrete1D(const char *algo)
Initialization for 1D discrete distributions.
void SetDPdf(const ROOT::Math::IGenFunction &dpdf)
set the Derivative of the PDF used for random sampling (one dim continous case)
TUnuran class.
Definition TUnuran.h:79
Double_t x[n]
Definition legend1.C:17
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition HFitImpl.cxx:133
Namespace for new Math classes and functions.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...