Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
DistSampler.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
11// Header file for class DistSampler
12
13#ifndef ROOT_Math_DistSampler
14#define ROOT_Math_DistSampler
15
16#include "Math/IFunctionfwd.h"
17
19
20#include <vector>
21#include <cassert>
22
23class TRandom;
24
25namespace ROOT {
26
27 namespace Fit {
28
29 class DataRange;
30 class BinData;
31 class UnBinData;
32 }
33
34 namespace Math {
35
36 class DistSamplerOptions;
37
38/**
39 @defgroup Random Interface classes for Random number generation
40
41 Pseudo-random numbers generator classes and for generation of random number distributions.
42 These classes implement several pseudo-random number generators and method for generation of random numbers
43 according to arbitrary distributions
44
45 @ingroup MathCore
46
47*/
48
49/**
50 Interface class for generic sampling of a distribution,
51 i.e. generating random numbers according to arbitrary distributions
52
53 @ingroup Random
54*/
55
56
58
59public:
60
61 /// default constructor
62 DistSampler() : fOwnFunc(false), fRange(nullptr), fFunc(nullptr) {}
63
64
65 /// virtual destructor
66 virtual ~DistSampler();
67
68
69
70 /// set the parent function distribution to use for sampling (generic case)
71 template<class Function>
72 void SetFunction(Function & func, unsigned int dim) {
74 fData.resize(dim);
75 // need to clone to avoid temporary
76 DoSetFunction(wf,true);
77 }
78
79 /// set the parent function distribution to use for random sampling (one dim case)
80 virtual void SetFunction(const ROOT::Math::IGenFunction & func) {
81 SetFunction<const ROOT::Math::IGenFunction>(func, 1);
82 }
83
84
85 /// set the parent function distribution to use for random sampling (multi-dim case)
86 virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func) {
87 DoSetFunction(func,false);
88 }
89
90 /// return the dimension of the parent distribution (and the data)
91 unsigned int NDim() const { return fData.size(); }
92
93
94 /**
95 Initialize the sampling generator with the given algorithm.
96 Implemented by the derived classes who needs it
97 (like UnuranSampler).
98 If nothing is specified use default algorithm
99 from DistSamplerOptions::SetDefaultAlgorithm
100 */
101 virtual bool Init(const char * =""/* algorithm */) { return true;}
102
103 /**
104 Initialize the generators with the given DistSamplerOption object.
105 The string will include the algorithm and in case additional options
106 which can be interpreted by a re-implemented method in the derived class.
107 The default implementation just calls the above method
108 passing just the algorithm name
109 */
110 virtual bool Init(const DistSamplerOptions & opt );
111
112
113 /**
114 Set the random engine to be used.
115 To be implemented by the derived classes who provides
116 random sampling
117 */
118 virtual void SetRandom(TRandom * ) {}
119
120 /**
121 Set the random seed for the TRandom instances used by the sampler
122 classes.
123 To be implemented by the derived classes who provides random sampling
124 */
125 virtual void SetSeed(unsigned int /*seed*/ ) {}
126
127 /**
128 Get the random engine used by the sampler.
129 To be implemented by the derived classes who needs it
130 Returns zero by default
131 */
132 virtual TRandom * GetRandom() { return nullptr; }
133
134 /// Set the range in a given dimension.
135 void SetRange(double xmin, double xmax, int icoord = 0);
136
137 /// Set the range for all dimensions.
138 void SetRange(const double * xmin, const double * xmax);
139 /// Set the range for all dimensions (use std::vector)
140 void SetRange(const std::vector<double> & xmin, const std::vector<double> & xmax){
141 assert(xmin.size() >= NDim() && xmax.size() >= NDim());
142 SetRange(xmin.data(),xmax.data());
143 }
144
145 /// Set the range using the ROOT::Fit::DataRange class.
146 void SetRange(const ROOT::Fit::DataRange & range);
147
148 /// Set the mode of the distribution (1D case).
149 /// It could be useful or needed by some sampling methods.
150 /// It is implemented by derived classes if needed (e.g. TUnuranSampler)
151 virtual void SetMode(double ) {}
152
153 /// Set the mode of the distribution (Multi-dim case).
154 virtual void SetMode(const std::vector<double> &) {}
155
156 /// Set the normalization area of distribution.
157 /// Implemented by derived classes if needed
158 virtual void SetArea(double) {}
159
160 /// Use the log of the provided pdf.
161 /// Implemented by the derived classes
162 virtual void SetUseLogPdf(bool = true) {}
163
164 /// Set usage of Derivative of PDF.
165 /// Can be implemented by derived class
166 virtual void SetDPdf(const ROOT::Math::IGenFunction & ) {}
167
168 /// Set usage of Cumulative of PDF.
169 /// Can be implemented by derived class
170 virtual void SetCdf(const ROOT::Math::IGenFunction &) {}
171
172 /// Get the parent distribution function (must be called after setting the function).
174 return *fFunc;
175 }
176
177 /// Check if there is a parent distribution defined.
178 bool HasParentPdf() const { return fFunc != nullptr; }
179
180 /**
181 Sample one event in one dimension.
182 Specialized implementation could be provided by the derived classes
183 */
184 virtual double Sample1D() {
185 Sample(&fData[0]);
186 return fData[0];
187 }
188
189 /**
190 Sample one event and return an array x with
191 sample coordinates values.
192 */
193 const double * Sample() {
194 Sample(&fData[0]);
195 return &fData.front();
196 }
197
198 /**
199 Sample one event in multi-dimension by filling the given array.
200 Return false if the sampling failed.
201 Abstract method to be re-implemented by the derived classes
202 */
203 virtual bool Sample(double * x) = 0;
204
205 /**
206 Sample one bin given an estimate of the pdf in the bin.
207 (this can be function value at the center or its integral in the bin
208 divided by the bin width)
209 By default do not do random sample, just return the function values
210 Typically Poisson statistics will be used
211 */
212 virtual bool SampleBin(double prob, double & value, double * error = nullptr) {
213 value = prob;
214 if (error) *error = 0;
215 return true;
216 }
217 /**
218 Sample a set of bins given a vector of probabilities
219 Typically multinomial statistics will be used and the sum of the probabilities
220 will be equal to the total number of events to be generated
221 For sampling the bins independently, SampleBin should be used
222 */
223 virtual bool SampleBins(unsigned int n, const double * prob, double * values, double * errors = nullptr) {
224 std::copy(prob,prob+n, values); // default impl returns prob values (Asimov data)
225 if (errors) std::fill(errors,errors+n,0);
226 return true;
227 }
228
229
230 /**
231 Generate a un-binned data set by filling the given data set object.
232 If the data set object is not empty, the new generated data will be appended to the
233 existing one.
234 */
235 virtual bool Generate(unsigned int nevt, ROOT::Fit::UnBinData & data);
236 /**
237 Generate a vector of events by filling the passed data vector.
238 The flag eventRow indicates how the events are arranged in the multi-dim case.
239 The can be arranged in rows or in columns.
240 With eventRow=false events are the columns in data: {x1,x2,.....,xn},{y1,....yn}
241 With eventRow=true events are rows in data: {x1,y1},{x2,y2},.....{xn,yn}
242 */
243 virtual bool Generate(unsigned int nevt, double * data, bool eventRow = false);
244
245 /**
246 Generate a binned data set.
247 A range must have been set before (otherwise inf is returned)
248 and the bins are equidistant in the previously defined range
249 bin center values must be present in given data set
250 If the sampler is implemented by a random one, the entries
251 will be binned according to the Poisson distribution
252 It is assumed the distribution is normalized, otherwise the nevt must be scaled
253 accordingly. The expected value/bin nexp = f(x_i) * binArea/ nevt
254 Extend control if use a fixed (i.e. multinomial statistics) or floating total number of events
255 */
256 virtual bool Generate(unsigned int nevt, const int * nbins, ROOT::Fit::BinData & data, bool extend = true, bool expErr = true);
257 /**
258 Same as before but passing the range in case of 1 dim data.
259 */
260 bool Generate(unsigned int nevt, int nbins, double xmin, double xmax, ROOT::Fit::BinData & data, bool extend = true, bool expErr = true ) {
262 int nbs[1]; nbs[0] = nbins;
263 return Generate(nevt, nbs, data, extend, expErr);
264 }
265
266
267protected:
268
269 // internal method to set the function
270 virtual void DoSetFunction(const ROOT::Math::IMultiGenFunction & func, bool copy);
271 // internal method to set the dimension
272 virtual void DoSetDimension(unsigned int ndim);
273 // check if generator have been initialized correctly and one can start generating
274 bool IsInitialized() ;
275 /// return the data range of the Pdf . Must be called after setting the function
277 assert(fRange);
278 return *fRange;
279 }
280
281private:
282
283 // private methods
284
285 bool fOwnFunc; ///< flag to indicate if the function is owned
286 mutable std::vector<double> fData; ///<! internal array used to cached the sample data
287 ROOT::Fit::DataRange * fRange; ///< data range
288 const ROOT::Math::IMultiGenFunction * fFunc; ///< internal function (ND)
289
290
291};
292
293 } // end namespace Math
294
295} // end namespace ROOT
296
297
298#endif /* ROOT_Math_DistSampler */
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
float xmin
float xmax
Double_t(* Function)(Double_t)
Definition Functor.C:4
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition BinData.h:52
class describing the range in the coordinates it supports multiple range in a coordinate.
Definition DataRange.h:35
Class describing the un-binned data sets (just x coordinates values) of any dimensions.
Definition UnBinData.h:46
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.
virtual bool Sample(double *x)=0
Sample one event in multi-dimension by filling the given array.
ROOT::Fit::DataRange * fRange
data range
virtual void DoSetFunction(const ROOT::Math::IMultiGenFunction &func, bool copy)
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:91
virtual void SetFunction(const ROOT::Math::IGenFunction &func)
set the parent function distribution to use for random sampling (one dim case)
Definition DistSampler.h:80
virtual ~DistSampler()
virtual destructor
virtual bool Generate(unsigned int nevt, ROOT::Fit::UnBinData &data)
Generate a un-binned data set by filling the given data set object.
virtual void SetCdf(const ROOT::Math::IGenFunction &)
Set usage of Cumulative of PDF.
const ROOT::Math::IMultiGenFunction * fFunc
internal function (ND)
DistSampler()
default constructor
Definition DistSampler.h:62
void SetRange(double xmin, double xmax, int icoord=0)
Set the range in a given dimension.
const ROOT::Fit::DataRange & PdfRange() const
return the data range of the Pdf . Must be called after setting the function
virtual bool SampleBin(double prob, double &value, double *error=nullptr)
Sample one bin given an estimate of the pdf in the bin.
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the parent function distribution to use for random sampling (multi-dim case)
Definition DistSampler.h:86
virtual void SetDPdf(const ROOT::Math::IGenFunction &)
Set usage of Derivative of PDF.
virtual void SetMode(const std::vector< double > &)
Set the mode of the distribution (Multi-dim case).
virtual void DoSetDimension(unsigned int ndim)
void SetFunction(Function &func, unsigned int dim)
set the parent function distribution to use for sampling (generic case)
Definition DistSampler.h:72
virtual TRandom * GetRandom()
Get the random engine used by the sampler.
virtual bool Init(const char *="")
Initialize the sampling generator with the given algorithm.
virtual bool SampleBins(unsigned int n, const double *prob, double *values, double *errors=nullptr)
Sample a set of bins given a vector of probabilities Typically multinomial statistics will be used an...
virtual void SetArea(double)
Set the normalization area of distribution.
virtual double Sample1D()
Sample one event in one dimension.
void SetRange(const std::vector< double > &xmin, const std::vector< double > &xmax)
Set the range for all dimensions (use std::vector)
virtual void SetRandom(TRandom *)
Set the random engine to be used.
std::vector< double > fData
! internal array used to cached the sample data
virtual void SetMode(double)
Set the mode of the distribution (1D case).
virtual void SetSeed(unsigned int)
Set the random seed for the TRandom instances used by the sampler classes.
bool Generate(unsigned int nevt, int nbins, double xmin, double xmax, ROOT::Fit::BinData &data, bool extend=true, bool expErr=true)
Same as before but passing the range in case of 1 dim data.
bool fOwnFunc
flag to indicate if the function is owned
bool HasParentPdf() const
Check if there is a parent distribution defined.
virtual void SetUseLogPdf(bool=true)
Use the log of the provided pdf.
Documentation for the abstract class IBaseFunctionMultiDim.
Definition IFunction.h:61
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:112
Template class to wrap any C++ callable object implementing operator() (const double * x) in a multi-...
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
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...