CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/RecoMuon/CosmicMuonProducer/src/CosmicMuonLinksProducer.cc

Go to the documentation of this file.
00001 #include "RecoMuon/CosmicMuonProducer/src/CosmicMuonLinksProducer.h"
00002 
00011 // system include files
00012 #include <memory>
00013 
00014 // user include files
00015 #include "FWCore/Framework/interface/Frameworkfwd.h"
00016 
00017 #include "FWCore/Framework/interface/Event.h"
00018 #include "FWCore/Framework/interface/MakerMacros.h"
00019 
00020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00021 
00022 #include "FWCore/Framework/interface/EventSetup.h"
00023 #include "FWCore/Framework/interface/ESHandle.h"
00024 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00025 
00026 #include "DataFormats/TrackReco/interface/Track.h"
00027 #include "DataFormats/TrackReco/interface/TrackExtra.h"
00028 #include "RecoMuon/TrackingTools/interface/MuonServiceProxy.h"
00029 
00030 using namespace edm;
00031 using namespace std;
00032 using namespace reco;
00033 
00034 //
00035 // constructors and destructor
00036 //
00037 CosmicMuonLinksProducer::CosmicMuonLinksProducer(const ParameterSet& iConfig)
00038 {
00039 
00040   category_ = "Muon|RecoMuon|CosmicMuon|CosmicMuonLinksProducer";
00041 
00042   ParameterSet serviceParameters = iConfig.getParameter<ParameterSet>("ServiceParameters");
00043 
00044   theService = new MuonServiceProxy(serviceParameters);
00045 
00046   std::vector<edm::ParameterSet> theMapPSets = iConfig.getParameter<std::vector<edm::ParameterSet> >("Maps");
00047   for (std::vector<edm::ParameterSet>::const_iterator iMPS = theMapPSets.begin();
00048        iMPS != theMapPSets.end(); iMPS++) {
00049      edm::InputTag subTrackTag = (*iMPS).getParameter<edm::InputTag>("subTrack");
00050      edm::InputTag parentTrackTag = (*iMPS).getParameter<edm::InputTag>("parentTrack");
00051      theTrackLinks.push_back( make_pair(subTrackTag, parentTrackTag) );
00052   }
00053 
00054   for(std::vector<std::pair<edm::InputTag, edm::InputTag> >::const_iterator iLink = theTrackLinks.begin();
00055      iLink != theTrackLinks.end(); iLink++) {
00056     LogDebug(category_) << "preparing map between " << (*iLink).first<<" & "<< (*iLink).second;
00057     std::string mapname = (*iLink).first.label() + "To" + (*iLink).second.label();
00058     produces<reco::TrackToTrackMap>(mapname);
00059   }
00060 
00061 }
00062 
00063 CosmicMuonLinksProducer::~CosmicMuonLinksProducer()
00064 {
00065   if (theService) delete theService;
00066 }
00067 
00068 
00069 // ------------ method called to produce the data  ------------
00070 void
00071 CosmicMuonLinksProducer::produce(Event& iEvent, const EventSetup& iSetup)
00072 {
00073   LogInfo(category_) << "Processing event number: " << iEvent.id();
00074 
00075   theService->update(iSetup);
00076 
00077   for(std::vector<std::pair<edm::InputTag, edm::InputTag> >::const_iterator iLink = theTrackLinks.begin();
00078      iLink != theTrackLinks.end(); iLink++){
00079     LogDebug(category_) << "making map between " << (*iLink).first<<" and "<< (*iLink).second;
00080     std::string mapname = (*iLink).first.label() + "To" + (*iLink).second.label();
00081     reco::TrackToTrackMap ttmap;
00082 
00083     Handle<reco::TrackCollection> subTracks;
00084     Handle<reco::TrackCollection> parentTracks;
00085 
00086     if ( iEvent.getByLabel( (*iLink).first, subTracks) && iEvent.getByLabel( (*iLink).second, parentTracks) ) {
00087 
00088          ttmap = mapTracks(subTracks, parentTracks); 
00089          LogTrace(category_) << "Mapped: "<<
00090 (*iLink).first.label()<<" "<<subTracks->size()<< " and "<<(*iLink).second.label()<<" "<<parentTracks->size()<<", results: "<< ttmap.size() <<endl;
00091 
00092     }
00093 
00094     auto_ptr<reco::TrackToTrackMap> trackToTrackmap(new reco::TrackToTrackMap(ttmap));
00095     iEvent.put(trackToTrackmap, mapname);
00096   }
00097 
00098 }
00099 
00100 reco::TrackToTrackMap CosmicMuonLinksProducer::mapTracks(const Handle<reco::TrackCollection>& subTracks, const Handle<reco::TrackCollection>& parentTracks) const {
00101   reco::TrackToTrackMap map;
00102   for ( unsigned int position1 = 0; position1 != subTracks->size(); ++position1) {
00103     TrackRef track1(subTracks, position1);
00104     for ( unsigned int position2 = 0; position2 != parentTracks->size(); ++position2) {
00105       TrackRef track2(parentTracks, position2);
00106       int shared = sharedHits(*track1, *track2); 
00107       LogTrace(category_)<<"sharedHits "<<shared<<" track1 "<<track1->found()<<" track2 "<<track2->found()<<endl;
00108  
00109       if (shared > (track1->found())/2 ) map.insert(track1, track2);
00110     }
00111   }
00112 
00113   return map; 
00114 }
00115 
00116 int CosmicMuonLinksProducer::sharedHits(const reco::Track& track1, const reco::Track& track2) const {
00117 
00118   int match = 0;
00119 
00120   for (trackingRecHit_iterator hit1 = track1.recHitsBegin(); hit1 != track1.recHitsEnd(); ++hit1) {
00121     if ( !(*hit1)->isValid() ) continue;
00122     DetId id1 = (*hit1)->geographicalId();
00123     if ( id1.det() != DetId::Muon ) continue; //ONLY MUON
00124     LogTrace(category_)<<"first ID "<<id1.rawId()<<" "<<(*hit1)->localPosition()<<endl;
00125     GlobalPoint pos1 = theService->trackingGeometry()->idToDet(id1)->surface().toGlobal((*hit1)->localPosition());
00126 
00127     for (trackingRecHit_iterator hit2 = track2.recHitsBegin(); hit2 != track2.recHitsEnd(); ++hit2) {
00128 
00129           if ( !(*hit2)->isValid() ) continue;
00130 
00131           DetId id2 = (*hit2)->geographicalId();
00132           if ( id2.det() != DetId::Muon ) continue; //ONLY MUON
00133 
00134 //          LogTrace(category_)<<"second ID "<<id2.rawId()<< (*hit2)->localPosition()<<endl;
00135 
00136           if (id2.rawId() != id1.rawId() ) continue;
00137 
00138           GlobalPoint pos2 = theService->trackingGeometry()->idToDet(id2)->surface().toGlobal((*hit2)->localPosition());
00139           if ( ( pos1 - pos2 ).mag()< 10e-5 ) match++;
00140 
00141         }
00142 
00143     }
00144 
00145    return match;
00146 
00147 }