All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PmaTrkCandidate.cxx
Go to the documentation of this file.
1 /**
2  * @file PmaTrkCandidate.cxx
3  *
4  * @author D.Stefan and R.Sulej
5  *
6  * @brief Track finding helper for the Projection Matching Algorithm
7  *
8  * Candidate for 3D track. Used to test 2D cluster associations, validadion result, MSE value.
9  * See PmaTrack3D.h file for details.
10  */
11 
18 
19 #include "messagefacility/MessageLogger/MessageLogger.h"
20 
21 #include "TVector3.h"
22 
24  : fParent(-1), fTrack(0), fKey(-1), fTreeId(-1), fMse(0), fValidation(0), fGood(false)
25 {}
26 // ------------------------------------------------------
27 
29  : fParent(-1), fTrack(trk), fKey(key), fTreeId(tid), fMse(0), fValidation(0), fGood(false)
30 {}
31 // ------------------------------------------------------
32 
33 void
35 {
36  if (fTrack) delete fTrack;
37  fTrack = trk;
38 }
39 // ------------------------------------------------------
40 
41 void
43 {
44  if (fTrack) delete fTrack;
45  fTrack = 0;
46 }
47 // ------------------------------------------------------
48 // ------------------------------------------------------
49 // ------------------------------------------------------
50 
51 int
53 {
54  for (size_t t = 0; t < fCandidates.size(); ++t)
55  if (fCandidates[t].Track() == candidate) return t;
56  return -1;
57 }
58 
59 int
61 {
62  int id = getCandidateIndex(candidate);
63  if (id >= 0)
64  return fCandidates[id].TreeId();
65  else
66  return -1;
67 }
68 
69 void
71 {
72  fParents.clear();
73 
74  size_t t = 0;
75  while (t < fCandidates.size()) {
76  if (fCandidates[t].IsValid()) {
77  fCandidates[t].SetParent(-1);
78  fCandidates[t].Daughters().clear();
79  t++;
80  }
81  else
82  fCandidates.erase(fCandidates.begin() + t);
83  }
84 
85  for (t = 0; t < fCandidates.size(); ++t) {
86  if (!fCandidates[t].IsValid()) continue;
87 
88  pma::Track3D const* trk = fCandidates[t].Track();
89  pma::Node3D const* firstNode = trk->Nodes().front();
90  if (firstNode->Prev()) // parent is a reconstructed track
91  {
92  pma::Track3D const* parentTrk = static_cast<pma::Segment3D*>(firstNode->Prev())->Parent();
93  fCandidates[t].SetParent(getCandidateIndex(parentTrk));
94  }
95  else if (fCandidates[t].Parent() < 0) // parent not reconstructed and not yet set, add empty
96  // candidate as a "primary" particle
97  {
98  fParents.push_back(pma::TrkCandidate());
99  fParents.back().SetTreeId(fCandidates[t].TreeId());
100  size_t pri_idx = fCandidates.size() + fParents.size() - 1;
101 
102  for (size_t i = 0; i < firstNode->NextCount(); ++i) {
103  pma::Track3D const* daughterTrk =
104  static_cast<pma::Segment3D*>(firstNode->Next(i))->Parent();
105  int idx = getCandidateIndex(daughterTrk);
106  if (idx >= 0) {
107  fCandidates[(size_t)idx].SetParent(pri_idx);
108  fParents.back().Daughters().push_back((size_t)idx);
109  }
110  }
111  }
112 
113  for (size_t n = 1; n < trk->Nodes().size(); ++n) {
114  auto node = trk->Nodes()[n];
115  for (size_t i = 0; i < node->NextCount(); ++i) {
116  pma::Track3D const* daughterTrk = static_cast<pma::Segment3D*>(node->Next(i))->Parent();
117  if (daughterTrk != trk) {
118  int idx = getCandidateIndex(daughterTrk);
119  if (idx >= 0) fCandidates[t].Daughters().push_back((size_t)idx);
120  }
121  }
122  }
123  }
124 }
125 // ------------------------------------------------------
126 
127 void
128 pma::TrkCandidateColl::setTreeId(int id, size_t trkIdx, bool isRoot)
129 {
130  pma::Track3D* trk = fCandidates[trkIdx].Track();
131  pma::Node3D* vtx = trk->Nodes().front();
132  pma::Segment3D* segThis = 0;
133  pma::Segment3D* seg = 0;
134 
135  if (!isRoot) {
136  segThis = trk->NextSegment(vtx);
137  if (segThis) vtx = static_cast<pma::Node3D*>(segThis->Next());
138  }
139 
140  while (vtx) {
141  segThis = trk->NextSegment(vtx);
142 
143  for (size_t i = 0; i < vtx->NextCount(); i++) {
144  seg = static_cast<pma::Segment3D*>(vtx->Next(i));
145  if (seg != segThis) {
146  int idx = getCandidateIndex(seg->Parent());
147 
148  if (idx >= 0)
149  setTreeId(id, idx, false);
150  else
151  mf::LogError("pma::setTreeId") << "Branch of the tree not found in tracks collection.";
152  }
153  }
154 
155  if (segThis)
156  vtx = static_cast<pma::Node3D*>(segThis->Next());
157  else
158  break;
159  }
160 
161  fCandidates[trkIdx].SetTreeId(id);
162 }
163 
164 int
166 {
167  for (auto& t : fCandidates)
168  t.SetTreeId(-1);
169 
170  int id = 0;
171  for (auto& t : fCandidates) {
172  if (!t.IsValid() || (t.TreeId() >= 0)) continue;
173 
174  // index of a valid (reconstructed) track without reconstructed parent track
175  int rootTrkIdx = getCandidateIndex(t.Track()->GetRoot());
176 
177  if (rootTrkIdx >= 0)
178  setTreeId(id, rootTrkIdx);
179  else
180  mf::LogError("pma::setTreeIds") << "Root of the tree not found in tracks collection.";
181 
182  id++;
183  }
184 
185  return id;
186 }
187 // ------------------------------------------------------
188 
189 void
191  size_t coordinate)
192 {
193  std::map<int, std::vector<pma::Track3D*>> toFlip;
194  std::map<int, double> minVal;
195 
196  setTreeIds();
197  for (auto& t : fCandidates) {
198  if (!t.IsValid()) continue;
199 
200  int tid = t.TreeId();
201  if (minVal.find(tid) == minVal.end()) minVal[tid] = 1.0e12;
202 
203  TVector3 pFront(t.Track()->front()->Point3D());
204  pFront.SetX(-pFront.X());
205  pFront.SetY(-pFront.Y());
206  TVector3 pBack(t.Track()->back()->Point3D());
207  pBack.SetX(-pBack.X());
208  pBack.SetY(-pBack.Y());
209 
210  bool pushed = false;
211  if (pFront[coordinate] < minVal[tid]) {
212  minVal[tid] = pFront[coordinate];
213  toFlip[tid].push_back(t.Track());
214  pushed = true;
215  }
216  if (pBack[coordinate] < minVal[tid]) {
217  minVal[tid] = pBack[coordinate];
218  if (!pushed) toFlip[tid].push_back(t.Track());
219  }
220  }
221 
222  for (auto& tEntry : toFlip)
223  if (tEntry.first >= 0) {
224  size_t attempts = 0;
225  while (!tEntry.second.empty()) {
226  pma::Track3D* trk = tEntry.second.back();
227  tEntry.second.pop_back();
228 
229  TVector3 pFront(trk->front()->Point3D());
230  pFront.SetX(-pFront.X());
231  pFront.SetY(-pFront.Y());
232  TVector3 pBack(trk->back()->Point3D());
233  pBack.SetX(-pBack.X());
234  pBack.SetY(-pBack.Y());
235 
236  if (pFront[coordinate] > pBack[coordinate]) {
237  if (setTreeOriginAtBack(detProp, trk)) { break; }
238  else {
239  mf::LogWarning("pma::TrkCandidateColl") << "Flip to coordinate failed.";
240  }
241  }
242  else {
243  setTreeOriginAtFront(detProp, trk);
244  break; // good orientation, go to the next tree
245  }
246 
247  if (attempts++ > 2) break; // do not try all the tracks in the queue...
248  }
249  }
250 }
251 // ------------------------------------------------------
252 
253 bool
255  pma::Track3D* trk)
256 {
257  int trkIdx = getCandidateIndex(trk);
258  int treeId = getCandidateTreeId(trk);
259  if (trkIdx < 0) {
260  throw cet::exception("pma::TrkCandidateColl")
261  << "Track not found in the collection." << std::endl;
262  }
263 
264  bool done = true;
265  pma::Node3D* n = trk->Nodes().front();
266  if (n->Prev()) {
267  pma::Segment3D* seg = static_cast<pma::Segment3D*>(n->Prev());
268  pma::Track3D* incoming = seg->Parent();
269  std::vector<pma::Track3D*> newTracks;
270  if (incoming->NextSegment(n)) // upfff, need to break the parent track
271  {
272  int idx = incoming->index_of(n);
273  if (idx >= 0) {
274  pma::Track3D* u = incoming->Split(detProp, idx, false); // u is in front of incoming
275  if (u) {
276  newTracks.push_back(u);
277  done = u->Flip(detProp, newTracks);
278  }
279  else {
280  done = false;
281  }
282  }
283  else {
284  throw cet::exception("pma::Track3D") << "Node not found." << std::endl;
285  }
286  }
287  else {
288  done = incoming->Flip(detProp, newTracks);
289  } // just flip incoming
290 
291  for (const auto ts : newTracks) {
292  fCandidates.emplace_back(ts, -1, treeId);
293  }
294  }
295  return done;
296 }
297 // ------------------------------------------------------
298 
299 bool
301  pma::Track3D* trk)
302 {
303  int trkIdx = getCandidateIndex(trk);
304  int treeId = getCandidateTreeId(trk);
305  if (trkIdx < 0) {
306  throw cet::exception("pma::TrkCandidateColl")
307  << "Track not found in the collection." << std::endl;
308  }
309 
310  pma::Track3D* incoming = fCandidates[trkIdx].Track();
311  std::vector<pma::Track3D*> newTracks;
312  bool done = incoming->Flip(detProp, newTracks);
313  for (const auto ts : newTracks) {
314  fCandidates.emplace_back(ts, -1, treeId);
315  }
316  return done;
317 }
318 // ------------------------------------------------------
319 
320 void
322 {
323  std::map<int, std::vector<pma::Track3D*>> trkMap;
324 
325  setTreeIds();
326  for (auto const& t : fCandidates) {
327  if (t.IsValid()) trkMap[t.TreeId()].push_back(t.Track());
328  }
329 
330  for (auto& tEntry : trkMap) {
331  std::sort(tEntry.second.begin(), tEntry.second.end(), pma::bTrack3DLonger());
332  for (size_t i = tEntry.second.size(); i > 0; --i) {
333  pma::Track3D* trk = tEntry.second[i - 1];
334  if (trk->CanFlip()) { trk->AutoFlip(pma::Track3D::kForward, 0.15); }
335  }
336  }
337 }
338 // ------------------------------------------------------
339 
340 void
341 pma::TrkCandidateColl::merge(size_t idx1, size_t idx2)
342 {
343  fCandidates[idx1].Track()->ExtendWith(fCandidates[idx2].Track()); // deletes track at idx2
344 
345  for (auto c : fCandidates[idx2].Clusters()) {
346  fCandidates[idx1].Clusters().push_back(c);
347  }
348 
349  fCandidates.erase(fCandidates.begin() + idx2);
350 
351  setTreeId(fCandidates[idx1].TreeId(), idx1);
352 }
353 
356 {
357  pma::Track3D* trk = fCandidates[trkIdx].Track();
358  pma::Node3D* vtx = trk->Nodes().front();
359  pma::Segment3D* segThis = 0;
360  pma::Segment3D* seg = 0;
361 
362  int key = fCandidates[trkIdx].Key();
363  int tid = fCandidates[trkIdx].TreeId();
364 
365  pma::Track3D* trkCopy = new pma::Track3D(*trk);
366  pma::Node3D* vtxCopy = trkCopy->Nodes().front();
367  pma::Segment3D* segThisCopy = 0;
368 
369  dst.tracks().emplace_back(trkCopy, key, tid);
370 
371  if (!isRoot) {
372  segThis = trk->NextSegment(vtx);
373  if (segThis) vtx = static_cast<pma::Node3D*>(segThis->Next());
374 
375  segThisCopy = trkCopy->NextSegment(vtxCopy);
376  if (segThisCopy) vtxCopy = static_cast<pma::Node3D*>(segThisCopy->Next());
377  }
378 
379  while (vtx) {
380  segThis = trk->NextSegment(vtx);
381  segThisCopy = trkCopy->NextSegment(vtxCopy);
382 
383  for (size_t i = 0; i < vtx->NextCount(); i++) {
384  seg = static_cast<pma::Segment3D*>(vtx->Next(i));
385  if (seg != segThis) {
386  int idx = getCandidateIndex(seg->Parent());
387 
388  if (idx >= 0) {
389  pma::Track3D* branchCopy = getTreeCopy(dst, idx, false);
390  if (!branchCopy->AttachTo(vtxCopy, true)) // no flip
391  mf::LogError("pma::getTreeCopy") << "Branch copy cannot be attached to the tree.";
392  }
393  else
394  mf::LogError("pma::getTreeCopy") << "Branch of the tree not found in source collection.";
395  }
396  }
397 
398  if (segThis)
399  vtx = static_cast<pma::Node3D*>(segThis->Next());
400  else
401  break;
402 
403  if (segThisCopy) vtxCopy = static_cast<pma::Node3D*>(segThisCopy->Next());
404  }
405 
406  return trkCopy;
407 }
void AutoFlip(pma::Track3D::EDirection dir, double thr=0.0, unsigned int n=0)
Definition: PmaTrack3D.cxx:673
pma::Track3D * getTreeCopy(pma::TrkCandidateColl &dst, size_t trkIdx, bool isRoot=true)
bool AttachTo(pma::Node3D *vStart, bool noFlip=false)
int getCandidateTreeId(pma::Track3D const *candidate) const
Implementation of the Projection Matching Algorithm.
pma::Hit3D const * front() const
Definition: PmaTrack3D.h:101
Implementation of the Projection Matching Algorithm.
int index_of(const pma::Hit3D *hit) const
Definition: PmaTrack3D.cxx:336
virtual unsigned int NextCount(void) const
Definition: SortedObjects.h:84
std::vector< pma::Node3D * > const & Nodes() const noexcept
Definition: PmaTrack3D.h:338
TVector3 const & Point3D() const
Definition: PmaHit3D.h:55
Implementation of the Projection Matching Algorithm.
bool setTreeOriginAtBack(detinfo::DetectorPropertiesData const &detProp, pma::Track3D *trk)
void SetTrack(pma::Track3D *trk)
void flipTreesToCoordinate(detinfo::DetectorPropertiesData const &detProp, size_t coordinate)
pma::Track3D * Split(detinfo::DetectorPropertiesData const &detProp, size_t idx, bool try_start_at_idx=true)
bool CanFlip() const
Check if the track can be flipped without breaking any other track.
Definition: PmaTrack3D.cxx:654
bool setTreeOriginAtFront(detinfo::DetectorPropertiesData const &detProp, pma::Track3D *trk)
int getCandidateIndex(pma::Track3D const *candidate) const
Implementation of the Projection Matching Algorithm.
void merge(size_t idx1, size_t idx2)
Track finding helper for the Projection Matching Algorithm.
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
Definition: SortedObjects.h:79
pma::Hit3D const * back() const
Definition: PmaTrack3D.h:106
TrackCollectionProxyElement< TrackCollProxy > Track
Proxy to an element of a proxy collection of recob::Track objects.
virtual pma::SortedObjectBase * Prev(void) const
Definition: SortedObjects.h:44
bool Flip(const detinfo::DetectorPropertiesData &detProp, std::vector< pma::Track3D * > &allTracks)
Definition: PmaTrack3D.cxx:534
pma::Track3D * Parent(void) const
Definition: PmaSegment3D.h:68
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
Definition: SortedObjects.h:45
bool SetParent(detinfo::DetectorPropertiesData const &detProp, std::string inFcnLabel, TCSlice &slc, PFPStruct &pfp, ShowerStruct3D &ss3, bool prt)
Definition: TCShower.cxx:1832
void setTreeId(int id, size_t trkIdx, bool isRoot=true)
pma::Segment3D * NextSegment(pma::Node3D *vtx) const
auto const detProp
std::vector< TrkCandidate > const & tracks() const