ROOT  6.06/09
Reference Guide
DistSampler.cxx
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 // implementation file for class DistSampler
12 
13 #include "Math/DistSampler.h"
15 #include "Math/Error.h"
16 
17 #include "Math/IFunction.h"
18 #include "Fit/BinData.h"
19 #include "Fit/UnBinData.h"
20 #include "Fit/DataRange.h"
21 
22 
23 namespace ROOT {
24 
25 
26 namespace Math {
27 
29  // destructor
30  if (fOwnFunc && fFunc != 0) delete fFunc;
31  if (fRange) delete fRange;
32 }
33 
35  // default initialization with algorithm name
36  return Init(opt.Algorithm().c_str() );
37 }
38 
39 void DistSampler::SetRange(double xmin, double xmax, int icoord) {
40  if (!fRange) {
41  MATH_ERROR_MSG("DistSampler::SetRange","Need to set function before setting the range");
42  return;
43  }
44  fRange->SetRange(icoord,xmin,xmax);
45 }
46 
47 void DistSampler::SetRange(const double * xmin, const double * xmax) {
48  // set range specifying a vector for all coordinates
49  if (!fRange) {
50  MATH_ERROR_MSG("DistSampler::SetRange","Need to set function before setting the range");
51  return;
52  }
53  for (unsigned int icoord = 0; icoord < NDim(); ++icoord)
54  fRange->SetRange(icoord,xmin[icoord],xmax[icoord]);
55 }
56 
58  // copy the given range
59  *fRange = range;
60 }
61 
63  // set the internal function
64  // if a range exists and it is compatible it will be re-used
65  if (fOwnFunc && fFunc != 0) delete fFunc;
66  if (copy) {
67  fOwnFunc = true;
68  fFunc = func.Clone();
69  }
70  else {
71  fOwnFunc = false;
72  fFunc = &func;
73  }
74  fData = std::vector<double>(func.NDim());
75  // delete a range if exists and it is not compatible
76  if (fRange && fRange->NDim() != fData.size() ) {
77  delete fRange;
78  fRange = 0;
79  }
80  if (!fRange) fRange = new ROOT::Fit::DataRange(func.NDim() );
81 }
82 
84  // test if sampler is initialized
85  // tryying to generate one event (for this cannot be const)
86  if (NDim() == 0) return false;
87  if (fFunc == 0) return false;
88  if (fFunc->NDim() != NDim() ) return false;
89  // test one event
90  if (!Sample(&fData[0]) ) return false;
91  return true;
92 }
93 
94 bool DistSampler::Generate(unsigned int nevt, ROOT::Fit::UnBinData & data) {
95  // generate a un-binned data sets (fill the given data set)
96  // if dataset has already data append to it
97  int n0 = data.DataSize();
98  if (n0 > 0 ) {
99  if (data.PointSize() != NDim() ) {
100  MATH_ERROR_MSG("DistSampler::Generate","unbin data not consistent with distribution");
101  return false;
102  }
103  }
104  if (!IsInitialized()) {
105  MATH_WARN_MSG("DistSampler::Generate","sampler has not been initialized correctly");
106  return false;
107  }
108 
109  data.Initialize( n0 + nevt, NDim() );
110  for (unsigned int i = 0; i < nevt; ++i) {
111  const double * x = Sample();
112  data.Add( x );
113  }
114  return true;
115 }
116 
117 
118  bool DistSampler::Generate(unsigned int nevt, const int * nbins, ROOT::Fit::BinData & data, bool extend) {
119  // generate a bin data set from given bin center values
120  // bin center values must be present in given data set
121  //if (!IsInitialized()) {
122  if (NDim() == 0 || fFunc == 0 ) {
123  MATH_WARN_MSG("DistSampler::Generate","sampler has not been initialized correctly");
124  return false;
125  }
126 
127 
128  int ntotbins = 1;
129  for (unsigned int j = 0; j < NDim(); ++j) {
130  ntotbins *= nbins[j];
131  }
132 
133  data.Initialize(ntotbins, NDim(), ROOT::Fit::BinData::kValueError); // store always the error
134  // use for the moment bin center (should use bin integral)
135  std::vector<double> dx(NDim() );
136  std::vector<double> x(NDim() );
137  double binVolume = 1;
138  for (unsigned int j = 0; j < dx.size(); ++j) {
139  double x1 = 0,x2 = 0;
140  if (!fRange || !fRange->Size(j)) {
141  MATH_WARN_MSG("DistSampler::Generate","sampler has not a range defined for all coordinates");
142  return false;
143  }
144  fRange->GetRange(j,x1,x2);
145  dx[j] = (x2-x1)/double(nbins[j]);
146  assert(dx[j] > 0 && 1./dx[j] > 0 ); // avoid dx <= 0 and not inf
147  x[j] = x1 + dx[j]/2; // use bin centers
148  binVolume *= dx[j];
149  }
150  double nnorm = nevt * binVolume;
151 
152  if (extend) {
153 
154  bool ret = true;
155  for (int j = NDim()-1; j >=0; --j) {
156  for (int i = 0; i < nbins[j]; ++i) {
157  //const double * v = Sample();
158  double val = 0;
159  double eval = 0;
160  double yval = (ParentPdf())(&x.front());
161  double nexp = yval * nnorm;
162  ret &= SampleBin(nexp,val,&eval);
163  data.Add(&x.front(), val, eval);
164  x[j] += dx[j]; // increment x bin the bin
165  }
166  if (!ret) {
167  MATH_WARN_MSG("DistSampler::Generate","error returned from SampleBin");
168  return false;
169  }
170  }
171  }
172  else {
173  MATH_WARN_MSG("DistSampler::Generate","generation with fixed events not yet impelmented");
174  return false;
175  }
176  return true;
177 }
178 
179 } // end namespace Math
180 } // end namespace ROOT
void Initialize(unsigned int maxpoints, unsigned int dim=1, ErrorType err=kValueError)
preallocate a data set with given size , dimension and error type (to get the full point size) If the...
Definition: BinData.cxx:215
float xmin
Definition: THbookFile.cxx:93
std::vector< double > fData
Definition: DistSampler.h:258
unsigned int NDim() const
return the dimension of the parent distribution (and the data)
Definition: DistSampler.h:95
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
#define assert(cond)
Definition: unittest.h:542
const double * Sample()
sample one event and rerturning array x with coordinates
Definition: DistSampler.h:173
Class describing the unbinned data sets (just x coordinates values) of any dimensions.
Definition: UnBinData.h:47
const ROOT::Math::IMultiGenFunction & ParentPdf() const
get the parent distribution function (must be called after setting the function)
Definition: DistSampler.h:156
int nbins[3]
#define MATH_WARN_MSG(loc, str)
Definition: Error.h:47
virtual bool Generate(unsigned int nevt, ROOT::Fit::UnBinData &data)
generate a un-binned data sets (fill the given data set) if dataset has already data append to it ...
Definition: DistSampler.cxx:94
static const double x2[5]
Double_t x[n]
Definition: legend1.C:17
virtual ~DistSampler()
virtual destructor
Definition: DistSampler.cxx:28
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:50
unsigned int Size(unsigned int icoord=0) const
return range size for coordinate icoord (starts from zero) Size == 0 indicates no range is present [-...
Definition: DataRange.h:70
void SetRange(unsigned int icoord, double xmin, double xmax)
set a range [xmin,xmax] for the new coordinate icoord If more range exists for other coordinates...
Definition: DataRange.cxx:124
DistSampler options class.
virtual unsigned int NDim() const =0
Retrieve the dimension of the function.
ROOT::Fit::DataRange * fRange
Definition: DistSampler.h:259
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
unsigned int NDim() const
get range dimension
Definition: DataRange.h:64
void SetRange(double xmin, double xmax, int icoord=0)
set range in a given dimension
Definition: DistSampler.cxx:39
static const double x1[5]
class describing the range in the coordinates it supports multiple range in a coordinate.
Definition: DataRange.h:34
void Add(double x, double y)
add one dim data with only coordinate and values
Definition: BinData.cxx:264
void Add(double x)
add one dim coordinate data (unweighted)
Definition: UnBinData.h:199
double func(double *x, double *p)
Definition: stressTF1.cxx:213
Namespace for new Math classes and functions.
const std::string & Algorithm() const
type of algorithm
unsigned int PointSize() const
return point size.
Definition: UnBinData.h:327
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 DoSetFunction(const ROOT::Math::IMultiGenFunction &func, bool copy)
Definition: DistSampler.cxx:62
Documentation for the abstract class IBaseFunctionMultiDim.
Definition: IFunction.h:63
void GetRange(unsigned int icoord, double &xmin, double &xmax) const
get the first range for given coordinate.
Definition: DataRange.h:103
unsigned int DataSize() const
return size of internal data vector (is 0 for external data)
Definition: UnBinData.h:334
virtual IBaseFunctionMultiDim * Clone() const =0
Clone a function.
void Initialize(unsigned int maxpoints, unsigned int dim=1, bool isWeighted=false)
preallocate a data set given size and dimension of the coordinates if a vector already exists with co...
Definition: UnBinData.cxx:177