CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackQuality.cc
Go to the documentation of this file.
1 /*
2  * TrackQuality.cc
3  *
4  * Created by Christophe Saout on 9/25/08.
5  * Copyright 2007 __MyCompanyName__. All rights reserved.
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 {
40 static const uint32_t NonMatchedTrackId = (uint32_t)-1;
41 
42 struct MatchedHit
43 {
44  DetId detId;
45  uint32_t simTrackId;
46  EncodedEventId collision;
47  int recHitId;
49 
50  bool operator < (const MatchedHit &other) const
51  {
52  if (detId < other.detId)
53  return true;
54  else if (detId > other.detId)
55  return false;
56  else if (collision < other.collision)
57  return true;
58  else if (other.collision < collision)
59  return false;
60  else
61  return simTrackId < other.simTrackId;
62  }
63 
64  bool operator == (const MatchedHit &other) const
65  {
66  return detId == other.detId &&
67  collision == other.collision &&
68  simTrackId == other.simTrackId;
69  }
70 };
71 
72 static bool operator < (const MatchedHit &hit, DetId detId)
73 {
74  return hit.detId < detId;
75 }
76 static bool operator < (DetId detId, const MatchedHit &hit)
77 {
78  return detId < hit.detId;
79 }
80 }
81 
82 typedef std::pair<TrackQuality::Layer::SubDet, short int> DetLayer;
83 
84 // in case multiple hits were found, figure out the highest priority
85 static const int statePriorities[] =
86 {
87  /* Unknown */ 3,
88  /* Good */ 5,
89  /* Missed */ 0,
90  /* Noise */ 7,
91  /* Bad */ 2,
92  /* Dead */ 4,
93  /* Shared */ 6,
94  /* Misassoc */ 1
95 };
96 
97 DetLayer getDetLayer(DetId detId, const TrackerTopology *tTopo)
98 {
100  short int layer = 0;
101 
102  switch (detId.det())
103  {
104  case DetId::Tracker:
105  layer=tTopo->layer(detId);
106  break;
107 
108  case DetId::Muon:
109  switch (detId.subdetId())
110  {
111  case MuonSubdetId::DT:
113  layer = DTLayerId(detId).layer();
114  break;
115 
116  case MuonSubdetId::CSC:
118  layer = CSCDetId(detId).layer();
119  break;
120 
121  case MuonSubdetId::RPC:
122  if (RPCDetId(detId).region())
124  else
126  layer = RPCDetId(detId).layer();
127  break;
128 
129  default:
130  /* should not get here */
131  ;
132  }
133  break;
134 
135  default:
136  /* should not get here */
137  ;
138  }
139 
140  return DetLayer(det, layer);
141 }
142 
144  associatorPSet_(config.getParameter<edm::ParameterSet>("hitAssociator"))
145 {
146 }
147 
149 {
151 }
152 
154  reco::TrackBaseRef const &tr,
155  const TrackerTopology *tTopo)
156 {
157  std::vector<MatchedHit> matchedHits;
158 
159  // iterate over reconstructed hits
161  hit != tr->recHitsEnd(); ++hit)
162  {
163  // on which module the hit lies
164  DetId detId = (*hit)->geographicalId();
165 
166  // FIXME: check for double-sided modules?
167 
168  // didn't find a hit on that module
169  if (!(*hit)->isValid())
170  {
171  MatchedHit matchedHit;
172  matchedHit.detId = detId;
173  matchedHit.simTrackId = NonMatchedTrackId;
174  // check why hit wasn't valid and propagate information
175  switch ((*hit)->getType())
176  {
178  matchedHit.state = Layer::Dead;
179  break;
180 
181  case TrackingRecHit::bad:
182  matchedHit.state = Layer::Bad;
183  break;
184 
185  default:
186  matchedHit.state = Layer::Missed;
187  }
188  matchedHit.recHitId = hit - tr->recHitsBegin();
189  matchedHits.push_back(matchedHit);
190  continue;
191  }
192 
193  // find simulated tracks causing hit that was reconstructed
194  std::vector<SimHitIdpr> simIds = associator_->associateHitId(**hit);
195 
196  // must be noise or so
197  if (simIds.empty())
198  {
199  MatchedHit matchedHit;
200  matchedHit.detId = detId;
201  matchedHit.simTrackId = NonMatchedTrackId;
202  matchedHit.state = Layer::Noise;
203  matchedHit.recHitId = hit - tr->recHitsBegin();
204  matchedHits.push_back(matchedHit);
205  continue;
206  }
207 
208  // register all simulated tracks contributing
209  for (std::vector<SimHitIdpr>::const_iterator i = simIds.begin();
210  i != simIds.end(); ++i)
211  {
212  MatchedHit matchedHit;
213  matchedHit.detId = detId;
214  matchedHit.simTrackId = i->first;
215  matchedHit.collision = i->second;
216  // RecHit <-> SimHit matcher currently doesn't support muon system
217  if (detId.det() == DetId::Muon)
218  matchedHit.state = Layer::Unknown;
219  else
220  // assume hit was mismatched (until possible confirmation)
221  matchedHit.state = Layer::Misassoc;
222  matchedHit.recHitId = hit - tr->recHitsBegin();
223  matchedHits.push_back(matchedHit);
224  }
225  }
226 
227  // sort hits found so far by module id
228  std::stable_sort(matchedHits.begin(), matchedHits.end());
229 
230  std::vector<MatchedHit>::size_type size = matchedHits.size();
231 
232 #warning "This file has been modified just to get it to compile without any regard as to whether it still functions as intended"
233 #ifdef REMOVED_JUST_TO_GET_IT_TO_COMPILE__THIS_CODE_NEEDS_TO_BE_CHECKED
234  // now iterate over simulated hits and compare (tracks in chain first)
235  for (SimParticleTrail::const_iterator track = spt.begin();
236  track != spt.end(); ++track)
237  {
238  // iterate over all hits in track
239  for (std::vector<PSimHit>::const_iterator hit =
240  (*track)->pSimHit_begin();
241  hit != (*track)->pSimHit_end(); ++hit)
242  {
243  MatchedHit matchedHit;
244  matchedHit.detId = DetId(hit->detUnitId());
245  matchedHit.simTrackId = hit->trackId();
246  matchedHit.collision = hit->eventId();
247 
248  // find range of reconstructed hits belonging to this module
249  std::pair<std::vector<MatchedHit>::iterator,
250  std::vector<MatchedHit>::iterator>
251  range = std::equal_range(
252  matchedHits.begin(),
253  matchedHits.begin() + size,
254  matchedHit.detId);
255 
256  // no reconstructed hit found, remember this as a missed module
257  if (range.first == range.second)
258  {
259  matchedHit.state = Layer::Missed;
260  matchedHit.recHitId = -1;
261  matchedHits.push_back(matchedHit);
262  continue;
263  }
264 
265  // now find if the hit belongs to the correct simulated track
266  std::vector<MatchedHit>::iterator pos =
267  std::lower_bound(range.first,
268  range.second,
269  matchedHit);
270 
271  // if it does, check for being a shared hit (was Misassoc before)
272  if (pos != range.second)
273  {
274  if (range.second - range.first > 1) // more than one SimHit
275  pos->state = Layer::Shared;
276  else
277  pos->state = Layer::Good; // only hit -> good hit
278  }
279  }
280  }
281 #endif
282 
283  // in case we added missed modules, re-sort
284  std::stable_sort(matchedHits.begin(), matchedHits.end());
285 
286  // prepare for ordering results by layer enum and layer/disk number
287  typedef std::multimap<DetLayer, const MatchedHit*> LayerHitMap;
288  LayerHitMap layerHitMap;
289 
290  // iterate over all simulated/reconstructed hits again
291  for (std::vector<MatchedHit>::const_iterator hit = matchedHits.begin();
292  hit != matchedHits.end();)
293  {
294  // we can have multiple reco-to-sim matches per module, find best one
295  const MatchedHit *best = 0;
296 
297  // this loop iterates over all subsequent hits in the same module
298  do
299  {
300  // update our best hit pointer
301  if (!best ||
302  statePriorities[hit->state] > statePriorities[best->state] ||
303  best->simTrackId == NonMatchedTrackId)
304  {
305  best = &*hit;
306  }
307  ++hit;
308  }
309  while (hit != matchedHits.end() &&
310  hit->detId == best->detId);
311 
312  // ignore hit in case track reco was looking at the wrong module
313  if (best->simTrackId != NonMatchedTrackId ||
314  best->state != Layer::Missed)
315  {
316  layerHitMap.insert(std::make_pair(
317  getDetLayer(best->detId,tTopo), best));
318  }
319  }
320 
321  layers_.clear();
322 
323 #ifdef DEBUG_TRACK_QUALITY
324  std::cout << "---------------------" << std::endl;
325 #endif
326  // now prepare final collection
327  for (LayerHitMap::const_iterator hit = layerHitMap.begin();
328  hit != layerHitMap.end(); ++hit)
329  {
330 #ifdef DEBUG_TRACK_QUALITY
331  std::cout
332  << "detLayer (" << hit->first.first << ", " << hit->first.second << ")"
333  << " [" << (uint32_t)hit->second->detId << "] sim(" << (int)hit->second->simTrackId << ")"
334  << " hit(" << hit->second->recHitId << ") -> " << hit->second->state << std::endl;
335 #endif
336 
337  // find out if we need to start a new layer
338  Layer *layer = layers_.empty() ? 0 : &layers_.back();
339  if (!layer ||
340  hit->first.first != layer->subDet ||
341  hit->first.second != layer->layer)
342  {
343  Layer newLayer;
344  newLayer.subDet = hit->first.first;
345  newLayer.layer = hit->first.second;
346  layers_.push_back(newLayer);
347  layer = &layers_.back();
348  }
349 
350  // create hit and add it to layer
351  Layer::Hit newHit;
352  newHit.recHitId = hit->second->recHitId;
353  newHit.state = hit->second->state;
354 
355  layer->hits.push_back(newHit);
356  }
357 }
int i
Definition: DBlmapReader.cc:9
std::vector< TrackingParticleRef > SimParticleTrail
Definition: TrackQuality.h:31
std::pair< TrackQuality::Layer::SubDet, short int > DetLayer
Definition: TrackQuality.cc:82
std::auto_ptr< TrackerHitAssociator > associator_
Definition: TrackQuality.h:94
bool operator==(const CaloTower &t1, const CaloTower &t2)
Definition: CaloTower.h:211
uint16_t size_type
TrackQuality(const edm::ParameterSet &)
Constructor by pset.
static const int CSC
Definition: MuonSubdetId.h:15
bool operator<(const FedChannelConnection &, const FedChannelConnection &)
void evaluate(SimParticleTrail const &, reco::TrackBaseRef const &, const TrackerTopology *tTopo)
Compute information about the track reconstruction quality.
DetLayer getDetLayer(DetId detId, const TrackerTopology *tTopo)
Definition: TrackQuality.cc:97
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
Definition: Track.h:63
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:39
const Layer & layer(unsigned int index) const
Return information about the given layer by index.
Definition: TrackQuality.h:87
std::vector< Layer > layers_
Definition: TrackQuality.h:96
Definition: DetId.h:20
char state
Definition: procUtils.cc:75
unsigned int layer(const DetId &id) const
static const int RPC
Definition: MuonSubdetId.h:16
std::vector< Hit > hits
Definition: TrackQuality.h:63
tuple cout
Definition: gather_cfg.py:121
static const int DT
Definition: MuonSubdetId.h:14
void newEvent(const edm::Event &, const edm::EventSetup &)
Pre-process event information (for accessing reconstruction information)
Detector det() const
get the detector field from this detid
Definition: DetId.h:37
tuple size
Write out results.
const edm::ParameterSet associatorPSet_
Definition: TrackQuality.h:93
static const int statePriorities[]
Definition: TrackQuality.cc:85
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.
Definition: Track.h:65