Logo ROOT   6.08/07
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 #ifndef ROOT_Math_DistSampler
17 #include "Math/DistSampler.h"
18 #endif
19 
20 
21 namespace 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 
47 class TRandom;
48 class TF1;
49 class TUnuran;
50 
52 
53 public:
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 
75  /**
76  initialize the generators with the given algorithm
77  If no algorithm is passed used the default one for the type of distribution
78  */
79  bool Init(const char * algo ="");
80 
81 
82  /**
83  initialize the generators with the given algorithm
84  If no algorithm is passed used the default one for the type of distribution
85  */
86  bool Init(const ROOT::Math::DistSamplerOptions & opt );
87 
88  /**
89  Set the random engine to be used
90  Needs to be called before Init to have effect
91  */
92  void SetRandom(TRandom * r);
93 
94  /**
95  Set the random seed for the TRandom instances used by the sampler
96  classes
97  Needs to be called before Init to have effect
98  */
99  void SetSeed(unsigned int seed);
100 
101  /**
102  Set the print level
103  (if level=-1 use default)
104  */
105  void SetPrintLevel(int level) {fLevel = level;}
106 
107  /*
108  set the mode
109  */
110  void SetMode(double mode) {
111  fMode = mode;
112  fHasMode = true;
113  }
114 
115  /*
116  set the area
117  */
118  void SetArea(double area) {
119  fArea = area;
120  fHasArea = true;
121  }
122 
123  /**
124  Get the random engine used by the sampler
125  */
126  TRandom * GetRandom();
127 
128  /**
129  sample one event in one dimension
130  better implementation could be provided by the derived classes
131  */
132  double Sample1D();// {
133 // return fUnuran->Sample();
134 // }
135 
136  /**
137  sample one event in multi-dimension by filling the given array
138  return false if sampling failed
139  */
140  bool Sample(double * x);
141 // {
142 // if (!fOneDim) return fUnuran->SampleMulti(x);
143 // x[0] = Sample1D();
144 // return true;
145 // }
146 
147  /**
148  sample one bin given an estimated of the pdf in the bin
149  (this can be function value at the center or its integral in the bin
150  divided by the bin width)
151  By default do not do random sample, just return the function values
152  */
153  bool SampleBin(double prob, double & value, double *error = 0);
154 
155 
156 
157 protected:
158 
159  //initialization for 1D distributions
160  bool DoInit1D(const char * algo);
161  //initialization for 1D discrete distributions
162  bool DoInitDiscrete1D(const char * algo);
163  //initialization for multi-dim distributions
164  bool DoInitND(const char * algo);
165 
166 
167 private:
168 
169  // private member
170  bool fOneDim; // flag to indicate if the function is 1 dimension
171  bool fDiscrete; // flag to indicate if the function is discrete
172  bool fHasMode; // flag to indicate if a mode is set
173  bool fHasArea; // flag to indicate if a area is set
174  int fLevel; // debug level
175  double fMode; // mode of dist
176  double fArea; // area of dist
177  const ROOT::Math::IGenFunction * fFunc1D; // 1D function pointer
178  TUnuran * fUnuran; // unuran engine class
179 
180  //ClassDef(TUnuranSampler,1) //Distribution sampler class based on UNU.RAN
181 
182 };
183 
184 
185 
186 #endif /* ROOT_TUnuranSampler */
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
void SetArea(double area)
set the normalization area of distribution implemented by derived classes if needed ...
void SetPrintLevel(int level)
Set the print level (if level=-1 use default)
void SetFunction(const ROOT::Math::IGenFunction &func)
set the parent function distribution to use for random sampling (one dim case)
Double_t x[n]
Definition: legend1.C:17
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:31
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:119
TRandom2 r(17)
DistSampler options class.
TUnuran * fUnuran
TUnuran class.
Definition: TUnuran.h:81
Interface class for generic sampling of a distribution, i.e.
Definition: DistSampler.h:61
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
Namespace for new Math classes and functions.
1-Dim function class
Definition: TF1.h:149
TUnuranSampler class class implementing the ROOT::Math::DistSampler interface using the UNU...
const ROOT::Math::IGenFunction * fFunc1D
void SetMode(double mode)
set the mode of the distribution (could be useful to some methods) implemented by derived classes if ...