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