CbmRoot
Loading...
Searching...
No Matches
LitTrackFinderNN.cxx
Go to the documentation of this file.
1/* Copyright (C) 2013-2016 GSI/JINR-LIT, Darmstadt/Dubna
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Andrey Lebedev [committer] */
4
11#include "LitTrackFinderNN.h"
12
13#include "LitAddMaterial.h"
14#include "LitExtrapolation.h"
15#include "LitFiltration.h"
16#include "LitMath.h"
17#include "LitTrackSelection.h"
18
19#include <algorithm>
20#include <iostream>
21#include <limits>
22#include <map>
23using std::cout;
24using std::endl;
25using std::for_each;
26using std::map;
27using std::numeric_limits;
28
30
32 : fTracks()
33 , fHitData()
34 , fUsedHitsSet()
35 , fUsedSeedsSet()
36 , fLayout()
37 , fNofIterations(0)
38 , fIteration(0)
39 , fMaxNofMissingHits()
40 , fPDG()
41 , fChiSqStripHitCut()
42 , fChiSqPixelHitCut()
43 , fSigmaCoef()
44{
45}
46
48
49void lit::parallel::LitTrackFinderNN::DoFind(const vector<lit::parallel::LitScalPixelHit*>& hits,
50 const vector<lit::parallel::LitScalTrack*>& trackSeeds,
51 vector<lit::parallel::LitScalTrack*>& tracks)
52{
53 fTracks.clear();
54 fUsedSeedsSet.clear();
55 fUsedHitsSet.clear();
56 fHitData.SetNofStations(fLayout.GetNofStations());
57
58 for (fIteration = 0; fIteration < fNofIterations; fIteration++) {
59 // cout << "LitTrackFinderNN::DoFind: iteration=" << fIteration << endl;
60 ArrangeHits(hits);
61 // cout << fHitData.ToString();
62
63 InitTrackSeeds(trackSeeds);
64 FollowTracks();
65 SelectTracks();
66 RemoveHits();
67 CopyToOutput(tracks);
68
69 for_each(fTracks.begin(), fTracks.end(), DeleteObject());
70 fTracks.clear();
71 fHitData.Clear();
72 }
73
74 static int eventNo = 0;
75 cout << "LitTrackFinderNN::DoFind: eventNo=" << eventNo++ << endl;
76}
77
78void lit::parallel::LitTrackFinderNN::ArrangeHits(const vector<lit::parallel::LitScalPixelHit*>& hits)
79{
80 unsigned int nofHits = hits.size();
81 for (unsigned int iHit = 0; iHit < nofHits; iHit++) {
82 LitScalPixelHit* hit = hits[iHit];
83 if (fUsedHitsSet.find(hit->refId) != fUsedHitsSet.end()) {
84 continue;
85 }
86 fHitData.AddHit(hit);
87 }
88 fHitData.Arrange();
89}
90
91void lit::parallel::LitTrackFinderNN::InitTrackSeeds(const vector<lit::parallel::LitScalTrack*>& trackSeeds)
92{
93 unsigned int nofSeeds = trackSeeds.size();
94 for (unsigned int iTrack = 0; iTrack < nofSeeds; iTrack++) {
95 LitScalTrack* seed = trackSeeds[iTrack];
96 // Apply cuts here
97 if (std::fabs(seed->GetParamFirst().Qp) > 10) continue; // momentum cut 0.1 GeV
98
99 //if (fUsedSeedsSet.find(seed->GetPreviousTrackId()) != fUsedSeedsSet.end()) { continue; }
100 LitScalTrack* track = new LitScalTrack();
101 // FIXME if several iterations this procedure will be repeated!!
102 LitTrackParamScal par = seed->GetParamFirst();
103 PropagateVirtualStations(par);
104 track->SetParamFirst(par);
105 track->SetParamLast(par);
107 fTracks.push_back(track);
108 }
109}
110
112{
113 unsigned char nofVirtualStations = fLayout.GetNofVirtualStations();
114 unsigned char nofSteps = (nofVirtualStations - 1) / 2;
115 for (unsigned char iStep = 0; iStep < nofSteps; iStep++) {
116 const LitVirtualStationScal& vsFront = fLayout.GetVirtualStation(2 * iStep + 0);
117 const LitVirtualStationScal& vsMiddle = fLayout.GetVirtualStation(2 * iStep + 1);
118 const LitVirtualStationScal& vsBack = fLayout.GetVirtualStation(2 * iStep + 2);
119 if (vsFront.GetField().IsEmpty() || vsMiddle.GetField().IsEmpty() || vsBack.GetField().IsEmpty()) {
120 LitLineExtrapolation(par, vsBack.GetZ());
121 }
122 else {
123 LitRK4Extrapolation(par, vsBack.GetZ(), vsFront.GetField(), vsMiddle.GetField(), vsBack.GetField());
124 }
125
126 if (!vsFront.GetMaterial().IsEmpty()) {
127 fscal thickness = vsFront.GetMaterial().GetMaterial(par.X, par.Y);
128 if (thickness > 0) LitAddMaterial<fscal>(par, thickness);
129 }
130
131 if (!vsMiddle.GetMaterial().IsEmpty()) {
132 fscal thickness = vsMiddle.GetMaterial().GetMaterial(par.X, par.Y);
133 if (thickness > 0) LitAddMaterial<fscal>(par, thickness);
134 }
135
136 if (!vsBack.GetMaterial().IsEmpty()) {
137 fscal thickness = vsBack.GetMaterial().GetMaterial(par.X, par.Y);
138 if (thickness > 0) LitAddMaterial<fscal>(par, thickness);
139 }
140 }
141}
142
144{
145 const LitStationScal& station = fLayout.GetStation(stationId);
146 unsigned char nofVirtualStations = station.GetNofVirtualStations();
147 if (nofVirtualStations == 1) {
148 const LitVirtualStationScal& vs = station.GetVirtualStation(0);
149 fscal z = vs.GetZ();
150 LitLineExtrapolation(par, z);
151 if (!vs.GetMaterial().IsEmpty()) {
152 fscal thickness = vs.GetMaterial().GetMaterial(par.X, par.Y);
153 if (thickness > 0) LitAddMaterial<fscal>(par, thickness);
154 }
155 }
156 else {
157 unsigned char nofSteps = (nofVirtualStations - 1) / 2;
158 for (unsigned char iStep = 0; iStep < nofSteps; iStep++) {
159 const LitVirtualStationScal& vsFront = station.GetVirtualStation(2 * iStep + 0);
160 const LitVirtualStationScal& vsMiddle = station.GetVirtualStation(2 * iStep + 1);
161 const LitVirtualStationScal& vsBack = station.GetVirtualStation(2 * iStep + 2);
162 if (vsFront.GetField().IsEmpty() || vsMiddle.GetField().IsEmpty() || vsBack.GetField().IsEmpty()) {
163 LitLineExtrapolation(par, vsBack.GetZ());
164 }
165 else {
166 LitRK4Extrapolation(par, vsBack.GetZ(), vsFront.GetField(), vsMiddle.GetField(), vsBack.GetField());
167 }
168
169 if (!vsFront.GetMaterial().IsEmpty()) {
170 fscal thickness = vsFront.GetMaterial().GetMaterial(par.X, par.Y);
171 if (thickness > 0) LitAddMaterial<fscal>(par, thickness);
172 }
173
174 if (!vsMiddle.GetMaterial().IsEmpty()) {
175 fscal thickness = vsMiddle.GetMaterial().GetMaterial(par.X, par.Y);
176 if (thickness > 0) LitAddMaterial<fscal>(par, thickness);
177 }
178
179 if (!vsBack.GetMaterial().IsEmpty()) {
180 fscal thickness = vsBack.GetMaterial().GetMaterial(par.X, par.Y);
181 if (thickness > 0) LitAddMaterial<fscal>(par, thickness);
182 }
183 }
184 }
185}
186
188{
189 unsigned int nofTracks = fTracks.size();
190 for (unsigned int iTrack = 0; iTrack < nofTracks; iTrack++) {
191 LitScalTrack* track = fTracks[iTrack];
192
193 unsigned char nofStations = fLayout.GetNofStations();
194 for (int iStation = 0; iStation < nofStations; iStation++) {
195 LitTrackParamScal par(track->GetParamLast());
196 // const LitStationScal& station = fLayout.GetStation(iStation);
197
198 fscal zMin = fHitData.GetMinZPos(iStation);
199 LitLineExtrapolation(par, zMin);
200
201 const vector<int>& bins = fHitData.GetZPosBins(iStation);
202 map<int, LitTrackParamScal> binParamMap;
203 vector<int>::const_iterator itBins;
204 for (itBins = bins.begin(); itBins != bins.end(); itBins++) {
205 binParamMap[*itBins] = LitTrackParamScal();
206 }
207
208 // Extrapolate track parameters to each Z position in the map.
209 // This is done to improve calculation speed.
210 // In case of planar station only 1 track extrapolation is required,
211 // since all hits located at the same Z.
212 map<int, LitTrackParamScal>::iterator itMap;
213 for (itMap = binParamMap.begin(); itMap != binParamMap.end(); itMap++) {
214 (*itMap).second = par;
215 // fscal z = fHitData.GetZPosByBin(iStation, (*itMap).first);
216 LitTrackParamScal& tpar = (*itMap).second;
217 PropagateToStation(iStation, tpar);
218 // LitLineExtrapolation(tpar, z);
219 // if (!station.GetVirtualStation().GetMaterial().IsEmpty()) {
220 // fscal thickness = station.GetVirtualStation().GetMaterial().GetMaterial(tpar.X, tpar.Y);
221 // if (thickness > 0) LitAddMaterial<fscal>(tpar, thickness);
222 // }
223 }
224
225 // Loop over hits
226 fscal minChiSq = numeric_limits<fscal>::max(); // minimum chi-square of hit
227 const LitScalPixelHit* minHit = NULL; // Pointer to hit with minimum chi-square
228 LitTrackParamScal minPar; // Track parameters for closest hit
229 const vector<LitScalPixelHit*>& hits = fHitData.GetHits(iStation);
230 unsigned int nofHits = hits.size();
231 for (unsigned int iHit = 0; iHit < nofHits; iHit++) {
232 const LitScalPixelHit* hit = hits[iHit];
233 int bin = fHitData.GetBinByZPos(iStation, hit->Z);
234 assert(binParamMap.find(bin) != binParamMap.end());
235 LitTrackParamScal tpar(binParamMap[bin]);
236
237 // Check preliminary if hit is in the validation gate.
238 // This is done in order to speed up the algorithm.
239 // Based on the predicted track position (w/o KF update step)
240 // and maximum hit position error.
241 fscal maxErrX = fHitData.GetMaxErrX(iStation);
242 fscal devX = fSigmaCoef[fIteration] * (sqrt(tpar.C0 + maxErrX * maxErrX));
243 fscal maxErrY = fHitData.GetMaxErrY(iStation);
244 fscal devY = fSigmaCoef[fIteration] * (sqrt(tpar.C5 + maxErrY * maxErrY));
245 bool hitInside = (hit->X < (tpar.X + devX)) && (hit->X > (tpar.X - devX)) && (hit->Y < (tpar.Y + devY))
246 && (hit->Y > (tpar.Y - devY));
247 if (!hitInside) continue;
248
249 fscal chi = numeric_limits<fscal>::max();
250 LitFiltration(tpar, *hit, chi);
251 bool hitInValidationGate = chi < fChiSqStripHitCut[fIteration];
252 if (hitInValidationGate && chi < minChiSq) { // Check if hit is inside validation gate and closer to the track.
253 minChiSq = chi;
254 minHit = hit;
255 minPar = tpar;
256 }
257 }
258
259 if (minHit != NULL) { // Check if hit was added
260 if (track->GetNofHits() == 0) track->SetParamFirst(minPar);
261 track->AddHit(minHit);
262 track->SetParamLast(minPar);
263 track->IncChiSq(minChiSq);
264 track->SetNDF(NDF(*track));
265 track->SetLastStationId(iStation);
266 }
267 else { // Missing hit
268 track->SetNofMissingHits(track->GetNofMissingHits() + 1);
269 if (track->GetNofMissingHits() > fMaxNofMissingHits[fIteration]) {
270 break;
271 }
272 }
273 }
274 }
275}
276
278{
279 unsigned int nofTracks = fTracks.size();
280 for (unsigned int iTrack = 0; iTrack < nofTracks; iTrack++) {
281 LitScalTrack* track = fTracks[iTrack];
282 track->IsGood(track->GetNofHits() > 0);
283 }
284 DoSelectSharedHits(fTracks);
285}
286
288{
289 unsigned int nofTracks = fTracks.size();
290 for (unsigned int iTrack = 0; iTrack < nofTracks; iTrack++) {
291 LitScalTrack* track = fTracks[iTrack];
292 if (!track->IsGood()) {
293 continue;
294 }
295 for (unsigned int iHit = 0; iHit < track->GetNofHits(); iHit++) {
296 fUsedHitsSet.insert(track->GetHit(iHit)->refId);
297 }
298 }
299}
300
301void lit::parallel::LitTrackFinderNN::CopyToOutput(vector<lit::parallel::LitScalTrack*>& tracks)
302{
303 unsigned int nofTracks = fTracks.size();
304 for (unsigned int iTrack = 0; iTrack < nofTracks; iTrack++) {
305 LitScalTrack* track = fTracks[iTrack];
306 if (!track->IsGood()) {
307 continue;
308 }
309 fUsedSeedsSet.insert(track->GetPreviousTrackId());
310 tracks.push_back(new LitScalTrack(*track));
311 }
312}
TClonesArray * tracks
static vector< vector< QAHit > > hits
friend fvec sqrt(const fvec &a)
Functions for calculation of the material effects.
Functions for track parameters extrapolation.
Useful math functions.
Parallel implementation of the nearest neighbor tracking algorithm.
Implementation of the track selection algorithms.
Functor class for convenient memory release.
Definition LitUtils.h:58
bool IsEmpty() const
Check if field was set.
static const fscal EPSILON
Definition LitHitData.h:211
bool IsEmpty() const
Check if material was set.
fscal GetMaterial(fscal x, fscal y) const
Return material thickness in silicon equivalent for (X, Y) position (scalar version).
Base class for scalar pixel hits.
Scalar track data class.
void IncChiSq(fscal dChiSq)
Increases chi square by dChiSq.
void SetPreviousTrackId(unsigned short previousTrackId)
Sets previous trackId.
const LitTrackParamScal & GetParamFirst() const
Returns first parameter of the track.
void SetNofMissingHits(unsigned short nofMissingHits)
Sets number of missing hits.
unsigned short GetPreviousTrackId() const
Return Previous track index.
void SetNDF(unsigned short NDF)
Sets number of degrees of freedom.
unsigned short GetNofHits() const
Returns number of hits in track.
const LitTrackParamScal & GetParamLast() const
Returns last parameter of the track.
unsigned short GetNofMissingHits() const
Returns number of missing hits.
bool IsGood() const
Returns true if track is good.
void SetParamLast(const LitTrackParamScal &param)
Sets last track parameter.
const LitScalPixelHit * GetHit(unsigned short index) const
Returns pointer to the hit.
void SetParamFirst(const LitTrackParamScal &param)
Sets first track parameter.
void AddHit(const LitScalPixelHit *hit)
Adds hit to track.
void SetLastStationId(unsigned short lastStationId)
Set last station ID.
Detector station.
Definition LitStation.h:38
const LitVirtualStation< T > & GetVirtualStation(unsigned char virtualStation) const
Return virtual station by index.
Definition LitStation.h:67
unsigned char GetNofVirtualStations() const
Return number of virtual stations.
Definition LitStation.h:60
virtual ~LitTrackFinderNN()
Destructor.
void CopyToOutput(vector< lit::parallel::LitScalTrack * > &tracks)
Copy tracks to output array.
void PropagateVirtualStations(LitTrackParamScal &par)
void ArrangeHits(const vector< lit::parallel::LitScalPixelHit * > &hits)
void RemoveHits()
Write already used hits to a used hits set.
void DoFind(const vector< lit::parallel::LitScalPixelHit * > &hits, const vector< lit::parallel::LitScalTrack * > &trackSeeds, vector< lit::parallel::LitScalTrack * > &tracks)
Main function for track reconstruction.
void InitTrackSeeds(const vector< lit::parallel::LitScalTrack * > &trackSeeds)
Initialize track seeds and copy to local array.
void FollowTracks()
Follow tracks through detector.
void PropagateToStation(unsigned char stationId, LitTrackParamScal &par)
Virtual detector station which stores information needed for track propagation.
const LitFieldGrid & GetField() const
const LitMaterialGrid & GetMaterial() const
LitTrackParam< fscal > LitTrackParamScal
Scalar version of LitTrackParam.
void LitRK4Extrapolation(LitTrackParam< T > &par, T zOut, const LitFieldGrid &field1, const LitFieldGrid &field2, const LitFieldGrid &field3)
void LitFiltration(LitTrackParam< T > &par, const LitPixelHit< T > &hit, T &chiSq)
Function implements Kalman filter update step for pixel hit.
void LitLineExtrapolation(LitTrackParam< T > &par, T zOut)
Line track extrapolation for the field free regions.
void DoSelectSharedHits(vector< LitScalTrack * > &tracks)
void LitAddMaterial(LitTrackParam< T > &par, T siliconThickness)
unsigned short NDF(const LitScalTrack &track)
Returns number of degrees of freedom for the track.
Definition LitMath.h:56