Logo ROOT  
Reference Guide
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 ~TUnuranSampler() override;
61
62
63 using DistSampler::SetFunction;
64
65 /// Set the parent function distribution to use for random sampling (one dim case).
66 void SetFunction(const ROOT::Math::IGenFunction & func) override {
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) override;
76
77 /// set the Derivative of the PDF used for random sampling (one dim continuous case)
78 void SetDPdf(const ROOT::Math::IGenFunction &dpdf) override;
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 ="") override;
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 ) override;
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) override;
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) override;
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) override {
116 fMode = mode;
117 fHasMode = true;
118 }
119
120 /*
121 set the mode (Multidim distribution)
122 */
123 void SetMode(const std::vector<double> &modes) override;
124
125
126 /*
127 set the area
128 */
129 void SetArea(double area) override {
130 fArea = area;
131 fHasArea = true;
132 }
133
134 /// Set using of logarithm of PDF (only for 1D continuous case)
135 void SetUseLogPdf(bool on = true) override { fUseLogPdf = on; }
136
137 /**
138 Get the random engine used by the sampler
139 */
140 TRandom * GetRandom() override;
141
142 /**
143 sample one event in one dimension
144 better implementation could be provided by the derived classes
145 */
146 double Sample1D() override;// {
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) override;
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) override;
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 */
#define ClassDef(name, id)
Definition: Rtypes.h:335
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 on
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char mode
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.
Definition: DistSampler.h:193
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
1D Derivative function pointer
double fArea
area of dist
bool Init(const char *algo="") override
initialize the generators with the given algorithm If no algorithm is passed used the default one for...
const ROOT::Math::IGenFunction * fCDF
CDF function pointer.
void SetRandom(TRandom *r) override
Set the random engine to be used Needs to be called before Init to have effect.
void SetUseLogPdf(bool on=true) override
Set using of logarithm of PDF (only for 1D continuous case)
void SetFunction(const ROOT::Math::IGenFunction &func) override
Set the parent function distribution to use for random sampling (one dim case).
TRandom * GetRandom() override
Get the random engine used by the sampler.
bool fHasMode
flag to indicate if a mode is set
bool fHasArea
flag to indicate if a area is set
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...
void SetPrintLevel(int level)
Set the print level (if level=-1 use default)
bool DoInitND(const char *algo)
Initialization for multi-dim distributions.
TUnuran * fUnuran
unuran engine class
void SetArea(double area) override
Set the normalization area of distribution.
const ROOT::Math::IGenFunction * fFunc1D
1D function pointer (pdf)
bool DoInit1D(const char *algo)
Initialization for 1D distributions.
void SetMode(double mode) override
Set the mode of the distribution (1D case).
bool fUseLogPdf
flag to indicate if we use the log of the PDF
double Sample1D() override
sample one event in one dimension better implementation could be provided by the derived classes
void SetDPdf(const ROOT::Math::IGenFunction &dpdf) override
set the Derivative of the PDF used for random sampling (one dim continuous case)
TUnuranSampler()
default constructor
bool fDiscrete
flag to indicate if the function is discrete
bool SampleBin(double prob, double &value, double *error=0) override
sample one bin given an estimated of the pdf in the bin (this can be function value at the center or ...
bool fOneDim
flag to indicate if the function is 1 dimension
void SetCdf(const ROOT::Math::IGenFunction &cdf) override
set the cumulative distribution function of the PDF used for random sampling (one dim case)
double fMode
mode of dist (1D)
std::vector< double > fNDMode
mode of the multi-dim distribution
int fLevel
debug level
~TUnuranSampler() override
virtual destructor
bool DoInitDiscrete1D(const char *algo)
Initialization for 1D discrete distributions.
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.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.