29 if (trk ==
t.first.Track())
return true;
37 if (!
Has(
t.first.Track()))
return false;
45 if (!rootTrk)
throw cet::exception(
"pma::VtxCandidate") <<
"Broken track.";
49 if (!rootAssn)
throw cet::exception(
"pma::VtxCandidate") <<
"Broken track.";
69 if (!trk_t)
throw cet::exception(
"pma::VtxCandidate") <<
"Broken track.";
71 for (
size_t u = 0; u <
fAssigned.size(); u++)
74 if (!trk_u)
throw cet::exception(
"pma::VtxCandidate") <<
"Broken track.";
87 if (
c.first.Track()->Length() > minLength) n++;
103 for (
size_t n = 0;
n < trk.
Track()->
Nodes().size() - 1;
n++) {
120 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++) {
147 for (
size_t n = 0;
n < trk.
Track()->
Nodes().size() - 1;
n++) {
159 double d_dist = (d_best -
d) / d_best;
160 if (lm + ln > 0.8 * d_dist * l_best)
172 if (d_best < kMaxDistToTrack) {
190 for (
size_t n = 0;
n < trk.
Track()->
Nodes().size() - 1;
n++) {
213 int tpc = trk->
Nodes()[
t.second]->TPC();
214 int cryo = trk->
Nodes()[
t.second]->Cryo();
233 mse += m / (double)k;
236 return mse / fAssigned.size();
245 double dw =
fErr[0] * other.
fErr[0] * dx * dx +
fErr[1] * other.
fErr[1] * dy * dy +
254 size_t max_l_idx = 0;
255 double l, max_l = 0.0;
256 for (
size_t i = 0; i <
fAssigned.size() - 1; i++) {
257 l =
fAssigned[i].first.Track()->Length();
265 dir_i *= 1.0 / dir_i.Mag();
270 for (
size_t j = 0; j <
fAssigned.size(); j++)
271 if ((j != max_l_idx) && (
fAssigned[j].first.Track()->Length() > minLength)) {
276 dir_j *= 1.0 / dir_j.Mag();
277 a = fabs(dir_i * dir_j);
278 if (a < min) min =
a;
281 return 180.0 * acos(min) / TMath::Pi();
293 double dw =
Test(other);
301 if (!
Has(
t.first.Track())) {
307 double mse0 =
fMse, mse1 = other.
fMse;
309 <<
"try: " << d <<
" mse0:" << sqrt(mse0) <<
" mse1:" << sqrt(mse1);
314 <<
"out: " <<
Size() <<
" mse:" << sqrt(mse) <<
" dw:" << dw;
342 std::vector<pma::Segment3D*> segments;
343 std::vector<std::pair<TVector3, TVector3>>
lines;
344 std::vector<double> weights;
351 double segLength = seg->
Length();
355 std::pair<TVector3, TVector3> endpoints(vtx1->
Point3D(), vtx2->
Point3D());
356 double dy = endpoints.first.Y() - endpoints.second.Y();
357 double fy_norm = asin(fabs(dy) / segLength) / (0.5 * TMath::Pi());
358 double w = 1.0 -
pow(fy_norm - 1.0, 12);
359 if (w < 0.3) w = 0.3;
361 lines.push_back(endpoints);
362 segments.push_back(seg);
363 weights.push_back(w);
368 fErr.SetXYZ(0., 0., 0.);
372 if (resultMse < 0.0) {
373 mf::LogWarning(
"pma::VtxCandidate") <<
"Cannot compute crossing point.";
380 for (
size_t s = 0;
s < segments.size();
s++) {
390 fErr[0] += weights[
s] * weights[
s];
394 fCenter[0] += weights[
s] * pproj.X();
403 fErr *= 1.0 / segments.size();
417 mf::LogError(
"pma::VtxCandidate") <<
"Tracks already attached to the vertex.";
423 <<
"JoinTracks (" <<
fAssigned.size() <<
") at:" 428 while (t < src.
size()) {
429 if (
c.first.Track() == src[
t].Track()) {
440 for (
auto&
c : fAssigned)
441 for (
auto const&
t : tracks.
tracks())
442 if (
c.first.Track() ==
t.Track()) {
443 c.first.SetTreeId(
t.TreeId());
448 std::vector<int> treeIds;
452 bool hasInnerCenter =
false;
454 for (
size_t i = 0; i < fAssigned.size(); i++) {
458 int key = fAssigned[i].first.Key();
459 int tid = fAssigned[i].first.TreeId();
460 size_t idx = fAssigned[i].second;
463 <<
" (nodes:" << trk->
Nodes().size() <<
")";
465 if (!
has(treeIds, tid))
467 treeIds.push_back(tid);
472 mf::LogError(
"pma::VtxCandidate") <<
"Root of the tree not found in tracks collection.";
475 TVector3 p0(trk->
Nodes()[idx]->Point3D());
476 TVector3 p1(trk->
Nodes()[idx + 1]->Point3D());
478 int tpc0 = trk->
Nodes()[idx]->TPC();
479 int tpc1 = trk->
Nodes()[idx + 1]->TPC();
481 int cryo0 = trk->
Nodes()[idx]->Cryo();
482 int cryo1 = trk->
Nodes()[idx + 1]->Cryo();
493 vtxCenter = trk->
Nodes().front();
499 if (trk->
AttachTo(vtxCenter)) nOK++;
505 mf::LogVerbatim(
"pma::VtxCandidate") <<
" flip trk to make new center";
507 vtxCenter = trk->
Nodes().front();
511 vtxCenter = trk->
Nodes().back();
518 mf::LogVerbatim(
"pma::VtxCandidate") <<
" flip trk to attach to inner";
520 if (trk->
AttachTo(vtxCenter)) nOK++;
529 bool canFlipPrev =
true;
530 if (vtxCenter && vtxCenter->
Prev()) {
538 if (hasInnerCenter || !canFlipPrev) {
559 mf::LogVerbatim(
"pma::VtxCandidate") <<
" add center at end of segment";
563 mf::LogVerbatim(
"pma::VtxCandidate") <<
" center at start of segment - no action";
571 <<
" trk size:" << trk->
size() <<
" (nodes:" << trk->
Nodes().size() <<
")";
576 <<
" t0 size:" << t0->
size() <<
" (nodes:" << t0->
Nodes().size() <<
")";
580 tracks.
tracks().emplace_back(t0, key, tid);
583 vtxCenter = trk->
Nodes().front();
588 if (trk->
AttachTo(vtxCenter)) nOK += 2;
595 hasInnerCenter =
true;
615 mf::LogVerbatim(
"pma::VtxCandidate") <<
" add center at end of segment";
619 mf::LogVerbatim(
"pma::VtxCandidate") <<
" center at start of segment - no action";
630 rootBranch = seg->
Parent();
637 for (
size_t j = 0; j < branches.size(); ++j) {
638 if (branches[j]->AttachTo(innerCenter,
true)) {}
640 vtxCenter = innerCenter;
643 vtxCenter = innerCenter;
657 rootSeg = static_cast<pma::Segment3D*>(vtxCenter->
Next(0));
658 else if (vtxCenter->
Prev())
659 rootSeg = static_cast<pma::Segment3D*>(vtxCenter->
Prev());
661 throw cet::exception(
"pma::VtxCandidate") <<
"Vertex with no segments attached.";
664 if (!rootTrk) rootTrk = rootSeg->
Parent();
666 std::vector<pma::Track3D const*> branchesToRemove;
667 bool noLoops = rootTrk->
GetBranches(branchesToRemove);
670 if (noLoops && (nOK > 1)) {
687 if (noLoops && tuneOK) {
689 for (
auto&
c : backupTracks.
tracks())
693 mf::LogVerbatim(
"pma::VtxCandidate") <<
"restore tracks from backup....";
694 for (
int tid : treeIds) {
696 while (t < tracks.
size()) {
697 if (tracks[t].TreeId() == tid) {
698 tracks[
t].DeleteTrack();
705 for (
const auto&
c : backupTracks.
tracks())
711 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.
TVector3 const & Point3D() const
bool Has(pma::Track3D *trk) const
double SolveLeastSquares3D(const std::vector< std::pair< TVector3, TVector3 >> &lines, TVector3 &result)
pma::Track3D * getTreeCopy(pma::TrkCandidateColl &dst, size_t trkIdx, bool isRoot=true)
bool AttachTo(pma::Node3D *vStart, bool noFlip=false)
static constexpr double g
Implementation of the Projection Matching Algorithm.
bool IsAttachedTo(pma::Track3D const *trk) const
double Dist2(const TVector2 &v1, const TVector2 &v2)
double Test(const VtxCandidate &other) const
bool JoinTracks(detinfo::DetectorPropertiesData const &detProp, pma::TrkCandidateColl &tracks, pma::TrkCandidateColl &src)
void erase_at(size_t pos)
Implementation of the Projection Matching Algorithm.
bool GetBranches(std::vector< pma::Track3D const * > &branches, bool skipFirst=false) const
std::vector< std::pair< pma::TrkCandidate, size_t > > fAssigned
virtual unsigned int NextCount(void) const
std::vector< pma::Track3D * > GetBranches() const
Planes which measure Z direction.
Implementation of the Projection Matching Algorithm.
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
double TuneFullTree(double eps=0.001, double gmax=50.0)
art framework interface to geometry description
bool Has(const VtxCandidate &other) const
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.
TVector2 GetProjectionToSegment(const TVector2 &p, const TVector2 &p0, const TVector2 &p1)
static constexpr double kMinDistToNode
int getCandidateIndex(pma::Track3D const *candidate) const
bool SetPoint3D(const TVector3 &p3d)
static constexpr double kMaxDistToTrack
Definition of data types for geometry description.
double MaxAngle(double minLength=0.0) const
void InsertNode(detinfo::DetectorPropertiesData const &detProp, TVector3 const &p3d, size_t at_idx, unsigned int tpc, unsigned int cryo)
bool MergeWith(const VtxCandidate &other)
Implementation of the Projection Matching Algorithm.
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
bool Add(const pma::TrkCandidate &trk)
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
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
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.
std::vector< pma::Node3D * > const & Nodes() const noexcept
virtual pma::SortedObjectBase * Prev(void) const
bool Flip(const detinfo::DetectorPropertiesData &detProp, std::vector< pma::Track3D * > &allTracks)
pma::Track3D * Track() 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)
std::vector< TrkCandidate > const & tracks() const
cet::coded_exception< error, detail::translate > exception
bool has(const std::vector< int > &v, int id) const