CMS 3D CMS Logo

TrackQuality.cc
Go to the documentation of this file.
1 /*
2  * TrackQuality.cc
3  *
4  * Created by Christophe Saout on 9/25/08.
5  * 2007 __MyCompanyName__.
6  *
7  */
8 
9 #include <algorithm>
10 #include <vector>
11 
13 
16 
26 
31 
33 
35 
36 // #define DEBUG_TRACK_QUALITY
37 
38 namespace {
39  const uint32_t NonMatchedTrackId = (uint32_t)-1;
40 
41  struct MatchedHit {
42  DetId detId;
43  uint32_t simTrackId;
44  EncodedEventId collision;
45  int recHitId;
47 
48  bool operator<(const MatchedHit &other) const {
49  if (detId < other.detId)
50  return true;
51  else if (detId > other.detId)
52  return false;
53  else if (collision < other.collision)
54  return true;
55  else if (other.collision < collision)
56  return false;
57  else
58  return simTrackId < other.simTrackId;
59  }
60 
61  bool operator==(const MatchedHit &other) const {
62  return detId == other.detId && collision == other.collision && simTrackId == other.simTrackId;
63  }
64  };
65  /*
66 static bool operator < (const MatchedHit &hit, DetId detId)
67 {
68  return hit.detId < detId;
69 }
70 static bool operator < (DetId detId, const MatchedHit &hit)
71 {
72  return detId < hit.detId;
73 }
74 */
75 } // namespace
76 
77 typedef std::pair<TrackQuality::Layer::SubDet, short int> DetLayer;
78 
79 // in case multiple hits were found, figure out the highest priority
80 static const int statePriorities[] = {
81  /* Unknown */ 3,
82  /* Good */ 5,
83  /* Missed */ 0,
84  /* Noise */ 7,
85  /* Bad */ 2,
86  /* Dead */ 4,
87  /* Shared */ 6,
88  /* Misassoc */ 1};
89 
90 DetLayer getDetLayer(DetId detId, const TrackerTopology *tTopo) {
92  short int layer = 0;
93 
94  switch (detId.det()) {
95  case DetId::Tracker:
96  layer = tTopo->layer(detId);
97  break;
98 
99  case DetId::Muon:
100  switch (detId.subdetId()) {
101  case MuonSubdetId::DT:
103  layer = DTLayerId(detId).layer();
104  break;
105 
106  case MuonSubdetId::CSC:
108  layer = CSCDetId(detId).layer();
109  break;
110 
111  case MuonSubdetId::RPC:
112  if (RPCDetId(detId).region())
114  else
116  layer = RPCDetId(detId).layer();
117  break;
118 
119  default:
120  /* should not get here */
121  ;
122  }
123  break;
124 
125  default:
126  /* should not get here */
127  ;
128  }
129 
130  return DetLayer(det, layer);
131 }
132 
134  : trackerHitAssociatorConfig_(config.getParameter<edm::ParameterSet>("hitAssociator"), std::move(iC)) {}
135 
138 }
139 
141  std::vector<MatchedHit> matchedHits;
142 
143  // iterate over reconstructed hits
144  for (trackingRecHit_iterator hit = tr->recHitsBegin(); hit != tr->recHitsEnd(); ++hit) {
145  // on which module the hit lies
146  DetId detId = (*hit)->geographicalId();
147 
148  // FIXME: check for double-sided modules?
149 
150  // didn't find a hit on that module
151  if (!(*hit)->isValid()) {
152  MatchedHit matchedHit;
153  matchedHit.detId = detId;
154  matchedHit.simTrackId = NonMatchedTrackId;
155  // check why hit wasn't valid and propagate information
156  switch ((*hit)->getType()) {
158  matchedHit.state = Layer::Dead;
159  break;
160 
161  case TrackingRecHit::bad:
162  matchedHit.state = Layer::Bad;
163  break;
164 
165  default:
166  matchedHit.state = Layer::Missed;
167  }
168  matchedHit.recHitId = hit - tr->recHitsBegin();
169  matchedHits.push_back(matchedHit);
170  continue;
171  }
172 
173  // find simulated tracks causing hit that was reconstructed
174  std::vector<SimHitIdpr> simIds = associator_->associateHitId(**hit);
175 
176  // must be noise or so
177  if (simIds.empty()) {
178  MatchedHit matchedHit;
179  matchedHit.detId = detId;
180  matchedHit.simTrackId = NonMatchedTrackId;
181  matchedHit.state = Layer::Noise;
182  matchedHit.recHitId = hit - tr->recHitsBegin();
183  matchedHits.push_back(matchedHit);
184  continue;
185  }
186 
187  // register all simulated tracks contributing
188  for (std::vector<SimHitIdpr>::const_iterator i = simIds.begin(); i != simIds.end(); ++i) {
189  MatchedHit matchedHit;
190  matchedHit.detId = detId;
191  matchedHit.simTrackId = i->first;
192  matchedHit.collision = i->second;
193  // RecHit <-> SimHit matcher currently doesn't support muon system
194  if (detId.det() == DetId::Muon)
195  matchedHit.state = Layer::Unknown;
196  else
197  // assume hit was mismatched (until possible confirmation)
198  matchedHit.state = Layer::Misassoc;
199  matchedHit.recHitId = hit - tr->recHitsBegin();
200  matchedHits.push_back(matchedHit);
201  }
202  }
203 
204  // sort hits found so far by module id
205  std::stable_sort(matchedHits.begin(), matchedHits.end());
206 
207  // in case we added missed modules, re-sort
208  std::stable_sort(matchedHits.begin(), matchedHits.end());
209 
210  // prepare for ordering results by layer enum and layer/disk number
211  typedef std::multimap<DetLayer, const MatchedHit *> LayerHitMap;
212  LayerHitMap layerHitMap;
213 
214  // iterate over all simulated/reconstructed hits again
215  for (std::vector<MatchedHit>::const_iterator hit = matchedHits.begin(); hit != matchedHits.end();) {
216  // we can have multiple reco-to-sim matches per module, find best one
217  const MatchedHit *best = nullptr;
218 
219  // this loop iterates over all subsequent hits in the same module
220  do {
221  // update our best hit pointer
222  if (!best || statePriorities[hit->state] > statePriorities[best->state] ||
223  best->simTrackId == NonMatchedTrackId) {
224  best = &*hit;
225  }
226  ++hit;
227  } while (hit != matchedHits.end() && hit->detId == best->detId);
228 
229  // ignore hit in case track reco was looking at the wrong module
230  if (best->simTrackId != NonMatchedTrackId || best->state != Layer::Missed) {
231  layerHitMap.insert(std::make_pair(getDetLayer(best->detId, tTopo), best));
232  }
233  }
234 
235  layers_.clear();
236 
237 #ifdef DEBUG_TRACK_QUALITY
238  std::cout << "---------------------" << std::endl;
239 #endif
240  // now prepare final collection
241  for (LayerHitMap::const_iterator hit = layerHitMap.begin(); hit != layerHitMap.end(); ++hit) {
242 #ifdef DEBUG_TRACK_QUALITY
243  std::cout << "detLayer (" << hit->first.first << ", " << hit->first.second << ")"
244  << " [" << (uint32_t)hit->second->detId << "] sim(" << (int)hit->second->simTrackId << ")"
245  << " hit(" << hit->second->recHitId << ") -> " << hit->second->state << std::endl;
246 #endif
247 
248  // find out if we need to start a new layer
249  Layer *layer = layers_.empty() ? nullptr : &layers_.back();
250  if (!layer || hit->first.first != layer->subDet || hit->first.second != layer->layer) {
251  Layer newLayer;
252  newLayer.subDet = hit->first.first;
253  newLayer.layer = hit->first.second;
254  layers_.push_back(newLayer);
255  layer = &layers_.back();
256  }
257 
258  // create hit and add it to layer
259  Layer::Hit newHit;
260  newHit.recHitId = hit->second->recHitId;
261  newHit.state = hit->second->state;
262 
263  layer->hits.push_back(newHit);
264  }
265 }
std::vector< TrackingParticleRef > SimParticleTrail
Definition: TrackQuality.h:30
std::unique_ptr< TrackerHitAssociator > associator_
Definition: TrackQuality.h:81
std::pair< TrackQuality::Layer::SubDet, short int > DetLayer
Definition: TrackQuality.cc:77
TrackerHitAssociator::Config trackerHitAssociatorConfig_
Definition: TrackQuality.h:80
bool ev
Definition: config.py:1
int layer() const
Return the layer number.
Definition: DTLayerId.h:53
int layer() const
Definition: CSCDetId.h:61
void evaluate(SimParticleTrail const &, reco::TrackBaseRef const &, const TrackerTopology *tTopo)
Compute information about the track reconstruction quality.
bool operator==(const QGLikelihoodParameters &lhs, const QGLikelihoodCategory &rhs)
Test if parameters are compatible with category.
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:41
DetLayer getDetLayer(DetId detId, const TrackerTopology *tTopo)
Definition: TrackQuality.cc:90
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:109
const Layer & layer(unsigned int index) const
Return information about the given layer by index.
Definition: TrackQuality.h:77
std::vector< Layer > layers_
Definition: TrackQuality.h:83
int layer() const
Definition: RPCDetId.h:108
Definition: DetId.h:18
static constexpr int RPC
Definition: MuonSubdetId.h:14
unsigned int layer(const DetId &id) const
bool operator<(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:73
HLT enums.
std::vector< Hit > hits
Definition: TrackQuality.h:56
void newEvent(const edm::Event &, const edm::EventSetup &)
Pre-process event information (for accessing reconstruction information)
static constexpr int DT
Definition: MuonSubdetId.h:12
TrackQuality(const edm::ParameterSet &, edm::ConsumesCollector &iC)
Constructor by pset.
static constexpr int CSC
Definition: MuonSubdetId.h:13
def move(src, dest)
Definition: eostools.py:511
static const int statePriorities[]
Definition: TrackQuality.cc:80
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:114