BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
MucRecQuadFit.cxx
Go to the documentation of this file.
1//$id$
2//
3//$log$
4
5/*
6 * 2003/12/24 Zhengyun You Peking University
7 *
8 * 2004/09/12 Zhengyun You Peking University
9 * transplanted to Gaudi framework
10 */
11
12#include "MucRecEvent/MucRecQuadFit.h"
13#include <CLHEP/Matrix/Matrix.h>
14#include <iostream>
15#include <math.h>
16
17using namespace std;
18using namespace CLHEP;
19
21 // Constructor.
22}
23
25 // Destructor.
26}
27
28/* --------------------------------------------------------------
29
30 utiQuadFit - a least squares straight line fitting program
31
32 DESCRIPTION:
33 Performs weighted least squares fit to line (Y = A*X + B )
34 using linear regression.
35
36 INPUT ARGUMENTS:
37 X(N),Y(N),W(N) - Input values and weights (N=1,2,3...)
38 N - Number of pairs of data points,
39 X - Array of data points in X-axis,
40 Y - Array of data points in Y-axis,
41 W - Array of weights for data points,
42
43 OUTPUT ARGUMENTS:
44 B - Y intercept of best fitted straight line,
45 SIGB - Standard deviation of B,
46 A - Slope of fitted straight line,
47 SIGA - Standard deviation of A,
48 CHISQ - Chi-square
49 LSWLF - = 0 return without error
50 = -1 got some fitting problems
51
52 AUTHOR:
53 Jawluen Tang, Physics department, UT-Austin
54 J. T. Mitchell - adapted for PHENIX use. Converted to C.
55
56 USAGE:
57 utiQuadFit(X,Y,W,N,&A,&B,&CHISQ,&SIGA,&SIGB)
58 The arrays must start counting at element 1.
59
60--------------------------------------------------------------- */
61
62int MucRecQuadFit::QuadFit( float x[], float y[], float w[], int n, float* a, float* b,
63 float* c,
64 int* half, // which half parabola 1: left 2 : right
65 float* chisq, float* siga, float* sigb, float* sigc )
66
67{
68 double sumx, sumx2, sumx3, sumx4, sumy, sumyx, sumyx2, det;
69 float chi;
70
71 /* The variable "i" is declared 8 bytes long to avoid triggering an
72 apparent alignment bug in optimized code produced by gcc-2.95.3.
73 The bug doesn't seem to be present in gcc-3.2. */
74 long i;
75
76 chi = 99999999.0;
77 *a = 0;
78 *b = 0;
79 *c = 0;
80 *half = 0;
81
82 /* N must be >= 2 for this guy to work */
83 if ( n < 3 )
84 {
85 // cout << "utiQuadFit-W: Too few points for quad fit \n" << endl;
86 return -1;
87 }
88
89 /* Initialization */
90 sumx = 0.0;
91 sumx2 = 0.0;
92 sumx3 = 0.0;
93 sumx4 = 0.0;
94 sumy = 0.0;
95 sumyx = 0.0;
96 sumyx2 = 0.0;
97
98 /* Find sum , sumx ,sumy, sumxx, sumxy */
99 for ( i = 0; i < n; i++ )
100 {
101 sumx = sumx + w[i] * x[i];
102 sumx2 = sumx2 + w[i] * x[i] * x[i];
103 sumx3 = sumx3 + w[i] * x[i] * x[i] * x[i];
104 sumx4 = sumx4 + w[i] * x[i] * x[i] * x[i] * x[i];
105 sumy = sumy + w[i] * y[i];
106 sumyx = sumyx + w[i] * y[i] * x[i];
107 sumyx2 = sumyx2 + w[i] * y[i] * x[i] * x[i];
108 // cout<<"x : y "<<x[i]<<" "<<y[i]<<endl;
109 }
110
111 /* compute the best fitted parameters A,B,C */
112
113 HepMatrix D( 3, 3, 1 );
114 HepMatrix C( 3, 1 ), ABC( 3, 1 );
115 D( 1, 1 ) = sumx4;
116 D( 1, 2 ) = D( 2, 1 ) = sumx3;
117 D( 1, 3 ) = D( 3, 1 ) = D( 2, 2 ) = sumx2;
118 D( 3, 2 ) = D( 2, 3 ) = sumx;
119 D( 3, 3 ) = n;
120 C( 1, 1 ) = sumyx2;
121 C( 2, 1 ) = sumyx;
122 C( 3, 1 ) = sumy;
123
124 int ierr;
125 ABC = ( D.inverse( ierr ) ) * C;
126
127 *a = ABC( 1, 1 );
128 *b = ABC( 2, 1 );
129 *c = ABC( 3, 1 );
130
131 // judge which half parabola these points belone to
132 float center = *b / ( -2 * ( *a ) );
133 float mean_x = 0.0;
134 for ( i = 0; i < n; i++ ) { mean_x += x[i] / n; }
135
136 if ( mean_x > center ) *half = 2; // right half
137 else *half = 1; // left half
138
139 // cout<<"in MucRecQuadFit:: which half= "<<*half<<endl;
140
141 /* calculate chi-square */
142 chi = 0.0;
143 for ( i = 0; i < n; i++ )
144 {
145 chi =
146 chi + ( w[i] ) * ( ( y[i] ) - *a * ( x[i] ) - *b ) * ( ( y[i] ) - *a * ( x[i] ) - *b );
147 }
148
149 //*siga = sum/det;
150 //*sigb = sxx/det;
151
152 *chisq = chi;
153 return 1;
154}
const Int_t n
double w
***************************************************************************************Pseudo Class RRes *****************************************************************************************Parameters and physical constants **Maarten sept ************************************************************************DOUBLE PRECISION xsmu **************************************************************************PARTICLE DATA all others are from PDG *Only resonances with known widths into electron pairs are sept ************************************************************************C Declarations C
Definition RRes.h:29
MucRecQuadFit()
Constructor.
~MucRecQuadFit()
Destructor.
int QuadFit(float x[], float y[], float w[], int n, float *a, float *b, float *c, int *half, float *chisq, float *siga, float *sigb, float *sigc)