T3DLineFitter Class Reference

A class to fit a TTrackBase object to a 3D line. More...

#include <T3DLineFitter.h>

Inheritance diagram for T3DLineFitter:

TMFitter List of all members.

Public Member Functions

 T3DLineFitter (const std::string &name)
 Constructor.
 T3DLineFitter (const std::string &name, bool m_sag, int m_prop, bool m_tof)
virtual ~T3DLineFitter ()
 Destructor.
void dump (const std::string &message=std::string(""), const std::string &prefix=std::string("")) const
 dumps debug information.
virtual int fit (TTrackBase &) const
virtual int fit (TTrackBase &, float t0Offset) const
void sag (bool)
void propagation (int)
void tof (bool)
const std::stringname (void) const
 returns name.

Protected Member Functions

void fitDone (TTrackBase &) const
 sets the fitted flag. (Bad implementation)

Private Member Functions

int dxda (const TMLink &, const T3DLine &, Vector &dxda, Vector &dyda, Vector &dzda, HepVector3D &wireDirection) const
 calculates dXda. 'TMLink' and 'T3DLine' are inputs. Others are outputs.
void drift (const T3DLine &, const TMLink &, float t0Offset, double &distance, double &err) const
 calculates drift distance and its error.

Private Attributes

bool _sag
int _propagation
bool _tof

Detailed Description

A class to fit a TTrackBase object to a 3D line.

Definition at line 41 of file T3DLineFitter.h.


Constructor & Destructor Documentation

T3DLineFitter::T3DLineFitter ( const std::string name  ) 

Constructor.

Definition at line 80 of file T3DLineFitter.cxx.

00081   : TMFitter(name),
00082 //    _sag(true),_propagation(1),_tof(false){
00083     _sag(false),_propagation(0),_tof(true){   //Liuqg, tmp
00084 
00085 }

T3DLineFitter::T3DLineFitter ( const std::string name,
bool  m_sag,
int  m_prop,
bool  m_tof 
)

Definition at line 86 of file T3DLineFitter.cxx.

00088   : TMFitter(name),
00089     _sag(m_sag),_propagation(m_prop),_tof(m_tof){
00090 }

T3DLineFitter::~T3DLineFitter (  )  [virtual]

Destructor.

Definition at line 92 of file T3DLineFitter.cxx.

00092                               {
00093 }


Member Function Documentation

void T3DLineFitter::drift ( const T3DLine ,
const TMLink ,
float  t0Offset,
double &  distance,
double &  err 
) const [private]

calculates drift distance and its error.

Definition at line 105 of file T3DLineFitter.cxx.

References MdcRec_wirhit::adc, check_raw_filter::dist, IMdcCalibFunSvc::driftTimeToDist(), IMdcCalibFunSvc::getSigma(), IMdcCalibFunSvc::getT0(), IMdcCalibFunSvc::getTimeWalk(), TMLink::hit(), TMDCWire::layerId(), TMDCWire::localId(), msgSvc(), TMLink::positionOnTrack(), TMLink::positionOnWire(), TMDCWireHit::reccdc(), MdcRec_wirhit::tdc, tof(), Bes_Common::WARNING, TMDCWireHit::wire(), WireHitLeft, and WireHitRight.

Referenced by fit().

