CMS 3D CMS Logo

Classes | Functions
ntuplePrintersDiff Namespace Reference

Classes

class  _DiffResult
 
class  _IndentPrinter
 
class  _RecHitPrinter
 
class  _TrackAssociation
 
class  _TrackingParticleMatchPrinter
 
class  _TracksByHitsMatcher
 
class  SeedPrinter
 
class  TrackingParticlePrinter
 
class  TrackPrinter
 

Functions

def _areSameTracks (trk1, trk2)
 
def _associateTracksByTrackingParticlesAndHits (lst1, lst2)
 
def _commonHits (trk1, trk2)
 
def _difflist (list1, list2)
 
def _formatHitDiffForTwiki (diffHits, prefix)
 
def _hitPatternSummary (hits)
 
def _makediff (list1, list2, equalPrefix=" ")
 
def _mapdiff (func, obj1, obj2)
 
def _matchTracksByHits (reftrk, trklist)
 
def diffTrackListsFromSameTrackingParticle (trackPrinter, lst1, lst2, lst1extra=[], lst2extra=[], diffByHitsOnly=False)
 
def diffTrackListsGeneric (trackPrinter, lst1, lst2, ignoreAdditionalLst2=False)
 

Function Documentation

◆ _areSameTracks()

def ntuplePrintersDiff._areSameTracks (   trk1,
  trk2 
)
private

Definition at line 178 of file ntuplePrintersDiff.py.

References _commonHits().

Referenced by diffTrackListsFromSameTrackingParticle(), and diffTrackListsGeneric().

178 def _areSameTracks(trk1, trk2):
179  ncommon = _commonHits(trk1, trk2)
180 
181  # if tracks have same hits, consider their reco as identical
182  if not (ncommon == trk1.nValid() and ncommon == trk2.nValid()):
183  return False
184 
185  # although if there is any change in their iterations, mark them different
186  if not (trk1.algoMask() == trk2.algoMask() and trk1.algo() == trk2.algo() and trk1.originalAlgo() == trk2.originalAlgo()):
187  return False
188 
189  # if reco is the same, check if they are matched to the
190  # same TPs (in case the track-TP matching is modified
191  # between ntuples)
192  if trk1.nMatchedTrackingParticles() != trk2.nMatchedTrackingParticles():
193  return False
194 
195  for tpInfo1, tpInfo2 in itertools.izip(trk1.matchedTrackingParticleInfos(), trk2.matchedTrackingParticleInfos()):
196  if tpInfo1.trackingParticle().index() != tpInfo2.trackingParticle().index():
197  return False
198 
199  return True
200 
def _areSameTracks(trk1, trk2)
def _commonHits(trk1, trk2)

◆ _associateTracksByTrackingParticlesAndHits()

def ntuplePrintersDiff._associateTracksByTrackingParticlesAndHits (   lst1,
  lst2 
)
private

Definition at line 371 of file ntuplePrintersDiff.py.

References str.

Referenced by diffTrackListsGeneric().

