All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Attributes | List of all members
pma::TrkCandidateColl Class Reference

#include <PmaTrkCandidate.h>

Public Member Functions

size_t size () const
 
void resize (size_t n)
 
bool empty () const
 
void push_back (const TrkCandidate &trk)
 
void erase_at (size_t pos)
 
void clear ()
 
TrkCandidateoperator[] (size_t i)
 
TrkCandidate const & operator[] (size_t i) const
 
TrkCandidatefront ()
 
TrkCandidate const & front () const
 
TrkCandidateback ()
 
TrkCandidate const & back () const
 
std::vector< TrkCandidate > const & tracks () const
 
std::vector< TrkCandidate > & tracks ()
 
std::vector< TrkCandidate > const & parents () const
 
int getCandidateIndex (pma::Track3D const *candidate) const
 
int getCandidateTreeId (pma::Track3D const *candidate) const
 
void merge (size_t idx1, size_t idx2)
 
void setParentDaughterConnections ()
 
void setTreeId (int id, size_t trkIdx, bool isRoot=true)
 
int setTreeIds ()
 
void flipTreesToCoordinate (detinfo::DetectorPropertiesData const &detProp, size_t coordinate)
 
void flipTreesByDQdx ()
 
bool setTreeOriginAtFront (detinfo::DetectorPropertiesData const &detProp, pma::Track3D *trk)
 
bool setTreeOriginAtBack (detinfo::DetectorPropertiesData const &detProp, pma::Track3D *trk)
 
pma::Track3DgetTreeCopy (pma::TrkCandidateColl &dst, size_t trkIdx, bool isRoot=true)
 

Private Attributes

std::vector< TrkCandidatefCandidates
 
std::vector< TrkCandidatefParents
 

Detailed Description

Definition at line 158 of file PmaTrkCandidate.h.

Member Function Documentation

TrkCandidate& pma::TrkCandidateColl::back ( )
inline

Definition at line 207 of file PmaTrkCandidate.h.

208  {
209  return fCandidates.back();
210  }
std::vector< TrkCandidate > fCandidates
TrkCandidate const& pma::TrkCandidateColl::back ( ) const
inline

Definition at line 212 of file PmaTrkCandidate.h.

213  {
214  return fCandidates.back();
215  }
std::vector< TrkCandidate > fCandidates
void pma::TrkCandidateColl::clear ( )
inline

Definition at line 187 of file PmaTrkCandidate.h.

188  {
189  fCandidates.clear();
190  }
std::vector< TrkCandidate > fCandidates
bool pma::TrkCandidateColl::empty ( ) const
inline

Definition at line 171 of file PmaTrkCandidate.h.

172  {
173  return fCandidates.empty();
174  }
std::vector< TrkCandidate > fCandidates
void pma::TrkCandidateColl::erase_at ( size_t  pos)
inline

Definition at line 182 of file PmaTrkCandidate.h.

183  {
184  fCandidates.erase(fCandidates.begin() + pos);
185  }
std::vector< TrkCandidate > fCandidates
void pma::TrkCandidateColl::flipTreesByDQdx ( )

Definition at line 321 of file PmaTrkCandidate.cxx.

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 }
void AutoFlip(pma::Track3D::EDirection dir, double thr=0.0, unsigned int n=0)
Definition: PmaTrack3D.cxx:673
std::vector< TrkCandidate > fCandidates
bool CanFlip() const
Check if the track can be flipped without breaking any other track.
Definition: PmaTrack3D.cxx:654
void pma::TrkCandidateColl::flipTreesToCoordinate ( detinfo::DetectorPropertiesData const &  detProp,
size_t  coordinate 
)

Definition at line 190 of file PmaTrkCandidate.cxx.

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 {
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 }
std::vector< TrkCandidate > fCandidates
pma::Hit3D const * front() const
Definition: PmaTrack3D.h:101
TVector3 const & Point3D() const
Definition: PmaHit3D.h:55
bool setTreeOriginAtBack(detinfo::DetectorPropertiesData const &detProp, pma::Track3D *trk)
bool setTreeOriginAtFront(detinfo::DetectorPropertiesData const &detProp, pma::Track3D *trk)
pma::Hit3D const * back() const
Definition: PmaTrack3D.h:106
auto const detProp
TrkCandidate& pma::TrkCandidateColl::front ( )
inline

