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 using namespace std;
00028
00029
00030 std::string dumpMeasurement(const TrajectoryMeasurement & tm)
00031 {
00032 std::stringstream buffer;
00033 buffer<<"layer pointer: "<<tm.layer()<<"\n"
00034 <<"estimate: "<<tm.estimate()<<"\n"
00035 <<"forward state: \n"
00036 <<"x: "<<tm.forwardPredictedState().globalPosition()<<"\n"
00037 <<"p: "<<tm.forwardPredictedState().globalMomentum()<<"\n"
00038 <<"geomdet pointer from rechit: "<<tm.recHit()->det()<<"\n"
00039 <<"detId: "<<tm.recHit()->geographicalId().rawId();
00040 if (tm.recHit()->isValid()){
00041 buffer<<"\n hit global x: "<<tm.recHit()->globalPosition()
00042 <<"\n hit global error: "<<tm.recHit()->globalPositionError().matrix()
00043 <<"\n hit local x:"<<tm.recHit()->localPosition()
00044 <<"\n hit local error"<<tm.recHit()->localPositionError();
00045 }
00046 return buffer.str();
00047 }
00048 std::string dumpMeasurements(const std::vector<TrajectoryMeasurement> & v)
00049 {
00050 std::stringstream buffer;
00051 buffer<<v.size()<<" total measurements\n";
00052 for (std::vector<TrajectoryMeasurement>::const_iterator it = v.begin(); it!=v.end();++it){
00053 buffer<<dumpMeasurement(*it);
00054 buffer<<"\n";}
00055 return buffer.str();
00056 }
00057
00058 std::string dumpCandidates(CkfTrajectoryBuilder::TempTrajectoryContainer & candidates){
00059 std::stringstream buffer;
00060 uint ic=0;
00061 for (CkfTrajectoryBuilder::TempTrajectoryContainer::const_iterator traj=candidates.begin();
00062 traj!=candidates.end(); traj++) {
00063 buffer<<ic++<<"] ";
00064 if (!traj->measurements().empty()){
00065 const TrajectoryMeasurement & last = traj->lastMeasurement();
00066 const TrajectoryStateOnSurface & tsos = last.updatedState();
00067 buffer<<"with: "<<traj->measurements().size()<<" measurements. Last state\n x: "<<tsos.globalPosition()<<"\n p: "<<tsos.globalMomentum()<<"\n";
00068 }
00069 else{
00070 buffer<<" no measurement. \n";}
00071 }
00072 return buffer.str();
00073 }
00074 std::string dumpCandidates(CkfTrajectoryBuilder::TrajectoryContainer & candidates){
00075 std::stringstream buffer;
00076 uint ic=0;
00077 for (CkfTrajectoryBuilder::TrajectoryContainer::const_iterator traj=candidates.begin();
00078 traj!=candidates.end(); traj++) {
00079 buffer<<ic++<<"] ";
00080 if (!traj->measurements().empty()){
00081 const TrajectoryMeasurement & last = traj->lastMeasurement();
00082 const TrajectoryStateOnSurface & tsos = last.updatedState();
00083 buffer<<"with: "<<traj->measurements().size()<<" measurements. Last state\n x: "<<tsos.globalPosition()<<"\n p: "<<tsos.globalMomentum()<<"\n";
00084 }
00085 else{
00086 buffer<<" no measurement. \n";}
00087 }
00088 return buffer.str();
00089 }
00090
00091
00092 CkfTrajectoryBuilder::
00093 CkfTrajectoryBuilder(const edm::ParameterSet& conf,
00094 const TrajectoryStateUpdator* updator,
00095 const Propagator* propagatorAlong,
00096 const Propagator* propagatorOpposite,
00097 const Chi2MeasurementEstimatorBase* estimator,
00098 const TransientTrackingRecHitBuilder* recHitBuilder,
00099 const MeasurementTracker* measurementTracker,
00100 const TrajectoryFilter* filter):
00101
00102 BaseCkfTrajectoryBuilder(conf,
00103 updator, propagatorAlong,propagatorOpposite,
00104 estimator, recHitBuilder, measurementTracker,filter)
00105 {
00106 theMaxCand = conf.getParameter<int>("maxCand");
00107 theLostHitPenalty = conf.getParameter<double>("lostHitPenalty");
00108 theIntermediateCleaning = conf.getParameter<bool>("intermediateCleaning");
00109 theAlwaysUseInvalidHits = conf.getParameter<bool>("alwaysUseInvalidHits");
00110 }
00111
00112 void CkfTrajectoryBuilder::setEvent(const edm::Event& event) const
00113 {
00114 theMeasurementTracker->update(event);
00115 }
00116
00117 CkfTrajectoryBuilder::TrajectoryContainer
00118 CkfTrajectoryBuilder::trajectories(const TrajectorySeed& seed) const
00119 {
00120 TrajectoryContainer result;
00121 result.reserve(5);
00122 trajectories(seed, result);
00123 return result;
00124 }
00125
00126 void
00127 CkfTrajectoryBuilder::trajectories(const TrajectorySeed& seed, CkfTrajectoryBuilder::TrajectoryContainer &result) const
00128 {
00129
00130
00131 TempTrajectory startingTraj = createStartingTrajectory( seed );
00132
00135 limitedCandidates( startingTraj, result);
00136
00137
00138 }
00139
00140
00141 void CkfTrajectoryBuilder::
00142 limitedCandidates( TempTrajectory& startingTraj,
00143 TrajectoryContainer& result) const
00144 {
00145 uint nIter=1;
00146 TempTrajectoryContainer candidates;
00147 TempTrajectoryContainer newCand;
00148 candidates.push_back( startingTraj);
00149
00150 while ( !candidates.empty()) {
00151
00152 newCand.clear();
00153 for (TempTrajectoryContainer::iterator traj=candidates.begin();
00154 traj!=candidates.end(); traj++) {
00155 std::vector<TM> meas;
00156 findCompatibleMeasurements(*traj, meas);
00157
00158
00159 if(!analyzeMeasurementsDebugger(*traj,meas,
00160 theMeasurementTracker,
00161 theForwardPropagator,theEstimator,
00162 theTTRHBuilder)) return;
00163
00164
00165 if ( meas.empty()) {
00166 if ( qualityFilter( *traj)) addToResult( *traj, result);
00167 }
00168 else {
00169 std::vector<TM>::const_iterator last;
00170 if ( theAlwaysUseInvalidHits) last = meas.end();
00171 else {
00172 if (meas.front().recHit()->isValid()) {
00173 last = find_if( meas.begin(), meas.end(), RecHitIsInvalid());
00174 }
00175 else last = meas.end();
00176 }
00177
00178 for( std::vector<TM>::const_iterator itm = meas.begin();
00179 itm != last; itm++) {
00180 TempTrajectory newTraj = *traj;
00181 updateTrajectory( newTraj, *itm);
00182
00183 if ( toBeContinued(newTraj)) {
00184 newCand.push_back(newTraj);
00185 }
00186 else {
00187 if ( qualityFilter(newTraj)) addToResult( newTraj, result);
00189 }
00190 }
00191 }
00192
00193 if ((int)newCand.size() > theMaxCand) {
00194 sort( newCand.begin(), newCand.end(), TrajCandLess<TempTrajectory>(theLostHitPenalty));
00195 newCand.erase( newCand.begin()+theMaxCand, newCand.end());
00196 }
00197 }
00198
00199 if (theIntermediateCleaning) IntermediateTrajectoryCleaner::clean(newCand);
00200
00201 candidates.swap(newCand);
00202
00203 LogDebug("CkfPattern") <<result.size()<<" candidates after "<<nIter++<<" CKF iteration: \n"
00204 <<dumpCandidates(result)
00205 <<"\n "<<candidates.size()<<" running candidates are: \n"
00206 <<dumpCandidates(candidates);
00207
00208 }
00209 }
00210
00211
00212
00213 void CkfTrajectoryBuilder::updateTrajectory( TempTrajectory& traj,
00214 const TM& tm) const
00215 {
00216 TSOS predictedState = tm.predictedState();
00217 TM::ConstRecHitPointer hit = tm.recHit();
00218
00219 if ( hit->isValid()) {
00220 TM tmp = TM( predictedState, theUpdator->update( predictedState, *hit),
00221 hit, tm.estimate(), tm.layer());
00222 traj.push(tmp );
00223 }
00224 else {
00225 traj.push( TM( predictedState, hit, 0, tm.layer()));
00226 }
00227 }
00228
00229
00230 void
00231 CkfTrajectoryBuilder::findCompatibleMeasurements( const TempTrajectory& traj,
00232 std::vector<TrajectoryMeasurement> & result) const
00233 {
00234 int invalidHits = 0;
00235 std::pair<TSOS,std::vector<const DetLayer*> > stateAndLayers = findStateAndLayers(traj);
00236 if (stateAndLayers.second.empty()) return;
00237
00238 vector<const DetLayer*>::iterator layerBegin = stateAndLayers.second.begin();
00239 vector<const DetLayer*>::iterator layerEnd = stateAndLayers.second.end();
00240 LogDebug("CkfPattern")<<"looping on "<< stateAndLayers.second.size()<<" layers.";
00241 for (vector<const DetLayer*>::iterator il = layerBegin;
00242 il != layerEnd; il++) {
00243
00244 LogDebug("CkfPattern")<<"looping on a layer in findCompatibleMeasurements.\n last layer: "<<traj.lastLayer()<<" current layer: "<<(*il);
00245
00246 TSOS stateToUse = stateAndLayers.first;
00247 if ((*il)==traj.lastLayer())
00248 {
00249 LogDebug("CkfPattern")<<" self propagating in findCompatibleMeasurements.\n from: \n"<<stateToUse;
00250
00251
00252 TransverseImpactPointExtrapolator middle;
00253 GlobalPoint center(0,0,0);
00254 stateToUse = middle.extrapolate(stateToUse, center, *theForwardPropagator);
00255
00256 if (!stateToUse.isValid()) continue;
00257 LogDebug("CkfPattern")<<"to: "<<stateToUse;
00258 }
00259
00260 vector<TrajectoryMeasurement> tmp = theLayerMeasurements->measurements((**il),stateAndLayers.first, *theForwardPropagator, *theEstimator);
00261
00262 if ( !tmp.empty()) {
00263 if ( result.empty()) result = tmp;
00264 else {
00265
00266 result.insert( result.end()-invalidHits, tmp.begin(), tmp.end());
00267 }
00268 invalidHits++;
00269 }
00270 }
00271
00272
00273 if ( result.size() > 1) {
00274 sort( result.begin(), result.end()-invalidHits, TrajMeasLessEstim());
00275 }
00276
00277 LogDebug("CkfPattern")<<"starting from:\n"
00278 <<"x: "<<stateAndLayers.first.globalPosition()<<"\n"
00279 <<"p: "<<stateAndLayers.first.globalMomentum()<<"\n"
00280 <<dumpMeasurements(result);
00281
00282 #ifdef DEBUG_INVALID
00283 bool afterInvalid = false;
00284 for (vector<TM>::const_iterator i=result.begin();
00285 i!=result.end(); i++) {
00286 if ( ! i->recHit().isValid()) afterInvalid = true;
00287 if (afterInvalid && i->recHit().isValid()) {
00288 edm::LogError("CkfPattern") << "CkfTrajectoryBuilder error: valid hit after invalid!" ;
00289 }
00290 }
00291 #endif
00292
00293
00294
00295 }
00296