372  trks1 = list(lst1)
373  trks2 = list(lst2)
374 
375  trks1Matcher = _TracksByHitsMatcher(trks1)
376  trks2Matcher = _TracksByHitsMatcher(trks2)
377 
378  # Used to have exactly the same Track objects for the same index
379  trks1Dict = {t.index(): t for t in trks1}
380  trks2Dict = {t.index(): t for t in trks2}
381 
382  # Bit of a hack...
383  tps1 = None
384  tps2 = None
385  if len(trks1) > 0:
386  tps1 = TrackingParticles(trks1[0]._tree)
387  if len(trks2) > 0:
388  tps2 = TrackingParticles(trks2[0]._tree)
389 
390  trkAssoc1 = {}
391  trkAssoc2 = {}
392 
393  def _getOrCreateAssoc(trk, d, **kwargs):
394  if trk.index() in d:
395  a = d[trk.index()]
396  else:
397  a = _TrackAssociation()
398  d[trk.index()] = a
399  a.extend(**kwargs)
400  return a
401 
402  while len(trks1) > 0:
403  trk1 = trks1.pop(0)
404  assoc1 = _getOrCreateAssoc(trk1, trkAssoc1, trks1=[trk1])
405 
406  # First associate via TP
407  if trk1.nMatchedTrackingParticles() > 0 and tps2:
408  matched = False
409 
410  for tpInfo1 in trk1.matchedTrackingParticleInfos():
411  tp1 = tpInfo1.trackingParticle()
412 
413  # Find possible duplicates within trks1
414  for trkInfo1 in tp1.matchedTrackInfos():
415  t1 = trkInfo1.track()
416  t1Index = t1.index()
417  if t1Index != trk1.index():
418  if t1Index in trks1Dict:
419  assoc1.extend(trks1=[t1]) # trk1 -> t1
420  _getOrCreateAssoc(t1, trkAssoc1, trks1=[t1, trk1]) # t1 -> trk1
421  #print "trk1 %d <-> t1 %d (TP)" % (trk1.index(), t1.index())
422  trks1.remove(trks1Dict[t1Index])
423  else:
424  #print "trk1 %d -> t1 %d (TP, not in list)" % (trk1.index(), t1.index())
425  assoc1.extend(trks1OutsideList=[t1]) # trk1 -> t1, if t1 is not in trks1
426 
427  # Then look for the same tp in trks2
428  tp2 = tps2[tp1.index()]
429  for trkInfo2 in tp2.matchedTrackInfos():
430  matched = True
431  t2 = trkInfo2.track()
432  t2Index = t2.index()
433  if t2Index in trks2Dict:
434  assoc1.extend(trks2=[t2]) # trk1 -> t2
435  _getOrCreateAssoc(t2, trkAssoc2, trks1=[trk1], trks2=[t2]) # t2 -> trk1
436  #print "trk1 %d <-> t2 %d (TP)" % (trk1.index(), t2.index())
437  try:
438  trks2.remove(trks2Dict[t2Index]) # can fail if t2 has already been matched via hits
439  except ValueError:
440  pass
441  else:
442  #print "trk1 %d -> t2 %d (TP, not in list)" % (trk1.index(), t2.index())
443  assoc1.extend(trks2OutsideList=[t2]) # trk1 -> t2, if t2 is not in trks2
444 
445  if matched:
446  continue
447 
448  # If no matching tracks in trks2 via TrackingParticles, then
449  # proceed finding the best match via hits
450  (matchedTrk2, ncommon) = trks2Matcher.match(trk1)
451  if matchedTrk2 is not None and ncommon >= 3:
452  assoc1.extend(trks2=[matchedTrk2])
453  assoc2 = _getOrCreateAssoc(matchedTrk2, trkAssoc2, trks1=[trk1], trks2=[matchedTrk2])
454  #print "trk1 %d <-> t2 %d (hits)" % (trk1.index(), matchedTrk2.index())
455  try:
456  trks2.remove(matchedTrk2) # can fail if matchedTrk2 has already been matched via TP
457  except ValueError:
458  pass
459 
460  (matchedTrk1, ncommon1) = trks1Matcher.match(matchedTrk2)
461  # if matchedTrk1 has TP, the link from matchedTrk1 -> matchedTrk2 will be created later
462  if (matchedTrk1.nMatchedTrackingParticles() == 0 or not tps2) and matchedTrk1.index() != trk1.index():
463  assoc2.extend(trks1=[matchedTrk1])
464  _getOrCreateAssoc(matchedTrk1, trkAssoc1, trks1=[matchedTrk1], trks2=[matchedTrk2])
465  #print "trk1 %d <-> t2 %d (hits, via t2)" % (matchedTrk1.index(), matchedTrk2.index())
466 
467  # no match
468 
469  # remaining tracks in trks2
470  for trk2 in trks2:
471  assoc2 = _getOrCreateAssoc(trk2, trkAssoc2, trks2=[trk2])
472  # collect duplicates
473  if trk2.nMatchedTrackingParticles() > 0:
474  for tpInfo2 in trk2.matchedTrackingParticleInfos():
475  tp2 = tpInfo2.trackingParticle()
476  for trkInfo2 in tp2.matchedTrackInfos():
477  t2 = trkInfo2.track()
478  t2Index = t2.index()
479  if t2Index in trks2Dict:
480  assoc2.extend(trks2=[t2])
481  #print "trk2 %d -> t2 %d (TP)" % (trk2.index(), t2.index())
482  else:
483  assoc2.extend(trks2OutsideList=[t2])
484  #print "trk2 %d -> t2 %d (TP, not in list)" % (trk2.index(), t2.index())
485 
486  # merge results
487  # any good way to avoid copy-past?
488  for ind, assoc in trkAssoc1.items():
489  for t1 in assoc.trks1():
490  a = trkAssoc1[t1.index()]
491  assoc.merge(a)
492  a.merge(assoc)
493  for t2 in assoc.trks2():
494  a = trkAssoc2[t2.index()]
495  assoc.merge(a)
496  a.merge(assoc)
497  for ind, assoc in trkAssoc2.items():
498  for t2 in assoc.trks2():
499  a = trkAssoc2[t2.index()]
500  assoc.merge(a)
501  a.merge(assoc)
502  for t1 in assoc.trks1():
503  a = trkAssoc1[t1.index()]
504  assoc.merge(a)
505  a.merge(assoc)
506 
507  for ind, assoc in itertools.chain(trkAssoc1.items(), trkAssoc2.items()):
508  #if ind in [437, 1101]:
509  # print "----"
510  # print ind, [t.index() for t in assoc.trks1()], [t.index() for t in assoc.trks2()]
511  for t1 in assoc.trks1():
512  a = trkAssoc1[t1.index()]
513  assoc.merge(a)
514  a.merge(assoc)
515 
516  #if ind in [437, 1101]:
517  # print ind, [t.index() for t in assoc.trks1()], [t.index() for t in assoc.trks2()]
518 
519  for t2 in assoc.trks2():
520  a = trkAssoc2[t2.index()]
521  assoc.merge(a)
522  a.merge(assoc)
523  #if ind in [437, 1101]:
524  # print ind, [t.index() for t in assoc.trks1()], [t.index() for t in assoc.trks2()]
525  # print "####"
526 
527  # collapse to a single collection of associations
528  allAssocs = []
529  while len(trkAssoc1) > 0:
530  (t1Index, assoc) = trkAssoc1.popitem()
531 
532  #if t1Index == 1299:
533  # print t1Index, [t.index() for t in assoc.trks2()]
534  for t1 in assoc.trks1():
535  if t1.index() == t1Index: continue
536  trkAssoc1.pop(t1.index())
537  for t2 in assoc.trks2():
538  trkAssoc2.pop(t2.index())
539  allAssocs.append(assoc)
540  while len(trkAssoc2) > 0:
541  (t2Index, assoc) = trkAssoc2.popitem()
542  if len(assoc.trks1()) > 0:
543  raise Exception("len(assoc.trks1()) %d != 0 !!! %s for t2 %d" % (len(assoc.trks1()), str([t.index() for t in assoc.trks1()]), t2Index))
544  for t2 in assoc.trks2():
545  if t2.index() == t2Index: continue
546  trkAssoc2.pop(t2.index())
547  allAssocs.append(assoc)
548 
549  return allAssocs
550 
#define str(s)
def _associateTracksByTrackingParticlesAndHits(lst1, lst2)

