ROOT  6.06/09
Reference Guide
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 "Math/Error.h"
15 
16 #include <cassert>
17 #include <cmath>
18 
19 namespace ROOT {
20 
21  namespace Fit {
22 
23 UnBinData::UnBinData(unsigned int maxpoints, unsigned int dim, bool isWeighted ) :
24  FitData(),
25  fDim(dim),
26  fPointSize( (isWeighted) ? dim +1 : dim),
27  fNPoints(0),
28  fDataVector(0),
29  fDataWrapper(0)
30 {
31  // constructor with default option and range
32  unsigned int n = fPointSize*maxpoints;
33  if ( n > MaxSize() )
34  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n )
35  else if (n > 0)
36  fDataVector = new DataVector(n);
37 }
38 
39 UnBinData::UnBinData (const DataRange & range, unsigned int maxpoints , unsigned int dim, bool isWeighted ) :
40  FitData(range),
41  fDim(dim),
42  fPointSize( (isWeighted) ? dim +1 : dim),
43  fNPoints(0),
44  fDataVector(0),
45  fDataWrapper(0)
46 {
47  // constructor from option and default range
48  unsigned int n = fPointSize*maxpoints;
49  if ( n > MaxSize() )
50  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n )
51  else if (n > 0)
52  fDataVector = new DataVector(n);
53 }
54 
55 UnBinData::UnBinData (const DataOptions & opt, const DataRange & range, unsigned int maxpoints, unsigned int dim, bool isWeighted) :
56  FitData( opt, range),
57  fDim(dim),
58  fPointSize( (isWeighted) ? dim +1 : dim),
59  fNPoints(0),
60  fDataVector(0),
61  fDataWrapper(0)
62 {
63  // constructor from options and range
64  unsigned int n = fPointSize*maxpoints;
65  if ( n > MaxSize() )
66  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n )
67  else if (n > 0)
68  fDataVector = new DataVector(n);
69 }
70 
71 UnBinData::UnBinData(unsigned int n, const double * dataX ) :
72  FitData( ),
73  fDim(1),
74  fPointSize(1),
75  fNPoints(n),
76  fDataVector(0)
77 {
78  // constructor for 1D external data
79  fDataWrapper = new DataWrapper(dataX);
80 }
81 
82 UnBinData::UnBinData(unsigned int n, const double * dataX, const double * dataY, bool isWeighted ) :
83  FitData( ),
84  fDim( (isWeighted) ? 1 : 2),
85  fPointSize(2),
86  fNPoints(n),
87  fDataVector(0),
88  fDataWrapper(0)
89 {
90  // constructor for 2D external data
91  fDataWrapper = new DataWrapper(dataX, dataY, 0, 0, 0, 0);
92 }
93 
94 UnBinData::UnBinData(unsigned int n, const double * dataX, const double * dataY, const double * dataZ, bool isWeighted ) :
95  FitData( ),
96  fDim( (isWeighted) ? 2 : 3),
97  fPointSize(3),
98  fNPoints(n),
99  fDataVector(0)
100 {
101  // constructor for 3D external data
102  fDataWrapper = new DataWrapper(dataX, dataY, dataZ, 0, 0, 0, 0, 0);
103 }
104 
105 UnBinData::UnBinData(unsigned int n, const double * dataX, const DataRange & range ) :
106  FitData(range),
107  fDim(1),
108  fPointSize(1),
109  fNPoints(0),
110  fDataVector(0),
111  fDataWrapper(0)
112 {
113  // constructor for 1D array data using a range to select the data
114  // copy the data inside
115  // when n = 0 construct just an empty data set
116  if ( n > MaxSize() )
117  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n )
118  else if (n > 0) {
119  fDataVector = new DataVector(n);
120 
121  for (unsigned int i = 0; i < n; ++i)
122  if ( range.IsInside(dataX[i]) ) Add(dataX[i] );
123 
124  if (fNPoints < n) (fDataVector->Data()).resize(fNPoints);
125  }
126 }
127 
128 UnBinData::UnBinData(unsigned int n, const double * dataX, const double * dataY, const DataRange & range, bool isWeighted ) :
129  FitData(range),
130  fDim( (isWeighted) ? 1 : 2 ),
131  fPointSize(2),
132  fNPoints(0),
133  fDataVector(0),
134  fDataWrapper(0)
135 {
136  // constructor for 2D array data using a range to select the data
137  // copy the data inside
138  if ( n > MaxSize() )
139  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n )
140  else if (n > 0) {
141  fDataVector = new DataVector(2*n);
142 
143  for (unsigned int i = 0; i < n; ++i)
144  if ( range.IsInside(dataX[i],0) &&
145  range.IsInside(dataY[i],1) )
146  Add(dataX[i], dataY[i] );
147 
148  if (fNPoints < n) (fDataVector->Data()).resize(2*fNPoints);
149  }
150 }
151 
152 UnBinData::UnBinData(unsigned int n, const double * dataX, const double * dataY, const double * dataZ,
153  const DataRange & range, bool isWeighted ) :
154  FitData(range ),
155  fDim( (isWeighted) ? 2 : 3 ),
156  fPointSize(3),
157  fNPoints(0),
158  fDataVector(0),
159  fDataWrapper(0)
160 {
161  // constructor for 3D array data using a range to select the data
162  if ( n > MaxSize() )
163  MATH_ERROR_MSGVAL("UnBinData","Invalid data size n - no allocation done", n )
164  else if (n > 0) {
165  fDataVector = new DataVector(3*n);
166 
167  for (unsigned int i = 0; i < n; ++i)
168  if ( range.IsInside(dataX[i],0) &&
169  range.IsInside(dataY[i],1) &&
170  range.IsInside(dataZ[i],2) )
171  Add(dataX[i], dataY[i], dataZ[i] );
172 
173  if (fNPoints < n) (fDataVector->Data()).resize(3*fNPoints);
174  }
175 }
176 
177 void UnBinData::Initialize(unsigned int maxpoints, unsigned int dim, bool isWeighted ) {
178  // preallocate a data set given size and dimension
179  unsigned int pointSize = (isWeighted) ? dim+1 : dim;
180  if ( (dim != fDim || pointSize != fPointSize) && fDataVector) {
181 // MATH_INFO_MSGVAL("BinData::Initialize"," Reset amd re-initialize with a new fit point size of ",
182 // dim);
183  delete fDataVector;
184  fDataVector = 0;
185  }
186  fDim = dim;
187  fPointSize = pointSize;
188  unsigned int n = fPointSize*maxpoints;
189  if ( n > MaxSize() ) {
190  MATH_ERROR_MSGVAL("UnBinData::Initialize","Invalid data size", n );
191  return;
192  }
193  if (fDataVector)
194  (fDataVector->Data()).resize( fDataVector->Size() + n );
195  else
196  fDataVector = new DataVector( n);
197 }
198 
199 void UnBinData::Resize(unsigned int npoints) {
200  // resize vector to new points
201  if (fDim == 0) return;
202  if ( npoints > MaxSize() ) {
203  MATH_ERROR_MSGVAL("BinData::Resize"," Invalid data size ", npoints );
204  return;
205  }
206  if (fDataVector != 0) {
207  int nextraPoints = npoints - fDataVector->Size()/fPointSize;
208  if (nextraPoints < 0) {
209  // delete extra points
210  (fDataVector->Data()).resize( npoints * fPointSize);
211  }
212  else if (nextraPoints > 0) {
213  // add extra points
214  Initialize(nextraPoints, fDim, IsWeighted() );
215  }
216  else // nextraPoints == 0
217  return;
218  }
219  else // no DataVector create it
220  fDataVector = new DataVector( npoints*fPointSize);
221 }
222 
223 
224 
225  } // end namespace Fit
226 
227 } // end namespace ROOT
228 
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
void Resize(unsigned int npoints)
resize the vector to the given npoints
Definition: UnBinData.cxx:199
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:132
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:23
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:177