Definition at line 196 of file PmaTrkCandidate.h.

197  {
198  return fCandidates.front();
199  }
std::vector< TrkCandidate > fCandidates
TrkCandidate const& pma::TrkCandidateColl::front ( ) const
inline

Definition at line 201 of file PmaTrkCandidate.h.

202  {
203  return fCandidates.front();
204  }
std::vector< TrkCandidate > fCandidates
int pma::TrkCandidateColl::getCandidateIndex ( pma::Track3D const *  candidate) const

Definition at line 52 of file PmaTrkCandidate.cxx.

53 {
54  for (size_t t = 0; t < fCandidates.size(); ++t)
55  if (fCandidates[t].Track() == candidate) return t;
56  return -1;
57 }
std::vector< TrkCandidate > fCandidates
TrackCollectionProxyElement< TrackCollProxy > Track
Proxy to an element of a proxy collection of recob::Track objects.
int pma::TrkCandidateColl::getCandidateTreeId ( pma::Track3D const *  candidate) const

Definition at line 60 of file PmaTrkCandidate.cxx.

61 {
62  int id = getCandidateIndex(candidate);
63  if (id >= 0)
64  return fCandidates[id].TreeId();
65  else
66  return -1;
67 }
std::vector< TrkCandidate > fCandidates
int getCandidateIndex(pma::Track3D const *candidate) const
pma::Track3D * pma::TrkCandidateColl::getTreeCopy ( pma::TrkCandidateColl dst,
size_t  trkIdx,
bool  isRoot = true 
)

Definition at line 355 of file PmaTrkCandidate.cxx.

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 }
pma::Track3D * getTreeCopy(pma::TrkCandidateColl &dst, size_t trkIdx, bool isRoot=true)
std::vector< TrkCandidate > fCandidates
bool AttachTo(pma::Node3D *vStart, bool noFlip=false)
virtual unsigned int NextCount(void) const
Definition: SortedObjects.h:84
std::vector< pma::Node3D * > const & Nodes() const noexcept
Definition: PmaTrack3D.h:338
int getCandidateIndex(pma::Track3D const *candidate) const
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
Definition: SortedObjects.h:79
pma::Track3D * Parent(void) const
Definition: PmaSegment3D.h:68
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
Definition: SortedObjects.h:45
pma::Segment3D * NextSegment(pma::Node3D *vtx) const
std::vector< TrkCandidate > const & tracks() const
void pma::TrkCandidateColl::merge ( size_t  idx1,
size_t  idx2 
)

Definition at line 341 of file PmaTrkCandidate.cxx.

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 }
std::vector< TrkCandidate > fCandidates
TrackCollectionProxyElement< TrackCollProxy > Track
Proxy to an element of a proxy collection of recob::Track objects.
void setTreeId(int id, size_t trkIdx, bool isRoot=true)
TrkCandidate& pma::TrkCandidateColl::operator[] ( size_t  i)
inline

Definition at line 192 of file PmaTrkCandidate.h.

192 { return fCandidates[i]; }
std::vector< TrkCandidate > fCandidates
TrkCandidate const& pma::TrkCandidateColl::operator[] ( size_t  i) const
inline

Definition at line 193 of file PmaTrkCandidate.h.

193 { return fCandidates[i]; }
std::vector< TrkCandidate > fCandidates
std::vector<TrkCandidate> const& pma::TrkCandidateColl::parents ( ) const
inline

Definition at line 229 of file PmaTrkCandidate.h.

230  {
231  return fParents;
232  }
std::vector< TrkCandidate > fParents
void pma::TrkCandidateColl::push_back ( const TrkCandidate trk)
inline

Definition at line 177 of file PmaTrkCandidate.h.

178  {
179  fCandidates.push_back(trk);
180  }
std::vector< TrkCandidate > fCandidates
void pma::TrkCandidateColl::resize ( size_t  n)
inline

Definition at line 166 of file PmaTrkCandidate.h.

167  {
168  return fCandidates.resize(n);
169  }
std::vector< TrkCandidate > fCandidates
void pma::TrkCandidateColl::setParentDaughterConnections ( )