◆ _commonHits()

def ntuplePrintersDiff._commonHits (   trk1,
  trk2 
)
private
Returns the number of common hits in trk1 and trk2. Matching is
done via the hit type and index, so effectively the matching is
done by clusters. Invalid hits are ignored.

Definition at line 14 of file ntuplePrintersDiff.py.

Referenced by _areSameTracks(), and diffTrackListsGeneric().

14 def _commonHits(trk1, trk2):
15  """Returns the number of common hits in trk1 and trk2. Matching is
16  done via the hit type and index, so effectively the matching is
17  done by clusters. Invalid hits are ignored.
18 
19  """
20  hits1 = set()
21  for hit in trk1.hits():
22  if not hit.isValidHit(): continue
23  hits1.add( (type(hit), hit.index()) )
24 
25  ncommon = 0
26  for hit in trk2.hits():
27  if not hit.isValidHit(): continue
28  if (type(hit), hit.index()) in hits1:
29  ncommon += 1
30 
31  return ncommon
32 
def _commonHits(trk1, trk2)

◆ _difflist()

def ntuplePrintersDiff._difflist (   list1,
  list2 
)
private

Definition at line 159 of file ntuplePrintersDiff.py.

Referenced by _makediff().

159 def _difflist(list1, list2):
160  diff = difflib.unified_diff(list1, list2, lineterm="", n=len(list1))
161  for item in diff:
162  if item[:2] == "@@":
163  break
164  return list(diff)
165 
def _difflist(list1, list2)

