18 fParent(-1), fTrack(0), fKey(-1), fTreeId(-1),
19 fMse(0), fValidation(0),
51 for (
size_t t = 0; t < fCandidates.size(); ++t)
52 if (fCandidates[t].
Track() == candidate)
return t;
58 int id = getCandidateIndex(candidate);
59 if (
id >= 0)
return fCandidates[id].TreeId();
68 while (t < fCandidates.size())
72 fCandidates[t].SetParent(-1);
73 fCandidates[t].Daughters().clear();
76 else fCandidates.erase(fCandidates.begin() + t);
79 for (t = 0; t < fCandidates.size(); ++t)
81 if (!fCandidates[t].
IsValid())
continue;
85 if (firstNode->
Prev())
88 fCandidates[t].SetParent(getCandidateIndex(parentTrk));
90 else if (fCandidates[t].
Parent() < 0)
93 fParents.back().SetTreeId(fCandidates[t].
TreeId());
94 size_t pri_idx = fCandidates.size() + fParents.size() - 1;
96 for (
size_t i = 0; i < firstNode->
NextCount(); ++i)
99 int idx = getCandidateIndex(daughterTrk);
102 fCandidates[(size_t)idx].
SetParent(pri_idx);
103 fParents.back().Daughters().push_back((
size_t)idx);
108 for (
size_t n = 1;
n < trk->
Nodes().size(); ++
n)
110 auto node = trk->
Nodes()[
n];
111 for (
size_t i = 0; i < node->NextCount(); ++i)
114 if (daughterTrk != trk)
116 int idx = getCandidateIndex(daughterTrk);
117 if (idx >= 0) fCandidates[t].Daughters().push_back((
size_t)idx);
142 for (
size_t i = 0; i < vtx->
NextCount(); i++)
147 int idx = getCandidateIndex(seg->
Parent());
149 if (idx >= 0) setTreeId(
id, idx,
false);
150 else mf::LogError(
"pma::setTreeId") <<
"Branch of the tree not found in tracks collection.";
158 fCandidates[trkIdx].SetTreeId(
id);
163 for (
auto & t : fCandidates) t.SetTreeId(-1);
166 for (
auto & t : fCandidates)
168 if (!t.IsValid() || (t.TreeId() >= 0))
continue;
171 int rootTrkIdx = getCandidateIndex(t.Track()->GetRoot());
173 if (rootTrkIdx >= 0) setTreeId(
id, rootTrkIdx);
174 else mf::LogError(
"pma::setTreeIds") <<
"Root of the tree not found in tracks collection.";
185 std::map< int, std::vector< pma::Track3D* > > toFlip;
186 std::map< int, double > minVal;
189 for (
auto & t : fCandidates)
191 if (!t.IsValid())
continue;
193 int tid = t.TreeId();
194 if (minVal.find(tid) == minVal.end()) minVal[tid] = 1.0e12;
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());
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()); }
204 for (
auto & tEntry : toFlip)
205 if (tEntry.first >= 0)
208 while (!tEntry.second.empty())
211 tEntry.second.pop_back();
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());
216 if (pFront[coordinate] > pBack[coordinate])
218 if (setTreeOriginAtBack(trk)) {
break; }
219 else {
mf::LogWarning(
"pma::TrkCandidateColl") <<
"Flip to coordinate failed."; }
223 setTreeOriginAtFront(trk);
227 if (attempts++ > 2)
break;
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; }
245 std::vector< pma::Track3D* > newTracks;
254 newTracks.push_back(u);
255 done = u->
Flip(newTracks);
257 else { done =
false; }
259 else {
throw cet::exception(
"pma::Track3D") <<
"Node not found." << std::endl; }
261 else { done = incoming->
Flip(newTracks); }
263 for (
const auto ts : newTracks) { fCandidates.emplace_back(ts, -1, treeId); }
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; }
276 std::vector< pma::Track3D* > newTracks;
277 bool done = incoming->
Flip(newTracks);
278 for (
const auto ts : newTracks)
280 fCandidates.emplace_back(ts, -1, treeId);
288 std::map< int, std::vector< pma::Track3D* > > trkMap;
291 for (
auto const & t : fCandidates)
293 if (t.IsValid()) trkMap[t.TreeId()].push_back(t.Track());
296 for (
auto & tEntry : trkMap)
299 for (
size_t i = tEntry.second.size(); i > 0; --i)
323 fCandidates[idx1].Track()->ExtendWith(fCandidates[idx2].
Track());
325 for (
auto c : fCandidates[idx2].
Clusters()) { fCandidates[idx1].Clusters().push_back(c); }
327 fCandidates.erase(fCandidates.begin() + idx2);
329 setTreeId(fCandidates[idx1].
TreeId(), idx1);
339 int key = fCandidates[trkIdx].Key();
340 int tid = fCandidates[trkIdx].TreeId();
346 dst.
tracks().emplace_back(trkCopy, key, tid);
354 if (segThisCopy) vtxCopy =
static_cast< pma::Node3D*
>(segThisCopy->
Next());
362 for (
size_t i = 0; i < vtx->
NextCount(); i++)
367 int idx = getCandidateIndex(seg->
Parent());
371 pma::Track3D* branchCopy = getTreeCopy(dst, idx,
false);
372 if (!branchCopy->
AttachTo(vtxCopy,
true))
373 mf::LogError(
"pma::getTreeCopy") <<
"Branch copy cannot be attached to the tree.";
375 else mf::LogError(
"pma::getTreeCopy") <<
"Branch of the tree not found in source collection.";
382 if (segThisCopy) vtxCopy =
static_cast< pma::Node3D*
>(segThisCopy->
Next());
const std::vector< size_t > & Clusters(void) const
void AutoFlip(pma::Track3D::EDirection dir, double thr=0.0, unsigned int n=0)
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
bool Flip(std::vector< pma::Track3D * > &allTracks)
void flipTreesToCoordinate(size_t coordinate)
void setParentDaughterConnections(void)
std::vector< pma::Node3D * > const & Nodes(void) const
int index_of(const pma::Hit3D *hit) const
virtual unsigned int NextCount(void) const
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)
int getCandidateIndex(pma::Track3D const *candidate) const
bool CanFlip(void) const
Check if the track can be flipped without breaking any other track.
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
Implementation of the Projection Matching Algorithm.
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
virtual pma::SortedObjectBase * Prev(void) const
pma::Track3D * Parent(void) const
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
void setTreeId(int id, size_t trkIdx, bool isRoot=true)
pma::Segment3D * NextSegment(pma::Node3D *vtx) const
cet::coded_exception< error, detail::translate > exception
pma::Track3D * Track(void) const