27 if (trk == t.first.Track())
return true;
34 if (!
Has(t.first.Track()))
return false;
41 if (!rootTrk)
throw cet::exception(
"pma::VtxCandidate") <<
"Broken track.";
46 if (!rootAssn)
throw cet::exception(
"pma::VtxCandidate") <<
"Broken track.";
62 for (
size_t t = 0; t <
fAssigned.size(); t++)
65 if (!trk_t)
throw cet::exception(
"pma::VtxCandidate") <<
"Broken track.";
67 for (
size_t u = 0; u <
fAssigned.size(); u++)
71 if (!trk_u)
throw cet::exception(
"pma::VtxCandidate") <<
"Broken track.";
83 if (c.first.Track()->Length() > minLength) n++;
112 min_mse = mse; n_best =
n; d_best =
d;
117 if (d_best < kMaxDistToTrack)
136 size_t n_best = 0, m_best = 0;
139 double lm, ln, l_best = 0;
140 for (
size_t m = 0; m < p0->
Nodes().size() - 1; m++)
162 double d_dist = (d_best -
d) / d_best;
163 if (lm + ln > 0.8 * d_dist * l_best)
166 n_best =
n; m_best = m;
167 d_best =
d; l_best = lm + ln;
173 if (d_best < kMaxDistToTrack)
219 int tpc = trk->
Nodes()[t.second]->TPC();
220 int cryo = trk->
Nodes()[t.second]->Cryo();
239 mse += m / (double)k;
242 return mse / fAssigned.size();
250 double dw =
fErr[0] * other.
fErr[0] * dx * dx
252 +
fErr[2] * other.
fErr[2] * dz * dz;
259 size_t max_l_idx = 0;
260 double l, max_l = 0.0;
261 for (
size_t i = 0; i <
fAssigned.size() - 1; i++)
263 l =
fAssigned[i].first.Track()->Length();
266 max_l = l; max_l_idx = i;
271 dir_i *= 1.0 / dir_i.Mag();
276 for (
size_t j = 0; j <
fAssigned.size(); j++)
277 if ((j != max_l_idx) && (
fAssigned[j].first.Track()->Length() > minLength))
283 dir_j *= 1.0 / dir_j.Mag();
284 a = fabs(dir_i * dir_j);
285 if (a < min) min = a;
288 return 180.0 * acos(min) / TMath::Pi();
300 double dw =
Test(other);
310 if (!
Has(t.first.Track()))
318 double mse0 =
fMse, mse1 = other.
fMse;
320 <<
"try: " << d <<
" mse0:" << sqrt(mse0) <<
" mse1:" << sqrt(mse1);
325 <<
"out: " <<
Size() <<
" mse:" << sqrt(mse) <<
" dw:" << dw;
352 std::vector< pma::Segment3D* > segments;
353 std::vector< std::pair<TVector3, TVector3> > lines;
354 std::vector< double > weights;
362 double segLength = seg->
Length();
367 std::pair<TVector3, TVector3> endpoints(vtx1->
Point3D(), vtx2->
Point3D());
368 double dy = endpoints.first.Y() - endpoints.second.Y();
369 double fy_norm = asin(fabs(dy) / segLength) / (0.5 * TMath::Pi());
370 double w = 1.0 - pow(fy_norm - 1.0, 12);
371 if (w < 0.3) w = 0.3;
373 lines.push_back(endpoints);
374 segments.push_back(seg);
375 weights.push_back(w);
379 fCenter.SetXYZ(0., 0., 0.);
fErr.SetXYZ(0., 0., 0.);
385 mf::LogWarning(
"pma::VtxCandidate") <<
"Cannot compute crossing point.";
392 for (
size_t s = 0;
s < segments.size();
s++)
403 fErr[0] += weights[
s] * weights[
s];
407 fCenter[0] += weights[
s] * pproj.X();
416 fErr *= 1.0 / segments.size();
428 mf::LogError(
"pma::VtxCandidate") <<
"Tracks already attached to the vertex.";
439 while (t < src.
size())
441 if (c.first.Track() == src[t].Track())
452 for (
auto & c : fAssigned)
453 for (
auto const & t : tracks.
tracks())
454 if (c.first.Track() == t.Track())
456 c.first.SetTreeId(t.TreeId());
break;
460 std::vector<int> treeIds;
464 bool hasInnerCenter =
false;
466 for (
size_t i = 0; i < fAssigned.size(); i++)
471 int key = fAssigned[i].first.Key();
472 int tid = fAssigned[i].first.TreeId();
473 size_t idx = fAssigned[i].second;
476 <<
" (nodes:" << trk->
Nodes().size() <<
")";
478 if (!
has(treeIds, tid))
480 treeIds.push_back(tid);
482 if (rootIdx >= 0) tracks.
getTreeCopy(backupTracks, rootIdx);
483 else mf::LogError(
"pma::VtxCandidate") <<
"Root of the tree not found in tracks collection.";
486 TVector3 p0(trk->
Nodes()[idx]->Point3D());
487 TVector3 p1(trk->
Nodes()[idx + 1]->Point3D());
489 int tpc0 = trk->
Nodes()[idx]->TPC();
490 int tpc1 = trk->
Nodes()[idx + 1]->TPC();
492 int cryo0 = trk->
Nodes()[idx]->Cryo();
493 int cryo1 = trk->
Nodes()[idx + 1]->Cryo();
506 vtxCenter = trk->
Nodes().front();
513 if (trk->
AttachTo(vtxCenter)) nOK++;
522 mf::LogVerbatim(
"pma::VtxCandidate") <<
" flip trk to make new center";
524 vtxCenter = trk->
Nodes().front();
529 vtxCenter = trk->
Nodes().back();
538 mf::LogVerbatim(
"pma::VtxCandidate") <<
" flip trk to attach to inner";
540 if (trk->
AttachTo(vtxCenter)) nOK++;
551 bool canFlipPrev =
true;
552 if (vtxCenter && vtxCenter->
Prev())
559 if (hasInnerCenter || !canFlipPrev)
563 if ((f >= 0.0F) && (f <= 1.0) &&
569 if (f < 0.5) { tpc = tpc0; cryo = cryo0; }
570 else { tpc = tpc1; cryo = cryo1; }
578 mf::LogVerbatim(
"pma::VtxCandidate") <<
" add center at end of segment";
583 mf::LogVerbatim(
"pma::VtxCandidate") <<
" center at start of segment - no action";
599 tracks.
tracks().emplace_back(t0, key, tid);
603 vtxCenter = trk->
Nodes().front();
609 if (trk->
AttachTo(vtxCenter)) nOK += 2;
617 hasInnerCenter =
true;
619 if ((f >= 0.0F) && (f <= 1.0) &&
625 if (f < 0.5) { tpc = tpc0; cryo = cryo0; }
626 else { tpc = tpc1; cryo = cryo1; }
634 mf::LogVerbatim(
"pma::VtxCandidate") <<
" add center at end of segment";
639 mf::LogVerbatim(
"pma::VtxCandidate") <<
" center at start of segment - no action";
651 rootBranch = seg->
Parent();
658 for (
size_t j = 0; j < branches.size(); ++j)
660 if (branches[j]->AttachTo(innerCenter,
true)) { }
662 vtxCenter = innerCenter;
666 vtxCenter = innerCenter;
680 if (vtxCenter->
NextCount()) rootSeg = static_cast< pma::Segment3D* >(vtxCenter->
Next(0));
681 else if (vtxCenter->
Prev()) rootSeg = static_cast< pma::Segment3D* >(vtxCenter->
Prev());
682 else throw cet::exception(
"pma::VtxCandidate") <<
"Vertex with no segments attached.";
685 if (!rootTrk) rootTrk = rootSeg->
Parent();
687 std::vector< pma::Track3D const * > branchesToRemove;
688 bool noLoops = rootTrk->
GetBranches(branchesToRemove);
691 if (noLoops && (nOK > 1))
709 if (noLoops && tuneOK)
712 for (
auto & c : backupTracks.
tracks()) c.DeleteTrack();
716 mf::LogVerbatim(
"pma::VtxCandidate") <<
"restore tracks from backup....";
717 for (
int tid : treeIds)
720 while (t < tracks.
size())
722 if (tracks[t].TreeId() == tid)
724 tracks[t].DeleteTrack();
734 else mf::LogError(
"pma::VtxCandidate") <<
"Cannot create common vertex";
Vertex finding helper for the Projection Matching Algorithm.
code to link reconstructed objects back to the MC truth information
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
bool HasPlane(unsigned int iplane) const
Returns whether a plane with index iplane is present in this TPC.
double ComputeMse2D(void)
bool Has(pma::Track3D *trk) const
pma::Track3D * getTreeCopy(pma::TrkCandidateColl &dst, size_t trkIdx, bool isRoot=true)
bool AttachTo(pma::Node3D *vStart, bool noFlip=false)
void InsertNode(TVector3 const &p3d, size_t at_idx, unsigned int tpc, unsigned int cryo)
bool IsAttachedTo(pma::Track3D const *trk) const
double Dist2(const TVector2 &v1, const TVector2 &v2)
double Test(const VtxCandidate &other) const
bool Flip(std::vector< pma::Track3D * > &allTracks)
TVector3 const & Point3D(void) const
void erase_at(size_t pos)
std::vector< pma::Node3D * > const & Nodes(void) const
bool GetBranches(std::vector< pma::Track3D const * > &branches, bool skipFirst=false) const
virtual unsigned int NextCount(void) const
bool JoinTracks(pma::TrkCandidateColl &tracks, pma::TrkCandidateColl &src)
Planes which measure Z direction.
std::vector< TrkCandidate > const & tracks(void) const
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
pma::Track3D * Split(size_t idx, bool try_start_at_idx=true)
double TuneFullTree(double eps=0.001, double gmax=50.0)
bool HasLoops(void) const
TVector2 GetProjectionToSegment(const TVector2 &p, const TVector2 &p0, const TVector2 &p1)
static const double kMinDistToNode
int getCandidateIndex(pma::Track3D const *candidate) const
bool CanFlip(void) const
Check if the track can be flipped without breaking any other track.
bool SetPoint3D(const TVector3 &p3d)
std::vector< std::pair< pma::TrkCandidate, size_t > > fAssigned
double SolveLeastSquares3D(const std::vector< std::pair< TVector3, TVector3 > > &lines, TVector3 &result)
double MaxAngle(double minLength=0.0) const
std::vector< pma::Track3D * > GetBranches(void) const
bool MergeWith(const VtxCandidate &other)
Implementation of the Projection Matching Algorithm.
pma::Track3D * GetRoot(void)
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
static const double kMaxDistToTrack
bool Add(const pma::TrkCandidate &trk)
double GetSegmentProjVector(const TVector2 &p, const TVector2 &p0, const TVector2 &p1)
TVector2 GetProjectionToPlane(const TVector3 &p, unsigned int plane, unsigned int tpc, unsigned int cryo)
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
void MakeProjection(void)
bool IsAttached(pma::Track3D *trk) const
double GetDistance2To(const TVector3 &p3d) const override
Distance [cm] from the 3D segment to the point 3D.
TPCGeo const & TPC(unsigned int const tpc=0, unsigned int const cstat=0) const
Returns the specified TPC.
virtual pma::SortedObjectBase * Prev(void) const
pma::Track3D * Parent(void) const
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
bool AttachBackTo(pma::Node3D *vStart)
pma::Segment3D * NextSegment(pma::Node3D *vtx) const
double Length(void) const
void push_back(const TrkCandidate &trk)
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
pma::Track3D * Track(void) const
bool has(const std::vector< int > &v, int id) const