Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

TCosmicFitter Class Reference

A class to fit a TTrackBase object to a helix. More...

#include <TCosmicFitter.h>

Inheritance diagram for TCosmicFitter:

TMFitter TMFitter List of all members.

Public Member Functions

void dump (const std::string &message=std::string(""), const std::string &prefix=std::string("")) const
 dumps debug information.
void dump (const std::string &message=std::string(""), const std::string &prefix=std::string("")) const
 dumps debug information.
int fit (TTrackBase &, float t0Offset) const
int fit (TTrackBase &) const
int fit (TTrackBase &, float t0Offset) const
int fit (TTrackBase &) const
int fitWithCathode (TTrackBase &, float t0Offset=0., float windowSize=0.6, int SysCorr=0)
int fitWithCathode (TTrackBase &, float t0Offset=0., float windowSize=0.6, int SysCorr=0)
const std::string & name (void) const
 returns name.
const std::string & name (void) const
 returns name.
 TCosmicFitter (const std::string &name)
 Constructor.
 TCosmicFitter (const std::string &name)
 Constructor.
virtual ~TCosmicFitter ()
 Destructor.
virtual ~TCosmicFitter ()
 Destructor.

Protected Member Functions

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

Private Member Functions

int dxda (const TMLink &link, const Helix &helix, double dPhi, HepVector &dxda, HepVector &dyda, HepVector &dzda, int doSagCorrection) const
 calculates dXda. 'link' and 'dPhi' are inputs. Others are outputs.
int dxda (const TMLink &link, const Helix &helix, double dPhi, HepVector &dxda, HepVector &dyda, HepVector &dzda, int doSagCorrection) const
 calculates dXda. 'link' and 'dPhi' are inputs. Others are outputs.

Private Attributes

IMagneticFieldSvcm_pmgnIMF
IMagneticFieldSvcm_pmgnIMF

Detailed Description

A class to fit a TTrackBase object to a helix.


Constructor & Destructor Documentation

TCosmicFitter::TCosmicFitter const std::string &  name  ) 
 

Constructor.

00084                                                    : TMFitter(name) {
00085 //jialk
00086    StatusCode scmgn = Gaudi::svcLocator()->service("MagneticFieldSvc",m_pmgnIMF); 
00087   if(scmgn!=StatusCode::SUCCESS) { 
00088     std::cout<< "Unable to open Magnetic field service"<<std::endl;
00089   }
00090 
00091 }

TCosmicFitter::~TCosmicFitter  )  [virtual]
 

Destructor.

00093                               {
00094 }

TCosmicFitter::TCosmicFitter const std::string &  name  ) 
 

Constructor.

virtual TCosmicFitter::~TCosmicFitter  )  [virtual]
 

Destructor.


Member Function Documentation

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

dumps debug information.

Reimplemented from TMFitter.

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

dumps debug information.

Reimplemented from TMFitter.

int TCosmicFitter::dxda const TMLink link,
const Helix helix,
double  dPhi,
HepVector &  dxda,
HepVector &  dyda,
HepVector &  dzda,
int  doSagCorrection
const [private]
 

calculates dXda. 'link' and 'dPhi' are inputs. Others are outputs.

int TCosmicFitter::dxda const TMLink link,
const Helix helix,
double  dPhi,
HepVector &  dxda,
HepVector &  dyda,
HepVector &  dzda,
int  doSagCorrection
const [private]
 

calculates dXda. 'link' and 'dPhi' are inputs. Others are outputs.

int TCosmicFitter::fit TTrackBase ,
float  t0Offset
const
 

int TCosmicFitter::fit TTrackBase  )  const [virtual]
 

Implements TMFitter.

int TCosmicFitter::fit TTrackBase ,
float  t0Offset
const
 