00107                                                              {
00108 
00109     //read t0 from TDS-------------------------------------//
00110     double _t0_bes = -1.;
00111     IMessageSvc *msgSvc;
00112     Gaudi::svcLocator()->service("MessageSvc", msgSvc);
00113     MsgStream log(msgSvc, "TCosmicFitter");
00114 
00115     IDataProviderSvc* eventSvc = NULL;
00116     Gaudi::svcLocator()->service("EventDataSvc", eventSvc);
00117 
00118     SmartDataPtr<RecEsTimeCol> aevtimeCol(eventSvc,"/Event/Recon/RecEsTimeCol");
00119     if (aevtimeCol) {
00120       RecEsTimeCol::iterator iter_evt = aevtimeCol->begin();
00121       _t0_bes = (*iter_evt)->getTest();
00122     }else{
00123       log << MSG::WARNING << "Could not find RecEsTimeCol" << endreq;
00124     }
00125     //----------------------------------------------------//
00126 
00127   const TMDCWireHit& h = *l.hit();
00128   const HepPoint3D& onTrack = l.positionOnTrack();
00129   const HepPoint3D& onWire = l.positionOnWire();
00130   unsigned leftRight = WireHitRight;
00131   //  if (onWire.cross(onTrack).z() < 0) leftRight = WireHitLeft;
00132   if((onWire.x()*onTrack.y()-onWire.y()*onTrack.x())<0) leftRight = WireHitLeft;
00133 
00134   //...No correction...
00135 /*  if ((t0Offset == 0.) && (_propagation==0) && (! _tof)) {
00136     distance = l.drift(leftRight);
00137     err = h.dDrift(leftRight);
00138     return;
00139   }*/  
00140  
00141   //...TOF correction...
00142   //    momentum ???? or velocity -> assumued light velocity
00143   float tof = 0.;
00144 //  if (_tof) {
00145     //    double length = ((onTrack - t.x0())*t.k())/t.k().mag();
00146 /*    double tl = t.tanl();
00147     double length = ((onTrack - t.x0())*t.k())/sqrt(1+tl*tl);
00148     static const double Ic = 1/29.9792; //1/[cm/ns] 
00149     tof = length * Ic;
00150 */
00151     //cal the time pass through the chamber
00152 /*    const float Rad = 81; // cm
00153     float dRho = t.helix().a()[0];
00154     float Lproj = sqrt(Rad*Rad - dRho*dRho);
00155     float tlmd = t.helix().a()[4];
00156     float fct = sqrt(1. + tlmd * tlmd);
00157 
00158     float x[3]={ onWire.x(), onWire.y(), onWire.z()};
00159 
00160     float Rad_wire = sqrt(x[0]*x[0]+x[1]*x[1]); // cm
00161     float L_wire = sqrt(Rad_wire*Rad_wire - dRho*dRho);
00162     float flyLength = Lproj - L_wire;
00163     if (x[1]<0) flyLength = Lproj + L_wire;
00164     float Tfly = flyLength/29.98*sqrt(1.0+(0.105/10)*(0.105/10))*fct; //approxiate... cm/ns
00165 */
00166     double Tfly = _t0_bes/220.*(110.-onWire.y());
00167 //  }
00168     
00169   //...T0 and propagation corrections...
00170   int wire = h.wire()->localId();
00171   int layerId = h.wire()->layerId();
00172 //  int wire = h.wire()->id();
00173   int side = leftRight;
00174 //  if (side==0) side = -1;
00175 //  float p[3] = {-t.sinPhi0(),t.cosPhi0(),t.tanl()};
00176 //  float x[3] = {onWire.x(), onWire.y(), onWire.z()};
00177 //  float time = h.reccdc()->tdc + t0Offset - tof;
00178   float time = h.reccdc()->tdc - Tfly;
00179   float dist;
00180   float edist;
00181   IMdcCalibFunSvc* l_mdcCalFunSvc;
00182   Gaudi::svcLocator() -> service("MdcCalibFunSvc", l_mdcCalFunSvc);
00183   double T0 = l_mdcCalFunSvc->getT0(layerId,wire);
00184   double rawadc = 0.;
00185   rawadc =h.reccdc()->adc;
00186 
00187   double timeWalk = l_mdcCalFunSvc->getTimeWalk(layerId, rawadc);
00188 //  int prop = _propagation;
00189   double drifttime =time -T0-timeWalk;
00190 //  dist = l_mdcCalFunSvc->rawTimeNoTOFToDist(time, layerId, wire, side, 0.0);
00191   dist = l_mdcCalFunSvc->driftTimeToDist(drifttime,layerId, wire, side, 0.0);
00192   edist = l_mdcCalFunSvc->getSigma(layerId, side, dist, 0.0);
00193 //  if(layerId<20) edist = 999999.;
00194   dist = dist/10.0; //mm->cm
00195   edist = edist/10.0;
00196 /*zsl 
00197   calcdc_driftdist_(& prop,
00198                     & wire,
00199                     & side,
00200                     p,
00201                     x,
00202                     & time,
00203                     & dist,
00204                     & edist);
00205  */
00206   distance = (double) dist;
00207   err = (double) edist;
00208   return;
00209 }

void T3DLineFitter::dump ( const std::string message = std::string(""),
const std::string prefix = std::string("") 
) const

