Logo ROOT   6.08/07
Reference Guide
DataVector.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 DataVector
12 
13 #ifndef ROOT_Fit_DataVector
14 #define ROOT_Fit_DataVector
15 
16 /**
17 @defgroup FitData Fit Data Classes
18 
19 Classes for describing the input data for fitting
20 
21 @ingroup Fit
22 */
23 
24 
25 // #ifndef ROOT_Fit_DataVectorfwd
26 // #include "Fit/DataVectorfwd.h"
27 // #endif
28 
29 #ifndef ROOT_Fit_DataOptions
30 #include "Fit/DataOptions.h"
31 #endif
32 
33 
34 #ifndef ROOT_Fit_DataRange
35 #include "Fit/DataRange.h"
36 #endif
37 
38 
39 #include <vector>
40 #include <cassert>
41 #include <iostream>
42 
43 
44 namespace ROOT {
45 
46  namespace Fit {
47 
48 
49  //class used for making shared_ptr of data classes managed by the user (i.e. when we don;t want to delete the contained object)
50  template <class T>
51  struct DummyDeleter
52  {
53  // a deleter not deleting the contained object
54  // used to avoid shared_ptr deleting the contained objects if managed externally
55  void operator()(T* /* p */) {
56  //printf("ROOT::Fit::DummyDeleter called - do not delete object %x \n", p);
57  }
58  };
59 
60 
61 /**
62  Base class for all the fit data types
63 
64  @ingroup FitData
65  */
66 
67 class FitData {
68 
69 public:
70 
71  /// construct with default option and data range
72  FitData() {}
73 
74  /// dummy virtual destructor
75  virtual ~FitData() {}
76 
77  /// construct passing options and default data range
78  FitData(const DataOptions & opt) :
79  fOptions(opt)
80  {}
81 
82 
83  /// construct passing range and default options
84  FitData(const DataRange & range) :
85  fRange(range)
86  {}
87 
88  /// construct passing options and data range
89  FitData (const DataOptions & opt, const DataRange & range) :
90  fOptions(opt),
91  fRange(range)
92  {}
93 
94  /**
95  access to options
96  */
97  const DataOptions & Opt() const { return fOptions; }
98  DataOptions & Opt() { return fOptions; }
99 
100  /**
101  access to range
102  */
103  const DataRange & Range() const { return fRange; }
104 
105  // range cannot be modified afterwards
106  // since fit method functions use all data
107 
108  /**
109  define a max size to avoid allocating too large arrays
110  */
111  static unsigned int MaxSize() {
112  return (unsigned int) (-1) / sizeof (double);
113  }
114 
115 
116 private:
117 
120 
121 };
122 
123 
124 
125 /**
126  class holding the fit data points. It is template on the type of point,
127  which can be for example a binned or unbinned point.
128  It is basicaly a wrapper on an std::vector
129 
130  @ingroup FitData
131 
132 */
133 
134 class DataVector {
135 
136 public:
137 
138 
139  typedef std::vector<double> FData;
140 
141  /**
142  default constructor for a vector of N -data
143  */
144  explicit DataVector (size_t n ) :
145  fData(std::vector<double>(n))
146 
147  {
148  //if (n!=0) fData.reserve(n);
149  }
150 
151 
152  /**
153  Destructor (no operations)
154  */
156 
157  // use default copy constructor and assignment operator
158 
159 
160  /**
161  const access to underlying vector
162  */
163  const FData & Data() const { return fData; }
164 
165  /**
166  non-const access to underlying vector (in case of insertion/deletion) and iterator
167  */
168  FData & Data() { return fData; }
169 
170 #ifndef __CINT__
171  /**
172  const iterator access
173  */
174  typedef FData::const_iterator const_iterator;
175  typedef FData::iterator iterator;
176 
177  const_iterator begin() const { return fData.begin(); }
178  const_iterator end() const { return fData.begin()+fData.size(); }
179 
180  /**
181  non-const iterator access
182  */
183  iterator begin() { return fData.begin(); }
184  iterator end() { return fData.end(); }
185 
186 #endif
187  /**
188  access to the point
189  */
190  const double & operator[] (unsigned int i) const { return fData[i]; }
191  double & operator[] (unsigned int i) { return fData[i]; }
192 
193 
194  /**
195  full size of data vector (npoints * point size)
196  */
197  size_t Size() const { return fData.size(); }
198 
199 
200 private:
201 
202  FData fData;
203 };
204 
205 
206 // // usefule typedef's of DataVector
207 // class BinPoint;
208 
209 // // declaration for various type of data vectors
210 // typedef DataVector<ROOT::Fit::BinPoint> BinData;
211 // typedef DataVector<ROOT::Fit::BinPoint>::const_iterator BinDataIterator;
212 
213 /**
214  class maintaining a pointer to external data
215  Using this class avoids copying the data when performing a fit
216  NOTE: this class is not thread-safe and should not be used in parallel fits
217 
218 
219  @ingroup FitData
220  */
221 
222 class DataWrapper {
223 
224 public:
225 
226  /**
227  specialized constructor for 1D data without errors and values
228  */
229  explicit DataWrapper(const double * dataX ) :
230  fDim(1),
231  fValues(0),
232  fErrors(0),
233  fCoords(std::vector<const double * >(1) ),
234  fX(std::vector<double>(1) )
235  {
236  fCoords[0] = dataX;
237  }
238 
239 
240  /**
241  constructor for 1D data (if errors are not present a null pointer should be passed)
242  */
243  DataWrapper(const double * dataX, const double * val, const double * eval , const double * ex ) :
244  fDim(1),
245  fValues(val),
246  fErrors(eval),
247  fCoords(std::vector<const double * >(1) ),
248  fErrCoords(std::vector<const double * >(1) ),
249  fX(std::vector<double>(1) ),
250  fErr(std::vector<double>(1) )
251  {
252  fCoords[0] = dataX;
253  fErrCoords[0] = ex;
254  }
255 
256  /**
257  constructor for 2D data (if errors are not present a null pointer should be passed)
258  */
259  DataWrapper(const double * dataX, const double * dataY, const double * val, const double * eval, const double * ex , const double * ey ) :
260  fDim(2),
261  fValues(val),
262  fErrors(eval),
263  fCoords(std::vector<const double * >(2) ),
264  fErrCoords(std::vector<const double * >(2) ),
265  fX(std::vector<double>(2) ),
266  fErr(std::vector<double>(2) )
267  {
268  fCoords[0] = dataX;
269  fCoords[1] = dataY;
270  fErrCoords[0] = ex;
271  fErrCoords[1] = ey;
272  }
273 
274  /**
275  constructor for 3D data (if errors are not present a null pointer should be passed)
276  */
277  DataWrapper(const double * dataX, const double * dataY, const double * dataZ, const double * val, const double * eval, const double * ex , const double * ey, const double * ez ) :
278  fDim(3),
279  fValues(val),
280  fErrors(eval),
281  fCoords(std::vector<const double * >(3) ),
282  fErrCoords(std::vector<const double * >(3) ),
283  fX(std::vector<double>(3) ),
284  fErr(std::vector<double>(3) )
285  {
286  fCoords[0] = dataX;
287  fCoords[1] = dataY;
288  fCoords[2] = dataZ;
289  fErrCoords[0] = ex;
290  fErrCoords[1] = ey;
291  fErrCoords[2] = ez;
292  }
293 
294  /**
295  constructor for multi-dim data without errors
296  */
297  template<class Iterator>
298  DataWrapper(unsigned int dim, Iterator coordItr ) :
299  fDim(dim),
300  fValues(0),
301  fErrors(0),
302  fCoords(std::vector<const double * >(coordItr, coordItr+dim) ),
303  fX(std::vector<double>(dim) )
304  { }
305 
306 
307  /**
308  constructor for multi-dim data with errors and values (if errors are not present a null pointer should be passed)
309  */
310  template<class Iterator>
311  DataWrapper(size_t dim, Iterator coordItr, const double * val, const double * eval, Iterator errItr ) :
312  // use size_t for dim to avoid allocating huge vector on 64 bits when dim=-1
313  fDim(dim),
314  fValues(val),
315  fErrors(eval),
316  fCoords(std::vector<const double * >(coordItr, coordItr+dim) ),
317  fErrCoords(std::vector<const double * >(errItr, errItr+dim) ),
318  fX(std::vector<double>(dim) ),
319  fErr(std::vector<double>(dim) )
320  { }
321 
322  // destructor
324  //printf("Delete Data wrapper\n");
325  // no operations
326  }
327 
328  // use default copy constructor and assignment operator
329  // copy the pointer of the data not the data
330 
331 
332  const double * Coords(unsigned int ipoint) const {
333  for (unsigned int i = 0; i < fDim; ++i) {
334  const double * x = fCoords[i];
335  assert (x != 0);
336  fX[i] = x[ipoint];
337  }
338  return &fX.front();
339  }
340 
341  double Coord(unsigned int ipoint, unsigned int icoord) const {
342  const double * x = fCoords[icoord];
343  assert (x != 0);
344  return x[ipoint];
345  }
346 
347 
348  const double * CoordErrors(unsigned int ipoint) const {
349  for (unsigned int i = 0; i < fDim; ++i) {
350  const double * err = fErrCoords[i];
351  if (err == 0) return 0;
352  fErr[i] = err[ipoint];
353  }
354  return &fErr.front();
355  }
356 
357  double CoordError(unsigned int ipoint, unsigned int icoord) const {
358  const double * err = fErrCoords[icoord];
359  return (err != 0) ? err[ipoint] : 0;
360  }
361 
362 
363  double Value(unsigned int ipoint) const {
364  return fValues[ipoint];
365  }
366 
367  double Error(unsigned int ipoint) const {
368  return (fErrors) ? fErrors[ipoint] : 0. ;
369  }
370 
371 
372 
373 private:
374 
375 
376  unsigned int fDim;
377  const double * fValues;
378  const double * fErrors;
379  std::vector<const double *> fCoords;
380  std::vector<const double *> fErrCoords;
381  // cached vector to return x[] and errors on x
382  mutable std::vector<double> fX;
383  mutable std::vector<double> fErr;
384 
385 };
386 
387 
388 
389  } // end namespace Fit
390 
391 } // end namespace ROOT
392 
393 
394 
395 #endif /* ROOT_Fit_DataVector */
DataWrapper(unsigned int dim, Iterator coordItr)
constructor for multi-dim data without errors
Definition: DataVector.h:298
const double * Coords(unsigned int ipoint) const
Definition: DataVector.h:332
double CoordError(unsigned int ipoint, unsigned int icoord) const
Definition: DataVector.h:357
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
FData & Data()
non-const access to underlying vector (in case of insertion/deletion) and iterator ...
Definition: DataVector.h:168
FData::const_iterator const_iterator
const iterator access
Definition: DataVector.h:174
double Value(unsigned int ipoint) const
Definition: DataVector.h:363
const double * CoordErrors(unsigned int ipoint) const
Definition: DataVector.h:348
double T(double x)
Definition: ChebyshevPol.h:34
FData::iterator iterator
Definition: DataVector.h:175
std::vector< double > FData
Definition: DataVector.h:139
Base class for all the fit data types.
Definition: DataVector.h:67
FitData(const DataOptions &opt)
construct passing options and default data range
Definition: DataVector.h:78
const double * fErrors
Definition: DataVector.h:378
STL namespace.
FitData()
construct with default option and data range
Definition: DataVector.h:72
double Coord(unsigned int ipoint, unsigned int icoord) const
Definition: DataVector.h:341
DataWrapper(const double *dataX, const double *dataY, const double *dataZ, const double *val, const double *eval, const double *ex, const double *ey, const double *ez)
constructor for 3D data (if errors are not present a null pointer should be passed) ...
Definition: DataVector.h:277
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
DataVector(size_t n)
default constructor for a vector of N -data
Definition: DataVector.h:144
DataOptions fOptions
Definition: DataVector.h:118
const double * fValues
Definition: DataVector.h:377
~DataVector()
Destructor (no operations)
Definition: DataVector.h:155
DataWrapper(const double *dataX, const double *val, const double *eval, const double *ex)
constructor for 1D data (if errors are not present a null pointer should be passed) ...
Definition: DataVector.h:243
iterator begin()
non-const iterator access
Definition: DataVector.h:183
std::vector< double > fX
Definition: DataVector.h:382
class maintaining a pointer to external data Using this class avoids copying the data when performing...
Definition: DataVector.h:222
FitData(const DataRange &range)
construct passing range and default options
Definition: DataVector.h:84
DataWrapper(const double *dataX)
specialized constructor for 1D data without errors and values
Definition: DataVector.h:229
DataWrapper(const double *dataX, const double *dataY, const double *val, const double *eval, const double *ex, const double *ey)
constructor for 2D data (if errors are not present a null pointer should be passed) ...
Definition: DataVector.h:259
DataOptions : simple structure holding the options on how the data are filled.
Definition: DataOptions.h:28
DataRange fRange
Definition: DataVector.h:119
const DataOptions & Opt() const
access to options
Definition: DataVector.h:97
std::vector< const double * > fErrCoords
Definition: DataVector.h:380
FitData(const DataOptions &opt, const DataRange &range)
construct passing options and data range
Definition: DataVector.h:89
class describing the range in the coordinates it supports multiple range in a coordinate.
Definition: DataRange.h:34
const_iterator end() const
Definition: DataVector.h:178
virtual ~FitData()
dummy virtual destructor
Definition: DataVector.h:75
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
Double_t ey[n]
Definition: legend1.C:17
static unsigned int MaxSize()
define a max size to avoid allocating too large arrays
Definition: DataVector.h:111
std::vector< double > fErr
Definition: DataVector.h:383
const_iterator begin() const
Definition: DataVector.h:177
std::vector< const double * > fCoords
Definition: DataVector.h:379
const DataRange & Range() const
access to range
Definition: DataVector.h:103
DataOptions & Opt()
Definition: DataVector.h:98
DataWrapper(size_t dim, Iterator coordItr, const double *val, const double *eval, Iterator errItr)
constructor for multi-dim data with errors and values (if errors are not present a null pointer shoul...
Definition: DataVector.h:311
Double_t ex[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
double Error(unsigned int ipoint) const
Definition: DataVector.h:367