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/PatternTools/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
00032 string refitDirectionName = parameterSet.getParameter<string>("RefitDirection");
00033
00034 if (refitDirectionName == "insideOut" ) theRefitDirection = insideOut;
00035 else if (refitDirectionName == "outsideIn" ) theRefitDirection = outsideIn;
00036 else
00037 throw cms::Exception("TrackTransformer constructor")
00038 <<"Wrong refit direction chosen in TrackTransformer ParameterSet"
00039 << "\n"
00040 << "Possible choices are:"
00041 << "\n"
00042 << "RefitDirection = insideOut or RefitDirection = outsideIn";
00043
00044 theFitterName = parameterSet.getParameter<string>("Fitter");
00045 theSmootherName = parameterSet.getParameter<string>("Smoother");
00046 thePropagatorName = parameterSet.getParameter<string>("Propagator");
00047
00048 theTrackerRecHitBuilderName = parameterSet.getParameter<string>("TrackerRecHitBuilder");
00049 theMuonRecHitBuilderName = parameterSet.getParameter<string>("MuonRecHitBuilder");
00050
00051 theRPCInTheFit = parameterSet.getParameter<bool>("RefitRPCHits");
00052
00053 theCacheId_TC = theCacheId_GTG = theCacheId_MG = theCacheId_TRH = 0;
00054 }
00055
00057 TrackTransformer::~TrackTransformer(){}
00058
00059
00060 void TrackTransformer::setServices(const EventSetup& setup){
00061
00062 const std::string metname = "Reco|TrackingTools|TrackTransformer";
00063
00064 unsigned long long newCacheId_TC = setup.get<TrackingComponentsRecord>().cacheIdentifier();
00065
00066 if ( newCacheId_TC != theCacheId_TC ){
00067 LogDebug(metname) << "Tracking Component changed!";
00068 theCacheId_TC = newCacheId_TC;
00069
00070 setup.get<TrackingComponentsRecord>().get(theFitterName,theFitter);
00071 setup.get<TrackingComponentsRecord>().get(theSmootherName,theSmoother);
00072
00073 setup.get<TrackingComponentsRecord>().get(thePropagatorName,thePropagator);
00074 }
00075
00076
00077 unsigned long long newCacheId_GTG = setup.get<GlobalTrackingGeometryRecord>().cacheIdentifier();
00078 if ( newCacheId_GTG != theCacheId_GTG ) {
00079 LogDebug(metname) << "GlobalTrackingGeometry changed!";
00080 theCacheId_GTG = newCacheId_GTG;
00081 setup.get<GlobalTrackingGeometryRecord>().get(theTrackingGeometry);
00082 }
00083
00084
00085 unsigned long long newCacheId_MG = setup.get<IdealMagneticFieldRecord>().cacheIdentifier();
00086 if ( newCacheId_MG != theCacheId_MG ) {
00087 LogDebug(metname) << "Magnetic Field changed!";
00088 theCacheId_MG = newCacheId_MG;
00089 setup.get<IdealMagneticFieldRecord>().get(theMGField);
00090 }
00091
00092
00093 unsigned long long newCacheId_TRH = setup.get<TransientRecHitRecord>().cacheIdentifier();
00094 if ( newCacheId_TRH != theCacheId_TRH ) {
00095 LogDebug(metname) << "TransientRecHitRecord changed!";
00096 setup.get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
00097 setup.get<TransientRecHitRecord>().get(theMuonRecHitBuilderName,theMuonRecHitBuilder);
00098 }
00099 }
00100
00101
00102 vector<Trajectory> TrackTransformer::transform(const reco::TrackRef& track) const {
00103 return transform(*track);
00104 }
00105
00106
00107 TransientTrackingRecHit::ConstRecHitContainer
00108 TrackTransformer::getTransientRecHits(const reco::TransientTrack& track) const {
00109
00110 TransientTrackingRecHit::ConstRecHitContainer result;
00111
00112 for (trackingRecHit_iterator hit = track.recHitsBegin(); hit != track.recHitsEnd(); ++hit)
00113 if((*hit)->isValid())
00114 if ( (*hit)->geographicalId().det() == DetId::Tracker )
00115 result.push_back(theTrackerRecHitBuilder->build(&**hit));
00116 else if ( (*hit)->geographicalId().det() == DetId::Muon ){
00117 if( (*hit)->geographicalId().subdetId() == 3 && !theRPCInTheFit){
00118 LogDebug("Reco|TrackingTools|TrackTransformer") << "RPC Rec Hit discarged";
00119 continue;
00120 }
00121 result.push_back(theMuonRecHitBuilder->build(&**hit));
00122 }
00123
00124 return result;
00125 }
00126
00127
00128 TrackTransformer::RefitDirection
00129 TrackTransformer::checkRecHitsOrdering(TransientTrackingRecHit::ConstRecHitContainer& recHits) const {
00130
00131 if (!recHits.empty()){
00132
00133
00134
00135
00136 double rFirst = trackingGeometry()->idToDet(recHits.front()->geographicalId())->position().mag();
00137 double rLast = trackingGeometry()->idToDet(recHits.back()->geographicalId() )->position().mag();
00138 if(rFirst < rLast) return insideOut;
00139 else if(rFirst > rLast) return outsideIn;
00140 else{
00141 LogError("Reco|TrackingTools|TrackTransformer") << "Impossible determine the rechits order" <<endl;
00142 return undetermined;
00143 }
00144 }
00145 else{
00146 LogError("Reco|TrackingTools|TrackTransformer") << "Impossible determine the rechits order" <<endl;
00147 return undetermined;
00148 }
00149 }
00150
00151
00153 vector<Trajectory> TrackTransformer::transform(const reco::Track& newTrack) const {
00154
00155 const std::string metname = "Reco|TrackingTools|TrackTransformer";
00156
00157 reco::TransientTrack track(newTrack,magneticField(),trackingGeometry());
00158
00159
00160 TransientTrackingRecHit::ConstRecHitContainer recHitsForReFit = getTransientRecHits(track);
00161
00162 return transform(newTrack, track, recHitsForReFit);
00163 }
00164
00165
00167 vector<Trajectory> TrackTransformer::transform(const reco::Track& newTrack,
00168 const reco::TransientTrack track,
00169 TransientTrackingRecHit::ConstRecHitContainer recHitsForReFit) const {
00170
00171 if(recHitsForReFit.size() < 2) return vector<Trajectory>();
00172
00173 const std::string metname = "Reco|TrackingTools|TrackTransformer";
00174
00175
00176 RefitDirection recHitsOrder = checkRecHitsOrdering(recHitsForReFit);
00177
00178
00179 if(theRefitDirection != recHitsOrder) reverse(recHitsForReFit.begin(),recHitsForReFit.end());
00180
00181
00182 TrajectoryStateOnSurface firstTSOS;
00183 unsigned int innerId;
00184 if(theRefitDirection == insideOut){
00185 innerId = newTrack.innerDetId();
00186 firstTSOS = track.innermostMeasurementState();
00187 }
00188 else{
00189 innerId = newTrack.outerDetId();
00190 firstTSOS = track.outermostMeasurementState();
00191 }
00192
00193 if(!firstTSOS.isValid()){
00194 LogWarning(metname)<<"Error wrong initial state!"<<endl;
00195 return vector<Trajectory>();
00196 }
00197
00198
00199 PTrajectoryStateOnDet garbage1;
00200 edm::OwnVector<TrackingRecHit> garbage2;
00201 PropagationDirection propDir =
00202 (firstTSOS.globalPosition().basicVector().dot(firstTSOS.globalMomentum().basicVector())>0) ? alongMomentum : oppositeToMomentum;
00203
00204
00205 if(propDir == alongMomentum && theRefitDirection == outsideIn) propDir=oppositeToMomentum;
00206 if(propDir == oppositeToMomentum && theRefitDirection == insideOut) propDir=alongMomentum;
00207
00208
00209 TrajectorySeed seed(garbage1,garbage2,propDir);
00210
00211 if(recHitsForReFit.front()->geographicalId() != DetId(innerId)){
00212 LogDebug(metname)<<"Propagation occured"<<endl;
00213 firstTSOS = propagator()->propagate(firstTSOS, recHitsForReFit.front()->det()->surface());
00214 if(!firstTSOS.isValid()){
00215 LogDebug(metname)<<"Propagation error!"<<endl;
00216 return vector<Trajectory>();
00217 }
00218 }
00219
00220 vector<Trajectory> trajectories = theFitter->fit(seed,recHitsForReFit,firstTSOS);
00221
00222 if(trajectories.empty()){
00223 LogDebug(metname)<<"No Track refitted!"<<endl;
00224 return vector<Trajectory>();
00225 }
00226
00227 Trajectory trajectoryBW = trajectories.front();
00228
00229 vector<Trajectory> trajectoriesSM = theSmoother->trajectories(trajectoryBW);
00230
00231
00232
00233
00234 if(trajectoriesSM.empty()){
00235 LogDebug(metname)<<"No Track smoothed!"<<endl;
00236 return vector<Trajectory>();
00237 }
00238
00239 return trajectoriesSM;
00240
00241 }
00242
00243