Logo ROOT   6.08/07
Reference Guide
UnBinData.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Wed Aug 30 11:15:23 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class UnBinData
12 
13 #ifndef ROOT_Fit_UnBinData
14 #define ROOT_Fit_UnBinData
15 
16 #ifndef ROOT_Fit_DataVector
17 #include "Fit/DataVector.h"
18 #endif
19 
20 #ifndef ROOT_Math_Error
21 #include "Math/Error.h"
22 #endif
23 
24 
25 
26 namespace ROOT {
27 
28  namespace Fit {
29 
30 
31 //___________________________________________________________________________________
32 /**
33  Class describing the unbinned data sets (just x coordinates values) of any dimensions
34 
35  There is the option to construct UnBindata copying the data in (using the DataVector class)
36  or using pointer to external data (DataWrapper) class.
37  In general is found to be more efficient to copy the data.
38  In case of really large data sets for limiting memory consumption then the other option can be used
39  Specialized constructor exists for using external data up to 3 dimensions.
40 
41  When the data are copying in the number of points can be set later (or re-set) using Initialize and
42  the data are inserted one by one using the Add method.
43  It is mandatory to set the size before using the Add method.
44 
45  @ingroup FitData
46 */
47 class UnBinData : public FitData {
48 
49 public :
50 
51  /**
52  constructor from dimension of point and max number of points (to pre-allocate vector)
53  */
54 
55  explicit UnBinData(unsigned int maxpoints = 0, unsigned int dim = 1, bool isWeighted = false );
56 
57 
58  /**
59  constructor from range and default option
60  */
61  explicit UnBinData (const DataRange & range, unsigned int maxpoints = 0, unsigned int dim = 1, bool isWeighted = false);
62 
63  /**
64  constructor from options and range
65  */
66  UnBinData (const DataOptions & opt, const DataRange & range, unsigned int maxpoints = 0, unsigned int dim = 1, bool isWeighted = false );
67 
68  /**
69  constructor for 1D external data (data are not copied inside)
70  */
71  UnBinData(unsigned int n, const double * dataX );
72 
73  /**
74  constructor for 2D external data (data are not copied inside)
75  or 1D data with a weight (if isWeighted = true)
76  */
77  UnBinData(unsigned int n, const double * dataX, const double * dataY, bool isWeighted = false );
78 
79  /**
80  constructor for 3D external data (data are not copied inside)
81  or 2D data with a weight (if isWeighted = true)
82  */
83  UnBinData(unsigned int n, const double * dataX, const double * dataY, const double * dataZ, bool isWeighted = false );
84 
85  /**
86  constructor for multi-dim external data (data are not copied inside)
87  Uses as argument an iterator of a list (or vector) containing the const double * of the data
88  An example could be the std::vector<const double *>::begin
89  In case of weighted data, the external data must have a dim+1 lists of data
90  The apssed dim refers just to the coordinate size
91  */
92  template<class Iterator>
93  UnBinData(unsigned int n, unsigned int dim, Iterator dataItr, bool isWeighted = false ) :
94  FitData( ),
95  fDim(dim),
96  fPointSize( (isWeighted) ? dim +1 : dim),
97  fNPoints(n),
98  fDataVector(0)
99  {
100  fDataWrapper = new DataWrapper(fPointSize, dataItr);
101  }
102 
103  /**
104  constructor for 1D data and a range (data are copied inside according to the given range)
105  */
106  UnBinData(unsigned int maxpoints, const double * dataX, const DataRange & range);
107 
108  /**
109  constructor for 2D data and a range (data are copied inside according to the given range)
110  or 1 1D data set + weight. If is weighted dataY is the pointer to the list of the weights
111  */
112  UnBinData(unsigned int maxpoints, const double * dataX, const double * dataY, const DataRange & range, bool isWeighted = false);
113 
114  /**
115  constructor for 3D data and a range (data are copied inside according to the given range)
116  or a 2D data set + weights. If is weighted dataZ is the pointer to the list of the weights
117  */
118  UnBinData(unsigned int maxpoints, const double * dataX, const double * dataY, const double * dataZ, const DataRange & range, bool isWeighted = false);
119 
120  /**
121  constructor for multi-dim external data and a range (data are copied inside according to the range)
122  Uses as argument an iterator of a list (or vector) containing the const double * of the data
123  An example could be the std::vector<const double *>::begin
124  */
125  template<class Iterator>
126  UnBinData(unsigned int maxpoints, unsigned int dim, Iterator dataItr, const DataRange & range, bool isWeighted = false ) :
127  FitData( ),
128  fDim(dim),
129  fPointSize( (isWeighted) ? dim +1 : dim),
130  fNPoints(0),
131  fDataVector(0),
132  fDataWrapper(0)
133  {
134  unsigned int n = fPointSize*maxpoints;
135  if ( n > MaxSize() ) {
136  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n );
137  }
138  else if (n > 0) {
139  fDataVector = new DataVector(n);
140 
141  // use data wrapper to get the data
142  ROOT::Fit::DataWrapper wdata(fPointSize, dataItr);
143  for (unsigned int i = 0; i < maxpoints; ++i) {
144  bool isInside = true;
145  for (unsigned int icoord = 0; icoord < dim; ++icoord)
146  isInside &= range.IsInside( wdata.Coords(i)[icoord], icoord );
147  // treat here the weight as an extra coordinate
148  if ( isInside ) Add(wdata.Coords(i));
149  }
150  if (fNPoints < maxpoints) (fDataVector->Data()).resize(fPointSize*fNPoints);
151  }
152  }
153 
154 
155 private:
156  /// copy constructor (private)
157  UnBinData(const UnBinData &) : FitData() {}
158  /// assignment operator (private)
159  UnBinData & operator= (const UnBinData &) { return *this; }
160 
161 public:
162 
163 #ifdef LATER
164  /**
165  Create from a compatible UnBinData set
166  */
167 
168  UnBinData (const UnBinData & data , const DataOptions & opt, const DataRange & range) :
169  DataVector(opt,range, data.DataSize() ),
170  fDim(data.fDim),
171  fPointSize(data.fPointSize),
172  fNPoints(data.fNPoints)
173  {
174 // for (Iterator itr = begin; itr != end; ++itr)
175 // if (itr->IsInRange(range) )
176 // Add(*itr);
177  }
178 #endif
179 
180  /**
181  destructor, delete pointer to internal data or external data wrapper
182  */
183  virtual ~UnBinData() {
184  if (fDataVector) delete fDataVector;
185  if (fDataWrapper) delete fDataWrapper;
186  }
187 
188  /**
189  preallocate a data set given size and dimension of the coordinates
190  if a vector already exists with correct dimension (point size) extend the existing one
191  to a total size of maxpoints (equivalent to a Resize)
192  */
193  void Initialize(unsigned int maxpoints, unsigned int dim = 1, bool isWeighted = false);
194 
195 
196  /**
197  add one dim coordinate data (unweighted)
198  */
199  void Add(double x) {
200  int index = fNPoints*PointSize();
201  assert(fDataVector != 0);
202  assert(PointSize() == 1);
203  assert (index + PointSize() <= DataSize() );
204 
205  (fDataVector->Data())[ index ] = x;
206 
207  fNPoints++;
208  }
209 
210 
211  /**
212  add 2-dim coordinate data
213  can also be used to add 1-dim data with a weight
214  */
215  void Add(double x, double y) {
216  int index = fNPoints*PointSize();
217  assert(fDataVector != 0);
218  assert(PointSize() == 2);
219  assert (index + PointSize() <= DataSize() );
220 
221  (fDataVector->Data())[ index ] = x;
222  (fDataVector->Data())[ index+1 ] = y;
223 
224  fNPoints++;
225  }
226 
227  /**
228  add 3-dim coordinate data
229  can also be used to add 2-dim data with a weight
230  */
231  void Add(double x, double y, double z) {
232  int index = fNPoints*PointSize();
233  assert(fDataVector != 0);
234  assert(PointSize() == 3);
235  assert (index + PointSize() <= DataSize() );
236 
237  (fDataVector->Data())[ index ] = x;
238  (fDataVector->Data())[ index+1 ] = y;
239  (fDataVector->Data())[ index+2 ] = z;
240 
241  fNPoints++;
242  }
243 
244  /**
245  add multi-dim coordinate data
246  */
247  void Add(const double *x) {
248  int index = fNPoints*fPointSize;
249 
250  assert(fDataVector != 0);
251  assert (index + PointSize() <= DataSize() );
252 
253  double * itr = &( (fDataVector->Data()) [ index ]);
254 
255  for (unsigned int i = 0; i < fDim; ++i)
256  *itr++ = x[i];
257 
258  fNPoints++;
259  }
260 
261  /**
262  add multi-dim coordinate data + weight
263  */
264  void Add(const double *x, double w) {
265  int index = fNPoints*fPointSize;
266 
267  assert(fDataVector != 0);
268  assert (index + PointSize() <= DataSize() );
269 
270  double * itr = &( (fDataVector->Data()) [ index ]);
271 
272  for (unsigned int i = 0; i < fDim; ++i)
273  *itr++ = x[i];
274  *itr = w;
275 
276  fNPoints++;
277  }
278 
279  /**
280  return pointer to coordinate data
281  */
282  const double * Coords(unsigned int ipoint) const {
283  if (fDataVector)
284  return &( (fDataVector->Data()) [ ipoint*fPointSize ] );
285  else
286  return fDataWrapper->Coords(ipoint);
287  }
288 
289  bool IsWeighted() const {
290  return (fPointSize == fDim+1);
291  }
292 
293  double Weight(unsigned int ipoint) const {
294  if (fPointSize == fDim) return 1;
295  if (fDataVector )
296  return (fDataVector->Data()) [ ipoint*fPointSize + 1 ] ;
297  else
298  return 0; // weights are not supported for wrapper data sets
299  }
300 
301 
302  /**
303  resize the vector to the given npoints
304  */
305  void Resize (unsigned int npoints);
306 
307 
308  /**
309  return number of contained points
310  */
311  unsigned int NPoints() const { return fNPoints; }
312 
313  /**
314  return number of contained points
315  */
316  unsigned int Size() const { return fNPoints; }
317 
318  /**
319  return coordinate data dimension
320  */
321  unsigned int NDim() const { return fDim; }
322 
323  /**
324  return point size. For unweighted data is equivalent to coordinate dimension,
325  for weighted data is NDim()+1
326  */
327  unsigned int PointSize() const {
328  return fPointSize;
329  }
330 
331  /**
332  return size of internal data vector (is 0 for external data)
333  */
334  unsigned int DataSize() const {
335  return (fDataVector) ? fDataVector->Size() : 0;
336  }
337 
338 
339 protected:
340 
341  void SetNPoints(unsigned int n) { fNPoints = n; }
342 
343 private:
344 
345  unsigned int fDim; // coordinate data dimension
346  unsigned int fPointSize; // poit size dimension (coordinate + weight)
347  unsigned int fNPoints; // numer of fit points
348 
349  DataVector * fDataVector; // pointer to internal data vector (null for external data)
350  DataWrapper * fDataWrapper; // pointer to structure wrapping external data (null when data are copied in)
351 
352 };
353 
354 
355  } // end namespace Fit
356 
357 } // end namespace ROOT
358 
359 
360 
361 #endif /* ROOT_Fit_UnBinData */
const double * Coords(unsigned int ipoint) const
Definition: DataVector.h:332
UnBinData(unsigned int n, unsigned int dim, Iterator dataItr, bool isWeighted=false)
constructor for multi-dim external data (data are not copied inside) Uses as argument an iterator of ...
Definition: UnBinData.h:93
unsigned int NDim() const
return coordinate data dimension
Definition: UnBinData.h:321
size_t Size() const
full size of data vector (npoints * point size)
Definition: DataVector.h:197
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
void SetNPoints(unsigned int n)
Definition: UnBinData.h:341
void Resize(unsigned int npoints)
resize the vector to the given npoints
Definition: UnBinData.cxx:200
Base class for all the fit data types.
Definition: DataVector.h:67
Class describing the unbinned data sets (just x coordinates values) of any dimensions.
Definition: UnBinData.h:47
DataWrapper * fDataWrapper
Definition: UnBinData.h:350
#define MATH_ERROR_MSGVAL(loc, str, x)
Definition: Error.h:69
bool IsInside(double x, unsigned int icoord=0) const
check if a point is inside the range for the given coordinate
Definition: DataRange.cxx:146
Double_t x[n]
Definition: legend1.C:17
class holding the fit data points.
Definition: DataVector.h:134
const FData & Data() const
const access to underlying vector
Definition: DataVector.h:163
void Add(double x, double y)
add 2-dim coordinate data can also be used to add 1-dim data with a weight
Definition: UnBinData.h:215
unsigned int DataSize() const
return size of internal data vector (is 0 for external data)
Definition: UnBinData.h:334
unsigned int fDim
Definition: UnBinData.h:345
DataVector * fDataVector
Definition: UnBinData.h:349
void Add(double x, double y, double z)
add 3-dim coordinate data can also be used to add 2-dim data with a weight
Definition: UnBinData.h:231
unsigned int fPointSize
Definition: UnBinData.h:346
class maintaining a pointer to external data Using this class avoids copying the data when performing...
Definition: DataVector.h:222
void Add(const double *x, double w)
add multi-dim coordinate data + weight
Definition: UnBinData.h:264
bool IsWeighted() const
Definition: UnBinData.h:289
double Weight(unsigned int ipoint) const
Definition: UnBinData.h:293
DataOptions : simple structure holding the options on how the data are filled.
Definition: DataOptions.h:28
UnBinData & operator=(const UnBinData &)
assignment operator (private)
Definition: UnBinData.h:159
UnBinData(const UnBinData &)
copy constructor (private)
Definition: UnBinData.h:157
UnBinData(unsigned int maxpoints, unsigned int dim, Iterator dataItr, const DataRange &range, bool isWeighted=false)
constructor for multi-dim external data and a range (data are copied inside according to the range) U...
Definition: UnBinData.h:126
unsigned int NPoints() const
return number of contained points
Definition: UnBinData.h:311
virtual ~UnBinData()
destructor, delete pointer to internal data or external data wrapper
Definition: UnBinData.h:183
const double * Coords(unsigned int ipoint) const
return pointer to coordinate data
Definition: UnBinData.h:282
class describing the range in the coordinates it supports multiple range in a coordinate.
Definition: DataRange.h:34
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition: HFitImpl.cxx:134
void Add(double x)
add one dim coordinate data (unweighted)
Definition: UnBinData.h:199
Double_t y[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
static unsigned int MaxSize()
define a max size to avoid allocating too large arrays
Definition: DataVector.h:111
unsigned int fNPoints
Definition: UnBinData.h:347
void Add(const double *x)
add multi-dim coordinate data
Definition: UnBinData.h:247
unsigned int PointSize() const
return point size.
Definition: UnBinData.h:327
const Int_t n
Definition: legend1.C:16
unsigned int Size() const
return number of contained points
Definition: UnBinData.h:316
UnBinData(unsigned int maxpoints=0, unsigned int dim=1, bool isWeighted=false)
constructor from dimension of point and max number of points (to pre-allocate vector) ...
Definition: UnBinData.cxx:24
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