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