◆ _formatHitDiffForTwiki()

def ntuplePrintersDiff._formatHitDiffForTwiki (   diffHits,
  prefix 
)
private

Definition at line 619 of file ntuplePrintersDiff.py.

References join().

Referenced by ntuplePrintersDiff.SeedPrinter.diff(), and ntuplePrintersDiff.TrackPrinter.diff().

619 def _formatHitDiffForTwiki(diffHits, prefix):
620  line_re = re.compile("(?P<sign>[ \-+])\s+(?P<det>[a-zA-Z]+)(?P<lay>\d+)\D*?(\((?P<missing>missing|inactive)\))?\s+\d+")
621 
622  summary = []
623  prevdet = ""
624  prevsign = " "
625  diffLines = diffHits.lines()
626 
627  # skip anything before the first line with "hits"
628  for line in diffLines:
629  if "hits" in line:
630  break
631 
632  header = True
633  for line in diffLines:
634  # skip multiple occurrances of "hits" line, but only until
635  # first line without "hits" is encountered
636  if header:
637  if "hits" in line:
638  continue
639  else:
640  header = False
641 
642  m = line_re.search(line)
643  if not m:
644  break
645  raise Exception("regex not found from line %s" % line.rstrip())
646  sign = m.group("sign")
647  det = m.group("det")
648  lay = m.group("lay")
649 
650  if det != prevdet:
651  if prevsign != " ":
652  summary.append("%ENDCOLOR%")
653  prevsign = " "
654  summary.extend([" ", det])
655  prevdet = det
656 
657  if sign != prevsign:
658  if prevsign != " ":
659  summary.append("%ENDCOLOR%")
660  if sign == "-":
661  summary.append("%RED%")
662  elif sign == "+":
663  summary.append("%GREEN%")
664  prevsign = sign
665 
666  #print sign, det, lay
667  #if len(summary) > 0:
668  # print " ", summary[-1]
669 
670  #if det != prevdet:
671  # if prevsign != " ":
672  # #if len(summary) > 0:
673  # # if
674  # summary.append("%ENDCOLOR")
675  # summary.extend([" ", det])
676  # if prevsign == "-":
677  # summary.append("%RED%")
678  # elif prevsign == "+":
679  # summary.append("%GREEN%")
680  # prevdet = det
681  summary.append(lay)
682  if m.group("missing"):
683  if m.group("missing") == "missing":
684  summary.append("(m)")
685  elif m.group("missing") == "inactive":
686  summary.append("(i)")
687 
688  if prevsign != " ":
689  summary.append("%ENDCOLOR%")
690  # prune "changed" missing/inactive hits
691  i = 2
692  while i < len(summary)-5:
693  if summary[i] == "(i)" or summary[i] == "(m)":
694  if summary[i-2] == "%RED%" and summary[i+1] == "%ENDCOLOR%" and summary[i+2] == "%GREEN%" and summary[i+3] == summary[i-1] and summary[i+4] == summary[i] and summary[i+5] == "%ENDCOLOR%":
695  summary[i-2:i+6] = [summary[i-1], summary[i]]
696  i += 1
697 
698  line = " "+"".join(summary)
699  return ["?"+prefix+line]
700 
701 # Common detailed printout helpers
def _formatHitDiffForTwiki(diffHits, prefix)
static std::string join(char **cmd)
Definition: RemoteFile.cc:21

◆ _hitPatternSummary()

def ntuplePrintersDiff._hitPatternSummary (   hits)
private

Definition at line 702 of file ntuplePrintersDiff.py.

References str.

Referenced by ntuplePrintersDiff.SeedPrinter.printHits(), ntuplePrintersDiff.TrackPrinter.printHits(), and ntuplePrintersDiff.TrackingParticlePrinter.printHits().