Definition at line 70 of file PmaTrkCandidate.cxx.

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 }
std::vector< TrkCandidate > fCandidates
virtual unsigned int NextCount(void) const
Definition: SortedObjects.h:84
std::vector< pma::Node3D * > const & Nodes() const noexcept
Definition: PmaTrack3D.h:338
std::vector< TrkCandidate > fParents
int getCandidateIndex(pma::Track3D const *candidate) const
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
Definition: SortedObjects.h:79
virtual pma::SortedObjectBase * Prev(void) const
Definition: SortedObjects.h:44
bool SetParent(detinfo::DetectorPropertiesData const &detProp, std::string inFcnLabel, TCSlice &slc, PFPStruct &pfp, ShowerStruct3D &ss3, bool prt)
Definition: TCShower.cxx:1832
void pma::TrkCandidateColl::setTreeId ( int  id,
size_t  trkIdx,
bool  isRoot = true 
)

Definition at line 128 of file PmaTrkCandidate.cxx.

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 }
std::vector< TrkCandidate > fCandidates
virtual unsigned int NextCount(void) const
Definition: SortedObjects.h:84
std::vector< pma::Node3D * > const & Nodes() const noexcept
Definition: PmaTrack3D.h:338
int getCandidateIndex(pma::Track3D const *candidate) const
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
Definition: SortedObjects.h:79
pma::Track3D * Parent(void) const
Definition: PmaSegment3D.h:68
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
int pma::TrkCandidateColl::setTreeIds ( )

Definition at line 165 of file PmaTrkCandidate.cxx.

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 }
std::vector< TrkCandidate > fCandidates
int getCandidateIndex(pma::Track3D const *candidate) const
void setTreeId(int id, size_t trkIdx, bool isRoot=true)
bool pma::TrkCandidateColl::setTreeOriginAtBack ( detinfo::DetectorPropertiesData const &  detProp,
pma::Track3D trk 
)

Definition at line 300 of file PmaTrkCandidate.cxx.

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 }
std::vector< TrkCandidate > fCandidates
int getCandidateTreeId(pma::Track3D const *candidate) const
int getCandidateIndex(pma::Track3D const *candidate) const
bool Flip(const detinfo::DetectorPropertiesData &detProp, std::vector< pma::Track3D * > &allTracks)
Definition: PmaTrack3D.cxx:534
auto const detProp
bool pma::TrkCandidateColl::setTreeOriginAtFront ( detinfo::DetectorPropertiesData const &  detProp,
pma::Track3D trk 
)

Definition at line 254 of file PmaTrkCandidate.cxx.

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 }
std::vector< TrkCandidate > fCandidates
int getCandidateTreeId(pma::Track3D const *candidate) const
int index_of(const pma::Hit3D *hit) const
Definition: PmaTrack3D.cxx:336
std::vector< pma::Node3D * > const & Nodes() const noexcept
Definition: PmaTrack3D.h:338
pma::Track3D * Split(detinfo::DetectorPropertiesData const &detProp, size_t idx, bool try_start_at_idx=true)
int getCandidateIndex(pma::Track3D const *candidate) const
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
pma::Segment3D * NextSegment(pma::Node3D *vtx) const
auto const detProp
size_t pma::TrkCandidateColl::size ( void  ) const
inline

Definition at line 161 of file PmaTrkCandidate.h.

162  {
163  return fCandidates.size();
164  }
std::vector< TrkCandidate > fCandidates
std::vector<TrkCandidate> const& pma::TrkCandidateColl::tracks ( ) const
inline

Definition at line 218 of file PmaTrkCandidate.h.

219  {
220  return fCandidates;
221  }
std::vector< TrkCandidate > fCandidates
std::vector<TrkCandidate>& pma::TrkCandidateColl::tracks ( )
inline

Definition at line 223 of file PmaTrkCandidate.h.

224  {
225  return fCandidates;
226  }
std::vector< TrkCandidate > fCandidates

Member Data Documentation

std::vector<TrkCandidate> pma::TrkCandidateColl::fCandidates
private

Definition at line 253 of file PmaTrkCandidate.h.

std::vector<TrkCandidate> pma::TrkCandidateColl::fParents
private

Definition at line 254 of file PmaTrkCandidate.h.


The documentation for this class was generated from the following files: