29 DistSampler::~DistSampler() {
31 if (fOwnFunc && fFunc != 0)
delete fFunc;
32 if (fRange)
delete fRange;
35 bool DistSampler::Init(
const DistSamplerOptions & opt ) {
37 return Init(opt.Algorithm().c_str() );
40 void DistSampler::SetRange(
double xmin,
double xmax,
int icoord) {
42 MATH_ERROR_MSG(
"DistSampler::SetRange",
"Need to set function before setting the range");
45 fRange->SetRange(icoord,xmin,xmax);
48 void DistSampler::SetRange(
const double * xmin,
const double * xmax) {
51 MATH_ERROR_MSG(
"DistSampler::SetRange",
"Need to set function before setting the range");
54 for (
unsigned int icoord = 0; icoord < NDim(); ++icoord)
55 fRange->SetRange(icoord,xmin[icoord],xmax[icoord]);
58 void DistSampler::SetRange(
const ROOT::Fit::DataRange & range) {
63 void DistSampler::DoSetFunction(
const ROOT::Math::IMultiGenFunction & func,
bool copy) {
66 if (fOwnFunc && fFunc != 0)
delete fFunc;
75 fData = std::vector<double>(func.NDim());
77 if (fRange && fRange->NDim() != fData.size() ) {
81 if (!fRange) fRange =
new ROOT::Fit::DataRange(func.NDim() );
84 bool DistSampler::IsInitialized() {
87 if (NDim() == 0)
return false;
88 if (fFunc == 0)
return false;
89 if (fFunc->NDim() != NDim() )
return false;
91 if (!Sample(&fData[0]) )
return false;
95 bool DistSampler::Generate(
unsigned int nevt, ROOT::Fit::UnBinData & data) {
98 if (!IsInitialized()) {
99 MATH_WARN_MSG(
"DistSampler::Generate",
"sampler has not been initialized correctly");
103 data.Append( nevt, NDim() );
104 for (
unsigned int i = 0; i < nevt; ++i) {
105 const double * x = Sample();
112 bool DistSampler::Generate(
unsigned int nevt,
const int * nbins, ROOT::Fit::BinData & data,
bool extend) {
116 if (NDim() == 0 || fFunc == 0 ) {
117 MATH_WARN_MSG(
"DistSampler::Generate",
"sampler has not been initialized correctly");
123 for (
unsigned int j = 0; j < NDim(); ++j) {
124 ntotbins *= nbins[j];
127 data.Append(ntotbins, NDim(), ROOT::Fit::BinData::kValueError);
129 std::vector<double> dx(NDim() );
130 std::vector<double> x(NDim() );
131 double binVolume = 1;
132 for (
unsigned int j = 0; j < dx.size(); ++j) {
133 double x1 = 0,x2 = 0;
134 if (!fRange || !fRange->Size(j)) {
135 MATH_WARN_MSG(
"DistSampler::Generate",
"sampler has not a range defined for all coordinates");
138 fRange->GetRange(j,x1,x2);
139 dx[j] = (x2-x1)/
double(nbins[j]);
140 assert(dx[j] > 0 && 1./dx[j] > 0 );
144 double nnorm = nevt * binVolume;
149 for (
int j = NDim()-1; j >=0; --j) {
150 for (
int i = 0; i < nbins[j]; ++i) {
154 double yval = (ParentPdf())(&x.front());
155 double nexp = yval * nnorm;
156 ret &= SampleBin(nexp,val,&eval);
157 data.Add(&x.front(), val, eval);
161 MATH_WARN_MSG(
"DistSampler::Generate",
"error returned from SampleBin");
167 MATH_WARN_MSG(
"DistSampler::Generate",
"generation with fixed events not yet impelmented");