CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/TrackingTools/TrackRefitter/src/TrackTransformer.cc

Go to the documentation of this file.
00001 #include "TrackingTools/TrackRefitter/interface/TrackTransformer.h"
00002 
00003 #include "FWCore/Framework/interface/EventSetup.h"
00004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 
00007 #include "Geometry/Records/interface/GlobalTrackingGeometryRecord.h"
00008 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00009 
00010 #include "TrackingTools/Records/interface/TrackingComponentsRecord.h"
00011 #include "TrackingTools/TrackFitters/interface/TrajectoryFitter.h"
00012 #include "TrackingTools/PatternTools/interface/TrajectorySmoother.h"
00013 
00014 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00015 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00016 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHitBuilder.h"
00017 #include "TrackingTools/Records/interface/TransientRecHitRecord.h"
00018 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00019 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
00020 
00021 #include "DataFormats/TrackReco/interface/Track.h"
00022 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00023 #include "DataFormats/DetId/interface/DetId.h"
00024 
00025 using namespace std;
00026 using namespace edm;
00027 
00029 TrackTransformer::TrackTransformer(const ParameterSet& parameterSet){
00030   
00031   // Refit direction
00032   string refitDirectionName = parameterSet.getParameter<string>("RefitDirection");
00033   theRefitDirection = RefitDirection(refitDirectionName);
00034   
00035   theFitterName = parameterSet.getParameter<string>("Fitter");  
00036   theSmootherName = parameterSet.getParameter<string>("Smoother");  
00037   thePropagatorName = parameterSet.getParameter<string>("Propagator");
00038 
00039   theTrackerRecHitBuilderName = parameterSet.getParameter<string>("TrackerRecHitBuilder");
00040   theMuonRecHitBuilderName = parameterSet.getParameter<string>("MuonRecHitBuilder");
00041 
00042   theRPCInTheFit = parameterSet.getParameter<bool>("RefitRPCHits");
00043   theDoPredictionsOnly = parameterSet.getParameter<bool>("DoPredictionsOnly");
00044 
00045   theCacheId_TC = theCacheId_GTG = theCacheId_MG = theCacheId_TRH = 0;
00046 }
00047 
00049 TrackTransformer::~TrackTransformer(){}
00050 
00051 
00052 void TrackTransformer::setServices(const EventSetup& setup){
00053   
00054   const std::string metname = "Reco|TrackingTools|TrackTransformer";
00055 
00056   setup.get<TrajectoryFitter::Record>().get(theFitterName,theFitter);
00057   setup.get<TrajectoryFitter::Record>().get(theSmootherName,theSmoother);
00058 
00059   
00060   unsigned long long newCacheId_TC = setup.get<TrackingComponentsRecord>().cacheIdentifier();
00061 
00062   if ( newCacheId_TC != theCacheId_TC ){
00063     LogTrace(metname) << "Tracking Component changed!";
00064     theCacheId_TC = newCacheId_TC;
00065     setup.get<TrackingComponentsRecord>().get(thePropagatorName,thePropagator);
00066   }
00067 
00068   // Global Tracking Geometry
00069   unsigned long long newCacheId_GTG = setup.get<GlobalTrackingGeometryRecord>().cacheIdentifier();
00070   if ( newCacheId_GTG != theCacheId_GTG ) {
00071     LogTrace(metname) << "GlobalTrackingGeometry changed!";
00072     theCacheId_GTG = newCacheId_GTG;
00073     setup.get<GlobalTrackingGeometryRecord>().get(theTrackingGeometry); 
00074   }
00075   
00076   // Magfield Field
00077   unsigned long long newCacheId_MG = setup.get<IdealMagneticFieldRecord>().cacheIdentifier();
00078   if ( newCacheId_MG != theCacheId_MG ) {
00079     LogTrace(metname) << "Magnetic Field changed!";
00080     theCacheId_MG = newCacheId_MG;
00081     setup.get<IdealMagneticFieldRecord>().get(theMGField);
00082   }
00083   
00084   // Transient Rechit Builders
00085   unsigned long long newCacheId_TRH = setup.get<TransientRecHitRecord>().cacheIdentifier();
00086   if ( newCacheId_TRH != theCacheId_TRH ) {
00087     theCacheId_TRH = newCacheId_TRH;
00088     LogTrace(metname) << "TransientRecHitRecord changed!";
00089     setup.get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
00090     setup.get<TransientRecHitRecord>().get(theMuonRecHitBuilderName,theMuonRecHitBuilder);
00091   }
00092 }
00093 
00094 
00095 vector<Trajectory> TrackTransformer::transform(const reco::TrackRef& track) const {
00096   return transform(*track);
00097 }
00098 
00099 
00100 TransientTrackingRecHit::ConstRecHitContainer
00101 TrackTransformer::getTransientRecHits(const reco::TransientTrack& track) const {
00102 
00103   TransientTrackingRecHit::ConstRecHitContainer result;
00104   
00105   for (trackingRecHit_iterator hit = track.recHitsBegin(); hit != track.recHitsEnd(); ++hit) {
00106     if((*hit)->isValid()) {
00107       if ( (*hit)->geographicalId().det() == DetId::Tracker ) {
00108         result.push_back(theTrackerRecHitBuilder->build(&**hit));
00109       } else if ( (*hit)->geographicalId().det() == DetId::Muon ){
00110         if( (*hit)->geographicalId().subdetId() == 3 && !theRPCInTheFit){
00111           LogTrace("Reco|TrackingTools|TrackTransformer") << "RPC Rec Hit discarged"; 
00112           continue;
00113         }
00114         result.push_back(theMuonRecHitBuilder->build(&**hit));
00115       }
00116     }
00117   }
00118   
00119   return result;
00120 }
00121 
00122 // FIXME: check this method!
00123 RefitDirection::GeometricalDirection
00124 TrackTransformer::checkRecHitsOrdering(TransientTrackingRecHit::ConstRecHitContainer& recHits) const {
00125   
00126   if (!recHits.empty()){
00127     GlobalPoint first = trackingGeometry()->idToDet(recHits.front()->geographicalId())->position();
00128     GlobalPoint last = trackingGeometry()->idToDet(recHits.back()->geographicalId())->position();
00129     
00130     double rFirst = first.mag();
00131     double rLast  = last.mag();
00132     if(rFirst < rLast) return RefitDirection::insideOut;
00133     else if(rFirst > rLast) return RefitDirection::outsideIn;
00134     else{
00135       LogDebug("Reco|TrackingTools|TrackTransformer") << "Impossible to determine the rechits order" <<endl;
00136       return RefitDirection::undetermined;
00137     }
00138   }
00139   else{
00140     LogDebug("Reco|TrackingTools|TrackTransformer") << "Impossible to determine the rechits order" <<endl;
00141     return RefitDirection::undetermined;
00142   }
00143 }
00144 
00145 // void reorder(TransientTrackingRecHit::ConstRecHitContainer& recHits, RefitDirection::GeometricalDirection recHitsOrder) const{
00146 
00147 //   if(theRefitDirection.geometricalDirection() != recHitsOrder) reverse(recHits.begin(),recHits.end());
00148 
00149 //   if(theRefitDirection.geometricalDirection() == RefitDirection::insideOut &&recHitsOrder){}
00150 //   else if(theRefitDirection.geometricalDirection() == RefitDirection::outsideIn){} 
00151 //   else LogWarning("Reco|TrackingTools|TrackTransformer") << "Impossible to determine the rechits order" <<endl;
00152 // }
00153 
00154 
00156 vector<Trajectory> TrackTransformer::transform(const reco::Track& newTrack) const {
00157 
00158   const std::string metname = "Reco|TrackingTools|TrackTransformer";
00159   
00160   reco::TransientTrack track(newTrack,magneticField(),trackingGeometry());   
00161 
00162   // Build the transient Rechits
00163   TransientTrackingRecHit::ConstRecHitContainer recHitsForReFit = getTransientRecHits(track);
00164 
00165   return transform(track, recHitsForReFit);
00166 }
00167 
00168 
00170 vector<Trajectory> TrackTransformer::transform(const reco::TransientTrack& track,
00171                                                const TransientTrackingRecHit::ConstRecHitContainer& _recHitsForReFit) const {
00172   
00173   TransientTrackingRecHit::ConstRecHitContainer recHitsForReFit =  _recHitsForReFit;
00174   const std::string metname = "Reco|TrackingTools|TrackTransformer";
00175 
00176   if(recHitsForReFit.size() < 2) return vector<Trajectory>();
00177  
00178   // 8 cases are foreseen: 
00179   // [RH = rec hit order, P = momentum dir, FD = fit direction. IO/OI = inside-out/outside-in, AM/OM = along momentum/opposite to momentum]
00180   // (1) RH IO | P IO | FD AM  ---> Start from IN
00181   // (2) RH IO | P IO | FD OM  ---> Reverse RH and start from OUT
00182   // (3) RH IO | P OI | FD AM  ---> Reverse RH and start from IN
00183   // (4) RH IO | P OI | FD OM  ---> Start from OUT
00184   // (5) RH OI | P IO | FD AM  ---> Reverse RH and start from IN
00185   // (6) RH OI | P IO | FD OM  ---> Start from OUT
00186   // (7) RH OI | P OI | FD AM  ---> Start from IN
00187   // (8) RH OI | P OI | FD OM  ---> Reverse RH and start from OUT
00188   //
00189   // *** Rules: ***
00190   // -A- If RH-FD agree (IO-AM,OI-OM) do not reverse the RH
00191   // -B- If FD along momentum start from innermost state, otherwise use outermost
00192   
00193   // Other special cases can be handled:
00194   // (1 bis) RH IO | P IO | GFD IO => FD AM  ---> Start from IN
00195   // (2 bis) RH IO | P IO | GFD OI => FD OM  ---> Reverse RH and start from OUT
00196   // (3 bis) RH IO | P OI | GFD OI => FD AM  ---> Reverse RH and start from OUT
00197   // (4 bis) RH IO | P OI | GFD IO => FD OM  ---> Start from IN
00198   // (5 bis) RH OI | P IO | GFD IO => FD AM  ---> Reverse RH and start from IN
00199   // (6 bis) RH OI | P IO | GFD OI => FD OM  ---> Start from OUT
00200   // (7 bis) RH OI | P OI | GFD OI => FD AM  ---> Start from OUT
00201   // (8 bis) RH OI | P OI | GFD IO => FD OM  ---> Reverse RH and start from IN
00202   // 
00203   // *** Additional rule: ***
00204   // -A0- If P and GFD agree, then FD is AM otherwise is OM
00205   // -A00- rechit must be ordered as GFD in order to handle the case of cosmics
00206   // -B0- The starting state is decided by GFD
00207 
00208   // Determine the RH order
00209   RefitDirection::GeometricalDirection recHitsOrder = checkRecHitsOrdering(recHitsForReFit); // FIXME change nome of the *type*  --> RecHit order!
00210   LogTrace(metname) << "RH order (0-insideOut, 1-outsideIn): " << recHitsOrder;
00211 
00212   PropagationDirection propagationDirection = theRefitDirection.propagationDirection();
00213 
00214   // Apply rule -A0-
00215   if(propagationDirection == anyDirection){
00216     GlobalVector momentum = track.innermostMeasurementState().globalMomentum();
00217     GlobalVector position = track.innermostMeasurementState().globalPosition() - GlobalPoint(0,0,0);
00218     RefitDirection::GeometricalDirection p = (momentum.x()*position.x() > 0 || momentum.y()*position.y() > 0) ? RefitDirection::insideOut : RefitDirection::outsideIn;
00219 
00220     propagationDirection = p == theRefitDirection.geometricalDirection() ? alongMomentum : oppositeToMomentum;
00221     LogTrace(metname) << "P  (0-insideOut, 1-outsideIn): " << p;
00222     LogTrace(metname) << "FD (0-OM, 1-AM, 2-ANY): " << propagationDirection;
00223   }
00224   // -A0-
00225 
00226   // Apply rule -A-
00227   if(theRefitDirection.propagationDirection() != anyDirection){
00228     if((recHitsOrder == RefitDirection::insideOut && propagationDirection == oppositeToMomentum) ||
00229        (recHitsOrder == RefitDirection::outsideIn && propagationDirection == alongMomentum) ) 
00230       reverse(recHitsForReFit.begin(),recHitsForReFit.end());}
00231   // -A-
00232   // Apply rule -A00-
00233   else{
00234     // reorder the rechit as defined in theRefitDirection.geometricalDirection(); 
00235     if(theRefitDirection.geometricalDirection() != recHitsOrder) reverse(recHitsForReFit.begin(),recHitsForReFit.end()); 
00236   }
00237   // -A00-
00238     
00239   // Apply rule -B-
00240   TrajectoryStateOnSurface firstTSOS = track.innermostMeasurementState();
00241   unsigned int innerId = track.track().innerDetId();
00242   if(theRefitDirection.propagationDirection() != anyDirection){
00243     if(propagationDirection == oppositeToMomentum){
00244       innerId   = track.track().outerDetId();
00245       firstTSOS = track.outermostMeasurementState();
00246     }
00247   }
00248   else { // if(theRefitDirection.propagationDirection() == anyDirection)
00249     // Apply rule -B0-
00250     if(theRefitDirection.geometricalDirection() == RefitDirection::outsideIn){
00251       innerId   = track.track().outerDetId();
00252       firstTSOS = track.outermostMeasurementState();
00253     }
00254     // -B0-
00255   }
00256   // -B-
00257 
00258   if(!firstTSOS.isValid()){
00259     LogTrace(metname)<<"Error wrong initial state!"<<endl;
00260     return vector<Trajectory>();
00261   }
00262 
00263   TrajectorySeed seed(PTrajectoryStateOnDet(),TrajectorySeed::recHitContainer(),propagationDirection);
00264 
00265   if(recHitsForReFit.front()->geographicalId() != DetId(innerId)){
00266     LogTrace(metname)<<"Propagation occured"<<endl;
00267     firstTSOS = propagator()->propagate(firstTSOS, recHitsForReFit.front()->det()->surface());
00268     if(!firstTSOS.isValid()){
00269       LogTrace(metname)<<"Propagation error!"<<endl;
00270       return vector<Trajectory>();
00271     }
00272   }
00273 
00274   if(theDoPredictionsOnly){
00275     Trajectory aTraj(seed,propagationDirection);
00276     TrajectoryStateOnSurface predTSOS = firstTSOS;
00277     for(TransientTrackingRecHit::ConstRecHitContainer::const_iterator ihit = recHitsForReFit.begin(); 
00278         ihit != recHitsForReFit.end(); ++ihit ) {
00279       predTSOS = propagator()->propagate(predTSOS, (*ihit)->det()->surface());
00280       if (predTSOS.isValid()) aTraj.push(TrajectoryMeasurement(predTSOS, *ihit));
00281     }
00282     return vector<Trajectory>(1, aTraj);
00283   }
00284 
00285 
00286   vector<Trajectory> trajectories = theFitter->fit(seed,recHitsForReFit,firstTSOS);
00287   
00288   if(trajectories.empty()){
00289     LogTrace(metname)<<"No Track refitted!"<<endl;
00290     return vector<Trajectory>();
00291   }
00292   
00293   Trajectory trajectoryBW = trajectories.front();
00294     
00295   vector<Trajectory> trajectoriesSM = theSmoother->trajectories(trajectoryBW);
00296 
00297   if(trajectoriesSM.empty()){
00298     LogTrace(metname)<<"No Track smoothed!"<<endl;
00299     return vector<Trajectory>();
00300   }
00301   
00302   return trajectoriesSM;
00303 
00304 }
00305 
00306