using System; using System.Collections.Generic; using System.Collections; using System.Text; namespace BrazilTimestampReconstruction { class RelativeFit { List x, y; LlseFit localFit; public RelativeFit () { x = new List(); y = new List(); //localFit = new LlseFit(); } public void InsertNewAnchorPoint (double p, double q) { x.Add (p); y.Add (q); } public void ComputeRelativeFit(int minAnchors, bool globalFit) { if (x.Count > minAnchors) { localFit = new LlseFit(); localFit.FitData(x.ToArray(), y.ToArray(), x.Count, new double[0], 0); # region Filter bad skews // Check to see if the skews are within a reasonable range // IF not, do not use these fits if (globalFit) { double skew = (1.0 - localFit.Alpha * 1024) * 1.0E6; if (Math.Abs(skew) >= Constants.MAX_GLOBAL_SKEW) localFit = null; } else { double skew = (1.0 - localFit.Alpha) * 1.0E6; if (Math.Abs(localFit.Alpha) >= Constants.MAX_LOCAL_SKEW) localFit = null; } #endregion } /* if (x.Count == 2) { // weight this fit less. localFit.ChiSquare = Constants.HIGH_CHI; } */ } public LlseFit LocalFit { set { localFit = value; } get { return localFit; } } public LlseFit ComputeGlobalFit(LlseFit globalFit, LlseFit localFit, bool flip, int minPoints) { if ((globalFit == null) || (localFit == null)) return null; // check if the min number of points // criterion is satisfied if ((globalFit.NumPoints < minPoints) || (localFit.NumPoints < minPoints)) { return null; } double alphaNew, betaNew; if (flip == false) { alphaNew = globalFit.Alpha / localFit.Alpha; betaNew = globalFit.Beta - alphaNew * localFit.Beta; } else { alphaNew = globalFit.Alpha * localFit.Alpha; betaNew = globalFit.Alpha * localFit.Beta + globalFit.Beta; } if (Double.IsNaN(alphaNew) || Double.IsNaN(alphaNew)) return null; LlseFit fofFit = new LlseFit(alphaNew, betaNew); Int16 totPoints = (short)(globalFit.NumPoints + localFit.NumPoints); #region strategy1: Weighted Reduced ChiSquare double redChi1 = globalFit.ChiSquare ; // /(globalFit.NumPoints-1); double redChi2 = localFit.ChiSquare; // /(localFit.NumPoints-1); double weight1 = (double) globalFit.NumPoints / (double) totPoints; double weight2 = (double) localFit.NumPoints / (double) totPoints; // weighted chi2 fofFit.ChiSquare = weight1 * redChi1 + weight2 * redChi2; fofFit.NumPoints = totPoints; #endregion #region strategy2: Non Weighted Reduced Chisquare // compute the reduced chi square of the combined //fofFit.ChiSquare = globalFit.ChiSquare + localFit.ChiSquare; //fofFit.NumPoints = totPoints; //fofFit.DegreesOfFreedom = (Int16) (globalFit.DegreesOfFreedom + localFit.DegreesOfFreedom); #endregion return fofFit; } } }