CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/TrackingTools/TrackFitters/src/KFTrajectorySmoother.cc

Go to the documentation of this file.
00001 #include "TrackingTools/TrackFitters/interface/KFTrajectorySmoother.h"
00002 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHit.h"
00003 #include "TrackingTools/TrackFitters/interface/TrajectoryStateWithArbitraryError.h"
00004 #include "TrackingTools/TrackFitters/interface/TrajectoryStateCombiner.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 #include "DataFormats/SiStripDetId/interface/TIBDetId.h"
00007 #include "DataFormats/SiStripDetId/interface/TOBDetId.h"
00008 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00009 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00010 #include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
00011 #include "DataFormats/SiPixelDetId/interface/PXFDetId.h"
00012 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
00013 #include "DataFormats/MuonDetId/interface/DTWireId.h"
00014 #include "DataFormats/MuonDetId/interface/RPCDetId.h"
00015 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
00016 
00017 KFTrajectorySmoother::~KFTrajectorySmoother() {
00018 
00019   delete thePropagator;
00020   delete theUpdator;
00021   delete theEstimator;
00022 
00023 }
00024 
00025 std::vector<Trajectory> 
00026 KFTrajectorySmoother::trajectories(const Trajectory& aTraj) const {
00027   // let's try to get return value optimization
00028   // the 'standard' case is when we return 1 tractory
00029 
00030   if (  aTraj.direction() == alongMomentum) {
00031     thePropagator->setPropagationDirection(oppositeToMomentum);
00032   } else {
00033     thePropagator->setPropagationDirection(alongMomentum);
00034   }
00035 
00036   std::vector<Trajectory> ret(1, Trajectory(aTraj.seed(), thePropagator->propagationDirection()));
00037   Trajectory & myTraj = ret.front();
00038 
00039 
00040   if(aTraj.empty()) { ret.clear(); return ret; } 
00041 
00042 
00043   const std::vector<TM> & avtm = aTraj.measurements();
00044   LogDebug("TrackFitters") << "KFTrajectorySmoother::trajectories starting with " << avtm.size() << " HITS\n";
00045 
00046   myTraj.reserve(avtm.size());
00047 
00048   for (unsigned int j=0;j<avtm.size();j++) { 
00049     if (avtm[j].recHit()->det()) 
00050       LogTrace("TrackFitters") << "hit #:" << j+1 << " rawId=" << avtm[j].recHit()->det()->geographicalId().rawId() 
00051                                << " validity=" << avtm[j].recHit()->isValid();
00052     else
00053       LogTrace("TrackFitters") << "hit #:" << j+1 << " Hit with no Det information";
00054   }
00055 
00056   TSOS predTsos = avtm.back().forwardPredictedState();
00057   predTsos.rescaleError(theErrorRescaling);
00058   TSOS currTsos;
00059  
00060   TrajectoryStateCombiner combiner;
00061 
00062   unsigned int hitcounter = avtm.size();
00063   for(std::vector<TM>::const_reverse_iterator itm = avtm.rbegin(); itm != (avtm.rend()); ++itm,--hitcounter) {
00064 
00065     TransientTrackingRecHit::ConstRecHitPointer hit = itm->recHit();
00066 
00067     //check surface just for safety: should never be ==0 because they are skipped in the fitter 
00068     if ( hit->surface()==0 ) {
00069       LogDebug("TrackFitters")<< " Error: invalid hit with no GeomDet attached .... skipping";
00070       continue;
00071     }
00072 
00073     if (hitcounter != avtm.size())//no propagation needed for first smoothed (==last fitted) hit 
00074       predTsos = thePropagator->propagate( currTsos, *(hit->surface()) );
00075 
00076     if(!predTsos.isValid()) {
00077       LogDebug("TrackFitters") << "KFTrajectorySmoother: predicted tsos not valid!";
00078       if( myTraj.foundHits() >= minHits_ ) {
00079         LogDebug("TrackFitters") << " breaking trajectory" << "\n";
00080       } else {        
00081         LogDebug("TrackFitters") << " killing trajectory" << "\n";      
00082         ret.clear(); 
00083       }
00084       break;      
00085     }
00086 
00087     if(hit->isValid()) {
00088       LogDebug("TrackFitters")
00089         << "----------------- HIT #" << hitcounter << " (VALID)-----------------------\n"
00090         << "HIT IS AT R   " << hit->globalPosition().perp() << "\n"
00091         << "HIT IS AT Z   " << hit->globalPosition().z() << "\n"
00092         << "HIT IS AT Phi " << hit->globalPosition().phi() << "\n"
00093         << "HIT IS AT Loc " << hit->localPosition() << "\n"
00094         << "WITH LocError " << hit->localPositionError() << "\n"
00095         << "HIT IS AT Glo " << hit->globalPosition() << "\n"
00096         << "SURFACE POSITION: " << hit->surface()->position() << "\n"
00097         << "SURFACE ROTATION: " << hit->surface()->rotation() << "\n"
00098         << "hit geographicalId=" << hit->geographicalId().rawId();
00099    
00100       DetId hitId = hit->geographicalId();
00101 
00102       if(hitId.det() == DetId::Tracker) {
00103         if (hitId.subdetId() == StripSubdetector::TIB )  
00104           LogTrace("TrackFitters") << " I am TIB " << TIBDetId(hitId).layer();
00105         else if (hitId.subdetId() == StripSubdetector::TOB ) 
00106           LogTrace("TrackFitters") << " I am TOB " << TOBDetId(hitId).layer();
00107         else if (hitId.subdetId() == StripSubdetector::TEC ) 
00108           LogTrace("TrackFitters") << " I am TEC " << TECDetId(hitId).wheel();
00109         else if (hitId.subdetId() == StripSubdetector::TID ) 
00110           LogTrace("TrackFitters") << " I am TID " << TIDDetId(hitId).wheel();
00111         else if (hitId.subdetId() == StripSubdetector::TID ) 
00112           LogTrace("TrackFitters") << " I am TID " << TIDDetId(hitId).wheel();
00113         else if (hitId.subdetId() == (int) PixelSubdetector::PixelBarrel ) 
00114           LogTrace("TrackFitters") << " I am PixBar " << PXBDetId(hitId).layer();
00115         else if (hitId.subdetId() == (int) PixelSubdetector::PixelEndcap )
00116           LogTrace("TrackFitters") << " I am PixFwd " << PXFDetId(hitId).disk();
00117         else 
00118           LogTrace("TrackFitters") << " UNKNOWN TRACKER HIT TYPE ";
00119       }
00120       else if(hitId.det() == DetId::Muon) {
00121         if(hitId.subdetId() == MuonSubdetId::DT)
00122           LogTrace("TrackFitters") << " I am DT " << DTWireId(hitId);
00123         else if (hitId.subdetId() == MuonSubdetId::CSC )
00124           LogTrace("TrackFitters") << " I am CSC " << CSCDetId(hitId);
00125         else if (hitId.subdetId() == MuonSubdetId::RPC )
00126           LogTrace("TrackFitters") << " I am RPC " << RPCDetId(hitId);
00127         else 
00128           LogTrace("TrackFitters") << " UNKNOWN MUON HIT TYPE ";
00129       }
00130       else
00131         LogTrace("TrackFitters") << " UNKNOWN HIT TYPE ";
00132     
00133       TSOS combTsos,smooTsos;
00134 
00135       //3 different possibilities to calculate smoothed state:
00136       //1: update combined predictions with hit
00137       //2: combine fwd-prediction with bwd-filter
00138       //3: combine bwd-prediction with fwd-filter
00139 
00140       //combTsos is the predicted state with N-1 hits information. this means: 
00141       //forward predicted state for first smoothed (last fitted) hit
00142       //backward predicted state for last smoothed (first fitted) hit
00143       //combination of forward and backward predictions for other hits
00144       if (hitcounter == avtm.size()) combTsos = itm->forwardPredictedState();
00145       else if (hitcounter == 1) combTsos = predTsos;
00146       else combTsos = combiner(predTsos, itm->forwardPredictedState());
00147       if(!combTsos.isValid()) {
00148         LogDebug("TrackFitters") << 
00149           "KFTrajectorySmoother: combined tsos not valid!\n" <<
00150           "pred Tsos pos: " << predTsos.globalPosition() << "\n" <<
00151           "pred Tsos mom: " << predTsos.globalMomentum() << "\n" <<
00152           "TrackingRecHit: " << hit->surface()->toGlobal(hit->localPosition()) << "\n" ;
00153         if( myTraj.foundHits() >= minHits_ ) {
00154           LogDebug("TrackFitters") << " breaking trajectory" << "\n";
00155         } else {        
00156           LogDebug("TrackFitters") << " killing trajectory" << "\n";       
00157           ret.clear(); 
00158         }
00159         break;      
00160       }
00161 
00162       TransientTrackingRecHit::RecHitPointer preciseHit = hit->clone(combTsos);
00163 
00164       if (preciseHit->isValid() == false){
00165         LogTrace("TrackFitters") << "THE Precise HIT IS NOT VALID: using currTsos = predTsos" << "\n";
00166         currTsos = predTsos;
00167         myTraj.push(TM(predTsos, hit, 0, theGeometry->idToLayer(hit->geographicalId()) ));
00168       }else{
00169         LogTrace("TrackFitters") << "THE Precise HIT IS VALID: updating currTsos" << "\n";
00170         
00171         //update backward predicted tsos with the hit
00172         currTsos = updator()->update(predTsos, *preciseHit);
00173         if (!currTsos.isValid()) {
00174             currTsos = predTsos;
00175             edm::LogWarning("KFSmoother_UpdateFailed") << 
00176                 "Failed updating state with hit. Rolling back to non-updated state.\n" <<
00177                 "State: "   << predTsos << 
00178                 "Hit local pos:  " << hit->localPosition() << "\n" <<
00179                 "Hit local err:  " << hit->localPositionError() << "\n" <<
00180                 "Hit global pos: " << hit->globalPosition() << "\n" <<
00181                 "Hit global err: " << hit->globalPositionError().matrix() << 
00182                 "\n";
00183         }
00184 
00185         //smooTsos updates the N-1 hits prediction with the hit
00186         if (hitcounter == avtm.size()) smooTsos = itm->updatedState();
00187         else if (hitcounter == 1) smooTsos = currTsos;
00188         else smooTsos = combiner(itm->forwardPredictedState(), currTsos); 
00189         
00190         if(!smooTsos.isValid()) {
00191           LogDebug("TrackFitters") << "KFTrajectorySmoother: smoothed tsos not valid!";
00192           if( myTraj.foundHits() >= minHits_ ) {
00193             LogDebug("TrackFitters") << " breaking trajectory" << "\n";
00194           } else {        
00195             LogDebug("TrackFitters") << " killing trajectory" << "\n";       
00196             ret.clear(); 
00197           }
00198           break;
00199         }
00200         
00201         double estimate;
00202         if (hitcounter != avtm.size()) estimate = estimator()->estimate(combTsos, *preciseHit ).second;//correct?
00203         else estimate = itm->estimate();
00204         
00205         LogTrace("TrackFitters")
00206           << "predTsos !" << "\n"
00207           << predTsos << "\n"
00208           << "currTsos !" << "\n"
00209           << currTsos << "\n"
00210           << "smooTsos !" << "\n"
00211           << smooTsos << "\n"
00212           << "smoothing estimate (with combTSOS)=" << estimate << "\n"
00213           << "filtering estimate=" << itm->estimate() << "\n";
00214         
00215         //check for valid hits with no det (refitter with constraints)
00216         if (preciseHit->det()) myTraj.push(TM(itm->forwardPredictedState(),
00217                                               predTsos,
00218                                               smooTsos,
00219                                               preciseHit,
00220                                               estimate,
00221                                               theGeometry->idToLayer(preciseHit->geographicalId()) ),
00222                                            estimator()->estimate(predTsos,*preciseHit).second);
00223         else myTraj.push(TM(itm->forwardPredictedState(),
00224                             predTsos,
00225                             smooTsos,
00226                             preciseHit,
00227                             estimate),
00228                          estimator()->estimate(predTsos,*preciseHit).second);
00229         //itm->estimate());
00230       }
00231     } else {
00232       LogDebug("TrackFitters") 
00233         << "----------------- HIT #" << hitcounter << " (INVALID)-----------------------";      
00234 
00235       //no update
00236       currTsos = predTsos;
00237       TSOS combTsos;
00238       if (hitcounter == avtm.size()) combTsos = itm->forwardPredictedState();
00239       else if (hitcounter == 1) combTsos = predTsos;
00240       else combTsos = combiner(predTsos, itm->forwardPredictedState());
00241       
00242       if(!combTsos.isValid()) {
00243         LogDebug("TrackFitters") << 
00244           "KFTrajectorySmoother: combined tsos not valid!";
00245         ret.clear(); break;
00246       }
00247       
00248       myTraj.push(TM(itm->forwardPredictedState(),
00249                      predTsos,
00250                      combTsos,
00251                      hit,
00252                      0,
00253                      theGeometry->idToLayer(hit->geographicalId()) ));
00254     }
00255   } // for loop
00256 
00257   return ret; 
00258   
00259 }