00001
00026 #include "RecoMuon/GlobalTrackingTools/interface/GlobalTrajectoryBuilderBase.h"
00027
00028
00029
00030
00031
00032 #include <iostream>
00033 #include <algorithm>
00034
00035
00036
00037
00038
00039 #include "FWCore/Framework/interface/Event.h"
00040 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00041 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00042
00043 #include "CommonTools/Statistics/interface/ChiSquaredProbability.h"
00044 #include "TrackingTools/TrackFitters/interface/RecHitLessByDet.h"
00045 #include "TrackingTools/PatternTools/interface/TrajectoryMeasurement.h"
00046 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00047 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00048 #include "TrackingTools/TrackRefitter/interface/TrackTransformer.h"
00049
00050 #include "DataFormats/Math/interface/deltaR.h"
00051
00052 #include "DataFormats/DetId/interface/DetId.h"
00053 #include "DataFormats/MuonDetId/interface/DTChamberId.h"
00054 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
00055 #include "DataFormats/MuonDetId/interface/RPCDetId.h"
00056
00057 #include "DataFormats/TrackReco/interface/Track.h"
00058 #include "DataFormats/TrackReco/interface/TrackExtraFwd.h"
00059 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00060
00061 #include "RecoTracker/TkTrackingRegions/interface/RectangularEtaPhiTrackingRegion.h"
00062
00063 #include "RecoMuon/GlobalTrackingTools/interface/GlobalMuonTrackMatcher.h"
00064 #include "RecoMuon/GlobalTrackingTools/interface/GlobalMuonRefitter.h"
00065 #include "RecoMuon/TransientTrackingRecHit/interface/MuonTransientTrackingRecHitBuilder.h"
00066 #include "RecoMuon/TransientTrackingRecHit/interface/MuonTransientTrackingRecHit.h"
00067 #include "RecoMuon/TrackingTools/interface/MuonCandidate.h"
00068 #include "RecoMuon/TrackingTools/interface/MuonServiceProxy.h"
00069
00070 #include "RecoMuon/GlobalTrackingTools/interface/MuonTrackingRegionBuilder.h"
00071 #include "TrackingTools/Records/interface/TrackingComponentsRecord.h"
00072 #include "TrackingTools/Records/interface/TransientRecHitRecord.h"
00073 #include "TrackingTools/TrackFitters/interface/TrajectoryFitter.h"
00074 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHitBuilder.h"
00075 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00076
00077 #include "RecoTracker/TkTrackingRegions/interface/TkTrackingRegionsMargin.h"
00078 #include "RecoTracker/TkMSParametrization/interface/PixelRecoRange.h"
00079
00080 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2D.h"
00081 #include "RecoTracker/TransientTrackingRecHit/interface/TSiStripRecHit2DLocalPos.h"
00082 #include "Geometry/CommonTopologies/interface/StripTopology.h"
00083
00084 using namespace std;
00085 using namespace edm;
00086
00087
00088
00089
00090 GlobalTrajectoryBuilderBase::GlobalTrajectoryBuilderBase(const edm::ParameterSet& par,
00091 const MuonServiceProxy* service) :
00092 theTrackMatcher(0),theLayerMeasurements(0),theTrackTransformer(0),theRegionBuilder(0), theService(service),theGlbRefitter(0) {
00093
00094 theCategory = par.getUntrackedParameter<string>("Category", "Muon|RecoMuon|GlobalMuon|GlobalTrajectoryBuilderBase");
00095
00096
00097 ParameterSet trackMatcherPSet = par.getParameter<ParameterSet>("GlobalMuonTrackMatcher");
00098 theTrackMatcher = new GlobalMuonTrackMatcher(trackMatcherPSet,theService);
00099
00100 theTrackerPropagatorName = par.getParameter<string>("TrackerPropagator");
00101
00102 ParameterSet trackTransformerPSet = par.getParameter<ParameterSet>("TrackTransformer");
00103 theTrackTransformer = new TrackTransformer(trackTransformerPSet);
00104
00105 ParameterSet regionBuilderPSet = par.getParameter<ParameterSet>("MuonTrackingRegionBuilder");
00106
00107 theRegionBuilder = new MuonTrackingRegionBuilder(regionBuilderPSet,theService);
00108
00109
00110 ParameterSet refitterParameters = par.getParameter<ParameterSet>("GlbRefitterParameters");
00111 theGlbRefitter = new GlobalMuonRefitter(refitterParameters, theService);
00112
00113 theMuonHitsOption = refitterParameters.getParameter<int>("MuonHitsOption");
00114
00115 theTrackerRecHitBuilderName = par.getParameter<string>("TrackerRecHitBuilder");
00116 theMuonRecHitBuilderName = par.getParameter<string>("MuonRecHitBuilder");
00117
00118 theRPCInTheFit = par.getParameter<bool>("RefitRPCHits");
00119
00120 theTECxScale = par.getParameter<double>("ScaleTECxFactor");
00121 theTECyScale = par.getParameter<double>("ScaleTECyFactor");
00122 thePtCut = par.getParameter<double>("PtCut");
00123 thePCut = par.getParameter<double>("PCut");
00124
00125 theCacheId_TRH = 0;
00126
00127 }
00128
00129
00130
00131
00132
00133 GlobalTrajectoryBuilderBase::~GlobalTrajectoryBuilderBase() {
00134
00135 if (theTrackMatcher) delete theTrackMatcher;
00136 if (theRegionBuilder) delete theRegionBuilder;
00137 if (theTrackTransformer) delete theTrackTransformer;
00138 if (theGlbRefitter) delete theGlbRefitter;
00139 }
00140
00141
00142
00143
00144
00145 void GlobalTrajectoryBuilderBase::setEvent(const edm::Event& event) {
00146
00147 theEvent = &event;
00148
00149 theTrackTransformer->setServices(theService->eventSetup());
00150 theRegionBuilder->setEvent(event);
00151
00152 theGlbRefitter->setEvent(event);
00153 theGlbRefitter->setServices(theService->eventSetup());
00154
00155 unsigned long long newCacheId_TRH = theService->eventSetup().get<TransientRecHitRecord>().cacheIdentifier();
00156 if ( newCacheId_TRH != theCacheId_TRH ) {
00157 LogDebug(theCategory) << "TransientRecHitRecord changed!";
00158 theCacheId_TRH = newCacheId_TRH;
00159 theService->eventSetup().get<TransientRecHitRecord>().get(theTrackerRecHitBuilderName,theTrackerRecHitBuilder);
00160 theService->eventSetup().get<TransientRecHitRecord>().get(theMuonRecHitBuilderName,theMuonRecHitBuilder);
00161 }
00162
00163 }
00164
00165
00166
00167
00168
00169 MuonCandidate::CandidateContainer
00170 GlobalTrajectoryBuilderBase::build(const TrackCand& staCand,
00171 MuonCandidate::CandidateContainer& tkTrajs) const {
00172
00173 LogTrace(theCategory) << " Begin Build" << endl;
00174
00175
00176 if ( tkTrajs.empty() ) return CandidateContainer();
00177
00178
00179 CandidateContainer refittedResult;
00180 ConstRecHitContainer muonRecHits = getTransientRecHits(*(staCand.second));
00181
00182
00183 if ( (muonRecHits.size() > 1) &&
00184 ( muonRecHits.front()->globalPosition().mag() >
00185 muonRecHits.back()->globalPosition().mag() ) ) {
00186 LogTrace(theCategory)<< " reverse order: ";
00187 stable_sort(muonRecHits.begin(),muonRecHits.end(),RecHitLessByDet(alongMomentum));
00188 }
00189
00190 stable_sort(muonRecHits.begin(),muonRecHits.end(),ComparatorInOut());
00191
00192 for ( CandidateContainer::const_iterator it = tkTrajs.begin(); it != tkTrajs.end(); it++ ) {
00193
00194
00195 LogTrace(theCategory)<< " Track p and pT " << (*it)->trackerTrack()->p() << " " << (*it)->trackerTrack()->pt();
00196 if( (*it)->trackerTrack()->p() < thePCut || (*it)->trackerTrack()->pt() < thePtCut ) continue;
00197
00198 ConstRecHitContainer trackerRecHits;
00199 if ((*it)->trackerTrack().isNonnull()) {
00200 trackerRecHits = getTransientRecHits(*(*it)->trackerTrack());
00201 } else {
00202 LogDebug(theCategory)<<" NEED HITS FROM TRAJ";
00203
00204 }
00205
00206
00207 if ( fabs((*it)->trackerTrack()->eta()) > 0.95 && fabs((*it)->trackerTrack()->eta()) < 1.15 && (*it)->trackerTrack()->pt() < 60 ) {
00208 if ( theTECxScale < 0 || theTECyScale < 0 )
00209 trackerRecHits = selectTrackerHits(trackerRecHits);
00210 else
00211 fixTEC(trackerRecHits,theTECxScale,theTECyScale);
00212 }
00213
00214 RefitDirection recHitDir = checkRecHitsOrdering(trackerRecHits);
00215 if ( recHitDir == outToIn ) reverse(trackerRecHits.begin(),trackerRecHits.end());
00216
00217 reco::TransientTrack tTT((*it)->trackerTrack(),&*theService->magneticField(),theService->trackingGeometry());
00218 TrajectoryStateOnSurface innerTsos = tTT.innermostMeasurementState();
00219
00220 edm::RefToBase<TrajectorySeed> tmpSeed;
00221 if((*it)->trackerTrack()->seedRef().isAvailable()) tmpSeed = (*it)->trackerTrack()->seedRef();
00222
00223 if ( !innerTsos.isValid() ) {
00224 LogTrace(theCategory) << " inner Trajectory State is invalid. ";
00225 continue;
00226 }
00227
00228 innerTsos.rescaleError(100.);
00229
00230 TC refitted0,refitted1;
00231 MuonCandidate* finalTrajectory = 0;
00232 Trajectory *tkTrajectory = 0;
00233
00234
00235 if ( ! ((*it)->trackerTrajectory() && (*it)->trackerTrajectory()->isValid()) ) {
00236 refitted0 = theTrackTransformer->transform((*it)->trackerTrack()) ;
00237 if (!refitted0.empty()) tkTrajectory = new Trajectory(*(refitted0.begin()));
00238 else LogWarning(theCategory)<< " Failed to load tracker track trajectory";
00239 } else tkTrajectory = (*it)->trackerTrajectory();
00240 if (tkTrajectory) tkTrajectory->setSeedRef(tmpSeed);
00241
00242
00243 ConstRecHitContainer allRecHits = trackerRecHits;
00244 allRecHits.insert(allRecHits.end(), muonRecHits.begin(),muonRecHits.end());
00245 refitted1 = theGlbRefitter->refit( *(*it)->trackerTrack(), tTT, allRecHits,theMuonHitsOption);
00246 LogTrace(theCategory)<<" This track-sta refitted to " << refitted1.size() << " trajectories";
00247
00248 Trajectory *glbTrajectory1 = 0;
00249 if (!refitted1.empty()) glbTrajectory1 = new Trajectory(*(refitted1.begin()));
00250 else LogDebug(theCategory)<< " Failed to load global track trajectory 1";
00251 if (glbTrajectory1) glbTrajectory1->setSeedRef(tmpSeed);
00252
00253 finalTrajectory = 0;
00254 if(glbTrajectory1 && tkTrajectory) finalTrajectory = new MuonCandidate(glbTrajectory1, (*it)->muonTrack(), (*it)->trackerTrack(),
00255 tkTrajectory? new Trajectory(*tkTrajectory) : 0);
00256
00257 if ( finalTrajectory )
00258 refittedResult.push_back(finalTrajectory);
00259
00260 if(tkTrajectory) delete tkTrajectory;
00261 }
00262
00263
00264 CandidateContainer selectedResult;
00265 MuonCandidate* tmpCand = 0;
00266 if ( refittedResult.size() > 0 ) tmpCand = *(refittedResult.begin());
00267 double minProb = 9999;
00268
00269 for (CandidateContainer::const_iterator iter=refittedResult.begin(); iter != refittedResult.end(); iter++) {
00270 double prob = trackProbability(*(*iter)->trajectory());
00271 LogTrace(theCategory)<<" refitted-track-sta with pT " << (*iter)->trackerTrack()->pt() << " has probability " << prob;
00272
00273 if (prob < minProb) {
00274 minProb = prob;
00275 tmpCand = (*iter);
00276 }
00277 }
00278
00279 if ( tmpCand ) selectedResult.push_back(new MuonCandidate(new Trajectory(*(tmpCand->trajectory())), tmpCand->muonTrack(), tmpCand->trackerTrack(),
00280 (tmpCand->trackerTrajectory())? new Trajectory( *(tmpCand->trackerTrajectory()) ):0 ) );
00281
00282 for (CandidateContainer::const_iterator it = refittedResult.begin(); it != refittedResult.end(); ++it) {
00283 if ( (*it)->trajectory() ) delete (*it)->trajectory();
00284 if ( (*it)->trackerTrajectory() ) delete (*it)->trackerTrajectory();
00285 if ( *it ) delete (*it);
00286 }
00287 refittedResult.clear();
00288
00289 return selectedResult;
00290
00291 }
00292
00293
00294
00295
00296
00297 vector<GlobalTrajectoryBuilderBase::TrackCand>
00298 GlobalTrajectoryBuilderBase::chooseRegionalTrackerTracks(const TrackCand& staCand,
00299 const vector<TrackCand>& tkTs) {
00300
00301
00302 RectangularEtaPhiTrackingRegion regionOfInterest = defineRegionOfInterest(staCand.second);
00303
00304
00305 PixelRecoRange<float> etaRange = regionOfInterest.etaRange();
00306 TkTrackingRegionsMargin<float> phiMargin = regionOfInterest.phiMargin();
00307
00308 vector<TrackCand> result;
00309
00310 double deltaR_max = 1.0;
00311
00312 for ( vector<TrackCand>::const_iterator is = tkTs.begin(); is != tkTs.end(); ++is ) {
00313
00314
00315
00316
00317 double deltaR_tmp = deltaR(static_cast<double>(regionOfInterest.direction().eta()),
00318 static_cast<double>(regionOfInterest.direction().phi()),
00319 is->second->eta(), is->second->phi());
00320
00321
00322
00323 if (deltaR_tmp < deltaR_max) {
00324 TrackCand tmpCand = TrackCand(*is);
00325 result.push_back(tmpCand);
00326 }
00327 }
00328
00329 return result;
00330
00331 }
00332
00333
00334
00335
00336
00337 RectangularEtaPhiTrackingRegion
00338 GlobalTrajectoryBuilderBase::defineRegionOfInterest(const reco::TrackRef& staTrack) const {
00339
00340 RectangularEtaPhiTrackingRegion* region1 = theRegionBuilder->region(staTrack);
00341
00342 TkTrackingRegionsMargin<float> etaMargin(fabs(region1->etaRange().min() - region1->etaRange().mean()),
00343 fabs(region1->etaRange().max() - region1->etaRange().mean()));
00344
00345 RectangularEtaPhiTrackingRegion region2(region1->direction(),
00346 region1->origin(),
00347 region1->ptMin(),
00348 region1->originRBound(),
00349 region1->originZBound(),
00350 etaMargin,
00351 region1->phiMargin());
00352
00353 delete region1;
00354 return region2;
00355
00356 }
00357
00358
00359
00360
00361
00362 double
00363 GlobalTrajectoryBuilderBase::trackProbability(const Trajectory& track) const {
00364
00365 if ( track.ndof() > 0 && track.chiSquared() > 0 ) {
00366 return -LnChiSquaredProbability(track.chiSquared(), track.ndof());
00367 } else {
00368 return 0.0;
00369 }
00370
00371 }
00372
00373
00374
00375
00376
00377 void GlobalTrajectoryBuilderBase::printHits(const ConstRecHitContainer& hits) const {
00378
00379 LogTrace(theCategory) << "Used RecHits: " << hits.size();
00380 for (ConstRecHitContainer::const_iterator ir = hits.begin(); ir != hits.end(); ir++ ) {
00381 if ( !(*ir)->isValid() ) {
00382 LogTrace(theCategory) << "invalid RecHit";
00383 continue;
00384 }
00385
00386 const GlobalPoint& pos = (*ir)->globalPosition();
00387
00388 LogTrace(theCategory)
00389 << "r = " << sqrt(pos.x() * pos.x() + pos.y() * pos.y())
00390 << " z = " << pos.z()
00391 << " dimension = " << (*ir)->dimension()
00392 << " " << (*ir)->det()->geographicalId().det()
00393 << " " << (*ir)->det()->subDetector();
00394
00395 }
00396
00397 }
00398
00399
00400
00401
00402 GlobalTrajectoryBuilderBase::RefitDirection
00403 GlobalTrajectoryBuilderBase::checkRecHitsOrdering(const TransientTrackingRecHit::ConstRecHitContainer& recHits) const {
00404
00405 if ( !recHits.empty() ) {
00406 ConstRecHitContainer::const_iterator frontHit = recHits.begin();
00407 ConstRecHitContainer::const_iterator backHit = recHits.end() - 1;
00408 while ( !(*frontHit)->isValid() && frontHit != backHit ) {frontHit++;}
00409 while ( !(*backHit)->isValid() && backHit != frontHit ) {backHit--;}
00410
00411 double rFirst = (*frontHit)->globalPosition().mag();
00412 double rLast = (*backHit) ->globalPosition().mag();
00413
00414 if ( rFirst < rLast ) return inToOut;
00415 else if (rFirst > rLast) return outToIn;
00416 else {
00417 LogError(theCategory) << "Impossible to determine the rechits order" << endl;
00418 return undetermined;
00419 }
00420 }
00421 else {
00422 LogError(theCategory) << "Impossible to determine the rechits order" << endl;
00423 return undetermined;
00424 }
00425 }
00426
00427
00428
00429
00430
00431 GlobalTrajectoryBuilderBase::ConstRecHitContainer
00432 GlobalTrajectoryBuilderBase::selectTrackerHits(const ConstRecHitContainer& all) const {
00433
00434 int nTEC(0);
00435
00436 ConstRecHitContainer hits;
00437 for (ConstRecHitContainer::const_iterator i = all.begin(); i != all.end(); i++) {
00438 if ( !(*i)->isValid() ) continue;
00439 if ( (*i)->det()->geographicalId().det() == DetId::Tracker &&
00440 (*i)->det()->geographicalId().subdetId() == StripSubdetector::TEC) {
00441 nTEC++;
00442 } else {
00443 hits.push_back((*i).get());
00444 }
00445 if ( nTEC > 1 ) return all;
00446 }
00447
00448 return hits;
00449
00450 }
00451
00452
00453
00454
00455
00456 void GlobalTrajectoryBuilderBase::fixTEC(ConstRecHitContainer& all,
00457 double scl_x,
00458 double scl_y) const {
00459
00460 int nTEC(0);
00461 ConstRecHitContainer::iterator lone_tec;
00462
00463 for ( ConstRecHitContainer::iterator i = all.begin(); i != all.end(); i++) {
00464 if ( !(*i)->isValid() ) continue;
00465
00466 if ( (*i)->det()->geographicalId().det() == DetId::Tracker &&
00467 (*i)->det()->geographicalId().subdetId() == StripSubdetector::TEC) {
00468 lone_tec = i;
00469 nTEC++;
00470
00471 if ( (i+1) != all.end() && (*(i+1))->isValid() &&
00472 (*(i+1))->det()->geographicalId().det() == DetId::Tracker &&
00473 (*(i+1))->det()->geographicalId().subdetId() == StripSubdetector::TEC) {
00474 nTEC++;
00475 break;
00476 }
00477 }
00478
00479 if (nTEC > 1) break;
00480 }
00481
00482 if ( nTEC == 1 && (*lone_tec)->hit()->isValid() &&
00483 (*lone_tec)->hit()->geographicalId().det() == DetId::Tracker &&
00484 (*lone_tec)->hit()->geographicalId().subdetId() == StripSubdetector::TEC) {
00485
00486
00487 const SiStripRecHit2D* strip = dynamic_cast<const SiStripRecHit2D*>((*lone_tec)->hit());
00488 const TSiStripRecHit2DLocalPos* Tstrip = dynamic_cast<const TSiStripRecHit2DLocalPos*>((*lone_tec).get());
00489 if (strip && Tstrip->det() && Tstrip) {
00490 LocalPoint pos = Tstrip->localPosition();
00491 if ((*lone_tec)->detUnit()) {
00492 const StripTopology* topology = dynamic_cast<const StripTopology*>(&(*lone_tec)->detUnit()->topology());
00493 if (topology) {
00494
00495 float angle = topology->stripAngle(topology->strip((*lone_tec)->hit()->localPosition()));
00496 LocalError error = Tstrip->localPositionError();
00497 LocalError rotError = error.rotate(angle);
00498 LocalError scaledError(rotError.xx() * scl_x * scl_x, 0, rotError.yy() * scl_y * scl_y);
00499 error = scaledError.rotate(-angle);
00500 MuonTransientTrackingRecHit* mtt_rechit;
00501 if (strip->cluster().isNonnull()) {
00505 SiStripRecHit2D* st = new SiStripRecHit2D(pos,error,
00506 (*lone_tec)->geographicalId().rawId(),
00507 strip->cluster());
00508 *lone_tec = mtt_rechit->build((*lone_tec)->det(),st);
00509 }
00510 else {
00511 SiStripRecHit2D* st = new SiStripRecHit2D(pos,error,
00512 (*lone_tec)->geographicalId().rawId(),
00513 strip->cluster_regional());
00514 *lone_tec = mtt_rechit->build((*lone_tec)->det(),st);
00515 }
00516 }
00517 }
00518 }
00519 }
00520
00521 }
00522
00523
00524
00525
00526
00527 TransientTrackingRecHit::ConstRecHitContainer
00528 GlobalTrajectoryBuilderBase::getTransientRecHits(const reco::Track& track) const {
00529
00530 TransientTrackingRecHit::ConstRecHitContainer result;
00531
00532 TrajectoryStateTransform tsTrans;
00533
00534 TrajectoryStateOnSurface currTsos = tsTrans.innerStateOnSurface(track, *theService->trackingGeometry(), &*theService->magneticField());
00535
00536 for (trackingRecHit_iterator hit = track.recHitsBegin(); hit != track.recHitsEnd(); ++hit) {
00537 if((*hit)->isValid()) {
00538 DetId recoid = (*hit)->geographicalId();
00539 if ( recoid.det() == DetId::Tracker ) {
00540 TransientTrackingRecHit::RecHitPointer ttrhit = theTrackerRecHitBuilder->build(&**hit);
00541 TrajectoryStateOnSurface predTsos = theService->propagator(theTrackerPropagatorName)->propagate(currTsos, theService->trackingGeometry()->idToDet(recoid)->surface());
00542
00543 if ( !predTsos.isValid() ) {
00544 edm::LogError("MissingTransientHit")
00545 <<"Could not get a tsos on the hit surface. We will miss a tracking hit.";
00546 continue;
00547 }
00548 currTsos = predTsos;
00549 TransientTrackingRecHit::RecHitPointer preciseHit = ttrhit->clone(predTsos);
00550 result.push_back(preciseHit);
00551 } else if ( recoid.det() == DetId::Muon ) {
00552 if ( (*hit)->geographicalId().subdetId() == 3 && !theRPCInTheFit) {
00553 LogDebug(theCategory) << "RPC Rec Hit discarded";
00554 continue;
00555 }
00556 result.push_back(theMuonRecHitBuilder->build(&**hit));
00557 }
00558 }
00559 }
00560
00561 return result;
00562 }