00113                                                        {
00114 
00115 #ifdef TRKRECO_DEBUG_DETAIL
00116     std::cout << "    TCosmicFitter::fit ..." << std::endl;
00117 #endif
00118 
00119     IMdcCalibFunSvc* l_mdcCalFunSvc;
00120     Gaudi::svcLocator() -> service("MdcCalibFunSvc", l_mdcCalFunSvc);
00121 
00122     //...Check # of hits...
00123     if (b.links().length() < 5) return TFitErrorFewHits;
00124     unsigned nValid = 0;
00125     for (unsigned i = 0; i < b.links().length(); i++) {
00126         unsigned state = b.links()[i]->hit()->state();
00127         if (state & WireHitInvalidForFit) continue;
00128         if (state & WireHitFittingValid) ++nValid;
00129     }
00130     if (nValid < 5) return TFitErrorFewHits;
00131 
00132     //...Type check...
00133     //    if (b.type() != Track) return TFitUnavailable;
00134     if (b.objectType() != Track) return TFitUnavailable;
00135     TTrack & t = (TTrack &) b;
00136 
00137     //read t0 from TDS-------------------------------------//
00138     double _t0_bes = -1.;
00139     IMessageSvc *msgSvc;
00140     Gaudi::svcLocator()->service("MessageSvc", msgSvc);
00141     MsgStream log(msgSvc, "TCosmicFitter");
00142 
00143     IDataProviderSvc* eventSvc = NULL;
00144     Gaudi::svcLocator()->service("EventDataSvc", eventSvc);
00145 
00146     SmartDataPtr<RecEsTimeCol> aevtimeCol(eventSvc,"/Event/Recon/RecEsTimeCol");
00147     if (aevtimeCol) {
00148       RecEsTimeCol::iterator iter_evt = aevtimeCol->begin();
00149       _t0_bes = (*iter_evt)->getTest();
00150 //      cout<<"  tev: "<<setw(4)<<_t0_bes<<endl;
00151     }else{
00152       log << MSG::WARNING << "Could not find RecEsTimeCol" << endreq;
00153     }
00154 //    if (_t0_bes < 7.) _t0_bes = 7.;
00155     //----------------------------------------------------//
00156 
00157     //...Setup...
00158     Vector a(5), da(5);
00159     a = t.helix().a();
00160     Vector dxda(5);
00161     Vector dyda(5);
00162     Vector dzda(5);
00163     Vector dDda(5);
00164     Vector dchi2da(5);
00165     SymMatrix d2chi2d2a(5, 0);
00166     double chi2;
00167     // double chi2Old = 10e99;
00168     double chi2Old = DBL_MAX;
00169     const double convergence = Convergence;
00170     bool allAxial = true;
00171     Matrix e(3, 3);
00172     Vector f(3);
00173     int err = 0;
00174     double factor = 1.0;//jtanaka0715
00175 
00176     Vector maxDouble(5);
00177     for (unsigned i = 0; i < 5; i++) maxDouble[i] = (FLT_MAX);
00178 
00179     //...Fitting loop...
00180     unsigned nTrial = 0;
00181     while (nTrial < NTrailMax) {
00182 
00183         //...Set up...
00184         chi2 = 0.;
00185         for (unsigned j = 0; j < 5; j++) dchi2da[j] = 0.;
00186         d2chi2d2a = SymMatrix(5, 0);
00187         
00188 #if CAL_TOF_HELIX
00189         //cal the time pass through the chamber
00190         const float Rad = 81; // cm
00191         float dRho = t.helix().a()[0];
00192         float Lproj = sqrt(Rad*Rad - dRho*dRho);
00193         float tlmd = t.helix().a()[4];
00194         float fct = sqrt(1. + tlmd * tlmd);
00195 #endif
00196 
00197         //...Loop with hits...
00198         unsigned i = 0;
00199         while (TMLink * l = t.links()[i++]) {
00200             const TMDCWireHit & h = * l->hit();
00201 
00202             //...Check state...
00203             if (h.state() & WireHitInvalidForFit) continue;
00204             if (! (h.state() & WireHitFittingValid)) continue;
00205 
00206             //...Check wire...
00207             if (! nTrial)
00208                 if (h.wire()->stereo()) allAxial = false;
00209 
00210             //...Cal. closest points...
00211             int doSagCorrection = 0;
00212 //          if(nTrial && !allAxial ) doSagCorrection = 1; //Liuqg
00213             t.approach(* l, doSagCorrection);
00214             double dPhi = l->dPhi();
00215             const HepPoint3D & onTrack = l->positionOnTrack();
00216             const HepPoint3D & onWire = l->positionOnWire();
00217 
00218 #ifdef TRKRECO_DEBUG_DETAIL
00219 //          std::cout << "    in fit " << onTrack << ", " << onWire;
00220 //          h.dump();
00221 #endif      
00222 
00223             //...Obtain drift distance and its error...
00224             unsigned leftRight = WireHitRight;
00225             if (onWire.cross(onTrack).z() < 0.) leftRight = WireHitLeft;
00226             double distance = h.drift(leftRight);
00227             double eDistance = h.dDrift(leftRight);
00228             //... 
00229             if(nTrial  && !allAxial){
00230                int side = leftRight;
00231 
00232                double Tfly = _t0_bes/220.*(110.-onWire.y());
00233 #if CAL_TOF_HELIX
00234                float Rad_wire = sqrt(x[0]*x[0]+x[1]*x[1]); // cm
00235                float L_wire = sqrt(Rad_wire*Rad_wire - dRho*dRho);
00236                float flyLength = Lproj - L_wire;
00237                if (x[1]<0) flyLength = Lproj + L_wire;
00238                Tfly = flyLength/29.98*sqrt(1.0+(0.105/0.5)*(0.105/0.5))*fct; //approxiate... cm/ns
00239 #endif
00240                float time = l->hit()->reccdc()->tdc - Tfly;
00241 
00242                int wire    = h.wire()->localId();
00243                int layerId = h.wire()->layerId();
00244                float dist =  distance;
00245                float edist = eDistance;
00246                dist  = l_mdcCalFunSvc->rawTimeNoTOFToDist(time, layerId, wire, side, 0.0);
00247                edist = l_mdcCalFunSvc->getSigma(layerId, side, dist, 0.0);
00248                dist = dist/10.0; //mm->cm
00249                edist = edist/10.0;
00250 //             if(layerId<20) edist = 9999.;
00251 
00252 //zsl          calcdc_driftdist_(&doPropDelay,&wire,&side,p,x,&time,&dist,&edist);
00253 
00254                distance = (double) dist;
00255                eDistance = (double) edist;
00256 
00257                l->drift(distance,0);  //update
00258                l->drift(distance,1);
00259                l->dDrift(eDistance,0);
00260                l->dDrift(eDistance,1);
00261                l->tof(Tfly);
00262             }
00263             double eDistance2 = eDistance * eDistance;
00264 
00265             //...Residual...
00266             HepVector3D v = onTrack - onWire;
00267             double vmag = v.mag();
00268             double dDistance = vmag - distance;
00269 
00270             //...dxda...
00271             this->dxda(*l, t.helix(), dPhi, dxda, dyda, dzda, doSagCorrection);
00272 
00273             //...Chi2 related...
00274             // dDda = (v.x() * dxda + v.y() * dyda + v.z() * dzda) / vmag;
00275             HepVector3D vw = h.wire()->direction();
00276             dDda = (vmag > 0.)
00277                 ? ((v.x() * (1. - vw.x() * vw.x()) -
00278                     v.y() * vw.x() * vw.y() - v.z() * vw.x() * vw.z())
00279                    * dxda + 
00280                    (v.y() * (1. - vw.y() * vw.y()) -
00281                     v.z() * vw.y() * vw.z() - v.x() * vw.y() * vw.x())
00282                    * dyda + 
00283                    (v.z() * (1. - vw.z() * vw.z()) -
00284                     v.x() * vw.z() * vw.x() - v.y() * vw.z() * vw.y())
00285                    * dzda) / vmag
00286                 : Vector(5,0);
00287             if(vmag<=0.0) {
00288               std::cout << "    in fit " << onTrack << ", " << onWire;
00289               h.dump();
00290             }
00291             dchi2da += (dDistance / eDistance2) * dDda;
00292             d2chi2d2a += vT_times_v(dDda) / eDistance2;
00293             double pChi2 = dDistance * dDistance / eDistance2;
00294             chi2 += pChi2;
00295 
00296             //...Store results...
00297             l->update(onTrack, onWire, leftRight, pChi2);
00298         }
00299 
00300         //...Check condition...
00301         double change = chi2Old - chi2;
00302         if (fabs(change) < convergence) break;
00303         //if (change < 0.) break;
00304         //jtanaka -- from traffs -- Ozaki-san added this part to traffs.        
00305         if (change < 0.){
00306 #ifdef TRKRECO_DEBUG_DETAIL
00307           std::cout << "chi2Old, chi2=" << chi2Old <<" "<< chi2 << std::endl;
00308 #endif
00309           //change to the old value.
00310           a += factor*da;
00311 //        a[2] = 0.000000001;
00312           t._helix->a(a);
00313 
00314           chi2 = 0.;
00315           for (unsigned j = 0; j < 5; j++) dchi2da[j] = 0.;
00316           d2chi2d2a = SymMatrix(5, 0);
00317 
00318           //...Loop with hits...
00319           unsigned i = 0;
00320           while (TMLink * l = t.links()[i++]) {
00321               const TMDCWireHit & h = * l->hit();
00322             
00323             //...Check state...
00324             if (h.state() & WireHitInvalidForFit) continue;
00325             if (! (h.state() & WireHitFittingValid)) continue;
00326 
00327             //...Check wire...
00328             if (! nTrial)
00329               if (h.wire()->stereo()) allAxial = false;
00330             
00331             //...Cal. closest points...
00332             int doSagCorrection = 0;
00333 //            if( nTrial  && !allAxial ) doSagCorrection = 1;  //Liuqg
00334             t.approach(* l, doSagCorrection);
00335             double dPhi = l->dPhi();
00336             const HepPoint3D & onTrack = l->positionOnTrack();
00337             const HepPoint3D & onWire = l->positionOnWire();
00338 
00339 #ifdef TRKRECO_DEBUG_DETAIL
00340             // std::cout << "    in fit in case of change < 0. " << onTrack << ", " << onWire;
00341             // h.dump();
00342 #endif      
00343             
00344             //...Obtain drift distance and its error...
00345             unsigned leftRight = WireHitRight;
00346             if (onWire.cross(onTrack).z() < 0.) leftRight = WireHitLeft;
00347             double distance = h.drift(leftRight);
00348             double eDistance = h.dDrift(leftRight);
00349             if(nTrial  && !allAxial){
00350                int side = leftRight;
00351 
00352                double Tfly = _t0_bes/220.*(110.-onWire.y());
00353 #if CAL_TOF_HELIX
00354                float Rad_wire = sqrt(x[0]*x[0]+x[1]*x[1]); // cm
00355                float L_wire = sqrt(Rad_wire*Rad_wire - dRho*dRho);
00356                float flyLength = Lproj - L_wire;
00357                if (x[1]<0) flyLength = Lproj + L_wire;
00358                Tfly = flyLength/29.98*sqrt(1.0+(0.105/10)*(0.105/10))*fct; //approxiate... cm/ns
00359 #endif
00360 
00361                float time = l->hit()->reccdc()->tdc - Tfly;
00362 
00363                int wire    = h.wire()->localId();
00364                int layerId = h.wire()->layerId();
00365                float dist= distance;
00366                float edist = eDistance;
00367 //               int doPropDelay = 1; // 
00368 
00369                dist  = l_mdcCalFunSvc->rawTimeNoTOFToDist(time, layerId, wire, side, 0.0);
00370                edist = l_mdcCalFunSvc->getSigma(layerId, side, dist, 0.0);
00371                dist = dist/10.0; //mm->cm
00372                edist = edist/10.0;
00373 //             if (layerId<20) edist = 9999.;
00374 
00375 //zsl          calcdc_driftdist_(&doPropDelay,&wire,&side,p,x,&time,&dist,&edist);
00376 
00377                distance = (double) dist;
00378                eDistance = (double) edist;
00379 
00380                l->drift(distance,0);   //update
00381                l->drift(distance,1);
00382                l->dDrift(eDistance,0);
00383                l->dDrift(eDistance,1);
00384                l->tof(Tfly);
00385             }
00386             double eDistance2 = eDistance * eDistance;
00387 
00388             //...Residual...
00389             HepVector3D v = onTrack - onWire;
00390             double vmag = v.mag();
00391             double dDistance = vmag - distance;
00392             
00393             //...dxda...
00394             this->dxda(*l, t.helix(), dPhi, dxda, dyda, dzda, doSagCorrection);
00395             
00396             //...Chi2 related...
00397             //dDda = (v.x() * dxda + v.y() * dyda + v.z() * dzda) / vmag;
00398             HepVector3D vw = h.wire()->direction();
00399             dDda = (vmag > 0.)
00400                 ? ((v.x() * (1. - vw.x() * vw.x()) -
00401                     v.y() * vw.x() * vw.y() - v.z() * vw.x() * vw.z())
00402                    * dxda + 
00403                    (v.y() * (1. - vw.y() * vw.y()) -
00404                     v.z() * vw.y() * vw.z() - v.x() * vw.y() * vw.x())
00405                    * dyda + 
00406                    (v.z() * (1. - vw.z() * vw.z()) -
00407                     v.x() * vw.z() * vw.x() - v.y() * vw.z() * vw.y())
00408                    * dzda) / vmag
00409                 : Vector(5,0);
00410             if(vmag<=0.0) {
00411               std::cout << "    in fit " << onTrack << ", " << onWire;
00412               h.dump();
00413             }
00414             dchi2da += (dDistance / eDistance2) * dDda;
00415             d2chi2d2a += vT_times_v(dDda) / eDistance2;
00416             double pChi2 = dDistance * dDistance / eDistance2;
00417             chi2 += pChi2;
00418             
00419             //...Store results...
00420             l->update(onTrack, onWire, leftRight, pChi2);
00421           }
00422           //break;
00423           factor *= 0.75;
00424 #ifdef TRKRECO_DEBUG_DETAIL 
00425           std::cout << "factor = " << factor << std::endl;
00426           std::cout << "chi2 = " << chi2 << std::endl;
00427 #endif
00428           if(factor < 0.01)break;
00429         }
00430 
00431         chi2Old = chi2;
00432 
00433         //...Cal. helix parameters for next loop...
00434         if (allAxial) {
00435             f = dchi2da.sub(1, 3);
00436             e = d2chi2d2a.sub(1, 3);
00437             f = solve(e, f);
00438             da[0] = f[0];
00439             da[1] = f[1];
00440             da[2] = f[2];
00441             da[3] = 0.;
00442             da[4] = 0.;
00443         }
00444         else {
00445             da = solve(d2chi2d2a, dchi2da);
00446         }
00447 
00448 #ifdef TRKRECO_DEBUG_DETAIL
00449         //std::cout << "    fit " << nTrial << " : " << da << std::endl;
00450         //std::cout << "        d2chi " << d2chi2d2a << std::endl;
00451         //std::cout << "        dchi2 " << dchi2da << std::endl;
00452 #endif      
00453 
00454         a -= factor*da;
00455 //      a[2] = 0.000000001;
00456         t._helix->a(a);
00457         ++nTrial;
00458     }
00459 
00460     //...Cal. error matrix...
00461     SymMatrix Ea(5, 0);
00462     unsigned dim;
00463     if (allAxial) {
00464         dim = 3;
00465         SymMatrix Eb(3, 0), Ec(3, 0);
00466         Eb = d2chi2d2a.sub(1, 3);
00467         Ec = Eb.inverse(err);
00468         Ea[0][0] = Ec[0][0];
00469         Ea[0][1] = Ec[0][1];
00470         Ea[0][2] = Ec[0][2];
00471         Ea[1][1] = Ec[1][1];
00472         Ea[1][2] = Ec[1][2];
00473         Ea[2][2] = Ec[2][2];
00474     }
00475     else {
00476         dim = 5;
00477         Ea = d2chi2d2a.inverse(err);
00478     }
00479 
00480     //...Store information...
00481     if (! err) {
00482         t._helix->a(a);
00483         t._helix->Ea(Ea);
00484     }
00485     else {
00486         err = TFitFailed;
00487     }
00488 
00489     t._ndf = nValid - dim;
00490     t._chi2 = chi2;
00491     // t._fitted = true;
00492     
00493     return err;
00494 }

