Logo ROOT   6.08/07
Reference Guide
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 #ifndef ROOT_Math_IFunctionfwd
17 #include "Math/IFunctionfwd.h"
18 #endif
19 
20 #ifndef ROOT_Math_WrappedFunction
21 #include "Math/WrappedFunction.h"
22 #endif
23 
24 #include <vector>
25 #include <cassert>
26 
27 class TRandom;
28 
29 namespace ROOT {
30 
31  namespace Fit {
32 
33  class DataRange;
34  class BinData;
35  class UnBinData;
36  }
37 
38  namespace Math {
39 
40  class DistSamplerOptions;
41 
42 /**
43  @defgroup Random Random Classes
44 
45  Pseudo-random numbers generator classes and for generation of random number distributions.
46  These classes implement several pseudo-random number generators and method for generation of random numbers
47  according to arbitrary distributions
48 
49  @ingroup MathCore
50 
51 */
52 
53 /**
54  Interface class for generic sampling of a distribution,
55  i.e. generating random numbers according to arbitrary distributions
56 
57  @ingroup Random
58 */
59 
60 
61 class DistSampler {
62 
63 public:
64 
65  /// default constructor
66  DistSampler() : fOwnFunc(false), fRange(0), fFunc(0) {}
67 
68 
69  /// virtual destructor
70  virtual ~DistSampler();
71 
72 
73 
74  /// set the parent function distribution to use for sampling (generic case)
75  template<class Function>
76  void SetFunction(Function & func, unsigned int dim) {
78  fData.resize(dim);
79  // need to clone to avoid temporary
80  DoSetFunction(wf,true);
81  }
82 
83  /// set the parent function distribution to use for random sampling (one dim case)
84  virtual void SetFunction(const ROOT::Math::IGenFunction & func) {
85  SetFunction<const ROOT::Math::IGenFunction>(func, 1);
86  }
87 
88 
89  /// set the parent function distribution to use for random sampling (multi-dim case)
91  DoSetFunction(func,false);
92  }
93 
94  /// return the dimension of the parent distribution (and the data)
95  unsigned int NDim() const { return fData.size(); }
96 
97 
98  /**
99  initialize the generators with the given algorithm
100  Implemented by derived classes who needs it
101  (like UnuranSampler)
102  If nothing is specified use default algorithm
103  from DistSamplerOptions::SetDefaultAlgorithm
104  */
105  virtual bool Init(const char * =""/* algorithm */) { return true;}
106 
107  /**
108  initialize the generators with the given option
109  which my include the algorithm but also more if
110  the method is re-impelmented by derived class
111  The default implementation calls the above method
112  passing just the algorithm name
113  */
114  virtual bool Init(const DistSamplerOptions & opt );
115 
116 
117  /**
118  Set the random engine to be used
119  To be implemented by the derived classes who provides
120  random sampling
121  */
122  virtual void SetRandom(TRandom * ) {}
123 
124  /**
125  Set the random seed for the TRandom instances used by the sampler
126  classes
127  To be implemented by the derived classes who provides random sampling
128  */
129  virtual void SetSeed(unsigned int /*seed*/ ) {}
130 
131  /**
132  Get the random engine used by the sampler
133  To be implemented by the derived classes who needs it
134  Returns zero by default
135  */
136  virtual TRandom * GetRandom() { return 0; }
137 
138  /// set range in a given dimension
139  void SetRange(double xmin, double xmax, int icoord = 0);
140 
141  /// set range for all dimensions
142  void SetRange(const double * xmin, const double * xmax);
143 
144  /// set range using DataRange class
145  void SetRange(const ROOT::Fit::DataRange & range);
146 
147  /// set the mode of the distribution (could be useful to some methods)
148  /// implemented by derived classes if needed
149  virtual void SetMode(double ) {}
150 
151  /// set the normalization area of distribution
152  /// implemented by derived classes if needed
153  virtual void SetArea(double) {}
154 
155  /// get the parent distribution function (must be called after setting the function)
157  return *fFunc;
158  }
159 
160 
161  /**
162  sample one event in one dimension
163  better implementation could be provided by the derived classes
164  */
165  virtual double Sample1D() {
166  Sample(&fData[0]);
167  return fData[0];
168  }
169 
170  /**
171  sample one event and rerturning array x with coordinates
172  */
173  const double * Sample() {
174  Sample(&fData[0]);
175  return &fData.front();
176  }
177 
178  /**
179  sample one event in multi-dimension by filling the given array
180  return false if sampling failed
181  */
182  virtual bool Sample(double * x) = 0;
183 
184  /**
185  sample one bin given an estimated of the pdf in the bin
186  (this can be function value at the center or its integral in the bin
187  divided by the bin width)
188  By default do not do random sample, just return the function values
189  Typically Poisson statistics will be used
190  */
191  virtual bool SampleBin(double prob, double & value, double * error = 0) {
192  value = prob;
193  if (error) *error = 0;
194  return true;
195  }
196  /**
197  sample a set of bins given a vector of probabilities
198  Typically multinomial statistics will be used and the sum of the probabilities
199  will be equal to the total number of events to be generated
200  For sampling the bins indipendently, SampleBin should be used
201  */
202  virtual bool SampleBins(unsigned int n, const double * prob, double * values, double * errors = 0) {
203  std::copy(prob,prob+n, values);
204  if (errors) std::fill(errors,errors+n,0);
205  return true;
206  }
207 
208 
209  /**
210  generate a un-binned data sets (fill the given data set)
211  if dataset has already data append to it
212  */
213  virtual bool Generate(unsigned int nevt, ROOT::Fit::UnBinData & data);
214 
215 
216  /**
217  generate a bin data set .
218  A range must have been set before (otherwise inf is returned)
219  and the bins are equidinstant in the previously defined range
220  bin center values must be present in given data set
221  If the sampler is implemented by a random one, the entries
222  will be binned according to the Poisson distribution
223  It is assumed the distribution is normalized, otherwise the nevt must be scaled
224  accordingly. The expected value/bin nexp = f(x_i) * binArea/ nevt
225  Extend control if use a fixed (i.e. multinomial statistics) or floating total number of events
226  */
227  virtual bool Generate(unsigned int nevt, const int * nbins, ROOT::Fit::BinData & data, bool extend = true);
228  /**
229  same as before but passing the range in case of 1 dim data
230  */
231  bool Generate(unsigned int nevt, int nbins, double xmin, double xmax, ROOT::Fit::BinData & data, bool extend = true) {
232  SetRange(xmin,xmax);
233  int nbs[1]; nbs[0] = nbins;
234  return Generate(nevt, nbs, data, extend);
235  }
236 
237 
238 protected:
239 
240  // internal method to set the function
241  virtual void DoSetFunction(const ROOT::Math::IMultiGenFunction & func, bool copy);
242  // check if generator have been initialized correctly and one can start generating
243  bool IsInitialized() ;
244  /// return the data range of the Pdf . Must be called after setting the function
245  const ROOT::Fit::DataRange & PdfRange() const {
246  assert(fRange);
247  return *fRange;
248  }
249 
250 
251 
252 
253 private:
254 
255  // private methods
256 
257  bool fOwnFunc; // flag to indicate if the function is owned
258  mutable std::vector<double> fData; // internal array used to cached the sample data
259  ROOT::Fit::DataRange * fRange; // data range
260  const ROOT::Math::IMultiGenFunction * fFunc; // internal function (ND)
261 
262 
263 };
264 
265  } // end namespace Math
266 
267 } // end namespace ROOT
268 
269 
270 #endif /* ROOT_Math_DistSampler */
virtual void SetMode(double)
set the mode of the distribution (could be useful to some methods) implemented by derived classes if ...
Definition: DistSampler.h:149
DistSampler()
default constructor
Definition: DistSampler.h:66
float xmin
Definition: THbookFile.cxx:93
std::vector< double > fData
Definition: DistSampler.h:258
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
fill
Definition: fit1_py.py:6
const double * Sample()
sample one event and rerturning array x with coordinates
Definition: DistSampler.h:173
virtual void SetFunction(const ROOT::Math::IGenFunction &func)
set the parent function distribution to use for random sampling (one dim case)
Definition: DistSampler.h:84
Class describing the unbinned data sets (just x coordinates values) of any dimensions.
Definition: UnBinData.h:47
int nbins[3]
Double_t x[n]
Definition: legend1.C:17
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:31
void SetFunction(Function &func, unsigned int dim)
set the parent function distribution to use for sampling (generic case)
Definition: DistSampler.h:76
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:119
virtual void SetRandom(TRandom *)
Set the random engine to be used To be implemented by the derived classes who provides random samplin...
Definition: DistSampler.h:122
DistSampler options class.
ROOT::Fit::DataRange * fRange
Definition: DistSampler.h:259
unsigned int NDim() const
return the dimension of the parent distribution (and the data)
Definition: DistSampler.h:95
const ROOT::Math::IMultiGenFunction * fFunc
Definition: DistSampler.h:260
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:61
float xmax
Definition: THbookFile.cxx:93
virtual double Sample1D()
sample one event in one dimension better implementation could be provided by the derived classes ...
Definition: DistSampler.h:165
virtual bool SampleBins(unsigned int n, const double *prob, double *values, double *errors=0)
sample a set of bins given a vector of probabilities Typically multinomial statistics will be used an...
Definition: DistSampler.h:202
virtual void SetArea(double)
set the normalization area of distribution implemented by derived classes if needed ...
Definition: DistSampler.h:153
Interface class for generic sampling of a distribution, i.e.
Definition: DistSampler.h:61
class describing the range in the coordinates it supports multiple range in a coordinate.
Definition: DataRange.h:34
bool Generate(unsigned int nevt, int nbins, double xmin, double xmax, ROOT::Fit::BinData &data, bool extend=true)
same as before but passing the range in case of 1 dim data
Definition: DistSampler.h:231
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition: HFitImpl.cxx:134
double func(double *x, double *p)
Definition: stressTF1.cxx:213
Template class to wrap any C++ callable object implementing operator() (const double * x) in a multi-...
Namespace for new Math classes and functions.
virtual void SetSeed(unsigned int)
Set the random seed for the TRandom instances used by the sampler classes To be implemented by the de...
Definition: DistSampler.h:129
const ROOT::Fit::DataRange & PdfRange() const
return the data range of the Pdf . Must be called after setting the function
Definition: DistSampler.h:245
virtual bool Init(const char *="")
initialize the generators with the given algorithm Implemented by derived classes who needs it (like ...
Definition: DistSampler.h:105
virtual 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 ...
Definition: DistSampler.h:191
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the parent function distribution to use for random sampling (multi-dim case)
Definition: DistSampler.h:90
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
virtual TRandom * GetRandom()
Get the random engine used by the sampler To be implemented by the derived classes who needs it Retur...
Definition: DistSampler.h:136
const ROOT::Math::IMultiGenFunction & ParentPdf() const
get the parent distribution function (must be called after setting the function)
Definition: DistSampler.h:156
const Int_t n
Definition: legend1.C:16