591 def multipleTrackCandidates(self,planesWithClusters=10,nMaxCl=8,dGap=0.2,dMax=0.8,dMax3=0.8,ovMax = 1,doublet=True,debug=False):
592 A,B = ROOT.TVector3(),ROOT.TVector3()
593 h = self.multipleTrackStore
594 h['trackCand'] = {0:{},1:{}}
595 h['sortedClusters'] = {}
596 sortedClusters = h['sortedClusters']
597 self.scifiCluster()
598 clusters = self.clusScifi
599 for aCl in clusters:
600 so = aCl.GetFirst()//100000
601 if not so in sortedClusters:
602 sortedClusters[so]=[]
603 aCl.GetPosition(A,B)
604 if aCl.GetFirst()//100000%10 == 0: pos = A[1]
605 else: pos = A[0]
606 sortedClusters[so].append([pos,A[2],aCl])
607
608 if len(sortedClusters)<planesWithClusters: return
609
610 h['mergedClusters'] = {}
611 mergedClusters = h['mergedClusters']
612 for so in sortedClusters:
613 pos={}
614 mergedClusters[so]=[]
615 for k in range(len(sortedClusters[so])):
616 pos[k] = sortedClusters[so][k][0]
617 sorted_pos = sorted(pos.items(), key=lambda x: x[1])
618 merged = -1
619 for i in range(len(sorted_pos)):
620 x = sorted_pos[i]
621 aClobj = sortedClusters[so][x[0]]
622 if merged < 0:
623 merged+=1
624 mergedClusters[so].append( [aClobj[0],aClobj[1],[aClobj[2]]] )
625 else:
626 prevPos = mergedClusters[so][merged][0]
627 pos = aClobj[0]
628 if pos-prevPos > dGap:
629 merged+=1
630 mergedClusters[so].append( [aClobj[0],aClobj[1],[aClobj[2]]] )
631 else:
632 N = len(mergedClusters[so][merged][2])
633 newPos = (prevPos*N+pos)/(N+1)
634 mergedClusters[so][merged][0]=newPos
635 mergedClusters[so][merged][2].append(aClobj[2])
636
637 for so in mergedClusters:
638 if len(mergedClusters[so]) > nMaxCl: return
639 if len(mergedClusters[so]) < 2: return
640 for p in range(2):
641 if debug: print('-------- p=',p)
642 if doublet:
643
644 h['doublet'] = {}
645 for j1 in range(1,5):
646 h['doublet'][j1] = {}
647 for k1 in range(len(mergedClusters[j1*10+p])):
648 aCl1 = mergedClusters[j1*10+p][k1]
649 h['doublet'][j1][k1] = []
650 j2 = j1+1
651 for k2 in range(len(mergedClusters[j2*10+p])):
652 aCl2 = mergedClusters[j2*10+p][k2]
653 D = aCl2[0]-aCl1[0]
654 if debug: print(j1,j2,'x1',aCl1[0],'x2',aCl2[0],'D',D)
655 if abs(D ) > dMax: continue
656 h['doublet'][j1][k1].append(10*j2+k2)
657 if len(h['doublet'][j1][k1]) == 0 and j2<5:
658 j2 = j1+2
659 for k2 in range(len(mergedClusters[j2*10+p])):
660 aCl2 = mergedClusters[j2*10+p][k2]
661 D = aCl2[0]-aCl1[0]
662 if debug: print(j1,j2,'x1',aCl1[0],'x2',aCl2[0],'D',D)
663 if abs(D ) > dMax: continue
664 h['doublet'][j1][k1].append(10*j2+k2)
665 h['trackCand'][p] = {}
666 for j1 in [1,2]:
667 for k1 in h['doublet'][j1]:
668 if j1==2:
669 alreadyUsed = False
670 for k0 in h['doublet'][1]:
671 for sk0 in h['doublet'][1][k0]:
672 if k1== (sk0%10) :
673 alreadyUsed = True
674 break
675 if alreadyUsed: continue
676 trackId = (k1+1)*10**(j1-1)
677 if debug: print(j1,k1,trackId)
678 for sk2 in h['doublet'][j1][k1]:
679 j2 = sk2//10
680 k2 = sk2%10
681 trackId = (k1+1)*10**(j1-1) + (k2+1)*10**(j2-1)
682 if debug: print(j2,k2,trackId)
683 for sk3 in h['doublet'][j2][k2]:
684 j3 = sk3//10
685 k3 = sk3%10
686 if j3>4: continue
687 trackId = (k1+1)*10**(j1-1) + (k2+1)*10**(j2-1) + (k3+1)*10**(j3-1)
688 if debug: print(j3,k3,trackId)
689 for sk4 in h['doublet'][j3][k3]:
690 j4 = sk4//10
691 k4 = sk4%10
692 trackId = (k1+1)*10**(j1-1) + (k2+1)*10**(j2-1) + (k3+1)*10**(j3-1) + (k4+1)*10**(j4-1)
693 if debug: print(j4,k4,trackId)
694 found = False
695 if j4<5:
696 for sk5 in h['doublet'][j4][k4]:
697 found = True
698 j5 = sk5//10
699 k5 = sk5%10
700 trackId = (k1+1)*10**(j1-1) + (k2+1)*10**(j2-1) + (k3+1)*10**(j3-1) + (k4+1)*10**(j4-1) + (k5+1)*10**(j5-1)
701 h['trackCand'][p][trackId] = ROOT.TGraph()
702 if not found:
703 h['trackCand'][p][trackId] = ROOT.TGraph()
704 for trackId in h['trackCand'][p]:
705 N = -1
706 for s in range(1,6):
707 k = (trackId//10**(s-1))%10-1
708 if not k<0:
709 N+=1
710 aCl = mergedClusters[s*10+p][k]
711 h['trackCand'][p][trackId].SetPoint(N,aCl[1],aCl[0])
712
713 else:
714 for k1 in range(len(mergedClusters[10+p])):
715 aCl1 = mergedClusters[10+p][k1]
716 for k2 in range(len(mergedClusters[20+p])):
717 aCl2 = mergedClusters[20+p][k2]
718 D = aCl2[0]-aCl1[0]
719 if debug: print(2,'x1',aCl1[0],'x2',aCl2[0],'D',D)
720 if abs(D) > dMax: continue
721 for k3 in range(len(mergedClusters[30+p])):
722 aCl3 = mergedClusters[30+p][k3]
723 D3 = 2*D+aCl1[0]-aCl3[0]
724 if debug: print(3,'x1',aCl1[0],'x2',aCl2[0],'x3',aCl3[0],'D',D,'D3',D3)
725 if abs(D3) > dMax3: continue
726 trackId = 1000+100*k3+10*k2+k1
727 trackId3 = trackId
728 h['trackCand'][p][trackId]=ROOT.TGraph()
729 h['trackCand'][p][trackId].SetPoint(0,aCl1[1],aCl1[0])
730 h['trackCand'][p][trackId].SetPoint(1,aCl2[1],aCl2[0])
731 h['trackCand'][p][trackId].SetPoint(2,aCl3[1],aCl3[0])
732 extr4 = h['trackCand'][p][trackId].Eval(aCl3[1]+self.dZ)
733 for k4 in range(len(mergedClusters[40+p])):
734 aCl4 = mergedClusters[40+p][k4]
735 D4 = aCl4[0]-extr4
736 if debug: print(4,'D4',D4,'x4',aCl4[0],'extr',extr4)
737 if abs(D4) > dMax3: continue
738 trackId = 10000+1000*k4+100*k3+10*k2+k1
739 trackId4 = trackId
740 if debug: print(4,trackId)
741 h['trackCand'][p][trackId] = h['trackCand'][p][trackId3].Clone()
742 h['trackCand'][p][trackId].SetPoint(3,aCl4[1],aCl4[0])
743 extr5 = h['trackCand'][p][trackId].Eval(aCl4[1]+self.dZ)
744 for k5 in range(len(mergedClusters[50+p])):
745 aCl5 = mergedClusters[50+p][k5]
746 D5 = aCl5[0]-extr5
747 if debug: print(5,'D5',D5,'x5',aCl5[0],'extr',extr5)
748 if abs(D5) > dMax3: continue
749 trackId = 100000+10000*k5+1000*k4+100*k3+10*k2+k1
750 h['trackCand'][p][trackId] = h['trackCand'][p][trackId4].Clone()
751 h['trackCand'][p][trackId].SetPoint(4,aCl5[1],aCl5[0])
752 if debug: print('final',trackId)
753 '''
754clonekiller
755if more than 2 clusters are shared by a track, take the track with better chi2
756 '''
757 h['cloneCand'] = {0:[],1:[]}
758 discarded = h['cloneCand']
759 for p in range(2):
760 keys = list(h['trackCand'][p])
761 for k1 in range( len(keys)-1):
762 if keys[k1] < 100000 and not doublet: continue
763 if keys[k1] in discarded[p]: continue
764 if doublet: test1 = str(keys[k1]).zfill(5)
765 else: test1 = str(keys[k1]%100000).zfill(5)
766 for k2 in range(k1+1,len(keys) ):
767 if keys[k2] < 100000 and not doublet: continue
768 if keys[k2] in discarded[p]: continue
769 if doublet: test2 = str(keys[k2]).zfill(5)
770 else: test2 = str(keys[k2]%100000).zfill(5)
771 ov = 0
772 for i in range(5):
773 if test1[i]==test2[i]: ov+=1
774 if ov>ovMax:
775 if doublet and test1.find('0')<0 and not test2.find('0')<0: discarded[p].append(keys[k2])
776 elif doublet and test2.find('0')<0 and not test1.find('0')<0: discarded[p].append(keys[k1])
777 else:
778 rc1 = h['trackCand'][p][keys[k1]].Fit('pol1','QS')
779 rc2 = h['trackCand'][p][keys[k2]].Fit('pol1','QS')
780 if rc1.Get().Chi2() < rc2.Get().Chi2(): discarded[p].append(keys[k2])
781 else: discarded[p].append(keys[k1])