NTupleFCN.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 #include "msdevstudio/MSconfig.h"
14 #endif
15 
16 #include "NTupleFCN.h"
17 
19 #include "datasrcs/DataSource.h"
20 #include "datasrcs/TupleCut.h"
21 
22 #include "functions/FunctionBase.h"
23 
24 #include <algorithm>
25 #include <functional>
26 
27 using std::bind2nd;
28 using std::count;
29 using std::find_if;
30 using std::not_equal_to;
31 using std::vector;
32 
33 using namespace hippodraw;
34 
37  : m_fit_cut ( 0 ),
38  m_ntuple ( 0 ),
39  m_has_errors ( false ),
40  m_fit_range ( false )
41 {
42 }
43 
45 NTupleFCN ( const NTupleFCN & fcn )
46  : StatedFCN ( fcn ),
47  m_fit_cut ( 0 ),
48  m_ntuple ( fcn.m_ntuple ),
49  m_has_errors ( fcn.m_has_errors ),
50  m_fit_range ( fcn.m_fit_range )
51 {
52 }
53 
54 void
56 copyFrom ( const StatedFCN * base )
57 {
58  StatedFCN::copyFrom ( base );
59 
60  const NTupleFCN * fcn = dynamic_cast < const NTupleFCN * > ( base );
61  if ( fcn != 0 ) {
62  m_fit_cut = fcn -> m_fit_cut;
63  m_fit_range = fcn -> m_fit_range;
64  m_indices = fcn -> m_indices;
65  m_ntuple = fcn -> m_ntuple;
66  m_has_errors = fcn -> m_has_errors;
67  }
68 }
69 
70 namespace dp2 = hippodraw::DataPoint2DTuple;
71 namespace dp3 = hippodraw::DataPoint3DTuple;
72 
73 void
75 setDataSource ( const DataSource * ntuple )
76 {
77  unsigned int size = ntuple -> columns ();
78  vector < int > indices ( size );
79 
80  for ( unsigned int i = 0; i < size; i++ ) {
81  indices [i] = i;
82  }
83 
84  setDataSource ( ntuple, -1, indices );
85 }
86 
87 void
89 setDataSource ( const DataSource * ntuple,
90  int dimension,
91  const std::vector < int > & indices )
92 {
93  m_ntuple = ntuple;
94  m_indices = indices;
95 }
96 
97 int
100 {
101  unsigned int dim = ( m_indices.size() -2 ) / 2;
102  int ie = m_indices [ 2 * dim + 1 ];
103 
104  return ie;
105 }
106 
107 bool
109 hasErrors ( ) const
110 {
111  bool yes = false;
112 
113  unsigned int ie = getErrorColumn ();
114  unsigned int cols = m_ntuple -> columns ();
115  if ( ie < cols ) {
116  const vector < double > & errors = m_ntuple -> getColumn ( ie );
117 
118  if ( errors.empty() ) return false;
119 
120  vector < double >::const_iterator first
121  = find_if ( errors.begin(), errors.end (),
122  bind2nd ( not_equal_to < double > (), 0.0 ) );
123 
124  yes = first != errors.end ();
125  }
126 
127  return yes;
128 }
129 
130 bool
133 {
134  bool didit = false;
135  if ( yes ) {
136  if ( hasErrors () ) {
137  m_has_errors = true;
138  didit = true;
139  }
140  else m_has_errors = false;
141  }
142  else {
143  m_has_errors = false;
144  didit = true;
145  }
146  return didit;
147 }
148 
149 bool
151 getUseErrors () const
152 {
153  return m_has_errors;
154 }
155 
156 int
159 {
160  int ie = getErrorColumn ();
161 
162  const vector < double > & errors = m_ntuple -> getColumn ( ie );
163  int number_points = errors.size();
164  if ( m_has_errors ) {
165  int zeros = count ( errors.begin(), errors.end(), 0.0 );
166  number_points -= zeros;
167  }
168 
169  vector< double > free_parms;
170  return number_points - getNumberFreeParms ();
171 }
172 
173 void
175 reset ( std::vector < std::vector < double > > & alpha,
176  std::vector < double > & beta,
177  unsigned int size )
178 {
179  beta.clear ();
180  beta.resize ( size, 0.0 );
181 
182  alpha.resize ( size );
183 
184  for ( unsigned int i = 0; i < alpha.size (); i++ ) {
185  alpha[i].clear ();
186  alpha[i].resize ( size, 0.0 );
187  }
188 }
189 
192 void
194 calcAlphaBeta ( std::vector < std::vector < double > > & alpha,
195  std::vector < double > & beta )
196 {
197  int ix = m_indices [ dp2::X ];
198  int iy = m_indices [ dp2::Y ];
199  int ie = m_indices [ dp2::YERR ];
200  unsigned int num_parms = getNumberFreeParms ();
201  reset ( alpha, beta, num_parms );
202 
203  unsigned int rows = m_ntuple -> rows ();
204  for ( unsigned int i = 0; i < rows; i++ ) {
205  if ( acceptRow ( i ) ) {
206  const vector < double > & row = m_ntuple -> getRow ( i );
207 
208  double err = ie < 0 ? 0. : row [ ie ];
209  if ( err == 0.0 && m_has_errors ) continue;
210  if ( m_has_errors == false ) err = 1.0;
211 
212  double x = row [ ix ];
213  double y = row [ iy ];
214 
215  double y_diff = y - m_function -> operator () ( x );
216  vector < double > derives;
217  fillFreeDerivatives ( derives, x );
218 
219  for ( unsigned int j = 0; j < num_parms; j++ ) {
220  double t = derives[j] / ( err * err );
221 
222  for ( unsigned int k = 0; k <= j; k++ ) {
223  alpha[j][k] = alpha[j][k] + t * derives[k];
224  }
225 
226  beta[j] += t * y_diff;
227  }
228  }
229  }
230 }
231 
234 void
237 {
238  m_fit_cut = cut;
239 
240  if ( cut != 0 ) {
241  int ix = m_indices [ dp2::X ];
242  cut -> setColumn ( ix );
243  }
244 }
245 
246 void
248 setFitRange ( bool yes )
249 {
250  m_fit_range = yes;
251 }
252 
253 bool
255 acceptRow ( unsigned int row ) const
256 {
257  bool yes = true;
258  if ( m_fit_cut != 0 &&
259  m_fit_cut -> isEnabled () &&
260  m_fit_range ) {
261  yes = m_fit_cut -> acceptRow ( m_ntuple, row );
262  }
263 
264  return yes;
265 }
virtual void copyFrom(const StatedFCN *other)
Makes a copy of the relevant attributes from other object.
Definition: StatedFCN.cxx:44
FunctionBase * m_function
The flags to indicated which parameters are to be held fixed during minimization of this object...
Definition: StatedFCN.h:90
unsigned int i
void fillFreeDerivatives(std::vector< double > &, double x)
Clears and fills the vector with the derivatives of the function a coordinate value x...
Definition: StatedFCN.cxx:167
virtual double operator()(const std::vector< double > &parms) const
Sets the model function parameters to parms and returns the objective value.
Definition: StatedFCN.cxx:185
A derived class of StatedFCN This class gets its data points from a DataSource.
Definition: NTupleFCN.h:31
void setDataSource(const DataSource *source)
Sets the data source.
Definition: NTupleFCN.cxx:75
bool m_fit_range
Set to true if the fitting range is turn on.
Definition: NTupleFCN.h:77
virtual bool setUseErrors(bool yes=true)
Sets the use of errors on the data point.
Definition: NTupleFCN.cxx:132
virtual bool getUseErrors() const
Gets the use of errors.
Definition: NTupleFCN.cxx:151
hippodraw::NTupleFCN class interface
TupleCut * m_fit_cut
The cut that manages the range on X to be used for fitting.
Definition: NTupleFCN.h:37
The class expresses a cut on a DataSource, i.e.
Definition: TupleCut.h:43
bool m_has_errors
Error use flag.
Definition: NTupleFCN.h:71
return yes
Definition: CanvasView.cxx:883
NTupleFCN()
The default constructor.
Definition: NTupleFCN.cxx:36
std::vector< int > m_indices
The indexes into the DataSource.
Definition: NTupleFCN.h:60
hippodraw::DataSource class interface.
intp size(numeric::array arr)
Definition: num_util.cpp:296
unsigned int getNumberFreeParms() const
Returns the number of free parameters.
Definition: StatedFCN.cxx:115
virtual void setFitRange(bool yes=true)
Sets use of a fitting range on or off.
Definition: NTupleFCN.cxx:248
const DataSource * m_ntuple
The data source containing the data points for the fitter.
Definition: NTupleFCN.h:64
void reset(std::vector< std::vector< double > > &alpha, std::vector< double > &beta, unsigned int size)
Resets the matrix alpha and the vector beta.
Definition: NTupleFCN.cxx:175
int getErrorColumn() const
Returns the index to the column containing error on the value coordinate.
Definition: NTupleFCN.cxx:99
virtual void copyFrom(const StatedFCN *)
Makes a copy of the relevant attributes from other object.
Definition: NTupleFCN.cxx:56
hippodraw::TupleCut class interface
virtual void calcAlphaBeta(std::vector< std::vector< double > > &alpha, std::vector< double > &beta)
Calculates alpha and beta.
Definition: NTupleFCN.cxx:194
hippodraw::FunctionBase class interface
hippodraw::DataPointTuple namespace interface
A derived class for FCNBase class.
Definition: StatedFCN.h:70
virtual void setFitCut(TupleCut *cut)
Sets the cut to limit range of fitting.
Definition: NTupleFCN.cxx:236
bool acceptRow(unsigned int row) const
Returns true if the DataSource row row is within the fit Range.
Definition: NTupleFCN.cxx:255
virtual int degreesOfFreedom() const
Calculates the number of degrees of freedom.
Definition: NTupleFCN.cxx:158
bool hasErrors() const
Examines state of error bars.
Definition: NTupleFCN.cxx:109
Base class for DataSource.
Definition: DataSource.h:55

Generated for HippoDraw Class Library by doxygen