Public Member Functions | List of all members
trkf::LinFitAlg Class Reference

#include <LinFitAlg.h>

Public Member Functions

void LinFit (std::vector< float > &x, std::vector< float > &y, std::vector< float > &ey2, float &Intercept, float &Slope, float &InterceptError, float &SlopeError, float &ChiDOF) const
 

Detailed Description

Definition at line 18 of file LinFitAlg.h.

Member Function Documentation

void trkf::LinFitAlg::LinFit ( std::vector< float > &  x,
std::vector< float > &  y,
std::vector< float > &  ey2,
float &  Intercept,
float &  Slope,
float &  InterceptError,
float &  SlopeError,
float &  ChiDOF 
) const

Definition at line 17 of file LinFitAlg.cxx.

20  {
21  // fit a line ala Bevington linfit.F. The number of points fit is defined by
22  // the size of the y vector.
23 
24  ChiDOF = 999.;
25 
26  if(y.size() < 2) return;
27  if(x.size() < y.size() || ey2.size() < y.size()) return;
28 
29  double sum = 0.;
30  double sumx = 0.;
31  double sumy = 0.;
32  double sumxy = 0.;
33  double sumx2 = 0.;
34  double sumy2 = 0.;
35  double weight, arg;
36  unsigned short ii;
37 
38  for(ii = 0; ii < y.size(); ++ii) {
39  weight = 1. / ey2[ii];
40  sum += weight;
41  sumx += weight * x[ii];
42  sumy += weight * y[ii];
43  sumx2 += weight * x[ii] * x[ii];
44  sumxy += weight * x[ii] * y[ii];
45  sumy2 += weight * y[ii] * y[ii];
46  }
47  // calculate coefficients and std dev
48  double delta = sum * sumx2 - sumx * sumx;
49  if(delta == 0.) return;
50  double A = (sumx2 * sumy - sumx * sumxy) / delta;
51  double B = (sumxy * sum - sumx * sumy) / delta;
52  Intercept = A;
53  Slope = B;
54  if(x.size() == 2) {
55  ChiDOF = 0.;
56  return;
57  }
58  double ndof = x.size() - 2;
59  double varnce = (sumy2 + A*A*sum + B*B*sumx2 - 2 * (A*sumy + B*sumxy - A*B*sumx)) / ndof;
60  if(varnce > 0.) {
61  InterceptError = sqrt(varnce * sumx2 / delta);
62  SlopeError = sqrt(varnce * sum / delta);
63  } else {
64  InterceptError = 0.;
65  SlopeError = 0.;
66  }
67  sum = 0.;
68  // calculate chisq
69  for(ii = 0; ii < y.size(); ++ii) {
70  arg = y[ii] - A - B * x[ii];
71  sum += arg * arg / ey2[ii];
72  }
73  ChiDOF = sum / ndof;
74  } // LinFit
weight
Definition: test.py:257
#define A
Definition: memgrp.cpp:38

The documentation for this class was generated from the following files: