CMS 3D CMS Logo

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