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