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