dumps debug information.

Reimplemented from TMFitter.

int T3DLineFitter::dxda ( const TMLink ,
const T3DLine ,
Vector dxda,
Vector dyda,
Vector dzda,
HepVector3D wireDirection 
) const [private]

calculates dXda. 'TMLink' and 'T3DLine' are inputs. Others are outputs.

Definition at line 389 of file T3DLineFitter.cxx.

References _sag, genRecEmupikp::i, TMLink::positionOnTrack(), TMLink::positionOnWire(), t(), v, w, and TMLink::wire().

Referenced by fit().

00391                                                          {
00392   //   onTrack = x0 + t * k
00393   //   onWire  = w0 + s * wireDirection
00394   //...Setup...
00395   const TMDCWire& w = *l.wire();
00396   const HepVector3D k = t.k();
00397   const double cosPhi0 = t.cosPhi0();
00398   const double sinPhi0 = t.sinPhi0();
00399   const double dr = t.dr();
00400   const HepPoint3D& onWire = l.positionOnWire();
00401   const HepPoint3D& onTrack = l.positionOnTrack();
00402   //  const Vector3 u = onTrack - onWire;
00403   const double t_t = (onWire - t.x0()).dot(k)/k.mag2();
00404 
00405   //...Sag correction...
00406   HepPoint3D xw = w.xyPosition();
00407   HepPoint3D wireBackwardPosition = w.backwardPosition();
00408   wireDirection = w.direction();
00409   if (_sag) w.wirePosition(onWire.z(),
00410                            xw,
00411                            wireBackwardPosition,
00412                            (HepVector3D&)wireDirection);
00413   const HepVector3D& v = wireDirection;
00414 
00415   //   onTrack = x0 + t * k
00416   //   onWire  = w0 + s * v
00417 
00418   const double v_k = v.dot(k);
00419   const double tvk = v_k*v_k-k.mag2();
00420   if(tvk==0) return(-1);
00421 
00422   const HepVector3D& dxdt_a = k;
00423 
00424   const HepVector3D dxda_t[4]
00425     ={HepVector3D(cosPhi0,sinPhi0,0),
00426       HepVector3D(-dr*sinPhi0-t_t*cosPhi0,dr*cosPhi0-t_t*sinPhi0,0),
00427       HepVector3D(0,0,1),
00428       HepVector3D(0,0,t_t)};
00429 
00430   const HepVector3D dx0da[4]
00431     ={HepVector3D(cosPhi0,sinPhi0,0),
00432       HepVector3D(-dr*sinPhi0,dr*cosPhi0,0),
00433       HepVector3D(0,0,1),
00434       HepVector3D(0,0,0)};
00435 
00436   const HepVector3D dkda[4]
00437     ={HepVector3D(0,0,0),
00438       HepVector3D(-cosPhi0,-sinPhi0,0),
00439       HepVector3D(0,0,0),
00440       HepVector3D(0,0,1)};
00441 
00442   const HepVector3D d = t.x0() - wireBackwardPosition;
00443   const HepVector3D kvkv = k - v_k*v;
00444 
00445   for(int i=0;i<4;i++){
00446     const double v_dkda = v.dot(dkda[i]);
00447 
00448     const double dtda = dx0da[i].dot(kvkv)/tvk
00449                       + d.dot(dkda[i]-v_dkda*v)/tvk
00450                       - d.dot(kvkv)*2*(v_k*v_dkda-k.dot(dkda[i]))/(tvk*tvk);
00451 
00452     const HepVector3D dxda3D = dxda_t[i] + dtda*dxdt_a;
00453 
00454     dxda[i] = dxda3D.x();
00455     dyda[i] = dxda3D.y();
00456     dzda[i] = dxda3D.z();
00457   }
00458     
00459   return 0;
00460 }

int T3DLineFitter::fit ( TTrackBase ,
float  t0Offset 
) const [virtual]

Definition at line 215 of file T3DLineFitter.cxx.

References _sag, DBL_MAX, drift(), dxda(), showlog::err, genRecEmupikp::i, ganga-rec::j, Line3D, NStereoHits(), TTrackBase::objectType(), ORIGIN, t(), T3DLine2DFit, TFitAlreadyFitted, TFitErrorFewHits, TFitFailed, TFitUnavailable, v, WireHitLeft, and WireHitRight.

