LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
PmaTrkCandidate.cxx
Go to the documentation of this file.
1 
18 
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 
34 {
35  if (fTrack) delete fTrack;
36  fTrack = trk;
37 }
38 // ------------------------------------------------------
39 
41 {
42  if (fTrack) delete fTrack;
43  fTrack = 0;
44 }
45 // ------------------------------------------------------
46 // ------------------------------------------------------
47 // ------------------------------------------------------
48 
50 {
51  for (size_t t = 0; t < fCandidates.size(); ++t)
52  if (fCandidates[t].Track() == candidate) return t;
53  return -1;
54 }
55 
57 {
58  int id = getCandidateIndex(candidate);
59  if (id >= 0)
60  return fCandidates[id].TreeId();
61  else
62  return -1;
63 }
64 
66 {
67  fParents.clear();
68 
69  size_t t = 0;
70  while (t < fCandidates.size()) {
71  if (fCandidates[t].IsValid()) {
72  fCandidates[t].SetParent(-1);
73  fCandidates[t].Daughters().clear();
74  t++;
75  }
76  else
77  fCandidates.erase(fCandidates.begin() + t);
78  }
79 
80  for (t = 0; t < fCandidates.size(); ++t) {
81  if (!fCandidates[t].IsValid()) continue;
82 
83  pma::Track3D const* trk = fCandidates[t].Track();
84  pma::Node3D const* firstNode = trk->Nodes().front();
85  if (firstNode->Prev()) // parent is a reconstructed track
86  {
87  pma::Track3D const* parentTrk = static_cast<pma::Segment3D*>(firstNode->Prev())->Parent();
88  fCandidates[t].SetParent(getCandidateIndex(parentTrk));
89  }
90  else if (fCandidates[t].Parent() < 0) // parent not reconstructed and not yet set, add empty
91  // candidate as a "primary" particle
92  {
93  fParents.push_back(pma::TrkCandidate());
94  fParents.back().SetTreeId(fCandidates[t].TreeId());
95  size_t pri_idx = fCandidates.size() + fParents.size() - 1;
96 
97  for (size_t i = 0; i < firstNode->NextCount(); ++i) {
98  pma::Track3D const* daughterTrk =
99  static_cast<pma::Segment3D*>(firstNode->Next(i))->Parent();
100  int idx = getCandidateIndex(daughterTrk);
101  if (idx >= 0) {
102  fCandidates[(size_t)idx].SetParent(pri_idx);
103  fParents.back().Daughters().push_back((size_t)idx);
104  }
105  }
106  }
107 
108  for (size_t n = 1; n < trk->Nodes().size(); ++n) {
109  auto node = trk->Nodes()[n];
110  for (size_t i = 0; i < node->NextCount(); ++i) {
111  pma::Track3D const* daughterTrk = static_cast<pma::Segment3D*>(node->Next(i))->Parent();
112  if (daughterTrk != trk) {
113  int idx = getCandidateIndex(daughterTrk);
114  if (idx >= 0) fCandidates[t].Daughters().push_back((size_t)idx);
115  }
116  }
117  }
118  }
119 }
120 // ------------------------------------------------------
121 
122 void pma::TrkCandidateColl::setTreeId(int id, size_t trkIdx, bool isRoot)
123 {
124  pma::Track3D* trk = fCandidates[trkIdx].Track();
125  pma::Node3D* vtx = trk->Nodes().front();
126  pma::Segment3D* segThis = 0;
127  pma::Segment3D* seg = 0;
128 
129  if (!isRoot) {
130  segThis = trk->NextSegment(vtx);
131  if (segThis) vtx = static_cast<pma::Node3D*>(segThis->Next());
132  }
133 
134  while (vtx) {
135  segThis = trk->NextSegment(vtx);
136 
137  for (size_t i = 0; i < vtx->NextCount(); i++) {
138  seg = static_cast<pma::Segment3D*>(vtx->Next(i));
139  if (seg != segThis) {
140  int idx = getCandidateIndex(seg->Parent());
141 
142  if (idx >= 0)
143  setTreeId(id, idx, false);
144  else
145  mf::LogError("pma::setTreeId") << "Branch of the tree not found in tracks collection.";
146  }
147  }
148 
149  if (segThis)
150  vtx = static_cast<pma::Node3D*>(segThis->Next());
151  else
152  break;
153  }
154 
155  fCandidates[trkIdx].SetTreeId(id);
156 }
157 
159 {
160  for (auto& t : fCandidates)
161  t.SetTreeId(-1);
162 
163  int id = 0;
164  for (auto& t : fCandidates) {
165  if (!t.IsValid() || (t.TreeId() >= 0)) continue;
166 
167  // index of a valid (reconstructed) track without reconstructed parent track
168  int rootTrkIdx = getCandidateIndex(t.Track()->GetRoot());
169 
170  if (rootTrkIdx >= 0)
171  setTreeId(id, rootTrkIdx);
172  else
173  mf::LogError("pma::setTreeIds") << "Root of the tree not found in tracks collection.";
174 
175  id++;
176  }
177 
178  return id;
179 }
180 // ------------------------------------------------------
181 
183  size_t coordinate)
184 {
185  std::map<int, std::vector<pma::Track3D*>> toFlip;
186  std::map<int, double> minVal;
187 
188  setTreeIds();
189  for (auto& t : fCandidates) {
190  if (!t.IsValid()) continue;
191 
192  int tid = t.TreeId();
193  if (minVal.find(tid) == minVal.end()) minVal[tid] = 1.0e12;
194 
195  TVector3 pFront(t.Track()->front()->Point3D());
196  pFront.SetX(-pFront.X());
197  pFront.SetY(-pFront.Y());
198  TVector3 pBack(t.Track()->back()->Point3D());
199  pBack.SetX(-pBack.X());
200  pBack.SetY(-pBack.Y());
201 
202  bool pushed = false;
203  if (pFront[coordinate] < minVal[tid]) {
204  minVal[tid] = pFront[coordinate];
205  toFlip[tid].push_back(t.Track());
206  pushed = true;
207  }
208  if (pBack[coordinate] < minVal[tid]) {
209  minVal[tid] = pBack[coordinate];
210  if (!pushed) toFlip[tid].push_back(t.Track());
211  }
212  }
213 
214  for (auto& tEntry : toFlip)
215  if (tEntry.first >= 0) {
216  size_t attempts = 0;
217  while (!tEntry.second.empty()) {
218  pma::Track3D* trk = tEntry.second.back();
219  tEntry.second.pop_back();
220 
221  TVector3 pFront(trk->front()->Point3D());
222  pFront.SetX(-pFront.X());
223  pFront.SetY(-pFront.Y());
224  TVector3 pBack(trk->back()->Point3D());
225  pBack.SetX(-pBack.X());
226  pBack.SetY(-pBack.Y());
227 
228  if (pFront[coordinate] > pBack[coordinate]) {
229  if (setTreeOriginAtBack(detProp, trk)) { break; }
230  else {
231  mf::LogWarning("pma::TrkCandidateColl") << "Flip to coordinate failed.";
232  }
233  }
234  else {
235  setTreeOriginAtFront(detProp, trk);
236  break; // good orientation, go to the next tree
237  }
238 
239  if (attempts++ > 2) break; // do not try all the tracks in the queue...
240  }
241  }
242 }
243 // ------------------------------------------------------
244 
246  pma::Track3D* trk)
247 {
248  int trkIdx = getCandidateIndex(trk);
249  int treeId = getCandidateTreeId(trk);
250  if (trkIdx < 0) {
251  throw cet::exception("pma::TrkCandidateColl")
252  << "Track not found in the collection." << std::endl;
253  }
254 
255  bool done = true;
256  pma::Node3D* n = trk->Nodes().front();
257  if (n->Prev()) {
258  pma::Segment3D* seg = static_cast<pma::Segment3D*>(n->Prev());
259  pma::Track3D* incoming = seg->Parent();
260  std::vector<pma::Track3D*> newTracks;
261  if (incoming->NextSegment(n)) // upfff, need to break the parent track
262  {
263  int idx = incoming->index_of(n);
264  if (idx >= 0) {
265  pma::Track3D* u = incoming->Split(detProp, idx, false); // u is in front of incoming
266  if (u) {
267  newTracks.push_back(u);
268  done = u->Flip(detProp, newTracks);
269  }
270  else {
271  done = false;
272  }
273  }
274  else {
275  throw cet::exception("pma::Track3D") << "Node not found." << std::endl;
276  }
277  }
278  else {
279  done = incoming->Flip(detProp, newTracks);
280  } // just flip incoming
281 
282  for (const auto ts : newTracks) {
283  fCandidates.emplace_back(ts, -1, treeId);
284  }
285  }
286  return done;
287 }
288 // ------------------------------------------------------
289 
291  pma::Track3D* trk)
292 {
293  int trkIdx = getCandidateIndex(trk);
294  int treeId = getCandidateTreeId(trk);
295  if (trkIdx < 0) {
296  throw cet::exception("pma::TrkCandidateColl")
297  << "Track not found in the collection." << std::endl;
298  }
299 
300  pma::Track3D* incoming = fCandidates[trkIdx].Track();
301  std::vector<pma::Track3D*> newTracks;
302  bool done = incoming->Flip(detProp, newTracks);
303  for (const auto ts : newTracks) {
304  fCandidates.emplace_back(ts, -1, treeId);
305  }
306  return done;
307 }
308 // ------------------------------------------------------
309 
311 {
312  std::map<int, std::vector<pma::Track3D*>> trkMap;
313 
314  setTreeIds();
315  for (auto const& t : fCandidates) {
316  if (t.IsValid()) trkMap[t.TreeId()].push_back(t.Track());
317  }
318 
319  for (auto& tEntry : trkMap) {
320  std::sort(tEntry.second.begin(), tEntry.second.end(), pma::bTrack3DLonger());
321  for (size_t i = tEntry.second.size(); i > 0; --i) {
322  pma::Track3D* trk = tEntry.second[i - 1];
323  if (trk->CanFlip()) { trk->AutoFlip(pma::Track3D::kForward, 0.15); }
324  }
325  }
326 }
327 // ------------------------------------------------------
328 
329 void pma::TrkCandidateColl::merge(size_t idx1, size_t idx2)
330 {
331  fCandidates[idx1].Track()->ExtendWith(fCandidates[idx2].Track()); // deletes track at idx2
332 
333  for (auto c : fCandidates[idx2].Clusters()) {
334  fCandidates[idx1].Clusters().push_back(c);
335  }
336 
337  fCandidates.erase(fCandidates.begin() + idx2);
338 
339  setTreeId(fCandidates[idx1].TreeId(), idx1);
340 }
341 
343  size_t trkIdx,
344  bool isRoot)
345 {
346  pma::Track3D* trk = fCandidates[trkIdx].Track();
347  pma::Node3D* vtx = trk->Nodes().front();
348  pma::Segment3D* segThis = 0;
349  pma::Segment3D* seg = 0;
350 
351  int key = fCandidates[trkIdx].Key();
352  int tid = fCandidates[trkIdx].TreeId();
353 
354  pma::Track3D* trkCopy = new pma::Track3D(*trk);
355  pma::Node3D* vtxCopy = trkCopy->Nodes().front();
356  pma::Segment3D* segThisCopy = 0;
357 
358  dst.tracks().emplace_back(trkCopy, key, tid);
359 
360  if (!isRoot) {
361  segThis = trk->NextSegment(vtx);
362  if (segThis) vtx = static_cast<pma::Node3D*>(segThis->Next());
363 
364  segThisCopy = trkCopy->NextSegment(vtxCopy);
365  if (segThisCopy) vtxCopy = static_cast<pma::Node3D*>(segThisCopy->Next());
366  }
367 
368  while (vtx) {
369  segThis = trk->NextSegment(vtx);
370  segThisCopy = trkCopy->NextSegment(vtxCopy);
371 
372  for (size_t i = 0; i < vtx->NextCount(); i++) {
373  seg = static_cast<pma::Segment3D*>(vtx->Next(i));
374  if (seg != segThis) {
375  int idx = getCandidateIndex(seg->Parent());
376 
377  if (idx >= 0) {
378  pma::Track3D* branchCopy = getTreeCopy(dst, idx, false);
379  if (!branchCopy->AttachTo(vtxCopy, true)) // no flip
380  mf::LogError("pma::getTreeCopy") << "Branch copy cannot be attached to the tree.";
381  }
382  else
383  mf::LogError("pma::getTreeCopy") << "Branch of the tree not found in source collection.";
384  }
385  }
386 
387  if (segThis)
388  vtx = static_cast<pma::Node3D*>(segThis->Next());
389  else
390  break;
391 
392  if (segThisCopy) vtxCopy = static_cast<pma::Node3D*>(segThisCopy->Next());
393  }
394 
395  return trkCopy;
396 }
void AutoFlip(pma::Track3D::EDirection dir, double thr=0.0, unsigned int n=0)
Definition: PmaTrack3D.cxx:662
bool IsValid() const
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:87
Implementation of the Projection Matching Algorithm.
int index_of(const pma::Hit3D *hit) const
Definition: PmaTrack3D.cxx:341
virtual unsigned int NextCount(void) const
Definition: SortedObjects.h:84
TVector3 const & Point3D() const
Definition: PmaHit3D.h:50
Implementation of the Projection Matching Algorithm.
bool setTreeOriginAtBack(detinfo::DetectorPropertiesData const &detProp, pma::Track3D *trk)
virtual pma::SortedObjectBase * Next(unsigned int=0) const
Definition: SortedObjects.h:43
void SetTrack(pma::Track3D *trk)
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
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:644
bool setTreeOriginAtFront(detinfo::DetectorPropertiesData const &detProp, pma::Track3D *trk)
int TreeId() const
int Parent() const
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.
Implementation of the Projection Matching Algorithm.
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
Definition: SortedObjects.h:77
void SetParent(int idx)
pma::Track3D * fTrack
const std::vector< size_t > & Clusters() const
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
pma::Hit3D const * back() const
Definition: PmaTrack3D.h:88
Char_t n[5]
std::vector< pma::Node3D * > const & Nodes() const noexcept
Definition: PmaTrack3D.h:274
virtual pma::SortedObjectBase * Prev(void) const
Definition: SortedObjects.h:42
bool Flip(const detinfo::DetectorPropertiesData &detProp, std::vector< pma::Track3D * > &allTracks)
Definition: PmaTrack3D.cxx:527
pma::Track3D * Track() const
pma::Track3D * Parent(void) const
Definition: PmaSegment3D.h:66
void setTreeId(int id, size_t trkIdx, bool isRoot=true)
pma::Segment3D * NextSegment(pma::Node3D *vtx) const
std::vector< TrkCandidate > const & tracks() const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33