702 def _hitPatternSummary(hits):
703  summary = ""
704 
705  prevdet = 0
706  for hit in hits:
707  det = hit.subdet()
708  lay = hit.layer()
709 
710  if det != prevdet:
711  summary += " "+SubDet.toString(det)
712  prevdet = det
713 
714  summary += str(lay)
715  if isinstance(hit, InvalidHit):
716  summary += "(%s)"%InvalidHit.Type.toString(hit.type())[0]
717 
718  return summary
719 
#define str(s)

◆ _makediff()

def ntuplePrintersDiff._makediff (   list1,
  list2,
  equalPrefix = " " 
)
private

Definition at line 166 of file ntuplePrintersDiff.py.

References _difflist().

Referenced by _mapdiff(), ntuplePrintersDiff.SeedPrinter.diff(), ntuplePrintersDiff.TrackPrinter.diff(), ntuplePrintersDiff.TrackingParticlePrinter.diffMatchedSeeds(), ntuplePrintersDiff.TrackingParticlePrinter.diffMatchedTracks(), and diffTrackListsFromSameTrackingParticle().

166 def _makediff(list1, list2, equalPrefix=" "):
167  diff = _difflist(list1, list2)
168  if len(diff) == 0:
169  return _DiffResult([equalPrefix+s for s in list1], hasDifference=False)
170  else:
171  return _DiffResult(diff, hasDifference=True)
172 
def _makediff(list1, list2, equalPrefix=" ")
def _difflist(list1, list2)

◆ _mapdiff()

def ntuplePrintersDiff._mapdiff (   func,
  obj1,
  obj2 
)
private

Definition at line 173 of file ntuplePrintersDiff.py.

References _makediff(), and EcalMonitorTask_cff.func.

Referenced by ntuplePrintersDiff.SeedPrinter.diff(), ntuplePrintersDiff.TrackPrinter.diff(), ntuplePrintersDiff.TrackingParticlePrinter.diff(), and diffTrackListsFromSameTrackingParticle().

173 def _mapdiff(func, obj1, obj2):
174  lst1 = func(obj1) if obj1 is not None else []
175  lst2 = func(obj2) if obj2 is not None else []
176  return _makediff(lst1, lst2)
177 
def _mapdiff(func, obj1, obj2)
def _makediff(list1, list2, equalPrefix=" ")

◆ _matchTracksByHits()

def ntuplePrintersDiff._matchTracksByHits (   reftrk,
  trklist 
)
private

Definition at line 33 of file ntuplePrintersDiff.py.

Referenced by ntuplePrintersDiff.TrackingParticlePrinter.diffMatchedSeeds(), and diffTrackListsFromSameTrackingParticle().

33 def _matchTracksByHits(reftrk, trklist):
34  if len(trklist) == 0:
35  return (None, 0)
36 
37  hits1 = set()
38  for hit in reftrk.hits():
39  if not hit.isValidHit(): continue
40  hits1.add( (type(hit), hit.index()) )
41 
42  best = (None, 0)
43  for trk in trklist:
44  ncommon = 0
45  for hit in trk.hits():
46  if not hit.isValidHit(): continue
47  if (type(hit), hit.index()) in hits1:
48  ncommon += 1
49  if ncommon > best[1]:
50  best = (trk, ncommon)
51 
52  return best
53 
def _matchTracksByHits(reftrk, trklist)

◆ diffTrackListsFromSameTrackingParticle()

def ntuplePrintersDiff.diffTrackListsFromSameTrackingParticle (   trackPrinter,
  lst1,
  lst2,
  lst1extra = [],
  lst2extra = [],
  diffByHitsOnly = False 
)
lst1 and lst2 are the main lists to make the diff from.

lst1extra and lst2extra are optional to provide suplementary
tracks. Use case: lst1 and lst2 are subset of full tracks,
lst1extra and lst2extra contain tracks matched to the same
TrackingParticle but are outside of the selection of lst1/lst2.

Definition at line 201 of file ntuplePrintersDiff.py.

References _areSameTracks(), _makediff(), _mapdiff(), _matchTracksByHits(), and GetRecoTauVFromDQM_MC_cff.next.

Referenced by ntuplePrintersDiff.TrackingParticlePrinter.diffMatchedTracks(), and diffTrackListsGeneric().