00215                                                           {
00216 
00217   //std::cout<<"T3DLineFitter::fit  start"<<std::endl;
00218 
00219   //...Type check...
00220   if(tb.objectType() != Line3D) return TFitUnavailable;
00221   T3DLine& t = (T3DLine&) tb;
00222 
00223   //...Already fitted ?...
00224   if(t.fitted()) return TFitAlreadyFitted;
00225 
00226   //...Count # of hits...
00227   AList<TMLink> cores = t.cores();
00228   unsigned nCores = cores.length();
00229   unsigned nStereoCores = NStereoHits(cores);
00230 
00231   
00232   //...Check # of hits...
00233   bool flag2D = false;
00234   if ((nStereoCores == 0) && (nCores > 3)) flag2D = true;
00235   else if ((nStereoCores < 2) || (nCores - nStereoCores < 3))
00236     return TFitErrorFewHits;
00237 
00238   //...Move pivot to ORIGIN...
00239   const HepPoint3D pivot_bak = t.pivot();
00240   t.pivot(ORIGIN);
00241 
00242   //...Setup...
00243   Vector a(4),da(4);
00244   a = t.a();
00245   Vector dxda(4);
00246   Vector dyda(4);
00247   Vector dzda(4);
00248   Vector dDda(4);
00249   Vector dchi2da(4);
00250   SymMatrix d2chi2d2a(4,0);
00251   static const SymMatrix zero4(4,0);
00252   double chi2;
00253   double chi2Old = DBL_MAX;
00254   double factor = 1.0;
00255   int err = 0;
00256   SymMatrix e(2,0);
00257   Vector f(2);
00258 
00259   //...Fitting loop...
00260   unsigned nTrial = 0;
00261   while(nTrial < 100){
00262     
00263     //...Set up...
00264     chi2 = 0;
00265     for (unsigned j=0;j<4;j++) dchi2da[j]=0;
00266     d2chi2d2a=zero4;
00267 
00268     //...Loop with hits...
00269     unsigned i=0;
00270     while(TMLink* l = cores[i++]){
00271       const TMDCWireHit& h = *l->hit();
00272 
00273       //...Cal. closest points...
00274       t.approach(*l,_sag);
00275       const HepPoint3D& onTrack=l->positionOnTrack();
00276       const HepPoint3D& onWire=l->positionOnWire();
00277       unsigned leftRight = WireHitRight;
00278       if (onWire.cross(onTrack).z() < 0.) leftRight = WireHitLeft;
00279 
00280       //...Obtain drift distance and its error...
00281       double distance;
00282       double eDistance;
00283       drift(t, * l, t0Offset, distance, eDistance);
00284       l->drift(distance,0);
00285       l->drift(distance,1);
00286       l->dDrift(eDistance,0);
00287       l->dDrift(eDistance,1);
00288       double eDistance2 = eDistance * eDistance;
00289       //cout<<"distance: "<< distance << " eDistance: " << eDistance << endl;
00290 
00291       //...Residual...
00292       HepVector3D v = onTrack - onWire;
00293       double vmag = v.mag();
00294       double dDistance = vmag - distance;
00295 
00296       HepVector3D vw;
00297       //...dxda...
00298       this->dxda(*l, t, dxda, dyda, dzda, vw);
00299 
00300       //...Chi2 related...
00301       dDda = (vmag > 0.)
00302         ? ((v.x() * (1. - vw.x() * vw.x()) -
00303             v.y() * vw.x() * vw.y() - v.z() * vw.x() * vw.z())
00304            * dxda + 
00305            (v.y() * (1. - vw.y() * vw.y()) -
00306             v.z() * vw.y() * vw.z() - v.x() * vw.y() * vw.x())
00307            * dyda + 
00308            (v.z() * (1. - vw.z() * vw.z()) -
00309             v.x() * vw.z() * vw.x() - v.y() * vw.z() * vw.y())
00310            * dzda) / vmag : Vector(4,0);
00311       if (vmag <= 0.0) {
00312         std::cout << "    in fit " << onTrack << ", " << onWire;
00313         h.dump();
00314       }
00315       dchi2da += (dDistance / eDistance2) * dDda;
00316       d2chi2d2a += vT_times_v(dDda) / eDistance2;
00317       double pChi2 = dDistance * dDistance / eDistance2;
00318       chi2 += pChi2;
00319       
00320       //...Store results...
00321       l->update(onTrack, onWire, leftRight, pChi2);
00322     }
00323 
00324     //...Check condition...
00325     double change = chi2Old - chi2;
00326 
00327     if (fabs(change) < 1.0e-5) break;
00328     if (change < 0.) {
00329       a += factor * da; //recover
00330       factor *= 0.1;
00331     }else{
00332       chi2Old = chi2;
00333       if(flag2D){
00334         f = dchi2da.sub(1,2);
00335         e = d2chi2d2a.sub(1,2);
00336         f = solve(e,f);
00337         da[0]=f[0];
00338         da[1]=f[1];
00339         da[2]= 0;
00340         da[3]= 0;
00341       }else{
00342         //...Cal. helix parameters for next loop...
00343         da = solve(d2chi2d2a, dchi2da);
00344       }
00345     }
00346     a -= factor * da;
00347     t.a(a);
00348     ++nTrial;
00349   }
00350 
00351   //...Cal. error matrix...
00352   SymMatrix Ea(4,0);
00353   unsigned dim;
00354   if(flag2D){
00355     dim=2;
00356     SymMatrix Eb(3,0),Ec(3,0);
00357     Eb = d2chi2d2a.sub(1,3);
00358     Ec = Eb.inverse(err);
00359     Ea[0][0] = Ec[0][0];
00360     Ea[0][1] = Ec[0][1];
00361     Ea[0][2] = Ec[0][2];
00362     Ea[1][1] = Ec[1][1];
00363     Ea[1][2] = Ec[1][2];
00364     Ea[2][2] = Ec[2][2];
00365   }else{
00366     dim=4;
00367     Ea = d2chi2d2a.inverse(err);
00368   }
00369 
00370   //...Store information...
00371   if(! err){
00372     t.a(a);
00373     t.Ea(Ea);
00374     t._fitted = true;
00375     if (flag2D) err = T3DLine2DFit;
00376   }else{
00377     err = TFitFailed;
00378   }
00379 
00380   t._ndf = nCores - dim;
00381   t._chi2 = chi2;
00382 
00383   //...Recover pivot...
00384   t.pivot(pivot_bak);
00385 
00386   return err;
00387 }

