CMS 3D CMS Logo

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