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