CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ParabolaFit.cc
Go to the documentation of this file.
1 #include "ParabolaFit.h"
2 #include <iostream>
3 using namespace std;
4 template <class T> T sqr( T t) {return t*t;}
5 
6 void ParabolaFit::addPoint(double x, double y)
7 {
8  hasWeights = false;
9  addPoint(x,y,1.);
10 }
11 
12 void ParabolaFit::addPoint(double x, double y, double w)
13 {
14  hasValues = false;
15  hasErrors = false;
16  Point p = {x,y,w};
17  points.push_back(p);
18 }
19 
20 const ParabolaFit::Result & ParabolaFit::result( bool doErrors ) const
21 {
22  if (hasErrors) return theResult;
23  if (hasValues && !doErrors) return theResult;
24 
25  double F0, F1, F2, F3, F4, F0y, F1y, F2y;
26  F0 = F1 = F2 = F3 = F4 = F0y = F1y = F2y = 0.;
27 
28  typedef vector<Point>::const_iterator IT;
29  for (IT ip = points.begin(); ip != points.end(); ip++) {
30 
31  double pow;
32  double x = ip->x;
33  double y = ip->y;
34  double w = ip->w;
35 
36  F0 += w; F0y += w*y;
37  F1 += w*x; F1y += w*x*y;
38  pow = x*x; F2 += w*pow; F2y += w*pow*y; // x^2
39  pow *= x; F3 += w*pow; // x^3
40  pow *= x; F4 += w*pow; // x^4
41  }
42 
43  Column cA = { F0, F1, F2 };
44  Column cB = { F1, F2, F3 };
45  Column cC = { F2, F3, F4 };
46  Column cY = { F0y, F1y, F2y };
47 
48  double det0 = det(cA, cB, cC);
49 
50  if ( !hasFixedParC) {
51  theResult.parA = det(cY, cB, cC) / det0;
52  theResult.parB = det(cA, cY, cC) / det0;
53  theResult.parC = det(cA, cB, cY) / det0;
54  } else {
55  Column cCY = {F0y-theResult.parC*F2, F1y-theResult.parC*F3, F2y-theResult.parC*F4};
56  double det0C = det(cA, cB);
57  theResult.parA = det(cCY, cB) / det0C;
58  theResult.parB = det(cA, cCY) / det0C;
59  }
60 
61 // std::cout <<" result: A="<<theResult.parA<<" B="<<theResult.parB<<" C="<<theResult.parC<<endl;
62  double vAA, vBB, vCC, vAB, vAC, vBC;
63  vAA = vBB = vCC = vAB = vAC = vBC = 0.;
64 
65  hasValues = true;
66  if (!doErrors) return theResult;
67 
68  if( !hasWeights && dof() > 0) {
69 // cout <<" CHI2: " << chi2() <<" DOF: " << dof() << endl;
70  double scale_w = 1./chi2()/dof();
71  for (IT ip = points.begin(); ip != points.end(); ip++) ip->w *= scale_w;
72 // cout <<" CHI2: " << chi2() <<" DOF: " << dof() << endl;
73  }
74 
75  for (IT ip = points.begin(); ip != points.end(); ip++) {
76 
77  double w = ip->w;
78  Column cX = {1., ip->x, sqr(ip->x) };
79 
80  double dXBC = det(cX, cB, cC);
81  double dAXC = det(cA, cX, cC);
82  double dABX = det(cA, cB, cX);
83 
84  vAA += w * sqr(dXBC);
85  vBB += w * sqr(dAXC);
86  vCC += w * sqr(dABX);
87  vAB += w * dXBC * dAXC;
88  vAC += w * dXBC * dABX;
89  vBC += w * dAXC * dABX;
90  }
91 
92  theResult.varAA = vAA/sqr(det0);
93  theResult.varBB = vBB/sqr(det0);
94  theResult.varCC = vCC/sqr(det0);
95  theResult.varAB = vAB/sqr(det0);
96  theResult.varAC = vAC/sqr(det0);
97  theResult.varBC = vBC/sqr(det0);
98 
99  hasErrors = true;
100  return theResult;
101 }
102 
103 
104 double ParabolaFit::chi2() const
105 {
106  double mychi2 = 0.;
107  for ( vector<Point>::const_iterator
108  ip = points.begin(); ip != points.end(); ip++) {
109  mychi2 += ip->w * sqr(ip->y - fun(ip->x));
110  }
111  return mychi2;
112 }
113 
114 double ParabolaFit::fun(double x) const
115 {
116  return theResult.parA + theResult.parB*x + theResult.parC*x*x;
117 }
118 
119 int ParabolaFit::dof() const
120 {
121  int nPar = 3; if (hasFixedParC) nPar--;
122  int nDof = points.size() - nPar;
123  return (nDof > 0) ? nDof : 0;
124 }
125 
127  const Column & c1, const Column & c2, const Column & c3) const
128 {
129  return c1.r1 * c2.r2 * c3.r3
130  + c2.r1 * c3.r2 * c1.r3
131  + c3.r1 * c1.r2 * c2.r3
132  - c1.r3 * c2.r2 * c3.r1
133  - c2.r3 * c3.r2 * c1.r1
134  - c3.r3 * c1.r2 * c2.r1;
135 }
136 
137 double ParabolaFit::det( const Column & c1, const Column & c2) const
138 {
139  return c1.r1*c2.r2 - c1.r2*c2.r1;
140 }
int dof() const
Definition: ParabolaFit.cc:119
const Result & result(bool doErrors) const
Definition: ParabolaFit.cc:20
const double w
Definition: UKUtility.cc:23
double det(const Column &c1, const Column &c2, const Column &c3) const
Definition: ParabolaFit.cc:126
T x() const
Cartesian x coordinate.
double chi2() const
Definition: ParabolaFit.cc:104
void addPoint(double x, double y)
Definition: ParabolaFit.cc:6
std::vector< LinkConnSpec >::const_iterator IT
double fun(double x) const
Definition: ParabolaFit.cc:114
Square< F >::type sqr(const F &f)
Definition: Square.h:13
long double T
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40