CMS 3D CMS Logo

MuonTrackProducer.cc
Go to the documentation of this file.
1 //
2 // modified & integrated by Giovanni Abbiendi
3 // from code by Arun Luthra: UserCode/luthra/MuonTrackSelector/src/MuonTrackSelector.cc
4 //
13 #include <sstream>
14 
18 
20  muonsToken(consumes<reco::MuonCollection>(parset.getParameter< edm::InputTag >("muonsTag"))),
21  inputDTRecSegment4DToken_(consumes<DTRecSegment4DCollection>(parset.getParameter<edm::InputTag>("inputDTRecSegment4DCollection"))),
22  inputCSCSegmentToken_(consumes<CSCSegmentCollection>(parset.getParameter<edm::InputTag>("inputCSCSegmentCollection"))),
23  selectionTags(parset.getParameter< std::vector<std::string> >("selectionTags")),
24  trackType(parset.getParameter< std::string >("trackType")),
25  parset_(parset)
26 {
27  edm::LogVerbatim("MuonTrackProducer") << "constructing MuonTrackProducer" << parset_.dump();
28  produces<reco::TrackCollection>();
29  produces<reco::TrackExtraCollection>();
30  produces<TrackingRecHitCollection>();
31 }
32 
34 }
35 
37 {
41 
43  iSetup.get<TrackerTopologyRcd>().get(httopo);
44  const TrackerTopology& ttopo = *httopo;
45 
46  std::unique_ptr<reco::TrackCollection> selectedTracks(new reco::TrackCollection);
47  std::unique_ptr<reco::TrackExtraCollection> selectedTrackExtras( new reco::TrackExtraCollection() );
48  std::unique_ptr<TrackingRecHitCollection> selectedTrackHits( new TrackingRecHitCollection() );
49 
53 
56 
57  edm::LogVerbatim("MuonTrackProducer") <<"\nThere are "<< dtSegmentCollectionH_->size()<<" DT segments.";
58  unsigned int index_dt_segment = 0;
60  segment != dtSegmentCollectionH_->end(); ++segment , index_dt_segment++) {
61  LocalPoint segmentLocalPosition = segment->localPosition();
62  LocalVector segmentLocalDirection = segment->localDirection();
63  LocalError segmentLocalPositionError = segment->localPositionError();
64  LocalError segmentLocalDirectionError = segment->localDirectionError();
65  DetId geoid = segment->geographicalId();
66  DTChamberId dtdetid = DTChamberId(geoid);
67  int wheel = dtdetid.wheel();
68  int station = dtdetid.station();
69  int sector = dtdetid.sector();
70 
71  float segmentX = segmentLocalPosition.x();
72  float segmentY = segmentLocalPosition.y();
73  float segmentdXdZ = segmentLocalDirection.x()/segmentLocalDirection.z();
74  float segmentdYdZ = segmentLocalDirection.y()/segmentLocalDirection.z();
75  float segmentXerr = sqrt(segmentLocalPositionError.xx());
76  float segmentYerr = sqrt(segmentLocalPositionError.yy());
77  float segmentdXdZerr = sqrt(segmentLocalDirectionError.xx());
78  float segmentdYdZerr = sqrt(segmentLocalDirectionError.yy());
79 
80  edm::LogVerbatim("MuonTrackProducer")
81  <<"\nDT segment index :"<<index_dt_segment
82  <<"\nchamber Wh:"<<wheel<<",St:"<<station<<",Se:"<<sector
83  <<"\nLocal Position (X,Y)=("<<segmentX<<","<<segmentY<<") +/- ("<<segmentXerr<<","<<segmentYerr<<"), "
84  <<"Local Direction (dXdZ,dYdZ)=("<<segmentdXdZ<<","<<segmentdYdZ<<") +/- ("<<segmentdXdZerr<<","<<segmentdYdZerr<<")";
85  }
86 
87  edm::LogVerbatim("MuonTrackProducer") <<"\nThere are "<< cscSegmentCollectionH_->size()<<" CSC segments.";
88  unsigned int index_csc_segment = 0;
90  segment != cscSegmentCollectionH_->end(); ++segment , index_csc_segment++) {
91  LocalPoint segmentLocalPosition = segment->localPosition();
92  LocalVector segmentLocalDirection = segment->localDirection();
93  LocalError segmentLocalPositionError = segment->localPositionError();
94  LocalError segmentLocalDirectionError = segment->localDirectionError();
95 
96  DetId geoid = segment->geographicalId();
97  CSCDetId cscdetid = CSCDetId(geoid);
98  int endcap = cscdetid.endcap();
99  int station = cscdetid.station();
100  int ring = cscdetid.ring();
101  int chamber = cscdetid.chamber();
102 
103  float segmentX = segmentLocalPosition.x();
104  float segmentY = segmentLocalPosition.y();
105  float segmentdXdZ = segmentLocalDirection.x()/segmentLocalDirection.z();
106  float segmentdYdZ = segmentLocalDirection.y()/segmentLocalDirection.z();
107  float segmentXerr = sqrt(segmentLocalPositionError.xx());
108  float segmentYerr = sqrt(segmentLocalPositionError.yy());
109  float segmentdXdZerr = sqrt(segmentLocalDirectionError.xx());
110  float segmentdYdZerr = sqrt(segmentLocalDirectionError.yy());
111 
112  edm::LogVerbatim("MuonTrackProducer")
113  <<"\nCSC segment index :"<<index_csc_segment
114  <<"\nchamber Endcap:"<<endcap<<",St:"<<station<<",Ri:"<<ring<<",Ch:"<<chamber
115  <<"\nLocal Position (X,Y)=("<<segmentX<<","<<segmentY<<") +/- ("<<segmentXerr<<","<<segmentYerr<<"), "
116  <<"Local Direction (dXdZ,dYdZ)=("<<segmentdXdZ<<","<<segmentdYdZ<<") +/- ("<<segmentdXdZerr<<","<<segmentdYdZerr<<")";
117  }
118 
119  edm::LogVerbatim("MuonTrackProducer") <<"\nThere are "<< muonCollectionH->size() <<" reco::Muons.";
120  unsigned int muon_index = 0;
121  for(reco::MuonCollection::const_iterator muon = muonCollectionH->begin();
122  muon != muonCollectionH->end(); ++muon, muon_index++) {
123  edm::LogVerbatim("MuonTrackProducer") <<"\n******* muon index : "<<muon_index;
124 
125  std::vector<bool> isGood;
126  for(unsigned int index=0; index<selectionTags.size(); ++index) {
127  isGood.push_back(false);
128 
130  isGood[index] = muon::isGoodMuon(*muon, muonType);
131  }
132 
133  bool isGoodResult=true;
134  for(unsigned int index=0; index<isGood.size(); ++index) {
135  edm::LogVerbatim("MuonTrackProducer") << "selectionTag = "<<selectionTags[index]<< ": "<<isGood[index]<<"\n";
136  isGoodResult *= isGood[index];
137  }
138 
139  if (isGoodResult) {
140  // new copy of Track
141  reco::TrackRef trackref;
142  if (trackType == "innerTrack") {
143  if (muon->innerTrack().isNonnull()) trackref = muon->innerTrack();
144  else continue;
145  }
146  else if (trackType == "outerTrack") {
147  if (muon->outerTrack().isNonnull()) trackref = muon->outerTrack();
148  else continue;
149  }
150  else if (trackType == "globalTrack") {
151  if (muon->globalTrack().isNonnull()) trackref = muon->globalTrack();
152  else continue;
153  }
154  else if (trackType == "innerTrackPlusSegments") {
155  if (muon->innerTrack().isNonnull()) trackref = muon->innerTrack();
156  else continue;
157  }
158  else if (trackType == "gemMuonTrack") {
159  if (muon->innerTrack().isNonnull() && muon->isGEMMuon()){
160  trackref = muon->innerTrack();
161  }
162  else continue;
163  }
164  else if (trackType == "me0MuonTrack") {
165  if (muon->innerTrack().isNonnull() && muon->isME0Muon()){
166  trackref = muon->innerTrack();
167  }
168  else continue;
169  }
170 
171  const reco::Track* trk = &(*trackref);
172  // pointer to old track:
173  reco::Track* newTrk = new reco::Track(*trk);
174 
175  newTrk->setExtra( reco::TrackExtraRef( rTrackExtras, idx++ ) );
176  PropagationDirection seedDir = trk->seedDirection();
177  // new copy of track Extras
178  reco::TrackExtra * newExtra = new reco::TrackExtra( trk->outerPosition(), trk->outerMomentum(),
179  trk->outerOk(), trk->innerPosition(),
180  trk->innerMomentum(), trk->innerOk(),
181  trk->outerStateCovariance(), trk->outerDetId(),
182  trk->innerStateCovariance(), trk->innerDetId() , seedDir ) ;
183 
184  // new copy of the silicon hits; add hit refs to Extra and hits to hit collection
185 
186  // edm::LogVerbatim("MuonTrackProducer")<<"\n printing initial hit_pattern";
187  // trk->hitPattern().print();
188  unsigned int nHitsToAdd = 0;
189  for (trackingRecHit_iterator iHit = trk->recHitsBegin(); iHit != trk->recHitsEnd(); iHit++) {
190  TrackingRecHit* hit = (*iHit)->clone();
191  selectedTrackHits->push_back( hit );
192  ++nHitsToAdd;
193  }
194  newExtra->setHits( rHits, hidx, nHitsToAdd );
195  hidx += nHitsToAdd;
196  if (trackType == "innerTrackPlusSegments") {
197 
198  int wheel, station, sector;
199  int endcap, /*station, */ ring, chamber;
200 
201  edm::LogVerbatim("MuonTrackProducer") <<"Number of chambers: "<<muon->matches().size()
202  <<", arbitrated: "<<muon->numberOfMatches(reco::Muon::SegmentAndTrackArbitration);
203  unsigned int index_chamber = 0;
204 
205  for(std::vector<reco::MuonChamberMatch>::const_iterator chamberMatch = muon->matches().begin();
206  chamberMatch != muon->matches().end(); ++chamberMatch, index_chamber++) {
207  std::stringstream chamberStr;
208  chamberStr <<"\nchamber index: "<<index_chamber;
209 
210  int subdet = chamberMatch->detector();
211  DetId did = chamberMatch->id;
212 
213  if (subdet == MuonSubdetId::DT) {
214  DTChamberId dtdetid = DTChamberId(did);
215  wheel = dtdetid.wheel();
216  station = dtdetid.station();
217  sector = dtdetid.sector();
218  chamberStr << ", DT chamber Wh:"<<wheel<<",St:"<<station<<",Se:"<<sector;
219  }
220  else if (subdet == MuonSubdetId::CSC) {
221  CSCDetId cscdetid = CSCDetId(did);
222  endcap = cscdetid.endcap();
223  station = cscdetid.station();
224  ring = cscdetid.ring();
225  chamber = cscdetid.chamber();
226  chamberStr << ", CSC chamber End:"<<endcap<<",St:"<<station<<",Ri:"<<ring<<",Ch:"<<chamber;
227  }
228 
229  chamberStr << ", Number of segments: "<<chamberMatch->segmentMatches.size();
230  edm::LogVerbatim("MuonTrackProducer") << chamberStr.str();
231 
232  unsigned int index_segment = 0;
233 
234  for(std::vector<reco::MuonSegmentMatch>::const_iterator segmentMatch = chamberMatch->segmentMatches.begin();
235  segmentMatch != chamberMatch->segmentMatches.end(); ++segmentMatch, index_segment++) {
236 
237  float segmentX = segmentMatch->x;
238  float segmentY = segmentMatch->y ;
239  float segmentdXdZ = segmentMatch->dXdZ;
240  float segmentdYdZ = segmentMatch->dYdZ;
241  float segmentXerr = segmentMatch->xErr;
242  float segmentYerr = segmentMatch->yErr;
243  float segmentdXdZerr = segmentMatch->dXdZErr;
244  float segmentdYdZerr = segmentMatch->dYdZErr;
245 
246  CSCSegmentRef segmentCSC = segmentMatch->cscSegmentRef;
247  DTRecSegment4DRef segmentDT = segmentMatch->dtSegmentRef;
248 
249  bool segment_arbitrated_Ok = (segmentMatch->isMask(reco::MuonSegmentMatch::BestInChamberByDR) &&
250  segmentMatch->isMask(reco::MuonSegmentMatch::BelongsToTrackByDR));
251 
252  std::string ARBITRATED(" ***Arbitrated Off*** ");
253  if (segment_arbitrated_Ok) ARBITRATED = " ***ARBITRATED OK*** ";
254 
255  if (subdet == MuonSubdetId::DT) {
256  edm::LogVerbatim("MuonTrackProducer")
257  <<"\n\t segment index: "<<index_segment << ARBITRATED
258  <<"\n\t Local Position (X,Y)=("<<segmentX<<","<<segmentY<<") +/- ("<<segmentXerr<<","<<segmentYerr<<"), "
259  <<"\n\t Local Direction (dXdZ,dYdZ)=("<<segmentdXdZ<<","<<segmentdYdZ<<") +/- ("<<segmentdXdZerr<<","<<segmentdYdZerr<<")";
260 
261  if (!segment_arbitrated_Ok) continue;
262 
263  if (segmentDT.get() != 0) {
264  const DTRecSegment4D* segment = segmentDT.get();
265 
266  edm::LogVerbatim("MuonTrackProducer")<<"\t ===> MATCHING with DT segment with index = "<<segmentDT.key();
267 
268  if(segment->hasPhi()) {
269  const DTChamberRecSegment2D* phiSeg = segment->phiSegment();
270  std::vector<const TrackingRecHit*> phiHits = phiSeg->recHits();
271  unsigned int nHitsAdded = 0;
272  for(std::vector<const TrackingRecHit*>::const_iterator ihit = phiHits.begin();
273  ihit != phiHits.end(); ++ihit) {
274  TrackingRecHit* seghit = (*ihit)->clone();
275  newTrk->appendHitPattern(*seghit, ttopo);
276  // edm::LogVerbatim("MuonTrackProducer")<<"hit pattern for position "<<index_hit<<" set to:";
277  // newTrk->hitPattern().printHitPattern(index_hit, std::cout);
278  selectedTrackHits->push_back( seghit );
279  ++nHitsAdded;
280  }
281  newExtra->setHits( rHits, hidx, nHitsAdded );
282  hidx += nHitsAdded;
283  }
284 
285  if(segment->hasZed()) {
286  const DTSLRecSegment2D* zSeg = (*segment).zSegment();
287  std::vector<const TrackingRecHit*> zedHits = zSeg->recHits();
288  unsigned int nHitsAdded = 0;
289  for(std::vector<const TrackingRecHit*>::const_iterator ihit = zedHits.begin();
290  ihit != zedHits.end(); ++ihit) {
291  TrackingRecHit* seghit = (*ihit)->clone();
292  newTrk->appendHitPattern(*seghit, ttopo);
293  // edm::LogVerbatim("MuonTrackProducer")<<"hit pattern for position "<<index_hit<<" set to:";
294  // newTrk->hitPattern().printHitPattern(index_hit, std::cout);
295  selectedTrackHits->push_back( seghit );
296  ++nHitsAdded;
297  }
298  newExtra->setHits( rHits, hidx, nHitsAdded );
299  hidx += nHitsAdded;
300  }
301  } else edm::LogWarning("MuonTrackProducer")<<"\n***WARNING: UNMATCHED DT segment ! \n";
302  } // if (subdet == MuonSubdetId::DT)
303 
304  else if (subdet == MuonSubdetId::CSC) {
305  edm::LogVerbatim("MuonTrackProducer")
306  <<"\n\t segment index: "<<index_segment << ARBITRATED
307  <<"\n\t Local Position (X,Y)=("<<segmentX<<","<<segmentY<<") +/- ("<<segmentXerr<<","<<segmentYerr<<"), "
308  <<"\n\t Local Direction (dXdZ,dYdZ)=("<<segmentdXdZ<<","<<segmentdYdZ<<") +/- ("<<segmentdXdZerr<<","<<segmentdYdZerr<<")";
309 
310  if (!segment_arbitrated_Ok) continue;
311 
312  if (segmentCSC.get() != 0) {
313  const CSCSegment* segment = segmentCSC.get();
314 
315  edm::LogVerbatim("MuonTrackProducer")<<"\t ===> MATCHING with CSC segment with index = "<<segmentCSC.key();
316 
317  std::vector<const TrackingRecHit*> hits = segment->recHits();
318  unsigned int nHitsAdded = 0;
319  for(std::vector<const TrackingRecHit*>::const_iterator ihit = hits.begin();
320  ihit != hits.end(); ++ihit) {
321  TrackingRecHit* seghit = (*ihit)->clone();
322  newTrk->appendHitPattern(*seghit, ttopo);
323  // edm::LogVerbatim("MuonTrackProducer")<<"hit pattern for position "<<index_hit<<" set to:";
324  // newTrk->hitPattern().printHitPattern(index_hit, std::cout);
325  selectedTrackHits->push_back( seghit );
326  ++nHitsAdded;
327  }
328  newExtra->setHits( rHits, hidx, nHitsAdded );
329  hidx += nHitsAdded;
330  } else edm::LogWarning("MuonTrackProducer")<<"\n***WARNING: UNMATCHED CSC segment ! \n";
331  } // else if (subdet == MuonSubdetId::CSC)
332 
333  } // loop on vector<MuonSegmentMatch>
334  } // loop on vector<MuonChamberMatch>
335  } // if (trackType == "innerTrackPlusSegments")
336 
337  // edm::LogVerbatim("MuonTrackProducer")<<"\n printing final hit_pattern";
338  // newTrk->hitPattern().print();
339 
340  selectedTracks->push_back( *newTrk );
341  selectedTrackExtras->push_back( *newExtra );
342 
343  } // if (isGoodResult)
344  } // loop on reco::MuonCollection
345 
346  iEvent.put(std::move(selectedTracks));
347  iEvent.put(std::move(selectedTrackExtras));
348  iEvent.put(std::move(selectedTrackHits));
349 }
int chamber() const
Definition: CSCDetId.h:68
float xx() const
Definition: LocalError.h:24
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
edm::EDGetTokenT< DTRecSegment4DCollection > inputDTRecSegment4DToken_
std::vector< std::string > selectionTags
std::string dump(unsigned int indent=0) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
void setHits(TrackingRecHitRefProd const &prod, unsigned firstH, unsigned int nH)
const DTChamberRecSegment2D * phiSegment() const
The superPhi segment: 0 if no phi projection available.
edm::EDGetTokenT< reco::MuonCollection > muonsToken
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
T y() const
Definition: PV3DBase.h:63
bool innerOk() const
return true if the innermost hit is valid
Definition: Track.h:50
PropagationDirection
key_type key() const
Accessor for product key.
Definition: Ref.h:264
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:65
virtual ~MuonTrackProducer()
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
SelectionType
Selector type.
Definition: MuonSelectors.h:17
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:55
int endcap() const
Definition: CSCDetId.h:93
int iEvent
Definition: GenABIO.cc:230
static const int CSC
Definition: MuonSubdetId.h:13
edm::Handle< CSCSegmentCollection > cscSegmentCollectionH_
float yy() const
Definition: LocalError.h:26
virtual std::vector< const TrackingRecHit * > recHits() const
Access to component RecHits (if any)
T sqrt(T t)
Definition: SSEVec.h:18
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
Definition: Track.h:75
T z() const
Definition: PV3DBase.h:64
virtual TrackingRecHit * clone() const =0
edm::Handle< DTRecSegment4DCollection > dtSegmentCollectionH_
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:94
static const unsigned int BestInChamberByDR
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:244
bool hasPhi() const
Does it have the Phi projection?
virtual void produce(edm::Event &, const edm::EventSetup &)
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:104
virtual std::vector< const TrackingRecHit * > recHits() const
Access to component RecHits (if any)
Definition: CSCSegment.cc:30
RefProd< PROD > getRefBeforePut()
Definition: Event.h:134
bool isGoodMuon(const reco::Muon &muon, SelectionType type, reco::Muon::ArbitrationType arbitrationType=reco::Muon::SegmentAndTrackArbitration)
main GoodMuon wrapper call
MuonTrackProducer(const edm::ParameterSet &)
int ring() const
Definition: CSCDetId.h:75
bool hasZed() const
Does it have the Z projection?
Definition: DetId.h:18
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:11
edm::OwnVector< TrackingRecHit > TrackingRecHitCollection
collection of TrackingRecHits
void setExtra(const TrackExtraRef &ref)
set reference to "extra" object
Definition: Track.h:184
const math::XYZVector & outerMomentum() const
momentum vector at the outermost hit position
Definition: Track.h:70
bool outerOk() const
return true if the outermost hit is valid
Definition: Track.h:45
const PropagationDirection & seedDirection() const
direction of how the hits were sorted in the original seed
Definition: Track.h:204
SelectionType selectionTypeFromString(const std::string &label)
Definition: MuonSelectors.cc:9
edm::Handle< reco::MuonCollection > muonCollectionH
const T & get() const
Definition: EventSetup.h:56
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
Definition: Track.h:80
bool appendHitPattern(const TrackingRecHit &hit, const TrackerTopology &ttopo)
append a single hit to the HitPattern
Definition: TrackBase.h:455
static const unsigned int BelongsToTrackByDR
const edm::ParameterSet parset_
fixed size matrix
HLT enums.
int sector() const
Definition: DTChamberId.h:61
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:60
int station() const
Definition: CSCDetId.h:86
static const int DT
Definition: MuonSubdetId.h:12
int station() const
Return the station number.
Definition: DTChamberId.h:51
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:45
T x() const
Definition: PV3DBase.h:62
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:99
edm::EDGetTokenT< CSCSegmentCollection > inputCSCSegmentToken_
def move(src, dest)
Definition: eostools.py:510
boost::remove_cv< typename boost::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:168
TrackingRecHitCollection::base::const_iterator trackingRecHit_iterator
iterator over a vector of reference to TrackingRecHit in the same collection
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:109