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