818 def findTracks(self):
819 hitPosLists = {}
820 stationCrossed = {}
821 fittedtrackids=[]
822 listOfIndices = {}
823 self.fGenFitArray.Clear()
824 self.fTrackletsArray.Delete()
825 self.fitTrack2MC.clear()
826
827
828 if global_variables.withT0:
829 self.SmearedHits = self.withT0Estimate()
830
831 else:
832 self.SmearedHits = self.smearHits(global_variables.withNoStrawSmearing)
833
834 nTrack = -1
835 trackCandidates = []
836
837 if global_variables.realPR:
838
839 track_hits =
shipPatRec.execute(self.SmearedHits, global_variables.ShipGeo, global_variables.realPR)
840
841 for i_track in track_hits.keys():
842 atrack = track_hits[i_track]
843 atrack_y12 = atrack['y12']
844 atrack_stereo12 = atrack['stereo12']
845 atrack_y34 = atrack['y34']
846 atrack_stereo34 = atrack['stereo34']
847 atrack_smeared_hits = list(atrack_y12) + list(atrack_stereo12) + list(atrack_y34) + list(atrack_stereo34)
848 for sm in atrack_smeared_hits:
849 detID = sm['detID']
850 station = int(detID//10000000)
851 trID = i_track
852
853 if trID not in hitPosLists:
854 hitPosLists[trID] = ROOT.std.vector('TVectorD')()
855 listOfIndices[trID] = []
856 stationCrossed[trID] = {}
857 m = array('d',[sm['xtop'],sm['ytop'],sm['z'],sm['xbot'],sm['ybot'],sm['z'],sm['dist']])
858 hitPosLists[trID].push_back(ROOT.TVectorD(7,m))
859 listOfIndices[trID].append(sm['digiHit'])
860 if station not in stationCrossed[trID]:
861 stationCrossed[trID][station] = 0
862 stationCrossed[trID][station] += 1
863 else:
864 for sm in self.SmearedHits:
865 detID = self.digiStraw[sm['digiHit']].GetDetectorID()
866 station = int(detID//10000000)
867 trID = self.sTree.strawtubesPoint[sm['digiHit']].GetTrackID()
868 if trID not in hitPosLists:
869 hitPosLists[trID] = ROOT.std.vector('TVectorD')()
870 listOfIndices[trID] = []
871 stationCrossed[trID] = {}
872 m = array('d',[sm['xtop'],sm['ytop'],sm['z'],sm['xbot'],sm['ybot'],sm['z'],sm['dist']])
873 hitPosLists[trID].push_back(ROOT.TVectorD(7,m))
874 listOfIndices[trID].append(sm['digiHit'])
875 if station not in stationCrossed[trID]: stationCrossed[trID][station]=0
876 stationCrossed[trID][station]+=1
877
878
879
880
881
882
883
884
885
886
887 for atrack in hitPosLists:
888 if atrack < 0: continue
889 pdg = self.sTree.MCTrack[atrack].GetPdgCode()
890 if not self.PDG.GetParticle(pdg): continue
891
892 meas = hitPosLists[atrack]
893 nM = meas.size()
894 if nM < 25 : continue
895 if len(stationCrossed[atrack]) < 3 : continue
896 if global_variables.debug:
897 mctrack = self.sTree.MCTrack[atrack]
898
899 posM = ROOT.TVector3(0, 0, 0)
900 momM = ROOT.TVector3(0,0,3.*u.GeV)
901
902 covM = ROOT.TMatrixDSym(6)
903 resolution = self.sigma_spatial
904 if global_variables.withT0:
905 resolution *= 1.4
906 for i in range(3): covM[i][i] = resolution*resolution
907 covM[0][0]=resolution*resolution*100.
908 for i in range(3,6): covM[i][i] = ROOT.TMath.Power(resolution / nM / ROOT.TMath.Sqrt(3), 2)
909
910 rep = ROOT.genfit.RKTrackRep(pdg)
911
912 stateSmeared = ROOT.genfit.MeasuredStateOnPlane(rep)
913 rep.setPosMomCov(stateSmeared, posM, momM, covM)
914
915 seedState = ROOT.TVectorD(6)
916 seedCov = ROOT.TMatrixDSym(6)
917 rep.get6DStateCov(stateSmeared, seedState, seedCov)
918 theTrack = ROOT.genfit.Track(rep, seedState, seedCov)
919 hitCov = ROOT.TMatrixDSym(7)
920 hitCov[6][6] = resolution*resolution
921 for m in meas:
922 tp = ROOT.genfit.TrackPoint(theTrack)
923 measurement = ROOT.genfit.WireMeasurement(m,hitCov,1,6,tp)
924
925 measurement.setMaxDistance(global_variables.ShipGeo.strawtubes.InnerStrawDiameter / 2.)
926
927 tp.addRawMeasurement(measurement)
928 theTrack.insertPoint(tp)
929
930 trackCandidates.append([theTrack,atrack])
931
932 for entry in trackCandidates:
933
934 atrack = entry[1]
935 theTrack = entry[0]
936 if not theTrack.checkConsistency():
937 print('Problem with track before fit, not consistent',atrack,theTrack)
938 continue
939
940 try: self.fitter.processTrack(theTrack)
941 except:
942 if global_variables.debug:
943 print("genfit failed to fit track")
944 error = "genfit failed to fit track"
945 ut.reportError(error)
946 continue
947
948 if not theTrack.checkConsistency():
949 if global_variables.debug:
950 print('Problem with track after fit, not consistent', atrack, theTrack)
951 error = "Problem with track after fit, not consistent"
952 ut.reportError(error)
953 continue
954 try:
955 fittedState = theTrack.getFittedState()
956 fittedMom = fittedState.getMomMag()
957 except:
958 error = "Problem with fittedstate"
959 ut.reportError(error)
960 continue
961 fitStatus = theTrack.getFitStatus()
962 nmeas = fitStatus.getNdf()
963 chi2 = fitStatus.getChi2()/nmeas
964 global_variables.h['chi2'].Fill(chi2)
965
966 nTrack = self.fGenFitArray.GetEntries()
967 if not global_variables.debug:
968 theTrack.prune("CFL")
969 self.fGenFitArray[nTrack] = theTrack
970
971 if global_variables.debug:
972 print('save track',theTrack,chi2,nmeas,fitStatus.isFitConverged())
973
974 track_ids = []
975 for index in listOfIndices[atrack]:
976 ahit = self.sTree.strawtubesPoint[index]
977 track_ids += [ahit.GetTrackID()]
978 frac, tmax = self.fracMCsame(track_ids)
979 self.fitTrack2MC.push_back(tmax)
980
981 nTracks = self.fTrackletsArray.GetEntries()
982 aTracklet = self.fTrackletsArray.ConstructedAt(nTracks)
983 listOfHits = aTracklet.getList()
984 aTracklet.setType(1)
985 for index in listOfIndices[atrack]:
986 listOfHits.push_back(index)
987 self.Tracklets.Fill()
988 self.fitTracks.Fill()
989 self.mcLink.Fill()
990
991 if global_variables.debug:
992 print('save tracklets:')
993 for x in self.sTree.Tracklets:
994 print(x.getType(),x.getList().size())
995 return nTrack+1
996
execute(smeared_hits, ship_geo, method='')