int T3DLineFitter::fit ( TTrackBase  )  const [virtual]

Implements TMFitter.

Definition at line 211 of file T3DLineFitter.cxx.

00211                                           {
00212   return fit(tb,0);
00213 }

void TMFitter::fitDone ( TTrackBase  )  const [protected, inherited]

sets the fitted flag. (Bad implementation)

Definition at line 24 of file TMFitter.cxx.

References t().

Referenced by TRobustLineFitter::fit(), TLineFitter::fit(), and TCircleFitter::fit().

00024                                       {
00025     t._fitted = true;
00026 }

const std::string & TMFitter::name ( void   )  const [inline, inherited]

returns name.

Definition at line 73 of file TMFitter.h.

References TMFitter::_name.

00073                          {
00074     return _name;
00075 }

void T3DLineFitter::propagation ( int   ) 

Definition at line 98 of file T3DLineFitter.cxx.

References _propagation.

00098                                       {
00099   _propagation = _in;
00100 }

void T3DLineFitter::sag ( bool   ) 

Definition at line 95 of file T3DLineFitter.cxx.

References _sag.

00095                                {
00096   _sag = _in;
00097 }

void T3DLineFitter::tof ( bool   ) 

Definition at line 101 of file T3DLineFitter.cxx.

References _tof.

Referenced by drift().

00101                                {
00102   _tof = _in;
00103 }


Member Data Documentation

int T3DLineFitter::_propagation [private]

Definition at line 82 of file T3DLineFitter.h.

Referenced by propagation().

bool T3DLineFitter::_sag [private]

Definition at line 81 of file T3DLineFitter.h.

Referenced by dxda(), fit(), and sag().

bool T3DLineFitter::_tof [private]

Definition at line 83 of file T3DLineFitter.h.

Referenced by tof().


Generated on Tue Nov 29 23:35:56 2016 for BOSS_7.0.2 by  doxygen 1.4.7