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 #include "FWCore/ServiceRegistry/interface/Service.h"
00029 #include "PhysicsTools/UtilAlgos/interface/TFileService.h"
00030
00031
00032 TSGFromPropagation::TSGFromPropagation(const edm::ParameterSet & iConfig) :theTkLayerMeasurements (0), theTracker(0), theMeasTracker(0), theNavigation(0), theService(0), theEstimator(0), theTSTransformer(0), theConfig (iConfig)
00033 {
00034 theCategory = "Muon|RecoMuon|TSGFromPropagation";
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), theConfig (iConfig)
00038 {
00039 theCategory = "Muon|RecoMuon|TSGFromPropagation";
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 TrajectoryStateOnSurface staState = outerTkState(staMuon);
00058
00059 if ( !staState.isValid() ) {
00060 LogTrace(theCategory) << "Error: initial state from L2 muon is invalid.";
00061 return;
00062 }
00063
00064 LogTrace(theCategory) << "begin of trackerSeed:\n staState pos: "<<staState.globalPosition()
00065 << " mom: "<<staState.globalMomentum()
00066 <<"pos eta: "<<staState.globalPosition().eta()
00067 <<"mom eta: "<<staState.globalMomentum().eta();
00068
00069 std::vector<const DetLayer*> nls = theNavigation->compatibleLayers(*(staState.freeState()), oppositeToMomentum);
00070
00071 LogTrace(theCategory) << " compatible layers: "<<nls.size();
00072
00073 if ( nls.empty() ) return;
00074
00075 int ndesLayer = 0;
00076
00077 bool usePredictedState = false;
00078
00079 if ( theUpdateStateFlag ) {
00080 std::vector<TrajectoryMeasurement> alltm;
00081
00082 for (std::vector<const DetLayer*>::const_iterator inl = nls.begin();
00083 inl != nls.end(); inl++, ndesLayer++ ) {
00084 if ( (*inl == 0) ) break;
00085 alltm = findMeasurements_new(*inl, staState);
00086 if ( (!alltm.empty()) ) {
00087 LogTrace(theCategory) << "final compatible layer: "<<ndesLayer;
00088 break;
00089 }
00090 }
00091
00092 if ( alltm.empty() ) {
00093 LogTrace(theCategory) << " NO Measurements Found: eta: "<<staState.globalPosition().eta() <<"pt "<<staState.globalMomentum().perp();
00094 usePredictedState = true;
00095 } else {
00096 LogTrace(theCategory) << " Measurements for seeds: "<<alltm.size();
00097 std::stable_sort(alltm.begin(),alltm.end(),increasingEstimate());
00098 if ( alltm.size() > 5 ) alltm.erase(alltm.begin() + 5, alltm.end());
00099
00100 int i = 0;
00101 for (std::vector<TrajectoryMeasurement>::const_iterator itm = alltm.begin();
00102 itm != alltm.end(); itm++, i++) {
00103 TrajectoryStateOnSurface updatedTSOS = updator()->update(itm->predictedState(), *(itm->recHit()));
00104 if ( updatedTSOS.isValid() && passSelection(updatedTSOS) ) {
00105 edm::OwnVector<TrackingRecHit> container;
00106 container.push_back(itm->recHit()->hit()->clone());
00107 TrajectorySeed ts = createSeed(updatedTSOS, container, itm->recHit()->geographicalId());
00108 result.push_back(ts);
00109 }
00110 }
00111 return;
00112 }
00113 }
00114
00115 if ( !theUpdateStateFlag || usePredictedState ) {
00116 LogTrace(theCategory) << "use predicted state: ";
00117 for (std::vector<const DetLayer*>::const_iterator inl = nls.begin();
00118 inl != nls.end(); inl++ ) {
00119
00120 if ( !result.empty() || *inl == 0 ) {
00121 break;
00122 }
00123 std::vector<DetLayer::DetWithState> compatDets = (*inl)->compatibleDets(staState, *propagator(), *estimator());
00124 LogTrace(theCategory) << " compatDets "<<compatDets.size();
00125 if ( compatDets.empty() ) continue;
00126 TrajectorySeed ts = createSeed(compatDets.front().second, compatDets.front().first->geographicalId());
00127 result.push_back(ts);
00128
00129 }
00130 LogTrace(theCategory) << "result: "<<result.size();
00131 return;
00132 }
00133 return;
00134 }
00135
00136 void TSGFromPropagation::init(const MuonServiceProxy* service) {
00137
00138 theMaxChi2 = theConfig.getParameter<double>("MaxChi2");
00139
00140 theErrorRescaling = theConfig.getParameter<double>("ErrorRescaling");
00141
00142 theEstimator = new Chi2MeasurementEstimator(theMaxChi2);
00143
00144 theCacheId_MT = 0;
00145
00146 thePropagatorName = theConfig.getParameter<std::string>("Propagator");
00147
00148 theService = service;
00149
00150 theUseVertexStateFlag = theConfig.getParameter<bool>("UseVertexState");
00151
00152 theUpdateStateFlag = theConfig.getParameter<bool>("UpdateState");
00153
00154 theUseSecondMeasurementsFlag = theConfig.getParameter<bool>("UseSecondMeasurements");
00155
00156 theSelectStateFlag = theConfig.getParameter<bool>("SelectState");
00157
00158 theUpdator = new KFUpdator();
00159
00160 theTSTransformer = new TrajectoryStateTransform();
00161
00162 edm::ParameterSet errorMatrixPset = theConfig.getParameter<edm::ParameterSet>("errorMatrixPset");
00163 if (!errorMatrixPset.empty()){
00164 theAdjustAtIp = errorMatrixPset.getParameter<bool>("atIP");
00165 theErrorMatrixAdjuster = new MuonErrorMatrix(errorMatrixPset);
00166 } else {
00167 theAdjustAtIp =false;
00168 theErrorMatrixAdjuster=0;
00169 }
00170
00171 theService->eventSetup().get<TrackerRecoGeometryRecord>().get(theTracker);
00172 theNavigation = new DirectTrackerNavigation(theTracker);
00173
00174 }
00175
00176 void TSGFromPropagation::setEvent(const edm::Event& iEvent) {
00177
00178 bool measTrackerChanged = false;
00179
00180 unsigned long long newCacheId_MT = theService->eventSetup().get<CkfComponentsRecord>().cacheIdentifier();
00181
00182 if ( newCacheId_MT != theCacheId_MT ) {
00183 LogTrace(theCategory) << "Measurment Tracker Geometry changed!";
00184 theCacheId_MT = newCacheId_MT;
00185 theService->eventSetup().get<CkfComponentsRecord>().get(theMeasTracker);
00186 measTrackerChanged = true;
00187 }
00188
00189 theMeasTracker->update(iEvent);
00190
00191 if ( measTrackerChanged && (&*theMeasTracker) ) {
00192 if ( theTkLayerMeasurements ) delete theTkLayerMeasurements;
00193 theTkLayerMeasurements = new LayerMeasurements(&*theMeasTracker);
00194 }
00195
00196 }
00197
00198 TrajectoryStateOnSurface TSGFromPropagation::innerState(const TrackCand& staMuon) const {
00199
00200 TrajectoryStateOnSurface innerTS;
00201
00202 if ( staMuon.first && staMuon.first->isValid() ) {
00203 if (staMuon.first->direction() == alongMomentum) {
00204 innerTS = staMuon.first->firstMeasurement().updatedState();
00205 }
00206 else if (staMuon.first->direction() == oppositeToMomentum) {
00207 innerTS = staMuon.first->lastMeasurement().updatedState();
00208 }
00209 } else {
00210 innerTS = theTSTransformer->innerStateOnSurface(*(staMuon.second),*theService->trackingGeometry(), &*theService->magneticField());
00211 }
00212
00213 if (theErrorMatrixAdjuster && !theAdjustAtIp) adjust(innerTS);
00214 else innerTS.rescaleError(theErrorRescaling);
00215
00216 return innerTS;
00217
00218
00219 }
00220
00221 TrajectoryStateOnSurface TSGFromPropagation::outerTkState(const TrackCand& staMuon) const {
00222
00223 TrajectoryStateOnSurface result;
00224
00225 if ( theUseVertexStateFlag && staMuon.second->pt() > 1.0 ) {
00226 FreeTrajectoryState iniState = theTSTransformer->initialFreeState(*(staMuon.second), &*theService->magneticField());
00227
00228 if (theErrorMatrixAdjuster && theAdjustAtIp){ adjust(iniState); }
00229 else iniState.rescaleError(theErrorRescaling);
00230
00231 StateOnTrackerBound fromInside(&*(theService->propagator("PropagatorWithMaterial")));
00232 result = fromInside(iniState);
00233 } else {
00234 StateOnTrackerBound fromOutside(&*propagator());
00235 result = fromOutside(innerState(staMuon));
00236 }
00237 return result;
00238 }
00239
00240 TrajectorySeed TSGFromPropagation::createSeed(const TrajectoryStateOnSurface& tsos, const DetId& id) const {
00241
00242 edm::OwnVector<TrackingRecHit> container;
00243 return createSeed(tsos, container, id);
00244
00245 }
00246
00247 TrajectorySeed TSGFromPropagation::createSeed(const TrajectoryStateOnSurface& tsos, const edm::OwnVector<TrackingRecHit>& container, const DetId& id) const {
00248
00249 PTrajectoryStateOnDet* seedTSOS = theTSTransformer->persistentState(tsos,id.rawId());
00250 return TrajectorySeed(*seedTSOS,container,oppositeToMomentum);
00251
00252 }
00253
00255 void TSGFromPropagation::selectMeasurements(std::vector<TrajectoryMeasurement>& tms) const {
00256
00257 }
00258
00259
00260 void TSGFromPropagation::validMeasurements(std::vector<TrajectoryMeasurement>& tms) const {
00261
00262 std::vector<TrajectoryMeasurement>::iterator tmsend = std::remove_if(tms.begin(), tms.end(), isInvalid());
00263 tms.erase(tmsend, tms.end());
00264 return;
00265
00266 }
00267
00268 std::vector<TrajectoryMeasurement> TSGFromPropagation::findMeasurements_new(const DetLayer* nl, const TrajectoryStateOnSurface& staState) const {
00269
00270 std::vector<TrajectoryMeasurement> result;
00271
00272 std::vector<DetLayer::DetWithState> compatDets = nl->compatibleDets(staState, *propagator(), *estimator());
00273 if ( compatDets.empty() ) return result;
00274
00275 for (std::vector<DetLayer::DetWithState>::const_iterator idws = compatDets.begin(); idws != compatDets.end(); ++idws) {
00276 if ( idws->second.isValid() && (idws->first) ) {
00277 std::vector<TrajectoryMeasurement> tmptm =
00278 theMeasTracker->idToDet(idws->first->geographicalId())->fastMeasurements(idws->second, idws->second, *propagator(), *estimator());
00279 validMeasurements(tmptm);
00280
00281
00282
00283
00284 result.insert(result.end(),tmptm.begin(), tmptm.end());
00285
00286 }
00287 }
00288
00289 return result;
00290
00291 }
00292
00293 std::vector<TrajectoryMeasurement> TSGFromPropagation::findMeasurements(const DetLayer* nl, const TrajectoryStateOnSurface& staState) const {
00294
00295 std::vector<TrajectoryMeasurement> result = tkLayerMeasurements()->measurements((*nl), staState, *propagator(), *estimator());
00296 validMeasurements(result);
00297 return result;
00298 }
00299
00300 void TSGFromPropagation::findSecondMeasurements(std::vector<TrajectoryMeasurement>& tms, const std::vector<const DetLayer*>& dls) const {
00301
00302 }
00303
00304 bool TSGFromPropagation::passSelection(const TrajectoryStateOnSurface& tsos) const {
00305 if ( !theSelectStateFlag ) return true;
00306 else {
00307 double theSigmaZ = 22;
00308 return ( (zDis(tsos) < theSigmaZ) );
00309 }
00310
00311
00312
00313 }
00314
00315 double TSGFromPropagation::dxyDis(const TrajectoryStateOnSurface& tsos) const {
00316 return fabs(( - tsos.globalPosition().x() * tsos.globalMomentum().y() + tsos.globalPosition().y() * tsos.globalMomentum().x() )/tsos.globalMomentum().perp());
00317 }
00318
00319 double TSGFromPropagation::zDis(const TrajectoryStateOnSurface& tsos) const {
00320 return fabs(tsos.globalPosition().z() - tsos.globalPosition().perp() * tsos.globalMomentum().z()/tsos.globalMomentum().perp());
00321 }
00322
00323 void TSGFromPropagation::adjust(FreeTrajectoryState & state) const {
00324 CurvilinearTrajectoryError oMat = state.curvilinearError();
00325 CurvilinearTrajectoryError sfMat = theErrorMatrixAdjuster->get(state.momentum());
00326 MuonErrorMatrix::multiply(oMat, sfMat);
00327
00328 state = FreeTrajectoryState(state.parameters(),
00329 oMat);
00330 }
00331
00332 void TSGFromPropagation::adjust(TrajectoryStateOnSurface & state) const {
00333 CurvilinearTrajectoryError oMat = state.curvilinearError();
00334 CurvilinearTrajectoryError sfMat = theErrorMatrixAdjuster->get(state.globalMomentum());
00335 MuonErrorMatrix::multiply(oMat, sfMat);
00336
00337 state = TrajectoryStateOnSurface(state.globalParameters(),
00338 oMat,
00339 state.surface(),
00340 state.surfaceSide(),
00341 state.weight());
00342 }
00343