CMS 3D CMS Logo

CosmicMuonLinksProducer.cc
Go to the documentation of this file.
2 
9 // system include files
10 #include <memory>
11 
12 // user include files
14 
17 
19 
23 
27 
28 using namespace edm;
29 using namespace std;
30 using namespace reco;
31 
32 //
33 // constructors and destructor
34 //
36 {
37 
38  category_ = "Muon|RecoMuon|CosmicMuon|CosmicMuonLinksProducer";
39 
40  ParameterSet serviceParameters = iConfig.getParameter<ParameterSet>("ServiceParameters");
41 
42  theService = new MuonServiceProxy(serviceParameters);
43 
44  std::vector<edm::ParameterSet> theMapPSets = iConfig.getParameter<std::vector<edm::ParameterSet> >("Maps");
45  for (std::vector<edm::ParameterSet>::const_iterator iMPS = theMapPSets.begin();
46  iMPS != theMapPSets.end(); iMPS++) {
47 
48  edm::InputTag sTag = (*iMPS).getParameter<edm::InputTag>("subTrack");
49  edm::InputTag pTag = (*iMPS).getParameter<edm::InputTag>("parentTrack");
50 
51  edm::EDGetTokenT<reco::TrackCollection> subTrackTag = consumes<reco::TrackCollection>(sTag );
52  edm::EDGetTokenT<reco::TrackCollection> parentTrackTag = consumes<reco::TrackCollection>(pTag);
53  theTrackLinks.push_back( make_pair(subTrackTag, parentTrackTag) );
54  theTrackLinkNames.push_back( make_pair(sTag.label(), pTag.label()) );
55 
56 
57  LogDebug(category_) << "preparing map between " << sTag<<" & "<< pTag;
58  std::string mapname = sTag.label() + "To" + pTag.label();
59  produces<reco::TrackToTrackMap>(mapname);
60 
61 
62  }
63 
64 }
65 
67 {
68  if (theService) delete theService;
69 }
70 
71 
72 // ------------ method called to produce the data ------------
73 void
75 {
76  LogInfo(category_) << "Processing event number: " << iEvent.id();
77 
78  theService->update(iSetup);
79 
80  unsigned int counter= 0;
81  for(std::vector<std::pair<edm::EDGetTokenT<reco::TrackCollection>,edm::EDGetTokenT<reco::TrackCollection> > >::const_iterator iLink = theTrackLinks.begin();
82  iLink != theTrackLinks.end(); iLink++){
83 #ifdef EDM_ML_DEBUG
84  edm::EDConsumerBase::Labels labels_first;
85  edm::EDConsumerBase::Labels labels_second;
86  labelsForToken(iLink->first, labels_first);
87  labelsForToken(iLink->second, labels_second);
88  LogDebug(category_) << "making map between " << labels_first.module <<" and "<< labels_second.module;
89 #endif
90  std::string mapname = theTrackLinkNames[counter].first + "To" + theTrackLinkNames[counter].second;
92 
94  Handle<reco::TrackCollection> parentTracks;
95 
96  iEvent.getByToken((*iLink).first, subTracks);
97  iEvent.getByToken((*iLink).second, parentTracks);
98 
99  ttmap = mapTracks(subTracks, parentTracks);
100  LogTrace(category_) << "Mapped: "<<
101  theTrackLinkNames[counter].first <<" "<<subTracks->size()<< " and "<<theTrackLinkNames[counter].second<<" "<<parentTracks->size()<<", results: "<< ttmap.size() <<endl;
102 
103  iEvent.put(std::make_unique<reco::TrackToTrackMap>(ttmap), mapname);
104 
105  counter++;
106  }
107 
108 }
109 
111  reco::TrackToTrackMap map(subTracks, parentTracks) ;
112  for ( unsigned int position1 = 0; position1 != subTracks->size(); ++position1) {
113  TrackRef track1(subTracks, position1);
114  for ( unsigned int position2 = 0; position2 != parentTracks->size(); ++position2) {
115  TrackRef track2(parentTracks, position2);
116  int shared = sharedHits(*track1, *track2);
117  LogTrace(category_)<<"sharedHits "<<shared<<" track1 "<<track1->found()<<" track2 "<<track2->found()<<endl;
118 
119  if (shared > (track1->found())/2 ) map.insert(track1, track2);
120  }
121  }
122 
123  return map;
124 }
125 
126 int CosmicMuonLinksProducer::sharedHits(const reco::Track& track1, const reco::Track& track2) const {
127 
128  int match = 0;
129 
130  for (trackingRecHit_iterator hit1 = track1.recHitsBegin(); hit1 != track1.recHitsEnd(); ++hit1) {
131  if ( !(*hit1)->isValid() ) continue;
132  DetId id1 = (*hit1)->geographicalId();
133  if ( id1.det() != DetId::Muon ) continue; //ONLY MUON
134  LogTrace(category_)<<"first ID "<<id1.rawId()<<" "<<(*hit1)->localPosition()<<endl;
135  GlobalPoint pos1 = theService->trackingGeometry()->idToDet(id1)->surface().toGlobal((*hit1)->localPosition());
136 
137  for (trackingRecHit_iterator hit2 = track2.recHitsBegin(); hit2 != track2.recHitsEnd(); ++hit2) {
138 
139  if ( !(*hit2)->isValid() ) continue;
140 
141  DetId id2 = (*hit2)->geographicalId();
142  if ( id2.det() != DetId::Muon ) continue; //ONLY MUON
143 
144 // LogTrace(category_)<<"second ID "<<id2.rawId()<< (*hit2)->localPosition()<<endl;
145 
146  if (id2.rawId() != id1.rawId() ) continue;
147 
148  GlobalPoint pos2 = theService->trackingGeometry()->idToDet(id2)->surface().toGlobal((*hit2)->localPosition());
149  if ( ( pos1 - pos2 ).mag()< 10e-5 ) match++;
150 
151  }
152 
153  }
154 
155  return match;
156 
157 }
#define LogDebug(id)
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:47
void produce(edm::Event &, const edm::EventSetup &) override
int sharedHits(const reco::Track &track1, const reco::Track &track2) const
int iEvent
Definition: GenABIO.cc:230
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:104
char const * module
Definition: ProductLabels.h:5
#define LogTrace(id)
Definition: DetId.h:18
size_type size() const
map size
void insert(const key_type &k, const data_type &v)
insert an association
std::string const & label() const
Definition: InputTag.h:36
edm::EventID id() const
Definition: EventBase.h:60
fixed size matrix
HLT enums.
static std::atomic< unsigned int > counter
reco::TrackToTrackMap mapTracks(const edm::Handle< reco::TrackCollection > &, const edm::Handle< reco::TrackCollection > &) const
CosmicMuonLinksProducer(const edm::ParameterSet &)
std::string match(BranchDescription const &a, BranchDescription const &b, std::string const &fileName)
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:39
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:109