CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10/src/RecoMuon/TrackerSeedGenerator/plugins/TSGFromPropagation.cc

Go to the documentation of this file.
00001 #include "RecoMuon/TrackerSeedGenerator/plugins/TSGFromPropagation.h"
00002 
00010 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00011 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00012 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00013 #include "TrackingTools/MeasurementDet/interface/LayerMeasurements.h"
00014 #include "TrackingTools/MeasurementDet/interface/MeasurementDet.h"
00015 
00016 #include "TrackingTools/KalmanUpdators/interface/Chi2MeasurementEstimator.h"
00017 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
00018 #include "TrackingTools/GeomPropagators/interface/StateOnTrackerBound.h"
00019 
00020 #include "RecoTracker/Record/interface/TrackerRecoGeometryRecord.h"
00021 #include "RecoTracker/Record/interface/CkfComponentsRecord.h"
00022 #include "RecoTracker/MeasurementDet/interface/MeasurementTracker.h"
00023 #include "RecoTracker/TkDetLayers/interface/GeometricSearchTracker.h"
00024 
00025 #include "RecoMuon/GlobalTrackingTools/interface/DirectTrackerNavigation.h"
00026 #include "TrackingTools/KalmanUpdators/interface/KFUpdator.h"
00027 
00028 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00029 
00030 TSGFromPropagation::TSGFromPropagation(const edm::ParameterSet & iConfig) :theTkLayerMeasurements (0), theTracker(0), theMeasTracker(0), theNavigation(0), theService(0), theEstimator(0), theTSTransformer(0), theSigmaZ(0), theConfig (iConfig)
00031 {
00032   theCategory = "Muon|RecoMuon|TSGFromPropagation";
00033   theMeasTrackerName = iConfig.getParameter<std::string>("MeasurementTrackerName");
00034 
00035 }
00036 
00037 TSGFromPropagation::TSGFromPropagation(const edm::ParameterSet & iConfig, const MuonServiceProxy* service) : theTkLayerMeasurements (0), theTracker(0), theMeasTracker(0), theNavigation(0), theService(service),theUpdator(0), theEstimator(0), theTSTransformer(0), theSigmaZ(0), theConfig (iConfig)
00038 {
00039   theCategory = "Muon|RecoMuon|TSGFromPropagation";
00040   theMeasTrackerName = iConfig.getParameter<std::string>("MeasurementTrackerName");
00041 }
00042 
00043 TSGFromPropagation::~TSGFromPropagation()
00044 {
00045 
00046   LogTrace(theCategory) << " TSGFromPropagation dtor called ";
00047   if ( theNavigation ) delete theNavigation;
00048   if ( theUpdator ) delete theUpdator;
00049   if ( theEstimator ) delete theEstimator;
00050   if ( theTkLayerMeasurements ) delete theTkLayerMeasurements;
00051   if ( theErrorMatrixAdjuster ) delete theErrorMatrixAdjuster;
00052 
00053 }
00054 
00055 void TSGFromPropagation::trackerSeeds(const TrackCand& staMuon, const TrackingRegion& region, std::vector<TrajectorySeed> & result) {
00056 
00057   if ( theResetMethod == "discrete" ) getRescalingFactor(staMuon);
00058 
00059   TrajectoryStateOnSurface staState = outerTkState(staMuon);
00060 
00061   if ( !staState.isValid() ) { 
00062     LogTrace(theCategory) << "Error: initial state from L2 muon is invalid.";
00063     return;
00064   }
00065 
00066   LogTrace(theCategory) << "begin of trackerSeed:\n staState pos: "<<staState.globalPosition()
00067                         << " mom: "<<staState.globalMomentum() 
00068                         <<"pos eta: "<<staState.globalPosition().eta()
00069                         <<"mom eta: "<<staState.globalMomentum().eta();
00070 
00071   std::vector<const DetLayer*> nls = theNavigation->compatibleLayers(*(staState.freeState()), oppositeToMomentum);
00072 
00073   LogTrace(theCategory) << " compatible layers: "<<nls.size();
00074 
00075   if ( nls.empty() ) return;
00076 
00077   int ndesLayer = 0;
00078 
00079   bool usePredictedState = false;
00080 
00081   if ( theUpdateStateFlag ) { //use updated states
00082      std::vector<TrajectoryMeasurement> alltm; 
00083 
00084      for (std::vector<const DetLayer*>::const_iterator inl = nls.begin();
00085          inl != nls.end(); inl++, ndesLayer++ ) {
00086          if ( (*inl == 0) ) break;
00087 //         if ( (inl != nls.end()-1 ) && ( (*inl)->subDetector() == GeomDetEnumerators::TEC ) && ( (*(inl+1))->subDetector() == GeomDetEnumerators::TOB ) ) continue; 
00088          alltm = findMeasurements_new(*inl, staState);
00089          if ( (!alltm.empty()) ) {
00090             LogTrace(theCategory) << "final compatible layer: "<<ndesLayer;
00091             break;
00092          }
00093      }
00094 
00095      if ( alltm.empty() ) {
00096         LogTrace(theCategory) << " NO Measurements Found: eta: "<<staState.globalPosition().eta() <<"pt "<<staState.globalMomentum().perp();
00097         usePredictedState = true;
00098      } else {
00099        LogTrace(theCategory) << " Measurements for seeds: "<<alltm.size();
00100        std::stable_sort(alltm.begin(),alltm.end(),increasingEstimate()); 
00101        if ( alltm.size() > 5 ) alltm.erase(alltm.begin() + 5, alltm.end());
00102 
00103        int i = 0;
00104        for (std::vector<TrajectoryMeasurement>::const_iterator itm = alltm.begin();
00105             itm != alltm.end(); itm++, i++) {
00106             TrajectoryStateOnSurface updatedTSOS = updator()->update(itm->predictedState(), *(itm->recHit()));
00107             if ( updatedTSOS.isValid() && passSelection(updatedTSOS) )  {
00108                edm::OwnVector<TrackingRecHit> container;
00109                container.push_back(itm->recHit()->hit()->clone());
00110                TrajectorySeed ts = createSeed(updatedTSOS, container, itm->recHit()->geographicalId());
00111                result.push_back(ts);
00112             }
00113        }
00114      LogTrace(theCategory) << "result: "<<result.size();
00115      return;
00116     }
00117   }
00118 
00119   if ( !theUpdateStateFlag || usePredictedState ) { //use predicted states
00120      LogTrace(theCategory) << "use predicted state: ";
00121      for (std::vector<const DetLayer*>::const_iterator inl = nls.begin();
00122          inl != nls.end(); inl++ ) {
00123 
00124          if ( !result.empty() || *inl == 0 ) {
00125             break;
00126          }
00127          std::vector<DetLayer::DetWithState> compatDets = (*inl)->compatibleDets(staState, *propagator(), *estimator());
00128          LogTrace(theCategory) << " compatDets "<<compatDets.size();
00129          if ( compatDets.empty() ) continue;
00130          TrajectorySeed ts = createSeed(compatDets.front().second, compatDets.front().first->geographicalId());
00131          result.push_back(ts);
00132 
00133      }
00134      LogTrace(theCategory) << "result: "<<result.size();
00135      return;
00136   } 
00137   return;
00138 }
00139 
00140 void TSGFromPropagation::init(const MuonServiceProxy* service) {
00141 
00142   theMaxChi2 = theConfig.getParameter<double>("MaxChi2");
00143 
00144   theFixedErrorRescaling = theConfig.getParameter<double>("ErrorRescaling");
00145 
00146   theFlexErrorRescaling = 1.0;
00147 
00148   theResetMethod = theConfig.getParameter<std::string>("ResetMethod");
00149 
00150   if (theResetMethod != "discrete" && theResetMethod != "fixed" && theResetMethod != "matrix"  ) {
00151     edm::LogError("TSGFromPropagation") 
00152       <<"Wrong error rescaling method: "<<theResetMethod <<"\n"
00153       <<"Possible choices are: discrete, fixed, matrix.\n"
00154       <<"Use discrete method" <<std::endl;
00155     theResetMethod = "discrete"; 
00156   }
00157 
00158   theEstimator = new Chi2MeasurementEstimator(theMaxChi2);
00159 
00160   theCacheId_MT = 0;
00161 
00162   theCacheId_TG = 0;
00163 
00164   thePropagatorName = theConfig.getParameter<std::string>("Propagator");
00165 
00166   theService = service;
00167 
00168   theUseVertexStateFlag = theConfig.getParameter<bool>("UseVertexState");
00169 
00170   theUpdateStateFlag = theConfig.getParameter<bool>("UpdateState");
00171 
00172   theSelectStateFlag = theConfig.getParameter<bool>("SelectState");
00173 
00174   theUpdator = new KFUpdator();
00175 
00176   theSigmaZ = theConfig.getParameter<double>("SigmaZ");
00177 
00178   theBeamSpotInputTag = theConfig.getParameter<edm::InputTag>("beamSpot");
00179 
00180   edm::ParameterSet errorMatrixPset = theConfig.getParameter<edm::ParameterSet>("errorMatrixPset");
00181   if ( theResetMethod == "matrix" && !errorMatrixPset.empty()){
00182     theAdjustAtIp = errorMatrixPset.getParameter<bool>("atIP");
00183     theErrorMatrixAdjuster = new MuonErrorMatrix(errorMatrixPset);
00184   } else {
00185     theAdjustAtIp =false;
00186     theErrorMatrixAdjuster=0;
00187   }
00188 
00189   theService->eventSetup().get<TrackerRecoGeometryRecord>().get(theTracker); 
00190   theNavigation = new DirectTrackerNavigation(theTracker);
00191 
00192 }
00193 
00194 void TSGFromPropagation::setEvent(const edm::Event& iEvent) {
00195 
00196   bool measTrackerChanged = false;
00197 
00198   //edm::Handle<reco::BeamSpot> beamSpot;
00199   iEvent.getByLabel(theBeamSpotInputTag, beamSpot);
00200 
00201   unsigned long long newCacheId_MT = theService->eventSetup().get<CkfComponentsRecord>().cacheIdentifier();
00202 
00203   if ( theUpdateStateFlag && newCacheId_MT != theCacheId_MT ) {
00204     LogTrace(theCategory) << "Measurment Tracker Geometry changed!";
00205     theCacheId_MT = newCacheId_MT;
00206     theService->eventSetup().get<CkfComponentsRecord>().get(theMeasTrackerName,theMeasTracker);
00207     measTrackerChanged = true;
00208   }
00209 
00210   if ( theUpdateStateFlag ) theMeasTracker->update(iEvent);
00211 
00212   if ( measTrackerChanged && (&*theMeasTracker) ) {
00213      if ( theTkLayerMeasurements ) delete theTkLayerMeasurements;
00214      theTkLayerMeasurements = new LayerMeasurements(&*theMeasTracker);
00215   }
00216 
00217   bool trackerGeomChanged = false;
00218 
00219   unsigned long long newCacheId_TG = theService->eventSetup().get<TrackerRecoGeometryRecord>().cacheIdentifier();
00220 
00221   if ( newCacheId_TG != theCacheId_TG ) {
00222     LogTrace(theCategory) << "Tracker Reco Geometry changed!";
00223     theCacheId_TG = newCacheId_TG;
00224     theService->eventSetup().get<TrackerRecoGeometryRecord>().get(theTracker);
00225     trackerGeomChanged = true;
00226   }
00227 
00228   if ( trackerGeomChanged && (&*theTracker) ) {
00229     if ( theNavigation ) delete theNavigation;
00230     theNavigation = new DirectTrackerNavigation(theTracker);
00231   }
00232 }
00233 
00234 TrajectoryStateOnSurface TSGFromPropagation::innerState(const TrackCand& staMuon) const {
00235 
00236   TrajectoryStateOnSurface innerTS;
00237 
00238   if ( staMuon.first && staMuon.first->isValid() ) {
00239     if (staMuon.first->direction() == alongMomentum) {
00240       innerTS = staMuon.first->firstMeasurement().updatedState();
00241     } 
00242     else if (staMuon.first->direction() == oppositeToMomentum) { 
00243       innerTS = staMuon.first->lastMeasurement().updatedState();
00244     }
00245   } else {
00246     innerTS = trajectoryStateTransform::innerStateOnSurface(*(staMuon.second),*theService->trackingGeometry(), &*theService->magneticField());
00247   }
00248   //rescale the error
00249   adjust(innerTS);
00250 
00251   return  innerTS;
00252 
00253 //    return trajectoryStateTransform::innerStateOnSurface(*(staMuon.second),*theService->trackingGeometry(), &*theService->magneticField());
00254 }
00255 
00256 TrajectoryStateOnSurface TSGFromPropagation::outerTkState(const TrackCand& staMuon) const {
00257 
00258   TrajectoryStateOnSurface result;
00259 
00260   if ( theUseVertexStateFlag && staMuon.second->pt() > 1.0 ) {
00261     FreeTrajectoryState iniState = trajectoryStateTransform::initialFreeState(*(staMuon.second), &*theService->magneticField());
00262     //rescale the error at IP
00263     adjust(iniState); 
00264 
00265     StateOnTrackerBound fromInside(&*(theService->propagator("PropagatorWithMaterial")));
00266     result = fromInside(iniState);
00267   } else {
00268     StateOnTrackerBound fromOutside(&*propagator());
00269     result = fromOutside(innerState(staMuon));
00270   }
00271   return result;
00272 }
00273 
00274 TrajectorySeed TSGFromPropagation::createSeed(const TrajectoryStateOnSurface& tsos, const DetId& id) const {
00275 
00276   edm::OwnVector<TrackingRecHit> container;
00277   return createSeed(tsos, container, id);
00278 
00279 }
00280 
00281 TrajectorySeed TSGFromPropagation::createSeed(const TrajectoryStateOnSurface& tsos, const edm::OwnVector<TrackingRecHit>& container, const DetId& id) const {
00282 
00283   PTrajectoryStateOnDet const & seedTSOS = trajectoryStateTransform::persistentState(tsos,id.rawId());
00284   return TrajectorySeed(seedTSOS,container,oppositeToMomentum);
00285 
00286 }
00287 
00288 
00289 void TSGFromPropagation::validMeasurements(std::vector<TrajectoryMeasurement>& tms) const {
00290 
00291   std::vector<TrajectoryMeasurement>::iterator tmsend = std::remove_if(tms.begin(), tms.end(), isInvalid());
00292   tms.erase(tmsend, tms.end());
00293   return;
00294 
00295 }
00296 
00297 std::vector<TrajectoryMeasurement> TSGFromPropagation::findMeasurements_new(const DetLayer* nl, const TrajectoryStateOnSurface& staState) const {
00298 
00299   std::vector<TrajectoryMeasurement> result;
00300 
00301   std::vector<DetLayer::DetWithState> compatDets = nl->compatibleDets(staState, *propagator(), *estimator());
00302   if ( compatDets.empty() )  return result;
00303 
00304   for (std::vector<DetLayer::DetWithState>::const_iterator idws = compatDets.begin(); idws != compatDets.end(); ++idws) {
00305      if ( idws->second.isValid() && (idws->first) )  {
00306          std::vector<TrajectoryMeasurement> tmptm = 
00307            theMeasTracker->idToDet(idws->first->geographicalId())->fastMeasurements(idws->second, idws->second, *propagator(), *estimator());
00308          validMeasurements(tmptm);
00309 //         if ( tmptm.size() > 2 ) {
00310 //            std::stable_sort(tmptm.begin(),tmptm.end(),increasingEstimate());
00311 //            result.insert(result.end(),tmptm.begin(), tmptm.begin()+2);
00312 //         } else {
00313             result.insert(result.end(),tmptm.begin(), tmptm.end());
00314 //         }
00315      }
00316   }
00317   
00318   return result;
00319 
00320 }
00321 
00322 std::vector<TrajectoryMeasurement> TSGFromPropagation::findMeasurements(const DetLayer* nl, const TrajectoryStateOnSurface& staState) const {
00323 
00324   std::vector<TrajectoryMeasurement> result = tkLayerMeasurements()->measurements((*nl), staState, *propagator(), *estimator());
00325   validMeasurements(result);
00326   return result;
00327 }
00328 
00329 bool TSGFromPropagation::passSelection(const TrajectoryStateOnSurface& tsos) const {
00330   if ( !theSelectStateFlag ) return true;
00331   else {
00332      if ( beamSpot.isValid() ) {
00333        return ( ( fabs(zDis(tsos) - beamSpot->z0() ) < theSigmaZ) );
00334 
00335      } else {
00336        return ( ( fabs(zDis(tsos)) < theSigmaZ) ); 
00337 //      double theDxyCut = 100;
00338 //      return ( (zDis(tsos) < theSigmaZ) && (dxyDis(tsos) < theDxyCut) );
00339      }
00340   }
00341 
00342 }
00343 
00344 double TSGFromPropagation::dxyDis(const TrajectoryStateOnSurface& tsos) const {
00345   return fabs(( - tsos.globalPosition().x() * tsos.globalMomentum().y() + tsos.globalPosition().y() * tsos.globalMomentum().x() )/tsos.globalMomentum().perp());
00346 }
00347 
00348 double TSGFromPropagation::zDis(const TrajectoryStateOnSurface& tsos) const {
00349   return tsos.globalPosition().z() - tsos.globalPosition().perp() * tsos.globalMomentum().z()/tsos.globalMomentum().perp();
00350 }
00351 
00352 void TSGFromPropagation::getRescalingFactor(const TrackCand& staMuon) {
00353     float pt = (staMuon.second)->pt();
00354     if ( pt < 13.0 ) theFlexErrorRescaling = 3; 
00355     else if ( pt < 30.0 ) theFlexErrorRescaling = 5;
00356     else theFlexErrorRescaling = 10;
00357     return;
00358 }
00359 
00360 
00361 void TSGFromPropagation::adjust(FreeTrajectoryState & state) const {
00362 
00363   //rescale the error
00364   if ( theResetMethod == "discreate" ) {
00365      state.rescaleError(theFlexErrorRescaling);
00366      return;
00367   }
00368 
00369   //rescale the error
00370   if ( theResetMethod == "fixed" || !theErrorMatrixAdjuster) {
00371      state.rescaleError(theFixedErrorRescaling);
00372      return;
00373   }
00374 
00375   CurvilinearTrajectoryError oMat = state.curvilinearError();
00376   CurvilinearTrajectoryError sfMat = theErrorMatrixAdjuster->get(state.momentum());//FIXME with position
00377   MuonErrorMatrix::multiply(oMat, sfMat);
00378   
00379   state = FreeTrajectoryState(state.parameters(),
00380                               oMat);
00381 }
00382 
00383 void TSGFromPropagation::adjust(TrajectoryStateOnSurface & state) const {
00384 
00385   //rescale the error
00386   if ( theResetMethod == "discreate" ) {
00387      state.rescaleError(theFlexErrorRescaling);
00388      return;
00389   }
00390 
00391   if ( theResetMethod == "fixed" || !theErrorMatrixAdjuster) {
00392      state.rescaleError(theFixedErrorRescaling);
00393      return;
00394   }
00395 
00396   CurvilinearTrajectoryError oMat = state.curvilinearError();
00397   CurvilinearTrajectoryError sfMat = theErrorMatrixAdjuster->get(state.globalMomentum());//FIXME with position
00398   MuonErrorMatrix::multiply(oMat, sfMat);
00399   
00400   state = TrajectoryStateOnSurface(state.globalParameters(),
00401                                    oMat,
00402                                    state.surface(),
00403                                    state.surfaceSide(),
00404                                    state.weight());
00405 }
00406