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