201 def diffTrackListsFromSameTrackingParticle(trackPrinter, lst1, lst2, lst1extra=[], lst2extra=[], diffByHitsOnly=False):
202  """lst1 and lst2 are the main lists to make the diff from.
203 
204  lst1extra and lst2extra are optional to provide suplementary
205  tracks. Use case: lst1 and lst2 are subset of full tracks,
206  lst1extra and lst2extra contain tracks matched to the same
207  TrackingParticle but are outside of the selection of lst1/lst2.
208  """
209 
210  diff = _DiffResult()
211 
212  _trks1extra = list(lst1extra)
213  _trks2extra = list(lst2extra)
214 
215  trks1 = list(lst1)+_trks1extra
216  trks2 = list(lst2)+_trks2extra # make copy because it is modified
217 
218  trks1extra = set([t.index() for t in _trks1extra])
219  trks2extra = set([t.index() for t in _trks2extra])
220 
221  trks1Empty = (len(trks1) == 0)
222  trks2Empty = (len(trks2) == 0)
223 
224  if trks1Empty and trks2Empty:
225  return diff
226 
227  # make sure all tracks really come from a single TP
228  # just to simplify the work loop, generalization can be considered later
229  commonTP = None
230  def _findCommonTP(_lst, _commonTP, _name):
231  for trk in _lst:
232  if trk.nMatchedTrackingParticles() != 1:
233  raise Exception("Track %d from %s is matched to %d TPs. This is not supported by this function yet." % (trk.index(), _name, trk.nMatchedTrackingParticles()))
234  if _commonTP is None:
235  _commonTP = next(trk.matchedTrackingParticleInfos()).trackingParticle()
236  else:
237  tp = next(trk.matchedTrackingParticleInfos()).trackingParticle()
238  if tp.index() != _commonTP.index():
239  raise Exception("Track %d from %s is matched to TP %d, which differs from the TP %d of already processed tracks." % (trk.index(), _name, _commonTP.index(), tp.index()))
240  return _commonTP
241  commonTP = _findCommonTP(trks1, commonTP, "lst1")
242  commonTP = _findCommonTP(trks2, commonTP, "lst2")
243 
244  # Need some tracks from trks1 and trks2 to print the TrackingParticle information
245  someTrk1 = trks1[0] if not trks1Empty else None
246  someTrk2 = trks2[0] if not trks2Empty else None
247 
248  for trk1 in trks1:
249  (matchedTrk2, ncommon) = _matchTracksByHits(trk1, trks2)
250 
251  # no more tracks in tp2
252  if matchedTrk2 is None:
253  if trk1.index() in trks1extra:
254  raise Exception("Track %d was found in trks1extra but matchedTrk2 is None, this should not happen" % trk1.index())
255  diff.extend(_makediff(trackPrinter.printTrack(trk1), []))
256  else: # diff trk1 to best-matching track from trks2
257  someTrk2 = matchedTrk2
258  trks2.remove(matchedTrk2)
259  tmp = trackPrinter.diff(trk1, matchedTrk2, diffTrackingParticles=False)
260  if diffByHitsOnly and _areSameTracks(trk1, matchedTrk2):
261  tmp.setDifference(False)
262  tmp.highlight(plus=(matchedTrk2.index() in trks2extra), minus=(trk1.index() in trks1extra))
263  diff.extend(tmp)
264 
265  for trk2 in trks2: # remaining tracks in trks2
266  if trk2.index() in trks2extra:
267  raise Exception("Track %d was found in trks2extra, but without matching track in trks1, this should not happen" % trk2.index())
268  diff.extend(_makediff([], trackPrinter.printTrack(trk2)))
269 
270  # finally add information of the trackingParticle
271  # easiest is to pass a track matched to the TP
272  tmp = _mapdiff(trackPrinter.printMatchedTrackingParticles, someTrk1, someTrk2)
273  tmp.setDifference(False) # we know the TP is the same, even if the "track match" information will differ
274  def _makere(lst):
275  r = []
276  for i in lst: r.extend([re.compile("Tracks:.*%d:"%i), re.compile("matched to tracks.*%d"%i)])
277  return r
278  plusre = _makere(trks2extra)
279  minusre = _makere(trks1extra)
280  tmp.highlightLines(plusre, minusre)
281  diff.extend(tmp)
282 
283  return diff
284 
def _mapdiff(func, obj1, obj2)
def _areSameTracks(trk1, trk2)
def _makediff(list1, list2, equalPrefix=" ")
def diffTrackListsFromSameTrackingParticle(trackPrinter, lst1, lst2, lst1extra=[], lst2extra=[], diffByHitsOnly=False)
def _matchTracksByHits(reftrk, trklist)

