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