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