◆ diffTrackListsGeneric()

def ntuplePrintersDiff.diffTrackListsGeneric (   trackPrinter,
  lst1,
  lst2,
  ignoreAdditionalLst2 = False 
)

Definition at line 551 of file ntuplePrintersDiff.py.

References _areSameTracks(), _associateTracksByTrackingParticlesAndHits(), _commonHits(), diffTrackListsFromSameTrackingParticle(), and GetRecoTauVFromDQM_MC_cff.next.

551 def diffTrackListsGeneric(trackPrinter, lst1, lst2, ignoreAdditionalLst2=False):
552  associations = _associateTracksByTrackingParticlesAndHits(lst1, lst2)
553 
554  # sort in eta
555  associations.sort(key=methodcaller("minEta"))
556 
557  diff = _DiffResult()
558  for assoc in associations:
559  if assoc.hasCommonTrackingParticle():
560  if len(assoc.trks1()) == 0 and ignoreAdditionalLst2:
561  continue
562 
563  tmp = diffTrackListsFromSameTrackingParticle(trackPrinter, assoc.trks1(), assoc.trks2(), lst1extra=assoc.trks1OutsideList(), lst2extra=assoc.trks2OutsideList(), diffByHitsOnly=True)
564  if tmp.hasDifference():
565  diff.extend(tmp)
566  diff.extend([" "])
567  elif len(assoc.trks1()) == 1 and len(assoc.trks2()) == 1:
568  trk1 = assoc.trks1()[0]
569  trk2 = assoc.trks2()[0]
570 
571  if not _areSameTracks(trk1, trk2):
572  diff.extend(trackPrinter.diff(trk1, trk2))
573  diff.extend([" "])
574  elif len(assoc.trks2()) == 0:
575  for t in assoc.trks1():
576  diff.extend(trackPrinter.diff(t, None))
577  diff.extend([" "])
578  elif len(assoc.trks1()) == 0:
579  if ignoreAdditionalLst2:
580  continue
581  for t in assoc.trks1():
582  diff.extend(trackPrinter.diff(None, t))
583  diff.extend([" "])
584  else:
585  # needs to be rather generic, let's start by sorting by the innermost hit
586  trks1 = list(assoc.trks1())
587  trks2 = list(assoc.trks2())
588  trks1.sort(key=lambda t: next(t.hits()).r())
589  trks2.sort(key=lambda t: next(t.hits()).r())
590 
591  # then calculate number of shared hits for each pair
592  ncommon = []
593  for i1, t1 in enumerate(trks1):
594  for i2, t2 in enumerate(trks2):
595  ncommon.append( (i1, i2, _commonHits(t1, t2)) )
596 
597  # sort that by number of common hits, descending order
598  ncommon.sort(key=itemgetter(2), reverse=True)
599 
600  # then make the diffs starting from the pair with largest number of common hits
601  pairs = [None]*len(trks1)
602  usedT2 = [False]*len(trks2)
603  for i1, i2, ncom in ncommon:
604  if pairs[i1] is None:
605  pairs[i1] = i2
606  usedT2[i2] = True
607 
608  for i1, i2 in enumerate(pairs):
609  t1 = trks1[i1]
610  t2 = trks2[i2]
611  diff.extend(trackPrinter.diff(t1, t2))
612  for i2, used in enumerate(usedT2):
613  if not used:
614  diff.extend(trackPrinter.diff(None, trks2[i2]))
615  diff.extend([" "])
616 
617  return diff
618 
def _areSameTracks(trk1, trk2)
def diffTrackListsFromSameTrackingParticle(trackPrinter, lst1, lst2, lst1extra=[], lst2extra=[], diffByHitsOnly=False)
def _commonHits(trk1, trk2)
def diffTrackListsGeneric(trackPrinter, lst1, lst2, ignoreAdditionalLst2=False)
def _associateTracksByTrackingParticlesAndHits(lst1, lst2)