19 #include "messagefacility/MessageLogger/MessageLogger.h"
24 : fParent(-1), fTrack(0), fKey(-1), fTreeId(-1), fMse(0), fValidation(0), fGood(
false)
29 : fParent(-1), fTrack(trk), fKey(key), fTreeId(tid), fMse(0), fValidation(0), fGood(
false)
36 if (fTrack)
delete fTrack;
44 if (fTrack)
delete fTrack;
54 for (
size_t t = 0; t < fCandidates.size(); ++t)
55 if (fCandidates[t].
Track() == candidate)
return t;
62 int id = getCandidateIndex(candidate);
64 return fCandidates[id].TreeId();
75 while (t < fCandidates.size()) {
76 if (fCandidates[t].IsValid()) {
77 fCandidates[t].SetParent(-1);
78 fCandidates[t].Daughters().clear();
82 fCandidates.erase(fCandidates.begin() + t);
85 for (t = 0; t < fCandidates.size(); ++t) {
86 if (!fCandidates[t].IsValid())
continue;
90 if (firstNode->
Prev())
93 fCandidates[t].SetParent(getCandidateIndex(parentTrk));
95 else if (fCandidates[t].Parent() < 0)
99 fParents.back().SetTreeId(fCandidates[t].TreeId());
100 size_t pri_idx = fCandidates.size() + fParents.size() - 1;
102 for (
size_t i = 0; i < firstNode->
NextCount(); ++i) {
105 int idx = getCandidateIndex(daughterTrk);
107 fCandidates[(size_t)idx].
SetParent(pri_idx);
108 fParents.back().Daughters().push_back((
size_t)idx);
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) {
117 if (daughterTrk != trk) {
118 int idx = getCandidateIndex(daughterTrk);
119 if (idx >= 0) fCandidates[t].Daughters().push_back((
size_t)idx);
143 for (
size_t i = 0; i < vtx->
NextCount(); i++) {
145 if (seg != segThis) {
146 int idx = getCandidateIndex(seg->
Parent());
149 setTreeId(
id, idx,
false);
151 mf::LogError(
"pma::setTreeId") <<
"Branch of the tree not found in tracks collection.";
161 fCandidates[trkIdx].SetTreeId(
id);
167 for (
auto& t : fCandidates)
171 for (
auto& t : fCandidates) {
172 if (!t.IsValid() || (t.TreeId() >= 0))
continue;
175 int rootTrkIdx = getCandidateIndex(t.Track()->GetRoot());
178 setTreeId(
id, rootTrkIdx);
180 mf::LogError(
"pma::setTreeIds") <<
"Root of the tree not found in tracks collection.";
193 std::map<int, std::vector<pma::Track3D*>> toFlip;
194 std::map<int, double> minVal;
197 for (
auto& t : fCandidates) {
198 if (!t.IsValid())
continue;
200 int tid = t.TreeId();
201 if (minVal.find(tid) == minVal.end()) minVal[tid] = 1.0e12;
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());
211 if (pFront[coordinate] < minVal[tid]) {
212 minVal[tid] = pFront[coordinate];
213 toFlip[tid].push_back(t.Track());
216 if (pBack[coordinate] < minVal[tid]) {
217 minVal[tid] = pBack[coordinate];
218 if (!pushed) toFlip[tid].push_back(t.Track());
222 for (
auto& tEntry : toFlip)
223 if (tEntry.first >= 0) {
225 while (!tEntry.second.empty()) {
227 tEntry.second.pop_back();
230 pFront.SetX(-pFront.X());
231 pFront.SetY(-pFront.Y());
233 pBack.SetX(-pBack.X());
234 pBack.SetY(-pBack.Y());
236 if (pFront[coordinate] > pBack[coordinate]) {
237 if (setTreeOriginAtBack(detProp, trk)) {
break; }
239 mf::LogWarning(
"pma::TrkCandidateColl") <<
"Flip to coordinate failed.";
243 setTreeOriginAtFront(detProp, trk);
247 if (attempts++ > 2)
break;
257 int trkIdx = getCandidateIndex(trk);
258 int treeId = getCandidateTreeId(trk);
260 throw cet::exception(
"pma::TrkCandidateColl")
261 <<
"Track not found in the collection." << std::endl;
269 std::vector<pma::Track3D*> newTracks;
276 newTracks.push_back(u);
277 done = u->
Flip(detProp, newTracks);
284 throw cet::exception(
"pma::Track3D") <<
"Node not found." << std::endl;
288 done = incoming->
Flip(detProp, newTracks);
291 for (
const auto ts : newTracks) {
292 fCandidates.emplace_back(ts, -1, treeId);
303 int trkIdx = getCandidateIndex(trk);
304 int treeId = getCandidateTreeId(trk);
306 throw cet::exception(
"pma::TrkCandidateColl")
307 <<
"Track not found in the collection." << std::endl;
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);
323 std::map<int, std::vector<pma::Track3D*>> trkMap;
326 for (
auto const& t : fCandidates) {
327 if (t.IsValid()) trkMap[t.TreeId()].push_back(t.Track());
330 for (
auto& tEntry : trkMap) {
332 for (
size_t i = tEntry.second.size(); i > 0; --i) {
343 fCandidates[idx1].Track()->ExtendWith(fCandidates[idx2].
Track());
345 for (
auto c : fCandidates[idx2].Clusters()) {
346 fCandidates[idx1].Clusters().push_back(c);
349 fCandidates.erase(fCandidates.begin() + idx2);
351 setTreeId(fCandidates[idx1].TreeId(), idx1);
362 int key = fCandidates[trkIdx].Key();
363 int tid = fCandidates[trkIdx].TreeId();
369 dst.
tracks().emplace_back(trkCopy, key, tid);
376 if (segThisCopy) vtxCopy =
static_cast<pma::Node3D*
>(segThisCopy->
Next());
383 for (
size_t i = 0; i < vtx->
NextCount(); i++) {
385 if (seg != segThis) {
386 int idx = getCandidateIndex(seg->
Parent());
389 pma::Track3D* branchCopy = getTreeCopy(dst, idx,
false);
390 if (!branchCopy->
AttachTo(vtxCopy,
true))
391 mf::LogError(
"pma::getTreeCopy") <<
"Branch copy cannot be attached to the tree.";
394 mf::LogError(
"pma::getTreeCopy") <<
"Branch of the tree not found in source collection.";
403 if (segThisCopy) vtxCopy =
static_cast<pma::Node3D*
>(segThisCopy->
Next());
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
Implementation of the Projection Matching Algorithm.
pma::Hit3D const * front() const
Implementation of the Projection Matching Algorithm.
int index_of(const pma::Hit3D *hit) const
virtual unsigned int NextCount(void) const
std::vector< pma::Node3D * > const & Nodes() const noexcept
TVector3 const & Point3D() const
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.
bool setTreeOriginAtFront(detinfo::DetectorPropertiesData const &detProp, pma::Track3D *trk)
void setParentDaughterConnections()
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
pma::Hit3D const * back() const
TrackCollectionProxyElement< TrackCollProxy > Track
Proxy to an element of a proxy collection of recob::Track objects.
virtual pma::SortedObjectBase * Prev(void) const
bool Flip(const detinfo::DetectorPropertiesData &detProp, std::vector< pma::Track3D * > &allTracks)
pma::Track3D * Parent(void) const
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
bool SetParent(detinfo::DetectorPropertiesData const &detProp, std::string inFcnLabel, TCSlice &slc, PFPStruct &pfp, ShowerStruct3D &ss3, bool prt)
void setTreeId(int id, size_t trkIdx, bool isRoot=true)
pma::Segment3D * NextSegment(pma::Node3D *vtx) const
std::vector< TrkCandidate > const & tracks() const