/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Muc/MucRecEvent/MucRecEvent-00-02-52/src/MucRecQuadFit.cxx

Go to the documentation of this file.
00001 //$id$
00002 //
00003 //$log$
00004 
00005 /*
00006  *    2003/12/24   Zhengyun You     Peking University
00007  * 
00008  *    2004/09/12   Zhengyun You     Peking University
00009  *                 transplanted to Gaudi framework
00010  */
00011 
00012 
00013 #include <iostream>
00014 #include <math.h>
00015 #include "MucRecEvent/MucRecQuadFit.h"
00016 #include <CLHEP/Matrix/Matrix.h>
00017 
00018 using namespace std;
00019 using namespace CLHEP;
00020 
00021 MucRecQuadFit::MucRecQuadFit()
00022 {
00023     // Constructor.
00024 }
00025 
00026 MucRecQuadFit::~MucRecQuadFit()
00027 {
00028   // Destructor.
00029 }
00030 
00031 
00032 /* --------------------------------------------------------------
00033 
00034  utiQuadFit - a least squares straight line fitting program
00035 
00036  DESCRIPTION:
00037  Performs weighted least squares fit to line (Y  =  A*X  +  B )
00038  using linear regression.
00039 
00040  INPUT ARGUMENTS:                                             
00041   X(N),Y(N),W(N) - Input values and weights (N=1,2,3...)
00042   N              - Number of pairs of data points,
00043   X              - Array of data points in X-axis,
00044   Y              - Array of data points in Y-axis,
00045   W              - Array of weights for data points,
00046 
00047  OUTPUT ARGUMENTS:            
00048   B              - Y intercept of best fitted straight line, 
00049   SIGB           - Standard deviation of B,                  
00050   A              - Slope of fitted straight line,            
00051   SIGA           - Standard deviation of A,                  
00052   CHISQ          - Chi-square                                
00053   LSWLF          - = 0  return without error                
00054                    = -1 got some fitting problems            
00055              
00056  AUTHOR:
00057  Jawluen Tang, Physics department, UT-Austin 
00058  J. T. Mitchell - adapted for PHENIX use. Converted to C.
00059                                                   
00060  USAGE:                                                       
00061  utiQuadFit(X,Y,W,N,&A,&B,&CHISQ,&SIGA,&SIGB)                   
00062  The arrays must start counting at element 1.
00063 
00064 --------------------------------------------------------------- */
00065 
00066 int
00067 MucRecQuadFit::QuadFit(float x[],
00068                        float y[], 
00069                        float w[],
00070                        int n, 
00071                        float *a, 
00072                        float *b,
00073                        float *c,
00074                        int *half, //which half parabola 1: left 2 : right
00075                        float *chisq,
00076                        float *siga, 
00077                        float *sigb,
00078                        float *sigc)
00079   
00080 {
00081   double sumx, sumx2, sumx3, sumx4,sumy, sumyx, sumyx2, det;
00082   float chi;
00083   
00084   /* The variable "i" is declared 8 bytes long to avoid triggering an
00085      apparent alignment bug in optimized code produced by gcc-2.95.3.
00086      The bug doesn't seem to be present in gcc-3.2. */
00087   long i; 
00088 
00089   chi = 99999999.0;
00090   *a = 0; *b = 0; *c = 0;
00091   *half = 0;
00092 
00093   /* N must be >= 2 for this guy to work */
00094   if (n < 3) 
00095     {
00096       //cout << "utiQuadFit-W: Too few points for quad fit \n" << endl;
00097       return -1;
00098     }
00099 
00100   /* Initialization */
00101   sumx  = 0.0;
00102   sumx2 = 0.0;
00103   sumx3 = 0.0;
00104   sumx4 = 0.0;
00105   sumy  = 0.0;
00106   sumyx = 0.0;
00107   sumyx2 = 0.0;
00108 
00109 
00110   /* Find sum , sumx ,sumy, sumxx, sumxy */
00111   for (i = 0; i < n; i++)
00112     {
00113       sumx = sumx + w[i] * x[i];
00114       sumx2 = sumx2 + w[i] * x[i] * x[i];
00115       sumx3 = sumx3 + w[i] * x[i] * x[i] * x[i];
00116       sumx4 = sumx4 + w[i] * x[i] * x[i] * x[i] * x[i];
00117       sumy = sumy + w[i] * y[i];
00118       sumyx = sumyx + w[i] * y[i] * x[i];
00119       sumyx2 = sumyx2 + w[i] * y[i] * x[i] * x[i];      
00120       //cout<<"x : y "<<x[i]<<" "<<y[i]<<endl;
00121 
00122     }
00123 
00124 
00125 
00126   /* compute the best fitted parameters A,B,C */
00127 
00128     HepMatrix D(3,3,1);
00129     HepMatrix C(3,1), ABC(3,1);
00130     D(1,1) = sumx4; D(1,2) = D(2,1) = sumx3; D(1,3) = D(3,1) = D(2,2) = sumx2;
00131     D(3,2) = D(2,3) = sumx; D(3,3) = n;
00132     C(1,1) = sumyx2; C(2,1) = sumyx; C(3,1) = sumy;
00133 
00134     int ierr;
00135     ABC = (D.inverse(ierr))*C;
00136 
00137     *a = ABC(1,1);
00138     *b = ABC(2,1);
00139     *c = ABC(3,1);
00140     
00141     //judge which half parabola these points belone to
00142     float center = *b/(-2*(*a));
00143     float mean_x;
00144     for(i = 0; i < n; i++){
00145       mean_x += x[i]/n;
00146     }
00147 
00148     if(mean_x > center) *half = 2;//right half
00149     else *half = 1;//left half
00150     
00151     //cout<<"in MucRecQuadFit:: which half= "<<*half<<endl;
00152 
00153   /* calculate chi-square */
00154   chi = 0.0;
00155   for (i=0; i<n; i++) 
00156     {
00157       chi = chi+(w[i])*((y[i])-*a*(x[i])-*b)*
00158         ((y[i])-*a*(x[i])-*b);
00159     }
00160   
00161   //*siga = sum/det;
00162   //*sigb = sxx/det;
00163   
00164   *chisq = chi;
00165   return 1;
00166 }   
00167 

Generated on Tue Nov 29 23:12:58 2016 for BOSS_7.0.2 by  doxygen 1.4.7