def _commonHits(trk1, trk2):
    """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.

    """
    hits1 = set()
    for hit in trk1.hits():
        if not hit.isValidHit(): continue
        hits1.add( (type(hit), hit.index()) )

    ncommon = 0
    for hit in trk2.hits():
        if not hit.isValidHit(): continue
        if (type(hit), hit.index()) in hits1:
            ncommon += 1

    return ncommon