CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/RecoMuon/GlobalTrackingTools/src/DynamicTruncation.cc

Go to the documentation of this file.
00001 
00017 #include "RecoMuon/GlobalTrackingTools/interface/DynamicTruncation.h"
00018 #include "DataFormats/MuonDetId/interface/RPCDetId.h"
00019 #include "TrackingTools/TrackFitters/interface/RecHitLessByDet.h"
00020 #include "RecoMuon/GlobalTrackingTools/interface/ChamberSegmentUtility.h"
00021 #include "TrackingTools/Records/interface/TransientRecHitRecord.h"
00022 #include "RecoMuon/DetLayers/interface/MuonDetLayerGeometry.h"
00023 #include "RecoMuon/Records/interface/MuonRecoGeometryRecord.h"
00024 #include "RecoMuon/Navigation/interface/MuonNavigationSchool.h"
00025 #include "TrackingTools/DetLayers/interface/NavigationSetter.h"
00026 #include "RecoMuon/Navigation/interface/MuonNavigationPrinter.h"
00027 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00028 #include "RecoTracker/Record/interface/NavigationSchoolRecord.h"
00029 #include "RecoMuon/GlobalTrackingTools/interface/MuonTkNavigationSchool.h"
00030 #include "RecoMuon/TrackingTools/interface/MuonPatternRecoDumper.h"
00031 #include "TrackingTools/KalmanUpdators/interface/Chi2MeasurementEstimator.h"
00032 #include "TrackPropagation/SteppingHelixPropagator/interface/SteppingHelixPropagator.h"
00033 #include "DataFormats/TrackingRecHit/interface/AlignmentPositionError.h"
00034 #include "DataFormats/GeometryCommonDetAlgo/interface/ErrorFrameTransformer.h"
00035 
00036 #include "CondFormats/Alignment/interface/Alignments.h"
00037 #include "CondFormats/Alignment/interface/AlignTransform.h"
00038 #include "CondFormats/AlignmentRecord/interface/DTAlignmentRcd.h"
00039 #include "CondFormats/AlignmentRecord/interface/CSCAlignmentRcd.h"
00040 #include "CondFormats/Alignment/interface/AlignmentErrors.h"
00041 #include "CondFormats/Alignment/interface/AlignTransformError.h"
00042 #include "CondFormats/AlignmentRecord/interface/DTAlignmentErrorRcd.h"
00043 #include "CondFormats/AlignmentRecord/interface/CSCAlignmentErrorRcd.h"
00044 
00045 
00046 #define MAX_THR 1e7
00047 
00048 using namespace edm;
00049 using namespace std;
00050 using namespace reco;
00051 
00052 DynamicTruncation::DynamicTruncation(const edm::Event& event, const MuonServiceProxy& theService):
00053   DTThr(0), CSCThr(0) 
00054 {
00055   theEvent = &event;
00056   theSetup = &theService.eventSetup();
00057   propagator = theService.propagator("SmartPropagator");
00058   propagatorCompatibleDet = theService.propagator("SteppingHelixPropagatorAny");
00059   theG = theService.trackingGeometry();
00060   theService.eventSetup().get<TransientRecHitRecord>().get("MuonRecHitBuilder",theMuonRecHitBuilder);
00061   theService.eventSetup().get<TrackingComponentsRecord>().get("KFUpdator",updatorHandle);
00062   theService.eventSetup().get<MuonGeometryRecord>().get(cscGeom);
00063   theService.eventSetup().get<MuonRecoGeometryRecord>().get(navMuon);
00064   theService.eventSetup().get<IdealMagneticFieldRecord>().get(magfield);
00065   navigation = new DirectMuonNavigation(theService.detLayerGeometry());
00066 }
00067 
00068 
00069  
00070 DynamicTruncation::~DynamicTruncation() {
00071   if (navigation) delete navigation;
00072 }
00073 
00074 
00075 
00076 TransientTrackingRecHit::ConstRecHitContainer DynamicTruncation::filter(const Trajectory& traj) {
00077   result.clear();
00078 
00079   // Fill the map with the APE chamber by chamber (DT)
00080   // IT ALSO WORKS IF APE IS 0
00081   edm::ESHandle<AlignmentErrors> dtAlignmentErrors;
00082   theSetup->get<DTAlignmentErrorRcd>().get( dtAlignmentErrors );
00083   for ( std::vector<AlignTransformError>::const_iterator it = dtAlignmentErrors->m_alignError.begin();
00084         it != dtAlignmentErrors->m_alignError.end(); it++ ) {
00085     CLHEP::HepSymMatrix error = (*it).matrix();
00086     GlobalError glbErr(error[0][0], error[1][0], error[1][1], error[2][0], error[2][1], error[2][2]);
00087     DTChamberId DTid((*it).rawId());    
00088     dtApeMap.insert( pair<DTChamberId, GlobalError> (DTid, glbErr) ); 
00089   }
00090   
00091   // Fill the map with the APE chamber by chamber (CSC)
00092   // IT ALSO WORKS IF APE IS 0
00093   edm::ESHandle<AlignmentErrors> cscAlignmentErrors;
00094   theSetup->get<CSCAlignmentErrorRcd>().get( cscAlignmentErrors );
00095   for ( std::vector<AlignTransformError>::const_iterator it = cscAlignmentErrors->m_alignError.begin();
00096         it != cscAlignmentErrors->m_alignError.end(); it++ ) {
00097     CLHEP::HepSymMatrix error = (*it).matrix();
00098     GlobalError glbErr(error[0][0], error[1][0], error[1][1], error[2][0], error[2][1], error[2][2]);
00099     CSCDetId CSCid((*it).rawId());
00100     cscApeMap.insert( pair<CSCDetId, GlobalError> (CSCid, glbErr) );
00101   }
00102 
00103   // Put the tracker hits in the final vector and get the last tracker valid measure
00104   std::vector<TrajectoryMeasurement> muonMeasurements = traj.measurements();
00105   TrajectoryMeasurement lastTKm = muonMeasurements.front();
00106   for (std::vector<TrajectoryMeasurement>::const_iterator imT = muonMeasurements.begin(); imT != muonMeasurements.end(); imT++ ) {
00107     if ( !(*imT).recHit()->isValid() ) continue;
00108     const TransientTrackingRecHit* hit = &(*(*imT).recHit());
00109     if (hit->geographicalId().det() == DetId::Tracker) {
00110       result.push_back((*imT).recHit());
00111       if (!(*imT).forwardPredictedState().isValid()) continue;
00112       if ((*imT).forwardPredictedState().globalPosition().mag() 
00113           > lastTKm.forwardPredictedState().globalPosition().mag()) lastTKm = *imT;
00114     }
00115   }
00116 
00117   // get the last (forward) predicted state for the tracker
00118   currentState = lastTKm.forwardPredictedState();
00119   
00120   // update the state with the last tracker measure
00121   update(currentState, lastTKm.recHit());
00122 
00123   // use the navigation to get the list of compatible dets
00124   map<int, std::vector<DetId> > detMap;
00125   compatibleDets(currentState, detMap);
00126   
00127   // selects the muon hits for the final refit
00128   filteringAlgo(detMap);
00129   
00130   return result;
00131 }
00132 
00133 
00134  
00135 void DynamicTruncation::setThr(int bThr, int eThr) {
00136   if (bThr <= MAX_THR && bThr >= 0) DTThr  = bThr; // DT thr 
00137   else DTThr = MAX_THR;
00138   if (eThr <= MAX_THR && eThr >= 0) CSCThr = eThr; // CSC thr
00139   else CSCThr = MAX_THR;
00140 }
00141 
00142 
00143 
00144 double DynamicTruncation::getBest(std::vector<CSCSegment>& segs, TrajectoryStateOnSurface& tsos, CSCSegment& bestCSCSeg) {
00145   unsigned int i = 0;
00146   double val = MAX_THR;
00147   std::vector<CSCSegment>::size_type sz = segs.size();
00148   for (i=0; i<sz; i++) {
00149     LocalError apeLoc; //default constructor is all zeroes, OK
00150     apeLoc = ErrorFrameTransformer().transform(cscApeMap.find(segs[i].cscDetId())->second, theG->idToDet(segs[i].cscDetId())->surface());
00151     StateSegmentMatcher estim(&tsos, &segs[i], &apeLoc);
00152     double tmp = estim.value();
00153     if (tmp < val) {
00154       bestCSCSeg = segs[i];
00155       val = tmp;
00156     }
00157   }
00158   return val;
00159 }
00160 
00161 
00162 
00163 double DynamicTruncation::getBest(std::vector<DTRecSegment4D>& segs, TrajectoryStateOnSurface& tsos, DTRecSegment4D& bestDTSeg) {
00164   unsigned int i = 0;
00165   double val = MAX_THR;
00166   std::vector<DTRecSegment4D>::size_type sz = segs.size();
00167   for (i=0; i<sz; i++) {
00168     LocalError apeLoc; //default constructor is all zeroes, OK
00169     apeLoc = ErrorFrameTransformer().transform(dtApeMap.find(segs[i].chamberId())->second, theG->idToDet(segs[i].chamberId())->surface());
00170     StateSegmentMatcher estim(&tsos, &segs[i], &apeLoc); 
00171     double tmp = estim.value();                                                                                                                                              
00172     if (tmp < val) {
00173       bestDTSeg = segs[i];
00174       val = tmp;
00175     }
00176   }
00177   return val;
00178 }
00179 
00180 
00181 
00182 void DynamicTruncation::compatibleDets(TrajectoryStateOnSurface& tsos, map<int, std::vector<DetId> >& detMap) {
00183   //  SteppingHelixPropagator prop(magfield.product(), anyDirection);
00184   //  MuonPatternRecoDumper dumper;
00185   MeasurementEstimator *theEstimator = new Chi2MeasurementEstimator(100, 3);
00186   std::vector<const DetLayer *> navLayers;
00187   navLayers = navigation->compatibleLayers(*(currentState.freeState()), alongMomentum);
00188   for ( unsigned int ilayer=0; ilayer<navLayers.size(); ilayer++ ) {
00189     // Skip RPC layers
00190     if (navLayers[ilayer]->subDetector() == GeomDetEnumerators::RPCEndcap 
00191         || navLayers[ilayer]->subDetector() == GeomDetEnumerators::RPCBarrel) continue;
00192     std::vector<DetLayer::DetWithState> comps = navLayers[ilayer]->compatibleDets(currentState, 
00193                                                                                   *propagatorCompatibleDet, *theEstimator);
00194     //    cout << comps.size() << " compatible Dets with " << navLayers[ilayer]->subDetector() << " Layer " << ilayer << " "
00195     //     << dumper.dumpLayer(navLayers[ilayer]) << " " << endl;
00196     if (comps.size() > 0) {
00197       for ( unsigned int icomp=0; icomp<comps.size(); icomp++ ) {
00198         DetId id(comps[icomp].first->geographicalId().rawId());
00199         detMap[ilayer].push_back(id);
00200       }
00201     }
00202   }
00203   if (theEstimator) delete theEstimator;
00204 }
00205 
00206 
00207 
00208 void DynamicTruncation::filteringAlgo(map<int, std::vector<DetId> >& detMap) {
00209   ChamberSegmentUtility getSegs(*theEvent, *theSetup);
00210   for (unsigned int iDet = 0; iDet < detMap.size(); ++iDet) {
00211     double bestLayerValue = MAX_THR;
00212     bool isDTorCSC = false;
00213     ConstRecHitContainer layerRH;
00214     std::vector<DetId> chamber = detMap[iDet];
00215     for (unsigned int j = 0; j < chamber.size(); ++j) {
00216       DetId id = chamber[j];
00217       
00218       if (id.det() == DetId::Muon && id.subdetId() == MuonSubdetId::DT) {
00219         isDTorCSC = true;
00220         
00221         DTChamberId DTid(id);
00222 
00223         std::vector<DTRecSegment4D> allDTsegs;
00224         std::map<int, std::vector<DTRecSegment4D> >::const_iterator dtIter = getSegs.getDTlist().find(DTid.station());
00225         if (dtIter != getSegs.getDTlist().end()){
00226           allDTsegs = dtIter->second;
00227         }
00228         std::vector<DTRecSegment4D>::size_type sz = allDTsegs.size();
00229         for (unsigned int iSeg=0; iSeg<sz; ++iSeg) {
00230           
00231           // Propagate the state to the current chamber
00232           TrajectoryStateOnSurface tsosdt = propagator->propagate(currentState, 
00233                                                                   theG->idToDet(allDTsegs[iSeg].chamberId())->surface());
00234           if (!tsosdt.isValid()) continue;
00235 
00236           std::vector<DTRecSegment4D> DTsegs;
00237           DTsegs.push_back(allDTsegs[iSeg]); 
00238           
00239           DTRecSegment4D bestDTSeg;
00240           double bestChamberValue = getBest(DTsegs, tsosdt, bestDTSeg);
00241           if (bestChamberValue < bestLayerValue) bestLayerValue = bestChamberValue;
00242           
00243           // Check if the best estimator value is below the THR and then get the RH componing the segment
00244           if (bestChamberValue >= DTThr || bestChamberValue > bestLayerValue) continue; 
00245           layerRH.clear();
00246           std::vector<DTRecHit1D> DTrh = getSegs.getDTRHmap(bestDTSeg);
00247           for (std::vector<DTRecHit1D>::iterator it = DTrh.begin(); it != DTrh.end(); it++) {
00248             layerRH.push_back(theMuonRecHitBuilder->build(&*it));
00249           }
00250         }
00251       }
00252       if (id.det() == DetId::Muon && id.subdetId() == MuonSubdetId::CSC) {
00253         isDTorCSC = true;
00254         CSCDetId CSCid(id);
00255         
00256         std::vector<CSCSegment> allCSCsegs;
00257         std::map<int, std::vector<CSCSegment> >::const_iterator cscIter = getSegs.getCSClist().find(CSCid.station()); 
00258         if (cscIter != getSegs.getCSClist().end()){
00259           allCSCsegs = cscIter->second;
00260         }
00261         
00262         std::vector<CSCSegment>::size_type sz = allCSCsegs.size();
00263         for (unsigned int iSeg=0; iSeg<sz; ++iSeg) {
00264           
00265           // Propagate the state to the current chamber
00266           TrajectoryStateOnSurface tsoscsc = propagator->propagate(currentState, 
00267                                                                    theG->idToDet(allCSCsegs[iSeg].cscDetId())->surface());
00268           if (!tsoscsc.isValid()) continue;
00269           
00270           std::vector<CSCSegment> CSCsegs;
00271           CSCsegs.push_back(allCSCsegs[iSeg]);
00272           
00273           CSCSegment bestCSCSeg;
00274           double bestChamberValue = getBest(CSCsegs, tsoscsc, bestCSCSeg);
00275           if (bestChamberValue < bestLayerValue) bestLayerValue = bestChamberValue;
00276           
00277           // Check if the best estimator value is below the THR and then get the RH componing the segment
00278           if (bestChamberValue >= CSCThr || bestChamberValue > bestLayerValue) continue;
00279           layerRH.clear();
00280           std::vector<CSCRecHit2D> CSCrh = getSegs.getCSCRHmap(bestCSCSeg);
00281           for (std::vector<CSCRecHit2D>::iterator it = CSCrh.begin(); it != CSCrh.end(); ++it) {
00282             layerRH.push_back(theMuonRecHitBuilder->build(&*it));
00283           }
00284         }
00285       }
00286     }
00287 
00288     if (isDTorCSC) {
00289       if (bestLayerValue < MAX_THR) estimators.push_back(bestLayerValue);
00290       else estimators.push_back(-1);
00291     }
00292 
00293     if (layerRH.size() > 0) {                                                                                                                                          
00294       for (ConstRecHitContainer::iterator it = layerRH.begin(); it != layerRH.end(); ++it) {                                                                         
00295         result.push_back((*it));                                                                                                                                         
00296       }                                                                                                                                                                
00297       
00298       // sort the vector                                                                                                                                             
00299       layerRH = sort(layerRH);                                                                                                                                         
00300       
00301       // update the currentState using all rh                                                                                                             
00302       DetId id = layerRH.front()->geographicalId();                                                                                                      
00303       if (id.subdetId() == MuonSubdetId::DT) updateWithDThits(layerRH);                                                                                 
00304       else updateWithCSChits(layerRH);                                                                                                                 
00305     }
00306     layerRH.clear();                                                                                                                                                    
00307   }
00308 }  
00309 
00310 
00311 
00312 void DynamicTruncation::update(TrajectoryStateOnSurface& tsos, ConstRecHitPointer rechit) {
00313   TrajectoryStateOnSurface temp = updatorHandle->update(tsos, *rechit);
00314   if (temp.isValid()) currentState = updatorHandle->update(tsos, *rechit);
00315 }
00316 
00317 
00318 
00319 void DynamicTruncation::updateWithDThits(ConstRecHitContainer& recHits) {
00320   for (ConstRecHitContainer::const_iterator it = recHits.begin(); it != recHits.end(); ++it) {
00321     DTLayerId layid((*it)->det()->geographicalId());
00322     TrajectoryStateOnSurface temp = propagator->propagate(currentState, theG->idToDet(layid)->surface());
00323     if (temp.isValid()) currentState = updatorHandle->update(temp, **it);
00324   }
00325 }
00326 
00327 
00328 
00329 void DynamicTruncation::updateWithCSChits(ConstRecHitContainer& recHits) {
00330   for (ConstRecHitContainer::const_iterator it = recHits.begin(); it != recHits.end(); ++it) {
00331     const CSCLayer* cscChamber = cscGeom->layer((*it)->det()->geographicalId());
00332     CSCDetId layid = cscChamber->geographicalId();
00333     TrajectoryStateOnSurface temp = propagator->propagate(currentState, theG->idToDet(layid)->surface());
00334     if (temp.isValid()) currentState = updatorHandle->update(temp, **it);
00335   }
00336 }
00337 
00338 
00339 
00340 TransientTrackingRecHit::ConstRecHitContainer DynamicTruncation::sort(ConstRecHitContainer& recHits) {
00341   unsigned int i=0;
00342   unsigned int j=0;
00343   ConstRecHitContainer::size_type n = recHits.size();
00344   for(i=1; i<n; ++i)
00345     for(j=n-1; j>=i; --j)
00346       {
00347         if(recHits[j-1]->globalPosition().mag() > recHits[j]->globalPosition().mag())
00348           {
00349             swap (recHits[j-1],recHits[j]);
00350           }
00351       }
00352   return recHits;
00353 }
00354