00001
00002
00003 #include <stdexcept>
00004 #include <math.h>
00005 #include "StThreeVectorF.hh"
00006 #include "StThreeVectorD.hh"
00007 #include "StThreeVector.hh"
00008 #include "StiHit.h"
00009 #include "StiHitContainer.h"
00010 #include "StiKalmanTrack.h"
00011 #include "StiDetector.h"
00012 #include "StiPlacement.h"
00013 #include "StiDetectorContainer.h"
00014 #include "StiLocalTrackSeedFinder.h"
00015 #include "StiSortedHitIterator.h"
00016 #include "StiMasterDetectorBuilder.h"
00017
00018 ostream& operator<<(ostream&, const StiDetector&);
00019
00020
00021 StiLocalTrackSeedFinder::StiLocalTrackSeedFinder(const string& name,
00022 const string& description,
00023 Factory<StiKalmanTrack> * trackFactory,
00024 StiHitContainer * hitContainer,
00025 StiDetectorContainer * detectorContainer)
00026 : StiTrackFinder(),
00027 _reset(true),
00028 _trackFactory(trackFactory),
00029 _hitContainer(hitContainer),
00030 _detectorContainer(detectorContainer)
00031 {
00032 fRxyMin=0;
00033 cout <<"StiLocalTrackSeedFinder::StiLocalTrackSeedFinder() -I- Started/Done"<<endl;
00034 }
00035
00036
00037 StiLocalTrackSeedFinder::~StiLocalTrackSeedFinder()
00038 {
00039 cout <<"StiLocalTrackSeedFinder::~StiLocalTrackSeedFinder() -I- Started/Done"<<endl;
00040 }
00041
00042
00047
00048 StiTrack* StiLocalTrackSeedFinder::findTrack(double rMin)
00049 {
00050 fRxyMin = rMin;
00051 StiKalmanTrack* track = 0;
00052 if (isReset())
00053 {
00054 cout << "StiLocalTrackSeedFinder::findTrack() -I- Getting iterator" << endl;
00055
00056 _hitIter = StiSortedHitIterator(_hitContainer,_detectorContainer->begin(),_detectorContainer->end());
00057 }
00058 for (;_hitIter!=StiSortedHitIterator() && track==0;++_hitIter)
00059 {
00060 try
00061 {
00062 StiHit *hit = &(*_hitIter);
00063 if (hit->timesUsed()) continue;
00064 if (fRxyMin && pow(hit->x(),2)+pow(hit->y(),2)<fRxyMin*fRxyMin) continue;
00065 track = makeTrack(&*_hitIter);
00066 }
00067 catch(runtime_error & rte )
00068 {
00069
00070 }
00071 }
00072
00073 fRxyMin = 0;
00074 return track;
00075 }
00076
00077
00081 bool StiLocalTrackSeedFinder::extendHit(StiHit& hit)
00082 {
00083
00084 _detectorContainer->setToDetector( hit.detector() );
00085
00086 if ( _detectorContainer->moveIn()==false ) return false;
00087 const StiDetector* d = _detectorContainer->getCurrentDetector();
00088 StiPlacement * p = d->getPlacement();
00089 if (p->getLayerRadius() < fRxyMin) return false;
00090 StiHit* closestHit = _hitContainer->getNearestHit(p->getLayerRadius(),
00091 p->getLayerAngle(),
00092 hit.y(), hit.z(),
00093 StiLocalTrackSeedFinderParameters::instance()->deltaY(),
00094 StiLocalTrackSeedFinderParameters::instance()->deltaZ());
00095
00096 if (!closestHit ) return false;
00097 _seedHits.push_back(closestHit);
00098 return true;
00099 }
00100
00101
00104 StiKalmanTrack* StiLocalTrackSeedFinder::makeTrack(StiHit* hit)
00105 {
00106
00107 StiKalmanTrack* track = 0;
00108 _seedHits.clear();
00109 _seedHits.push_back(hit);
00110
00111 bool go=true;
00112 StiLocalTrackSeedFinderParameters *seedPars=StiLocalTrackSeedFinderParameters::instance();
00113 int iSeedLength = seedPars->seedLength();
00114 int iExtrapMaxLength = seedPars->extrapMaxLength();
00115 int iExtrapMinLength = seedPars->extrapMinLength();
00116 int iMaxSkipped = seedPars->maxSkipped();
00117
00118 while ( go && (int)_seedHits.size()<iSeedLength)
00119 {
00120 go = extendHit( *_seedHits.back() );
00121 }
00122
00123
00124 if ( (int)_seedHits.size()<iSeedLength )
00125 {
00126 return track;
00127 }
00128
00129 _skipped = 0;
00130 go=true;
00131 while ( go && _skipped<=iMaxSkipped &&
00132 (int)_seedHits.size()<=( iSeedLength+iExtrapMaxLength))
00133 {
00134 go = extrapolate();
00135 }
00136
00137
00138
00139 if ( (int)_seedHits.size()<( iSeedLength+iExtrapMinLength) )
00140 {
00141
00142 return track;
00143 }
00144 track = initializeTrack(_trackFactory->getInstance());
00145 return track;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 bool StiLocalTrackSeedFinder::extrapolate()
00178 {
00179
00180
00181
00182 const StiHit* hit1 = *( _seedHits.begin()+_seedHits.size() -2 );
00183 const StiHit* hit2 = _seedHits.back();
00184
00185
00186
00187 double r1 = hit1->x();
00188 double y1 = hit1->y();
00189 double z1 = hit1->z();
00190 double r2 = hit2->x();
00191 double y2 = hit2->y();
00192 double z2 = hit2->z();
00193
00194 double dr = r2-r1;
00195 double dy = y2-y1;
00196 double dz = z2-z1;
00197 assert (fabs(dr) >1.e-3);
00198
00199
00200 _detectorContainer->setToDetector( hit2->detector());
00201
00202 for (int i=0; i<=_skipped; ++i)
00203 {
00204 if ( _detectorContainer->moveIn()==false)
00205 {
00206
00207 return false;
00208 }
00209 }
00210
00211 const StiDetector* newLayer = **_detectorContainer;
00212 double r3 = newLayer->getPlacement()->getNormalRadius();
00213
00214
00215
00216 if (r3<=25.) { return false; }
00217
00218
00219
00220
00221
00222 double y3 = y1 + dy/dr*(r3-r1);
00223
00224
00225 double beta_ry = atan2(dr, dy);
00226 double rho_ry = ::sqrt(dr*dr + dy*dy);
00227 double alpha_ry = atan2(StiLocalTrackSeedFinderParameters::instance()->extrapDeltaY(), 2.*rho_ry);
00228 double tanplus_ry = tan(beta_ry+alpha_ry);
00229 double tanminus_ry = tan(beta_ry-alpha_ry);
00230 if (tanplus_ry==0. || tanminus_ry==0.)
00231 cout<<"StiLocalTrackSeedFidner::extrapolate(). -W- tanplus_ry==0. || tanminus_ry==0."<<endl;
00232
00233 double y3_minus = (r3-r1)/tanplus_ry + y1;
00234 double y3_plus = (r3-r1)/tanminus_ry + y1;
00235
00236 double z3 = z1 + dz/dr*(r3-r1);
00237
00238 double beta_rz = atan2(dr, dz);
00239 double rho_rz = ::sqrt(dr*dr + dz*dz);
00240 double alpha_rz = atan2(StiLocalTrackSeedFinderParameters::instance()->extrapDeltaZ(), 2.*rho_rz);
00241 double tanplus_rz = tan(beta_rz+alpha_rz);
00242 double tanminus_rz = tan(beta_rz-alpha_rz);
00243 if (tanplus_rz==0. || tanminus_rz==0.)
00244 cout<<"StiLocalTrackSeedFidner::extrapolate(). -W- tanplus_rz==0. || tanminus_rz==0."<<endl;
00245 double z3_minus = (r3-r1)/tanplus_rz + z1;
00246 double z3_plus = (r3-r1)/tanminus_rz + z1;
00247
00248
00249
00250
00251
00252
00253
00254 StiPlacement * p = newLayer->getPlacement();
00255 StiHit* closestHit = _hitContainer->getNearestHit(p->getLayerRadius(),
00256 p->getLayerAngle(),
00257 y3,z3,
00258 fabs(y3_plus-y3_minus) /2.,
00259 fabs(z3_plus-z3_minus) /2.);
00260 if ( !closestHit )
00261 {
00262
00263 ++_skipped;
00264 return true;
00265 }
00266 _skipped=0;
00267 _seedHits.push_back(closestHit);
00268 return true;
00269 }
00270
00271
00272
00275 StiKalmanTrack* StiLocalTrackSeedFinder::initializeTrack(StiKalmanTrack* track)
00276 {
00277 bool status = fit(track);
00278 if (!status) track = 0;
00279 return track;
00280 }
00281
00282
00283 bool StiLocalTrackSeedFinder::fit(StiKalmanTrack* track)
00284 {
00285 int ierr = track->initialize(_seedHits);
00286 return (ierr==0);
00287 }
00288
00289 #if 0
00290
00291 void StiLocalTrackSeedFinder::calculate(StiKalmanTrack* track)
00292 {
00293
00294 const StThreeVectorF& outside = _seedHits.front()->globalPosition();
00295 const StThreeVectorF& middle = (*(_seedHits.begin()+_seedHits.size()/2))->globalPosition();
00296 const StThreeVectorF& inside = _seedHits.back()->globalPosition();
00297
00298 _helixCalculator.calculate( StThreeVector<double>( inside.x(), inside.y(), inside.z() ),
00299 StThreeVector<double>( middle.x(), middle.y(), middle.z() ),
00300 StThreeVector<double>( outside.x(), outside.y(), outside.z() ) );
00301
00302
00303
00304
00305
00306
00307 track->initialize( _helixCalculator.curvature(),
00308 _helixCalculator.tanLambda(),
00309 StThreeVectorD(_helixCalculator.xCenter(),_helixCalculator.yCenter(), 0.),
00310 _seedHits);
00311
00312 }
00313
00314
00315 void StiLocalTrackSeedFinder::calculateWithOrigin(StiKalmanTrack* track)
00316 {
00317
00318 const StThreeVectorF& outside = _seedHits.front()->globalPosition();
00319 const StThreeVectorF& middle = _seedHits.back()->globalPosition();
00320
00321
00322 _helixCalculator.calculate( StThreeVector<double>(0., 0., 0.),
00323 StThreeVector<double>( middle.x(), middle.y(), middle.z() ),
00324 StThreeVector<double>( outside.x(), outside.y(), outside.z() ) );
00325
00326
00327
00328
00329
00330
00331
00332
00333 track->initialize( _helixCalculator.curvature(), _helixCalculator.tanLambda(),
00334 StThreeVectorD(_helixCalculator.xCenter(), _helixCalculator.yCenter(), 0.),
00335 _seedHits);
00336 }
00337 #endif
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 void StiLocalTrackSeedFinder::print() const
00357 {
00358 cout <<"StiLocalTrackSeedFinder::print() -I- ";
00359 for (vector<StiDetector*>::const_iterator it=_detectorContainer->begin();
00360 it!=_detectorContainer->end();
00361 ++it)
00362 {
00363 cout << **it <<endl;
00364 }
00365 cout <<"\n Search Window in Y:\t"<<StiLocalTrackSeedFinderParameters::instance()->deltaY()<<endl;
00366 cout <<"\n Search Window in Z:\t"<<StiLocalTrackSeedFinderParameters::instance()->deltaZ()<<endl;
00367 }
00368
00369
00370 ostream& operator<<(ostream& os, const StiLocalTrackSeedFinder & f)
00371 {
00372 return os << " StiLocalTrackSeedFinder " << endl;
00373 }