CGEM BOSS 6.6.5.h
BESIII Offline Software System
Loading...
Searching...
No Matches
CgemSegmentFun.cxx
Go to the documentation of this file.
2
3#include "CLHEP/Units/PhysicalConstants.h"
4
5#include "CLHEP/Matrix/Vector.h"
6#include "CLHEP/Matrix/SymMatrix.h"
7#include "CLHEP/Geometry/Point3D.h"
9//#include "TrackUtil/Helix.h"
10
11#include "CLHEP/Matrix/Matrix.h"
12
13#include "TROOT.h"
14#include "TObjArray.h"
15#include "TH1F.h"
16#include "TF1.h"
17#include "TGraph.h"
18
19#include <iostream>
20#include <iomanip>
21#include <cstring>
22
23using namespace std;
24
25/////////////////////////////////////////////////////////////////////////////
26
27void CgemSegmentFun::GetChisqF(CLHEP::HepVector trkpar, HepPoint3D pivot, double& chisq){
28
29 double dphi[3],dz[3];
30 HepMatrix delta_Par(2,1);
31 HepSymMatrix ParErr(2);
32 HepMatrix S(2,2);
33 HepSymMatrix HitErr(2);
34
35 KalmanFit::Helix* cgem_helix = new KalmanFit::Helix(pivot, trkpar);
36 chisq = 0.;
37
38 for(int i = 0; i < 3; i++){
39 double tmp_x = cgem_helix->x(IntersectCylinder(rl[i], *cgem_helix)).x();
40 double tmp_y = cgem_helix->x(IntersectCylinder(rl[i], *cgem_helix)).y();
41 double tmp_phi = atan2(tmp_y , tmp_x ); //[-pi,pi)
42 dphi[i] = vec_phi[i] - tmp_phi;
43 if(dphi[i] < -M_PI) dphi[i] += 2*M_PI;
44 if(dphi[i] >= M_PI) dphi[i] -= 2*M_PI;
45 dz[i] = vec_z[i] - cgem_helix->x(IntersectCylinder(rl[i], *cgem_helix)).z();
46// cout<<"dphi["<<i<<"] = "<<dphi[i]<<endl;
47// cout<<"dz["<<i<<"] = "<<dz[i]<<endl;
48
49 delta_Par(1,1) = dphi[i]; delta_Par(2,1) = dz[i];
50
51 ParErr(1,1) = x_reso[i]*x_reso[i]; ParErr(2,2) = v_reso[i]*v_reso[i]; ParErr(1,2) = 0.; ParErr(2,1) = 0.;
52 S(1,1) = 1./r_layer[i]; S(1,2) = 0.; S(2,1) = -1./tan(a_stero[i]); S(2,2) = 1/sin(a_stero[i]); // which radius?
53// HitErr = ParErr.similarity(S);
54// int& ifail = i;
55// HitErr.invert(ifail);
56 HitErr(1,1) = (pow(S(2,2),2)*ParErr(2,2)+pow(S(2,1),2)*ParErr(1,1))/(pow(S(2,2)*S(1,1),2)*ParErr(2,2)*ParErr(1,1));
57 HitErr(1,2) = -S(2,1)/(S(1,1)*ParErr(2,2)*pow(S(2,2),2));
58 HitErr(2,1) = -S(2,1)/(S(1,1)*ParErr(2,2)*pow(S(2,2),2));
59 HitErr(2,2) = 1./(ParErr(2,2)*pow(S(2,2),2));
60
61 chisq += (HitErr.similarityT(delta_Par)).determinant();
62// double sigma = (HitErr.similarityT(delta_Par)).determinant();
63// chisq += (delta_Par.T()*delta_Par).determinant()/sigma;
64 }
65 delete cgem_helix;
66}
67
68HepSymMatrix CgemSegmentFun::InvC(int i)
69{
70 HepSymMatrix C(2,0);
71 C(1,1) = (r_X[i]*r_X[i]*v_reso[i]*v_reso[i]+cos(a_stero[i])*cos(a_stero[i])*r_V[i]*r_V[i]*x_reso[i]*x_reso[i])/(rl[i]*rl[i]*x_reso[i]*x_reso[i]*v_reso[i]*v_reso[i]);
72 C(2,1) = r_V[i]*sin(a_stero[i])*cos(a_stero[i])/(rl[i]*v_reso[i]*v_reso[i]);
73 C(2,2) = sin(a_stero[i])*sin(a_stero[i])/(v_reso[i]*v_reso[i]);
74 return C;
75}
76
77HepVector CgemSegmentFun::CalculateHelix(CLHEP::HepVector trkpar, HepPoint3D pivot)
78{
79 KalmanFit::Helix cgem_helix_0(pivot, trkpar);
80 HepMatrix J_dmdx(2,3,0), J_dxda(3,5,0), J(2,5,0), A(6,5,0);
81 HepSymMatrix U(5, 0), W(6,0), C(2,0);
82 HepVector a_new(5), dm(6), da(5);
83 double dphi[3],dz[3];
84
85 for(int i = 0; i < 3; i++) {
86 double d_phi = IntersectCylinder(rl[i], cgem_helix_0);
87 HepPoint3D pos = cgem_helix_0.x(d_phi);
88 double x = pos.x();
89 double y = pos.y();
90 double z = pos.z();
91 double r2 = x*x+y*y;
92 double tmp_phi = atan2(y, x); //[-pi,pi)
93 dphi[i] = vec_phi[i] - tmp_phi;
94 if(dphi[i] < -M_PI) dphi[i] += 2*M_PI;
95 if(dphi[i] >= M_PI) dphi[i] -= 2*M_PI;
96
97 dm[i*2]=dphi[i]*rl[i];
98 dm[i*2+1]=vec_z[i]-z;
99 //cout<<"rl, r ="<<rl[i]<<", "<<sqrt(r2)<<endl;
100
101 J_dmdx(1,1)=-y/rl[i];
102 J_dmdx(1,2)= x/rl[i];
103 J_dmdx(2,3)= 1;
104 J_dxda=cgem_helix_0.delXDelA(d_phi);
105 J=J_dmdx*J_dxda;
106 A.sub(i*2+1,1,J);
107
108 C=InvC(i);
109 W.sub(i*2+1,C);
110
111 cout<<"iLayer = "<<i<<endl;
112 cout<<"dm: "<<dm<<endl;
113 cout<<"J_dmdx: "<<J_dmdx<<endl;
114 cout<<"J_dxda: "<<J_dxda<<endl;
115 cout<<"J: "<<J<<endl;
116 cout<<"A: "<<A<<endl;
117 cout<<"C: "<<C<<endl;
118 cout<<"W: "<<W<<endl;
119 }
120 int ifail=99;
121 U=W.similarityT(A);
122 cout<<"U = "<<U<<endl;
123 cout<<"U.det() = "<<U.determinant()<<endl;
124 double det=0.;
125 if(DetSquareMatrix(U,det)) cout<<"det = "<<det<<endl;
126 HepSymMatrix U_inv = U;
127 if(InverseSymMatrix(U,U_inv)) {
128 cout<<"U_inv = "<<U_inv<<endl;
129 cout<<"I = "<<U*U_inv<<endl;
130 }
131
132 //HepMatrix UU=A.T()*W*A;
133 //cout<<"UU = "<<UU<<endl;
134 HepSymMatrix U0 = U;
135
136 HepSymMatrix U1=U;
137 cout<<"U1 = "<<U1<<endl;
138 U1.invertCholesky5(ifail);
139 cout<<"ifail = "<<ifail<<endl;
140 cout<<"U1^-1 = "<<U1<<endl;
141 cout<<"I1="<<U*U1<<endl;
142
143 HepSymMatrix U2=U;
144 cout<<"U2 = "<<U2<<endl;
145 U2.invertHaywood5(ifail);
146 cout<<"ifail = "<<ifail<<endl;
147 cout<<"U2^-1 = "<<U2<<endl;
148 cout<<"I2="<<U*U2<<endl;
149
150 HepSymMatrix U3=U;
151 cout<<"U3 = "<<U3<<endl;
152 U3.invertBunchKaufman(ifail);
153 cout<<"ifail = "<<ifail<<endl;
154 cout<<"U3^-1 = "<<U3<<endl;
155 cout<<"I3="<<U*U3<<endl;
156
157////////HepSymMatrix U4(5,1);
158////////cout<<"U4 = "<<U4<<endl;
159////////U4.invertBunchKaufman(ifail);
160////////cout<<"ifail = "<<ifail<<endl;
161////////cout<<"U4^-1 = "<<U4<<endl;
162
163 U.invert(ifail);
164 cout<<"ifail = "<<ifail<<endl;
165 cout<<"U^-1 = "<<U<<endl;
166 cout<<"I="<<U*U0<<endl;
167 da=U*A.T()*W*dm;
168 //cout<<"A="<<A<<endl;
169 //cout<<"A^T="<<A.T()<<endl;
170 cout<<"del_a="<<da<<endl;
171 a_new=trkpar+da;
172 cout<<"a_new="<<a_new<<endl;
173 return a_new;
174}
175
176void CgemSegmentFun::GetLikelihoodF(CLHEP::HepVector trkpar, HepPoint3D pivot, double& chisq){
177
178 double dphi[3],dv[3];
179 double total = 1.;
180 KalmanFit::Helix* cgem_helix = new KalmanFit::Helix(pivot, trkpar);
181 for(int i = 0; i < 3; i++){
182 double tmp_x = cgem_helix->x(IntersectCylinder(rl[i], *cgem_helix)).x();
183 double tmp_y = cgem_helix->x(IntersectCylinder(rl[i], *cgem_helix)).y();
184 double tmp_z = cgem_helix->x(IntersectCylinder(rl[i], *cgem_helix)).z();
185 double tmp_phi = atan2(tmp_y , tmp_x ); //[-pi,pi)
186 while(tmp_phi<0) tmp_phi += 2*M_PI;
187 dphi[i] = tmp_phi - vec_phi[i];
188 if(dphi[i] < -M_PI) dphi[i] += 2*M_PI;
189 if(dphi[i] >= M_PI) dphi[i] -= 2*M_PI;
190
191 double tmp_v = tmp_z*sin(a_stero[i])*r_layer[i]/rl[i]+r_layer[i]*tmp_phi*cos(a_stero[i]);
192 dv[i] = tmp_v - vec_v[i];
193
194 double f1;
195 if(nStrip_X[i] == 1) f1 = fLadder(dphi[i], DeltaPhi[i]);
196 else f1 = fGauss(dphi[i], phi_mean[i], phi_reso[i]);
197// f1 = fGauss(dphi[i], phi_mean[i], phi_reso[i]);
198 double f2 = fGauss(dv[i], v_mean[i], v_reso[i]);
199 total *= f1*f2;
200 }
201 chisq = -1.*log(total);
202 delete cgem_helix;
203}
204
205void CgemSegmentFun::fcnTrk(int &npar, double *gin, double &f, double *par, int iflag){
206
207 HepPoint3D pivot(0.,0.,0.);
208 HepVector trkpar(5);
209 for(int i = 0; i < 5; i++){
210 trkpar[i] = par[i];
211 }
212 GetChisqF(trkpar, pivot, f);
213// GetLikelihoodF(trkpar, pivot, f);
214}
215
216double CgemSegmentFun::fLadder(double x, double delta_phi){
217 if(fabs(x)>delta_phi) return 0.;
218 else return 0.5/delta_phi;
219}
220
221double CgemSegmentFun::fGauss(double x, double mean, double sigma){
222 return 1./sqrt(2*M_PI)/sigma*exp(-1.*pow((x-mean)/sigma,2)/2);
223}
224
226 double m_rad = helix.radius();
227 double l = helix.center().perp();
228
229 double cosPhi = (m_rad * m_rad + l * l - r * r) / (2 * m_rad * l);
230
231 if(cosPhi < -1 || cosPhi > 1) return 0;
232
233 double dPhi = helix.center().phi() - acos(cosPhi) - helix.phi0();
234
235 if(dPhi < -M_PI) dPhi += 2 * M_PI;
236
237 return dPhi;
238}
239
240double CgemSegmentFun::GetKappaAfterFit(int charge, double* rec_phi){
241
242 double x[3]={0.};double y[3]= {0.};
243 for(int i = 0; i < 3; i++){
244 x[i] = r_layer[i]*cos(rec_phi[i]); //get the position on the anode
245 y[i] = r_layer[i]*sin(rec_phi[i]);
246 }
247 double x0,y0,r;
248 x0 = ((pow(x[2],2)+pow(y[2],2))*(y[1]-y[0])+(pow(x[1],2)+pow(y[1],2))*(y[0]-y[2])+(pow(x[0],2)+pow(y[0],2))*(y[2]-y[1]))/(2.*((x[2] - x[0])*(y[1] - y[0]) - (x[1] -x[0])*(y[2] - y[0])));
249 y0 = ((pow(x[2],2)+pow(y[2],2))*(x[1]-x[0])+(pow(x[1],2)+pow(y[1],2))*(x[0]-x[2])+(pow(x[0],2)+pow(y[0],2))*(x[2]-x[1]))/(2.*((y[2] - y[0])*(x[1] - x[0]) - (y[1] -y[0])*(x[2] - x[0])));
250 r = sqrt(pow(x[1]-x0, 2) + pow(y[1]-y0,2));
251
252 return 333.564*charge/r;
253}
254
255void CgemSegmentFun::GetHelixVarBeforeFit(int charge, double& d0, double& phi0, double& a_kappa, double& z0, double& tanl){
256
257 double x[3]={0.};double y[3]= {0.};
258 for(int i = 0; i < 3; i++){
259 x[i] = rl[i]*cos(vec_phi[i]);
260 y[i] = rl[i]*sin(vec_phi[i]);
261 }
262 double x0,y0,r;
263 x0 = ((pow(x[2],2)+pow(y[2],2))*(y[1]-y[0])+(pow(x[1],2)+pow(y[1],2))*(y[0]-y[2])+(pow(x[0],2)+pow(y[0],2))*(y[2]-y[1]))/(2.*((x[2] - x[0])*(y[1] - y[0]) - (x[1] -x[0])*(y[2] - y[0])));
264 y0 = ((pow(x[2],2)+pow(y[2],2))*(x[1]-x[0])+(pow(x[1],2)+pow(y[1],2))*(x[0]-x[2])+(pow(x[0],2)+pow(y[0],2))*(x[2]-x[1]))/(2.*((y[2] - y[0])*(x[1] - x[0]) - (y[1] -y[0])*(x[2] - x[0])));
265
266
267 r = sqrt(pow(x[1]-x0, 2) + pow(y[1]-y0,2));
268// cout<<"r = "<<r<<endl;
269// cout<<"x0 = "<<x0<<"\ty0 = "<<y0<<endl;
270 d0 = sqrt(x0*x0 + y0*y0) - r;
271 if(d0 >= 0) d0 = charge*d0;
272 else d0 = -charge*d0;
273 //phi0 = atan2(y0,x0) + M_PI/2.;
274 phi0 = atan2(-y0*charge,-x0*charge);
275 //phi0 = atan2(y0*charge,x0*charge);
276 //phi0 = atan2(y0,x0);
277 if(phi0 < 0)phi0 += 2.*M_PI;
278 // omega = 1/r;
279 double pt_temp= r/333.564*charge;
280 a_kappa = 1./pt_temp;
281
282 TGraph* Zz = new TGraph(3);
283 TF1* f1 = new TF1("f1","[0]+[1]*x",-1000.,1000.);
284 double s[3];
285 for(int i = 0; i < 3; i++){
286 s[i] = r*acos(((x0 - x[i])*x0 + (y0 - y[i])*y0)/r/sqrt(x0*x0 + y0*y0));
287 Zz->SetPoint(i,s[i],vec_z[i]);
288 //cout<<"s, z = "<<s[i]<<", "<<vec_z[i]<<"; cosAng = "<<((x0 - x[i])*x0 + (y0 - y[i])*y0)/r/sqrt(x0*x0 + y0*y0)<<", phi0 = "<<phi0<<endl;
289 }
290 Zz->Fit("f1","Q");
291 z0 = f1->GetParameter(0);
292 tanl = f1->GetParameter(1);
293 delete f1;
294 delete Zz;
295
296 //std::cout<<" Get Helix Var before global fit PatPar :"<<d0<<"\t"<<phi0<<"\t"<<a_kappa<<"\t"<<z0<<"\t"<<tanl<< std::endl;
297}
298
299double CgemSegmentFun::detA(double arcs[10][10],int n)
300{
301 if(n==1)
302 {
303 return arcs[0][0];
304 }
305 double ans = 0;
306 double temp[10][10]={0.0};
307 int i,j,k;
308 for(i=0;i<n;i++)
309 {
310 for(j=0;j<n-1;j++)
311 {
312 for(k=0;k<n-1;k++)
313 {
314 temp[j][k] = arcs[j+1][(k>=i)?k+1:k];
315
316 }
317 }
318 double t = detA(temp,n-1);
319 if(i%2==0)
320 {
321 ans += arcs[0][i]*t;
322 }
323 else
324 {
325 ans -= arcs[0][i]*t;
326 }
327 }
328 return ans;
329}
330
331bool CgemSegmentFun::DetSquareMatrix(HepMatrix m, double &det)
332{
333 det = 0.;
334 cout<<"m="<<m<<endl;
335 int nrow=m.num_row();
336 int ncol=m.num_col();
337 if(nrow!=ncol||nrow<=0||ncol<=0) return false;
338
339 int n = nrow;
340 double arcs[10][10];
341 for(int i=0; i<n; i++)
342 for(int j=0; j<n; j++)
343 arcs[i][j]=m(i+1,j+1);
344 det = detA(arcs,n);
345 return true;
346}
347
348bool CgemSegmentFun::GetMatrixInverse(double src[10][10],int n,double des[10][10])
349{
350 double flag=detA(src,n);
351 double t[10][10];
352 if(flag==0)
353 {
354 return false;
355 }
356 else
357 {
358 getAStart(src,n,t);
359 for(int i=0;i<n;i++)
360 {
361 for(int j=0;j<n;j++)
362 {
363 des[i][j]=t[i][j]/flag;
364 }
365
366 }
367 }
368
369
370 return true;
371
372}
373
374void CgemSegmentFun::getAStart(double arcs[10][10],int n,double ans[10][10])
375{
376 if(n==1)
377 {
378 ans[0][0] = 1;
379 return;
380 }
381 int i,j,k,t;
382 double temp[10][10];
383 for(i=0;i<n;i++)
384 {
385 for(j=0;j<n;j++)
386 {
387 for(k=0;k<n-1;k++)
388 {
389 for(t=0;t<n-1;t++)
390 {
391 temp[k][t] = arcs[k>=i?k+1:k][t>=j?t+1:t];
392 }
393 }
394
395
396 ans[j][i] = detA(temp,n-1);
397 if((i+j)%2 == 1)
398 {
399 ans[j][i] = - ans[j][i];
400 }
401 }
402 }
403}
404
405bool CgemSegmentFun::InverseSymMatrix(HepSymMatrix m, HepSymMatrix &m_inv)
406{
407 HepMatrix mm=m;
408 int n = mm.num_row();
409 double arcs[10][10];
410 for(int i=0; i<n; i++)
411 for(int j=0; j<n; j++)
412 arcs[i][j]=mm(i+1,j+1);
413 double des[10][10];
414 bool stat = GetMatrixInverse(arcs,n,des);
415 if(stat) {
416 for(int i=0; i<n; i++)
417 for(int j=0; j<=i; j++)
418 m_inv(i+1,j+1)=des[i][j];
419 }
420 return stat;
421}
422
423/*global variables of CGEM Geomtry*/
424double CgemSegmentFun::rl[3] = {79.838/10. ,125.104/10. ,167.604/10.}; //cm
425double CgemSegmentFun::a_stero[3]= {(45.94*3.1415926/180),(31.10*3.1415926/180),(32.99*3.1415926/180)};
426double CgemSegmentFun::r_layer[3]= {87.5026/10.,132.7686/10.,175.2686/10.}; //cm
429double CgemSegmentFun::x_reso[3] = {0.1372/10.,0.1476/10.,0.1412/10.}; //cm
430double CgemSegmentFun::v_reso[3] = {0.1273/10.,0.1326/10.,0.1378/10.}; //cm
431
432double CgemSegmentFun::phi_reso[3] = {0.000371, 0.000214, 0.000165}; //'
433double CgemSegmentFun::phi_mean[3] = {0.000009, 0.000003,-0.000002}; //'
434//double CgemSegmentFun::phi_mean[3] = {0., 0., 0.}; //'
435//double CgemSegmentFun::v_reso[3] = { 0.013702, 0.013136, 0.012774}; //cm
436double CgemSegmentFun::v_mean[3] = {-0.000208, 0.000465,-0.000598}; //cm
437//double CgemSegmentFun::v_mean[3] = {0., 0., 0.}; //cm
438double CgemSegmentFun::DeltaPhi[3] = {0.0033, 0.0022, 0.0016}; //'
439
440/*global variables of CGEM segment*/
double tan(const BesAngle a)
Definition BesAngle.h:216
double sin(const BesAngle a)
Definition BesAngle.h:210
double cos(const BesAngle a)
Definition BesAngle.h:213
const double a_stero[3]
const double r_layer[3]
const Int_t n
TFile * f1
Double_t x[10]
EvtComplex exp(const EvtComplex &c)
XmlRpcServer s
const double x_reso[3]
const double v_reso[3]
***************************************************************************************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
#define M_PI
Definition TConstant.h:4
const HepPoint3D & center(void) const
returns position of helix center(z = 0.);
HepPoint3D x(double dPhi=0.) const
returns position after rotating angle dPhi in phi direction.
void getAStart(double arcs[10][10], int n, double ans[10][10])
bool DetSquareMatrix(HepMatrix m, double &det)
void GetHelixVarBeforeFit(int charge, double &d0, double &phi0, double &omega, double &z0, double &tanl)
HepSymMatrix InvC(int iLayer)
bool InverseSymMatrix(HepSymMatrix m, HepSymMatrix &m_inv)
double fLadder(double x, double delta_phi)
double GetKappaAfterFit(int charge, double *rec_phi)
double fGauss(double x, double mean, double sigma)
bool GetMatrixInverse(double src[10][10], int n, double des[10][10])
double detA(double arcs[10][10], int n)
void GetLikelihoodF(CLHEP::HepVector trkpar, HepPoint3D pivot, double &chisq)
HepVector CalculateHelix(CLHEP::HepVector trkpar, HepPoint3D pivot)
void fcnTrk(int &npar, double *gin, double &f, double *par, int iflag)
void GetChisqF(CLHEP::HepVector trkpar, HepPoint3D pivot, double &chisq)
double IntersectCylinder(double r, KalmanFit::Helix helix)
int t()
Definition t.c:1