00001 // @(#)root/mathcore:$Id: TUnuranSampler.h 37419 2010-12-08 21:19:45Z moneta $ 00002 // Author: L. Moneta Fri Sep 22 15:06:47 2006 00003 00004 /********************************************************************** 00005 * * 00006 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT * 00007 * * 00008 * * 00009 **********************************************************************/ 00010 // Header file for class TUnuranSampler 00011 00012 #ifndef ROOT_TUnuranSampler 00013 #define ROOT_TUnuranSampler 00014 00015 00016 #ifndef ROOT_Math_DistSampler 00017 #include "Math/DistSampler.h" 00018 #endif 00019 00020 00021 namespace ROOT { 00022 00023 namespace Fit { 00024 00025 class DataRange; 00026 class BinData; 00027 class UnBinData; 00028 } 00029 00030 namespace Math { 00031 } 00032 } 00033 00034 00035 00036 //_______________________________________________________________________________ 00037 /** 00038 TUnuranSampler class 00039 class implementing the ROOT::Math::DistSampler interface using the UNU.RAN 00040 package for sampling distributions. 00041 00042 00043 */ 00044 00045 class TRandom; 00046 class TF1; 00047 class TUnuran; 00048 00049 class TUnuranSampler : public ROOT::Math::DistSampler { 00050 00051 public: 00052 00053 /// default constructor 00054 TUnuranSampler(); 00055 00056 00057 /// virtual destructor 00058 virtual ~TUnuranSampler(); 00059 00060 00061 using DistSampler::SetFunction; 00062 00063 /// set the parent function distribution to use for random sampling (one dim case) 00064 void SetFunction(const ROOT::Math::IGenFunction & func) { 00065 fFunc1D = &func; 00066 SetFunction<const ROOT::Math::IGenFunction>(func, 1); 00067 } 00068 00069 /// set the Function using a TF1 pointer 00070 void SetFunction(TF1 * pdf); 00071 00072 00073 /** 00074 initialize the generators with the given algorithm 00075 If no algorithm is passed used the default one for the type of distribution 00076 */ 00077 bool Init(const char * algo =""); 00078 00079 00080 /** 00081 initialize the generators with the given algorithm 00082 If no algorithm is passed used the default one for the type of distribution 00083 */ 00084 bool Init(const ROOT::Math::DistSamplerOptions & opt ); 00085 00086 /** 00087 Set the random engine to be used 00088 Needs to be called before Init to have effect 00089 */ 00090 void SetRandom(TRandom * r); 00091 00092 /** 00093 Set the random seed for the TRandom instances used by the sampler 00094 classes 00095 Needs to be called before Init to have effect 00096 */ 00097 void SetSeed(unsigned int seed); 00098 00099 /** 00100 Set the print level 00101 (if level=-1 use default) 00102 */ 00103 void SetPrintLevel(int level) {fLevel = level;} 00104 00105 /* 00106 set the mode 00107 */ 00108 void SetMode(double mode) { 00109 fMode = mode; 00110 fHasMode = true; 00111 } 00112 00113 /* 00114 set the area 00115 */ 00116 void SetArea(double area) { 00117 fArea = area; 00118 fHasArea = true; 00119 } 00120 00121 /** 00122 Get the random engine used by the sampler 00123 */ 00124 TRandom * GetRandom(); 00125 00126 /** 00127 sample one event in one dimension 00128 better implementation could be provided by the derived classes 00129 */ 00130 double Sample1D();// { 00131 // return fUnuran->Sample(); 00132 // } 00133 00134 /** 00135 sample one event in multi-dimension by filling the given array 00136 return false if sampling failed 00137 */ 00138 bool Sample(double * x); 00139 // { 00140 // if (!fOneDim) return fUnuran->SampleMulti(x); 00141 // x[0] = Sample1D(); 00142 // return true; 00143 // } 00144 00145 /** 00146 sample one bin given an estimated of the pdf in the bin 00147 (this can be function value at the center or its integral in the bin 00148 divided by the bin width) 00149 By default do not do random sample, just return the function values 00150 */ 00151 bool SampleBin(double prob, double & value, double *error = 0); 00152 00153 00154 00155 protected: 00156 00157 //initialization for 1D distributions 00158 bool DoInit1D(const char * algo); 00159 //initialization for 1D discrete distributions 00160 bool DoInitDiscrete1D(const char * algo); 00161 //initialization for multi-dim distributions 00162 bool DoInitND(const char * algo); 00163 00164 00165 private: 00166 00167 // private member 00168 bool fOneDim; // flag to indicate if the function is 1 dimension 00169 bool fDiscrete; // flag to indicate if the function is discrete 00170 bool fHasMode; // flag to indicate if a mode is set 00171 bool fHasArea; // flag to indicate if a area is set 00172 int fLevel; // debug level 00173 double fMode; // mode of dist 00174 double fArea; // area of dist 00175 const ROOT::Math::IGenFunction * fFunc1D; // 1D function pointer 00176 TUnuran * fUnuran; // unuran engine class 00177 00178 //ClassDef(TUnuranSampler,1) //Distribution sampler class based on UNU.RAN 00179 00180 }; 00181 00182 00183 00184 #endif /* ROOT_TUnuranSampler */