#ifndef TLineFit_hxx_seen #define TLineFit_hxx_seen namespace COMET { class ILineFit; }; /// Do a line fit of Y as a function of X to a set of data points using a chi2 /// fit. The point values are not saved so this can be used to do running /// fits, but has the disadvantage that you have to calculate chi2 for /// yourself. Almost no internal information is saved, so you can use this as /// in inline variable. /// /// \code /// ILineFit fit; /// /// cout << fit.GetIntercept(); -- prints "0" /// cout << fit.GetSlope(); -- prints "0" /// /// fit.AddPoint(1,1,1); /// cout << fit.GetIntercept(); -- prints "1" /// cout << fit.GetSlope(); -- prints "0" /// /// fit.AddPoint(2,2,1); /// cout << fit.GetIntercept(); -- prints "0" /// cout << fit.GetSlope(); -- prints "1" /// /// fit.AddPoint(3,1,1); /// cout << fit.GetIntercept(); -- prints "1.33333" /// cout << fit.GetSlope(); -- prints "0" /// /// fit.RemovePoint(1,1,1); /// cout << fit.GetIntercept(); -- prints "4" /// cout << fit.GetSlope(); -- prints "-1" /// \endcode /// The code is protected against points with zero or negative uncertainty, /// and against removing too many points. Note that if you remove a point /// that was not actually added, your screwed. /// /// One final comment. This code would make a numerist or computer algorithm /// expert shudder since it will accumulate error over the calculation. Don't /// let that scare you away, but do let it warn you. This is intended as a /// way to incrementally follow a line, it is not intended as a way to get the /// best fit for a line. If you want numeric accuracy, use a global fit. If /// you want robust ray shooting, use this. class COMET::ILineFit { public: ILineFit(); /// Do a line fit of n points with independent variable X, and dependent /// variable Y. ILineFit(int n, double x[], double y[], double sigma[]); /// Add a point to the fit with X as the independent variable, and Y as /// the dependent variable. void AddPoint(double x, double y, double sigma=1); /// Remove a point from the fit with X as the independent variable, and Y /// as the dependent variable. void RemovePoint(double x, double y, double sigma=1); /// Get the intercept. double GetIntercept(void) const; /// Get the uncertainty in the intercept position (doesn't include any /// correlation to the slope). double GetInterceptUncertainty(void) const; /// Get the slope double GetSlope(void) const; /// Get the uncertainty in the slope. double GetSlopeUncertainty(void) const; /// Get the value of the dependent variable as a function of the /// independent variable. double GetValue(double x) const; /// Get the central value of the independent variable. double GetCentralIndependent(void) const; /// Get the number of points in the current fit. int GetNumberOfPoints(void) const {return fPoints;} /// Return true if the fit will have a valid slope and intercept. bool IsValid(void) const; private: int fPoints; double fX, fA, fB, fC, fD, fE; }; #endif