ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
UnBinData.cxx
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Wed Aug 30 11:10:03 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Implementation file for class UnBinData
12 
13 #include "Fit/UnBinData.h"
14 #include "Fit/DataVector.h"
15 #include "Math/Error.h"
16 
17 #include <cassert>
18 #include <cmath>
19 
20 namespace ROOT {
21 
22  namespace Fit {
23 
24 UnBinData::UnBinData(unsigned int maxpoints, unsigned int dim, bool isWeighted ) :
25  FitData(),
26  fDim(dim),
27  fPointSize( (isWeighted) ? dim +1 : dim),
28  fNPoints(0),
29  fDataVector(0),
30  fDataWrapper(0)
31 {
32  // constructor with default option and range
33  unsigned int n = fPointSize*maxpoints;
34  if ( n > MaxSize() )
35  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n )
36  else if (n > 0)
37  fDataVector = new DataVector(n);
38 }
39 
40 UnBinData::UnBinData (const DataRange & range, unsigned int maxpoints , unsigned int dim, bool isWeighted ) :
41  FitData(range),
42  fDim(dim),
43  fPointSize( (isWeighted) ? dim +1 : dim),
44  fNPoints(0),
45  fDataVector(0),
46  fDataWrapper(0)
47 {
48  // constructor from option and default range
49  unsigned int n = fPointSize*maxpoints;
50  if ( n > MaxSize() )
51  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n )
52  else if (n > 0)
53  fDataVector = new DataVector(n);
54 }
55 
56 UnBinData::UnBinData (const DataOptions & opt, const DataRange & range, unsigned int maxpoints, unsigned int dim, bool isWeighted) :
57  FitData( opt, range),
58  fDim(dim),
59  fPointSize( (isWeighted) ? dim +1 : dim),
60  fNPoints(0),
61  fDataVector(0),
62  fDataWrapper(0)
63 {
64  // constructor from options and range
65  unsigned int n = fPointSize*maxpoints;
66  if ( n > MaxSize() )
67  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n )
68  else if (n > 0)
69  fDataVector = new DataVector(n);
70 }
71 
72 UnBinData::UnBinData(unsigned int n, const double * dataX ) :
73  FitData( ),
74  fDim(1),
75  fPointSize(1),
76  fNPoints(n),
77  fDataVector(0)
78 {
79  // constructor for 1D external data
80  fDataWrapper = new DataWrapper(dataX);
81 }
82 
83 UnBinData::UnBinData(unsigned int n, const double * dataX, const double * dataY, bool isWeighted ) :
84  FitData( ),
85  fDim( (isWeighted) ? 1 : 2),
86  fPointSize(2),
87  fNPoints(n),
88  fDataVector(0),
89  fDataWrapper(0)
90 {
91  // constructor for 2D external data
92  fDataWrapper = new DataWrapper(dataX, dataY, 0, 0, 0, 0);
93 }
94 
95 UnBinData::UnBinData(unsigned int n, const double * dataX, const double * dataY, const double * dataZ, bool isWeighted ) :
96  FitData( ),
97  fDim( (isWeighted) ? 2 : 3),
98  fPointSize(3),
99  fNPoints(n),
100  fDataVector(0)
101 {
102  // constructor for 3D external data
103  fDataWrapper = new DataWrapper(dataX, dataY, dataZ, 0, 0, 0, 0, 0);
104 }
105 
106 UnBinData::UnBinData(unsigned int n, const double * dataX, const DataRange & range ) :
107  FitData(range),
108  fDim(1),
109  fPointSize(1),
110  fNPoints(0),
111  fDataVector(0),
112  fDataWrapper(0)
113 {
114  // constructor for 1D array data using a range to select the data
115  // copy the data inside
116  // when n = 0 construct just an empty data set
117  if ( n > MaxSize() )
118  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n )
119  else if (n > 0) {
120  fDataVector = new DataVector(n);
121 
122  for (unsigned int i = 0; i < n; ++i)
123  if ( range.IsInside(dataX[i]) ) Add(dataX[i] );
124 
125  if (fNPoints < n) (fDataVector->Data()).resize(fNPoints);
126  }
127 }
128 
129 UnBinData::UnBinData(unsigned int n, const double * dataX, const double * dataY, const DataRange & range, bool isWeighted ) :
130  FitData(range),
131  fDim( (isWeighted) ? 1 : 2 ),
132  fPointSize(2),
133  fNPoints(0),
134  fDataVector(0),
135  fDataWrapper(0)
136 {
137  // constructor for 2D array data using a range to select the data
138  // copy the data inside
139  if ( n > MaxSize() )
140  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n )
141  else if (n > 0) {
142  fDataVector = new DataVector(2*n);
143 
144  for (unsigned int i = 0; i < n; ++i)
145  if ( range.IsInside(dataX[i],0) &&
146  range.IsInside(dataY[i],1) )
147  Add(dataX[i], dataY[i] );
148 
149  if (fNPoints < n) (fDataVector->Data()).resize(2*fNPoints);
150  }
151 }
152 
153 UnBinData::UnBinData(unsigned int n, const double * dataX, const double * dataY, const double * dataZ,
154  const DataRange & range, bool isWeighted ) :
155  FitData(range ),
156  fDim( (isWeighted) ? 2 : 3 ),
157  fPointSize(3),
158  fNPoints(0),
159  fDataVector(0),
160  fDataWrapper(0)
161 {
162  // constructor for 3D array data using a range to select the data
163  if ( n > MaxSize() )
164  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n )
165  else if (n > 0) {
166  fDataVector = new DataVector(3*n);
167 
168  for (unsigned int i = 0; i < n; ++i)
169  if ( range.IsInside(dataX[i],0) &&
170  range.IsInside(dataY[i],1) &&
171  range.IsInside(dataZ[i],2) )
172  Add(dataX[i], dataY[i], dataZ[i] );
173 
174  if (fNPoints < n) (fDataVector->Data()).resize(3*fNPoints);
175  }
176 }
177 
178 void UnBinData::Initialize(unsigned int maxpoints, unsigned int dim, bool isWeighted ) {
179  // preallocate a data set given size and dimension
180  unsigned int pointSize = (isWeighted) ? dim+1 : dim;
181  if ( (dim != fDim || pointSize != fPointSize) && fDataVector) {
182 // MATH_INFO_MSGVAL("BinData::Initialize"," Reset amd re-initialize with a new fit point size of ",
183 // dim);
184  delete fDataVector;
185  fDataVector = 0;
186  }
187  fDim = dim;
188  fPointSize = pointSize;
189  unsigned int n = fPointSize*maxpoints;
190  if ( n > MaxSize() ) {
191  MATH_ERROR_MSGVAL("UnBinData::Initialize","Invalid data size", n );
192  return;
193  }
194  if (fDataVector)
195  (fDataVector->Data()).resize( fDataVector->Size() + n );
196  else
197  fDataVector = new DataVector( n);
198 }
199 
200 void UnBinData::Resize(unsigned int npoints) {
201  // resize vector to new points
202  if (fDim == 0) return;
203  if ( npoints > MaxSize() ) {
204  MATH_ERROR_MSGVAL("BinData::Resize"," Invalid data size ", npoints );
205  return;
206  }
207  if (fDataVector != 0) {
208  int nextraPoints = npoints - fDataVector->Size()/fPointSize;
209  if (nextraPoints < 0) {
210  // delete extra points
211  (fDataVector->Data()).resize( npoints * fPointSize);
212  }
213  else if (nextraPoints > 0) {
214  // add extra points
215  Initialize(nextraPoints, fDim, IsWeighted() );
216  }
217  else // nextraPoints == 0
218  return;
219  }
220  else // no DataVector create it
221  fDataVector = new DataVector( npoints*fPointSize);
222 }
223 
224 
225 
226  } // end namespace Fit
227 
228 } // end namespace ROOT
229 
void Resize(unsigned int npoints)
resize the vector to the given npoints
Definition: UnBinData.cxx:200
Base class for all the fit data types.
Definition: DataVector.h:67
DataWrapper * fDataWrapper
Definition: UnBinData.h:350
#define MATH_ERROR_MSGVAL(loc, str, x)
Definition: Error.h:69
const FData & Data() const
const access to underlying vector
Definition: DataVector.h:163
class holding the fit data points.
Definition: DataVector.h:134
unsigned int fDim
Definition: UnBinData.h:345
DataVector * fDataVector
Definition: UnBinData.h:349
unsigned int fPointSize
Definition: UnBinData.h:346
class maintaining a pointer to external data Using this class avoids copying the data when performing...
Definition: DataVector.h:222
DataOptions : simple structure holding the options on how the data are filled.
Definition: DataOptions.h:28
bool IsWeighted() const
Definition: UnBinData.h:289
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:133
void Add(double x)
add one dim coordinate data (unweighted)
Definition: UnBinData.h:199
size_t Size() const
full size of data vector (npoints * point size)
Definition: DataVector.h:197
static unsigned int MaxSize()
define a max size to avoid allocating too large arrays
Definition: DataVector.h:111
unsigned int fNPoints
Definition: UnBinData.h:347
const Int_t n
Definition: legend1.C:16
bool IsInside(double x, unsigned int icoord=0) const
check if a point is inside the range for the given coordinate
Definition: DataRange.cxx:146
UnBinData(unsigned int maxpoints=0, unsigned int dim=1, bool isWeighted=false)
constructor from dimension of point and max number of points (to pre-allocate vector) ...
Definition: UnBinData.cxx:24
void Initialize(unsigned int maxpoints, unsigned int dim=1, bool isWeighted=false)
preallocate a data set given size and dimension of the coordinates if a vector already exists with co...
Definition: UnBinData.cxx:178