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