Logo ROOT   6.10/09
Reference Guide
FitData.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id: FitData.h 45076 2012-07-16 13:45:18Z mborinsk $
2 // Author: M. Borinsky
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_FitData
14 #define ROOT_Fit_FitData
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 #include "Fit/DataOptions.h"
26 #include "Fit/DataRange.h"
27 
28 #include <vector>
29 #include <cassert>
30 #include <iostream>
31 
32 
33 namespace ROOT {
34 
35  namespace Fit {
36 
37  //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)
38  template <class T>
39  struct DummyDeleter {
40  // a deleter not deleting the contained object
41  // used to avoid shared_ptr deleting the contained objects if managed externally
42  void operator()(T * /* p */)
43  {
44  //printf("ROOT::Fit::DummyDeleter called - do not delete object %x \n", p);
45  }
46  };
47 
48  /**
49  * Base class for all the fit data types:
50  * Stores the coordinates and the DataOptions
51 
52  @ingroup FitData
53  */
54 
55 
56  /**
57  class holding the fit data points. It is template on the type of point,
58  which can be for example a binned or unbinned point.
59  It is basicaly a wrapper on an std::vector
60 
61  @ingroup FitData
62 
63  */
64 
65  class FitData {
66  public:
67 
68  /// construct with default option and data range
69  explicit FitData(unsigned int maxpoints = 0, unsigned int dim = 1);
70 
71  /// construct passing options and default data range
72  explicit FitData(const DataOptions &opt, unsigned int maxpoints = 0, unsigned int dim = 1);
73 
74 
75  /// construct passing range and default options
76  explicit FitData(const DataRange &range, unsigned int maxpoints = 0, unsigned int dim = 1);
77 
78  /// construct passing options and data range
79  FitData(const DataOptions &opt, const DataRange &range,
80  unsigned int maxpoints = 0, unsigned int dim = 1);
81 
82  /// constructor from external data for 1D data
83  FitData(unsigned int n, const double *dataX);
84 
85  /// constructor from external data for 2D data
86  FitData(unsigned int n, const double *dataX, const double *dataY);
87 
88  /// constructor from external data for 3D data
89  FitData(unsigned int n, const double *dataX, const double *dataY,
90  const double *dataZ);
91 
92  /**
93  constructor for multi-dim external data and a range (data are copied inside according to the range)
94  Uses as argument an iterator of a list (or vector) containing the const double * of the data
95  An example could be the std::vector<const double *>::begin
96  */
97  FitData(const DataRange &range, unsigned int maxpoints, const double *dataX);
98 
99  /**
100  constructor for multi-dim external data and a range (data are copied inside according to the range)
101  Uses as argument an iterator of a list (or vector) containing the const double * of the data
102  An example could be the std::vector<const double *>::begin
103  */
104  FitData(const DataRange &range, unsigned int maxpoints, const double *dataX, const double *dataY);
105 
106  /**
107  constructor for multi-dim external data and a range (data are copied inside according to the range)
108  Uses as argument an iterator of a list (or vector) containing the const double * of the data
109  An example could be the std::vector<const double *>::begin
110  */
111  FitData(const DataRange &range, unsigned int maxpoints, const double *dataX, const double *dataY,
112  const double *dataZ);
113 
114  /**
115  constructor for multi-dim external data (data are not copied inside)
116  Uses as argument an iterator of a list (or vector) containing the const double * of the data
117  An example could be the std::vector<const double *>::begin
118  In case of weighted data, the external data must have a dim+1 lists of data
119  The apssed dim refers just to the coordinate size
120  */
121  template<class Iterator>
122  FitData(unsigned int n, unsigned int dim, Iterator dataItr) :
123  fWrapped(true),
124  fMaxPoints(n),
125  fNPoints(fMaxPoints),
126  fDim(dim),
127  fCoordsPtr(fDim),
128  fpTmpCoordVector(NULL)
129  {
130  assert(fDim >= 1);
131  for (unsigned int i = 0; i < fDim; i++) {
132  fCoordsPtr[i] = *dataItr++;
133  }
134 
135  if (fpTmpCoordVector) {
136  delete[] fpTmpCoordVector;
137  fpTmpCoordVector = NULL;
138  }
139 
140  fpTmpCoordVector = new double [fDim];
141  }
142 
143  /**
144  constructor for multi-dim external data and a range (data are copied inside according to the range)
145  Uses as argument an iterator of a list (or vector) containing the const double * of the data
146  An example could be the std::vector<const double *>::begin
147  */
148  template<class Iterator>
149  FitData(const DataRange &range, unsigned int maxpoints, unsigned int dim, Iterator dataItr) :
150  fWrapped(false),
151  fRange(range),
152  fMaxPoints(maxpoints),
153  fNPoints(0),
154  fDim(dim),
155  fpTmpCoordVector(NULL)
156  {
157  assert(fDim >= 1);
158  InitCoordsVector();
159 
160  InitFromRange(dataItr);
161  }
162 
163  /// dummy virtual destructor
164  virtual ~FitData();
165 
166  FitData(const FitData &rhs);
167 
168  FitData &operator= (const FitData &rhs);
169 
170  void Append(unsigned int newPoints, unsigned int dim = 1);
171 
172  protected:
173  /**
174  * initializer routines to set the corresponding pointers right
175  * The vectors must NOT be resized after this initialization
176  * without setting the corresponding pointers in the
177  * same moment ( has to be an atomic operation in case
178  * of multithreading ).
179  */
181  {
182  fCoords.resize(fDim);
183  fCoordsPtr.resize(fDim);
184 
185  for (unsigned int i = 0; i < fDim; i++) {
186  fCoords[i].resize(fMaxPoints);
187  fCoordsPtr[i] = &fCoords[i].front();
188  }
189 
190  if (fpTmpCoordVector) {
191  delete[] fpTmpCoordVector;
192  fpTmpCoordVector = NULL;
193  }
194 
195  fpTmpCoordVector = new double [fDim];
196  }
197 
198  template<class Iterator>
199  void InitFromRange(Iterator dataItr)
200  {
201  for (unsigned int i = 0; i < fMaxPoints; i++) {
202  bool isInside = true;
203  Iterator tmpItr = dataItr;
204 
205  for (unsigned int j = 0; j < fDim; j++)
206  isInside &= fRange.IsInside((*tmpItr++)[i], j);
207 
208  if (isInside) {
209  tmpItr = dataItr;
210 
211  for (unsigned int k = 0; k < fDim; k++)
212  fpTmpCoordVector[k] = (*tmpItr++)[i];
213 
214  Add(fpTmpCoordVector);
215  }
216  }
217  }
218 
219 
220  public:
221 
222  /**
223  returns a single coordinate component of a point.
224  This function is threadsafe in contrast to Coords(...)
225  and can easily get vectorized by the compiler in loops
226  running over the ipoint-index.
227  */
228  const double *GetCoordComponent(unsigned int ipoint, unsigned int icoord) const
229  {
230  assert(ipoint < fMaxPoints);
231  assert(icoord < fDim);
232  assert(fCoordsPtr.size() == fDim);
233  assert(fCoordsPtr[icoord]);
234  assert(fCoords.empty() || &fCoords[icoord].front() == fCoordsPtr[icoord]);
235 
236  return &fCoordsPtr[icoord][ipoint];
237  }
238 
239  /**
240  return a pointer to the coordinates data for the given fit point
241  */
242  // not threadsafe, to be replaced with never constructs!
243  // for example: just return std::array or std::vector, there's
244  // is going to be only minor overhead in c++11.
245  const double *Coords(unsigned int ipoint) const
246  {
247  assert(fpTmpCoordVector);
248  assert(ipoint < fMaxPoints);
249 
250  for (unsigned int i = 0; i < fDim; i++) {
251  assert(fCoordsPtr[i]);
252  assert(fCoords.empty() || &fCoords[i].front() == fCoordsPtr[i]);
253 
254  fpTmpCoordVector[i] = fCoordsPtr[i][ipoint];
255  }
256 
257  return fpTmpCoordVector;
258  }
259 
260  /**
261  add one dim data with only coordinate and values
262  */
263  void Add(double x)
264  {
265  assert(!fWrapped);
266  assert(!fCoordsPtr.empty() && fCoordsPtr.size() == 1 && fCoordsPtr[0]);
267  assert(1 == fDim);
268  assert(fNPoints < fMaxPoints);
269 
270  fCoords[0][ fNPoints ] = x;
271 
272  fNPoints++;
273  }
274 
275  /**
276  add multi-dim coordinate data with only value
277  */
278  void Add(const double *x)
279  {
280  assert(!fWrapped);
281  assert(!fCoordsPtr.empty() && fCoordsPtr.size() == fDim);
282  assert(fNPoints < fMaxPoints);
283 
284  for (unsigned int i = 0; i < fDim; i++) {
285  fCoords[i][ fNPoints ] = x[i];
286  }
287 
288  fNPoints++;
289  }
290 
291  /**
292  return number of fit points
293  */
294  unsigned int NPoints() const
295  {
296  return fNPoints;
297  }
298 
299  /**
300  return number of fit points
301  */
302  unsigned int Size() const
303  {
304  return fNPoints;
305  }
306 
307  /**
308  return coordinate data dimension
309  */
310  unsigned int NDim() const
311  {
312  return fDim;
313  }
314 
315  /**
316  access to options
317  */
318  const DataOptions &Opt() const
319  {
320  return fOptions;
321  }
323  {
324  return fOptions;
325  }
326 
327  /**
328  access to range
329  */
330  const DataRange &Range() const
331  {
332  return fRange;
333  }
334 
335  /**
336  direct access to coord data ptrs
337  */
338  const std::vector< const double * > &GetCoordDataPtrs() const
339  {
340  return fCoordsPtr;
341  }
342 
343 
344  protected:
345  void UnWrap()
346  {
347  assert(fWrapped);
348  assert(fCoords.empty());
349 
350  fCoords.resize(fDim);
351  for (unsigned int i = 0; i < fDim; i++) {
352  assert(fCoordsPtr[i]);
353  fCoords[i].resize(fNPoints);
354  std::copy(fCoordsPtr[i], fCoordsPtr[i] + fNPoints, fCoords[i].begin());
355  fCoordsPtr[i] = &fCoords[i].front();
356  }
357 
358  fWrapped = false;
359  }
360 
361  protected:
362  bool fWrapped;
363 
364  private:
365 
368 
369  protected:
370  unsigned int fMaxPoints;
371  unsigned int fNPoints;
372  unsigned int fDim;
373 
374  private:
375  /**
376  * This vector stores the vectorizable data:
377  * The inner vectors contain the coordinates data
378  * fCoords[0] is the vector for the x-coords
379  * fCoords[1] is the vector for the y-coords
380  * etc.
381  * The vector of pointers stores the pointers
382  * to the first elements of the corresponding
383  * elements
384  *
385  * If fWrapped is true, fCoords is empty.
386  * the data can only be accessed by using
387  * fCoordsPtr.
388  */
389  std::vector< std::vector< double > > fCoords;
390  std::vector< const double * > fCoordsPtr;
391 
392  double *fpTmpCoordVector; // non threadsafe stuff!
393 
394  };
395 
396  } // end namespace Fit
397 
398 } // end namespace ROOT
399 
400 
401 
402 #endif /* ROOT_Fit_Data */
403 
double * fpTmpCoordVector
Definition: FitData.h:392
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
double T(double x)
Definition: ChebyshevPol.h:34
FitData(unsigned int n, unsigned int dim, Iterator dataItr)
constructor for multi-dim external data (data are not copied inside) Uses as argument an iterator of ...
Definition: FitData.h:122
const std::vector< const double *> & GetCoordDataPtrs() const
direct access to coord data ptrs
Definition: FitData.h:338
Base class for all the fit data types: Stores the coordinates and the DataOptions.
Definition: FitData.h:65
std::vector< std::vector< double > > fCoords
This vector stores the vectorizable data: The inner vectors contain the coordinates data fCoords[0] i...
Definition: FitData.h:389
#define NULL
Definition: RtypesCore.h:88
const double * Coords(unsigned int ipoint) const
return a pointer to the coordinates data for the given fit point
Definition: FitData.h:245
void Add(const double *x)
add multi-dim coordinate data with only value
Definition: FitData.h:278
Double_t x[n]
Definition: legend1.C:17
const double * GetCoordComponent(unsigned int ipoint, unsigned int icoord) const
returns a single coordinate component of a point.
Definition: FitData.h:228
DataOptions fOptions
Definition: FitData.h:366
void operator()(T *)
Definition: FitData.h:42
unsigned int Size() const
return number of fit points
Definition: FitData.h:302
FitData(const DataRange &range, unsigned int maxpoints, unsigned int dim, Iterator dataItr)
constructor for multi-dim external data and a range (data are copied inside according to the range) U...
Definition: FitData.h:149
void InitCoordsVector()
initializer routines to set the corresponding pointers right The vectors must NOT be resized after th...
Definition: FitData.h:180
DataOptions : simple structure holding the options on how the data are filled.
Definition: DataOptions.h:28
DataRange fRange
Definition: FitData.h:367
void Add(double x)
add one dim data with only coordinate and values
Definition: FitData.h:263
const DataOptions & Opt() const
access to options
Definition: FitData.h:318
void Add(THist< DIMENSIONS, PRECISION_TO, STAT_TO... > &to, const THist< DIMENSIONS, PRECISION_FROM, STAT_FROM... > &from)
Add two histograms.
Definition: THist.hxx:336
std::vector< const double *> fCoordsPtr
Definition: FitData.h:390
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
unsigned int fNPoints
Definition: FitData.h:371
Binding & operator=(OUT(*fun)(void))
unsigned int fDim
Definition: FitData.h:372
unsigned int fMaxPoints
Definition: FitData.h:370
void InitFromRange(Iterator dataItr)
Definition: FitData.h:199
unsigned int NPoints() const
return number of fit points
Definition: FitData.h:294
unsigned int NDim() const
return coordinate data dimension
Definition: FitData.h:310
const DataRange & Range() const
access to range
Definition: FitData.h:330
DataOptions & Opt()
Definition: FitData.h:322
const Int_t n
Definition: legend1.C:16