CGEM BOSS 6.6.5.h
BESIII Offline Software System
Loading...
Searching...
No Matches
KalFitTrack.cxx
Go to the documentation of this file.
1
2//#include "TrackUtil/Helix.h"
9#include "MdcGeomSvc/MdcGeomSvc.h"
10#include "GaudiKernel/Bootstrap.h"
11#include "CLHEP/Matrix/Vector.h"
12#include "CLHEP/Matrix/Matrix.h"
13#include "CLHEP/Matrix/SymMatrix.h"
14#include "CLHEP/Vector/ThreeVector.h"
15#include "CLHEP/Units/PhysicalConstants.h"
16#include "Math/Point3Dfwd.h"
17#include "Math/Point3D.h"
18#include "Math/Vector3D.h"
22
23using namespace ROOT::Math;
24
25//using namespace KalmanFit;
26
27using namespace CLHEP;
28
29
30// for debug purpose .
33int KalFitTrack::numf_ = 0;
35double KalFitTrack::Bznom_ = 10;
39// Mdc factors :
42double KalFitTrack::chi2_hitf_ = 1000;
43double KalFitTrack::chi2_hits_ = 1000;
45//double KalFitTrack::chi2_hitf_ = DBL_MAX;
46int KalFitTrack::lead_ = 2;
47int KalFitTrack::back_ = 1;
48// attention resolflag !!!
51// chi2 cut set :
53//int KalFitTrack::LR_ = 0;
54int KalFitTrack::LR_ = 1;
55
56
57double KalFitTrack::dchi2cutf_anal[43] = {0.};
58double KalFitTrack::dchi2cuts_anal[43] = {0.};
59double KalFitTrack::dchi2cutf_calib[43] = {0.};
60double KalFitTrack::dchi2cuts_calib[43] = {0.};
61
62
63///
65
66
67const double KalFitTrack::MASS[NMASS] = { 0.000510999,
68 0.105658,
69 0.139568,
70 0.493677,
71 0.938272 };
72const double
73M_PI2 = 2. * M_PI;
74
75const double
76M_PI4 = 4. * M_PI;
77
78const double
79M_PI8 = 8. * M_PI;
80
81
82
83// constructor
85 const HepVector& a,
86 const HepSymMatrix& Ea,
87 unsigned int m, double chisq, unsigned int nhits)
88: KalmanFit::Helix(pivot, a, Ea), type_(0), insist_(0), chiSq_(0),
89 nchits_(0), nster_(0), ncath_(0),
90 ndf_back_(0), chiSq_back_(0), pathip_(0),
91 path_rd_(0), path_ab_(0), tof_(0), dchi2_max_(0), r_max_(0),
92 tof_kaon_(0), tof_proton_(0), pat1_(0), pat2_(0), layer_prec_(0),
93 trasan_id_(0), nhit_r_(0), nhit_z_(0),pathSM_(0),tof2_(0)
94{
95 memset(PathL_, 0, sizeof(PathL_));
96 l_mass_ = m;
97 if (m < NMASS) mass_ = MASS[m];
98 else mass_ = MASS[2];
99 r0_ = fabs(center().perp() - fabs(radius()));
100 //bFieldZ(Bznom_);
101 Bznom_=bFieldZ(); // 2012-09-13 wangll
102 update_last();
103
104 resetLayerUsed();// myLayerUsed
105}
106
107// destructor
109{
110 // delete all objects
111
112}
113
115{
116 pivot_last_ = pivot();
117 a_last_ = a();
118 Ea_last_ = Ea();
119}
120
122{
123 pivot_forMdc_ = pivot();
124 a_forMdc_ = a();
125 Ea_forMdc_ = Ea();
126}
127
129{
130 double m_rad = radius();
131 double l = center().perp();
132
133 double cosPhi = (m_rad * m_rad + l * l - r * r) / (2 * m_rad * l);
134
135 if(cosPhi < -1 || cosPhi > 1) return 0;
136
137 double dPhi = center().phi() - acos(cosPhi) - phi0();
138
139 if(dPhi < -M_PI) dPhi += 2 * M_PI;
140
141 return dPhi;
142}
143
145 double y) const
146{
147 HepPoint3D xc = plane * center();
148 double r = radius();
149 double d = r * r - (y - xc.y()) * (y - xc.y());
150 if(d < 0) return 0;
151
152 double xx = xc.x();
153 if(xx > 0) xx -= sqrt(d);
154 else xx += sqrt(d);
155
156 double l = (plane.inverse() *
157 HepPoint3D(xx, y, 0)).perp();
158
159 return intersect_cylinder(l);
160}
161
163 double x) const
164{
165 HepPoint3D xc = plane * center();
166 double r = radius();
167 double d = r * r - (x - xc.x()) * (x - xc.x());
168 if(d < 0) return 0;
169
170 double yy = xc.y();
171 if(yy > 0) yy -= sqrt(d);
172 else yy += sqrt(d);
173
174 double l = (plane.inverse() *
175 HepPoint3D(x, yy, 0)).perp();
176
177 return intersect_cylinder(l);
178}
179
181{
182 if (tanl() != 0 && radius() != 0)
183 return (pivot().z() + dz() - z) / (radius() * tanl());
184 else return 0;
185}
186
187void KalFitTrack::ms(double path,
188 const KalFitMaterial& material, int index)
189{
190 HepSymMatrix ea = Ea();
191 //cout<<"ms():path "<<path<<endl;
192 //cout<<"ms():ea before: "<<ea<<endl;
193 double k = kappa();
194 double t = tanl();
195 double t2 = t * t;
196 double pt2 = 1 + t2;
197 double k2 = k * k;
198
199 double pmag = 1 / fabs(k) * sqrt(pt2);
200 double dth = material.mcs_angle(mass_, path, pmag);
201 double dth2 = dth * dth;
202 double pt2dth2 = pt2 * dth2;
203
204 ea[1][1] += pt2dth2;
205 ea[2][2] += k2 * t2 * dth2;
206 ea[2][4] += k * t * pt2dth2;
207 ea[4][4] += pt2 * pt2dth2;
208
209 ea[3][3] += dth2 * path * path /3 / (1 + t2);
210 ea[3][4] += dth2 * path/2 * sqrt(1 + t2);
211 ea[3][2] += dth2 * t / sqrt(1 + t2) * k * path/2;
212 ea[0][0] += dth2 * path * path/3;
213 ea[0][1] += dth2 * sqrt(1 + t2) * path/2;
214
215 Ea(ea);
216 //cout<<"ms():ms angle in this: "<<dth<<endl;
217 //cout<<"ms():ea after: "<<Ea()<<endl;
218 if (index < 0) {
219 double x0 = material.X0();
220 if (x0) path_rd_ += path/x0;
221 }
222}
223
224void KalFitTrack::msgasmdc(double path, int index)
225{
226 double k = kappa();
227 double t = tanl();
228 double t2 = t * t;
229 double k2 = k * k;
230
231 double pmag = ( 1 / fabs(k) ) * sqrt(1 + t2);
232 double psq = pmag*pmag;
233 /*
234 double Zprims = 3/2*0.076 + 0.580/9.79*4.99*(4.99+1) +
235 0.041/183.85*74*(74+1) + 0.302/26.98 * 13 * (13+1);
236 double chicc = 0.00039612 * sqrt(Zprims * 0.001168);
237 double dth = 2.557 * chicc * sqrt(path * (mass_*mass_ + psq)) / psq;
238 */
239
240 //std::cout<<" mdcGasRadlen: "<<mdcGasRadlen_<<std::endl;
241 double pathx = path/mdcGasRadlen_;
242 double dth = 0.0136* sqrt(pathx * (mass_*mass_ + psq))/psq
243 *(1 + 0.038 * log(pathx));;
244 HepSymMatrix ea = Ea();
245#ifdef YDEBUG
246 cout<<"msgasmdc():path "<<path<<" pathx "<<pathx<<endl;
247 cout<<"msgasmdc():dth "<<dth<<endl;
248 cout<<"msgasmdc():ea before: "<<ea<<endl;
249#endif
250 double dth2 = dth * dth;
251
252 ea[1][1] += (1 + t2) * dth2;
253 ea[2][2] += k2 * t2 * dth2;
254 ea[2][4] += k * t * (1 + t2) * dth2;
255 ea[4][4] += (1 + t2) * (1 + t2) * dth2;
256
257 // additionnal terms (terms proportional to l and l^2)
258
259 ea[3][3] += dth2 * path * path /3 / (1 + t2);
260 ea[3][4] += dth2 * path/2 * sqrt(1 + t2);
261 ea[3][2] += dth2 * t / sqrt(1 + t2) * k * path/2;
262 ea[0][0] += dth2 * path * path/3;
263 ea[0][1] += dth2 * sqrt(1 + t2) * path/2;
264
265 Ea(ea);
266#ifdef YDEBUG
267 cout<<"msgasmdc():ea after: "<<Ea()<<endl;
268#endif
269 if (index < 0) {
270 pathip_ += path;
271 // RMK : put by hand, take care !!
272 double x0 = mdcGasRadlen_; // for the Mdc gas
273 path_rd_ += path/x0;
274 tof(path);
275#ifdef YDEBUG
276 cout<<"ms...pathip..."<<pathip_<<endl;
277#endif
278 }
279}
280
281void KalFitTrack::eloss(double path,
282 const KalFitMaterial& material, int index)
283{
284#ifdef YDEBUG
285 cout<<"eloss():ea before: "<<Ea()<<endl;
286#endif
287 HepVector v_a = a();
288 double v_a_2_2 = v_a[2] * v_a[2];
289 double v_a_4_2 = v_a[4] * v_a[4];
290 double pmag = 1 / fabs(v_a[2]) * sqrt(1 + v_a_4_2);
291 double psq = pmag * pmag;
292 double E = sqrt(mass_ * mass_ + psq);
293 double dE = material.dE(mass_, path, pmag);
294 //std::cout<<" eloss(): dE: "<<dE<<std::endl;//wangll
295
296 if (index > 0)
297 psq += dE * (dE + 2 * sqrt(mass_ * mass_ + psq));
298 else {
299 double dE_max = E - mass_;
300 if( dE<dE_max ) psq += dE * (dE - 2 * sqrt(mass_ * mass_ + psq));
301 else psq=-1.0;
302 }
303
304 if (tofall_ && index < 0){
305 // Kaon case :
306 if (p_kaon_ > 0){
307 double psq_kaon = p_kaon_ * p_kaon_;
308 double dE_kaon = material.dE(MASS[3], path, p_kaon_);
309 psq_kaon += dE_kaon * (dE_kaon -
310 2 * sqrt(MASS[3] * MASS[3] + psq_kaon));
311 if (psq_kaon < 0) psq_kaon = 0;
312 p_kaon_ = sqrt(psq_kaon);
313 }
314 // Proton case :
315 if (p_proton_ > 0){
316 double psq_proton = p_proton_ * p_proton_;
317 double dE_proton = material.dE(MASS[4], path, p_proton_);
318 psq_proton += dE_proton * (dE_proton -
319 2 * sqrt(MASS[4] * MASS[4] + psq_proton));
320 if (psq_proton < 0) psq_proton = 0;
321 p_proton_ = sqrt(psq_proton);
322 }
323 }
324
325 double dpt;
326 //cout<<"eloss(): psq = "<<psq<<endl;//wangll
327 if(psq < 0) dpt = 9999;
328 else dpt = v_a[2] * pmag / sqrt(psq);
329 //cout<<"eloss():k: "<<v_a[2]<<" k' "<<dpt<<endl;//wangll
330#ifdef YDEBUG
331 cout<<"eloss():k: "<<v_a[2]<<" k' "<<dpt<<endl;
332#endif
333 // attempt to take account of energy loss for error matrix
334
335 HepSymMatrix ea = Ea();
336 double r_E_prim = (E + dE)/E;
337
338 // 1/ Straggling in the energy loss :
339 if (strag_){
340 double del_E(0);
341 if(l_mass_==0) {
342 del_E = dE*factor_strag_;
343 } else {
344 del_E = material.del_E(mass_, path, pmag);
345 }
346
347 ea[2][2] += (v_a[2] * v_a[2]) * E * E * del_E * del_E / (psq*psq);
348 }
349
350 // Effect of the change of curvature (just variables change):
351 double dpt2 = dpt*dpt;
352 double A = dpt*dpt2/(v_a_2_2*v_a[2])*r_E_prim;
353 double B = v_a[4]/(1+v_a_4_2)*
354 dpt*(1-dpt2/v_a_2_2*r_E_prim);
355
356 double ea_2_0 = A*ea[2][0] + B*ea[4][0];
357 double ea_2_1 = A*ea[2][1] + B*ea[4][1];
358 double ea_2_2 = A*A*ea[2][2] + 2*A*B*ea[2][4] + B*B*ea[4][4];
359 double ea_2_3 = A*ea[2][3] + B*ea[4][3];
360 double ea_2_4 = A*ea[2][4] + B*ea[4][4];
361
362 v_a[2] = dpt;
363 a(v_a);
364
365 ea[2][0] = ea_2_0;
366 //std::cout<<"ea[2][0] is "<<ea[2][0]<<" ea(3,1) is "<<ea(3,1)<<std::endl;
367 ea[2][1] = ea_2_1;
368 //std::cout<<"ea[2][2] is "<<ea[2][2]<<" ea(3,3) is "<<ea(3,3)<<std::endl;
369 ea[2][2] = ea_2_2;
370 ea[2][3] = ea_2_3;
371 ea[2][4] = ea_2_4;
372
373 Ea(ea);
374 //cout<<"eloss():dE: "<<dE<<endl;
375 //cout<<"eloss():A: "<<A<<" B: "<<B<<endl;
376 //cout<<"eloss():ea after: "<<Ea()<<endl;
377 r0_ = fabs(center().perp() - fabs(radius()));
378}
379
381{
382 unsigned int nhit = HitsMdc_.size();
383 KalmanFit::Helix tracktest = *(KalmanFit::Helix*)this;
384
385 KalmanFit::Helix helixOrigin = *(KalmanFit::Helix*)this;
386 HepPoint3D origin(0,0,0);
387 helixOrigin.pivot(origin);
388
389 int ind = 0;
390 double* Rad = new double[nhit];
391 double* Ypos = new double[nhit];
392 for( unsigned i=0 ; i < nhit; i++ ){
393
394 HepPoint3D fwd(HitsMdc_[i].wire().fwd());
395 HepPoint3D bck(HitsMdc_[i].wire().bck());
396 Hep3Vector wire = (CLHEP::Hep3Vector)fwd -(CLHEP::Hep3Vector)bck;
397
398 // Modification for stereo wires :
399 KalmanFit::Helix work = tracktest;
400 work.ignoreErrorMatrix();
401 work.pivot((fwd + bck) * .5);
402 HepPoint3D x0 = (work.x(0).z() - bck.z())
403 / wire.z() * wire + bck;
404
405 tracktest.pivot(x0);
406 Rad[ind] = tracktest.x(0).perp();// radius !
407 HepPoint3D posOnTrk = tracktest.x(0);
408 //Rad[ind] = helixOrigin.flightArc(posOnTrk);// wangll added 2020-05-22
409 Ypos[ind] = x0.y();
410 ind++;
411 //cout<<"Ypos: "<<Ypos[ind-1]<<endl;
412 }
413
414 // Reorder...
415 if (index < 0)
416 for(int j, k = nhit - 1; k >= 0; k = j){
417 j = -1;
418 for(int i = 1; i <= k; i++)
419 if(Rad[i - 1] > Rad[i]){
420 j = i - 1;
421 std::swap(Rad[i], Rad[j]);
422 std::swap(HitsMdc_[i], HitsMdc_[j]);
423 }
424 }
425 if (index > 0)
426 for(int j, k = nhit - 1; k >= 0; k = j){
427 j = -1;
428 for(int i = 1; i <= k; i++)
429 if(Rad[i - 1] < Rad[i]){
430 j = i - 1;
431 std::swap(Rad[i], Rad[j]);
432 std::swap(HitsMdc_[i], HitsMdc_[j]);
433 }
434 }
435 if (index == 0)
436 for(int j, k = nhit - 1; k >= 0; k = j){
437 j = -1;
438 for(int i = 1; i <= k; i++)
439 if(Ypos[i - 1] > Ypos[i]){
440 j = i - 1;
441 std::swap(Ypos[i], Ypos[j]);
442 std::swap(HitsMdc_[i], HitsMdc_[j]);
443 }
444 }
445 delete [] Rad;
446 delete [] Ypos;
447}
448
450 for(int it=0; it<HitsMdc().size()-1; it++){
451 if(HitsMdc_[it].wire().layer().layerId() == HitsMdc_[it+1].wire().layer().layerId())
452 {
453 if((kappa()<0)&&(HitsMdc_[it].wire().localId() > HitsMdc_[it+1].wire().localId())){
454 std::swap(HitsMdc_[it], HitsMdc_[it+1]);
455 }
456 if((kappa()>0)&&(HitsMdc_[it].wire().localId() < HitsMdc_[it+1].wire().localId())){
457 std::swap(HitsMdc_[it], HitsMdc_[it+1]);
458 }
459 }
460 }
461}
462
463
465{
466 unsigned int nhit = HitsMdc_.size();
467 int Num[50] = {0};
468 for( unsigned i=0 ; i < nhit; i++ )
469 Num[HitsMdc_[i].wire().layer().layerId()]++;
470 for( unsigned i=0 ; i < nhit; i++ )
471 if (Num[HitsMdc_[i].wire().layer().layerId()]>2)
472 HitsMdc_[i].chi2(-2);
473}
474
475
476double KalFitTrack::smoother_Mdc(KalFitHitMdc& HitMdc, Hep3Vector& meas, KalFitHelixSeg& seg, double& dchi2, int csmflag)
477{
478 //
479 double lr = HitMdc.LR();
480 // For taking the propagation delay into account :
481 int wire_ID = HitMdc.wire().geoID(); // from 0 to 6796
482 int layerid = HitMdc.wire().layer().layerId();
483 double entrangle = HitMdc.rechitptr()->getEntra();
484 if (wire_ID<0 || wire_ID>6796){ //bes
485 std::cout << "KalFitTrack : wire_ID problem : " << wire_ID << std::endl;
486 return 0;
487 }
488
489 double x[3] ={pivot().x(), pivot().y(), pivot().z()};
490 double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
491
492 double tofest(0);
493 double dd(0.);
494 double phi = fmod(phi0() + M_PI4, M_PI2);
495 double csf0 = cos(phi);
496 double snf0 = (1. - csf0) * (1. + csf0);
497 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
498 if(phi > M_PI) snf0 = - snf0;
499
500 if (Tof_correc_) {
501 Hep3Vector ip(0, 0, 0);
502 KalmanFit::Helix work = *(KalmanFit::Helix*)this;
503 work.ignoreErrorMatrix();
504 work.pivot(ip);
505 double phi_ip = work.phi0();
506 if (fabs(phi - phi_ip) > M_PI) {
507 if (phi > phi_ip) phi -= 2 * M_PI;
508 else phi_ip -= 2 * M_PI;
509 }
510 double t = tanl();
511 double l = fabs(radius() * (phi - phi_ip) * sqrt(1 + t * t));
512 double pmag( sqrt( 1.0 + t*t ) / kappa());
513 double mass_over_p( mass_ / pmag );
514 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
515 tofest = l / ( 29.9792458 * beta );
516 if(csmflag==1 && HitMdc.wire().y()>0.) tofest= -1. * tofest;
517 }
518
519 const HepSymMatrix& ea = Ea();
520 const HepVector& v_a = a();
521
522 HepPoint3D pivot_work = pivot();
523
524 double dchi2R(99999999), dchi2L(99999999);
525
526 HepVector v_H(5, 0);
527 v_H[0] = -csf0 * meas.x() - snf0 * meas.y();
528 v_H[3] = -meas.z();
529 HepMatrix v_HT = v_H.T();
530
531 double estim = (v_HT * v_a)[0];
532 HepVector ea_v_H = ea * v_H;
533 HepMatrix ea_v_HT = (ea_v_H).T();
534 HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
535
536 HepSymMatrix eaNew(5);
537 HepVector aNew(5);
538
539 //double time = HitMdc.tdc();
540 // if (Tof_correc_)
541 // time -= tofest;
542
543 double drifttime = getDriftTime(HitMdc , tofest);
544 //cout<<"drifttime = "<<drifttime<<endl;
545 seg.dt(drifttime);
546 double ddl = getDriftDist(HitMdc, drifttime, 0);
547 double ddr = getDriftDist(HitMdc, drifttime, 1);
548 /*cout<<endl<<" $$$ smoother_Mdc():: "<<endl;//wangll
549 cout<<"pivot = "<<pivot()<<endl;//wangll
550 cout<<"helix = "<<a()<<endl;//wangll
551 cout<<"drifttime = "<<drifttime<<endl;//wangll
552 cout<<"ddl, ddr = "<<ddl<<", "<<ddr<<endl;//wangll
553 */
554 double erddl = getSigma( HitMdc, a()[4], 0, ddl);
555 double erddr = getSigma( HitMdc, a()[4], 1, ddr);
556
557 // double dd = 1.0e-4 * 40.0 * time;
558 double dmeas2[2] = { 0.1*ddl, 0.1*ddr }; //millimeter to centimeter
559 double er_dmeas2[2] = {0. , 0.};
560 if(resolflag_== 1) {
561 er_dmeas2[0] = 0.1*erddl;
562 er_dmeas2[1] = 0.1*erddr;
563 }
564 else if(resolflag_ == 0) {
565 //int layid = HitMdc.wire().layer().layerId();
566 //double sigma = getSigma(layid, dd);
567 //er_dmeas2[0] = er_dmeas2[1] = sigma;
568 }
569
570 // Left hypothesis :
571 if (lr == -1) {
572 double er_dmeasL, dmeasL;
573 if(Tof_correc_) {
574 dmeasL = (-1.0)*dmeas2[0]; // the dmeas&erdmeas calculated by myself
575 er_dmeasL = er_dmeas2[0];
576 } else {
577 dmeasL = (-1.0)*fabs(HitMdc.dist()[0]); // the dmeas&erdmeas calculated by track-finding algorithm
578 er_dmeasL = HitMdc.erdist()[0];
579 }
580
581 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
582 eaNew.assign(ea - ea_v_H * AL * ea_v_HT);
583 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
584 if(0. == RkL) RkL = 1.e-4;
585
586 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
587 aNew = v_a + diffL;
588 double sigL = (dmeasL - (v_HT * aNew)[0]);
589 dchi2L = (sigL*sigL) / (RkL * er_dmeasL*er_dmeasL);
590 } else if (lr == 1) {
591 // Right hypothesis :
592
593 double er_dmeasR, dmeasR;
594 if(Tof_correc_) {
595 dmeasR = dmeas2[1];
596 er_dmeasR = er_dmeas2[1];
597 } else {
598 dmeasR = fabs(HitMdc.dist()[1]);
599 er_dmeasR = HitMdc.erdist()[1];
600 }
601
602
603 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
604 eaNew.assign(ea - ea_v_H * AR * ea_v_HT);
605 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
606 if(0. == RkR) RkR = 1.e-4;
607
608 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
609 aNew = v_a + diffR;
610 double sigR = (dmeasR- (v_HT * aNew)[0]);
611 dchi2R = (sigR*sigR) / (RkR * er_dmeasR*er_dmeasR);
612 }
613
614 // Update Kalman result :
615#ifdef YDEBUG
616 cout<<"in smoother_Mdc: lr= "<<lr<<" a: "<<v_a<<" a_NEW: "<<aNew<<endl;
617#endif
618 double dchi2_loc(0);
619 if ((dchi2R < dchi2cuts_anal[layerid] && lr == 1) ||
620 (dchi2L < dchi2cuts_anal[layerid] && lr == -1)) {
621 ndf_back_++;
622 int chge(1);
623 if ( !( aNew[2] && fabs(aNew[2]-a()[2]) < 1.0 ) ) chge=0;
624 if (lr == 1) {
625 chiSq_back_ += dchi2R;
626 dchi2_loc = dchi2R;
627 dd = 0.1*ddr;
628 } else if (lr == -1) {
629 chiSq_back_ += dchi2L;
630 dchi2_loc = dchi2L;
631 dd = -0.1*ddl;
632 }
633 if (chge){
634 a(aNew);
635 Ea(eaNew);
636 }
637 }
638 dchi2=dchi2_loc;
639
640 seg.doca_include(fabs((v_HT*a())[0]));
641 seg.doca_exclude(fabs(estim));
642 /// move the pivot of the helixseg to IP(0,0,0)
643 Hep3Vector ip(0, 0, 0);
644 KalFitTrack helixNew1(pivot_work, a(), Ea(), 0, 0, 0);
645 helixNew1.pivot(ip);
646 HepVector a_temp1 = helixNew1.a();
647 HepSymMatrix ea_temp1 = helixNew1.Ea();
648 seg.Ea(ea_temp1);
649 seg.a(a_temp1);
650 seg.Ea_include(ea_temp1);
651 seg.a_include(a_temp1);
652
653 KalFitTrack helixNew2(pivot_work, v_a, ea, 0, 0, 0);
654 helixNew2.pivot(ip);
655 HepVector a_temp2 = helixNew2.a();
656 HepSymMatrix ea_temp2 = helixNew2.Ea();
657 seg.Ea_exclude(ea_temp2);
658 seg.a_exclude(a_temp2);
659
660 seg.tof(tofest);
661 seg.dd(dd);
662
663 return chiSq_back_;
664}
665
666// Smoothing procedure in Mdc cosmic align
667// RMK attention for the case chi2R = chi2L during forward filter !
668double KalFitTrack::smoother_Mdc_csmalign(KalFitHelixSeg& seg, Hep3Vector& meas,int& flg, int csmflag )
669{
670
671
672 HepPoint3D ip(0., 0., 0.);
673 // attention! we should not to decide the left&right in the smoother process,
674 // because we choose the left&right of hits from the filter process.
675
676 KalFitHitMdc* HitMdc = seg.HitMdc();
677 double lr = HitMdc->LR();
678
679 // For taking the propagation delay into account :
680 int layerid = (*HitMdc).wire().layer().layerId();
681 int wire_ID = HitMdc->wire().geoID();
682 double entrangle = HitMdc->rechitptr()->getEntra();
683
684 if (wire_ID<0 || wire_ID>6796){ //bes
685 std::cout << "KalFitTrack : wire_ID problem : " << wire_ID << std::endl;
686 return 0;
687 }
688
689 double x[3] ={pivot().x(), pivot().y(), pivot().z()};
690 double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
691 double dd(0.);
692 double tofest(0);
693 double phi = fmod(phi0() + M_PI4, M_PI2);
694 double csf0 = cos(phi);
695 double snf0 = (1. - csf0) * (1. + csf0);
696 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
697 if(phi > M_PI) snf0 = - snf0;
698
699 if (Tof_correc_) {
700 Hep3Vector ip(0, 0, 0);
701 KalmanFit::Helix work = *(KalmanFit::Helix*)this;
702 work.ignoreErrorMatrix();
703 work.pivot(ip);
704 double phi_ip = work.phi0();
705 if (fabs(phi - phi_ip) > M_PI) {
706 if (phi > phi_ip) phi -= 2 * M_PI;
707 else phi_ip -= 2 * M_PI;
708 }
709 double t = tanl();
710 double l = fabs(radius() * (phi - phi_ip) * sqrt(1 + t * t));
711 double pmag( sqrt( 1.0 + t*t ) / kappa());
712 double mass_over_p( mass_ / pmag );
713 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
714 tofest = l / ( 29.9792458 * beta );
715 if(csmflag==1 && (*HitMdc).wire().y()>0.) tofest= -1. * tofest;
716 }
717
718 const HepSymMatrix& ea = Ea();
719 const HepVector& v_a = a();
720
721
722 HepPoint3D pivot_work = pivot();
723
724 /*
725 HepVector a_work = a();
726 HepSymMatrix ea_work = Ea();
727
728 KalFitTrack helix_work(pivot_work, a_work, ea_work, 0, 0, 0);
729 helix_work.pivot(ip);
730
731 HepVector a_temp = helix_work.a();
732 HepSymMatrix ea_temp = helix_work.Ea();
733
734 seg.Ea_pre_bwd(ea_temp);
735 seg.a_pre_bwd(a_temp);
736
737 */
738
739 seg.a_pre_bwd(a());
740 seg.Ea_pre_bwd(Ea());
741
742 double dchi2R(99999999), dchi2L(99999999);
743 HepVector v_H(5, 0);
744 v_H[0] = -csf0 * meas.x() - snf0 * meas.y();
745 v_H[3] = -meas.z();
746 HepMatrix v_HT = v_H.T();
747
748 double estim = (v_HT * v_a)[0];
749 HepVector ea_v_H = ea * v_H;
750 HepMatrix ea_v_HT = (ea_v_H).T();
751 HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
752 HepSymMatrix eaNew(5);
753 HepVector aNew(5);
754 double time = (*HitMdc).tdc();
755
756 //seg.dt(time);
757 // if (Tof_correc_)
758 // {
759 // time -= tofest;
760 // seg.dt(time);
761 // }
762 // double dd = 1.0e-4 * 40.0 * time;
763 // seg.dd(dd);
764
765 double drifttime = getDriftTime(*HitMdc , tofest);
766 seg.dt(drifttime);
767 double ddl = getDriftDist(*HitMdc, drifttime, 0 );
768 double ddr = getDriftDist(*HitMdc, drifttime, 1 );
769 double erddl = getSigma( *HitMdc, a()[4], 0, ddl);
770 double erddr = getSigma( *HitMdc, a()[4], 1, ddr);
771
772 double dmeas2[2] = { 0.1*ddl, 0.1*ddr }; // millimeter to centimeter
773 double er_dmeas2[2] = {0., 0.};
774 if(resolflag_ == 1) {
775 er_dmeas2[0] = 0.1*erddl;
776 er_dmeas2[1] = 0.1*erddr;
777 }else if(resolflag_ == 0)
778 {
779 }
780
781 // Left hypothesis :
782 if (lr == -1) {
783 double er_dmeasL, dmeasL;
784 if(Tof_correc_) {
785 dmeasL = (-1.0)*dmeas2[0];
786 er_dmeasL = er_dmeas2[0];
787 } else {
788 dmeasL = (-1.0)*fabs((*HitMdc).dist()[0]);
789 er_dmeasL = (*HitMdc).erdist()[0];
790 }
791
792
793 //if(layerid < 4) er_dmeasL*=2.0;
794
795 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
796 eaNew.assign(ea - ea_v_H * AL * ea_v_HT);
797 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
798 if(0. == RkL) RkL = 1.e-4;
799
800 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
801 aNew = v_a + diffL;
802 double sigL = (dmeasL - (v_HT * aNew)[0]);
803 dchi2L = (sigL*sigL) / (RkL * er_dmeasL*er_dmeasL);
804 } else if (lr == 1) {
805 // Right hypothesis :
806
807 double er_dmeasR, dmeasR;
808 if(Tof_correc_) {
809 dmeasR = dmeas2[1];
810 er_dmeasR = er_dmeas2[1];
811 } else {
812 dmeasR = fabs((*HitMdc).dist()[1]);
813 er_dmeasR = (*HitMdc).erdist()[1];
814 }
815
816
817 //if(layerid < 4) er_dmeasR*=2.0;
818
819
820 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
821 eaNew.assign(ea - ea_v_H * AR * ea_v_HT);
822 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
823 if(0. == RkR) RkR = 1.e-4;
824
825 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
826 aNew = v_a + diffR;
827 double sigR = (dmeasR- (v_HT * aNew)[0]);
828 dchi2R = (sigR*sigR) / (RkR * er_dmeasR*er_dmeasR);
829 }
830
831 // Update Kalman result :
832#ifdef YDEBUG
833 cout<<"in smoother_Mdc: lr= "<<lr<<" a: "<<v_a<<" a_NEW: "<<aNew<<endl;
834#endif
835 double dchi2_loc(0);
836 if ((dchi2R < dchi2cuts_calib[layerid] && lr == 1) ||
837 (dchi2L < dchi2cuts_calib[layerid] && lr == -1)) {
838
839 ndf_back_++;
840 flg = 1;
841 int chge(1);
842 if (!(aNew[2] && fabs(aNew[2]-a()[2]) < 1.0)) chge = 0;
843 if (lr == 1) {
844 chiSq_back_ += dchi2R;
845 dchi2_loc = dchi2R;
846 dd = 0.1*ddr;
847 // if(debug_ ==4) std::cout<<"in smoother "<<std::endl;
848
849 } else if (lr == -1) {
850 chiSq_back_ += dchi2L;
851 dchi2_loc = dchi2L;
852 dd = -0.1*ddl;
853
854 }
855 if (chge){
856 a(aNew);
857 Ea(eaNew);
858 }
859
860 seg.a_filt_bwd(aNew);
861 seg.Ea_filt_bwd(eaNew);
862
863 HepVector a_pre_fwd_temp=seg.a_pre_fwd();
864 if( (a_pre_fwd_temp[1]-seg.a_pre_bwd()[1]) > 3. * M_PI /2.) a_pre_fwd_temp[1]-= M_PI2;
865 if( (a_pre_fwd_temp[1]-seg.a_pre_bwd()[1]) < -3. * M_PI /2. ) a_pre_fwd_temp[1]+= M_PI2;
866
867 seg.a_pre_fwd(a_pre_fwd_temp);
868
869 HepVector a_pre_filt_temp=seg.a_filt_fwd();
870 if( (a_pre_filt_temp[1]-seg.a_pre_bwd()[1]) > 3. * M_PI /2. ) a_pre_filt_temp[1] -= M_PI2;
871 if( (a_pre_filt_temp[1]-seg.a_pre_bwd()[1]) < -3. * M_PI /2.) a_pre_filt_temp[1] += M_PI2;
872 seg.a_filt_fwd(a_pre_filt_temp);
873
874 /*
875 KalFitTrack helixNew(pivot_work, aNew, eaNew, 0, 0, 0);
876 helixNew.pivot(ip);
877 a_temp = helixNew.a();
878 ea_temp = helixNew.Ea();
879 seg.Ea_filt_bwd(ea_temp);
880 seg.a_filt_bwd(a_temp);
881 */
882
883 int inv;
884
885 if(debug_ == 4){
886 std::cout<<"seg.Ea_pre_bwd().inverse(inv) ..."<<seg.Ea_pre_bwd().inverse(inv)<<std::endl;
887 std::cout<<"seg.Ea_pre_fwd().inverse(inv) ..."<<seg.Ea_pre_fwd().inverse(inv)<<std::endl;
888 }
889
890 HepSymMatrix Ea_pre_comb = (seg.Ea_pre_bwd().inverse(inv)+seg.Ea_pre_fwd().inverse(inv)).inverse(inv);
891 seg.Ea_exclude(Ea_pre_comb);
892
893
894 if(debug_ == 4) {
895 std::cout<<"Ea_pre_comb_ ... "<<Ea_pre_comb<<std::endl;
896 std::cout<<"seg.a_pre_bwd()..."<<seg.a_pre_bwd()<<std::endl;
897 std::cout<<"seg.a_pre_fwd()..."<<seg.a_pre_fwd()<<std::endl;
898 }
899 HepVector a_pre_comb = Ea_pre_comb*((seg.Ea_pre_bwd().inverse(inv))*seg.a_pre_bwd()+(seg.Ea_pre_fwd().inverse(inv))*seg.a_pre_fwd());
900 seg.a_exclude(a_pre_comb);
901
902 if(debug_ == 4) {
903 std::cout<<"seg.Ea_filt_fwd()..."<<seg.Ea_filt_fwd()<<std::endl;
904 std::cout<<"seg.Ea_filt_fwd().inverse(inv)..."<<seg.Ea_filt_fwd().inverse(inv)<<std::endl;
905 }
906 seg.Ea((seg.Ea_filt_fwd().inverse(inv)+seg.Ea_pre_bwd().inverse(inv)).inverse(inv));
907 seg.Ea_include((seg.Ea_filt_fwd().inverse(inv)+seg.Ea_pre_bwd().inverse(inv)).inverse(inv));
908
909 if(debug_ == 4) {
910 std::cout<<"seg.Ea() is ..."<<seg.Ea()<<std::endl;
911 std::cout<<"seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd() ..."<<seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd()<<std::endl;
912 std::cout<<"seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd() ... "<<seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd()<<std::endl;
913 }
914
915 seg.a(seg.Ea()*(seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd()+seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd()));
916 seg.a_include(seg.Ea()*(seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd()+seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd()));
917
918 if(inv != 0) {
919 //std::cout<<"ERROR OCCUR WHEN INVERSE MATRIX !"<<std::endl;
920 }
921
922 seg.residual_exclude(dd - (v_HT*a_pre_comb)[0]);
923 seg.residual_include(dd - (v_HT*seg.a())[0]);
924 seg.doca_exclude(fabs((v_HT*a_pre_comb)[0]));
925 seg.doca_include(fabs((v_HT*seg.a())[0]));
926
927 if(debug_ == 4){
928 std::cout<<"the dd in smoother_Mdc is "<<dd
929 <<" the (v_HT*a_pre_comb)[0] is "<<(v_HT*a_pre_comb)[0]<<std::endl;
930 }
931
932 //compare the two method to calculate the include doca value :
933 if(debug_ == 4){
934 std::cout<<"method 1 ..."<<sqrt(seg.a()[0]*seg.a()[0]+seg.a()[3]*seg.a()[3])<<std::endl;
935 std::cout<<"method 2 ..."<<fabs((v_HT*seg.a())[0])<<std::endl;
936 }
937
938
939 /// move the pivot of the helixseg to IP(0,0,0)
940 KalFitTrack helixNew1(pivot_work, seg.a(), seg.Ea(), 0, 0, 0);
941 helixNew1.pivot(ip);
942 HepVector a_temp1 = helixNew1.a();
943 HepSymMatrix ea_temp1 = helixNew1.Ea();
944 seg.Ea(ea_temp1);
945 seg.a(a_temp1);
946 seg.Ea_include(ea_temp1);
947 seg.a_include(a_temp1);
948
949 KalFitTrack helixNew2(pivot_work, seg.a_exclude(), seg.Ea_exclude(), 0, 0, 0);
950 helixNew2.pivot(ip);
951 HepVector a_temp2 = helixNew2.a();
952 HepSymMatrix ea_temp2 = helixNew2.Ea();
953 seg.Ea_exclude(ea_temp2);
954 seg.a_exclude(a_temp2);
955
956 seg.tof(tofest);
957 seg.dd(dd);
958
959 }
960 return chiSq_back_;
961}
962
963// Smoothing procedure in Mdc calib
964// RMK attention for the case chi2R = chi2L during forward filter !
965double KalFitTrack::smoother_Mdc(KalFitHelixSeg& seg, Hep3Vector& meas,int& flg, int csmflag )
966{
967
968
969 HepPoint3D ip(0., 0., 0.);
970 // attention! we should not to decide the left&right in the smoother process,
971 // because we choose the left&right of hits from the filter process.
972
973 KalFitHitMdc* HitMdc = seg.HitMdc();
974 double lr = HitMdc->LR();
975
976 // For taking the propagation delay into account :
977 int layerid = (*HitMdc).wire().layer().layerId();
978 int wire_ID = HitMdc->wire().geoID();
979 double entrangle = HitMdc->rechitptr()->getEntra();
980
981 if (wire_ID<0 || wire_ID>6796){ //bes
982 std::cout << "KalFitTrack : wire_ID problem : " << wire_ID << std::endl;
983 return 0;
984 }
985
986 double x[3] ={pivot().x(), pivot().y(), pivot().z()};
987 double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
988 double dd(0.);
989 double tofest(0);
990 double phi = fmod(phi0() + M_PI4, M_PI2);
991 double csf0 = cos(phi);
992 double snf0 = (1. - csf0) * (1. + csf0);
993 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
994 if(phi > M_PI) snf0 = - snf0;
995
996 if (Tof_correc_) {
997 Hep3Vector ip(0, 0, 0);
998 KalmanFit::Helix work = *(KalmanFit::Helix*)this;
999 work.ignoreErrorMatrix();
1000 work.pivot(ip);
1001 double phi_ip = work.phi0();
1002 if (fabs(phi - phi_ip) > M_PI) {
1003 if (phi > phi_ip) phi -= 2 * M_PI;
1004 else phi_ip -= 2 * M_PI;
1005 }
1006 double t = tanl();
1007 double l = fabs(radius() * (phi - phi_ip) * sqrt(1 + t * t));
1008 double pmag( sqrt( 1.0 + t*t ) / kappa());
1009 double mass_over_p( mass_ / pmag );
1010 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
1011 tofest = l / ( 29.9792458 * beta );
1012 if(csmflag==1 && (*HitMdc).wire().y()>0.) tofest= -1. * tofest;
1013 }
1014
1015 const HepSymMatrix& ea = Ea();
1016 const HepVector& v_a = a();
1017
1018
1019
1020 HepPoint3D pivot_work = pivot();
1021
1022 /*
1023 HepVector a_work = a();
1024 HepSymMatrix ea_work = Ea();
1025
1026 KalFitTrack helix_work(pivot_work, a_work, ea_work, 0, 0, 0);
1027 helix_work.pivot(ip);
1028
1029 HepVector a_temp = helix_work.a();
1030 HepSymMatrix ea_temp = helix_work.Ea();
1031
1032 seg.Ea_pre_bwd(ea_temp);
1033 seg.a_pre_bwd(a_temp);
1034
1035 */
1036
1037 seg.a_pre_bwd(a());
1038 seg.Ea_pre_bwd(Ea());
1039
1040 double dchi2R(99999999), dchi2L(99999999);
1041 HepVector v_H(5, 0);
1042 v_H[0] = -csf0 * meas.x() - snf0 * meas.y();
1043 v_H[3] = -meas.z();
1044 HepMatrix v_HT = v_H.T();
1045
1046 double estim = (v_HT * v_a)[0];
1047 HepVector ea_v_H = ea * v_H;
1048 HepMatrix ea_v_HT = (ea_v_H).T();
1049 HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
1050 HepSymMatrix eaNew(5);
1051 HepVector aNew(5);
1052 double time = (*HitMdc).tdc();
1053
1054 //seg.dt(time);
1055 // if (Tof_correc_)
1056 // {
1057 // time -= tofest;
1058 // seg.dt(time);
1059 // }
1060 // double dd = 1.0e-4 * 40.0 * time;
1061 // seg.dd(dd);
1062
1063 double drifttime = getDriftTime(*HitMdc , tofest);
1064 seg.dt(drifttime);
1065 double ddl = getDriftDist(*HitMdc, drifttime, 0 );
1066 double ddr = getDriftDist(*HitMdc, drifttime, 1 );
1067 double erddl = getSigma( *HitMdc, a()[4], 0, ddl);
1068 double erddr = getSigma( *HitMdc, a()[4], 1, ddr);
1069
1070 double dmeas2[2] = { 0.1*ddl, 0.1*ddr }; // millimeter to centimeter
1071 double er_dmeas2[2] = {0., 0.};
1072 if(resolflag_ == 1) {
1073 er_dmeas2[0] = 0.1*erddl;
1074 er_dmeas2[1] = 0.1*erddr;
1075 }else if(resolflag_ == 0)
1076 {
1077 }
1078
1079 // Left hypothesis :
1080 if (lr == -1) {
1081 double er_dmeasL, dmeasL;
1082 if(Tof_correc_) {
1083 dmeasL = (-1.0)*dmeas2[0];
1084 er_dmeasL = er_dmeas2[0];
1085 } else {
1086 dmeasL = (-1.0)*fabs((*HitMdc).dist()[0]);
1087 er_dmeasL = (*HitMdc).erdist()[0];
1088 }
1089
1090
1091 //if(layerid < 4) er_dmeasL*=2.0;
1092
1093 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
1094 eaNew.assign(ea - ea_v_H * AL * ea_v_HT);
1095 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
1096 if(0. == RkL) RkL = 1.e-4;
1097
1098 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
1099 aNew = v_a + diffL;
1100 double sigL = (dmeasL - (v_HT * aNew)[0]);
1101 dchi2L = (sigL*sigL) / (RkL * er_dmeasL*er_dmeasL);
1102 } else if (lr == 1) {
1103 // Right hypothesis :
1104
1105 double er_dmeasR, dmeasR;
1106 if(Tof_correc_) {
1107 dmeasR = dmeas2[1];
1108 er_dmeasR = er_dmeas2[1];
1109 } else {
1110 dmeasR = fabs((*HitMdc).dist()[1]);
1111 er_dmeasR = (*HitMdc).erdist()[1];
1112 }
1113
1114
1115 //if(layerid < 4) er_dmeasR*=2.0;
1116
1117
1118 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
1119 eaNew.assign(ea - ea_v_H * AR * ea_v_HT);
1120 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
1121 if(0. == RkR) RkR = 1.e-4;
1122
1123 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
1124 aNew = v_a + diffR;
1125 double sigR = (dmeasR- (v_HT * aNew)[0]);
1126 dchi2R = (sigR*sigR) / (RkR * er_dmeasR*er_dmeasR);
1127 }
1128
1129 // Update Kalman result :
1130#ifdef YDEBUG
1131 cout<<"in smoother_Mdc: lr= "<<lr<<" a: "<<v_a<<" a_NEW: "<<aNew<<endl;
1132#endif
1133 double dchi2_loc(0);
1134 if ((dchi2R < dchi2cuts_calib[layerid] && lr == 1) ||
1135 (dchi2L < dchi2cuts_calib[layerid] && lr == -1)) {
1136
1137 ndf_back_++;
1138 flg = 1;
1139 int chge(1);
1140 if (!(aNew[2] && fabs(aNew[2]-a()[2]) < 1.0)) chge = 0;
1141 if (lr == 1) {
1142 chiSq_back_ += dchi2R;
1143 dchi2_loc = dchi2R;
1144 dd = 0.1*ddr;
1145 // if(debug_ ==4) std::cout<<"in smoother "<<std::endl;
1146
1147 } else if (lr == -1) {
1148 chiSq_back_ += dchi2L;
1149 dchi2_loc = dchi2L;
1150 dd = -0.1*ddl;
1151
1152 }
1153 if (chge){
1154 a(aNew);
1155 Ea(eaNew);
1156 }
1157
1158 seg.a_filt_bwd(aNew);
1159 seg.Ea_filt_bwd(eaNew);
1160
1161 HepVector a_pre_fwd_temp=seg.a_pre_fwd();
1162 if( (a_pre_fwd_temp[1]-seg.a_pre_bwd()[1]) > 3. * M_PI /2.) a_pre_fwd_temp[1]-= M_PI2;
1163 if( (a_pre_fwd_temp[1]-seg.a_pre_bwd()[1]) < -3. * M_PI /2. ) a_pre_fwd_temp[1]+= M_PI2;
1164
1165 seg.a_pre_fwd(a_pre_fwd_temp);
1166
1167 HepVector a_pre_filt_temp=seg.a_filt_fwd();
1168 if( (a_pre_filt_temp[1]-seg.a_pre_bwd()[1]) > 3. * M_PI /2. ) a_pre_filt_temp[1] -= M_PI2;
1169 if( (a_pre_filt_temp[1]-seg.a_pre_bwd()[1]) < -3. * M_PI /2.) a_pre_filt_temp[1] += M_PI2;
1170 seg.a_filt_fwd(a_pre_filt_temp);
1171
1172 /*
1173 KalFitTrack helixNew(pivot_work, aNew, eaNew, 0, 0, 0);
1174 helixNew.pivot(ip);
1175 a_temp = helixNew.a();
1176 ea_temp = helixNew.Ea();
1177 seg.Ea_filt_bwd(ea_temp);
1178 seg.a_filt_bwd(a_temp);
1179 */
1180
1181 int inv;
1182
1183 if(debug_ == 4){
1184 std::cout<<"seg.Ea_pre_bwd().inverse(inv) ..."<<seg.Ea_pre_bwd().inverse(inv)<<std::endl;
1185 std::cout<<"seg.Ea_pre_fwd().inverse(inv) ..."<<seg.Ea_pre_fwd().inverse(inv)<<std::endl;
1186 }
1187
1188 HepSymMatrix Ea_pre_comb = (seg.Ea_pre_bwd().inverse(inv)+seg.Ea_pre_fwd().inverse(inv)).inverse(inv);
1189 seg.Ea_exclude(Ea_pre_comb);
1190
1191
1192 if(debug_ == 4) {
1193 std::cout<<"Ea_pre_comb_ ... "<<Ea_pre_comb<<std::endl;
1194 std::cout<<"seg.a_pre_bwd()..."<<seg.a_pre_bwd()<<std::endl;
1195 std::cout<<"seg.a_pre_fwd()..."<<seg.a_pre_fwd()<<std::endl;
1196 }
1197 HepVector a_pre_comb = Ea_pre_comb*((seg.Ea_pre_bwd().inverse(inv))*seg.a_pre_bwd()+(seg.Ea_pre_fwd().inverse(inv))*seg.a_pre_fwd());
1198 seg.a_exclude(a_pre_comb);
1199
1200
1201 if(debug_ == 4) {
1202 std::cout<<"seg.Ea_filt_fwd()..."<<seg.Ea_filt_fwd()<<std::endl;
1203 std::cout<<"seg.Ea_filt_fwd().inverse(inv)..."<<seg.Ea_filt_fwd().inverse(inv)<<std::endl;
1204 }
1205 seg.Ea((seg.Ea_filt_fwd().inverse(inv)+seg.Ea_pre_bwd().inverse(inv)).inverse(inv));
1206 seg.Ea_include((seg.Ea_filt_fwd().inverse(inv)+seg.Ea_pre_bwd().inverse(inv)).inverse(inv));
1207
1208 if(debug_ == 4) {
1209 std::cout<<"seg.Ea() is ..."<<seg.Ea()<<std::endl;
1210 std::cout<<"seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd() ..."<<seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd()<<std::endl;
1211 std::cout<<"seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd() ... "<<seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd()<<std::endl;
1212 }
1213
1214 seg.a(seg.Ea()*(seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd()+seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd()));
1215 seg.a_include(seg.Ea()*(seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd()+seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd()));
1216
1217 if(inv != 0) {
1218 //std::cout<<"ERROR OCCUR WHEN INVERSE MATRIX !"<<std::endl;
1219 }
1220
1221 seg.residual_exclude(dd - (v_HT*a_pre_comb)[0]);
1222 seg.residual_include(dd - (v_HT*seg.a())[0]);
1223 seg.doca_exclude(fabs((v_HT*a_pre_comb)[0]));
1224 seg.doca_include(fabs((v_HT*seg.a())[0]));
1225
1226 if(debug_ == 4){
1227 std::cout<<"the dd in smoother_Mdc is "<<dd
1228 <<" the (v_HT*a_pre_comb)[0] is "<<(v_HT*a_pre_comb)[0]<<std::endl;
1229 }
1230
1231 //compare the two method to calculate the include doca value :
1232 if(debug_ == 4){
1233 std::cout<<"method 1 ..."<<sqrt(seg.a()[0]*seg.a()[0]+seg.a()[3]*seg.a()[3])<<std::endl;
1234 std::cout<<"method 2 ..."<<fabs((v_HT*seg.a())[0])<<std::endl;
1235 }
1236
1237
1238 /// move the pivot of the helixseg to IP(0,0,0)
1239 KalFitTrack helixNew1(pivot_work, seg.a(), seg.Ea(), 0, 0, 0);
1240 helixNew1.pivot(ip);
1241 HepVector a_temp1 = helixNew1.a();
1242 HepSymMatrix ea_temp1 = helixNew1.Ea();
1243 seg.Ea(ea_temp1);
1244 seg.a(a_temp1);
1245 seg.Ea_include(ea_temp1);
1246 seg.a_include(a_temp1);
1247
1248 KalFitTrack helixNew2(pivot_work, seg.a_exclude(), seg.Ea_exclude(), 0, 0, 0);
1249 helixNew2.pivot(ip);
1250 HepVector a_temp2 = helixNew2.a();
1251 HepSymMatrix ea_temp2 = helixNew2.Ea();
1252 seg.Ea_exclude(ea_temp2);
1253 seg.a_exclude(a_temp2);
1254
1255 seg.tof(tofest);
1256 seg.dd(dd);
1257
1258 }
1259 return chiSq_back_;
1260}
1261
1262
1263
1264
1265
1266double KalFitTrack::filter(double v_m, const HepVector& m_H,
1267 double v_d, double m_V)
1268{
1269 HepMatrix m_HT = m_H.T();
1270 HepPoint3D x0 = x(0);
1271 HepVector v_x(3);
1272 v_x[0] = x0.x();
1273 v_x[1] = x0.y();
1274 v_x[2] = x0.z();
1275 HepMatrix m_X = delXDelA(0);
1276 HepMatrix m_XT = m_X.T();
1277 HepMatrix m_C = m_X * Ea() * m_XT;
1278 //int err;
1279 HepVector m_K = 1 / (m_V + (m_HT * m_C * m_H)[0]) * m_H;
1280 HepVector v_a = a();
1281 HepSymMatrix ea = Ea();
1282 HepMatrix mXTmK = m_XT * m_K;
1283 double v_r = v_m - v_d - (m_HT * v_x)[0];
1284 v_a += ea * mXTmK * v_r;
1285 a(v_a);
1286 ea.assign(ea - ea * mXTmK * m_HT * m_X * ea);
1287 Ea(ea);
1288 // Record last hit included parameters :
1289 update_last();
1290 HepMatrix mCmK = m_C * m_K;
1291 v_x += mCmK * v_r;
1292 m_C -= mCmK * m_HT * m_C;
1293 v_r = v_m - v_d - (m_HT * v_x)[0];
1294 double m_R = m_V - (m_HT * m_C * m_H)[0];
1295 double chiSq = v_r * v_r / m_R;
1296 chiSq_ += chiSq;
1297 return chiSq;
1298}
1299
1300
1301void KalFitTrack::path_add(double path)
1302{
1303 pathip_ += path;
1304 tof(path);
1305}
1306
1307
1308void KalFitTrack::addPathSM(double path){
1309 pathSM_ += path;
1310}
1311
1312
1314 tof2_ += time;
1315}
1316
1317
1318void KalFitTrack::fiTerm(double fi){
1319 fiTerm_ = fi;
1320}
1321
1322
1323void KalFitTrack::tof(double path)
1324{
1325 double light_speed( 29.9792458 ); // light speed in cm/nsec
1326 double t = tanl();
1327 double pmag( sqrt( 1.0 + t*t ) / kappa());
1328 if (pmag !=0) {
1329 double mass_over_p( mass_ / pmag );
1330 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
1331 tof_ += path / ( light_speed * beta );
1332 }
1333
1334 if (tofall_) {
1335 if (p_kaon_ > 0){
1336 double massk_over_p( MASS[3] / p_kaon_ );
1337 double beta_kaon( 1.0 / sqrt( 1.0 + massk_over_p * massk_over_p ) );
1338 tof_kaon_ += path / (light_speed * beta_kaon);
1339 }
1340 if (p_proton_ > 0){
1341 double massp_over_p( MASS[4] / p_proton_ );
1342 double beta_proton( 1.0 / sqrt( 1.0 + massp_over_p * massp_over_p ) );
1343 tof_proton_ += path / (light_speed * beta_proton);
1344 }
1345 }
1346}
1347
1348double KalFitTrack::radius_numf(void) const {
1349
1350 double Bz(Bznom_);
1351 //std::cout<<"Bz: "<<Bz<<std::endl;
1352 if (numf_ > 10){
1353 double dr = a()[0];
1354 double phi0 = a()[1];
1355 double dz = a()[3];
1356 double phi = fmod(phi0 + M_PI4, M_PI2);
1357 double csf0 = cos(phi);
1358 double snf0 = (1. - csf0) * (1. + csf0);
1359 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
1360 if(phi > M_PI) snf0 = - snf0;
1361 //XYZPoint
1362 HepPoint3D x0((pivot().x() + dr*csf0),
1363 (pivot().y() + dr*snf0),
1364 (pivot().z() + dz));
1365
1366 //XYZVector b;
1367 HepVector3D b;
1368
1369 //std::cout<<"b: "<<b<<std::endl;
1370
1371 MFSvc_->fieldVector(10.*x0, b);
1372
1373 //std::cout<<"b: "<<b<<std::endl;
1374
1375
1376 b = 10000.*b;
1377 Bz = b.z();
1378 }
1379 if (Bz == 0)
1380 Bz = Bznom_;
1381 double ALPHA_loc = 10000./2.99792458/Bz;
1382 return ALPHA_loc / a()[2];
1383}
1384
1385const HepPoint3D &
1387
1388 int nstep(1);
1389 HepPoint3D delta_x((newPivot-pivot()).x()/double(inner_steps_),
1390 (newPivot-pivot()).y()/double(inner_steps_),
1391 (newPivot-pivot()).z()/double(inner_steps_));
1392 int i = 1;
1393
1394 while (i <= inner_steps_) {
1395 HepPoint3D nextPivot(pivot()+delta_x);
1396 double xnp(nextPivot.x()), ynp(nextPivot.y()), znp(nextPivot.z());
1397 HepSymMatrix Ea_now = Ea();
1398 HepPoint3D piv(pivot());
1399 double xp(piv.x()), yp(piv.y()), zp(piv.z());
1400 double dr = a()[0];
1401 double phi0 = a()[1];
1402 double kappa = a()[2];
1403 double dz = a()[3];
1404 double tanl = a()[4];
1405 double m_rad(0);
1406 if (numfcor_ == 1)
1407 m_rad = radius_numf();
1408 else
1409 m_rad = radius();
1410 double rdr = dr + m_rad;
1411 double phi = fmod(phi0 + M_PI4, M_PI2);
1412 double csf0 = cos(phi);
1413 double snf0 = (1. - csf0) * (1. + csf0);
1414 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
1415 if(phi > M_PI) snf0 = - snf0;
1416
1417 double xc = xp + rdr * csf0;
1418 double yc = yp + rdr * snf0;
1419 double csf = (xc - xnp) / m_rad;
1420 double snf = (yc - ynp) / m_rad;
1421 double anrm = sqrt(csf * csf + snf * snf);
1422 csf /= anrm;
1423 snf /= anrm;
1424 phi = atan2(snf, csf);
1425 double phid = fmod(phi - phi0 + M_PI8, M_PI2);
1426 if(phid > M_PI) phid = phid - M_PI2;
1427 double drp = (xp + dr * csf0 + m_rad * (csf0 - csf) - xnp)
1428 * csf
1429 + (yp + dr * snf0 + m_rad * (snf0 - snf) - ynp) * snf;
1430 double dzp = zp + dz - m_rad * tanl * phid - znp;
1431
1432 HepVector ap(5);
1433 ap[0] = drp;
1434 ap[1] = fmod(phi + M_PI4, M_PI2);
1435 ap[2] = kappa;
1436 ap[3] = dzp;
1437 ap[4] = tanl;
1438
1439 // Modification due to non uniform magnetic field :
1440 if (numf_ > 10) {
1441
1442 Hep3Vector x1(xp + dr*csf0, yp + dr*snf0, zp + dz);
1443 double csf0p = cos(ap[1]);
1444 double snf0p = (1. - csf0p) * (1. + csf0p);
1445 snf0p = sqrt((snf0p > 0.) ? snf0p : 0.);
1446 if(ap[1] > M_PI) snf0p = - snf0p;
1447
1448 Hep3Vector x2(xnp + drp*csf0p,
1449 ynp + drp*snf0p,
1450 znp + dzp);
1451 Hep3Vector dist((x1 - x2).x()/100.0,
1452 (x1 - x2).y()/100.0,
1453 (x1 - x2).z()/100.0);
1454 HepPoint3D middlebis((x1.x() + x2.x())/2,
1455 (x1.y() + x2.y())/2,
1456 (x1.z() + x2.z())/2);
1457 HepVector3D field;
1458 MFSvc_->fieldVector(10.*middlebis,field);
1459 field = 1000.*field;
1460 Hep3Vector dB(field.x(),
1461 field.y(),
1462 (field.z()-0.1*Bznom_));
1463 if (field.z()) {
1464 double akappa(fabs(kappa));
1465 double sign = kappa/akappa;
1466 HepVector dp(3);
1467 dp = 0.299792458 * sign * dB.cross(dist);
1468 HepVector dhp(3);
1469 dhp[0] = -akappa*(dp[0]*csf0p+dp[1]*snf0p);
1470 dhp[1] = kappa*akappa*(dp[0]*snf0p-dp[1]*csf0p);
1471 dhp[2] = dp[2]*akappa+dhp[1]*tanl/kappa;
1472 if (numfcor_ == 0){
1473 ap[1] += dhp[0];
1474 }
1475 ap[2] += dhp[1];
1476 ap[4] += dhp[2];
1477 }
1478 }
1479 HepMatrix m_del = delApDelA(ap);
1480 Ea_now.assign(m_del * Ea_now * m_del.T());
1481 pivot(nextPivot);
1482 a(ap);
1483 Ea(Ea_now);
1484 i++;
1485 }
1486 return newPivot;
1487}
1488
1489const HepPoint3D &
1490KalFitTrack::pivot_numf(const HepPoint3D & newPivot, double & pathl) {
1491
1492 KalmanFit::Helix tracktest = *(KalmanFit::Helix*)this;
1493 tracktest.ignoreErrorMatrix();
1494 double tl = a()[4];
1495 double th = 90.0 - 180.0*M_1_PI*atan( tl );
1496 /*
1497 int nstep(1);
1498 if (steplev_ == 1)
1499 nstep = 3;
1500 else if (steplev_ == 2 && (th > 140 || th <45))
1501 if ((pivot()-newPivot).mag()<3.)
1502 nstep = 3;
1503 else
1504 nstep = 6;
1505 */
1506 Hep3Vector delta_x((newPivot-pivot()).x()/double(outer_steps_),
1507 (newPivot-pivot()).y()/double(outer_steps_),
1508 (newPivot-pivot()).z()/double(outer_steps_));
1509 int i = 1;
1510
1511 while (i <= outer_steps_) {
1512 HepPoint3D nextPivot(pivot()+delta_x);
1513 double xnp(nextPivot.x()), ynp(nextPivot.y()), znp(nextPivot.z());
1514
1515 HepSymMatrix Ea_now = Ea();
1516 HepPoint3D piv(pivot());
1517 double xp(piv.x()), yp(piv.y()), zp(piv.z());
1518
1519 double dr = a()[0];
1520 double phi0 = a()[1];
1521 double kappa = a()[2];
1522 double dz = a()[3];
1523 double tanl = a()[4];
1524
1525 double m_rad(0);
1526 m_rad = radius();
1527
1528 double rdr = dr + m_rad;
1529 double phi = fmod(phi0 + M_PI4, M_PI2);
1530 double csf0 = cos(phi);
1531 double snf0 = (1. - csf0) * (1. + csf0);
1532 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
1533 if(phi > M_PI) snf0 = - snf0;
1534
1535 double xc = xp + rdr * csf0;
1536 double yc = yp + rdr * snf0;
1537 double csf = (xc - xnp) / m_rad;
1538 double snf = (yc - ynp) / m_rad;
1539 double anrm = sqrt(csf * csf + snf * snf);
1540 csf /= anrm;
1541 snf /= anrm;
1542 phi = atan2(snf, csf);
1543 double phid = fmod(phi - phi0 + M_PI8, M_PI2);
1544 if(phid > M_PI) phid = phid - M_PI2;
1545 double drp = (xp + dr * csf0 + m_rad * (csf0 - csf) - xnp)
1546 * csf
1547 + (yp + dr * snf0 + m_rad * (snf0 - snf) - ynp) * snf;
1548 double dzp = zp + dz - m_rad * tanl * phid - znp;
1549
1550 HepVector ap(5);
1551 ap[0] = drp;
1552 ap[1] = fmod(phi + M_PI4, M_PI2);
1553 ap[2] = kappa;
1554 ap[3] = dzp;
1555 ap[4] = tanl;
1556
1557 //std::cout<<" numf_: "<<numf_<<std::endl;
1558
1559 // Modification due to non uniform magnetic field :
1560 if (numf_ > 10) {
1561
1562 Hep3Vector x1(xp + dr*csf0, yp + dr*snf0, zp + dz);
1563
1564 double csf0p = cos(ap[1]);
1565 double snf0p = (1. - csf0p) * (1. + csf0p);
1566 snf0p = sqrt((snf0p > 0.) ? snf0p : 0.);
1567 if(ap[1] > M_PI) snf0p = - snf0p;
1568
1569 Hep3Vector x2(xnp + drp*csf0p,
1570 ynp + drp*snf0p,
1571 znp + dzp);
1572
1573 Hep3Vector dist((x1 - x2).x()/100.0,
1574 (x1 - x2).y()/100.0,
1575 (x1 - x2).z()/100.0);
1576
1577 HepPoint3D middlebis((x1.x() + x2.x())/2,
1578 (x1.y() + x2.y())/2,
1579 (x1.z() + x2.z())/2);
1580
1581 HepVector3D field;
1582 MFSvc_->fieldVector(10.*middlebis,field);
1583 field = 1000.*field;
1584
1585 //std::cout<<"B field: "<<field<<std::endl;
1586 Hep3Vector dB(field.x(),
1587 field.y(),
1588 (field.z()-0.1*Bznom_));
1589
1590
1591 //std::cout<<" dB: "<<dB<<std::endl;
1592
1593
1594 if (field.z()) {
1595 double akappa(fabs(kappa));
1596 double sign = kappa/akappa;
1597 HepVector dp(3);
1598 dp = 0.299792458 * sign * dB.cross(dist);
1599
1600 //std::cout<<"dp: "<<dp<<std::endl;
1601
1602 HepVector dhp(3);
1603 dhp[0] = -akappa*(dp[0]*csf0p+dp[1]*snf0p);
1604 dhp[1] = kappa*akappa*(dp[0]*snf0p-dp[1]*csf0p);
1605 dhp[2] = dp[2]*akappa+dhp[1]*tanl/kappa;
1606
1607
1608 //std::cout<<"dhp: "<<dhp<<std::endl;
1609
1610
1611 ap[1] += dhp[0];
1612 ap[2] += dhp[1];
1613 ap[4] += dhp[2];
1614 }
1615 }
1616 HepMatrix m_del = delApDelA(ap);
1617 Ea_now.assign(m_del * Ea_now * m_del.T());
1618 pivot(nextPivot);
1619 a(ap);
1620 Ea(Ea_now);
1621 i++;
1622
1623 //std::cout<<" a: "<<a()<<std::endl;
1624 }
1625
1626 // Estimation of the path length :
1627 double tanl_0(tracktest.a()[4]);
1628 double phi0_0(tracktest.a()[1]);
1629 double radius_0(tracktest.radius());
1630 tracktest.pivot(newPivot);
1631
1632 double phi0_1 = tracktest.a()[1];
1633 if (fabs(phi0_1 - phi0_0) > M_PI) {
1634 if (phi0_1 > phi0_0) phi0_1 -= 2 * M_PI;
1635 else phi0_0 -= 2 * M_PI;
1636 }
1637 if(phi0_1 == phi0_0) phi0_1 = phi0_0 + 1.e-10;
1638 pathl = fabs(radius_0 * (phi0_1 - phi0_0)
1639 * sqrt(1 + tanl_0 * tanl_0));
1640 return newPivot;
1641}
1642
1643// function to calculate the path length in the layer
1644/*
1645 double KalFitTrack::PathL(int layer){
1646 HepPoint3D piv(pivot());
1647 double phi0 = a()[1];
1648 double kappa = a()[2];
1649 double tanl = a()[4];
1650
1651#ifdef YDEBUG
1652cout<<"helix "<<a()<<endl;
1653#endif
1654// Choose the local field !!
1655double Bz(Bznom_);
1656if (numf_ > 10){
1657HepVector3D b;
1658MFSvc_->fieldVector(10.*piv, b);
1659b = 10000.*b;
1660Bz=b.z();
1661}
1662double ALPHA_loc = 10000./2.99792458/Bz;
1663int charge = ( kappa >= 0 )? 1 : -1;
1664double rho = ALPHA_loc/kappa;
1665double pt = fabs( 1.0/kappa );
1666double lambda = 180.0*M_1_PI*atan( tanl );
1667double theta = 90.0 - lambda;
1668theta = 2.0*M_PI*theta/360.;
1669double phi = fmod(phi0 + M_PI4, M_PI2);
1670double csf0 = cos(phi);
1671double snf0 = (1. - csf0) * (1. + csf0);
1672snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
1673if(phi > M_PI) snf0 = - snf0;
1674
1675double x_c = piv.x() + ( dr() + rho )*csf0;
1676double y_c = piv.y() + ( dr() + rho )*snf0;
1677double z_c = piv.z() + dz();
1678HepPoint3D ccenter(x_c, y_c, 0);
1679double m_c_perp(ccenter.perp());
1680Hep3Vector m_c_unit(((CLHEP::Hep3Vector)ccenter).unit());
1681#ifdef YDEBUG
1682cout<<"rho=..."<<rho<<endl;
1683cout<<"ccenter "<<ccenter<<" m_c_unit "<<m_c_unit<<endl;
1684#endif
1685//phi resion estimation
1686double phi_io[2];
1687HepPoint3D IO = charge*m_c_unit;
1688double dphi0 = fmod( IO.phi()+4*M_PI,2*M_PI ) - phi;
1689if( dphi0 > M_PI ) dphi0 -= 2*M_PI;
1690else if( dphi0 < -M_PI ) dphi0 += 2*M_PI;
1691phi_io[0] = -(1+charge)*M_PI_2 - charge*dphi0;
1692phi_io[1] = phi_io[0]+1.5*M_PI;
1693double phi_in(0); /// for debug
1694
1695double m_crio[2];
1696double m_zb, m_zf;
1697int m_div;
1698
1699// retrieve Mdc geometry information
1700// IMdcGeomSvc* geosvc;
1701StatusCode sc= Gaudi::svcLocator()->service("MdcGeomSvc", geosvc);
1702if(sc==StatusCode::FAILURE) cout << "GeoSvc failing!!!!!!!SC=" << sc << endl;
1703
1704MdcGeomSvc* geomsvc = dynamic_cast<MdcGeomSvc*>(iGeomSvc_);
1705if(!geomsvc){
1706std::cout<<"ERROR OCCUR when dynamic_cast in KalFitTrack.cxx !!"<<std::endl;
1707}
1708double rcsiz1 = geomsvc->Layer(layer)->RCSiz1();
1709double rcsiz2 = geomsvc->Layer(layer)->RCSiz2();
1710double rlay = geomsvc->Layer(layer)->Radius();
1711m_zb = 0.5*(geomsvc->Layer(layer)->Length());
1712m_zf = -0.5*(geomsvc->Layer(layer)->Length());
1713
1714m_crio[0] = rlay - rcsiz1;
1715m_crio[1] = rlay + rcsiz2;
1716//conversion of the units(mm=>cm)
1717m_crio[0] = 0.1*m_crio[0];
1718m_crio[1] = 0.1*m_crio[1];
1719m_zb = 0.1*m_zb;
1720m_zf = 0.1*m_zf;
1721
1722int sign = 1; ///assumed the first half circle
1723int epflag[2];
1724Hep3Vector iocand;
1725Hep3Vector cell_IO[2];
1726double dphi;
1727for( int i = 0; i < 2; i++ ){
1728 double cos_alpha = m_c_perp*m_c_perp + m_crio[i]*m_crio[i] - rho*rho;
1729 cos_alpha = 0.5*cos_alpha/( m_c_perp*m_crio[i] );
1730 double Calpha = acos( cos_alpha );
1731 epflag[i] = 0;
1732 iocand = m_c_unit;
1733 iocand.rotateZ( charge*sign*Calpha );
1734 iocand *= m_crio[i];
1735 double xx = iocand.x() - x_c;
1736 double yy = iocand.y() - y_c;
1737 dphi = atan2(yy, xx) - phi0 - M_PI_2*(1+charge);
1738 dphi = fmod( dphi + 8.0*M_PI, 2*M_PI );
1739
1740 if( dphi < phi_io[0] ){
1741 dphi += 2*M_PI;
1742 }
1743 else if( phi_io[1] < dphi ){
1744 dphi -= 2*M_PI;
1745 }
1746
1747 ///in the Local Helix case, phi must be small
1748
1749 Hep3Vector zvector( 0., 0., z_c-rho*dphi*tanl);
1750
1751 double xcio, ycio, phip;
1752 ///// z region check active volume is between zb+2. and zf-2. [cm]
1753 double delrin = 2.0;
1754 if( m_zf-delrin > zvector.z() ){
1755 phip = z_c - m_zb + delrin;
1756 phip = phip/( rho*tanl );
1757 phip = phip + phi0;
1758 xcio = x_c - rho*cos( phip );
1759 ycio = y_c - rho*sin( phip );
1760 cell_IO[i].setX( xcio );
1761 cell_IO[i].setY( ycio );
1762 cell_IO[i].setZ( m_zb+delrin );
1763 epflag[i] = 1;
1764 }
1765 else if( m_zb+delrin < zvector.z() ){
1766 phip = z_c - m_zf-delrin;
1767 phip = phip/( rho*tanl );
1768 phip = phip + phi0;
1769 xcio = x_c - rho*cos( phip );
1770 ycio = y_c - rho*sin( phip );
1771 cell_IO[i].setX( xcio );
1772 cell_IO[i].setY( ycio );
1773 cell_IO[i].setZ( m_zf-delrin );
1774 epflag[i] = 1;
1775 }
1776 else{
1777 cell_IO[i] = iocand;
1778 cell_IO[i] += zvector;
1779 }
1780 /// for debug
1781 if( i == 0 ) phi_in = dphi;
1782}
1783// path length estimation
1784// phi calculation
1785Hep3Vector cl = cell_IO[1] - cell_IO[0];
1786#ifdef YDEBUG
1787cout<<"cell_IO[0] "<<cell_IO[0]<<" cell_IO[1] "<<cell_IO[1]<<" cl "<<cl<<endl;
1788#endif
1789
1790double ch_theta;
1791double ch_dphi;
1792double ch_ltrk = 0;
1793double ch_ltrk_rp = 0;
1794ch_dphi = cl.perp()*0.5/(ALPHA_loc*pt);
1795ch_dphi = 2.0 * asin( ch_dphi );
1796ch_ltrk_rp = ch_dphi*ALPHA_loc*pt;
1797#ifdef YDEBUG
1798cout<<"ch_dphi "<<ch_dphi<<" ch_ltrk_rp "<<ch_ltrk_rp<<" cl.z "<<cl.z()<<endl;
1799#endif
1800ch_ltrk = sqrt( ch_ltrk_rp*ch_ltrk_rp + cl.z()*cl.z() );
1801ch_theta = cl.theta();
1802
1803if( ch_theta < theta*0.85 || 1.15*theta < ch_theta)
1804 ch_ltrk *= -1; /// miss calculation
1805
1806if( epflag[0] == 1 || epflag [1] == 1 )
1807 ch_ltrk *= -1; /// invalid resion for dE/dx or outside of Mdc
1808
1809 mom_[layer] = momentum( phi_in );
1810 PathL_[layer] = ch_ltrk;
1811#ifdef YDEBUG
1812 cout<<"ch_ltrk "<<ch_ltrk<<endl;
1813#endif
1814 return ch_ltrk;
1815 }
1816*/
1817
1818
1820 int j(0);
1821 if (i < 31){
1822 j = (int) pow(2.,i);
1823 if (!(pat1_ & j))
1824 pat1_ = pat1_ | j;
1825 } else if (i < 50) {
1826 j = (int) pow(2.,(i-31));
1827 if (!(pat2_ & j))
1828 pat2_ = pat2_ | j;
1829 }
1830}
1831
1832int KalFitTrack::nmass(void){ return NMASS;}
1833double KalFitTrack::mass(int i){ return MASS[i];}
1835 mass_=MASS[i];
1836 l_mass_=i;
1837}
1838
1839void KalFitTrack::lead(int i){ lead_ = i;}
1840int KalFitTrack::lead(void){ return lead_;}
1841void KalFitTrack::back(int i){ back_ = i;}
1842int KalFitTrack::back(void){ return back_;}
1845void KalFitTrack::numf(int i){ numf_ = i;}
1846int KalFitTrack::numf(void){ return numf_;}
1847void KalFitTrack::LR(int x){ LR_ = x;}
1848void KalFitTrack::HitsMdc(vector<KalFitHitMdc>& lh){ HitsMdc_ = lh;}
1849void KalFitTrack::appendHitsMdc(KalFitHitMdc h){ HitsMdc_.push_back(h);}
1850
1851/*
1852 This member function is in charge of the forward filter,
1853 for the tof correction of the drift distance in the case of cosmic rays
1854 if way=1, it's a down track means same as track in collision data,
1855 if way=-1, it's a up track !
1856 */
1857
1858double KalFitTrack::update_hits(KalFitHitMdc & HitMdc, int inext, Hep3Vector& meas, int way, double& dchi2out, double& dtrack,double& dtracknew, double& dtdc, int csmflag) {
1859
1860 double lr = HitMdc.LR();
1861 //std::cout<<" in update_hits: lr= " << lr <<std::endl;
1862 // For taking the propagation delay into account :
1863 const KalFitWire& Wire = HitMdc.wire();
1864 int wire_ID = Wire.geoID();
1865 int layerid = HitMdc.wire().layer().layerId();
1866 //std::cout<<" when in layer: "<<layerid<<std::endl;
1867 if(debug_ == 4) std::cout<<endl<<"update_hits(): layer, wire = "<<layerid<<", "<<wire_ID<<std::endl;
1868 double entrangle = HitMdc.rechitptr()->getEntra();
1869
1870 if (wire_ID<0 || wire_ID>6796){ //bes
1871 std::cout << " KalFitTrack: wire_ID problem : " << wire_ID
1872 << std::endl;
1873 return 0;
1874 }
1875 int flag_ster = Wire.stereo();
1876 double x[3] ={pivot().x(), pivot().y(), pivot().z()};
1877 double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
1878 double tofest(0);
1879 double phi = fmod(phi0() + M_PI4, M_PI2);
1880 double csf0 = cos(phi);
1881 double snf0 = (1. - csf0) * (1. + csf0);
1882 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
1883 if(phi > M_PI) snf0 = - snf0;
1884 if (Tof_correc_){
1885 double t = tanl();
1886 double dphi(0);
1887
1888 KalmanFit::Helix work = *(KalmanFit::Helix*)this;
1889 work.ignoreErrorMatrix();
1890 double phi_ip(0);
1891 Hep3Vector ip(0, 0, 0);
1892 work.pivot(ip);
1893 phi_ip = work.phi0();
1894 if (fabs(phi - phi_ip) > M_PI) {
1895 if (phi > phi_ip) phi -= 2 * M_PI;
1896 else phi_ip -= 2 * M_PI;
1897 }
1898 dphi = phi - phi_ip;
1899 double l = fabs(radius() * dphi * sqrt(1 + t * t));
1900 double pmag( sqrt( 1.0 + t*t ) / kappa());
1901 double mass_over_p( mass_ / pmag );
1902 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
1903 tofest = l / ( 29.9792458 * beta );
1904 if(csmflag==1 && HitMdc.wire().y()>0.) tofest= -1. * tofest;
1905 }
1906
1907 const HepSymMatrix& ea = Ea();
1908 if(debug_ == 4) std::cout<<endl<<"update_hits(): Ea = "<<ea<<std::endl;
1909 const HepVector& v_a = a();
1910 double dchi2R(99999999), dchi2L(99999999);
1911
1912 HepVector v_H(5, 0);
1913 v_H[0] = -csf0 * meas.x() - snf0 * meas.y();
1914 v_H[3] = -meas.z();
1915 HepMatrix v_HT = v_H.T();
1916
1917 double estim = (v_HT * v_a)[0];
1918 dtrack = estim;
1919 //std::cout<<" in update_hits estim is "<<estim<<std::endl;
1920 HepVector ea_v_H = ea * v_H;
1921 HepMatrix ea_v_HT = (ea_v_H).T();
1922 HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
1923
1924 HepSymMatrix eaNewL(5), eaNewR(5);
1925 HepVector aNewL(5), aNewR(5);
1926
1927 double drifttime = getDriftTime(HitMdc , tofest);
1928 double ddl = getDriftDist(HitMdc, drifttime, 0 );
1929 double ddr = getDriftDist(HitMdc, drifttime, 1 );
1930 double erddl = getSigma( HitMdc, a()[4], 0, ddl);
1931 double erddr = getSigma( HitMdc, a()[4], 1, ddr);
1932
1933 if(debug_ == 4)
1934 {
1935 std::cout<<"drifttime in update_hits() for ananlysis is "<<drifttime<<std::endl;
1936 std::cout<<"ddl in update_hits() for ananlysis is "<<0.1*ddl<<std::endl;
1937 std::cout<<"ddr in update_hits() for ananlysis is "<<0.1*ddr<<std::endl;
1938 std::cout<<"erddl in update_hits() for ananlysis is "<<0.1*erddl<<std::endl;
1939 std::cout<<"erddr in update_hits() for ananlysis is "<<0.1*erddr<<std::endl;
1940 std::cout<<"in update_hits estim is "<<estim<<std::endl;
1941 }
1942
1943 //the following 3 line : tempory
1944 double dmeas2[2] = { 0.1*ddl, 0.1*ddr }; // millimeter to centimeter
1945 double er_dmeas2[2] = {0.,0.};
1946 if(resolflag_ == 1) {
1947 er_dmeas2[0] = 0.1*erddl;
1948 er_dmeas2[1] = 0.1*erddr;
1949 }
1950 else if(resolflag_ == 0) {
1951 // int layid = HitMdc.wire().layer().layerId();
1952 // double sigma = getSigma(layid, dd);
1953 // er_dmeas2[0] = er_dmeas2[1] = sigma;
1954 }
1955
1956 // Left hypothesis :
1957 if ((LR_==0 && lr != 1.0) ||
1958 (LR_==1 && lr == -1.0)) {
1959 double er_dmeasL, dmeasL;
1960 if(Tof_correc_) {
1961 dmeasL = (-1.0)*fabs(dmeas2[0]);
1962 er_dmeasL = er_dmeas2[0];
1963 } else {
1964 dmeasL = (-1.0)*fabs(HitMdc.dist()[0]);
1965 er_dmeasL = HitMdc.erdist()[0];
1966 }
1967 dtdc = dmeasL;
1968 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
1969 eaNewL.assign(ea - ea_v_H * AL * ea_v_HT);
1970 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
1971 if(debug_ == 4)
1972 {
1973 std::cout<<" ea_v_H * AL * ea_v_HT is: "<<ea_v_H * AL * ea_v_HT<<std::endl;
1974 std::cout<<" v_H is: "<<v_H<<" Ea is: "<<ea<<" v_H_T_ea_v_H is: "<<v_H_T_ea_v_H<<std::endl;
1975 std::cout<<" AL is: "<<AL<<" (v_H_T_ea_v_H)[0] * AL is: "<<(v_H_T_ea_v_H)[0]*AL<<std::endl;
1976 }
1977
1978 if(0. == RkL) RkL = 1.e-4;
1979
1980 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
1981 aNewL = v_a + diffL;
1982 double sigL = dmeasL -(v_HT * aNewL)[0];
1983 dtracknew = (v_HT * aNewL)[0];
1984 dchi2L = (sigL * sigL)/(RkL * er_dmeasL*er_dmeasL);
1985
1986 if(debug_ == 4)
1987 {
1988 std::cout<<" fwd_filter : in left hypothesis dchi2L is "<<dchi2L<<std::endl;
1989 std::cout<<" diffL="<<diffL<<std::endl;
1990 }
1991 }
1992
1993 if ((LR_==0 && lr != -1.0) ||
1994 (LR_==1 && lr == 1.0)) {
1995 double er_dmeasR, dmeasR;
1996 if(Tof_correc_) {
1997 dmeasR = dmeas2[1];
1998 er_dmeasR = er_dmeas2[1];
1999 } else {
2000 dmeasR = fabs(HitMdc.dist()[1]);
2001 er_dmeasR = HitMdc.erdist()[1];
2002 }
2003 dtdc = dmeasR;
2004 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
2005 eaNewR.assign(ea - ea_v_H * AR * ea_v_HT);
2006 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
2007
2008 if(debug_ == 4)
2009 {
2010 std::cout<<" ea_v_H * AR * ea_v_HT is: "<<ea_v_H * AR * ea_v_HT<<std::endl;
2011 std::cout<<" v_H is: "<<v_H<<" Ea is: "<<ea<<" v_H_T_ea_v_H is: "<<v_H_T_ea_v_H<<std::endl;
2012 std::cout<<" AR is: "<<AR<<" (v_H_T_ea_v_H)[0] * AR is: "<<(v_H_T_ea_v_H)[0]*AR<<std::endl;
2013 }
2014
2015 if(0. == RkR) RkR = 1.e-4;
2016 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
2017 aNewR = v_a + diffR;
2018 double sigR = dmeasR -(v_HT * aNewR)[0];
2019 dtracknew = (v_HT * aNewR)[0];
2020 dchi2R = (sigR*sigR)/(RkR * er_dmeasR*er_dmeasR);
2021 if(debug_ == 4)
2022 {
2023 std::cout<<" fwd_filter : in right hypothesis dchi2R is "<<dchi2R<<std::endl;
2024 std::cout<<" diffR="<<diffR<<std::endl;
2025 }
2026 }
2027 // Update Kalman result :
2028 double dchi2_loc(0);
2029 double dchi2_beforeCut(9999);
2030 if (fabs(dchi2R-dchi2L)<10. && inext>0) {
2031 KalFitHitMdc & HitMdc_next = HitsMdc_[inext];
2032 KalmanFit::Helix H_fromR(pivot(), aNewR, eaNewR);
2033 double chi2_fromR(chi2_next(H_fromR, HitMdc_next, csmflag));
2034 KalmanFit::Helix H_fromL(pivot(), aNewL, eaNewL);
2035 double chi2_fromL(chi2_next(H_fromL, HitMdc_next, csmflag));
2036#ifdef YDEBUG
2037 std::cout << " chi2_fromL = " << chi2_fromL
2038 << ", chi2_fromR = " << chi2_fromR << std::endl;
2039#endif
2040 if (fabs(chi2_fromR-chi2_fromL)<10.){
2041 int inext2(-1);
2042 if (inext+1<HitsMdc_.size())
2043 for( int k=inext+1 ; k < HitsMdc_.size(); k++ )
2044 if (!(HitsMdc_[k].chi2()<0)) {
2045 inext2 = k;
2046 break;
2047 }
2048
2049 if (inext2 != -1){
2050 KalFitHitMdc & HitMdc_next2 = HitsMdc_[inext2];
2051 double chi2_fromR2(chi2_next(H_fromR, HitMdc_next2, csmflag));
2052 double chi2_fromL2(chi2_next(H_fromL, HitMdc_next2, csmflag));
2053#ifdef YDEBUG
2054 std::cout << " chi2_fromL2 = " << chi2_fromL2
2055 << ", chi2_fromR2 = " << chi2_fromR2 << std::endl;
2056#endif
2057 if (fabs(dchi2R+chi2_fromR+chi2_fromR2-(dchi2L+chi2_fromL+chi2_fromL2))<2) {
2058 if (chi2_fromR2<chi2_fromL2)
2059 dchi2L = DBL_MAX;
2060 else
2061 dchi2R = DBL_MAX;
2062 }
2063 }
2064 }
2065
2066 if (!(dchi2L == DBL_MAX && dchi2R == DBL_MAX)) {
2067 if (dchi2R+chi2_fromR < dchi2L+chi2_fromL){
2068 dchi2L = DBL_MAX;
2069#ifdef YDEBUG
2070 std::cout << " choose right..." << std::endl;
2071#endif
2072 } else {
2073 dchi2R = DBL_MAX;
2074#ifdef YDEBUG
2075 std::cout << " choose left..." << std::endl;
2076#endif
2077 }
2078 }
2079 }
2080
2081 if(LR_==1 && lr == -1) dchi2_beforeCut=dchi2L;
2082 if(LR_==1 && lr == 1) dchi2_beforeCut=dchi2R;
2083
2084 if ((0 < dchi2R && dchi2R < dchi2cutf_anal[layerid]) ||
2085 (0 < dchi2L && dchi2L < dchi2cutf_anal[layerid])) {
2086
2087 if (((LR_==0 && dchi2R < dchi2L && lr != -1) ||
2088 (LR_==1 && lr == 1)) &&
2089 fabs(aNewR[2]-a()[2]) < 1000. && aNewR[2]) {
2090 if (nchits_ < (unsigned) nmdc_hit2_ || dchi2R < dchi2cutf_anal[layerid]){
2091 nchits_++;
2092 if (flag_ster) nster_++;
2093 useLayer(layerid);// myLayerUsed
2094 Ea(eaNewR);
2095 a(aNewR);
2096 chiSq_ += dchi2R;
2097 dchi2_loc = dchi2R;
2098 if (l_mass_ == lead_){
2099 HitMdc.LR(1);
2100 }
2102 }
2103 } else if (((LR_==0 && dchi2L <= dchi2R && lr != 1) ||
2104 (LR_==1 && lr == -1)) &&
2105 fabs(aNewL[2]-a()[2]) < 1000. && aNewL[2]){
2106 if (nchits_ < (unsigned) nmdc_hit2_ || dchi2L < dchi2cutf_anal[layerid]){
2107 nchits_++;
2108 if (flag_ster) nster_++;
2109 useLayer(layerid);// myLayerUsed
2110 Ea(eaNewL);
2111 a(aNewL);
2112 chiSq_ += dchi2L;
2113 dchi2_loc = dchi2L;
2114 if (l_mass_ == lead_){
2115 HitMdc.LR(-1);
2116 }
2118 }
2119 }
2120 }
2121 if(debug_ == 4) {
2122 std::cout<<"a from "<<v_a
2123 <<" to "<<a()
2124 <<std::endl;
2125 }
2126 //cout<<"__FUNCTION__: layerid = "<<layerid<<endl;
2127 if (dchi2_loc > dchi2_max_) {
2128 dchi2_max_ = dchi2_loc ;
2129 r_max_ = pivot().perp();
2130 }
2131 dchi2out = dchi2_loc;
2132 if(dchi2out==0) dchi2out = -dchi2_beforeCut;
2133 //if(dchi2out == 0) {
2134 // dchi2out = ( (dchi2L < dchi2R ) ? dchi2L : dchi2R ) ;
2135 // }
2136 return chiSq_;
2137}
2138
2139
2140double KalFitTrack::update_hits_csmalign(KalFitHelixSeg& HelixSeg, int inext, Hep3Vector& meas,int way, double& dchi2out, int csmflag ) {
2141
2142
2143 HepPoint3D ip(0.,0.,0.);
2144
2146 double lr = HitMdc->LR();
2147 int layerid = (*HitMdc).wire().layer().layerId();
2148 // cout<<"layerid: "<<layerid<<endl;
2149 double entrangle = HitMdc->rechitptr()->getEntra();
2150
2151 if(debug_ == 4) {
2152 std::cout<<"in update_hits(kalFitHelixSeg,...) : the layerid ="<<layerid<<std::endl;
2153 std::cout<<" update_hits: lr= " << lr <<std::endl;
2154 }
2155 // For taking the propagation delay into account :
2156 const KalFitWire& Wire = HitMdc->wire();
2157 int wire_ID = Wire.geoID();
2158
2159 if (wire_ID<0 || wire_ID>6796){ //bes
2160 std::cout << " KalFitTrack: wire_ID problem : " << wire_ID
2161 << std::endl;
2162 return 0;
2163 }
2164 int flag_ster = Wire.stereo();
2165 double x[3] ={pivot().x(), pivot().y(), pivot().z()};
2166 double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
2167 double tofest(0);
2168 double phi = fmod(phi0() + M_PI4, M_PI2);
2169 double csf0 = cos(phi);
2170 double snf0 = (1. - csf0) * (1. + csf0);
2171 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
2172 if(phi > M_PI) snf0 = - snf0;
2173 if (Tof_correc_){
2174 double t = tanl();
2175 double dphi(0);
2176
2177 KalmanFit::Helix work = *(KalmanFit::Helix*)this;
2178 work.ignoreErrorMatrix();
2179 double phi_ip(0);
2180 Hep3Vector ip(0, 0, 0);
2181 work.pivot(ip);
2182 phi_ip = work.phi0();
2183 if (fabs(phi - phi_ip) > M_PI) {
2184 if (phi > phi_ip) phi -= 2 * M_PI;
2185 else phi_ip -= 2 * M_PI;
2186 }
2187 dphi = phi - phi_ip;
2188
2189 double l = fabs(radius() * dphi * sqrt(1 + t * t));
2190 double pmag( sqrt( 1.0 + t*t ) / kappa());
2191 double mass_over_p( mass_ / pmag );
2192 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
2193 tofest = l / ( 29.9792458 * beta );
2194 if(csmflag==1 && (*HitMdc).wire().y()>0.) tofest= -1. * tofest;
2195 }
2196
2197 const HepSymMatrix& ea = Ea();
2198 HelixSeg.Ea_pre_fwd(ea);
2200
2201
2202 const HepVector& v_a = a();
2203 HelixSeg.a_pre_fwd(v_a);
2204 HelixSeg.a_filt_fwd(v_a);
2205
2206 /*
2207
2208 HepPoint3D pivot_work = pivot();
2209 HepVector a_work = a();
2210 HepSymMatrix ea_work = Ea();
2211 KalFitTrack helix_work(pivot_work, a_work, ea_work, 0, 0, 0);
2212 helix_work.pivot(ip);
2213
2214 HepVector a_temp = helix_work.a();
2215 HepSymMatrix ea_temp = helix_work.Ea();
2216
2217 HelixSeg.Ea_pre_fwd(ea_temp);
2218 HelixSeg.a_pre_fwd(a_temp);
2219
2220 // My God, for the protection purpose
2221 HelixSeg.Ea_filt_fwd(ea_temp);
2222 HelixSeg.a_filt_fwd(a_temp);
2223
2224 */
2225
2226 double dchi2R(99999999), dchi2L(99999999);
2227
2228 HepVector v_H(5, 0);
2229 v_H[0] = -csf0 * meas.x() - snf0 * meas.y();
2230 v_H[3] = -meas.z();
2231 HepMatrix v_HT = v_H.T();
2232
2233 double estim = (v_HT * v_a)[0];
2234 HepVector ea_v_H = ea * v_H;
2235 HepMatrix ea_v_HT = (ea_v_H).T();
2236 HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
2237
2238 HepSymMatrix eaNewL(5), eaNewR(5);
2239 HepVector aNewL(5), aNewR(5);
2240#ifdef YDEBUG
2241 cout<<"phi "<<phi<<" snf0 "<<snf0<<" csf0 "<<csf0<<endl
2242 <<" meas: "<<meas <<endl;
2243 cout<<"the related matrix:"<<endl;
2244 cout<<"v_H "<<v_H<<endl;
2245 cout<<"v_a "<<v_a<<endl;
2246 cout<<"ea "<<ea<<endl;
2247 cout<<"v_H_T_ea_v_H "<<v_H_T_ea_v_H<<endl;
2248 cout<<"LR_= "<< LR_ << "lr= " << lr <<endl;
2249#endif
2250
2251 double time = (*HitMdc).tdc();
2252 // if (Tof_correc_)
2253 // time = time - way*tofest;
2254
2255 // double dd = 1.0e-4 * 40.0 * time;
2256 //the following 3 line : tempory
2257
2258 double drifttime = getDriftTime(*HitMdc , tofest);
2259 double ddl = getDriftDist(*HitMdc, drifttime, 0 );
2260 double ddr = getDriftDist(*HitMdc, drifttime, 1 );
2261 double erddl = getSigma( *HitMdc, a()[4], 0, ddl);
2262 double erddr = getSigma( *HitMdc, a()[4], 1, ddr);
2263
2264 if(debug_ == 4){
2265 std::cout<<"the pure drift time is "<<drifttime<<std::endl;
2266 std::cout<<"the drift dist left hypothesis is "<<ddl<<std::endl;
2267 std::cout<<"the drift dist right hypothesis is "<<ddr<<std::endl;
2268 std::cout<<"the error sigma left hypothesis is "<<erddl<<std::endl;
2269 std::cout<<"the error sigma right hypothesis is "<<erddr<<std::endl;
2270 }
2271 double dmeas2[2] = { 0.1*ddl , 0.1*ddr }; //millimeter to centimeter
2272 double er_dmeas2[2] = {0. , 0.} ;
2273
2274 if(resolflag_ == 1) {
2275 er_dmeas2[0] = 0.1*erddl;
2276 er_dmeas2[1] = 0.1*erddr;
2277 }
2278 else if(resolflag_ == 0) {
2279 // int layid = (*HitMdc).wire().layer().layerId();
2280 // double sigma = getSigma(layid, dd);
2281 // er_dmeas2[0] = er_dmeas2[1] = sigma;
2282 }
2283
2284 // Left hypothesis :
2285 if ((LR_==0 && lr != 1.0) ||
2286 (LR_==1 && lr == -1.0)) {
2287
2288 double er_dmeasL, dmeasL;
2289 if(Tof_correc_) {
2290 dmeasL = (-1.0)*fabs(dmeas2[0]);
2291 er_dmeasL = er_dmeas2[0];
2292 } else {
2293 dmeasL = (-1.0)*fabs((*HitMdc).dist()[0]);
2294 er_dmeasL = (*HitMdc).erdist()[0];
2295 }
2296
2297 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
2298 eaNewL.assign(ea - ea_v_H * AL * ea_v_HT);
2299 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
2300 if(0. == RkL) RkL = 1.e-4;
2301
2302 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
2303
2304 aNewL = v_a + diffL;
2305 double sigL = dmeasL -(v_HT * aNewL)[0];
2306 dchi2L = (sigL * sigL) / (RkL * er_dmeasL*er_dmeasL);
2307 }
2308
2309 if ((LR_==0 && lr != -1.0) ||
2310 (LR_==1 && lr == 1.0)) {
2311
2312 double er_dmeasR, dmeasR;
2313 if(Tof_correc_) {
2314 dmeasR = dmeas2[1];
2315 er_dmeasR = er_dmeas2[1];
2316 } else {
2317 dmeasR = fabs((*HitMdc).dist()[1]);
2318 er_dmeasR = (*HitMdc).erdist()[1];
2319 }
2320
2321 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
2322 eaNewR.assign(ea - ea_v_H * AR * ea_v_HT);
2323 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
2324 if(0. == RkR) RkR = 1.e-4;
2325
2326 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
2327
2328 aNewR = v_a + diffR;
2329 double sigR = dmeasR -(v_HT * aNewR)[0];
2330 dchi2R = (sigR*sigR) / (RkR * er_dmeasR*er_dmeasR);
2331 }
2332
2333 // Update Kalman result :
2334 double dchi2_loc(0);
2335
2336#ifdef YDEBUG
2337 cout<<" track::update_hits......"<<endl;
2338 std::cout << " dchi2R = " << dchi2R << ", dchi2L = " << dchi2L
2339 << " estimate = "<<estim<< std::endl;
2340 std::cout << " dmeasR = " << dmeas2[1]
2341 << ", dmeasL = " << (-1.0)*fabs(dmeas2[0]) << ", check HitMdc.ddl = "
2342 << (*HitMdc).dist()[0] << std::endl;
2343 std::cout<<"dchi2L : "<<dchi2L<<" ,dchi2R: "<<dchi2R<<std::endl;
2344#endif
2345
2346
2347 if (fabs(dchi2R-dchi2L)<10. && inext>0) {
2348
2349 KalFitHitMdc & HitMdc_next = HitsMdc_[inext];
2350
2351 KalmanFit::Helix H_fromR(pivot(), aNewR, eaNewR);
2352 double chi2_fromR(chi2_next(H_fromR, HitMdc_next,csmflag));
2353 //double chi2_fromR(chi2_next(H_fromR, HitMdc_next));
2354
2355 KalmanFit::Helix H_fromL(pivot(), aNewL, eaNewL);
2356 double chi2_fromL(chi2_next(H_fromL, HitMdc_next,csmflag));
2357 //double chi2_fromL(chi2_next(H_fromL, HitMdc_next));
2358#ifdef YDEBUG
2359 std::cout << " chi2_fromL = " << chi2_fromL
2360 << ", chi2_fromR = " << chi2_fromR << std::endl;
2361#endif
2362 if (fabs(chi2_fromR-chi2_fromL)<10.){
2363 int inext2(-1);
2364 if (inext+1<HitsMdc_.size())
2365 for( int k=inext+1 ; k < HitsMdc_.size(); k++ )
2366 if (!(HitsMdc_[k].chi2()<0)) {
2367 inext2 = k;
2368 break;
2369 }
2370
2371 if (inext2 != -1){
2372 KalFitHitMdc & HitMdc_next2 = HitsMdc_[inext2];
2373 // double chi2_fromR2(chi2_next(H_fromR, HitMdc_next2));
2374 // double chi2_fromL2(chi2_next(H_fromL, HitMdc_next2));
2375 double chi2_fromR2(chi2_next(H_fromR, HitMdc_next2, csmflag));
2376 double chi2_fromL2(chi2_next(H_fromL, HitMdc_next2, csmflag));
2377#ifdef YDEBUG
2378 std::cout << " chi2_fromL2 = " << chi2_fromL2
2379 << ", chi2_fromR2 = " << chi2_fromR2 << std::endl;
2380#endif
2381 if (fabs(dchi2R+chi2_fromR+chi2_fromR2-(dchi2L+chi2_fromL+chi2_fromL2))<2) {
2382 if (chi2_fromR2<chi2_fromL2)
2383 dchi2L = DBL_MAX;
2384 else
2385 dchi2R = DBL_MAX;
2386 }
2387 }
2388 }
2389
2390 if (!(dchi2L == DBL_MAX && dchi2R == DBL_MAX)) {
2391 if (dchi2R+chi2_fromR < dchi2L+chi2_fromL){
2392 dchi2L = DBL_MAX;
2393#ifdef YDEBUG
2394 std::cout << " choose right..." << std::endl;
2395#endif
2396 } else {
2397 dchi2R = DBL_MAX;
2398#ifdef YDEBUG
2399 std::cout << " choose left..." << std::endl;
2400#endif
2401 }
2402 }
2403 }
2404
2405 if ((0 < dchi2R && dchi2R < dchi2cutf_calib[layerid]) ||
2406 (0 < dchi2L && dchi2L < dchi2cutf_calib[layerid])) {
2407
2408 if (((LR_==0 && dchi2R < dchi2L && lr != -1) ||
2409 (LR_==1 && lr == 1)) &&
2410 fabs(aNewR[2]-a()[2]) < 1000. && aNewR[2]) {
2411 if (nchits_ < (unsigned) nmdc_hit2_ || dchi2R < dchi2cutf_calib[layerid]){
2412 nchits_++;
2413 if (flag_ster) nster_++;
2414 if(layerid>19){
2415 Ea(eaNewR);
2416 HelixSeg.Ea_filt_fwd(eaNewR);
2417 a(aNewR);
2418 HelixSeg.a_filt_fwd(aNewR);
2419 }
2420 /*
2421 Ea(eaNewR);
2422 a(aNewR);
2423
2424 KalFitTrack helixR(pivot_work, aNewR, eaNewR, 0, 0, 0);
2425 helixR.pivot(ip);
2426
2427 a_temp = helixR.a();
2428 ea_temp = helixR.Ea();
2429
2430 HelixSeg.Ea_filt_fwd(ea_temp);
2431 HelixSeg.a_filt_fwd(a_temp);
2432 //HelixSeg.filt_include(1);
2433
2434 */
2435
2436 chiSq_ += dchi2R;
2437 dchi2_loc = dchi2R;
2438 if (l_mass_ == lead_){
2439 (*HitMdc).LR(1);
2440 HelixSeg.LR(1);
2441 }
2442 update_bit((*HitMdc).wire().layer().layerId());
2443 }
2444 } else if (((LR_==0 && dchi2L <= dchi2R && lr != 1) ||
2445 (LR_==1 && lr == -1)) &&
2446 fabs(aNewL[2]-a()[2]) < 1000. && aNewL[2]){
2447 if (nchits_ < (unsigned) nmdc_hit2_ || dchi2L < dchi2cutf_calib[layerid]){
2448 nchits_++;
2449 if (flag_ster) nster_++;
2450 if(layerid>19){
2451 Ea(eaNewL);
2452 HelixSeg.Ea_filt_fwd(eaNewL);
2453 a(aNewL);
2454 HelixSeg.a_filt_fwd(aNewL);
2455 }
2456
2457 /*
2458 Ea(eaNewL);
2459 a(aNewL);
2460
2461 KalFitTrack helixL(pivot_work, aNewL, eaNewL, 0, 0, 0);
2462 helixL.pivot(ip);
2463 a_temp = helixL.a();
2464 ea_temp = helixL.Ea();
2465 HelixSeg.Ea_filt_fwd(ea_temp);
2466 HelixSeg.a_filt_fwd(a_temp);
2467 //HelixSeg.filt_include(1);
2468
2469 */
2470
2471 chiSq_ += dchi2L;
2472 dchi2_loc = dchi2L;
2473 if (l_mass_ == lead_){
2474 (*HitMdc).LR(-1);
2475 HelixSeg.LR(-1);
2476 }
2477 update_bit((*HitMdc).wire().layer().layerId());
2478 }
2479 }
2480 }
2481
2482 if (dchi2_loc > dchi2_max_) {
2483 dchi2_max_ = dchi2_loc ;
2484 r_max_ = pivot().perp();
2485 }
2486 dchi2out = dchi2_loc;
2487 // if(dchi2out == 0) {
2488 // dchi2out = ( (dchi2L < dchi2R ) ? dchi2L : dchi2R ) ;
2489 // }
2490 return chiSq_;
2491}
2492
2493
2494double KalFitTrack::update_hits(KalFitHelixSeg& HelixSeg, int inext, Hep3Vector& meas,int way, double& dchi2out, int csmflag) {
2495
2496
2497 HepPoint3D ip(0.,0.,0.);
2498
2500 double lr = HitMdc->LR();
2501 int layerid = (*HitMdc).wire().layer().layerId();
2502 double entrangle = HitMdc->rechitptr()->getEntra();
2503
2504 if(debug_ == 4) {
2505 std::cout<<"in update_hits(kalFitHelixSeg,...) : the layerid ="<<layerid<<std::endl;
2506 std::cout<<" update_hits: lr= " << lr <<std::endl;
2507 }
2508 // For taking the propagation delay into account :
2509 const KalFitWire& Wire = HitMdc->wire();
2510 int wire_ID = Wire.geoID();
2511
2512 if (wire_ID<0 || wire_ID>6796){ //bes
2513 std::cout << " KalFitTrack: wire_ID problem : " << wire_ID
2514 << std::endl;
2515 return 0;
2516 }
2517 int flag_ster = Wire.stereo();
2518 double x[3] ={pivot().x(), pivot().y(), pivot().z()};
2519 double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
2520 double tofest(0);
2521 double phi = fmod(phi0() + M_PI4, M_PI2);
2522 double csf0 = cos(phi);
2523 double snf0 = (1. - csf0) * (1. + csf0);
2524 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
2525 if(phi > M_PI) snf0 = - snf0;
2526 if (Tof_correc_){
2527 double t = tanl();
2528 double dphi(0);
2529
2530 KalmanFit::Helix work = *(KalmanFit::Helix*)this;
2531 work.ignoreErrorMatrix();
2532 double phi_ip(0);
2533 Hep3Vector ip(0, 0, 0);
2534 work.pivot(ip);
2535 phi_ip = work.phi0();
2536 if (fabs(phi - phi_ip) > M_PI) {
2537 if (phi > phi_ip) phi -= 2 * M_PI;
2538 else phi_ip -= 2 * M_PI;
2539 }
2540 dphi = phi - phi_ip;
2541
2542 double l = fabs(radius() * dphi * sqrt(1 + t * t));
2543 double pmag( sqrt( 1.0 + t*t ) / kappa());
2544 double mass_over_p( mass_ / pmag );
2545 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
2546 tofest = l / ( 29.9792458 * beta );
2547 if(csmflag==1 && (*HitMdc).wire().y()>0.) tofest= -1. * tofest;
2548 }
2549
2550 const HepSymMatrix& ea = Ea();
2551 HelixSeg.Ea_pre_fwd(ea);
2553
2554
2555 const HepVector& v_a = a();
2556 HelixSeg.a_pre_fwd(v_a);
2557 HelixSeg.a_filt_fwd(v_a);
2558
2559 /*
2560
2561 HepPoint3D pivot_work = pivot();
2562 HepVector a_work = a();
2563 HepSymMatrix ea_work = Ea();
2564 KalFitTrack helix_work(pivot_work, a_work, ea_work, 0, 0, 0);
2565 helix_work.pivot(ip);
2566
2567 HepVector a_temp = helix_work.a();
2568 HepSymMatrix ea_temp = helix_work.Ea();
2569
2570 HelixSeg.Ea_pre_fwd(ea_temp);
2571 HelixSeg.a_pre_fwd(a_temp);
2572
2573 // My God, for the protection purpose
2574 HelixSeg.Ea_filt_fwd(ea_temp);
2575 HelixSeg.a_filt_fwd(a_temp);
2576
2577 */
2578
2579
2580 double dchi2R(99999999), dchi2L(99999999);
2581
2582 HepVector v_H(5, 0);
2583 v_H[0] = -csf0 * meas.x() - snf0 * meas.y();
2584 v_H[3] = -meas.z();
2585 HepMatrix v_HT = v_H.T();
2586
2587 double estim = (v_HT * v_a)[0];
2588 HepVector ea_v_H = ea * v_H;
2589 HepMatrix ea_v_HT = (ea_v_H).T();
2590 HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
2591
2592 HepSymMatrix eaNewL(5), eaNewR(5);
2593 HepVector aNewL(5), aNewR(5);
2594#ifdef YDEBUG
2595 cout<<"phi "<<phi<<" snf0 "<<snf0<<" csf0 "<<csf0<<endl
2596 <<" meas: "<<meas <<endl;
2597 cout<<"the related matrix:"<<endl;
2598 cout<<"v_H "<<v_H<<endl;
2599 cout<<"v_a "<<v_a<<endl;
2600 cout<<"ea "<<ea<<endl;
2601 cout<<"v_H_T_ea_v_H "<<v_H_T_ea_v_H<<endl;
2602 cout<<"LR_= "<< LR_ << "lr= " << lr <<endl;
2603#endif
2604
2605 double time = (*HitMdc).tdc();
2606 // if (Tof_correc_)
2607 // time = time - way*tofest;
2608
2609 // double dd = 1.0e-4 * 40.0 * time;
2610 //the following 3 line : tempory
2611
2612 double drifttime = getDriftTime(*HitMdc , tofest);
2613 double ddl = getDriftDist(*HitMdc, drifttime, 0 );
2614 double ddr = getDriftDist(*HitMdc, drifttime, 1 );
2615 double erddl = getSigma( *HitMdc, a()[4], 0, ddl);
2616 double erddr = getSigma( *HitMdc, a()[4], 1, ddr);
2617
2618 if(debug_ == 4){
2619 std::cout<<"the pure drift time is "<<drifttime<<std::endl;
2620 std::cout<<"the drift dist left hypothesis is "<<ddl<<std::endl;
2621 std::cout<<"the drift dist right hypothesis is "<<ddr<<std::endl;
2622 std::cout<<"the error sigma left hypothesis is "<<erddl<<std::endl;
2623 std::cout<<"the error sigma right hypothesis is "<<erddr<<std::endl;
2624 }
2625 double dmeas2[2] = { 0.1*ddl , 0.1*ddr }; //millimeter to centimeter
2626 double er_dmeas2[2] = {0. , 0.} ;
2627
2628 if(resolflag_ == 1) {
2629 er_dmeas2[0] = 0.1*erddl;
2630 er_dmeas2[1] = 0.1*erddr;
2631 }
2632 else if(resolflag_ == 0) {
2633 // int layid = (*HitMdc).wire().layer().layerId();
2634 // double sigma = getSigma(layid, dd);
2635 // er_dmeas2[0] = er_dmeas2[1] = sigma;
2636 }
2637
2638 // Left hypothesis :
2639 if ((LR_==0 && lr != 1.0) ||
2640 (LR_==1 && lr == -1.0)) {
2641
2642 double er_dmeasL, dmeasL;
2643 if(Tof_correc_) {
2644 dmeasL = (-1.0)*fabs(dmeas2[0]);
2645 er_dmeasL = er_dmeas2[0];
2646 } else {
2647 dmeasL = (-1.0)*fabs((*HitMdc).dist()[0]);
2648 er_dmeasL = (*HitMdc).erdist()[0];
2649 }
2650
2651 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
2652 eaNewL.assign(ea - ea_v_H * AL * ea_v_HT);
2653 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
2654 if(0. == RkL) RkL = 1.e-4;
2655
2656 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
2657
2658 aNewL = v_a + diffL;
2659 double sigL = dmeasL -(v_HT * aNewL)[0];
2660 dchi2L = (sigL * sigL) / (RkL * er_dmeasL*er_dmeasL);
2661 }
2662
2663 if ((LR_==0 && lr != -1.0) ||
2664 (LR_==1 && lr == 1.0)) {
2665
2666 double er_dmeasR, dmeasR;
2667 if(Tof_correc_) {
2668 dmeasR = dmeas2[1];
2669 er_dmeasR = er_dmeas2[1];
2670 } else {
2671 dmeasR = fabs((*HitMdc).dist()[1]);
2672 er_dmeasR = (*HitMdc).erdist()[1];
2673 }
2674
2675 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
2676 eaNewR.assign(ea - ea_v_H * AR * ea_v_HT);
2677 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
2678 if(0. == RkR) RkR = 1.e-4;
2679
2680 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
2681
2682 aNewR = v_a + diffR;
2683 double sigR = dmeasR -(v_HT * aNewR)[0];
2684 dchi2R = (sigR*sigR) / (RkR * er_dmeasR*er_dmeasR);
2685 }
2686
2687 // Update Kalman result :
2688 double dchi2_loc(0);
2689
2690#ifdef YDEBUG
2691 cout<<" track::update_hits......"<<endl;
2692 std::cout << " dchi2R = " << dchi2R << ", dchi2L = " << dchi2L
2693 << " estimate = "<<estim<< std::endl;
2694 std::cout << " dmeasR = " << dmeas2[1]
2695 << ", dmeasL = " << (-1.0)*fabs(dmeas2[0]) << ", check HitMdc.ddl = "
2696 << (*HitMdc).dist()[0] << std::endl;
2697#endif
2698
2699 if (fabs(dchi2R-dchi2L)<10. && inext>0) {
2700
2701 KalFitHitMdc & HitMdc_next = HitsMdc_[inext];
2702
2703 KalmanFit::Helix H_fromR(pivot(), aNewR, eaNewR);
2704 double chi2_fromR(chi2_next(H_fromR, HitMdc_next,csmflag));
2705
2706 KalmanFit::Helix H_fromL(pivot(), aNewL, eaNewL);
2707 double chi2_fromL(chi2_next(H_fromL, HitMdc_next,csmflag));
2708#ifdef YDEBUG
2709 std::cout << " chi2_fromL = " << chi2_fromL
2710 << ", chi2_fromR = " << chi2_fromR << std::endl;
2711#endif
2712 if (fabs(chi2_fromR-chi2_fromL)<10.){
2713 int inext2(-1);
2714 if (inext+1<HitsMdc_.size())
2715 for( int k=inext+1 ; k < HitsMdc_.size(); k++ )
2716 if (!(HitsMdc_[k].chi2()<0)) {
2717 inext2 = k;
2718 break;
2719 }
2720
2721 if (inext2 != -1){
2722 KalFitHitMdc & HitMdc_next2 = HitsMdc_[inext2];
2723 double chi2_fromR2(chi2_next(H_fromR, HitMdc_next2, csmflag));
2724 double chi2_fromL2(chi2_next(H_fromL, HitMdc_next2, csmflag));
2725#ifdef YDEBUG
2726 std::cout << " chi2_fromL2 = " << chi2_fromL2
2727 << ", chi2_fromR2 = " << chi2_fromR2 << std::endl;
2728#endif
2729 if (fabs(dchi2R+chi2_fromR+chi2_fromR2-(dchi2L+chi2_fromL+chi2_fromL2))<2) {
2730 if (chi2_fromR2<chi2_fromL2)
2731 dchi2L = DBL_MAX;
2732 else
2733 dchi2R = DBL_MAX;
2734 }
2735 }
2736 }
2737
2738 if (!(dchi2L == DBL_MAX && dchi2R == DBL_MAX)) {
2739 if (dchi2R+chi2_fromR < dchi2L+chi2_fromL){
2740 dchi2L = DBL_MAX;
2741#ifdef YDEBUG
2742 std::cout << " choose right..." << std::endl;
2743#endif
2744 } else {
2745 dchi2R = DBL_MAX;
2746#ifdef YDEBUG
2747 std::cout << " choose left..." << std::endl;
2748#endif
2749 }
2750 }
2751 }
2752
2753 if ((0 < dchi2R && dchi2R < dchi2cutf_calib[layerid]) ||
2754 (0 < dchi2L && dchi2L < dchi2cutf_calib[layerid])) {
2755
2756 if (((LR_==0 && dchi2R < dchi2L && lr != -1) ||
2757 (LR_==1 && lr == 1)) &&
2758 fabs(aNewR[2]-a()[2]) < 1000. && aNewR[2]) {
2759 if (nchits_ < (unsigned) nmdc_hit2_ || dchi2R < dchi2cutf_calib[layerid]){
2760 nchits_++;
2761 if (flag_ster) nster_++;
2762
2763 Ea(eaNewR);
2764 HelixSeg.Ea_filt_fwd(eaNewR);
2765 a(aNewR);
2766 HelixSeg.a_filt_fwd(aNewR);
2767
2768 /*
2769 Ea(eaNewR);
2770 a(aNewR);
2771
2772 KalFitTrack helixR(pivot_work, aNewR, eaNewR, 0, 0, 0);
2773 helixR.pivot(ip);
2774
2775 a_temp = helixR.a();
2776 ea_temp = helixR.Ea();
2777
2778 HelixSeg.Ea_filt_fwd(ea_temp);
2779 HelixSeg.a_filt_fwd(a_temp);
2780 //HelixSeg.filt_include(1);
2781
2782 */
2783
2784 chiSq_ += dchi2R;
2785 dchi2_loc = dchi2R;
2786 if (l_mass_ == lead_){
2787 (*HitMdc).LR(1);
2788 HelixSeg.LR(1);
2789 }
2790 update_bit((*HitMdc).wire().layer().layerId());
2791 }
2792 } else if (((LR_==0 && dchi2L <= dchi2R && lr != 1) ||
2793 (LR_==1 && lr == -1)) &&
2794 fabs(aNewL[2]-a()[2]) < 1000. && aNewL[2]){
2795 if (nchits_ < (unsigned) nmdc_hit2_ || dchi2L < dchi2cutf_calib[layerid]){
2796 nchits_++;
2797 if (flag_ster) nster_++;
2798 Ea(eaNewL);
2799 HelixSeg.Ea_filt_fwd(eaNewL);
2800 a(aNewL);
2801 HelixSeg.a_filt_fwd(aNewL);
2802
2803
2804 /*
2805 Ea(eaNewL);
2806 a(aNewL);
2807
2808 KalFitTrack helixL(pivot_work, aNewL, eaNewL, 0, 0, 0);
2809 helixL.pivot(ip);
2810 a_temp = helixL.a();
2811 ea_temp = helixL.Ea();
2812 HelixSeg.Ea_filt_fwd(ea_temp);
2813 HelixSeg.a_filt_fwd(a_temp);
2814 //HelixSeg.filt_include(1);
2815
2816 */
2817
2818 chiSq_ += dchi2L;
2819 dchi2_loc = dchi2L;
2820 if (l_mass_ == lead_){
2821 (*HitMdc).LR(-1);
2822 HelixSeg.LR(-1);
2823 }
2824 update_bit((*HitMdc).wire().layer().layerId());
2825 }
2826 }
2827 }
2828
2829 if (dchi2_loc > dchi2_max_) {
2830 dchi2_max_ = dchi2_loc ;
2831 r_max_ = pivot().perp();
2832 }
2833 dchi2out = dchi2_loc;
2834 // if(dchi2out == 0) {
2835 // dchi2out = ( (dchi2L < dchi2R ) ? dchi2L : dchi2R ) ;
2836 // }
2837 return chiSq_;
2838}
2839
2840
2841double KalFitTrack::update_hits(RecCgemCluster* Cluster, double recR, int way, int csmflag) //add by Huang Zhen
2842{
2843 //const double a_stero[3] = {(45.94*3.1415926/180),(31.10*3.1415926/180),(32.99*3.1415926/180)};
2844 //const double r_layer[3] = {87.5026,132.7686,175.2686};
2845 //const double x_reso[3]={0.1372,0.1476,0.1412};
2846 //const double v_reso[3]={0.1273,0.1326,0.1378};
2847
2848 // --- measurement
2849 double recZ = Cluster->getRecZ()/10;// cm
2850 double recPhi = Cluster->getrecphi();
2851 while(recPhi > M_PI) recPhi-=2*M_PI;
2852 while(recPhi <-M_PI) recPhi+=2*M_PI;
2853 HepVector v_measu(2,0);
2854 v_measu(1) = recPhi;
2855 v_measu(2) = recZ;
2856
2857 // --- estimation
2858 //double Phi = intersect_cylinder(recR);
2859 HepPoint3D x0kal = x(intersect_cylinder(recR));
2860 pivot_numf(x0kal);
2861 HepVector v_estim(2,0);
2862 v_estim(1) = x0kal.phi();
2863 v_estim(2) = x0kal.z();
2864
2865 const HepSymMatrix& ea = Ea();
2866 HepVector v_a = a();
2867
2868 // --- derivative matrix
2869 double dPhi=intersect_cylinder(recR);
2870 double dr = v_a(1);
2871 double phi0 = v_a(2);
2872 double kappa = v_a(3);
2873 double tanl = v_a(5);
2874 double x0 = x0kal.x();
2875 double y0 = x0kal.y();
2876 double Alpha = alpha();
2877 HepMatrix H(2, 5, 0);
2878 H(1,1) = -y0*cos(phi0)/(y0*y0+x0*x0)+x0*sin(phi0)/(x0*x0+y0*y0);
2879 H(1,2) = -y0/(y0*y0+x0*x0)*((-1)*dr*sin(phi0)+Alpha/kappa*(sin(phi0+dPhi)-sin(phi0)))+x0/(x0*x0+y0*y0)*(dr*cos(phi0)+Alpha/kappa*(cos(phi0)-cos(phi0+dPhi)));
2880 H(1,3) = -y0/(y0*y0+x0*x0)*(-1)*Alpha/(kappa*kappa)*(cos(phi0)-cos(phi0+dPhi))+x0/(x0*x0+y0*y0)*(-1)*Alpha/(kappa*kappa)*(sin(phi0)-sin(phi0+dPhi));
2881 H(2,3) = Alpha/(kappa*kappa)*tanl*dPhi;
2882 H(2,4) = 1.0;
2883 H(2,5) = -1*(Alpha/kappa)*dPhi;
2884
2885 // --- error matrix of Cgem
2886 int layer = Cluster->getlayerid();
2887 ISvcLocator* svcLocator = Gaudi::svcLocator();
2888 ICgemGeomSvc* ISvc;
2889 StatusCode Cgem_sc=svcLocator->service("CgemGeomSvc", ISvc);
2890 CgemGeomSvc* CgemGeomSvc_=dynamic_cast<CgemGeomSvc *>(ISvc);
2891 CgemGeoLayer* CgemLayer = CgemGeomSvc_->getCgemLayer(layer);
2892 double R_x = (CgemLayer->getInnerROfAnodeCu2() + CgemLayer->getOuterROfAnodeCu2())/2;// in mm
2893 //R_v = (CgemLayer->getInnerROfAnodeCu1() + CgemLayer->getOuterROfAnodeCu1())/2;
2894 double a_stero = (CgemLayer->getAngleOfStereo())*M_PI/180;
2895
2896 ICgemCalibFunSvc* CgemCalibSvc;
2897 StatusCode sc = svcLocator->service ("CgemCalibFunSvc", CgemCalibSvc);
2898 int iView(0), mode(2);
2899 double Q(100), T(100);
2900 double Phi_momentum = momentum(dPhi).phi();
2901 double Phi_position = x0kal.phi();
2902 double delta_phi=Phi_momentum - Phi_position;
2903 while(delta_phi>M_PI) delta_phi-=CLHEP::twopi;
2904 while(delta_phi<-M_PI) delta_phi+=CLHEP::twopi;
2905 double sigma_X = CgemCalibSvc->getSigma(layer,iView,mode,delta_phi,Q,T);// in mm
2906 double sigma_V = CgemCalibSvc->getSigma(layer,iView,mode,delta_phi,Q,T);// in mm
2907
2908 HepSymMatrix V(2,0);
2909 //V(1,1) = pow(x_reso[layer]/r_layer[layer],2);
2910 //V(2,2) = pow(sqrt(0.01*v_reso[layer]*v_reso[layer]/(sin(a_stero[layer])*sin(a_stero[layer]))+0.01*x_reso[layer]*x_reso[layer]/(tan(a_stero[layer])*tan(a_stero[layer]))),2);
2911 V(1,1) = pow(sigma_X/R_x,2);
2912 V(2,2) = pow(sqrt(0.01*sigma_V*sigma_V/(sin(a_stero)*sin(a_stero))+0.01*sigma_X*sigma_X/(tan(a_stero)*tan(a_stero))),2);
2913 //V(1,2) = V(1,1)*R_x/tan(a_stero);
2914 //V(2,1) = V(1,2);
2915
2916 // --- gain matrix
2917 int ierr=-1;
2918 HepMatrix K = ea*H.T()*(V+H*ea*H.T()).inverse(ierr);
2919 if(ierr != 0) cout<<"KalFitTrack::update_hits(cluster) errer in inverse operation of gain matrix!"<<endl;
2920
2921 // --- new error matrix
2922 HepSymMatrix EaNew(5,0);
2923 EaNew.assign(ea-K*H*ea);
2924 //Ea(EaNew);
2925
2926 // --- diff
2927 HepVector v_diff = v_measu - v_estim;
2928 while(v_diff(1) > M_PI) v_diff(1)-=2*M_PI;
2929 while(v_diff(1) <- M_PI) v_diff(1)+=2*M_PI;
2930
2931 // --- new parameters
2932 HepVector aNew = v_a + K*v_diff;
2933
2934 // --- new v_estim
2935 a(v_a + K*v_diff);// temporary for new v_estim
2936 HepPoint3D x0kal_new = x(intersect_cylinder(recR));
2937 HepVector v_estim_new(2,0);
2938 v_estim_new(1) = x0kal_new.phi();
2939 v_estim_new(2) = x0kal_new.z();
2940
2941 // --- difference between measurement and updated estimation
2942 v_diff = v_measu - v_estim_new;
2943 while(v_diff(1) > M_PI) v_diff(1)-=2*M_PI;
2944 while(v_diff(1) <- M_PI) v_diff(1)+=2*M_PI;
2945
2946 // --- new derivative matrix (needed?)
2947 //track_new.pivot_numf(x0kal_new,pathl);
2948 //HepVector a_new = track_new.a();
2949 //HepVector Ea_new = track_new.ea();
2950
2951 // --- R matrix and Chisuqre
2952 HepSymMatrix R(2,0);
2953 R.assign(V-H*EaNew*H.T());
2954 HepVector dChi2 = v_diff.T()*R.inverse(ierr)*v_diff;
2955 if(ierr != 0) cout<<"KalFitTrack::update_hits(cluster) errer in inverse operation of R matrix!"<<endl;
2956
2957 // --- check if update the track
2958 int layerid = Cluster->getlayerid();
2959 if(dChi2(1)>0 && fabs(aNew[2]-a()[2]) < 1000. && aNew[2] && dChi2(1) < 2.0*dchi2cutf_anal[layerid])
2960 {
2961 nchits_++;
2962 nchits_++;
2963 nster_++;
2964 // update track parameters and errors
2965 Ea(EaNew);
2966 a(aNew);
2967 // --- update Chisqure of track
2968 if(way>0) chiSq(chiSq()+dChi2(1));
2969 if(way<0) chiSq_back(chiSq_back()+dChi2(1));
2970 }
2971 else {
2972 a(v_a);
2973 //cout<<"KalTrack drop a cgem cluster, layer="<<layerid
2974 // <<", v_measure="<<v_measu(1)<<", "<<v_measu(2)
2975 // <<", v_estim="<<v_estim(1)<<", "<<v_estim(2)
2976 // <<", v_estim_new="<<v_estim_new(1)<<", "<<v_estim_new(2)
2977 // <<", v_diff="<<v_diff(1)<<", "<<v_diff(2)
2978 // <<", chi2="<<dChi2(1)<<", kappa="<<a()[2]<<", new_kappa="<<aNew[2]<<", chi2_cut="<<2.0*dchi2cutf_anal[layerid]<<endl;
2979 }
2980
2981
2982 return dChi2(1);
2983}
2984
2985
2987
2988 double lr = HitMdc.LR();
2989 const KalFitWire& Wire = HitMdc.wire();
2990 int wire_ID = Wire.geoID();
2991 int layerid = HitMdc.wire().layer().layerId();
2992 double entrangle = HitMdc.rechitptr()->getEntra();
2993
2994 HepPoint3D fwd(Wire.fwd());
2995 HepPoint3D bck(Wire.bck());
2996 Hep3Vector wire = (Hep3Vector)fwd -(Hep3Vector)bck;
2997 KalmanFit::Helix work = H;
2998 work.ignoreErrorMatrix();
2999 work.pivot((fwd + bck) * .5);
3000 HepPoint3D x0kal = (work.x(0).z() - bck.z())/ wire.z() * wire + bck;
3001 H.pivot(x0kal);
3002
3003 Hep3Vector meas = H.momentum(0).cross(wire).unit();
3004
3005 if (wire_ID<0 || wire_ID>6796){ //bes
3006 std::cout << "KalFitTrack : wire_ID problem : " << wire_ID
3007 << std::endl;
3008 return DBL_MAX;
3009 }
3010
3011 double x[3] ={pivot().x(), pivot().y(), pivot().z()};
3012 double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
3013 double tofest(0);
3014 double phi = fmod(phi0() + M_PI4, M_PI2);
3015 double csf0 = cos(phi);
3016 double snf0 = (1. - csf0) * (1. + csf0);
3017 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
3018 if(phi > M_PI) snf0 = - snf0;
3019
3020 if (Tof_correc_) {
3021 Hep3Vector ip(0, 0, 0);
3022 KalmanFit::Helix work = *(KalmanFit::Helix*)this;
3023 work.ignoreErrorMatrix();
3024 work.pivot(ip);
3025 double phi_ip = work.phi0();
3026 if (fabs(phi - phi_ip) > M_PI) {
3027 if (phi > phi_ip) phi -= 2 * M_PI;
3028 else phi_ip -= 2 * M_PI;
3029 }
3030 double t = tanl();
3031 double l = fabs(radius() * (phi - phi_ip) * sqrt(1 + t * t));
3032 double pmag( sqrt( 1.0 + t*t ) / kappa());
3033 double mass_over_p( mass_ / pmag );
3034 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
3035 tofest = l / ( 29.9792458 * beta );
3036 // if(csmflag==1 && HitMdc.wire().y()>0.) tofest= -1. * tofest;
3037 }
3038
3039 const HepSymMatrix& ea = H.Ea();
3040 const HepVector& v_a = H.a();
3041 double dchi2R(DBL_MAX), dchi2L(DBL_MAX);
3042
3043 HepVector v_H(5, 0);
3044 v_H[0] = -csf0 * meas.x() - snf0 * meas.y();
3045 v_H[3] = -meas.z();
3046 HepMatrix v_HT = v_H.T();
3047
3048 double estim = (v_HT * v_a)[0];
3049 HepVector ea_v_H = ea * v_H;
3050 HepMatrix ea_v_HT = (ea_v_H).T();
3051 HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
3052
3053 HepSymMatrix eaNewL(5), eaNewR(5);
3054 HepVector aNewL(5), aNewR(5);
3055
3056 //double time = HitMdc.tdc();
3057 //if (Tof_correc_)
3058 // time = time - tofest;
3059 double drifttime = getDriftTime(HitMdc , tofest);
3060 double ddl = getDriftDist(HitMdc, drifttime , 0 );
3061 double ddr = getDriftDist(HitMdc, drifttime , 1 );
3062 double erddl = getSigma( HitMdc, H.a()[4], 0, ddl);
3063 double erddr = getSigma( HitMdc, H.a()[4], 1, ddr);
3064
3065 double dmeas2[2] = { 0.1*ddl, 0.1*ddr };
3066 double er_dmeas2[2] = {0. , 0.};
3067 if(resolflag_ == 1) {
3068 er_dmeas2[0] = 0.1*erddl;
3069 er_dmeas2[1] = 0.1*erddr;
3070 }
3071 else if(resolflag_ == 0) {
3072 // int layid = HitMdc.wire().layer().layerId();
3073 // double sigma = getSigma(layid, dd);
3074 // er_dmeas2[0] = er_dmeas2[1] = sigma;
3075 }
3076
3077 if ((LR_==0 && lr != 1.0) ||
3078 (LR_==1 && lr == -1.0)) {
3079
3080 double er_dmeasL, dmeasL;
3081 if(Tof_correc_) {
3082 dmeasL = (-1.0)*fabs(dmeas2[0]);
3083 er_dmeasL = er_dmeas2[0];
3084 } else {
3085 dmeasL = (-1.0)*fabs(HitMdc.dist()[0]);
3086 er_dmeasL = HitMdc.erdist()[0];
3087 }
3088
3089 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
3090 eaNewL.assign(ea - ea_v_H * AL * ea_v_HT);
3091 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
3092 if(0. == RkL) RkL = 1.e-4;
3093
3094 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
3095 aNewL = v_a + diffL;
3096 double sigL = dmeasL -(v_HT * aNewL)[0];
3097 dchi2L = (sigL * sigL) / (RkL * er_dmeasL*er_dmeasL);
3098 }
3099
3100 if ((LR_==0 && lr != -1.0) ||
3101 (LR_==1 && lr == 1.0)) {
3102
3103 double er_dmeasR, dmeasR;
3104 if(Tof_correc_) {
3105 dmeasR = dmeas2[1];
3106 er_dmeasR = er_dmeas2[1];
3107 } else {
3108 dmeasR = fabs(HitMdc.dist()[1]);
3109 er_dmeasR = HitMdc.erdist()[1];
3110 }
3111
3112 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
3113 eaNewR.assign(ea - ea_v_H * AR * ea_v_HT);
3114 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
3115 if(0. == RkR) RkR = 1.e-4;
3116
3117 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
3118 aNewR = v_a + diffR;
3119 double sigR = dmeasR -(v_HT * aNewR)[0];
3120 dchi2R = (sigR*sigR) / (RkR * er_dmeasR*er_dmeasR);
3121 }
3122
3123 if (dchi2R < dchi2L){
3124 H.a(aNewR); H.Ea(eaNewR);
3125 } else {
3126 H.a(aNewL); H.Ea(eaNewL);
3127 }
3128 return ((dchi2R < dchi2L) ? dchi2R : dchi2L);
3129}
3130
3132
3133 double lr = HitMdc.LR();
3134 const KalFitWire& Wire = HitMdc.wire();
3135 int wire_ID = Wire.geoID();
3136 int layerid = HitMdc.wire().layer().layerId();
3137 double entrangle = HitMdc.rechitptr()->getEntra();
3138
3139 HepPoint3D fwd(Wire.fwd());
3140 HepPoint3D bck(Wire.bck());
3141 Hep3Vector wire = (Hep3Vector)fwd -(Hep3Vector)bck;
3142 KalmanFit::Helix work = H;
3143 work.ignoreErrorMatrix();
3144 work.pivot((fwd + bck) * .5);
3145 HepPoint3D x0kal = (work.x(0).z() - bck.z())/ wire.z() * wire + bck;
3146 H.pivot(x0kal);
3147
3148 Hep3Vector meas = H.momentum(0).cross(wire).unit();
3149
3150 if (wire_ID<0 || wire_ID>6796){ //bes
3151 std::cout << "KalFitTrack : wire_ID problem : " << wire_ID
3152 << std::endl;
3153 return DBL_MAX;
3154 }
3155
3156 double x[3] ={pivot().x(), pivot().y(), pivot().z()};
3157 double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
3158 double tofest(0);
3159 double phi = fmod(phi0() + M_PI4, M_PI2);
3160 double csf0 = cos(phi);
3161 double snf0 = (1. - csf0) * (1. + csf0);
3162 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
3163 if(phi > M_PI) snf0 = - snf0;
3164
3165 if (Tof_correc_) {
3166 Hep3Vector ip(0, 0, 0);
3167 KalmanFit::Helix work = *(KalmanFit::Helix*)this;
3168 work.ignoreErrorMatrix();
3169 work.pivot(ip);
3170 double phi_ip = work.phi0();
3171 if (fabs(phi - phi_ip) > M_PI) {
3172 if (phi > phi_ip) phi -= 2 * M_PI;
3173 else phi_ip -= 2 * M_PI;
3174 }
3175 double t = tanl();
3176 double l = fabs(radius() * (phi - phi_ip) * sqrt(1 + t * t));
3177 double pmag( sqrt( 1.0 + t*t ) / kappa());
3178 double mass_over_p( mass_ / pmag );
3179 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
3180 tofest = l / ( 29.9792458 * beta );
3181 if(csmflag==1 && HitMdc.wire().y()>0.) tofest= -1. * tofest;
3182 }
3183
3184 const HepSymMatrix& ea = H.Ea();
3185 const HepVector& v_a = H.a();
3186 double dchi2R(DBL_MAX), dchi2L(DBL_MAX);
3187
3188 HepVector v_H(5, 0);
3189 v_H[0] = -csf0 * meas.x() - snf0 * meas.y();
3190 v_H[3] = -meas.z();
3191 HepMatrix v_HT = v_H.T();
3192
3193 double estim = (v_HT * v_a)[0];
3194 HepVector ea_v_H = ea * v_H;
3195 HepMatrix ea_v_HT = (ea_v_H).T();
3196 HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
3197
3198 HepSymMatrix eaNewL(5), eaNewR(5);
3199 HepVector aNewL(5), aNewR(5);
3200
3201 //double time = HitMdc.tdc();
3202 //if (Tof_correc_)
3203 // time = time - tofest;
3204 double drifttime = getDriftTime(HitMdc , tofest);
3205 double ddl = getDriftDist(HitMdc, drifttime , 0 );
3206 double ddr = getDriftDist(HitMdc, drifttime , 1 );
3207 double erddl = getSigma( HitMdc, H.a()[4], 0, ddl);
3208 double erddr = getSigma( HitMdc, H.a()[4], 1, ddr);
3209
3210 double dmeas2[2] = { 0.1*ddl, 0.1*ddr };
3211 double er_dmeas2[2] = {0. , 0.};
3212 if(resolflag_ == 1) {
3213 er_dmeas2[0] = 0.1*erddl;
3214 er_dmeas2[1] = 0.1*erddr;
3215 }
3216 else if(resolflag_ == 0) {
3217 // int layid = HitMdc.wire().layer().layerId();
3218 // double sigma = getSigma(layid, dd);
3219 // er_dmeas2[0] = er_dmeas2[1] = sigma;
3220 }
3221
3222 if ((LR_==0 && lr != 1.0) ||
3223 (LR_==1 && lr == -1.0)) {
3224
3225 double er_dmeasL, dmeasL;
3226 if(Tof_correc_) {
3227 dmeasL = (-1.0)*fabs(dmeas2[0]);
3228 er_dmeasL = er_dmeas2[0];
3229 } else {
3230 dmeasL = (-1.0)*fabs(HitMdc.dist()[0]);
3231 er_dmeasL = HitMdc.erdist()[0];
3232 }
3233
3234 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
3235 eaNewL.assign(ea - ea_v_H * AL * ea_v_HT);
3236 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
3237 if(0. == RkL) RkL = 1.e-4;
3238
3239 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
3240 aNewL = v_a + diffL;
3241 double sigL = dmeasL -(v_HT * aNewL)[0];
3242 dchi2L = (sigL * sigL) / (RkL * er_dmeasL*er_dmeasL);
3243 }
3244
3245 if ((LR_==0 && lr != -1.0) ||
3246 (LR_==1 && lr == 1.0)) {
3247
3248 double er_dmeasR, dmeasR;
3249 if(Tof_correc_) {
3250 dmeasR = dmeas2[1];
3251 er_dmeasR = er_dmeas2[1];
3252 } else {
3253 dmeasR = fabs(HitMdc.dist()[1]);
3254 er_dmeasR = HitMdc.erdist()[1];
3255 }
3256
3257 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
3258 eaNewR.assign(ea - ea_v_H * AR * ea_v_HT);
3259 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
3260 if(0. == RkR) RkR = 1.e-4;
3261
3262 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
3263 aNewR = v_a + diffR;
3264 double sigR = dmeasR -(v_HT * aNewR)[0];
3265 dchi2R = (sigR*sigR) / (RkR * er_dmeasR*er_dmeasR);
3266 }
3267
3268 if (dchi2R < dchi2L){
3269 H.a(aNewR); H.Ea(eaNewR);
3270 } else {
3271 H.a(aNewL); H.Ea(eaNewL);
3272 }
3273 return ((dchi2R < dchi2L) ? dchi2R : dchi2L);
3274}
3275
3276
3277double KalFitTrack::getSigma(int layerId, double driftDist ) const {
3278 double sigma1,sigma2,f;
3279 driftDist *= 10;//mm
3280 if(layerId<8){
3281 if(driftDist<0.5){
3282 sigma1=0.112784; sigma2=0.229274; f=0.666;
3283 }else if(driftDist<1.){
3284 sigma1=0.103123; sigma2=0.269797; f=0.934;
3285 }else if(driftDist<1.5){
3286 sigma1=0.08276; sigma2=0.17493; f=0.89;
3287 }else if(driftDist<2.){
3288 sigma1=0.070109; sigma2=0.149859; f=0.89;
3289 }else if(driftDist<2.5){
3290 sigma1=0.064453; sigma2=0.130149; f=0.886;
3291 }else if(driftDist<3.){
3292 sigma1=0.062383; sigma2=0.138806; f=0.942;
3293 }else if(driftDist<3.5){
3294 sigma1=0.061873; sigma2=0.145696; f=0.946;
3295 }else if(driftDist<4.){
3296 sigma1=0.061236; sigma2=0.119584; f=0.891;
3297 }else if(driftDist<4.5){
3298 sigma1=0.066292; sigma2=0.148426; f=0.917;
3299 }else if(driftDist<5.){
3300 sigma1=0.078074; sigma2=0.188148; f=0.911;
3301 }else if(driftDist<5.5){
3302 sigma1=0.088657; sigma2=0.27548; f=0.838;
3303 }else{
3304 sigma1=0.093089; sigma2=0.115556; f=0.367;
3305 }
3306 }else{
3307 if(driftDist<0.5){
3308 sigma1=0.112433; sigma2=0.327548; f=0.645;
3309 }else if(driftDist<1.){
3310 sigma1=0.096703; sigma2=0.305206; f=0.897;
3311 }else if(driftDist<1.5){
3312 sigma1=0.082518; sigma2=0.248913; f= 0.934;
3313 }else if(driftDist<2.){
3314 sigma1=0.072501; sigma2=0.153868; f= 0.899;
3315 }else if(driftDist<2.5){
3316 sigma1= 0.065535; sigma2=0.14246; f=0.914;
3317 }else if(driftDist<3.){
3318 sigma1=0.060497; sigma2=0.126489; f=0.918;
3319 }else if(driftDist<3.5){
3320 sigma1=0.057643; sigma2= 0.112927; f=0.892;
3321 }else if(driftDist<4.){
3322 sigma1=0.055266; sigma2=0.094833; f=0.887;
3323 }else if(driftDist<4.5){
3324 sigma1=0.056263; sigma2=0.124419; f= 0.932;
3325 }else if(driftDist<5.){
3326 sigma1=0.056599; sigma2=0.124248; f=0.923;
3327 }else if(driftDist<5.5){
3328 sigma1= 0.061377; sigma2=0.146147; f=0.964;
3329 }else if(driftDist<6.){
3330 sigma1=0.063978; sigma2=0.150591; f=0.942;
3331 }else if(driftDist<6.5){
3332 sigma1=0.072951; sigma2=0.15685; f=0.913;
3333 }else if(driftDist<7.){
3334 sigma1=0.085438; sigma2=0.255109; f=0.931;
3335 }else if(driftDist<7.5){
3336 sigma1=0.101635; sigma2=0.315529; f=0.878;
3337 }else{
3338 sigma1=0.149529; sigma2=0.374697; f=0.89;
3339 }
3340 }
3341 double sigmax = sqrt(f*sigma1*sigma1+(1 - f)*sigma2*sigma2)*0.1;
3342 return sigmax;//cm
3343}
3344
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
**********INTEGER nmxhep !maximum number of particles DOUBLE PRECISION vhep INTEGER jdahep COMMON hepevt $ !serial number $ !number of particles $ !status code $ !particle ident KF $ !parent particles $ !childreen particles $ !four momentum
const double a_stero[3]
double sigma2(0)
Double_t x[10]
Double_t time
const DifPoint origin
HepGeom::Point3D< double > HepPoint3D
Definition Gam4pikp.cxx:37
const double M_PI8
const double M_PI2
const double M_PI4
HepGeom::Transform3D HepTransform3D
Definition KalFitTrack.h:7
#define DBL_MAX
Definition KalFitTrack.h:4
********INTEGER modcns REAL m_C
Definition PseuMar.h:13
#define M_PI
Definition TConstant.h:4
double getAngleOfStereo() const
double getOuterROfAnodeCu2() const
double getInnerROfAnodeCu2() const
CgemGeoLayer * getCgemLayer(int i) const
Definition CgemGeomSvc.h:48
virtual double getSigma(int layer, int xvFlag, int readoutMode, double angle, double Q, double T) const =0
virtual StatusCode fieldVector(const HepGeom::Point3D< double > &xyz, HepGeom::Vector3D< double > &fvec) const =0
CLHEP::HepVector a_pre_fwd(void)
CLHEP::HepSymMatrix Ea_filt_fwd(void)
CLHEP::HepVector a_filt_bwd(void)
CLHEP::HepVector a_include(void)
double tof(void)
CLHEP::HepSymMatrix Ea_pre_bwd(void)
double doca_include(void)
CLHEP::HepVector a_filt_fwd(void)
double dt(void)
double doca_exclude(void)
CLHEP::HepVector a_pre_bwd(void)
CLHEP::HepSymMatrix & Ea_pre_fwd(void)
double dd(void)
CLHEP::HepVector a_exclude(void)
CLHEP::HepSymMatrix Ea_filt_bwd(void)
double residual_exclude(void)
CLHEP::HepSymMatrix Ea_include(void)
CLHEP::HepSymMatrix Ea_exclude(void)
KalFitHitMdc * HitMdc(void)
double residual_include(void)
Description of a Hit in Mdc.
int LR(void) const
RecMdcHit * rechitptr(void)
const double * erdist(void) const
const double * dist(void) const
const KalFitWire & wire(void) const
const int layerId(void) const
returns layer ID
double X0(void) const
Extractor.
double del_E(double mass, double path, double p) const
Calculate the straggling of energy loss.
double mcs_angle(double mass, double path, double p) const
Calculate Multiple Scattering angle.
double dE(double mass, double path, double p) const
Calculate energy loss.
Description of a track class (<- Helix.cc)
Definition KalFitTrack.h:36
double * pathl(void)
~KalFitTrack(void)
destructor
static int lead(void)
Magnetic field map.
double getDriftTime(KalFitHitMdc &hitmdc, double toftime) const
static double chi2_hitf_
Cut chi2 for each hit.
KalFitTrack(const HepPoint3D &pivot, const CLHEP::HepVector &a, const CLHEP::HepSymMatrix &Ea, unsigned int m, double chiSq, unsigned int nhits)
constructor
static double dchi2cuts_anal[43]
Definition KalFitTrack.h:58
static double dchi2cuts_calib[43]
Definition KalFitTrack.h:60
static int debug_
for debug
static int nmass(void)
double filter(double v_m, const CLHEP::HepVector &m_H, double v_d, double m_V)
double tof(void) const
static double Bznom_
static int back(void)
void update_bit(int i)
void order_wirhit(int index)
double chiSq(void) const
static int nmdc_hit2_
Cut chi2 for each hit.
double getSigma(int layerId, double driftDist) const
static int tofall_
KalFitHelixSeg & HelixSeg(int i)
void addTofSM(double time)
static int resolflag_
wire resoltion flag
static int steplev_
void chgmass(int i)
static double factor_strag_
factor of energy loss straggling for electron
void useLayer(int iLay)
const HepPoint3D & pivot_numf(const HepPoint3D &newPivot)
Sets pivot position in a given mag field.
void ms(double path, const KalFitMaterial &m, int index)
static int Tof_correc_
Flag for TOF correction.
void update_forMdc(void)
double intersect_cylinder(double r) const
Intersection with different geometry.
double chiSq_back(void) const
void resetLayerUsed()
vector< KalFitHitMdc > & HitsMdc(void)
void fiTerm(double fi)
static double chi2_hits_
void number_wirhit(void)
double chi2_next(KalmanFit::Helix &H, KalFitHitMdc &HitMdc, int csmflag)
static int numf_
Flag for treatment of non-uniform mag field.
void path_add(double path)
Update the path length estimation.
static int inner_steps_
double mass(void) const
static int outer_steps_
double update_hits_csmalign(KalFitHelixSeg &HelixSeg, int inext, CLHEP::Hep3Vector &meas, int way, double &dchi2, int csmflag)
static int drifttime_choice_
the drifttime choice
double smoother_Mdc(KalFitHelixSeg &seg, CLHEP::Hep3Vector &meas, int &flg, int csmflag)
Kalman smoother for Mdc.
void msgasmdc(double path, int index)
Calculate multiple scattering angle.
void appendHitsMdc(KalFitHitMdc h)
Functions for Mdc hits list.
void addPathSM(double path)
static double mdcGasRadlen_
static double dchi2cutf_calib[43]
Definition KalFitTrack.h:59
static int resol(void)
void eloss(double path, const KalFitMaterial &m, int index)
Calculate total energy lost in material.
void update_last(void)
Record the current parameters as ..._last information :
static void LR(int x)
double getDriftDist(KalFitHitMdc &hitmdc, double drifttime, int lr) const
double intersect_yz_plane(const HepTransform3D &plane, double x) const
static int numf(void)
double intersect_zx_plane(const HepTransform3D &plane, double y) const
static int numfcor_
NUMF treatment improved.
void order_hits(void)
double intersect_xy_plane(double z) const
double radius_numf(void) const
Estimation of the radius in a given mag field.
double smoother_Mdc_csmalign(KalFitHelixSeg &seg, CLHEP::Hep3Vector &meas, int &flg, int csmflag)
double update_hits(KalFitHitMdc &HitMdc, int inext, CLHEP::Hep3Vector &meas, int way, double &dchi2, double &dtrack, double &dtracknew, double &dtdc, int csmflag)
Include the Mdc wire hits.
static double dchi2cutf_anal[43]
Definition KalFitTrack.h:57
static int LR_
Use L/R decision from MdcRecHit information :
KalFitHitMdc & HitMdc(int i)
static int strag_
Flag to take account of energy loss straggling :
Description of a Wire class.
Definition KalFitWire.h:46
double y(void) const
Definition KalFitWire.h:101
unsigned int geoID(void) const
Definition KalFitWire.h:74
HepPoint3D bck(void) const
Definition KalFitWire.h:93
HepPoint3D fwd(void) const
Geometry :
Definition KalFitWire.h:92
const KalFitLayer_Mdc & layer(void) const
Definition KalFitWire.h:70
unsigned int stereo(void) const
Definition KalFitWire.h:75
const HepPoint3D & center(void) const
returns position of helix center(z = 0.);
void ignoreErrorMatrix(void)
unsets error matrix. Error calculations will be ignored after this function call until an error matri...
const HepSymMatrix & Ea(void) const
returns error matrix.
HepPoint3D x(double dPhi=0.) const
returns position after rotating angle dPhi in phi direction.
double dr(void) const
returns an element of parameters.
const HepVector & a(void) const
returns helix parameters.
const HepPoint3D & pivot(void) const
returns pivot position.
double getRecZ(void) const
int getlayerid(void) const
double getrecphi(void) const
const double getEntra(void) const
Definition RecMdcHit.h:54
IMPLICIT REAL *A H
Definition myXsection.h:1
int t()
Definition t.c:1