int TCosmicFitter::fit TTrackBase  )  const [virtual]
 

Implements TMFitter.

00097                                        {
00098 
00099 //  double t0_bes;
00100 //  TrkReco::t0(t0_bes);
00101 //  const double t0_bes = TrkReco::t0();
00102 
00103     //...Already fitted ?...
00104     if (b.fitted()) return TFitAlreadyFitted;
00105 
00106     int err = fit(b, 0.);
00107     if (! err) b._fitted = true;
00108 
00109     return err;
00110 }

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

sets the fitted flag. (Bad implementation)

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

sets the fitted flag. (Bad implementation)

00024                                       {
00025     t._fitted = true;
00026 }

int TCosmicFitter::fitWithCathode TTrackBase ,
float  t0Offset = 0.,
float  windowSize = 0.6,
int  SysCorr = 0
 

int TCosmicFitter::fitWithCathode TTrackBase ,
float  t0Offset = 0.,
float  windowSize = 0.6,
int  SysCorr = 0
 

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

returns name.

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

returns name.

00073                          {
00074     return _name;
00075 }


Member Data Documentation

IMagneticFieldSvc* TCosmicFitter::m_pmgnIMF [private]
 

IMagneticFieldSvc* TCosmicFitter::m_pmgnIMF [private]
 


The documentation for this class was generated from the following files:
Generated on Wed Feb 2 18:51:11 2011 for BOSS6.5.5 by  doxygen 1.3.9.1