/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Reconstruction/KalFitAlg/KalFitAlg-00-07-55-p03/src/KalFitTrack.cxx

Go to the documentation of this file.
00001 
00002 //#include "TrackUtil/Helix.h"
00003 #include "KalFitAlg/helix/Helix.h"
00004 #include "KalFitAlg/KalFitWire.h"
00005 #include "KalFitAlg/KalFitTrack.h"
00006 #include "KalFitAlg/KalFitMaterial.h"
00007 #include "KalFitAlg/KalFitElement.h"
00008 #include "KalFitAlg/KalFitCylinder.h"
00009 #include "MdcGeomSvc/MdcGeomSvc.h"
00010 #include "GaudiKernel/Bootstrap.h"
00011 #include "CLHEP/Matrix/Vector.h"
00012 #include "CLHEP/Matrix/Matrix.h"
00013 #include "CLHEP/Matrix/SymMatrix.h"
00014 #include "CLHEP/Vector/ThreeVector.h"
00015 #include "CLHEP/Units/PhysicalConstants.h"
00016 #include "Math/Point3Dfwd.h"
00017 #include "Math/Point3D.h"
00018 #include "Math/Vector3D.h"
00019 
00020 using namespace ROOT::Math;
00021 
00022 using namespace KalmanFit;
00023 
00024 using namespace CLHEP;
00025 
00026 
00027 // for debug purpose .
00028 int KalFitTrack::drifttime_choice_ = 0;
00029 int KalFitTrack::debug_ = 0;
00030 int KalFitTrack::numf_ = 0;
00031 int KalFitTrack::numfcor_ = 1;
00032 double KalFitTrack::Bznom_ = 10;
00033 int KalFitTrack::steplev_ = 0;
00034 int KalFitTrack::inner_steps_ = 3;
00035 int KalFitTrack::outer_steps_ = 3;
00036 // Mdc factors :
00037 int KalFitTrack::Tof_correc_ = 1;
00038 int KalFitTrack::strag_ = 1;
00039 double KalFitTrack::chi2_hitf_ = 1000;
00040 double KalFitTrack::chi2_hits_ = 1000;
00041 double KalFitTrack::factor_strag_ = 0.4;
00042 //double KalFitTrack::chi2_hitf_ = DBL_MAX;
00043 int KalFitTrack::lead_ = 2;
00044 int KalFitTrack::back_ = 1;
00045 // attention resolflag !!!
00046 int KalFitTrack::resolflag_ = 0;
00047 int KalFitTrack::tofall_ = 1;
00048 // chi2 cut set :
00049 int KalFitTrack::nmdc_hit2_ = 500;
00050 //int KalFitTrack::LR_ = 0;
00051 int KalFitTrack::LR_ = 1;
00052 
00053 
00054 double KalFitTrack::dchi2cutf_anal[43] = {0.};
00055 double KalFitTrack::dchi2cuts_anal[43] = {0.};
00056 double KalFitTrack::dchi2cutf_calib[43] = {0.};
00057 double KalFitTrack::dchi2cuts_calib[43] = {0.};
00058 
00059 
00061 double KalFitTrack::mdcGasRadlen_ = 0.;
00062 
00063 
00064 const double KalFitTrack::MASS[NMASS] = { 0.000510999,
00065         0.105658,
00066         0.139568,
00067         0.493677,
00068         0.938272 };
00069 const double
00070 M_PI2 = 2. * M_PI;
00071 
00072 const double
00073 M_PI4 = 4. * M_PI;
00074 
00075 const double
00076 M_PI8 = 8. * M_PI;
00077 
00078 
00079 
00080 // constructor
00081 KalFitTrack::KalFitTrack(const HepPoint3D& pivot,
00082                 const HepVector& a,
00083                 const HepSymMatrix& Ea,
00084                 unsigned int m, double chisq, unsigned int nhits)
00085 : Helix(pivot, a, Ea), type_(0), insist_(0), chiSq_(0),
00086         nchits_(0), nster_(0), ncath_(0),
00087         ndf_back_(0), chiSq_back_(0), pathip_(0), 
00088         path_rd_(0), path_ab_(0), tof_(0), dchi2_max_(0), r_max_(0),
00089         tof_kaon_(0), tof_proton_(0), pat1_(0), pat2_(0), layer_prec_(0),
00090         trasan_id_(0), nhit_r_(0), nhit_z_(0),pathSM_(0),tof2_(0)
00091 {
00092         memset(PathL_, 0, sizeof(PathL_));
00093         l_mass_ = m;
00094         if (m < NMASS) mass_ = MASS[m];
00095         else mass_ = MASS[2];
00096         r0_ = fabs(center().perp() - fabs(radius()));
00097         //bFieldZ(Bznom_);
00098         Bznom_=bFieldZ(); // 2012-09-13 wangll
00099         update_last();
00100 }
00101 
00102 // destructor
00103 KalFitTrack::~KalFitTrack(void)
00104 {
00105         // delete all objects
00106 
00107 }
00108 
00109 void KalFitTrack::update_last(void)
00110 {
00111         pivot_last_ = pivot();
00112         a_last_ = a();
00113         Ea_last_ = Ea();
00114 }
00115 
00116 void KalFitTrack::update_forMdc(void)
00117 {
00118         pivot_forMdc_ = pivot();
00119         a_forMdc_ = a();
00120         Ea_forMdc_ = Ea();
00121 }
00122 
00123 double KalFitTrack::intersect_cylinder(double r) const
00124 {
00125         double m_rad = radius();
00126         double l = center().perp();
00127 
00128         double cosPhi = (m_rad * m_rad + l * l  - r * r) / (2 * m_rad * l);
00129 
00130         if(cosPhi < -1 || cosPhi > 1) return 0;
00131 
00132         double dPhi = center().phi() - acos(cosPhi) - phi0();
00133 
00134         if(dPhi < -M_PI) dPhi += 2 * M_PI;
00135 
00136         return dPhi;
00137 }
00138 
00139 double KalFitTrack::intersect_zx_plane(const HepTransform3D& plane,
00140                 double y) const
00141 {
00142         HepPoint3D xc = plane * center();
00143         double r = radius();
00144         double d = r * r - (y - xc.y()) * (y - xc.y());
00145         if(d < 0) return 0;
00146 
00147         double xx = xc.x();
00148         if(xx > 0) xx -= sqrt(d);
00149         else xx += sqrt(d);
00150 
00151         double l = (plane.inverse() *
00152                         HepPoint3D(xx, y, 0)).perp();
00153 
00154         return intersect_cylinder(l);
00155 }
00156 
00157 double KalFitTrack::intersect_yz_plane(const HepTransform3D& plane,
00158                 double x) const
00159 {
00160         HepPoint3D xc = plane * center();
00161         double r = radius();
00162         double d = r * r - (x - xc.x()) * (x - xc.x());
00163         if(d < 0) return 0;
00164 
00165         double yy = xc.y();
00166         if(yy > 0) yy -= sqrt(d);
00167         else yy += sqrt(d);
00168 
00169         double l = (plane.inverse() *
00170                         HepPoint3D(x, yy, 0)).perp();
00171 
00172         return intersect_cylinder(l);
00173 }
00174 
00175 double KalFitTrack::intersect_xy_plane(double z) const
00176 {
00177         if (tanl() != 0 && radius() != 0)
00178                 return (pivot().z() + dz() - z) / (radius() * tanl());
00179         else return 0;
00180 }
00181 
00182 void KalFitTrack::ms(double path,
00183                 const KalFitMaterial& material, int index)
00184 {
00185         HepSymMatrix ea = Ea();
00186         //cout<<"ms():path  "<<path<<endl;
00187         //cout<<"ms():ea before: "<<ea<<endl;
00188         double k = kappa();
00189         double t = tanl();
00190         double t2 = t * t;
00191         double pt2 = 1 + t2;
00192         double k2 = k * k;
00193 
00194         double pmag = 1 / fabs(k) * sqrt(pt2);
00195         double dth = material.mcs_angle(mass_, path, pmag);
00196         double dth2 = dth * dth;
00197         double pt2dth2 = pt2 * dth2;
00198 
00199         ea[1][1] += pt2dth2;
00200         ea[2][2] += k2 * t2 * dth2;
00201         ea[2][4] += k * t * pt2dth2;
00202         ea[4][4] += pt2 * pt2dth2;
00203 
00204         ea[3][3] += dth2 * path * path /3 / (1 + t2);
00205         ea[3][4] += dth2 * path/2 * sqrt(1 + t2);
00206         ea[3][2] += dth2 * t / sqrt(1 + t2) * k * path/2;
00207         ea[0][0] += dth2 * path * path/3;
00208         ea[0][1] += dth2 * sqrt(1 + t2) * path/2;
00209 
00210         Ea(ea);
00211         //cout<<"ms():ms angle in this: "<<dth<<endl;  
00212         //cout<<"ms():ea after: "<<Ea()<<endl;
00213         if (index < 0) {
00214                 double x0 = material.X0();
00215                 if (x0) path_rd_ += path/x0;
00216         }
00217 }
00218 
00219 void KalFitTrack::msgasmdc(double path, int index)
00220 {
00221         double k = kappa();
00222         double t = tanl();
00223         double t2 = t * t;
00224         double k2 = k * k;
00225 
00226         double pmag = ( 1 / fabs(k) ) * sqrt(1 + t2);
00227         double psq = pmag*pmag;
00228         /*
00229            double Zprims = 3/2*0.076 + 0.580/9.79*4.99*(4.99+1) +
00230            0.041/183.85*74*(74+1) + 0.302/26.98 * 13 * (13+1);
00231            double chicc = 0.00039612 * sqrt(Zprims * 0.001168);
00232            double dth = 2.557 * chicc * sqrt(path * (mass_*mass_ + psq)) / psq;
00233          */
00234 
00235         //std::cout<<" mdcGasRadlen: "<<mdcGasRadlen_<<std::endl;
00236         double pathx = path/mdcGasRadlen_;
00237         double dth =  0.0136* sqrt(pathx * (mass_*mass_ + psq))/psq 
00238                 *(1 + 0.038 * log(pathx));;
00239         HepSymMatrix ea = Ea();
00240 #ifdef YDEBUG
00241         cout<<"msgasmdc():path  "<<path<<"  pathx "<<pathx<<endl;
00242         cout<<"msgasmdc():dth  "<<dth<<endl;
00243         cout<<"msgasmdc():ea before: "<<ea<<endl;
00244 #endif
00245         double dth2 = dth * dth;
00246 
00247         ea[1][1] += (1 + t2) * dth2;
00248         ea[2][2] += k2 * t2 * dth2;
00249         ea[2][4] += k * t * (1 + t2) * dth2;
00250         ea[4][4] += (1 + t2) * (1 + t2) * dth2;
00251 
00252         // additionnal terms (terms proportional to l and l^2)
00253 
00254         ea[3][3] += dth2 * path * path /3 / (1 + t2);
00255         ea[3][4] += dth2 * path/2 * sqrt(1 + t2);
00256         ea[3][2] += dth2 * t / sqrt(1 + t2) * k * path/2;
00257         ea[0][0] += dth2 * path * path/3;
00258         ea[0][1] += dth2 * sqrt(1 + t2) * path/2;
00259 
00260         Ea(ea);
00261 #ifdef YDEBUG
00262         cout<<"msgasmdc():ea after: "<<Ea()<<endl;
00263 #endif
00264         if (index < 0) {
00265                 pathip_ += path;
00266                 // RMK : put by hand, take care !!
00267                 double x0 = mdcGasRadlen_; // for the Mdc gas
00268                 path_rd_ += path/x0;
00269                 tof(path);
00270 #ifdef YDEBUG
00271                 cout<<"ms...pathip..."<<pathip_<<endl;
00272 #endif
00273         }
00274 }
00275 
00276 void KalFitTrack::eloss(double path,
00277                 const KalFitMaterial& material, int index)
00278 {
00279 #ifdef YDEBUG
00280         cout<<"eloss():ea before: "<<Ea()<<endl;
00281 #endif
00282         HepVector v_a = a();
00283         double v_a_2_2 = v_a[2] * v_a[2];
00284         double v_a_4_2 = v_a[4] * v_a[4];
00285         double pmag = 1 / fabs(v_a[2]) * sqrt(1 + v_a_4_2);
00286         double psq = pmag * pmag;
00287         double E = sqrt(mass_ * mass_ + psq);
00288         double dE = material.dE(mass_, path, pmag);
00289         //std::cout<<" eloss(): dE: "<<dE<<std::endl;//wangll
00290 
00291         if (index > 0) 
00292                 psq += dE * (dE + 2 * sqrt(mass_ * mass_ + psq));
00293         else {
00294                 double dE_max = E - mass_;
00295                 if( dE<dE_max ) psq += dE * (dE - 2 * sqrt(mass_ * mass_ + psq));
00296                 else psq=-1.0;
00297         }
00298 
00299         if (tofall_ && index < 0){
00300                 // Kaon case :
00301                 if (p_kaon_ > 0){
00302                         double psq_kaon = p_kaon_ * p_kaon_;
00303                         double dE_kaon = material.dE(MASS[3], path, p_kaon_);
00304                         psq_kaon += dE_kaon * (dE_kaon - 
00305                                         2 * sqrt(MASS[3] * MASS[3] + psq_kaon));
00306                         if (psq_kaon < 0) psq_kaon = 0;
00307                         p_kaon_ = sqrt(psq_kaon);
00308                 }
00309                 // Proton case :
00310                 if (p_proton_ > 0){
00311                         double psq_proton = p_proton_ * p_proton_;
00312                         double dE_proton = material.dE(MASS[4], path, p_proton_);
00313                         psq_proton += dE_proton * (dE_proton - 
00314                                         2 * sqrt(MASS[4] * MASS[4] + psq_proton));
00315                         if (psq_proton < 0) psq_proton = 0;
00316                         p_proton_ = sqrt(psq_proton);
00317                 }
00318         }
00319 
00320         double dpt;
00321         //cout<<"eloss(): psq = "<<psq<<endl;//wangll
00322         if(psq < 0) dpt = 9999;
00323         else dpt = v_a[2] * pmag / sqrt(psq);
00324         //cout<<"eloss():k:   "<<v_a[2]<<"  k'  "<<dpt<<endl;//wangll
00325 #ifdef YDEBUG
00326         cout<<"eloss():k:   "<<v_a[2]<<"  k'  "<<dpt<<endl;
00327 #endif
00328         // attempt to take account of energy loss for error matrix 
00329 
00330         HepSymMatrix ea = Ea();
00331         double r_E_prim = (E + dE)/E;
00332 
00333         // 1/ Straggling in the energy loss :
00334         if (strag_){
00335                 double del_E(0);
00336                 if(l_mass_==0) {
00337                         del_E = dE*factor_strag_;
00338                 }   else  {
00339                         del_E  = material.del_E(mass_, path, pmag);
00340                 }
00341 
00342                 ea[2][2] += (v_a[2] * v_a[2]) * E * E * del_E * del_E / (psq*psq);
00343         }
00344 
00345         // Effect of the change of curvature (just variables change):
00346         double dpt2 = dpt*dpt;
00347         double A = dpt*dpt2/(v_a_2_2*v_a[2])*r_E_prim;
00348         double B = v_a[4]/(1+v_a_4_2)*
00349                 dpt*(1-dpt2/v_a_2_2*r_E_prim);
00350 
00351         double ea_2_0 = A*ea[2][0] + B*ea[4][0];
00352         double ea_2_1 = A*ea[2][1] + B*ea[4][1];
00353         double ea_2_2 = A*A*ea[2][2] + 2*A*B*ea[2][4] + B*B*ea[4][4]; 
00354         double ea_2_3 = A*ea[2][3] + B*ea[4][3]; 
00355         double ea_2_4 = A*ea[2][4] + B*ea[4][4];
00356 
00357         v_a[2] = dpt;
00358         a(v_a);
00359 
00360         ea[2][0] = ea_2_0;
00361         //std::cout<<"ea[2][0] is "<<ea[2][0]<<" ea(3,1) is "<<ea(3,1)<<std::endl;
00362         ea[2][1] = ea_2_1;
00363         //std::cout<<"ea[2][2] is "<<ea[2][2]<<" ea(3,3) is "<<ea(3,3)<<std::endl;
00364         ea[2][2] = ea_2_2;
00365         ea[2][3] = ea_2_3;
00366         ea[2][4] = ea_2_4;
00367 
00368         Ea(ea);
00369         //cout<<"eloss():dE:   "<<dE<<endl;
00370         //cout<<"eloss():A:   "<<A<<"   B:    "<<B<<endl;
00371         //cout<<"eloss():ea after: "<<Ea()<<endl;
00372         r0_ = fabs(center().perp() - fabs(radius()));
00373 }
00374 
00375 void KalFitTrack::order_wirhit(int index)
00376 {
00377         unsigned int nhit = HitsMdc_.size();
00378         Helix tracktest = *(Helix*)this;
00379         int ind = 0;
00380         double* Rad = new double[nhit];
00381         double* Ypos = new double[nhit];
00382         for( unsigned i=0 ; i < nhit; i++ ){
00383 
00384                 HepPoint3D fwd(HitsMdc_[i].wire().fwd());
00385                 HepPoint3D bck(HitsMdc_[i].wire().bck());
00386                 Hep3Vector wire = (CLHEP::Hep3Vector)fwd -(CLHEP::Hep3Vector)bck;
00387 
00388                 // Modification for stereo wires :
00389                 Helix work = tracktest;
00390                 work.ignoreErrorMatrix();
00391                 work.pivot((fwd + bck) * .5);
00392                 HepPoint3D x0 = (work.x(0).z() - bck.z())
00393                         / wire.z() * wire + bck;
00394 
00395                 tracktest.pivot(x0);
00396                 Rad[ind] = tracktest.x(0).perp();
00397                 Ypos[ind] = x0.y();
00398                 ind++;
00399                 //cout<<"Ypos: "<<Ypos[ind-1]<<endl;
00400         }
00401 
00402         // Reorder...
00403         if (index < 0)
00404                 for(int j, k = nhit - 1; k >= 0; k = j){
00405                         j = -1;
00406                         for(int i = 1; i <= k; i++)
00407                                 if(Rad[i - 1] > Rad[i]){
00408                                         j = i - 1;
00409                                         std::swap(Rad[i], Rad[j]);
00410                                         std::swap(HitsMdc_[i], HitsMdc_[j]);
00411                                 }
00412                 }
00413         if (index > 0)
00414                 for(int j, k = nhit - 1; k >= 0; k = j){
00415                         j = -1;
00416                         for(int i = 1; i <= k; i++)
00417                                 if(Rad[i - 1] < Rad[i]){
00418                                         j = i - 1;
00419                                         std::swap(Rad[i], Rad[j]);
00420                                         std::swap(HitsMdc_[i], HitsMdc_[j]);
00421                                 }
00422                 }
00423         if (index == 0)
00424                 for(int j, k = nhit - 1; k >= 0; k = j){
00425                         j = -1;
00426                         for(int i = 1; i <= k; i++)
00427                                 if(Ypos[i - 1] > Ypos[i]){
00428                                         j = i - 1;
00429                                         std::swap(Ypos[i], Ypos[j]);
00430                                         std::swap(HitsMdc_[i], HitsMdc_[j]);
00431                                 }
00432                 }
00433         delete [] Rad;
00434         delete [] Ypos;
00435 }
00436 
00437 void KalFitTrack::order_hits(void){
00438         for(int it=0; it<HitsMdc().size()-1; it++){
00439                 if(HitsMdc_[it].wire().layer().layerId() == HitsMdc_[it+1].wire().layer().layerId())    
00440                 {
00441                         if((kappa()<0)&&(HitsMdc_[it].wire().localId() > HitsMdc_[it+1].wire().localId())){
00442                                 std::swap(HitsMdc_[it], HitsMdc_[it+1]);
00443                         }
00444                         if((kappa()>0)&&(HitsMdc_[it].wire().localId() < HitsMdc_[it+1].wire().localId())){
00445                                 std::swap(HitsMdc_[it], HitsMdc_[it+1]);
00446                         }
00447                 }
00448         }
00449 }
00450 
00451 
00452 void KalFitTrack::number_wirhit(void)
00453 {
00454         unsigned int nhit = HitsMdc_.size();
00455         int Num[50] = {0};
00456         for( unsigned i=0 ; i < nhit; i++ )
00457                 Num[HitsMdc_[i].wire().layer().layerId()]++;
00458         for( unsigned i=0 ; i < nhit; i++ )
00459                 if (Num[HitsMdc_[i].wire().layer().layerId()]>2)
00460                         HitsMdc_[i].chi2(-2);
00461 }
00462 
00463 
00464 double KalFitTrack::smoother_Mdc(KalFitHitMdc& HitMdc, Hep3Vector& meas, KalFitHelixSeg& seg, double& dchi2, int csmflag)
00465 {
00466         // 
00467         double lr = HitMdc.LR();
00468         // For taking the propagation delay into account :
00469         int wire_ID = HitMdc.wire().geoID(); // from 0 to 6796
00470         int layerid = HitMdc.wire().layer().layerId();
00471         double entrangle = HitMdc.rechitptr()->getEntra();
00472         if (wire_ID<0 || wire_ID>6796){ //bes
00473                 std::cout << "KalFitTrack : wire_ID problem : " << wire_ID << std::endl;
00474                 return 0;
00475         } 
00476 
00477         double x[3] ={pivot().x(), pivot().y(), pivot().z()};
00478         double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
00479 
00480         double tofest(0);
00481         double dd(0.);
00482         double phi = fmod(phi0() + M_PI4, M_PI2);
00483         double csf0 = cos(phi);
00484         double snf0 = (1. - csf0) * (1. + csf0);
00485         snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
00486         if(phi > M_PI) snf0 = - snf0;
00487 
00488         if (Tof_correc_) {
00489                 Hep3Vector ip(0, 0, 0);
00490                 Helix work = *(Helix*)this;
00491                 work.ignoreErrorMatrix();
00492                 work.pivot(ip);
00493                 double phi_ip = work.phi0();
00494                 if (fabs(phi - phi_ip) > M_PI) {
00495                         if (phi > phi_ip) phi -= 2 * M_PI;
00496                         else phi_ip -= 2 * M_PI;
00497                 }
00498                 double t = tanl();
00499                 double l = fabs(radius() * (phi - phi_ip) * sqrt(1 + t * t));
00500                 double pmag( sqrt( 1.0 + t*t ) / kappa());
00501                 double mass_over_p( mass_ / pmag );
00502                 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
00503                 tofest = l / ( 29.9792458 * beta );
00504                 if(csmflag==1 && HitMdc.wire().y()>0.)  tofest= -1. * tofest;
00505         }
00506 
00507         const HepSymMatrix& ea = Ea();
00508         const HepVector& v_a = a();
00509 
00510         HepPoint3D pivot_work = pivot();
00511 
00512         double dchi2R(99999999), dchi2L(99999999);
00513 
00514         HepVector v_H(5, 0);
00515         v_H[0] =  -csf0 * meas.x() - snf0 * meas.y();
00516         v_H[3] =  -meas.z();
00517         HepMatrix v_HT = v_H.T();
00518 
00519         double estim = (v_HT * v_a)[0];
00520         HepVector ea_v_H = ea * v_H;  
00521         HepMatrix ea_v_HT = (ea_v_H).T();
00522         HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
00523 
00524         HepSymMatrix eaNew(5);
00525         HepVector aNew(5);
00526 
00527         //double time = HitMdc.tdc();
00528         // if (Tof_correc_)
00529         //  time -= tofest;
00530 
00531         double drifttime = getDriftTime(HitMdc , tofest); 
00532         //cout<<"drifttime = "<<drifttime<<endl;
00533         seg.dt(drifttime);
00534         double ddl = getDriftDist(HitMdc, drifttime, 0);
00535         double ddr = getDriftDist(HitMdc, drifttime, 1);
00536         /*cout<<endl<<" $$$  smoother_Mdc():: "<<endl;//wangll
00537         cout<<"pivot = "<<pivot()<<endl;//wangll
00538         cout<<"helix = "<<a()<<endl;//wangll
00539         cout<<"drifttime = "<<drifttime<<endl;//wangll
00540         cout<<"ddl, ddr = "<<ddl<<", "<<ddr<<endl;//wangll
00541         */
00542         double erddl = getSigma( HitMdc, a()[4], 0, ddl);
00543         double erddr = getSigma( HitMdc, a()[4], 1, ddr); 
00544 
00545         //  double dd = 1.0e-4 * 40.0 * time;
00546         double dmeas2[2] = { 0.1*ddl, 0.1*ddr }; //millimeter to centimeter 
00547         double er_dmeas2[2] = {0. , 0.};
00548         if(resolflag_== 1) { 
00549                 er_dmeas2[0] = 0.1*erddl;
00550                 er_dmeas2[1] = 0.1*erddr;
00551         } 
00552         else if(resolflag_ == 0) {
00553                 //int layid = HitMdc.wire().layer().layerId();
00554                 //double sigma = getSigma(layid, dd);
00555                 //er_dmeas2[0] = er_dmeas2[1] = sigma;
00556         }
00557 
00558         // Left hypothesis :
00559         if (lr == -1) {
00560                 double er_dmeasL, dmeasL;
00561                 if(Tof_correc_) {
00562                         dmeasL = (-1.0)*dmeas2[0];  // the dmeas&erdmeas  calculated by myself 
00563                         er_dmeasL = er_dmeas2[0];
00564                 } else {
00565                         dmeasL = (-1.0)*fabs(HitMdc.dist()[0]); // the dmeas&erdmeas  calculated by track-finding algorithm
00566                         er_dmeasL = HitMdc.erdist()[0];
00567                 }
00568 
00569                 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
00570                 eaNew.assign(ea - ea_v_H * AL * ea_v_HT);
00571                 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
00572                 if(0. == RkL) RkL = 1.e-4;
00573 
00574                 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
00575                 aNew = v_a + diffL;
00576                 double sigL = (dmeasL - (v_HT * aNew)[0]);
00577                 dchi2L =  (sigL*sigL) /  (RkL * er_dmeasL*er_dmeasL);
00578         } else if (lr == 1) {
00579                 // Right hypothesis :
00580 
00581                 double er_dmeasR, dmeasR;
00582                 if(Tof_correc_) {
00583                         dmeasR = dmeas2[1];
00584                         er_dmeasR = er_dmeas2[1];
00585                 } else {
00586                         dmeasR = fabs(HitMdc.dist()[1]);
00587                         er_dmeasR = HitMdc.erdist()[1];
00588                 }
00589 
00590 
00591                 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
00592                 eaNew.assign(ea - ea_v_H * AR * ea_v_HT);
00593                 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
00594                 if(0. == RkR) RkR = 1.e-4;
00595 
00596                 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
00597                 aNew = v_a + diffR;
00598                 double sigR = (dmeasR- (v_HT * aNew)[0]);
00599                 dchi2R =  (sigR*sigR) /  (RkR * er_dmeasR*er_dmeasR);
00600         }
00601 
00602         // Update Kalman result :
00603 #ifdef YDEBUG
00604         cout<<"in smoother_Mdc: lr= "<<lr<<" a: "<<v_a<<" a_NEW: "<<aNew<<endl;
00605 #endif
00606         double dchi2_loc(0);
00607         if ((dchi2R < dchi2cuts_anal[layerid] && lr == 1) || 
00608                         (dchi2L < dchi2cuts_anal[layerid] && lr == -1)) {
00609                 ndf_back_++;
00610                 int chge(1);
00611                 if ( !( aNew[2] && fabs(aNew[2]-a()[2]) < 1.0 ) ) chge=0;
00612                 if (lr == 1) {
00613                         chiSq_back_ += dchi2R;
00614                         dchi2_loc = dchi2R;
00615                         dd = 0.1*ddr;
00616                 } else if (lr == -1) {
00617                         chiSq_back_ += dchi2L;
00618                         dchi2_loc = dchi2L;
00619                         dd = -0.1*ddl;
00620                 } 
00621                 if (chge){
00622                         a(aNew);
00623                         Ea(eaNew);
00624                 }
00625         }
00626         dchi2=dchi2_loc;
00627 
00628         seg.doca_include(fabs((v_HT*a())[0]));
00629         seg.doca_exclude(fabs(estim));
00631         Hep3Vector ip(0, 0, 0);
00632         KalFitTrack helixNew1(pivot_work, a(), Ea(), 0, 0, 0);
00633         helixNew1.pivot(ip);
00634         HepVector    a_temp1  = helixNew1.a();
00635         HepSymMatrix ea_temp1 = helixNew1.Ea();
00636         seg.Ea(ea_temp1);
00637         seg.a(a_temp1);
00638         seg.Ea_include(ea_temp1);
00639         seg.a_include(a_temp1);
00640 
00641         KalFitTrack helixNew2(pivot_work, v_a, ea, 0, 0, 0);
00642         helixNew2.pivot(ip);
00643         HepVector    a_temp2  = helixNew2.a();
00644         HepSymMatrix ea_temp2 = helixNew2.Ea();
00645         seg.Ea_exclude(ea_temp2);
00646         seg.a_exclude(a_temp2);
00647 
00648         seg.tof(tofest);
00649         seg.dd(dd);
00650 
00651         return chiSq_back_;
00652 }
00653 
00654 // Smoothing procedure in Mdc cosmic align
00655 // RMK attention for the case chi2R = chi2L during forward filter !
00656 double KalFitTrack::smoother_Mdc_csmalign(KalFitHelixSeg& seg, Hep3Vector& meas,int& flg, int csmflag )
00657 {
00658 
00659 
00660         HepPoint3D ip(0., 0., 0.);
00661         // attention! we should not to decide the left&right in the smoother process,
00662         // because we choose the left&right of hits from the filter process. 
00663 
00664         KalFitHitMdc* HitMdc = seg.HitMdc();
00665         double lr = HitMdc->LR();
00666 
00667         // For taking the propagation delay into account :
00668         int layerid  = (*HitMdc).wire().layer().layerId();
00669         int wire_ID = HitMdc->wire().geoID();
00670         double entrangle = HitMdc->rechitptr()->getEntra();
00671 
00672         if (wire_ID<0 || wire_ID>6796){ //bes
00673                 std::cout << "KalFitTrack : wire_ID problem : " << wire_ID << std::endl;
00674                 return 0;
00675         } 
00676 
00677         double x[3] ={pivot().x(), pivot().y(), pivot().z()};
00678         double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
00679         double dd(0.);
00680         double tofest(0);
00681         double phi = fmod(phi0() + M_PI4, M_PI2);
00682         double csf0 = cos(phi);
00683         double snf0 = (1. - csf0) * (1. + csf0);
00684         snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
00685         if(phi > M_PI) snf0 = - snf0;
00686 
00687         if (Tof_correc_) {
00688                 Hep3Vector ip(0, 0, 0);
00689                 Helix work = *(Helix*)this;
00690                 work.ignoreErrorMatrix();
00691                 work.pivot(ip);
00692                 double phi_ip = work.phi0();
00693                 if (fabs(phi - phi_ip) > M_PI) {
00694                         if (phi > phi_ip) phi -= 2 * M_PI;
00695                         else phi_ip -= 2 * M_PI;
00696                 }
00697                 double t = tanl();
00698                 double l = fabs(radius() * (phi - phi_ip) * sqrt(1 + t * t));
00699                 double pmag( sqrt( 1.0 + t*t ) / kappa());
00700                 double mass_over_p( mass_ / pmag );
00701                 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
00702                 tofest = l / ( 29.9792458 * beta );
00703                 if(csmflag==1 && (*HitMdc).wire().y()>0.)  tofest= -1. * tofest;
00704         }
00705 
00706         const HepSymMatrix& ea = Ea();
00707         const HepVector& v_a = a();
00708 
00709 
00710         HepPoint3D pivot_work = pivot();
00711 
00712         /*
00713            HepVector a_work = a();
00714            HepSymMatrix ea_work = Ea();
00715 
00716            KalFitTrack helix_work(pivot_work, a_work, ea_work, 0, 0, 0);
00717            helix_work.pivot(ip);
00718 
00719            HepVector a_temp = helix_work.a();
00720            HepSymMatrix ea_temp = helix_work.Ea();
00721 
00722            seg.Ea_pre_bwd(ea_temp);     
00723            seg.a_pre_bwd(a_temp);
00724 
00725          */
00726 
00727         seg.a_pre_bwd(a()); 
00728         seg.Ea_pre_bwd(Ea()); 
00729 
00730         double dchi2R(99999999), dchi2L(99999999);
00731         HepVector v_H(5, 0);
00732         v_H[0] =  -csf0 * meas.x() - snf0 * meas.y();
00733         v_H[3] =  -meas.z();
00734         HepMatrix v_HT = v_H.T();
00735 
00736         double estim = (v_HT * v_a)[0];
00737         HepVector ea_v_H = ea * v_H;  
00738         HepMatrix ea_v_HT = (ea_v_H).T();
00739         HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
00740         HepSymMatrix eaNew(5);
00741         HepVector aNew(5);
00742         double time = (*HitMdc).tdc();
00743 
00744         //seg.dt(time);
00745         // if (Tof_correc_)
00746         // { 
00747         //  time -= tofest;
00748         //  seg.dt(time);
00749         // }
00750         // double dd = 1.0e-4 * 40.0 * time;
00751         // seg.dd(dd);
00752 
00753         double drifttime = getDriftTime(*HitMdc , tofest);
00754         seg.dt(drifttime);
00755         double ddl = getDriftDist(*HitMdc, drifttime, 0 );
00756         double ddr = getDriftDist(*HitMdc, drifttime, 1 );
00757         double erddl = getSigma( *HitMdc, a()[4], 0, ddl);
00758         double erddr = getSigma( *HitMdc, a()[4], 1, ddr);
00759 
00760         double dmeas2[2] = { 0.1*ddl, 0.1*ddr }; // millimeter to centimeter
00761         double er_dmeas2[2] = {0., 0.};
00762         if(resolflag_ == 1) { 
00763                 er_dmeas2[0] = 0.1*erddl;
00764                 er_dmeas2[1] = 0.1*erddr;
00765         }else if(resolflag_ == 0)  
00766         {
00767         }
00768 
00769         // Left hypothesis :
00770         if (lr == -1) {
00771                 double er_dmeasL, dmeasL;
00772                 if(Tof_correc_) {
00773                         dmeasL = (-1.0)*dmeas2[0];
00774                         er_dmeasL = er_dmeas2[0];
00775                 } else {
00776                         dmeasL = (-1.0)*fabs((*HitMdc).dist()[0]);
00777                         er_dmeasL = (*HitMdc).erdist()[0];
00778                 }
00779 
00780 
00781                 //if(layerid < 4)  er_dmeasL*=2.0;
00782 
00783                 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
00784                 eaNew.assign(ea - ea_v_H * AL * ea_v_HT);
00785                 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
00786                 if(0. == RkL) RkL = 1.e-4;
00787 
00788                 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
00789                 aNew = v_a + diffL;
00790                 double sigL = (dmeasL - (v_HT * aNew)[0]);
00791                 dchi2L =  (sigL*sigL) /  (RkL * er_dmeasL*er_dmeasL);
00792         } else if (lr == 1) {
00793                 // Right hypothesis :
00794 
00795                 double er_dmeasR, dmeasR;
00796                 if(Tof_correc_) {
00797                         dmeasR = dmeas2[1];
00798                         er_dmeasR = er_dmeas2[1];
00799                 } else {
00800                         dmeasR = fabs((*HitMdc).dist()[1]);
00801                         er_dmeasR = (*HitMdc).erdist()[1];
00802                 }
00803 
00804 
00805                 //if(layerid < 4)  er_dmeasR*=2.0;
00806 
00807 
00808                 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
00809                 eaNew.assign(ea - ea_v_H * AR * ea_v_HT);
00810                 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
00811                 if(0. == RkR) RkR = 1.e-4;
00812 
00813                 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
00814                 aNew = v_a + diffR;
00815                 double sigR = (dmeasR- (v_HT * aNew)[0]);
00816                 dchi2R =  (sigR*sigR) /  (RkR * er_dmeasR*er_dmeasR);
00817         }
00818 
00819         // Update Kalman result :
00820 #ifdef YDEBUG
00821         cout<<"in smoother_Mdc: lr= "<<lr<<" a: "<<v_a<<" a_NEW: "<<aNew<<endl;
00822 #endif
00823         double dchi2_loc(0);
00824         if ((dchi2R < dchi2cuts_calib[layerid] && lr == 1) || 
00825                         (dchi2L < dchi2cuts_calib[layerid] && lr == -1)) {
00826 
00827                 ndf_back_++;
00828                 flg = 1;
00829                 int chge(1);
00830                 if (!(aNew[2] && fabs(aNew[2]-a()[2]) < 1.0))  chge = 0;
00831                 if (lr == 1) {
00832                         chiSq_back_ += dchi2R;
00833                         dchi2_loc = dchi2R;
00834                         dd = 0.1*ddr;
00835                         // if(debug_ ==4) std::cout<<"in  smoother "<<std::endl;
00836 
00837                 } else if (lr == -1) {
00838                         chiSq_back_ += dchi2L;
00839                         dchi2_loc = dchi2L;
00840                         dd = -0.1*ddl;
00841 
00842                 } 
00843                 if (chge){
00844                         a(aNew);
00845                         Ea(eaNew);
00846                 }
00847 
00848                 seg.a_filt_bwd(aNew);
00849                 seg.Ea_filt_bwd(eaNew);
00850 
00851                 HepVector a_pre_fwd_temp=seg.a_pre_fwd();
00852                 if( (a_pre_fwd_temp[1]-seg.a_pre_bwd()[1]) > 3. * M_PI /2.)  a_pre_fwd_temp[1]-= M_PI2;
00853                 if( (a_pre_fwd_temp[1]-seg.a_pre_bwd()[1]) < -3. * M_PI /2. )  a_pre_fwd_temp[1]+= M_PI2;
00854 
00855                 seg.a_pre_fwd(a_pre_fwd_temp);
00856 
00857                 HepVector a_pre_filt_temp=seg.a_filt_fwd();
00858                 if( (a_pre_filt_temp[1]-seg.a_pre_bwd()[1]) > 3. * M_PI /2. )  a_pre_filt_temp[1] -= M_PI2;
00859                 if( (a_pre_filt_temp[1]-seg.a_pre_bwd()[1]) < -3. * M_PI /2.)  a_pre_filt_temp[1] += M_PI2;
00860                 seg.a_filt_fwd(a_pre_filt_temp);
00861 
00862                 /*   
00863                      KalFitTrack helixNew(pivot_work, aNew, eaNew, 0, 0, 0);
00864                      helixNew.pivot(ip);
00865                      a_temp = helixNew.a();
00866                      ea_temp = helixNew.Ea();
00867                      seg.Ea_filt_bwd(ea_temp);
00868                      seg.a_filt_bwd(a_temp);
00869                  */
00870 
00871                 int inv;
00872 
00873                 if(debug_ == 4){
00874                         std::cout<<"seg.Ea_pre_bwd().inverse(inv) ..."<<seg.Ea_pre_bwd().inverse(inv)<<std::endl;
00875                         std::cout<<"seg.Ea_pre_fwd().inverse(inv) ..."<<seg.Ea_pre_fwd().inverse(inv)<<std::endl;
00876                 }
00877 
00878                 HepSymMatrix  Ea_pre_comb = (seg.Ea_pre_bwd().inverse(inv)+seg.Ea_pre_fwd().inverse(inv)).inverse(inv);
00879                 seg.Ea_exclude(Ea_pre_comb);
00880 
00881 
00882                 if(debug_ == 4) {
00883                         std::cout<<"Ea_pre_comb_ ... "<<Ea_pre_comb<<std::endl;
00884                         std::cout<<"seg.a_pre_bwd()..."<<seg.a_pre_bwd()<<std::endl;
00885                         std::cout<<"seg.a_pre_fwd()..."<<seg.a_pre_fwd()<<std::endl;
00886                 }
00887                 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());
00888                 seg.a_exclude(a_pre_comb);
00889 
00890                 if(debug_ == 4) {
00891                         std::cout<<"seg.Ea_filt_fwd()..."<<seg.Ea_filt_fwd()<<std::endl;
00892                         std::cout<<"seg.Ea_filt_fwd().inverse(inv)..."<<seg.Ea_filt_fwd().inverse(inv)<<std::endl;
00893                 }
00894                 seg.Ea((seg.Ea_filt_fwd().inverse(inv)+seg.Ea_pre_bwd().inverse(inv)).inverse(inv));
00895                 seg.Ea_include((seg.Ea_filt_fwd().inverse(inv)+seg.Ea_pre_bwd().inverse(inv)).inverse(inv));
00896 
00897                 if(debug_ == 4) {
00898                         std::cout<<"seg.Ea() is ..."<<seg.Ea()<<std::endl;
00899                         std::cout<<"seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd() ..."<<seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd()<<std::endl;
00900                         std::cout<<"seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd() ... "<<seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd()<<std::endl;
00901                 }
00902 
00903                 seg.a(seg.Ea()*(seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd()+seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd()));
00904                 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()));
00905 
00906                 if(inv != 0) {
00907                         //std::cout<<"ERROR OCCUR WHEN INVERSE MATRIX !"<<std::endl;
00908                 }
00909 
00910                 seg.residual_exclude(dd - (v_HT*a_pre_comb)[0]);
00911                 seg.residual_include(dd - (v_HT*seg.a())[0]);
00912                 seg.doca_exclude(fabs((v_HT*a_pre_comb)[0]));
00913                 seg.doca_include(fabs((v_HT*seg.a())[0]));
00914 
00915                 if(debug_ == 4){
00916                         std::cout<<"the dd in smoother_Mdc is "<<dd
00917                                 <<" the (v_HT*a_pre_comb)[0] is "<<(v_HT*a_pre_comb)[0]<<std::endl;
00918                 }
00919 
00920                 //compare the two method to calculate the include doca value : 
00921                 if(debug_ == 4){
00922                         std::cout<<"method 1 ..."<<sqrt(seg.a()[0]*seg.a()[0]+seg.a()[3]*seg.a()[3])<<std::endl;
00923                         std::cout<<"method 2 ..."<<fabs((v_HT*seg.a())[0])<<std::endl;
00924                 }
00925 
00926 
00928                 KalFitTrack helixNew1(pivot_work, seg.a(), seg.Ea(), 0, 0, 0);
00929                 helixNew1.pivot(ip);
00930                 HepVector    a_temp1  = helixNew1.a();
00931                 HepSymMatrix ea_temp1 = helixNew1.Ea();
00932                 seg.Ea(ea_temp1);
00933                 seg.a(a_temp1);
00934                 seg.Ea_include(ea_temp1);
00935                 seg.a_include(a_temp1);
00936 
00937                 KalFitTrack helixNew2(pivot_work, seg.a_exclude(), seg.Ea_exclude(), 0, 0, 0);
00938                 helixNew2.pivot(ip);
00939                 HepVector    a_temp2  = helixNew2.a();
00940                 HepSymMatrix ea_temp2 = helixNew2.Ea();    
00941                 seg.Ea_exclude(ea_temp2);                    
00942                 seg.a_exclude(a_temp2);             
00943 
00944                 seg.tof(tofest);
00945                 seg.dd(dd);
00946 
00947         }
00948         return chiSq_back_;
00949 }
00950 
00951 // Smoothing procedure in Mdc calib
00952 // RMK attention for the case chi2R = chi2L during forward filter !
00953 double KalFitTrack::smoother_Mdc(KalFitHelixSeg& seg, Hep3Vector& meas,int& flg, int csmflag )
00954 {
00955 
00956 
00957         HepPoint3D ip(0., 0., 0.);
00958         // attention! we should not to decide the left&right in the smoother process,
00959         // because we choose the left&right of hits from the filter process. 
00960 
00961         KalFitHitMdc* HitMdc = seg.HitMdc();
00962         double lr = HitMdc->LR();
00963 
00964         // For taking the propagation delay into account :
00965         int layerid  = (*HitMdc).wire().layer().layerId();
00966         int wire_ID = HitMdc->wire().geoID();
00967         double entrangle = HitMdc->rechitptr()->getEntra();
00968 
00969         if (wire_ID<0 || wire_ID>6796){ //bes
00970                 std::cout << "KalFitTrack : wire_ID problem : " << wire_ID << std::endl;
00971                 return 0;
00972         } 
00973 
00974         double x[3] ={pivot().x(), pivot().y(), pivot().z()};
00975         double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
00976         double dd(0.);
00977         double tofest(0);
00978         double phi = fmod(phi0() + M_PI4, M_PI2);
00979         double csf0 = cos(phi);
00980         double snf0 = (1. - csf0) * (1. + csf0);
00981         snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
00982         if(phi > M_PI) snf0 = - snf0;
00983 
00984         if (Tof_correc_) {
00985                 Hep3Vector ip(0, 0, 0);
00986                 Helix work = *(Helix*)this;
00987                 work.ignoreErrorMatrix();
00988                 work.pivot(ip);
00989                 double phi_ip = work.phi0();
00990                 if (fabs(phi - phi_ip) > M_PI) {
00991                         if (phi > phi_ip) phi -= 2 * M_PI;
00992                         else phi_ip -= 2 * M_PI;
00993                 }
00994                 double t = tanl();
00995                 double l = fabs(radius() * (phi - phi_ip) * sqrt(1 + t * t));
00996                 double pmag( sqrt( 1.0 + t*t ) / kappa());
00997                 double mass_over_p( mass_ / pmag );
00998                 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
00999                 tofest = l / ( 29.9792458 * beta );
01000                 if(csmflag==1 && (*HitMdc).wire().y()>0.)  tofest= -1. * tofest;
01001         }
01002 
01003         const HepSymMatrix& ea = Ea();
01004         const HepVector& v_a = a();
01005 
01006 
01007 
01008         HepPoint3D pivot_work = pivot();
01009 
01010         /*
01011            HepVector a_work = a();
01012            HepSymMatrix ea_work = Ea();
01013 
01014            KalFitTrack helix_work(pivot_work, a_work, ea_work, 0, 0, 0);
01015            helix_work.pivot(ip);
01016 
01017            HepVector a_temp = helix_work.a();
01018            HepSymMatrix ea_temp = helix_work.Ea();
01019 
01020            seg.Ea_pre_bwd(ea_temp);     
01021            seg.a_pre_bwd(a_temp);
01022 
01023          */
01024 
01025         seg.a_pre_bwd(a()); 
01026         seg.Ea_pre_bwd(Ea()); 
01027 
01028         double dchi2R(99999999), dchi2L(99999999);
01029         HepVector v_H(5, 0);
01030         v_H[0] =  -csf0 * meas.x() - snf0 * meas.y();
01031         v_H[3] =  -meas.z();
01032         HepMatrix v_HT = v_H.T();
01033 
01034         double estim = (v_HT * v_a)[0];
01035         HepVector ea_v_H = ea * v_H;  
01036         HepMatrix ea_v_HT = (ea_v_H).T();
01037         HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
01038         HepSymMatrix eaNew(5);
01039         HepVector aNew(5);
01040         double time = (*HitMdc).tdc();
01041 
01042         //seg.dt(time);
01043         // if (Tof_correc_)
01044         // { 
01045         //  time -= tofest;
01046         //  seg.dt(time);
01047         // }
01048         // double dd = 1.0e-4 * 40.0 * time;
01049         // seg.dd(dd);
01050 
01051         double drifttime = getDriftTime(*HitMdc , tofest);
01052         seg.dt(drifttime);
01053         double ddl = getDriftDist(*HitMdc, drifttime, 0 );
01054         double ddr = getDriftDist(*HitMdc, drifttime, 1 );
01055         double erddl = getSigma( *HitMdc, a()[4], 0, ddl);
01056         double erddr = getSigma( *HitMdc, a()[4], 1, ddr);
01057 
01058         double dmeas2[2] = { 0.1*ddl, 0.1*ddr }; // millimeter to centimeter
01059         double er_dmeas2[2] = {0., 0.};
01060         if(resolflag_ == 1) { 
01061                 er_dmeas2[0] = 0.1*erddl;
01062                 er_dmeas2[1] = 0.1*erddr;
01063         }else if(resolflag_ == 0)  
01064         {
01065         }
01066 
01067         // Left hypothesis :
01068         if (lr == -1) {
01069                 double er_dmeasL, dmeasL;
01070                 if(Tof_correc_) {
01071                         dmeasL = (-1.0)*dmeas2[0];
01072                         er_dmeasL = er_dmeas2[0];
01073                 } else {
01074                         dmeasL = (-1.0)*fabs((*HitMdc).dist()[0]);
01075                         er_dmeasL = (*HitMdc).erdist()[0];
01076                 }
01077 
01078 
01079                 //if(layerid < 4)  er_dmeasL*=2.0;
01080 
01081                 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
01082                 eaNew.assign(ea - ea_v_H * AL * ea_v_HT);
01083                 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
01084                 if(0. == RkL) RkL = 1.e-4;
01085 
01086                 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
01087                 aNew = v_a + diffL;
01088                 double sigL = (dmeasL - (v_HT * aNew)[0]);
01089                 dchi2L =  (sigL*sigL) /  (RkL * er_dmeasL*er_dmeasL);
01090         } else if (lr == 1) {
01091                 // Right hypothesis :
01092 
01093                 double er_dmeasR, dmeasR;
01094                 if(Tof_correc_) {
01095                         dmeasR = dmeas2[1];
01096                         er_dmeasR = er_dmeas2[1];
01097                 } else {
01098                         dmeasR = fabs((*HitMdc).dist()[1]);
01099                         er_dmeasR = (*HitMdc).erdist()[1];
01100                 }
01101 
01102 
01103                 //if(layerid < 4)  er_dmeasR*=2.0;
01104 
01105 
01106                 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
01107                 eaNew.assign(ea - ea_v_H * AR * ea_v_HT);
01108                 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
01109                 if(0. == RkR) RkR = 1.e-4;
01110 
01111                 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
01112                 aNew = v_a + diffR;
01113                 double sigR = (dmeasR- (v_HT * aNew)[0]);
01114                 dchi2R =  (sigR*sigR) /  (RkR * er_dmeasR*er_dmeasR);
01115         }
01116 
01117         // Update Kalman result :
01118 #ifdef YDEBUG
01119         cout<<"in smoother_Mdc: lr= "<<lr<<" a: "<<v_a<<" a_NEW: "<<aNew<<endl;
01120 #endif
01121         double dchi2_loc(0);
01122         if ((dchi2R < dchi2cuts_calib[layerid] && lr == 1) || 
01123                         (dchi2L < dchi2cuts_calib[layerid] && lr == -1)) {
01124 
01125                 ndf_back_++;
01126                 flg = 1;
01127                 int chge(1);
01128                 if (!(aNew[2] && fabs(aNew[2]-a()[2]) < 1.0))  chge = 0;
01129                 if (lr == 1) {
01130                         chiSq_back_ += dchi2R;
01131                         dchi2_loc = dchi2R;
01132                         dd = 0.1*ddr;
01133                         // if(debug_ ==4) std::cout<<"in  smoother "<<std::endl;
01134 
01135                 } else if (lr == -1) {
01136                         chiSq_back_ += dchi2L;
01137                         dchi2_loc = dchi2L;
01138                         dd = -0.1*ddl;
01139 
01140                 } 
01141                 if (chge){
01142                         a(aNew);
01143                         Ea(eaNew);
01144                 }
01145 
01146                 seg.a_filt_bwd(aNew);
01147                 seg.Ea_filt_bwd(eaNew);
01148 
01149                 HepVector a_pre_fwd_temp=seg.a_pre_fwd();
01150                 if( (a_pre_fwd_temp[1]-seg.a_pre_bwd()[1]) > 3. * M_PI /2.)  a_pre_fwd_temp[1]-= M_PI2;
01151                 if( (a_pre_fwd_temp[1]-seg.a_pre_bwd()[1]) < -3. * M_PI /2. )  a_pre_fwd_temp[1]+= M_PI2;
01152 
01153                 seg.a_pre_fwd(a_pre_fwd_temp);
01154 
01155                 HepVector a_pre_filt_temp=seg.a_filt_fwd();
01156                 if( (a_pre_filt_temp[1]-seg.a_pre_bwd()[1]) > 3. * M_PI /2. )  a_pre_filt_temp[1] -= M_PI2;
01157                 if( (a_pre_filt_temp[1]-seg.a_pre_bwd()[1]) < -3. * M_PI /2.)  a_pre_filt_temp[1] += M_PI2;
01158                 seg.a_filt_fwd(a_pre_filt_temp);
01159 
01160                 /*   
01161                      KalFitTrack helixNew(pivot_work, aNew, eaNew, 0, 0, 0);
01162                      helixNew.pivot(ip);
01163                      a_temp = helixNew.a();
01164                      ea_temp = helixNew.Ea();
01165                      seg.Ea_filt_bwd(ea_temp);
01166                      seg.a_filt_bwd(a_temp);
01167                  */
01168 
01169                 int inv;
01170 
01171                 if(debug_ == 4){
01172                         std::cout<<"seg.Ea_pre_bwd().inverse(inv) ..."<<seg.Ea_pre_bwd().inverse(inv)<<std::endl;
01173                         std::cout<<"seg.Ea_pre_fwd().inverse(inv) ..."<<seg.Ea_pre_fwd().inverse(inv)<<std::endl;
01174                 }
01175 
01176                 HepSymMatrix  Ea_pre_comb = (seg.Ea_pre_bwd().inverse(inv)+seg.Ea_pre_fwd().inverse(inv)).inverse(inv);
01177                 seg.Ea_exclude(Ea_pre_comb);
01178 
01179 
01180                 if(debug_ == 4) {
01181                         std::cout<<"Ea_pre_comb_ ... "<<Ea_pre_comb<<std::endl;
01182                         std::cout<<"seg.a_pre_bwd()..."<<seg.a_pre_bwd()<<std::endl;
01183                         std::cout<<"seg.a_pre_fwd()..."<<seg.a_pre_fwd()<<std::endl;
01184                 }
01185                 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());
01186                 seg.a_exclude(a_pre_comb);
01187 
01188 
01189                 if(debug_ == 4) {
01190                         std::cout<<"seg.Ea_filt_fwd()..."<<seg.Ea_filt_fwd()<<std::endl;
01191                         std::cout<<"seg.Ea_filt_fwd().inverse(inv)..."<<seg.Ea_filt_fwd().inverse(inv)<<std::endl;
01192                 }
01193                 seg.Ea((seg.Ea_filt_fwd().inverse(inv)+seg.Ea_pre_bwd().inverse(inv)).inverse(inv));
01194                 seg.Ea_include((seg.Ea_filt_fwd().inverse(inv)+seg.Ea_pre_bwd().inverse(inv)).inverse(inv));
01195 
01196                 if(debug_ == 4) {
01197                         std::cout<<"seg.Ea() is ..."<<seg.Ea()<<std::endl;
01198                         std::cout<<"seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd() ..."<<seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd()<<std::endl;
01199                         std::cout<<"seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd() ... "<<seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd()<<std::endl;
01200                 }
01201 
01202                 seg.a(seg.Ea()*(seg.Ea_filt_fwd().inverse(inv)*seg.a_filt_fwd()+seg.Ea_pre_bwd().inverse(inv)*seg.a_pre_bwd()));
01203                 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()));
01204 
01205                 if(inv != 0) {
01206                         //std::cout<<"ERROR OCCUR WHEN INVERSE MATRIX !"<<std::endl;
01207                 }
01208 
01209                 seg.residual_exclude(dd - (v_HT*a_pre_comb)[0]);
01210                 seg.residual_include(dd - (v_HT*seg.a())[0]);
01211                 seg.doca_exclude(fabs((v_HT*a_pre_comb)[0]));
01212                 seg.doca_include(fabs((v_HT*seg.a())[0]));
01213 
01214                 if(debug_ == 4){
01215                         std::cout<<"the dd in smoother_Mdc is "<<dd
01216                                 <<" the (v_HT*a_pre_comb)[0] is "<<(v_HT*a_pre_comb)[0]<<std::endl;
01217                 }
01218 
01219                 //compare the two method to calculate the include doca value : 
01220                 if(debug_ == 4){
01221                         std::cout<<"method 1 ..."<<sqrt(seg.a()[0]*seg.a()[0]+seg.a()[3]*seg.a()[3])<<std::endl;
01222                         std::cout<<"method 2 ..."<<fabs((v_HT*seg.a())[0])<<std::endl;
01223                 }
01224 
01225 
01227                 KalFitTrack helixNew1(pivot_work, seg.a(), seg.Ea(), 0, 0, 0);
01228                 helixNew1.pivot(ip);
01229                 HepVector    a_temp1  = helixNew1.a();
01230                 HepSymMatrix ea_temp1 = helixNew1.Ea();
01231                 seg.Ea(ea_temp1);
01232                 seg.a(a_temp1);
01233                 seg.Ea_include(ea_temp1);
01234                 seg.a_include(a_temp1);
01235 
01236                 KalFitTrack helixNew2(pivot_work, seg.a_exclude(), seg.Ea_exclude(), 0, 0, 0);
01237                 helixNew2.pivot(ip);
01238                 HepVector    a_temp2  = helixNew2.a();
01239                 HepSymMatrix ea_temp2 = helixNew2.Ea();    
01240                 seg.Ea_exclude(ea_temp2);                    
01241                 seg.a_exclude(a_temp2);             
01242 
01243                 seg.tof(tofest);
01244                 seg.dd(dd);
01245 
01246         }
01247         return chiSq_back_;
01248 }
01249 
01250 
01251 
01252 
01253 
01254 double KalFitTrack::filter(double v_m, const HepVector& m_H,
01255                 double v_d, double m_V)
01256 {
01257         HepMatrix m_HT = m_H.T();
01258         HepPoint3D x0 = x(0);
01259         HepVector v_x(3);
01260         v_x[0] = x0.x();
01261         v_x[1] = x0.y();
01262         v_x[2] = x0.z();
01263         HepMatrix m_X = delXDelA(0);
01264         HepMatrix m_XT = m_X.T();
01265         HepMatrix m_C = m_X * Ea() * m_XT;
01266         //int err;
01267         HepVector m_K = 1 / (m_V + (m_HT * m_C * m_H)[0]) * m_H;
01268         HepVector v_a = a();
01269         HepSymMatrix ea = Ea();
01270         HepMatrix mXTmK = m_XT * m_K;
01271         double v_r = v_m - v_d - (m_HT * v_x)[0];
01272         v_a += ea * mXTmK * v_r;
01273         a(v_a);
01274         ea.assign(ea - ea * mXTmK * m_HT * m_X * ea);
01275         Ea(ea);
01276         // Record last hit included parameters :
01277         update_last();
01278         HepMatrix mCmK = m_C * m_K;
01279         v_x += mCmK * v_r;
01280         m_C -= mCmK * m_HT * m_C;
01281         v_r = v_m - v_d - (m_HT * v_x)[0];
01282         double m_R = m_V - (m_HT * m_C * m_H)[0];
01283         double chiSq = v_r * v_r / m_R;
01284         chiSq_ += chiSq;
01285         return chiSq;
01286 }
01287 
01288 
01289 void KalFitTrack::path_add(double path)
01290 {
01291         pathip_ += path;
01292         tof(path);
01293 }
01294 
01295 
01296 void KalFitTrack::addPathSM(double path){
01297         pathSM_ += path;
01298 }
01299 
01300 
01301 void KalFitTrack::addTofSM(double time){
01302         tof2_ += time;
01303 } 
01304 
01305 
01306 void KalFitTrack::fiTerm(double fi){
01307         fiTerm_ = fi;
01308 }
01309 
01310 
01311 void KalFitTrack::tof(double path)
01312 {
01313         double light_speed( 29.9792458 );     // light speed in cm/nsec
01314         double t = tanl();
01315         double pmag( sqrt( 1.0 + t*t ) / kappa());
01316         if (pmag !=0) {
01317                 double mass_over_p( mass_ / pmag );
01318                 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
01319                 tof_ += path / ( light_speed * beta );
01320         }
01321 
01322         if (tofall_) {
01323                 if (p_kaon_ > 0){
01324                         double massk_over_p( MASS[3] / p_kaon_ );
01325                         double beta_kaon( 1.0 / sqrt( 1.0 + massk_over_p * massk_over_p ) );
01326                         tof_kaon_ += path / (light_speed * beta_kaon);
01327                 }
01328                 if (p_proton_ > 0){
01329                         double massp_over_p( MASS[4] / p_proton_ );
01330                         double beta_proton( 1.0 / sqrt( 1.0 + massp_over_p * massp_over_p ) );
01331                         tof_proton_ += path / (light_speed * beta_proton);
01332                 }
01333         }
01334 }
01335 
01336 double KalFitTrack::radius_numf(void) const {
01337 
01338         double Bz(Bznom_);
01339         //std::cout<<"Bz: "<<Bz<<std::endl;
01340         if (numf_ > 10){
01341                 double dr    = a()[0];
01342                 double phi0  = a()[1];
01343                 double dz    = a()[3];
01344                 double phi = fmod(phi0 + M_PI4, M_PI2);
01345                 double csf0 = cos(phi);
01346                 double snf0 = (1. - csf0) * (1. + csf0);
01347                 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
01348                 if(phi > M_PI) snf0 = - snf0;
01349                 //XYZPoint
01350                 HepPoint3D x0((pivot().x() + dr*csf0),
01351                                 (pivot().y() + dr*snf0),
01352                                 (pivot().z() + dz));
01353 
01354                 //XYZVector b;
01355                 HepVector3D b;
01356 
01357                 //std::cout<<"b: "<<b<<std::endl;
01358 
01359                 MFSvc_->fieldVector(10.*x0, b);
01360 
01361                 //std::cout<<"b: "<<b<<std::endl;
01362 
01363 
01364                 b = 10000.*b;
01365                 Bz = b.z();
01366         }
01367         if (Bz == 0)
01368                 Bz = Bznom_;
01369         double ALPHA_loc = 10000./2.99792458/Bz;
01370         return ALPHA_loc / a()[2];
01371 }
01372 
01373 const HepPoint3D &
01374 KalFitTrack::pivot_numf(const HepPoint3D & newPivot) {
01375 
01376         int nstep(1);
01377         HepPoint3D  delta_x((newPivot-pivot()).x()/double(inner_steps_),
01378                         (newPivot-pivot()).y()/double(inner_steps_),
01379                         (newPivot-pivot()).z()/double(inner_steps_));
01380         int i = 1;
01381 
01382         while (i <= inner_steps_) {
01383                 HepPoint3D nextPivot(pivot()+delta_x);
01384                 double xnp(nextPivot.x()), ynp(nextPivot.y()), znp(nextPivot.z());  
01385                 HepSymMatrix Ea_now = Ea();
01386                 HepPoint3D piv(pivot());
01387                 double xp(piv.x()), yp(piv.y()), zp(piv.z());  
01388                 double dr    = a()[0];
01389                 double phi0  = a()[1];
01390                 double kappa = a()[2];
01391                 double dz    = a()[3];
01392                 double tanl  = a()[4];
01393                 double m_rad(0);
01394                 if (numfcor_ == 1)
01395                         m_rad = radius_numf();
01396                 else
01397                         m_rad = radius();
01398                 double rdr = dr + m_rad;
01399                 double phi = fmod(phi0 + M_PI4, M_PI2);
01400                 double csf0 = cos(phi);
01401                 double snf0 = (1. - csf0) * (1. + csf0);
01402                 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
01403                 if(phi > M_PI) snf0 = - snf0;
01404 
01405                 double xc = xp + rdr * csf0;
01406                 double yc = yp + rdr * snf0;
01407                 double csf = (xc - xnp) / m_rad;
01408                 double snf = (yc - ynp) / m_rad;
01409                 double anrm = sqrt(csf * csf + snf * snf);
01410                 csf /= anrm;
01411                 snf /= anrm;
01412                 phi = atan2(snf, csf);
01413                 double phid = fmod(phi - phi0 + M_PI8, M_PI2);
01414                 if(phid > M_PI) phid = phid - M_PI2;
01415                 double drp = (xp + dr * csf0 + m_rad * (csf0 - csf) - xnp)
01416                         * csf
01417                         + (yp + dr * snf0 + m_rad * (snf0 - snf) - ynp) * snf;
01418                 double dzp = zp + dz - m_rad * tanl * phid - znp;
01419 
01420                 HepVector ap(5);
01421                 ap[0] = drp;
01422                 ap[1] = fmod(phi + M_PI4, M_PI2);
01423                 ap[2] = kappa;
01424                 ap[3] = dzp;
01425                 ap[4] = tanl;
01426 
01427                 // Modification due to non uniform magnetic field :
01428                 if (numf_ > 10) {
01429 
01430                         Hep3Vector x1(xp + dr*csf0, yp + dr*snf0, zp + dz);
01431                         double csf0p = cos(ap[1]);
01432                         double snf0p = (1. - csf0p) * (1. + csf0p);
01433                         snf0p = sqrt((snf0p > 0.) ? snf0p : 0.);
01434                         if(ap[1] > M_PI) snf0p = - snf0p;
01435 
01436                         Hep3Vector x2(xnp + drp*csf0p,
01437                                         ynp + drp*snf0p,
01438                                         znp + dzp);
01439                         Hep3Vector dist((x1 - x2).x()/100.0,
01440                                         (x1 - x2).y()/100.0,
01441                                         (x1 - x2).z()/100.0);
01442                         HepPoint3D middlebis((x1.x() + x2.x())/2,
01443                                         (x1.y() + x2.y())/2,
01444                                         (x1.z() + x2.z())/2);
01445                         HepVector3D field;
01446                         MFSvc_->fieldVector(10.*middlebis,field); 
01447                         field = 1000.*field;
01448                         Hep3Vector dB(field.x(),
01449                                         field.y(),
01450                                         (field.z()-0.1*Bznom_));
01451                         if (field.z()) {
01452                                 double akappa(fabs(kappa));
01453                                 double sign = kappa/akappa;
01454                                 HepVector dp(3);
01455                                 dp = 0.299792458 * sign * dB.cross(dist);
01456                                 HepVector dhp(3);
01457                                 dhp[0] = -akappa*(dp[0]*csf0p+dp[1]*snf0p);
01458                                 dhp[1] = kappa*akappa*(dp[0]*snf0p-dp[1]*csf0p);
01459                                 dhp[2] = dp[2]*akappa+dhp[1]*tanl/kappa;
01460                                 if (numfcor_ == 0){
01461                                         ap[1] += dhp[0];
01462                                 }
01463                                 ap[2] += dhp[1];
01464                                 ap[4] += dhp[2];
01465                         }
01466                 }
01467                 HepMatrix m_del = delApDelA(ap);
01468                 Ea_now.assign(m_del * Ea_now * m_del.T());
01469                 pivot(nextPivot);
01470                 a(ap);
01471                 Ea(Ea_now);
01472                 i++;
01473         }
01474         return newPivot;
01475 }
01476 
01477 const HepPoint3D &
01478 KalFitTrack::pivot_numf(const HepPoint3D & newPivot, double & pathl) {
01479 
01480         Helix tracktest = *(Helix*)this;
01481         tracktest.ignoreErrorMatrix();
01482         double tl  = a()[4];
01483         double th = 90.0 - 180.0*M_1_PI*atan( tl );
01484         /*
01485            int nstep(1);
01486            if (steplev_ == 1)
01487            nstep = 3;
01488            else if (steplev_ == 2 && (th > 140 || th <45))
01489            if ((pivot()-newPivot).mag()<3.)
01490            nstep = 3;
01491            else
01492            nstep = 6;
01493          */
01494         Hep3Vector delta_x((newPivot-pivot()).x()/double(outer_steps_),
01495                         (newPivot-pivot()).y()/double(outer_steps_),
01496                         (newPivot-pivot()).z()/double(outer_steps_));
01497         int i = 1;
01498 
01499         while (i <= outer_steps_) {
01500                 HepPoint3D nextPivot(pivot()+delta_x);
01501                 double xnp(nextPivot.x()), ynp(nextPivot.y()), znp(nextPivot.z());  
01502 
01503                 HepSymMatrix Ea_now = Ea();
01504                 HepPoint3D piv(pivot());
01505                 double xp(piv.x()), yp(piv.y()), zp(piv.z());  
01506 
01507                 double dr    = a()[0];
01508                 double phi0  = a()[1];
01509                 double kappa = a()[2];
01510                 double dz    = a()[3];
01511                 double tanl  = a()[4];
01512 
01513                 double m_rad(0);
01514                 m_rad = radius();
01515 
01516                 double rdr = dr + m_rad;
01517                 double phi = fmod(phi0 + M_PI4, M_PI2);
01518                 double csf0 = cos(phi);
01519                 double snf0 = (1. - csf0) * (1. + csf0);
01520                 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
01521                 if(phi > M_PI) snf0 = - snf0;
01522 
01523                 double xc = xp + rdr * csf0;
01524                 double yc = yp + rdr * snf0;
01525                 double csf = (xc - xnp) / m_rad;
01526                 double snf = (yc - ynp) / m_rad;
01527                 double anrm = sqrt(csf * csf + snf * snf);
01528                 csf /= anrm;
01529                 snf /= anrm;
01530                 phi = atan2(snf, csf);
01531                 double phid = fmod(phi - phi0 + M_PI8, M_PI2);
01532                 if(phid > M_PI) phid = phid - M_PI2;
01533                 double drp = (xp + dr * csf0 + m_rad * (csf0 - csf) - xnp)
01534                         * csf
01535                         + (yp + dr * snf0 + m_rad * (snf0 - snf) - ynp) * snf;
01536                 double dzp = zp + dz - m_rad * tanl * phid - znp;
01537 
01538                 HepVector ap(5);
01539                 ap[0] = drp;
01540                 ap[1] = fmod(phi + M_PI4, M_PI2);
01541                 ap[2] = kappa;
01542                 ap[3] = dzp;
01543                 ap[4] = tanl;
01544 
01545                 //std::cout<<" numf_: "<<numf_<<std::endl;
01546 
01547                 // Modification due to non uniform magnetic field :
01548                 if (numf_ > 10) {
01549 
01550                         Hep3Vector x1(xp + dr*csf0, yp + dr*snf0, zp + dz);
01551 
01552                         double csf0p = cos(ap[1]);
01553                         double snf0p = (1. - csf0p) * (1. + csf0p);
01554                         snf0p = sqrt((snf0p > 0.) ? snf0p : 0.);
01555                         if(ap[1] > M_PI) snf0p = - snf0p;
01556 
01557                         Hep3Vector x2(xnp + drp*csf0p,
01558                                         ynp + drp*snf0p,
01559                                         znp + dzp);
01560 
01561                         Hep3Vector dist((x1 - x2).x()/100.0,
01562                                         (x1 - x2).y()/100.0,
01563                                         (x1 - x2).z()/100.0);
01564 
01565                         HepPoint3D middlebis((x1.x() + x2.x())/2,
01566                                         (x1.y() + x2.y())/2,
01567                                         (x1.z() + x2.z())/2);
01568 
01569                         HepVector3D field;
01570                         MFSvc_->fieldVector(10.*middlebis,field);
01571                         field = 1000.*field;
01572 
01573                         //std::cout<<"B field: "<<field<<std::endl;
01574                         Hep3Vector dB(field.x(),
01575                                         field.y(),
01576                                         (field.z()-0.1*Bznom_));
01577 
01578 
01579                         //std::cout<<" dB: "<<dB<<std::endl;
01580 
01581 
01582                         if (field.z()) {
01583                                 double akappa(fabs(kappa));
01584                                 double sign = kappa/akappa;
01585                                 HepVector dp(3);
01586                                 dp = 0.299792458 * sign * dB.cross(dist);
01587 
01588                                 //std::cout<<"dp: "<<dp<<std::endl;
01589 
01590                                 HepVector dhp(3);
01591                                 dhp[0] = -akappa*(dp[0]*csf0p+dp[1]*snf0p);
01592                                 dhp[1] = kappa*akappa*(dp[0]*snf0p-dp[1]*csf0p);
01593                                 dhp[2] = dp[2]*akappa+dhp[1]*tanl/kappa;
01594 
01595 
01596                                 //std::cout<<"dhp: "<<dhp<<std::endl;
01597 
01598 
01599                                 ap[1] += dhp[0];
01600                                 ap[2] += dhp[1];
01601                                 ap[4] += dhp[2];
01602                         }
01603                 }
01604                 HepMatrix m_del = delApDelA(ap);
01605                 Ea_now.assign(m_del * Ea_now * m_del.T());
01606                 pivot(nextPivot);
01607                 a(ap);
01608                 Ea(Ea_now);
01609                 i++;
01610 
01611                 //std::cout<<" a: "<<a()<<std::endl;
01612         }
01613 
01614         // Estimation of the path length :
01615         double tanl_0(tracktest.a()[4]);
01616         double phi0_0(tracktest.a()[1]);
01617         double radius_0(tracktest.radius());
01618         tracktest.pivot(newPivot);
01619 
01620         double phi0_1 = tracktest.a()[1];
01621         if (fabs(phi0_1 - phi0_0) > M_PI) {
01622                 if (phi0_1 > phi0_0) phi0_1 -= 2 * M_PI;
01623                 else phi0_0 -= 2 * M_PI;
01624         }  
01625         if(phi0_1 == phi0_0) phi0_1 = phi0_0 + 1.e-10;
01626         pathl = fabs(radius_0 * (phi0_1 - phi0_0)
01627                         * sqrt(1 + tanl_0 * tanl_0));
01628         return newPivot;
01629 }
01630 
01631 // function to calculate the path length in the layer
01632 /*
01633    double KalFitTrack::PathL(int layer){
01634    HepPoint3D piv(pivot());
01635    double phi0  = a()[1];
01636    double kappa = a()[2];
01637    double tanl  = a()[4];
01638 
01639 #ifdef YDEBUG
01640 cout<<"helix "<<a()<<endl;
01641 #endif
01642 // Choose the local field !!
01643 double Bz(Bznom_);
01644 if (numf_ > 10){
01645 HepVector3D b;
01646 MFSvc_->fieldVector(10.*piv, b);   
01647 b = 10000.*b;
01648 Bz=b.z();
01649 }
01650 double ALPHA_loc = 10000./2.99792458/Bz;
01651 int charge = ( kappa >= 0 )? 1 : -1;
01652 double rho = ALPHA_loc/kappa;
01653 double pt = fabs( 1.0/kappa );
01654 double lambda = 180.0*M_1_PI*atan( tanl );
01655 double theta = 90.0 - lambda;
01656 theta = 2.0*M_PI*theta/360.;
01657 double phi = fmod(phi0 + M_PI4, M_PI2);
01658 double csf0 = cos(phi);
01659 double snf0 = (1. - csf0) * (1. + csf0);
01660 snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
01661 if(phi > M_PI) snf0 = - snf0;
01662 
01663 double x_c = piv.x() + ( dr() + rho )*csf0;
01664 double y_c = piv.y() + ( dr() + rho )*snf0;
01665 double z_c = piv.z() + dz();
01666 HepPoint3D ccenter(x_c, y_c, 0);
01667 double m_c_perp(ccenter.perp());
01668 Hep3Vector m_c_unit(((CLHEP::Hep3Vector)ccenter).unit());
01669 #ifdef YDEBUG
01670 cout<<"rho=..."<<rho<<endl;
01671 cout<<"ccenter "<<ccenter<<" m_c_unit "<<m_c_unit<<endl;
01672 #endif
01673 //phi resion estimation
01674 double phi_io[2];
01675 HepPoint3D IO = charge*m_c_unit;
01676 double dphi0 = fmod( IO.phi()+4*M_PI,2*M_PI ) - phi;
01677 if( dphi0 > M_PI ) dphi0 -= 2*M_PI;
01678 else if( dphi0 < -M_PI ) dphi0 += 2*M_PI;
01679 phi_io[0] = -(1+charge)*M_PI_2 - charge*dphi0;
01680 phi_io[1] = phi_io[0]+1.5*M_PI;
01681 double phi_in(0); /// for debug
01682 
01683 double m_crio[2];
01684 double m_zb, m_zf;
01685 int m_div;
01686 
01687 // retrieve Mdc  geometry information
01688 // IMdcGeomSvc* geosvc;
01689 StatusCode sc= Gaudi::svcLocator()->service("MdcGeomSvc", geosvc);
01690 if(sc==StatusCode::FAILURE) cout << "GeoSvc failing!!!!!!!SC=" << sc << endl; 
01691 
01692 MdcGeomSvc* geomsvc = dynamic_cast<MdcGeomSvc*>(iGeomSvc_);
01693 if(!geomsvc){
01694 std::cout<<"ERROR OCCUR when dynamic_cast in KalFitTrack.cxx !!"<<std::endl;
01695 }
01696 double rcsiz1 = geomsvc->Layer(layer)->RCSiz1();
01697 double rcsiz2 = geomsvc->Layer(layer)->RCSiz2();
01698 double rlay = geomsvc->Layer(layer)->Radius();
01699 m_zb = 0.5*(geomsvc->Layer(layer)->Length());
01700 m_zf = -0.5*(geomsvc->Layer(layer)->Length());
01701 
01702 m_crio[0] = rlay - rcsiz1;
01703 m_crio[1] = rlay + rcsiz2;
01704 //conversion of the units(mm=>cm)
01705 m_crio[0] = 0.1*m_crio[0];
01706 m_crio[1] = 0.1*m_crio[1];
01707 m_zb = 0.1*m_zb;
01708 m_zf = 0.1*m_zf;
01709 
01710 int sign = 1;  ///assumed the first half circle
01711 int epflag[2];
01712 Hep3Vector iocand;
01713 Hep3Vector cell_IO[2];
01714 double dphi;
01715 for( int i = 0; i < 2; i++ ){
01716         double cos_alpha = m_c_perp*m_c_perp + m_crio[i]*m_crio[i] - rho*rho;
01717         cos_alpha = 0.5*cos_alpha/( m_c_perp*m_crio[i] );
01718         double Calpha =  acos( cos_alpha );
01719         epflag[i] = 0;
01720         iocand = m_c_unit;
01721         iocand.rotateZ( charge*sign*Calpha );
01722         iocand *= m_crio[i];
01723         double xx = iocand.x() - x_c;
01724         double yy = iocand.y() - y_c;
01725         dphi = atan2(yy, xx) - phi0 - M_PI_2*(1+charge);
01726         dphi = fmod( dphi + 8.0*M_PI, 2*M_PI );
01727 
01728         if( dphi < phi_io[0] ){
01729                 dphi += 2*M_PI;
01730         }
01731         else if( phi_io[1] < dphi ){
01732                 dphi -= 2*M_PI;
01733         }
01734 
01736 
01737         Hep3Vector zvector( 0., 0., z_c-rho*dphi*tanl);
01738 
01739         double xcio, ycio, phip;
01741         double delrin = 2.0;
01742         if( m_zf-delrin > zvector.z() ){
01743                 phip = z_c - m_zb + delrin;
01744                 phip = phip/( rho*tanl );
01745                 phip = phip + phi0;
01746                 xcio = x_c - rho*cos( phip );
01747                 ycio = y_c - rho*sin( phip );
01748                 cell_IO[i].setX( xcio );
01749                 cell_IO[i].setY( ycio );
01750                 cell_IO[i].setZ( m_zb+delrin );
01751                 epflag[i] = 1;
01752         }
01753         else if( m_zb+delrin < zvector.z() ){
01754                 phip = z_c - m_zf-delrin;
01755                 phip = phip/( rho*tanl );
01756                 phip = phip + phi0;
01757                 xcio = x_c - rho*cos( phip );
01758                 ycio = y_c - rho*sin( phip );
01759                 cell_IO[i].setX( xcio );
01760                 cell_IO[i].setY( ycio );
01761                 cell_IO[i].setZ( m_zf-delrin );
01762                 epflag[i] = 1;
01763         }
01764         else{
01765                 cell_IO[i] = iocand;
01766                 cell_IO[i] += zvector;
01767         }
01769         if( i == 0 ) phi_in = dphi;
01770 }
01771 // path length estimation
01772 // phi calculation
01773 Hep3Vector cl = cell_IO[1] - cell_IO[0];
01774 #ifdef YDEBUG
01775 cout<<"cell_IO[0] "<<cell_IO[0]<<" cell_IO[1] "<<cell_IO[1]<<" cl "<<cl<<endl; 
01776 #endif
01777 
01778 double ch_theta;
01779 double ch_dphi;
01780 double ch_ltrk = 0;
01781 double ch_ltrk_rp = 0;
01782 ch_dphi = cl.perp()*0.5/(ALPHA_loc*pt);
01783 ch_dphi = 2.0 * asin( ch_dphi );
01784 ch_ltrk_rp = ch_dphi*ALPHA_loc*pt;
01785 #ifdef YDEBUG
01786 cout<<"ch_dphi "<<ch_dphi<<" ch_ltrk_rp "<<ch_ltrk_rp<<" cl.z "<<cl.z()<<endl; 
01787 #endif
01788 ch_ltrk = sqrt( ch_ltrk_rp*ch_ltrk_rp + cl.z()*cl.z() );
01789 ch_theta = cl.theta();
01790 
01791 if( ch_theta < theta*0.85 || 1.15*theta < ch_theta)
01792         ch_ltrk *= -1; /// miss calculation
01793 
01794 if( epflag[0] == 1 || epflag [1] == 1 )
01795         ch_ltrk *= -1;  /// invalid resion for dE/dx or outside of Mdc
01796 
01797         mom_[layer] = momentum( phi_in );
01798         PathL_[layer] = ch_ltrk;
01799 #ifdef YDEBUG
01800         cout<<"ch_ltrk "<<ch_ltrk<<endl; 
01801 #endif
01802         return ch_ltrk;
01803         }
01804 */
01805 
01806 
01807 void KalFitTrack::update_bit(int i){
01808         int j(0);
01809         if (i < 31){
01810                 j = (int) pow(2.,i);
01811                 if (!(pat1_ & j))
01812                         pat1_ = pat1_ | j;
01813         } else if (i < 50) {
01814                 j = (int) pow(2.,(i-31));
01815                 if (!(pat2_ & j))
01816                         pat2_ = pat2_ | j;
01817         }
01818 }
01819 
01820 int KalFitTrack::nmass(void){  return NMASS;}
01821 double KalFitTrack::mass(int i){  return MASS[i];}
01822 void KalFitTrack::chgmass(int i){
01823         mass_=MASS[i];
01824         l_mass_=i;
01825 }
01826 
01827 void KalFitTrack::lead(int i){ lead_ = i;}
01828 int KalFitTrack::lead(void){ return lead_;}
01829 void KalFitTrack::back(int i){ back_ = i;}
01830 int KalFitTrack::back(void){ return back_;}
01831 void KalFitTrack::resol(int i){ resolflag_ = i;}
01832 int KalFitTrack::resol(void){ return resolflag_;}
01833 void KalFitTrack::numf(int i){ numf_ = i;}
01834 int KalFitTrack::numf(void){ return numf_;}
01835 void KalFitTrack::LR(int x){ LR_ = x;}
01836 void KalFitTrack::HitsMdc(vector<KalFitHitMdc>& lh){ HitsMdc_ = lh;}
01837 void KalFitTrack::appendHitsMdc(KalFitHitMdc h){ HitsMdc_.push_back(h);}
01838 
01839 /* 
01840    This member function is in charge of the forward filter,
01841    for the tof correction of the drift distance in the case of cosmic rays
01842    if way=1, it's a down track means same as track in collision data,
01843    if way=-1, it's a up track !
01844  */
01845 
01846 double KalFitTrack::update_hits(KalFitHitMdc & HitMdc, int inext, Hep3Vector& meas, int way, double& dchi2out, double& dtrack,double& dtracknew, double& dtdc, int csmflag) {
01847 
01848         double lr = HitMdc.LR();
01849         //std::cout<<" in update_hits: lr= " << lr <<std::endl;
01850         // For taking the propagation delay into account :
01851         const KalFitWire& Wire = HitMdc.wire();
01852         int wire_ID = Wire.geoID();
01853         int layerid = HitMdc.wire().layer().layerId();
01854         //std::cout<<" when in layer: "<<layerid<<std::endl;
01855         double entrangle = HitMdc.rechitptr()->getEntra();
01856 
01857         if (wire_ID<0 || wire_ID>6796){  //bes
01858                 std::cout << " KalFitTrack: wire_ID problem : " << wire_ID 
01859                         << std::endl;
01860                 return 0;
01861         } 
01862         int flag_ster = Wire.stereo();
01863         double x[3] ={pivot().x(), pivot().y(), pivot().z()};
01864         double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
01865         double tofest(0);
01866         double phi = fmod(phi0() + M_PI4, M_PI2);
01867         double csf0 = cos(phi);
01868         double snf0 = (1. - csf0) * (1. + csf0);
01869         snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
01870         if(phi > M_PI) snf0 = - snf0;
01871         if (Tof_correc_){
01872                 double t = tanl();
01873                 double dphi(0);
01874 
01875                 Helix work = *(Helix*)this;
01876                 work.ignoreErrorMatrix();
01877                 double phi_ip(0);
01878                 Hep3Vector ip(0, 0, 0);
01879                 work.pivot(ip);
01880                 phi_ip = work.phi0();
01881                 if (fabs(phi - phi_ip) > M_PI) {
01882                         if (phi > phi_ip) phi -= 2 * M_PI;
01883                         else phi_ip -= 2 * M_PI;
01884                 }
01885                 dphi = phi - phi_ip;
01886                 double l = fabs(radius() * dphi * sqrt(1 + t * t));
01887                 double pmag( sqrt( 1.0 + t*t ) / kappa());
01888                 double mass_over_p( mass_ / pmag );
01889                 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
01890                 tofest = l / ( 29.9792458 * beta );
01891                 if(csmflag==1 && HitMdc.wire().y()>0.)  tofest= -1. * tofest;
01892         }
01893 
01894         const HepSymMatrix& ea = Ea();
01895         const HepVector& v_a = a();
01896         double dchi2R(99999999), dchi2L(99999999);
01897 
01898         HepVector v_H(5, 0);
01899         v_H[0] =  -csf0 * meas.x() - snf0 * meas.y();
01900         v_H[3] =  -meas.z();
01901         HepMatrix v_HT = v_H.T();
01902 
01903         double estim = (v_HT * v_a)[0];
01904         dtrack = estim;
01905         // std::cout<<" in update_hits estim is  "<<estim<<std::endl;
01906         HepVector ea_v_H = ea * v_H;
01907         HepMatrix ea_v_HT = (ea_v_H).T();
01908         HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
01909 
01910         HepSymMatrix eaNewL(5), eaNewR(5);
01911         HepVector aNewL(5), aNewR(5);
01912 
01913         double drifttime = getDriftTime(HitMdc , tofest);
01914         double ddl = getDriftDist(HitMdc, drifttime, 0 );
01915         double ddr = getDriftDist(HitMdc, drifttime, 1 );
01916         double erddl = getSigma( HitMdc, a()[4], 0, ddl);
01917         double erddr = getSigma( HitMdc, a()[4], 1, ddr);
01918 
01919         if(debug_ == 4) {
01920                 std::cout<<"drifttime in update_hits() for ananlysis is  "<<drifttime<<std::endl;
01921                 std::cout<<"ddl in update_hits() for ananlysis is  "<<ddl<<std::endl;
01922                 std::cout<<"ddr in update_hits() for ananlysis is  "<<ddr<<std::endl;
01923                 std::cout<<"erddl in update_hits() for ananlysis is  "<<erddl<<std::endl;
01924                 std::cout<<"erddr in update_hits() for ananlysis is  "<<erddr<<std::endl;
01925         }
01926 
01927         //the following 3 line : tempory
01928         double dmeas2[2] = { 0.1*ddl, 0.1*ddr };  // millimeter to centimeter
01929         double er_dmeas2[2] = {0.,0.};
01930         if(resolflag_ == 1) { 
01931                 er_dmeas2[0] = 0.1*erddl;
01932                 er_dmeas2[1] = 0.1*erddr;
01933         } 
01934         else if(resolflag_ == 0) {
01935                 //   int layid = HitMdc.wire().layer().layerId();
01936                 //   double sigma = getSigma(layid, dd);
01937                 //   er_dmeas2[0] = er_dmeas2[1] = sigma;
01938         }
01939 
01940         // Left hypothesis :
01941         if ((LR_==0 && lr != 1.0) || 
01942                         (LR_==1 && lr == -1.0)) {
01943                 double er_dmeasL, dmeasL;
01944                 if(Tof_correc_) {
01945                         dmeasL = (-1.0)*fabs(dmeas2[0]);
01946                         er_dmeasL = er_dmeas2[0];
01947                 } else {
01948                         dmeasL = (-1.0)*fabs(HitMdc.dist()[0]);
01949                         er_dmeasL = HitMdc.erdist()[0];
01950                 }
01951                 dtdc = dmeasL;
01952                 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
01953                 eaNewL.assign(ea - ea_v_H * AL * ea_v_HT);
01954                 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
01955                 if(debug_ == 4){
01956                         std::cout<<" ea_v_H * AL * ea_v_HT is: "<<ea_v_H * AL * ea_v_HT<<std::endl;
01957                         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;
01958                         std::cout<<" AL is: "<<AL<<" (v_H_T_ea_v_H)[0] * AL is: "<<(v_H_T_ea_v_H)[0]*AL<<std::endl;
01959                 }
01960 
01961                 if(0. == RkL) RkL = 1.e-4;
01962 
01963                 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
01964                 aNewL = v_a + diffL;
01965                 double sigL = dmeasL -(v_HT * aNewL)[0];
01966                 dtracknew = (v_HT * aNewL)[0];
01967                 dchi2L = (sigL * sigL)/(RkL * er_dmeasL*er_dmeasL);
01968 
01969                 if(debug_ == 4){
01970                         std::cout<<" fwd_filter : in left hypothesis dchi2L is "<<dchi2L<<std::endl;
01971                 }
01972         }
01973 
01974         if ((LR_==0 && lr != -1.0) ||
01975                         (LR_==1 && lr == 1.0)) {
01976                 double er_dmeasR, dmeasR;
01977                 if(Tof_correc_) {
01978                         dmeasR = dmeas2[1];
01979                         er_dmeasR = er_dmeas2[1];
01980                 } else {
01981                         dmeasR = fabs(HitMdc.dist()[1]);
01982                         er_dmeasR = HitMdc.erdist()[1];
01983                 }
01984                 dtdc = dmeasR;
01985                 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
01986                 eaNewR.assign(ea - ea_v_H * AR * ea_v_HT);
01987                 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
01988 
01989                 if(debug_ == 4){
01990                         std::cout<<" ea_v_H * AR * ea_v_HT is: "<<ea_v_H * AR * ea_v_HT<<std::endl;
01991                         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;
01992                         std::cout<<" AR is: "<<AR<<" (v_H_T_ea_v_H)[0] * AR is: "<<(v_H_T_ea_v_H)[0]*AR<<std::endl;
01993                 }
01994 
01995                 if(0. == RkR) RkR = 1.e-4;
01996                 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
01997                 aNewR = v_a + diffR;
01998                 double sigR = dmeasR -(v_HT * aNewR)[0];
01999                 dtracknew = (v_HT * aNewR)[0];
02000                 dchi2R = (sigR*sigR)/(RkR * er_dmeasR*er_dmeasR);
02001                 if(debug_ == 4){
02002                         std::cout<<" fwd_filter : in right hypothesis dchi2R is "<<dchi2R<<std::endl;
02003                 }
02004         }
02005         // Update Kalman result :
02006         double dchi2_loc(0);
02007         if (fabs(dchi2R-dchi2L)<10. && inext>0) {
02008                 KalFitHitMdc & HitMdc_next = HitsMdc_[inext];
02009                 Helix H_fromR(pivot(), aNewR, eaNewR);
02010                 double chi2_fromR(chi2_next(H_fromR, HitMdc_next, csmflag));
02011                 Helix H_fromL(pivot(), aNewL, eaNewL);
02012                 double chi2_fromL(chi2_next(H_fromL, HitMdc_next, csmflag));
02013 #ifdef YDEBUG
02014                 std::cout << "   chi2_fromL = " << chi2_fromL 
02015                         << ", chi2_fromR = " << chi2_fromR << std::endl;
02016 #endif
02017                 if (fabs(chi2_fromR-chi2_fromL)<10.){
02018                         int inext2(-1);
02019                         if (inext+1<HitsMdc_.size())
02020                                 for( int k=inext+1 ; k < HitsMdc_.size(); k++ )
02021                                         if (!(HitsMdc_[k].chi2()<0)) {
02022                                                 inext2 = k;
02023                                                 break;
02024                                         }
02025 
02026                         if (inext2 != -1){
02027                                 KalFitHitMdc & HitMdc_next2 = HitsMdc_[inext2];
02028                                 double chi2_fromR2(chi2_next(H_fromR, HitMdc_next2, csmflag));
02029                                 double chi2_fromL2(chi2_next(H_fromL, HitMdc_next2, csmflag));
02030 #ifdef YDEBUG
02031                                 std::cout << "   chi2_fromL2 = " << chi2_fromL2 
02032                                         << ", chi2_fromR2 = " << chi2_fromR2 << std::endl;
02033 #endif
02034                                 if (fabs(dchi2R+chi2_fromR+chi2_fromR2-(dchi2L+chi2_fromL+chi2_fromL2))<2) {
02035                                         if (chi2_fromR2<chi2_fromL2)
02036                                                 dchi2L = DBL_MAX;
02037                                         else 
02038                                                 dchi2R = DBL_MAX;
02039                                 }
02040                         } 
02041                 }
02042 
02043                 if (!(dchi2L == DBL_MAX && dchi2R == DBL_MAX)) {
02044                         if (dchi2R+chi2_fromR < dchi2L+chi2_fromL){
02045                                 dchi2L = DBL_MAX;
02046 #ifdef YDEBUG
02047                                 std::cout << " choose right..." << std::endl;
02048 #endif
02049                         } else {
02050                                 dchi2R = DBL_MAX;
02051 #ifdef YDEBUG
02052                                 std::cout << " choose left..." << std::endl;
02053 #endif
02054                         }
02055                 }
02056         }
02057         if ((0 < dchi2R && dchi2R < dchi2cutf_anal[layerid]) ||
02058                         (0 < dchi2L && dchi2L < dchi2cutf_anal[layerid])) {
02059 
02060                 if (((LR_==0 && dchi2R < dchi2L && lr != -1) || 
02061                                         (LR_==1 && lr == 1)) && 
02062                                 fabs(aNewR[2]-a()[2]) < 1000. && aNewR[2]) {
02063                         if (nchits_ < (unsigned) nmdc_hit2_ || dchi2R < dchi2cutf_anal[layerid]){
02064                                 nchits_++;
02065                                 if (flag_ster) nster_++;
02066                                 Ea(eaNewR);
02067                                 a(aNewR);
02068                                 chiSq_ += dchi2R;
02069                                 dchi2_loc = dchi2R;
02070                                 if (l_mass_ == lead_){
02071                                         HitMdc.LR(1);
02072                                 }        
02073                                 update_bit(HitMdc.wire().layer().layerId());
02074                         }
02075                 } else if (((LR_==0 && dchi2L <= dchi2R && lr != 1) || 
02076                                         (LR_==1 && lr == -1)) && 
02077                                 fabs(aNewL[2]-a()[2]) < 1000. && aNewL[2]){
02078                         if (nchits_ < (unsigned) nmdc_hit2_ || dchi2L < dchi2cutf_anal[layerid]){
02079                                 nchits_++;
02080                                 if (flag_ster) nster_++;
02081                                 Ea(eaNewL);
02082                                 a(aNewL);
02083                                 chiSq_ += dchi2L;
02084                                 dchi2_loc = dchi2L;
02085                                 if (l_mass_ == lead_){
02086                                         HitMdc.LR(-1);
02087                                 }
02088                                 update_bit(HitMdc.wire().layer().layerId());
02089                         }
02090                 }
02091         }
02092         if (dchi2_loc > dchi2_max_) {
02093                 dchi2_max_ = dchi2_loc ;
02094                 r_max_ = pivot().perp();
02095         }
02096         dchi2out = dchi2_loc;
02097         //if(dchi2out == 0) {
02098         //   dchi2out = ( (dchi2L < dchi2R ) ? dchi2L : dchi2R ) ;
02099         // }
02100         return chiSq_;
02101 }
02102 
02103 
02104 double KalFitTrack::update_hits_csmalign(KalFitHelixSeg& HelixSeg, int inext, Hep3Vector& meas,int way, double& dchi2out, int csmflag ) {
02105 
02106 
02107         HepPoint3D ip(0.,0.,0.);
02108 
02109         KalFitHitMdc* HitMdc = HelixSeg.HitMdc();
02110         double lr = HitMdc->LR();
02111         int layerid = (*HitMdc).wire().layer().layerId();
02112         // cout<<"layerid: "<<layerid<<endl;
02113         double entrangle = HitMdc->rechitptr()->getEntra();
02114 
02115         if(debug_ == 4) {
02116                 std::cout<<"in update_hits(kalFitHelixSeg,...) : the layerid ="<<layerid<<std::endl; 
02117                 std::cout<<" update_hits: lr= " << lr <<std::endl;
02118         }
02119         // For taking the propagation delay into account :
02120         const KalFitWire& Wire = HitMdc->wire();
02121         int wire_ID = Wire.geoID();
02122 
02123         if (wire_ID<0 || wire_ID>6796){  //bes
02124                 std::cout << " KalFitTrack: wire_ID problem : " << wire_ID 
02125                         << std::endl;
02126                 return 0;
02127         } 
02128         int flag_ster = Wire.stereo();
02129         double x[3] ={pivot().x(), pivot().y(), pivot().z()};
02130         double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
02131         double tofest(0);
02132         double phi = fmod(phi0() + M_PI4, M_PI2);
02133         double csf0 = cos(phi);
02134         double snf0 = (1. - csf0) * (1. + csf0);
02135         snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
02136         if(phi > M_PI) snf0 = - snf0;
02137         if (Tof_correc_){
02138                 double t = tanl();
02139                 double dphi(0);
02140 
02141                 Helix work = *(Helix*)this;
02142                 work.ignoreErrorMatrix();
02143                 double phi_ip(0);
02144                 Hep3Vector ip(0, 0, 0);
02145                 work.pivot(ip);
02146                 phi_ip = work.phi0();
02147                 if (fabs(phi - phi_ip) > M_PI) {
02148                         if (phi > phi_ip) phi -= 2 * M_PI;
02149                         else phi_ip -= 2 * M_PI;
02150                 }
02151                 dphi = phi - phi_ip;
02152 
02153                 double l = fabs(radius() * dphi * sqrt(1 + t * t));
02154                 double pmag( sqrt( 1.0 + t*t ) / kappa());
02155                 double mass_over_p( mass_ / pmag );
02156                 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
02157                 tofest = l / ( 29.9792458 * beta );
02158                 if(csmflag==1 && (*HitMdc).wire().y()>0.)  tofest= -1. * tofest;
02159         }
02160 
02161         const HepSymMatrix& ea = Ea();
02162         HelixSeg.Ea_pre_fwd(ea);
02163         HelixSeg.Ea_filt_fwd(ea);
02164 
02165 
02166         const HepVector& v_a = a();
02167         HelixSeg.a_pre_fwd(v_a);
02168         HelixSeg.a_filt_fwd(v_a);
02169 
02170         /*
02171 
02172            HepPoint3D pivot_work = pivot();
02173            HepVector a_work = a();
02174            HepSymMatrix ea_work = Ea();
02175            KalFitTrack helix_work(pivot_work, a_work, ea_work, 0, 0, 0);
02176            helix_work.pivot(ip);
02177 
02178            HepVector a_temp = helix_work.a();
02179            HepSymMatrix ea_temp = helix_work.Ea();
02180 
02181            HelixSeg.Ea_pre_fwd(ea_temp);        
02182            HelixSeg.a_pre_fwd(a_temp);
02183 
02184         // My God, for the protection purpose 
02185         HelixSeg.Ea_filt_fwd(ea_temp);
02186         HelixSeg.a_filt_fwd(a_temp);
02187 
02188          */
02189 
02190         double dchi2R(99999999), dchi2L(99999999);
02191 
02192         HepVector v_H(5, 0);
02193         v_H[0] =  -csf0 * meas.x() - snf0 * meas.y();
02194         v_H[3] =  -meas.z();
02195         HepMatrix v_HT = v_H.T();
02196 
02197         double estim = (v_HT * v_a)[0];
02198         HepVector ea_v_H = ea * v_H;
02199         HepMatrix ea_v_HT = (ea_v_H).T();
02200         HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
02201 
02202         HepSymMatrix eaNewL(5), eaNewR(5);
02203         HepVector aNewL(5), aNewR(5);
02204 #ifdef YDEBUG
02205         cout<<"phi  "<<phi<<"  snf0 "<<snf0<<"  csf0  "<<csf0<<endl
02206                 <<"  meas:  "<<meas <<endl;   
02207         cout<<"the related matrix:"<<endl;
02208         cout<<"v_H  "<<v_H<<endl;
02209         cout<<"v_a  "<<v_a<<endl;
02210         cout<<"ea  "<<ea<<endl;
02211         cout<<"v_H_T_ea_v_H  "<<v_H_T_ea_v_H<<endl;
02212         cout<<"LR_= "<< LR_ << "lr= " << lr <<endl;
02213 #endif
02214 
02215         double time = (*HitMdc).tdc();
02216         //  if (Tof_correc_)
02217         //  time = time - way*tofest;
02218 
02219         //  double dd = 1.0e-4 * 40.0 * time;
02220         //the following 3 line : tempory
02221 
02222         double drifttime = getDriftTime(*HitMdc , tofest);
02223         double ddl = getDriftDist(*HitMdc, drifttime, 0 );
02224         double ddr = getDriftDist(*HitMdc, drifttime, 1 );
02225         double erddl = getSigma( *HitMdc, a()[4], 0, ddl);
02226         double erddr = getSigma( *HitMdc, a()[4], 1, ddr);
02227 
02228         if(debug_ == 4){
02229                 std::cout<<"the pure drift time is "<<drifttime<<std::endl;
02230                 std::cout<<"the drift dist left hypothesis is "<<ddl<<std::endl;
02231                 std::cout<<"the drift dist right hypothesis is "<<ddr<<std::endl;
02232                 std::cout<<"the error sigma left hypothesis is "<<erddl<<std::endl;
02233                 std::cout<<"the error sigma right hypothesis is "<<erddr<<std::endl;
02234         }
02235         double dmeas2[2] = { 0.1*ddl , 0.1*ddr }; //millimeter to centimeter
02236         double er_dmeas2[2] = {0. , 0.} ;
02237 
02238         if(resolflag_ == 1) { 
02239                 er_dmeas2[0] = 0.1*erddl;
02240                 er_dmeas2[1] = 0.1*erddr;
02241         } 
02242         else if(resolflag_ == 0) {
02243                 //   int layid = (*HitMdc).wire().layer().layerId();
02244                 //   double sigma = getSigma(layid, dd);
02245                 //   er_dmeas2[0] = er_dmeas2[1] = sigma;
02246         }
02247 
02248         // Left hypothesis :
02249         if ((LR_==0 && lr != 1.0) || 
02250                         (LR_==1 && lr == -1.0)) {
02251 
02252                 double er_dmeasL, dmeasL;
02253                 if(Tof_correc_) {
02254                         dmeasL = (-1.0)*fabs(dmeas2[0]);
02255                         er_dmeasL = er_dmeas2[0];
02256                 } else {
02257                         dmeasL = (-1.0)*fabs((*HitMdc).dist()[0]);
02258                         er_dmeasL = (*HitMdc).erdist()[0];
02259                 }
02260 
02261                 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
02262                 eaNewL.assign(ea - ea_v_H * AL * ea_v_HT);
02263                 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
02264                 if(0. == RkL) RkL = 1.e-4;
02265 
02266                 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
02267 
02268                 aNewL = v_a + diffL;
02269                 double sigL = dmeasL -(v_HT * aNewL)[0];
02270                 dchi2L = (sigL * sigL) /  (RkL * er_dmeasL*er_dmeasL);
02271         }
02272 
02273         if ((LR_==0 && lr != -1.0) ||
02274                         (LR_==1 && lr == 1.0)) {
02275 
02276                 double er_dmeasR, dmeasR;
02277                 if(Tof_correc_) {
02278                         dmeasR = dmeas2[1];
02279                         er_dmeasR = er_dmeas2[1];
02280                 } else {
02281                         dmeasR = fabs((*HitMdc).dist()[1]);
02282                         er_dmeasR = (*HitMdc).erdist()[1];
02283                 }
02284 
02285                 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
02286                 eaNewR.assign(ea - ea_v_H * AR * ea_v_HT);
02287                 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
02288                 if(0. == RkR) RkR = 1.e-4;
02289 
02290                 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
02291 
02292                 aNewR = v_a + diffR;
02293                 double sigR = dmeasR -(v_HT * aNewR)[0];
02294                 dchi2R = (sigR*sigR) /  (RkR * er_dmeasR*er_dmeasR);
02295         }
02296 
02297         // Update Kalman result :
02298         double dchi2_loc(0);
02299 
02300 #ifdef YDEBUG
02301         cout<<" track::update_hits......"<<endl;
02302         std::cout << "   dchi2R = " << dchi2R << ", dchi2L = " << dchi2L 
02303                 << "   estimate =  "<<estim<< std::endl;
02304         std::cout << "   dmeasR = " << dmeas2[1] 
02305                 << ", dmeasL = " << (-1.0)*fabs(dmeas2[0]) << ", check HitMdc.ddl = " 
02306                 << (*HitMdc).dist()[0] << std::endl;
02307         std::cout<<"dchi2L : "<<dchi2L<<" ,dchi2R:  "<<dchi2R<<std::endl; 
02308 #endif
02309 
02310 
02311         if (fabs(dchi2R-dchi2L)<10. && inext>0) {
02312 
02313                 KalFitHitMdc & HitMdc_next = HitsMdc_[inext];
02314 
02315                 Helix H_fromR(pivot(), aNewR, eaNewR);
02316                 double chi2_fromR(chi2_next(H_fromR, HitMdc_next,csmflag));
02317                 //double chi2_fromR(chi2_next(H_fromR, HitMdc_next));
02318 
02319                 Helix H_fromL(pivot(), aNewL, eaNewL);
02320                 double chi2_fromL(chi2_next(H_fromL, HitMdc_next,csmflag));
02321                 //double chi2_fromL(chi2_next(H_fromL, HitMdc_next));
02322 #ifdef YDEBUG
02323                 std::cout << "   chi2_fromL = " << chi2_fromL 
02324                         << ", chi2_fromR = " << chi2_fromR << std::endl;
02325 #endif
02326                 if (fabs(chi2_fromR-chi2_fromL)<10.){
02327                         int inext2(-1);
02328                         if (inext+1<HitsMdc_.size())
02329                                 for( int k=inext+1 ; k < HitsMdc_.size(); k++ )
02330                                         if (!(HitsMdc_[k].chi2()<0)) {
02331                                                 inext2 = k;
02332                                                 break;
02333                                         }
02334 
02335                         if (inext2 != -1){
02336                                 KalFitHitMdc & HitMdc_next2 = HitsMdc_[inext2];
02337                                 //      double chi2_fromR2(chi2_next(H_fromR, HitMdc_next2));
02338                                 //      double chi2_fromL2(chi2_next(H_fromL, HitMdc_next2));
02339                                 double chi2_fromR2(chi2_next(H_fromR, HitMdc_next2, csmflag));
02340                                 double chi2_fromL2(chi2_next(H_fromL, HitMdc_next2, csmflag));
02341 #ifdef YDEBUG
02342                                 std::cout << "   chi2_fromL2 = " << chi2_fromL2 
02343                                         << ", chi2_fromR2 = " << chi2_fromR2 << std::endl;
02344 #endif
02345                                 if (fabs(dchi2R+chi2_fromR+chi2_fromR2-(dchi2L+chi2_fromL+chi2_fromL2))<2) {
02346                                         if (chi2_fromR2<chi2_fromL2)
02347                                                 dchi2L = DBL_MAX;
02348                                         else 
02349                                                 dchi2R = DBL_MAX;
02350                                 }
02351                         } 
02352                 }
02353 
02354                 if (!(dchi2L == DBL_MAX && dchi2R == DBL_MAX)) {
02355                         if (dchi2R+chi2_fromR < dchi2L+chi2_fromL){
02356                                 dchi2L = DBL_MAX;
02357 #ifdef YDEBUG
02358                                 std::cout << " choose right..." << std::endl;
02359 #endif
02360                         } else {
02361                                 dchi2R = DBL_MAX;
02362 #ifdef YDEBUG
02363                                 std::cout << " choose left..." << std::endl;
02364 #endif
02365                         }
02366                 }
02367         }
02368 
02369         if ((0 < dchi2R && dchi2R < dchi2cutf_calib[layerid]) ||
02370                         (0 < dchi2L && dchi2L < dchi2cutf_calib[layerid])) {
02371 
02372                 if (((LR_==0 && dchi2R < dchi2L && lr != -1) || 
02373                                         (LR_==1 && lr == 1)) && 
02374                                 fabs(aNewR[2]-a()[2]) < 1000. && aNewR[2]) {
02375                         if (nchits_ < (unsigned) nmdc_hit2_ || dchi2R < dchi2cutf_calib[layerid]){
02376                                 nchits_++;
02377                                 if (flag_ster) nster_++;
02378                                 if(layerid>19){  
02379                                         Ea(eaNewR);
02380                                         HelixSeg.Ea_filt_fwd(eaNewR);
02381                                         a(aNewR);
02382                                         HelixSeg.a_filt_fwd(aNewR);
02383                                 }         
02384                                 /*
02385                                    Ea(eaNewR);
02386                                    a(aNewR);
02387 
02388                                    KalFitTrack helixR(pivot_work, aNewR, eaNewR, 0, 0, 0);
02389                                    helixR.pivot(ip);
02390 
02391                                    a_temp = helixR.a();
02392                                    ea_temp = helixR.Ea();
02393 
02394                                    HelixSeg.Ea_filt_fwd(ea_temp);
02395                                    HelixSeg.a_filt_fwd(a_temp);
02396                                 //HelixSeg.filt_include(1);
02397 
02398                                  */
02399 
02400                                 chiSq_ += dchi2R;
02401                                 dchi2_loc = dchi2R;
02402                                 if (l_mass_ == lead_){
02403                                         (*HitMdc).LR(1);
02404                                         HelixSeg.LR(1);
02405                                 }        
02406                                 update_bit((*HitMdc).wire().layer().layerId());
02407                         }
02408                 } else if (((LR_==0 && dchi2L <= dchi2R && lr != 1) || 
02409                                         (LR_==1 && lr == -1)) && 
02410                                 fabs(aNewL[2]-a()[2]) < 1000. && aNewL[2]){
02411                         if (nchits_ < (unsigned) nmdc_hit2_ || dchi2L < dchi2cutf_calib[layerid]){
02412                                 nchits_++;
02413                                 if (flag_ster) nster_++;
02414                                 if(layerid>19){
02415                                         Ea(eaNewL);
02416                                         HelixSeg.Ea_filt_fwd(eaNewL);
02417                                         a(aNewL);
02418                                         HelixSeg.a_filt_fwd(aNewL);
02419                                 }
02420 
02421                                 /* 
02422                                    Ea(eaNewL);
02423                                    a(aNewL);
02424 
02425                                    KalFitTrack helixL(pivot_work, aNewL, eaNewL, 0, 0, 0);
02426                                    helixL.pivot(ip);
02427                                    a_temp = helixL.a();
02428                                    ea_temp = helixL.Ea();
02429                                    HelixSeg.Ea_filt_fwd(ea_temp);
02430                                    HelixSeg.a_filt_fwd(a_temp);
02431                                 //HelixSeg.filt_include(1);
02432 
02433                                  */
02434 
02435                                 chiSq_ += dchi2L;
02436                                 dchi2_loc = dchi2L;
02437                                 if (l_mass_ == lead_){
02438                                         (*HitMdc).LR(-1);
02439                                         HelixSeg.LR(-1);
02440                                 }
02441                                 update_bit((*HitMdc).wire().layer().layerId());
02442                         }
02443                 }
02444         }
02445 
02446         if (dchi2_loc > dchi2_max_) {
02447                 dchi2_max_ = dchi2_loc ;
02448                 r_max_ = pivot().perp();
02449         }
02450         dchi2out = dchi2_loc;
02451         //  if(dchi2out == 0) {
02452         //     dchi2out = ( (dchi2L < dchi2R ) ? dchi2L : dchi2R ) ;
02453         //  }
02454         return chiSq_;
02455 }
02456 
02457 
02458 double KalFitTrack::update_hits(KalFitHelixSeg& HelixSeg, int inext, Hep3Vector& meas,int way, double& dchi2out, int csmflag) {
02459 
02460 
02461         HepPoint3D ip(0.,0.,0.);
02462 
02463         KalFitHitMdc* HitMdc = HelixSeg.HitMdc();
02464         double lr = HitMdc->LR();
02465         int layerid = (*HitMdc).wire().layer().layerId();
02466         double entrangle = HitMdc->rechitptr()->getEntra();
02467 
02468         if(debug_ == 4) {
02469                 std::cout<<"in update_hits(kalFitHelixSeg,...) : the layerid ="<<layerid<<std::endl; 
02470                 std::cout<<" update_hits: lr= " << lr <<std::endl;
02471         }
02472         // For taking the propagation delay into account :
02473         const KalFitWire& Wire = HitMdc->wire();
02474         int wire_ID = Wire.geoID();
02475 
02476         if (wire_ID<0 || wire_ID>6796){  //bes
02477                 std::cout << " KalFitTrack: wire_ID problem : " << wire_ID 
02478                         << std::endl;
02479                 return 0;
02480         } 
02481         int flag_ster = Wire.stereo();
02482         double x[3] ={pivot().x(), pivot().y(), pivot().z()};
02483         double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
02484         double tofest(0);
02485         double phi = fmod(phi0() + M_PI4, M_PI2);
02486         double csf0 = cos(phi);
02487         double snf0 = (1. - csf0) * (1. + csf0);
02488         snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
02489         if(phi > M_PI) snf0 = - snf0;
02490         if (Tof_correc_){
02491                 double t = tanl();
02492                 double dphi(0);
02493 
02494                 Helix work = *(Helix*)this;
02495                 work.ignoreErrorMatrix();
02496                 double phi_ip(0);
02497                 Hep3Vector ip(0, 0, 0);
02498                 work.pivot(ip);
02499                 phi_ip = work.phi0();
02500                 if (fabs(phi - phi_ip) > M_PI) {
02501                         if (phi > phi_ip) phi -= 2 * M_PI;
02502                         else phi_ip -= 2 * M_PI;
02503                 }
02504                 dphi = phi - phi_ip;
02505 
02506                 double l = fabs(radius() * dphi * sqrt(1 + t * t));
02507                 double pmag( sqrt( 1.0 + t*t ) / kappa());
02508                 double mass_over_p( mass_ / pmag );
02509                 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
02510                 tofest = l / ( 29.9792458 * beta );
02511                 if(csmflag==1 && (*HitMdc).wire().y()>0.)  tofest= -1. * tofest;
02512         }
02513 
02514         const HepSymMatrix& ea = Ea();
02515         HelixSeg.Ea_pre_fwd(ea);
02516         HelixSeg.Ea_filt_fwd(ea);
02517 
02518 
02519         const HepVector& v_a = a();
02520         HelixSeg.a_pre_fwd(v_a);
02521         HelixSeg.a_filt_fwd(v_a);
02522 
02523         /*
02524 
02525            HepPoint3D pivot_work = pivot();
02526            HepVector a_work = a();
02527            HepSymMatrix ea_work = Ea();
02528            KalFitTrack helix_work(pivot_work, a_work, ea_work, 0, 0, 0);
02529            helix_work.pivot(ip);
02530 
02531            HepVector a_temp = helix_work.a();
02532            HepSymMatrix ea_temp = helix_work.Ea();
02533 
02534            HelixSeg.Ea_pre_fwd(ea_temp);        
02535            HelixSeg.a_pre_fwd(a_temp);
02536 
02537         // My God, for the protection purpose 
02538         HelixSeg.Ea_filt_fwd(ea_temp);
02539         HelixSeg.a_filt_fwd(a_temp);
02540 
02541          */
02542 
02543 
02544         double dchi2R(99999999), dchi2L(99999999);
02545 
02546         HepVector v_H(5, 0);
02547         v_H[0] =  -csf0 * meas.x() - snf0 * meas.y();
02548         v_H[3] =  -meas.z();
02549         HepMatrix v_HT = v_H.T();
02550 
02551         double estim = (v_HT * v_a)[0];
02552         HepVector ea_v_H = ea * v_H;
02553         HepMatrix ea_v_HT = (ea_v_H).T();
02554         HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
02555 
02556         HepSymMatrix eaNewL(5), eaNewR(5);
02557         HepVector aNewL(5), aNewR(5);
02558 #ifdef YDEBUG
02559         cout<<"phi  "<<phi<<"  snf0 "<<snf0<<"  csf0  "<<csf0<<endl
02560                 <<"  meas:  "<<meas <<endl;   
02561         cout<<"the related matrix:"<<endl;
02562         cout<<"v_H  "<<v_H<<endl;
02563         cout<<"v_a  "<<v_a<<endl;
02564         cout<<"ea  "<<ea<<endl;
02565         cout<<"v_H_T_ea_v_H  "<<v_H_T_ea_v_H<<endl;
02566         cout<<"LR_= "<< LR_ << "lr= " << lr <<endl;
02567 #endif
02568 
02569         double time = (*HitMdc).tdc();
02570         //  if (Tof_correc_)
02571         //  time = time - way*tofest;
02572 
02573         //  double dd = 1.0e-4 * 40.0 * time;
02574         //the following 3 line : tempory
02575 
02576         double drifttime = getDriftTime(*HitMdc , tofest);
02577         double ddl = getDriftDist(*HitMdc, drifttime, 0 );
02578         double ddr = getDriftDist(*HitMdc, drifttime, 1 );
02579         double erddl = getSigma( *HitMdc, a()[4], 0, ddl);
02580         double erddr = getSigma( *HitMdc, a()[4], 1, ddr);
02581 
02582         if(debug_ == 4){
02583                 std::cout<<"the pure drift time is "<<drifttime<<std::endl;
02584                 std::cout<<"the drift dist left hypothesis is "<<ddl<<std::endl;
02585                 std::cout<<"the drift dist right hypothesis is "<<ddr<<std::endl;
02586                 std::cout<<"the error sigma left hypothesis is "<<erddl<<std::endl;
02587                 std::cout<<"the error sigma right hypothesis is "<<erddr<<std::endl;
02588         }
02589         double dmeas2[2] = { 0.1*ddl , 0.1*ddr }; //millimeter to centimeter
02590         double er_dmeas2[2] = {0. , 0.} ;
02591 
02592         if(resolflag_ == 1) { 
02593                 er_dmeas2[0] = 0.1*erddl;
02594                 er_dmeas2[1] = 0.1*erddr;
02595         } 
02596         else if(resolflag_ == 0) {
02597                 //   int layid = (*HitMdc).wire().layer().layerId();
02598                 //   double sigma = getSigma(layid, dd);
02599                 //   er_dmeas2[0] = er_dmeas2[1] = sigma;
02600         }
02601 
02602         // Left hypothesis :
02603         if ((LR_==0 && lr != 1.0) || 
02604                         (LR_==1 && lr == -1.0)) {
02605 
02606                 double er_dmeasL, dmeasL;
02607                 if(Tof_correc_) {
02608                         dmeasL = (-1.0)*fabs(dmeas2[0]);
02609                         er_dmeasL = er_dmeas2[0];
02610                 } else {
02611                         dmeasL = (-1.0)*fabs((*HitMdc).dist()[0]);
02612                         er_dmeasL = (*HitMdc).erdist()[0];
02613                 }
02614 
02615                 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
02616                 eaNewL.assign(ea - ea_v_H * AL * ea_v_HT);
02617                 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
02618                 if(0. == RkL) RkL = 1.e-4;
02619 
02620                 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
02621 
02622                 aNewL = v_a + diffL;
02623                 double sigL = dmeasL -(v_HT * aNewL)[0];
02624                 dchi2L = (sigL * sigL) /  (RkL * er_dmeasL*er_dmeasL);
02625         }
02626 
02627         if ((LR_==0 && lr != -1.0) ||
02628                         (LR_==1 && lr == 1.0)) {
02629 
02630                 double er_dmeasR, dmeasR;
02631                 if(Tof_correc_) {
02632                         dmeasR = dmeas2[1];
02633                         er_dmeasR = er_dmeas2[1];
02634                 } else {
02635                         dmeasR = fabs((*HitMdc).dist()[1]);
02636                         er_dmeasR = (*HitMdc).erdist()[1];
02637                 }
02638 
02639                 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
02640                 eaNewR.assign(ea - ea_v_H * AR * ea_v_HT);
02641                 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
02642                 if(0. == RkR) RkR = 1.e-4;
02643 
02644                 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
02645 
02646                 aNewR = v_a + diffR;
02647                 double sigR = dmeasR -(v_HT * aNewR)[0];
02648                 dchi2R = (sigR*sigR) /  (RkR * er_dmeasR*er_dmeasR);
02649         }
02650 
02651         // Update Kalman result :
02652         double dchi2_loc(0);
02653 
02654 #ifdef YDEBUG
02655         cout<<" track::update_hits......"<<endl;
02656         std::cout << "   dchi2R = " << dchi2R << ", dchi2L = " << dchi2L 
02657                 << "   estimate =  "<<estim<< std::endl;
02658         std::cout << "   dmeasR = " << dmeas2[1] 
02659                 << ", dmeasL = " << (-1.0)*fabs(dmeas2[0]) << ", check HitMdc.ddl = " 
02660                 << (*HitMdc).dist()[0] << std::endl;
02661 #endif
02662 
02663         if (fabs(dchi2R-dchi2L)<10. && inext>0) {
02664 
02665                 KalFitHitMdc & HitMdc_next = HitsMdc_[inext];
02666 
02667                 Helix H_fromR(pivot(), aNewR, eaNewR);
02668                 double chi2_fromR(chi2_next(H_fromR, HitMdc_next,csmflag));
02669 
02670                 Helix H_fromL(pivot(), aNewL, eaNewL);
02671                 double chi2_fromL(chi2_next(H_fromL, HitMdc_next,csmflag));
02672 #ifdef YDEBUG
02673                 std::cout << "   chi2_fromL = " << chi2_fromL 
02674                         << ", chi2_fromR = " << chi2_fromR << std::endl;
02675 #endif
02676                 if (fabs(chi2_fromR-chi2_fromL)<10.){
02677                         int inext2(-1);
02678                         if (inext+1<HitsMdc_.size())
02679                                 for( int k=inext+1 ; k < HitsMdc_.size(); k++ )
02680                                         if (!(HitsMdc_[k].chi2()<0)) {
02681                                                 inext2 = k;
02682                                                 break;
02683                                         }
02684 
02685                         if (inext2 != -1){
02686                                 KalFitHitMdc & HitMdc_next2 = HitsMdc_[inext2];
02687                                 double chi2_fromR2(chi2_next(H_fromR, HitMdc_next2, csmflag));
02688                                 double chi2_fromL2(chi2_next(H_fromL, HitMdc_next2, csmflag));
02689 #ifdef YDEBUG
02690                                 std::cout << "   chi2_fromL2 = " << chi2_fromL2 
02691                                         << ", chi2_fromR2 = " << chi2_fromR2 << std::endl;
02692 #endif
02693                                 if (fabs(dchi2R+chi2_fromR+chi2_fromR2-(dchi2L+chi2_fromL+chi2_fromL2))<2) {
02694                                         if (chi2_fromR2<chi2_fromL2)
02695                                                 dchi2L = DBL_MAX;
02696                                         else 
02697                                                 dchi2R = DBL_MAX;
02698                                 }
02699                         } 
02700                 }
02701 
02702                 if (!(dchi2L == DBL_MAX && dchi2R == DBL_MAX)) {
02703                         if (dchi2R+chi2_fromR < dchi2L+chi2_fromL){
02704                                 dchi2L = DBL_MAX;
02705 #ifdef YDEBUG
02706                                 std::cout << " choose right..." << std::endl;
02707 #endif
02708                         } else {
02709                                 dchi2R = DBL_MAX;
02710 #ifdef YDEBUG
02711                                 std::cout << " choose left..." << std::endl;
02712 #endif
02713                         }
02714                 }
02715         }
02716 
02717         if ((0 < dchi2R && dchi2R < dchi2cutf_calib[layerid]) ||
02718                         (0 < dchi2L && dchi2L < dchi2cutf_calib[layerid])) {
02719 
02720                 if (((LR_==0 && dchi2R < dchi2L && lr != -1) || 
02721                                         (LR_==1 && lr == 1)) && 
02722                                 fabs(aNewR[2]-a()[2]) < 1000. && aNewR[2]) {
02723                         if (nchits_ < (unsigned) nmdc_hit2_ || dchi2R < dchi2cutf_calib[layerid]){
02724                                 nchits_++;
02725                                 if (flag_ster) nster_++;
02726 
02727                                 Ea(eaNewR);
02728                                 HelixSeg.Ea_filt_fwd(eaNewR);
02729                                 a(aNewR);
02730                                 HelixSeg.a_filt_fwd(aNewR);
02731 
02732                                 /*
02733                                    Ea(eaNewR);
02734                                    a(aNewR);
02735 
02736                                    KalFitTrack helixR(pivot_work, aNewR, eaNewR, 0, 0, 0);
02737                                    helixR.pivot(ip);
02738 
02739                                    a_temp = helixR.a();
02740                                    ea_temp = helixR.Ea();
02741 
02742                                    HelixSeg.Ea_filt_fwd(ea_temp);
02743                                    HelixSeg.a_filt_fwd(a_temp);
02744                                 //HelixSeg.filt_include(1);
02745 
02746                                  */
02747 
02748                                 chiSq_ += dchi2R;
02749                                 dchi2_loc = dchi2R;
02750                                 if (l_mass_ == lead_){
02751                                         (*HitMdc).LR(1);
02752                                         HelixSeg.LR(1);
02753                                 }        
02754                                 update_bit((*HitMdc).wire().layer().layerId());
02755                         }
02756                 } else if (((LR_==0 && dchi2L <= dchi2R && lr != 1) || 
02757                                         (LR_==1 && lr == -1)) && 
02758                                 fabs(aNewL[2]-a()[2]) < 1000. && aNewL[2]){
02759                         if (nchits_ < (unsigned) nmdc_hit2_ || dchi2L < dchi2cutf_calib[layerid]){
02760                                 nchits_++;
02761                                 if (flag_ster) nster_++;
02762                                 Ea(eaNewL);
02763                                 HelixSeg.Ea_filt_fwd(eaNewL);
02764                                 a(aNewL);
02765                                 HelixSeg.a_filt_fwd(aNewL);
02766 
02767 
02768                                 /* 
02769                                    Ea(eaNewL);
02770                                    a(aNewL);
02771 
02772                                    KalFitTrack helixL(pivot_work, aNewL, eaNewL, 0, 0, 0);
02773                                    helixL.pivot(ip);
02774                                    a_temp = helixL.a();
02775                                    ea_temp = helixL.Ea();
02776                                    HelixSeg.Ea_filt_fwd(ea_temp);
02777                                    HelixSeg.a_filt_fwd(a_temp);
02778                                 //HelixSeg.filt_include(1);
02779 
02780                                  */
02781 
02782                                 chiSq_ += dchi2L;
02783                                 dchi2_loc = dchi2L;
02784                                 if (l_mass_ == lead_){
02785                                         (*HitMdc).LR(-1);
02786                                         HelixSeg.LR(-1);
02787                                 }
02788                                 update_bit((*HitMdc).wire().layer().layerId());
02789                         }
02790                 }
02791         }
02792 
02793         if (dchi2_loc > dchi2_max_) {
02794                 dchi2_max_ = dchi2_loc ;
02795                 r_max_ = pivot().perp();
02796         }
02797         dchi2out = dchi2_loc;
02798         //  if(dchi2out == 0) {
02799         //     dchi2out = ( (dchi2L < dchi2R ) ? dchi2L : dchi2R ) ;
02800         //  }
02801         return chiSq_;
02802 }
02803 
02804 
02805 double KalFitTrack::chi2_next(Helix& H, KalFitHitMdc & HitMdc ){
02806 
02807         double lr = HitMdc.LR();
02808         const KalFitWire& Wire = HitMdc.wire();
02809         int wire_ID = Wire.geoID();
02810         int layerid = HitMdc.wire().layer().layerId();
02811         double entrangle = HitMdc.rechitptr()->getEntra();      
02812 
02813         HepPoint3D fwd(Wire.fwd());
02814         HepPoint3D bck(Wire.bck());
02815         Hep3Vector wire = (Hep3Vector)fwd -(Hep3Vector)bck;
02816         Helix work = H;
02817         work.ignoreErrorMatrix();
02818         work.pivot((fwd + bck) * .5);
02819         HepPoint3D x0kal = (work.x(0).z() - bck.z())/ wire.z() * wire + bck;
02820         H.pivot(x0kal);
02821 
02822         Hep3Vector meas = H.momentum(0).cross(wire).unit();
02823 
02824         if (wire_ID<0 || wire_ID>6796){  //bes
02825                 std::cout << "KalFitTrack : wire_ID problem : " << wire_ID 
02826                         << std::endl;
02827                 return DBL_MAX;
02828         } 
02829 
02830         double x[3] ={pivot().x(), pivot().y(), pivot().z()};
02831         double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
02832         double tofest(0);
02833         double phi = fmod(phi0() + M_PI4, M_PI2);
02834         double csf0 = cos(phi);
02835         double snf0 = (1. - csf0) * (1. + csf0);
02836         snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
02837         if(phi > M_PI) snf0 = - snf0;
02838 
02839         if (Tof_correc_) {
02840                 Hep3Vector ip(0, 0, 0);
02841                 Helix work = *(Helix*)this;
02842                 work.ignoreErrorMatrix();
02843                 work.pivot(ip);
02844                 double phi_ip = work.phi0();
02845                 if (fabs(phi - phi_ip) > M_PI) {
02846                         if (phi > phi_ip) phi -= 2 * M_PI;
02847                         else phi_ip -= 2 * M_PI;
02848                 }
02849                 double t = tanl();
02850                 double l = fabs(radius() * (phi - phi_ip) * sqrt(1 + t * t));
02851                 double pmag( sqrt( 1.0 + t*t ) / kappa());
02852                 double mass_over_p( mass_ / pmag );
02853                 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
02854                 tofest = l / ( 29.9792458 * beta );
02855                 //  if(csmflag==1 && HitMdc.wire().y()>0.)  tofest= -1. * tofest;
02856         }
02857 
02858         const HepSymMatrix& ea = H.Ea();
02859         const HepVector& v_a = H.a();
02860         double dchi2R(DBL_MAX), dchi2L(DBL_MAX);
02861 
02862         HepVector v_H(5, 0);
02863         v_H[0] =  -csf0 * meas.x() - snf0 * meas.y();
02864         v_H[3] =  -meas.z();
02865         HepMatrix v_HT = v_H.T();
02866 
02867         double estim = (v_HT * v_a)[0];
02868         HepVector ea_v_H = ea * v_H;
02869         HepMatrix ea_v_HT = (ea_v_H).T();
02870         HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
02871 
02872         HepSymMatrix eaNewL(5), eaNewR(5);
02873         HepVector aNewL(5), aNewR(5);
02874 
02875         //double time = HitMdc.tdc();
02876         //if (Tof_correc_)
02877         // time = time - tofest;
02878         double drifttime = getDriftTime(HitMdc , tofest);
02879         double ddl = getDriftDist(HitMdc, drifttime , 0 );
02880         double ddr = getDriftDist(HitMdc, drifttime , 1 );
02881         double erddl = getSigma( HitMdc, H.a()[4], 0, ddl);
02882         double erddr = getSigma( HitMdc, H.a()[4], 1, ddr);
02883 
02884         double dmeas2[2] = { 0.1*ddl, 0.1*ddr };
02885         double er_dmeas2[2] = {0. , 0.};
02886         if(resolflag_ == 1) { 
02887                 er_dmeas2[0] = 0.1*erddl; 
02888                 er_dmeas2[1] = 0.1*erddr;
02889         } 
02890         else if(resolflag_ == 0) {
02891                 //  int layid = HitMdc.wire().layer().layerId();
02892                 //  double sigma = getSigma(layid, dd);
02893                 //  er_dmeas2[0] = er_dmeas2[1] = sigma;
02894         }
02895 
02896         if ((LR_==0 && lr != 1.0) || 
02897                         (LR_==1 && lr == -1.0)) {
02898 
02899                 double er_dmeasL, dmeasL;
02900                 if(Tof_correc_) {
02901                         dmeasL = (-1.0)*fabs(dmeas2[0]);
02902                         er_dmeasL = er_dmeas2[0];
02903                 } else {
02904                         dmeasL = (-1.0)*fabs(HitMdc.dist()[0]);
02905                         er_dmeasL = HitMdc.erdist()[0];
02906                 }
02907 
02908                 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
02909                 eaNewL.assign(ea - ea_v_H * AL * ea_v_HT);
02910                 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
02911                 if(0. == RkL) RkL = 1.e-4;
02912 
02913                 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
02914                 aNewL = v_a + diffL;
02915                 double sigL = dmeasL -(v_HT * aNewL)[0];
02916                 dchi2L = (sigL * sigL) /  (RkL * er_dmeasL*er_dmeasL);
02917         }
02918 
02919         if ((LR_==0 && lr != -1.0) ||
02920                         (LR_==1 && lr == 1.0)) {
02921 
02922                 double er_dmeasR, dmeasR;
02923                 if(Tof_correc_) {
02924                         dmeasR = dmeas2[1];
02925                         er_dmeasR = er_dmeas2[1];
02926                 } else {
02927                         dmeasR = fabs(HitMdc.dist()[1]);
02928                         er_dmeasR = HitMdc.erdist()[1];
02929                 }
02930 
02931                 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
02932                 eaNewR.assign(ea - ea_v_H * AR * ea_v_HT);
02933                 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
02934                 if(0. == RkR) RkR = 1.e-4;
02935 
02936                 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
02937                 aNewR = v_a + diffR;
02938                 double sigR = dmeasR -(v_HT * aNewR)[0];
02939                 dchi2R = (sigR*sigR) /  (RkR * er_dmeasR*er_dmeasR);
02940         } 
02941 
02942         if (dchi2R < dchi2L){
02943                 H.a(aNewR); H.Ea(eaNewR);
02944         } else {
02945                 H.a(aNewL); H.Ea(eaNewL);
02946         }
02947         return ((dchi2R < dchi2L) ? dchi2R : dchi2L);
02948 }
02949 
02950 double KalFitTrack::chi2_next(Helix& H, KalFitHitMdc & HitMdc, int csmflag){
02951 
02952         double lr = HitMdc.LR();
02953         const KalFitWire& Wire = HitMdc.wire();
02954         int wire_ID = Wire.geoID();
02955         int layerid = HitMdc.wire().layer().layerId();
02956         double entrangle = HitMdc.rechitptr()->getEntra();      
02957 
02958         HepPoint3D fwd(Wire.fwd());
02959         HepPoint3D bck(Wire.bck());
02960         Hep3Vector wire = (Hep3Vector)fwd -(Hep3Vector)bck;
02961         Helix work = H;
02962         work.ignoreErrorMatrix();
02963         work.pivot((fwd + bck) * .5);
02964         HepPoint3D x0kal = (work.x(0).z() - bck.z())/ wire.z() * wire + bck;
02965         H.pivot(x0kal);
02966 
02967         Hep3Vector meas = H.momentum(0).cross(wire).unit();
02968 
02969         if (wire_ID<0 || wire_ID>6796){  //bes
02970                 std::cout << "KalFitTrack : wire_ID problem : " << wire_ID 
02971                         << std::endl;
02972                 return DBL_MAX;
02973         } 
02974 
02975         double x[3] ={pivot().x(), pivot().y(), pivot().z()};
02976         double pmom[3] ={momentum().x(), momentum().y(), momentum().z()};
02977         double tofest(0);
02978         double phi = fmod(phi0() + M_PI4, M_PI2);
02979         double csf0 = cos(phi);
02980         double snf0 = (1. - csf0) * (1. + csf0);
02981         snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
02982         if(phi > M_PI) snf0 = - snf0;
02983 
02984         if (Tof_correc_) {
02985                 Hep3Vector ip(0, 0, 0);
02986                 Helix work = *(Helix*)this;
02987                 work.ignoreErrorMatrix();
02988                 work.pivot(ip);
02989                 double phi_ip = work.phi0();
02990                 if (fabs(phi - phi_ip) > M_PI) {
02991                         if (phi > phi_ip) phi -= 2 * M_PI;
02992                         else phi_ip -= 2 * M_PI;
02993                 }
02994                 double t = tanl();
02995                 double l = fabs(radius() * (phi - phi_ip) * sqrt(1 + t * t));
02996                 double pmag( sqrt( 1.0 + t*t ) / kappa());
02997                 double mass_over_p( mass_ / pmag );
02998                 double beta( 1.0 / sqrt( 1.0 + mass_over_p * mass_over_p ) );
02999                 tofest = l / ( 29.9792458 * beta );
03000                 if(csmflag==1 && HitMdc.wire().y()>0.)  tofest= -1. * tofest;
03001         }
03002 
03003         const HepSymMatrix& ea = H.Ea();
03004         const HepVector& v_a = H.a();
03005         double dchi2R(DBL_MAX), dchi2L(DBL_MAX);
03006 
03007         HepVector v_H(5, 0);
03008         v_H[0] =  -csf0 * meas.x() - snf0 * meas.y();
03009         v_H[3] =  -meas.z();
03010         HepMatrix v_HT = v_H.T();
03011 
03012         double estim = (v_HT * v_a)[0];
03013         HepVector ea_v_H = ea * v_H;
03014         HepMatrix ea_v_HT = (ea_v_H).T();
03015         HepVector v_H_T_ea_v_H = v_HT * ea_v_H;
03016 
03017         HepSymMatrix eaNewL(5), eaNewR(5);
03018         HepVector aNewL(5), aNewR(5);
03019 
03020         //double time = HitMdc.tdc();
03021         //if (Tof_correc_)
03022         // time = time - tofest;
03023         double drifttime = getDriftTime(HitMdc , tofest);
03024         double ddl = getDriftDist(HitMdc, drifttime , 0 );
03025         double ddr = getDriftDist(HitMdc, drifttime , 1 );
03026         double erddl = getSigma( HitMdc, H.a()[4], 0, ddl);
03027         double erddr = getSigma( HitMdc, H.a()[4], 1, ddr);
03028 
03029         double dmeas2[2] = { 0.1*ddl, 0.1*ddr };
03030         double er_dmeas2[2] = {0. , 0.};
03031         if(resolflag_ == 1) { 
03032                 er_dmeas2[0] = 0.1*erddl; 
03033                 er_dmeas2[1] = 0.1*erddr;
03034         } 
03035         else if(resolflag_ == 0) {
03036                 //  int layid = HitMdc.wire().layer().layerId();
03037                 //  double sigma = getSigma(layid, dd);
03038                 //  er_dmeas2[0] = er_dmeas2[1] = sigma;
03039         }
03040 
03041         if ((LR_==0 && lr != 1.0) || 
03042                         (LR_==1 && lr == -1.0)) {
03043 
03044                 double er_dmeasL, dmeasL;
03045                 if(Tof_correc_) {
03046                         dmeasL = (-1.0)*fabs(dmeas2[0]);
03047                         er_dmeasL = er_dmeas2[0];
03048                 } else {
03049                         dmeasL = (-1.0)*fabs(HitMdc.dist()[0]);
03050                         er_dmeasL = HitMdc.erdist()[0];
03051                 }
03052 
03053                 double AL = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasL*er_dmeasL);
03054                 eaNewL.assign(ea - ea_v_H * AL * ea_v_HT);
03055                 double RkL = 1 - (v_H_T_ea_v_H)[0] * AL;
03056                 if(0. == RkL) RkL = 1.e-4;
03057 
03058                 HepVector diffL = ea_v_H * AL * (dmeasL - estim);
03059                 aNewL = v_a + diffL;
03060                 double sigL = dmeasL -(v_HT * aNewL)[0];
03061                 dchi2L = (sigL * sigL) /  (RkL * er_dmeasL*er_dmeasL);
03062         }
03063 
03064         if ((LR_==0 && lr != -1.0) ||
03065                         (LR_==1 && lr == 1.0)) {
03066 
03067                 double er_dmeasR, dmeasR;
03068                 if(Tof_correc_) {
03069                         dmeasR = dmeas2[1];
03070                         er_dmeasR = er_dmeas2[1];
03071                 } else {
03072                         dmeasR = fabs(HitMdc.dist()[1]);
03073                         er_dmeasR = HitMdc.erdist()[1];
03074                 }
03075 
03076                 double AR = 1 / ((v_H_T_ea_v_H)[0] + er_dmeasR*er_dmeasR);
03077                 eaNewR.assign(ea - ea_v_H * AR * ea_v_HT);
03078                 double RkR = 1 - (v_H_T_ea_v_H)[0] * AR;
03079                 if(0. == RkR) RkR = 1.e-4;
03080 
03081                 HepVector diffR = ea_v_H * AR * (dmeasR - estim);
03082                 aNewR = v_a + diffR;
03083                 double sigR = dmeasR -(v_HT * aNewR)[0];
03084                 dchi2R = (sigR*sigR) /  (RkR * er_dmeasR*er_dmeasR);
03085         } 
03086 
03087         if (dchi2R < dchi2L){
03088                 H.a(aNewR); H.Ea(eaNewR);
03089         } else {
03090                 H.a(aNewL); H.Ea(eaNewL);
03091         }
03092         return ((dchi2R < dchi2L) ? dchi2R : dchi2L);
03093 }
03094 
03095 
03096 double KalFitTrack::getSigma(int layerId, double driftDist ) const {
03097         double sigma1,sigma2,f;
03098         driftDist *= 10;//mm
03099         if(layerId<8){
03100                 if(driftDist<0.5){
03101                         sigma1=0.112784;      sigma2=0.229274;      f=0.666;
03102                 }else if(driftDist<1.){
03103                         sigma1=0.103123;      sigma2=0.269797;      f=0.934;
03104                 }else if(driftDist<1.5){
03105                         sigma1=0.08276;        sigma2=0.17493;      f=0.89;
03106                 }else if(driftDist<2.){
03107                         sigma1=0.070109;      sigma2=0.149859;      f=0.89;
03108                 }else if(driftDist<2.5){
03109                         sigma1=0.064453;      sigma2=0.130149;      f=0.886;
03110                 }else if(driftDist<3.){
03111                         sigma1=0.062383;      sigma2=0.138806;      f=0.942;
03112                 }else if(driftDist<3.5){
03113                         sigma1=0.061873;      sigma2=0.145696;      f=0.946;
03114                 }else if(driftDist<4.){
03115                         sigma1=0.061236;      sigma2=0.119584;      f=0.891;
03116                 }else if(driftDist<4.5){
03117                         sigma1=0.066292;      sigma2=0.148426;      f=0.917;
03118                 }else if(driftDist<5.){
03119                         sigma1=0.078074;      sigma2=0.188148;      f=0.911;
03120                 }else if(driftDist<5.5){
03121                         sigma1=0.088657;      sigma2=0.27548;      f=0.838;
03122                 }else{
03123                         sigma1=0.093089;      sigma2=0.115556;      f=0.367;
03124                 }
03125         }else{
03126                 if(driftDist<0.5){
03127                         sigma1=0.112433;      sigma2=0.327548;      f=0.645;
03128                 }else if(driftDist<1.){
03129                         sigma1=0.096703;      sigma2=0.305206;      f=0.897;
03130                 }else if(driftDist<1.5){
03131                         sigma1=0.082518;      sigma2=0.248913;      f= 0.934;
03132                 }else if(driftDist<2.){
03133                         sigma1=0.072501;      sigma2=0.153868;      f= 0.899;
03134                 }else if(driftDist<2.5){
03135                         sigma1= 0.065535;     sigma2=0.14246;       f=0.914;
03136                 }else if(driftDist<3.){
03137                         sigma1=0.060497;      sigma2=0.126489;      f=0.918;
03138                 }else if(driftDist<3.5){
03139                         sigma1=0.057643;      sigma2= 0.112927;     f=0.892;
03140                 }else if(driftDist<4.){
03141                         sigma1=0.055266;      sigma2=0.094833;      f=0.887;
03142                 }else if(driftDist<4.5){
03143                         sigma1=0.056263;      sigma2=0.124419;      f= 0.932;
03144                 }else if(driftDist<5.){
03145                         sigma1=0.056599;      sigma2=0.124248;      f=0.923;
03146                 }else if(driftDist<5.5){
03147                         sigma1= 0.061377;     sigma2=0.146147;      f=0.964;
03148                 }else if(driftDist<6.){
03149                         sigma1=0.063978;      sigma2=0.150591;      f=0.942;
03150                 }else if(driftDist<6.5){
03151                         sigma1=0.072951;      sigma2=0.15685;       f=0.913;
03152                 }else if(driftDist<7.){
03153                         sigma1=0.085438;      sigma2=0.255109;      f=0.931;
03154                 }else if(driftDist<7.5){
03155                         sigma1=0.101635;      sigma2=0.315529;      f=0.878;
03156                 }else{
03157                         sigma1=0.149529;      sigma2=0.374697;      f=0.89;
03158                 }
03159         }
03160         double sigmax = sqrt(f*sigma1*sigma1+(1 - f)*sigma2*sigma2)*0.1;
03161         return sigmax;//cm
03162 }
03163 

Generated on Tue Nov 29 23:13:24 2016 for BOSS_7.0.2 by  doxygen 1.4.7