CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/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/KalmanUpdators/interface/Chi2MeasurementEstimator.h"
00015 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
00016 #include "TrackingTools/GeomPropagators/interface/StateOnTrackerBound.h"
00017 
00018 #include "RecoTracker/Record/interface/TrackerRecoGeometryRecord.h"
00019 #include "RecoTracker/Record/interface/CkfComponentsRecord.h"
00020 #include "RecoTracker/MeasurementDet/interface/TkStripMeasurementDet.h"
00021 #include "RecoTracker/MeasurementDet/interface/MeasurementTracker.h"
00022 #include "RecoTracker/TkDetLayers/interface/GeometricSearchTracker.h"
00023 
00024 #include "RecoMuon/GlobalTrackingTools/interface/DirectTrackerNavigation.h"
00025 #include "TrackingTools/KalmanUpdators/interface/KFUpdator.h"
00026 
00027 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00028 
00029 TSGFromPropagation::TSGFromPropagation(const edm::ParameterSet & iConfig) :theTkLayerMeasurements (0), theTracker(0), theMeasTracker(0), theNavigation(0), theService(0), theEstimator(0), theTSTransformer(0), theSigmaZ(0), theConfig (iConfig)
00030 {
00031   theCategory = "Muon|RecoMuon|TSGFromPropagation";
00032   theMeasTrackerName = iConfig.getParameter<std::string>("MeasurementTrackerName");
00033 
00034 }
00035 
00036 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)
00037 {
00038   theCategory = "Muon|RecoMuon|TSGFromPropagation";
00039   theMeasTrackerName = iConfig.getParameter<std::string>("MeasurementTrackerName");
00040 }
00041 
00042 TSGFromPropagation::~TSGFromPropagation()
00043 {
00044 
00045   LogTrace(theCategory) << " TSGFromPropagation dtor called ";
00046   if ( theNavigation ) delete theNavigation;
00047   if ( theUpdator ) delete theUpdator;
00048   if ( theEstimator ) delete theEstimator;
00049   if ( theTkLayerMeasurements ) delete theTkLayerMeasurements;
00050   if ( theTSTransformer ) delete  theTSTransformer;
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   theTSTransformer = new TrajectoryStateTransform();
00177 
00178   theSigmaZ = theConfig.getParameter<double>("SigmaZ");
00179 
00180   theBeamSpotInputTag = theConfig.getParameter<edm::InputTag>("beamSpot");
00181 
00182   edm::ParameterSet errorMatrixPset = theConfig.getParameter<edm::ParameterSet>("errorMatrixPset");
00183   if ( theResetMethod == "matrix" && !errorMatrixPset.empty()){
00184     theAdjustAtIp = errorMatrixPset.getParameter<bool>("atIP");
00185     theErrorMatrixAdjuster = new MuonErrorMatrix(errorMatrixPset);
00186   } else {
00187     theAdjustAtIp =false;
00188     theErrorMatrixAdjuster=0;
00189   }
00190 
00191   theService->eventSetup().get<TrackerRecoGeometryRecord>().get(theTracker); 
00192   theNavigation = new DirectTrackerNavigation(theTracker);
00193 
00194 }
00195 
00196 void TSGFromPropagation::setEvent(const edm::Event& iEvent) {
00197 
00198   bool measTrackerChanged = false;
00199 
00200   //edm::Handle<reco::BeamSpot> beamSpot;
00201   iEvent.getByLabel(theBeamSpotInputTag, beamSpot);
00202 
00203   unsigned long long newCacheId_MT = theService->eventSetup().get<CkfComponentsRecord>().cacheIdentifier();
00204 
00205   if ( theUpdateStateFlag && newCacheId_MT != theCacheId_MT ) {
00206     LogTrace(theCategory) << "Measurment Tracker Geometry changed!";
00207     theCacheId_MT = newCacheId_MT;
00208     theService->eventSetup().get<CkfComponentsRecord>().get(theMeasTrackerName,theMeasTracker);
00209     measTrackerChanged = true;
00210   }
00211 
00212   if ( theUpdateStateFlag ) theMeasTracker->update(iEvent);
00213 
00214   if ( measTrackerChanged && (&*theMeasTracker) ) {
00215      if ( theTkLayerMeasurements ) delete theTkLayerMeasurements;
00216      theTkLayerMeasurements = new LayerMeasurements(&*theMeasTracker);
00217   }
00218 
00219   bool trackerGeomChanged = false;
00220 
00221   unsigned long long newCacheId_TG = theService->eventSetup().get<TrackerRecoGeometryRecord>().cacheIdentifier();
00222 
00223   if ( newCacheId_TG != theCacheId_TG ) {
00224     LogTrace(theCategory) << "Tracker Reco Geometry changed!";
00225     theCacheId_TG = newCacheId_TG;
00226     theService->eventSetup().get<TrackerRecoGeometryRecord>().get(theTracker);
00227     trackerGeomChanged = true;
00228   }
00229 
00230   if ( trackerGeomChanged && (&*theTracker) ) {
00231     if ( theNavigation ) delete theNavigation;
00232     theNavigation = new DirectTrackerNavigation(theTracker);
00233   }
00234 }
00235 
00236 TrajectoryStateOnSurface TSGFromPropagation::innerState(const TrackCand& staMuon) const {
00237 
00238   TrajectoryStateOnSurface innerTS;
00239 
00240   if ( staMuon.first && staMuon.first->isValid() ) {
00241     if (staMuon.first->direction() == alongMomentum) {
00242       innerTS = staMuon.first->firstMeasurement().updatedState();
00243     } 
00244     else if (staMuon.first->direction() == oppositeToMomentum) { 
00245       innerTS = staMuon.first->lastMeasurement().updatedState();
00246     }
00247   } else {
00248     innerTS = theTSTransformer->innerStateOnSurface(*(staMuon.second),*theService->trackingGeometry(), &*theService->magneticField());
00249   }
00250   //rescale the error
00251   adjust(innerTS);
00252 
00253   return  innerTS;
00254 
00255 //    return theTSTransformer->innerStateOnSurface(*(staMuon.second),*theService->trackingGeometry(), &*theService->magneticField());
00256 }
00257 
00258 TrajectoryStateOnSurface TSGFromPropagation::outerTkState(const TrackCand& staMuon) const {
00259 
00260   TrajectoryStateOnSurface result;
00261 
00262   if ( theUseVertexStateFlag && staMuon.second->pt() > 1.0 ) {
00263     FreeTrajectoryState iniState = theTSTransformer->initialFreeState(*(staMuon.second), &*theService->magneticField());
00264     //rescale the error at IP
00265     adjust(iniState); 
00266 
00267     StateOnTrackerBound fromInside(&*(theService->propagator("PropagatorWithMaterial")));
00268     result = fromInside(iniState);
00269   } else {
00270     StateOnTrackerBound fromOutside(&*propagator());
00271     result = fromOutside(innerState(staMuon));
00272   }
00273   return result;
00274 }
00275 
00276 TrajectorySeed TSGFromPropagation::createSeed(const TrajectoryStateOnSurface& tsos, const DetId& id) const {
00277 
00278   edm::OwnVector<TrackingRecHit> container;
00279   return createSeed(tsos, container, id);
00280 
00281 }
00282 
00283 TrajectorySeed TSGFromPropagation::createSeed(const TrajectoryStateOnSurface& tsos, const edm::OwnVector<TrackingRecHit>& container, const DetId& id) const {
00284 
00285   PTrajectoryStateOnDet* seedTSOS = theTSTransformer->persistentState(tsos,id.rawId());
00286   return TrajectorySeed(*seedTSOS,container,oppositeToMomentum);
00287 
00288 }
00289 
00290 
00291 void TSGFromPropagation::validMeasurements(std::vector<TrajectoryMeasurement>& tms) const {
00292 
00293   std::vector<TrajectoryMeasurement>::iterator tmsend = std::remove_if(tms.begin(), tms.end(), isInvalid());
00294   tms.erase(tmsend, tms.end());
00295   return;
00296 
00297 }
00298 
00299 std::vector<TrajectoryMeasurement> TSGFromPropagation::findMeasurements_new(const DetLayer* nl, const TrajectoryStateOnSurface& staState) const {
00300 
00301   std::vector<TrajectoryMeasurement> result;
00302 
00303   std::vector<DetLayer::DetWithState> compatDets = nl->compatibleDets(staState, *propagator(), *estimator());
00304   if ( compatDets.empty() )  return result;
00305 
00306   for (std::vector<DetLayer::DetWithState>::const_iterator idws = compatDets.begin(); idws != compatDets.end(); ++idws) {
00307      if ( idws->second.isValid() && (idws->first) )  {
00308          std::vector<TrajectoryMeasurement> tmptm = 
00309            theMeasTracker->idToDet(idws->first->geographicalId())->fastMeasurements(idws->second, idws->second, *propagator(), *estimator());
00310          validMeasurements(tmptm);
00311 //         if ( tmptm.size() > 2 ) {
00312 //            std::stable_sort(tmptm.begin(),tmptm.end(),increasingEstimate());
00313 //            result.insert(result.end(),tmptm.begin(), tmptm.begin()+2);
00314 //         } else {
00315             result.insert(result.end(),tmptm.begin(), tmptm.end());
00316 //         }
00317      }
00318   }
00319   
00320   return result;
00321 
00322 }
00323 
00324 std::vector<TrajectoryMeasurement> TSGFromPropagation::findMeasurements(const DetLayer* nl, const TrajectoryStateOnSurface& staState) const {
00325 
00326   std::vector<TrajectoryMeasurement> result = tkLayerMeasurements()->measurements((*nl), staState, *propagator(), *estimator());
00327   validMeasurements(result);
00328   return result;
00329 }
00330 
00331 bool TSGFromPropagation::passSelection(const TrajectoryStateOnSurface& tsos) const {
00332   if ( !theSelectStateFlag ) return true;
00333   else {
00334      if ( beamSpot.isValid() ) {
00335        return ( ( fabs(zDis(tsos) - beamSpot->z0() ) < theSigmaZ) );
00336 
00337      } else {
00338        return ( ( fabs(zDis(tsos)) < theSigmaZ) ); 
00339 //      double theDxyCut = 100;
00340 //      return ( (zDis(tsos) < theSigmaZ) && (dxyDis(tsos) < theDxyCut) );
00341      }
00342   }
00343 
00344 }
00345 
00346 double TSGFromPropagation::dxyDis(const TrajectoryStateOnSurface& tsos) const {
00347   return fabs(( - tsos.globalPosition().x() * tsos.globalMomentum().y() + tsos.globalPosition().y() * tsos.globalMomentum().x() )/tsos.globalMomentum().perp());
00348 }
00349 
00350 double TSGFromPropagation::zDis(const TrajectoryStateOnSurface& tsos) const {
00351   return tsos.globalPosition().z() - tsos.globalPosition().perp() * tsos.globalMomentum().z()/tsos.globalMomentum().perp();
00352 }
00353 
00354 void TSGFromPropagation::getRescalingFactor(const TrackCand& staMuon) {
00355     float pt = (staMuon.second)->pt();
00356     if ( pt < 13.0 ) theFlexErrorRescaling = 3; 
00357     else if ( pt < 30.0 ) theFlexErrorRescaling = 5;
00358     else theFlexErrorRescaling = 10;
00359     return;
00360 }
00361 
00362 
00363 void TSGFromPropagation::adjust(FreeTrajectoryState & state) const {
00364 
00365   //rescale the error
00366   if ( theResetMethod == "discreate" ) {
00367      state.rescaleError(theFlexErrorRescaling);
00368      return;
00369   }
00370 
00371   //rescale the error
00372   if ( theResetMethod == "fixed" || !theErrorMatrixAdjuster) {
00373      state.rescaleError(theFixedErrorRescaling);
00374      return;
00375   }
00376 
00377   CurvilinearTrajectoryError oMat = state.curvilinearError();
00378   CurvilinearTrajectoryError sfMat = theErrorMatrixAdjuster->get(state.momentum());//FIXME with position
00379   MuonErrorMatrix::multiply(oMat, sfMat);
00380   
00381   state = FreeTrajectoryState(state.parameters(),
00382                               oMat);
00383 }
00384 
00385 void TSGFromPropagation::adjust(TrajectoryStateOnSurface & state) const {
00386 
00387   //rescale the error
00388   if ( theResetMethod == "discreate" ) {
00389      state.rescaleError(theFlexErrorRescaling);
00390      return;
00391   }
00392 
00393   if ( theResetMethod == "fixed" || !theErrorMatrixAdjuster) {
00394      state.rescaleError(theFixedErrorRescaling);
00395      return;
00396   }
00397 
00398   CurvilinearTrajectoryError oMat = state.curvilinearError();
00399   CurvilinearTrajectoryError sfMat = theErrorMatrixAdjuster->get(state.globalMomentum());//FIXME with position
00400   MuonErrorMatrix::multiply(oMat, sfMat);
00401   
00402   state = TrajectoryStateOnSurface(state.globalParameters(),
00403                                    oMat,
00404                                    state.surface(),
00405                                    state.surfaceSide(),
00406                                    state.weight());
00407 }
00408