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