00001 #include "RecoTracker/CkfPattern/interface/CkfTrajectoryBuilder.h"
00002
00003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00004
00005 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00006
00007 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
00008 #include "TrackingTools/PatternTools/interface/TrajectoryStateUpdator.h"
00009 #include "TrackingTools/KalmanUpdators/interface/Chi2MeasurementEstimatorBase.h"
00010
00011 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00012 #include "TrackingTools/PatternTools/interface/TrajMeasLessEstim.h"
00013 #include "TrackingTools/TrajectoryState/interface/BasicSingleTrajectoryState.h"
00014 #include "RecoTracker/MeasurementDet/interface/MeasurementTracker.h"
00015 #include "TrackingTools/MeasurementDet/interface/LayerMeasurements.h"
00016
00017
00018 #include "RecoTracker/CkfPattern/src/RecHitIsInvalid.h"
00019 #include "RecoTracker/CkfPattern/interface/TrajCandLess.h"
00020
00021 #include "RecoTracker/TkDetLayers/interface/GeometricSearchTracker.h"
00022
00023 #include "RecoTracker/CkfPattern/interface/IntermediateTrajectoryCleaner.h"
00024 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00025 #include "TrackingTools/PatternTools/interface/TransverseImpactPointExtrapolator.h"
00026
00027 #include "FWCore/Services/interface/UpdaterService.h"
00028
00029 using namespace std;
00030
00031
00032 CkfTrajectoryBuilder::
00033 CkfTrajectoryBuilder(const edm::ParameterSet& conf,
00034 const TrajectoryStateUpdator* updator,
00035 const Propagator* propagatorAlong,
00036 const Propagator* propagatorOpposite,
00037 const Chi2MeasurementEstimatorBase* estimator,
00038 const TransientTrackingRecHitBuilder* recHitBuilder,
00039 const MeasurementTracker* measurementTracker,
00040 const TrajectoryFilter* filter):
00041
00042 BaseCkfTrajectoryBuilder(conf,
00043 updator, propagatorAlong,propagatorOpposite,
00044 estimator, recHitBuilder, measurementTracker,filter)
00045 {
00046 theMaxCand = conf.getParameter<int>("maxCand");
00047 theLostHitPenalty = conf.getParameter<double>("lostHitPenalty");
00048 theIntermediateCleaning = conf.getParameter<bool>("intermediateCleaning");
00049 theAlwaysUseInvalidHits = conf.getParameter<bool>("alwaysUseInvalidHits");
00050
00051
00052
00053
00054
00055
00056
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066 CkfTrajectoryBuilder::TrajectoryContainer
00067 CkfTrajectoryBuilder::trajectories(const TrajectorySeed& seed) const
00068 {
00069 TrajectoryContainer result;
00070 result.reserve(5);
00071 trajectories(seed, result);
00072 return result;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 void
00134 CkfTrajectoryBuilder::trajectories(const TrajectorySeed& seed, CkfTrajectoryBuilder::TrajectoryContainer &result) const
00135 {
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 TempTrajectory startingTraj = createStartingTrajectory( seed );
00151
00154 limitedCandidates( startingTraj, result);
00155
00156
00157
00158
00159
00160
00161
00162 }
00163
00164 void CkfTrajectoryBuilder::
00165 limitedCandidates( TempTrajectory& startingTraj,
00166 TrajectoryContainer& result) const
00167 {
00168 TempTrajectoryContainer candidates;
00169 candidates.push_back( startingTraj);
00170 limitedCandidates(candidates,result);
00171 }
00172
00173 void CkfTrajectoryBuilder::
00174 limitedCandidates( TempTrajectoryContainer &candidates,
00175 TrajectoryContainer& result) const
00176 {
00177 unsigned int nIter=1;
00178
00179 TempTrajectoryContainer newCand;
00180
00181
00182 while ( !candidates.empty()) {
00183
00184 newCand.clear();
00185 for (TempTrajectoryContainer::iterator traj=candidates.begin();
00186 traj!=candidates.end(); traj++) {
00187 std::vector<TM> meas;
00188 findCompatibleMeasurements(*traj, meas);
00189
00190
00191 if(!analyzeMeasurementsDebugger(*traj,meas,
00192 theMeasurementTracker,
00193 theForwardPropagator,theEstimator,
00194 theTTRHBuilder)) return;
00195
00196
00197 if ( meas.empty()) {
00198 if ( qualityFilter( *traj)) addToResult( *traj, result);
00199 }
00200 else {
00201 std::vector<TM>::const_iterator last;
00202 if ( theAlwaysUseInvalidHits) last = meas.end();
00203 else {
00204 if (meas.front().recHit()->isValid()) {
00205 last = find_if( meas.begin(), meas.end(), RecHitIsInvalid());
00206 }
00207 else last = meas.end();
00208 }
00209
00210 for( std::vector<TM>::const_iterator itm = meas.begin();
00211 itm != last; itm++) {
00212 TempTrajectory newTraj = *traj;
00213 updateTrajectory( newTraj, *itm);
00214
00215 if ( toBeContinued(newTraj)) {
00216 newCand.push_back(newTraj);
00217 }
00218 else {
00219 if ( qualityFilter(newTraj)) addToResult( newTraj, result);
00221 }
00222 }
00223 }
00224
00225 if ((int)newCand.size() > theMaxCand) {
00226 sort( newCand.begin(), newCand.end(), TrajCandLess<TempTrajectory>(theLostHitPenalty));
00227 newCand.erase( newCand.begin()+theMaxCand, newCand.end());
00228 }
00229 }
00230
00231 if (theIntermediateCleaning) IntermediateTrajectoryCleaner::clean(newCand);
00232
00233 candidates.swap(newCand);
00234
00235 LogDebug("CkfPattern") <<result.size()<<" candidates after "<<nIter++<<" CKF iteration: \n"
00236 <<PrintoutHelper::dumpCandidates(result)
00237 <<"\n "<<candidates.size()<<" running candidates are: \n"
00238 <<PrintoutHelper::dumpCandidates(candidates);
00239
00240 }
00241 }
00242
00243
00244
00245 void CkfTrajectoryBuilder::updateTrajectory( TempTrajectory& traj,
00246 const TM& tm) const
00247 {
00248 TSOS predictedState = tm.predictedState();
00249 TM::ConstRecHitPointer hit = tm.recHit();
00250
00251 if ( hit->isValid()) {
00252 TM tmp = TM( predictedState, theUpdator->update( predictedState, *hit),
00253 hit, tm.estimate(), tm.layer());
00254 traj.push(tmp );
00255 }
00256 else {
00257 traj.push( TM( predictedState, hit, 0, tm.layer()));
00258 }
00259 }
00260
00261
00262 void
00263 CkfTrajectoryBuilder::findCompatibleMeasurements( const TempTrajectory& traj,
00264 std::vector<TrajectoryMeasurement> & result) const
00265 {
00266 int invalidHits = 0;
00267 std::pair<TSOS,std::vector<const DetLayer*> > stateAndLayers = findStateAndLayers(traj);
00268 if (stateAndLayers.second.empty()) return;
00269
00270 vector<const DetLayer*>::iterator layerBegin = stateAndLayers.second.begin();
00271 vector<const DetLayer*>::iterator layerEnd = stateAndLayers.second.end();
00272 LogDebug("CkfPattern")<<"looping on "<< stateAndLayers.second.size()<<" layers.";
00273 for (vector<const DetLayer*>::iterator il = layerBegin;
00274 il != layerEnd; il++) {
00275
00276 LogDebug("CkfPattern")<<"looping on a layer in findCompatibleMeasurements.\n last layer: "<<traj.lastLayer()<<" current layer: "<<(*il);
00277
00278 TSOS stateToUse = stateAndLayers.first;
00279 if ((*il)==traj.lastLayer())
00280 {
00281 LogDebug("CkfPattern")<<" self propagating in findCompatibleMeasurements.\n from: \n"<<stateToUse;
00282
00283
00284 TransverseImpactPointExtrapolator middle;
00285 GlobalPoint center(0,0,0);
00286 stateToUse = middle.extrapolate(stateToUse, center, *theForwardPropagator);
00287
00288 if (!stateToUse.isValid()) continue;
00289 LogDebug("CkfPattern")<<"to: "<<stateToUse;
00290 }
00291
00292 vector<TrajectoryMeasurement> tmp = theLayerMeasurements->measurements((**il),stateToUse, *theForwardPropagator, *theEstimator);
00293
00294 if ( !tmp.empty()) {
00295 if ( result.empty()) result = tmp;
00296 else {
00297
00298 result.insert( result.end()-invalidHits, tmp.begin(), tmp.end());
00299 }
00300 invalidHits++;
00301 }
00302 }
00303
00304
00305 if ( result.size() > 1) {
00306 sort( result.begin(), result.end()-invalidHits, TrajMeasLessEstim());
00307 }
00308
00309 LogDebug("CkfPattern")<<"starting from:\n"
00310 <<"x: "<<stateAndLayers.first.globalPosition()<<"\n"
00311 <<"p: "<<stateAndLayers.first.globalMomentum()<<"\n"
00312 <<PrintoutHelper::dumpMeasurements(result);
00313
00314 #ifdef DEBUG_INVALID
00315 bool afterInvalid = false;
00316 for (vector<TM>::const_iterator i=result.begin();
00317 i!=result.end(); i++) {
00318 if ( ! i->recHit().isValid()) afterInvalid = true;
00319 if (afterInvalid && i->recHit().isValid()) {
00320 edm::LogError("CkfPattern") << "CkfTrajectoryBuilder error: valid hit after invalid!" ;
00321 }
00322 }
00323 #endif
00324
00325
00326
00327 }
00328