Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions CRVReco/inc/TrkCrvGeom.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
#ifndef CRVReco_TrkCrvGeom_hh
#define CRVReco_TrkCrvGeom_hh
#include "Offline/DataProducts/inc/SurfaceId.hh"
#include "Offline/RecoDataProducts/inc/KalIntersection.hh"
#include "KinKal/General/Vectors.hh"
#include "KinKal/Geometry/Plane.hh"
#include "KinKal/Geometry/Rectangle.hh"
#include <cmath>
#include <memory>
#include <string>
#include <vector>
Expand All @@ -30,5 +33,28 @@ namespace mu2e {
std::vector<TrkCrvSector> trkCrvSectors();
// the shield with the largest PE sum among the cluster's pulses, -1 if none; multiSector set if pulses span shields
int trkCrvDominantShield(CrvCoincidenceCluster const& cluster, CosmicRayShield const& crs, bool* multiSector = nullptr);

constexpr double trkCrvMinCosToNormal = 1e-3; // |direction . plane normal| below which a track is parallel to the plane

// Covariance of the track position on the plane through point with the sector axes (u, v), for the straight line from a KalSeed
// intersection reaching that plane at time (the intersection state's covariance carried over the straight step; time = ki.time()
// gives the covariance of the state itself, measured in the plane). 0 for a line parallel to the plane.
inline KinKal::PMAT trkCrvPlaneCovariance(KalIntersection const& ki, TrkCrvSector const& g, KinKal::VEC3 const& point, double time) {
if(std::fabs(ki.momentum3().Unit().Dot(g.w)) < trkCrvMinCosToNormal) return KinKal::PMAT();
return ki.kinematicLine().planeCovariance(time, KinKal::Plane(g.w, g.u, point), trkCrvMinCosToNormal);
}

// Covariance of the track position at a KalSeed intersection with the sector, in the sector axes (u, v): the KinKal projected
// covariance of the intersection (filled by the extrapolation, rotated from the intersection's local plane axes) if present,
// otherwise that of the intersection state measured in the plane.
inline KinKal::PMAT trkCrvXingCovariance(KalIntersection const& ki, TrkCrvSector const& g) {
auto const& inter = ki.intersection();
if(!inter.hascov_) return trkCrvPlaneCovariance(ki, g, ki.position3(), ki.time());
ROOT::Math::SMatrix<double,2,2> rot;
KinKal::VEC3 const* axes[2] = {&g.u, &g.v};
KinKal::VEC3 const* idirs[2] = {&inter.udir_, &inter.vdir_};
for(int i = 0; i < 2; ++i) for(int j = 0; j < 2; ++j) rot(i, j) = axes[i]->Dot(*idirs[j]);
return ROOT::Math::Similarity(rot, inter.pcov_);
}
}
#endif
6 changes: 2 additions & 4 deletions CRVReco/inc/TrkCrvMatchTool.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace mu2e {
fhicl::Sequence<double> dtXingWindow{Name("DtXingWindow"), Comment("storage window on t_cluster - t_track at the cluster plane [ns]")};
fhicl::Atom<double> maxDv{Name("MaxDv"), Comment("storage window on |across-bar residual| [mm]")};
fhicl::Atom<double> maxDu{Name("MaxDu"), Comment("storage window on |along-bar residual| for two-ended clusters [mm]")};
fhicl::Atom<bool> doGeometric{Name("DoGeometric"), Comment("form geometric candidates from CRV KalIntersections, or, for a line fit with none, from its projections onto the sectors' extended planes")};
fhicl::Atom<bool> doGeometric{Name("DoGeometric"), Comment("form geometric candidates from CRV KalIntersections, including the out-of-bounds near misses the extrapolation records")};
fhicl::Atom<bool> requireSameSector{Name("RequireSameSector"), Comment("project only from a crossing of the cluster's own sector (A/B tests)"), false};
fhicl::Sequence<double> sigmaV{Name("SigmaV"), Comment("CRV across-bar resolution [mm], indexed by the cluster's sector type (1 first): one entry per sector type of the loaded CRV geometry, or one entry for all; sector type numbering depends on the geometry")};
fhicl::Sequence<double> sigmaU{Name("SigmaU"), Comment("CRV along-bar resolution of two-ended clusters [mm], by sector type")};
Expand Down Expand Up @@ -80,10 +80,8 @@ namespace mu2e {
};
double resolution(std::vector<double> const& tab, int sectorType) const;
void checkResolutions() const; // throws if a Sigma* list does not describe the loaded CRV geometry's sector types
TrkCrvPlaneCov errorModel(ROOT::Math::SMatrix<double,2,2,ROOT::Math::MatRepSym<double,2>> const& cov) const; // k^2 C + a^2 I
TrkCrvPlaneCov errorModel(KinKal::PMAT const& cov) const; // k^2 C + a^2 I
void probe(TrkCrvXing& x, double speed) const;
// a straight-line fit's crossing of a sector's extended plane, if out of bounds; false if in bounds or parallel
bool projectXing(KalSeed const& ks, int shield, double sigT, TrkCrvXing& x) const;
static bool sameCluster(art::Ptr<CrvCoincidenceCluster> const& a, art::Ptr<CrvCoincidenceCluster> const& b);

std::vector<SurfaceId> refSurfaces_;
Expand Down
45 changes: 0 additions & 45 deletions CRVReco/inc/TrkCrvPlaneError.hh

This file was deleted.

84 changes: 7 additions & 77 deletions CRVReco/src/TrkCrvMatchTool.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "Offline/CRVReco/inc/TrkCrvMatchTool.hh"
#include "Offline/CRVReco/inc/TrkCrvPlaneError.hh"
#include "Offline/CosmicRayShieldGeom/inc/CosmicRayShield.hh"
#include "Offline/GeometryService/inc/DetectorSystem.hh"
#include "Offline/GeometryService/inc/GeomHandle.hh"
Expand Down Expand Up @@ -29,7 +28,7 @@ namespace mu2e {
for(auto const& s : conf.refSurfaces()) refSurfaces_.emplace_back(s);
}

TrkCrvPlaneCov TrkCrvMatchTool::errorModel(TrkCrvCov2 const& cov) const {
TrkCrvPlaneCov TrkCrvMatchTool::errorModel(KinKal::PMAT const& cov) const {
TrkCrvPlaneCov out;
for(int i = 0; i < 2; ++i)
for(int j = i; j < 2; ++j) out(i, j) = trkErrScale_ * trkErrScale_ * cov(i, j) + (i == j ? posErrQuad_ * posErrQuad_ : 0.0);
Expand Down Expand Up @@ -182,44 +181,6 @@ namespace mu2e {
}
}

bool TrkCrvMatchTool::projectXing(KalSeed const& ks, int shield, double sigT, TrkCrvXing& x) const {
auto const& g = sectors_[shield];
// the straight line from the KalIntersection nearest in path to the sector's extended mid-plane
int kbest = -1;
double sbest = 0;
for(size_t k = 0; k < ks.intersections().size(); ++k) {
auto const& ki = ks.intersections()[k];
double dn = ki.momentum3().Unit().Dot(g.w);
if(std::fabs(dn) < trkCrvMinCosToNormal) continue;
double s = (g.center - ki.position3()).Dot(g.w) / dn;
if(kbest < 0 || std::fabs(s) < std::fabs(sbest)) { kbest = static_cast<int>(k); sbest = s; }
}
if(kbest < 0) return false;
auto const& ki = ks.intersections()[kbest];
auto d = ki.momentum3().Unit();
KinKal::VEC3 p = ki.position3() + sbest * d;
KinKal::VEC3 loc = p - g.center;
double dEdgeU = g.uHalf - std::fabs(loc.Dot(g.u)), dEdgeV = g.vHalf - std::fabs(loc.Dot(g.v));
if(dEdgeU >= 0 && dEdgeV >= 0) return false; // in-bounds crossings belong to the extrapolation's intersections
double speed = std::sqrt(ki.velocity().Mag2());
x.sid_ = g.sid;
x.inter_ = static_cast<int16_t>(kbest);
x.sector_ = static_cast<int16_t>(shield);
x.pos_ = XYZVectorF(p.X(), p.Y(), p.Z());
x.dir_ = XYZVectorF(d.X(), d.Y(), d.Z());
x.time_ = ki.time() + (speed > 0 ? sbest / speed : 0.0);
x.inBounds_ = false;
x.cov_ = errorModel(trkCrvPlaneCovariance(ki.pstate_.stateCovariance(), ki.momentum3(), g.u, g.v, g.w, sbest));
x.sigT_ = sigT;
x.mom_ = ki.mom();
x.momErr_ = ki.momerr();
x.dEdgeU_ = dEdgeU;
x.dEdgeV_ = dEdgeV;
x.edgeSig_ = (x.sigU() > 0 && x.sigV() > 0) ? std::min(x.dEdgeU_ / x.sigU(), x.dEdgeV_ / x.sigV()) : 0.f;
if(layerProbe_) probe(x, speed);
return true;
}

TrkCrvMatch TrkCrvMatchTool::match(art::Ptr<KalSeed> const& seed, std::vector<art::Ptr<CrvCoincidenceCluster>> const& usedInFit) const {
auto const& ks = *seed;
TrkCrvMatch m;
Expand Down Expand Up @@ -277,8 +238,9 @@ namespace mu2e {
x.pos_ = XYZVectorF(p.X(), p.Y(), p.Z());
x.dir_ = XYZVectorF(d.X(), d.Y(), d.Z());
x.time_ = ki.time();
x.inBounds_ = ki.inBounds();
x.cov_ = errorModel(trkCrvPlaneCovariance(ki.pstate_.stateCovariance(), ki.momentum3(), g.u, g.v, g.w, 0.0));
x.inBounds_ = ki.inBounds(); // false for a near miss: an out-of-bounds crossing the extrapolation kept for its bounds fraction
x.inbFrac_ = ki.intersection().hascov_ ? ki.intersection().inbfrac_ : (x.inBounds_ ? 1.0f : 0.0f);
x.cov_ = errorModel(trkCrvXingCovariance(ki, g));
x.sigT_ = sigT;
x.mom_ = ki.mom();
x.momErr_ = ki.momerr();
Expand All @@ -290,22 +252,6 @@ namespace mu2e {
m.xings_.push_back(x);
}
}
// a straight-line fit with no CRV crossing passes outside every sector: project it onto the extended plane of each
// cluster's sector, so that edge clusters get geometric candidates
bool projectLine = doGeometric_ && m.xings_.empty() && ks.kinematicLineFit();
std::map<int,int16_t> projected; // cluster shield -> its projected crossing in m.xings_
if(projectLine) {
std::set<int> shields;
for(auto const& ci : cinfo_)
if(ci.shield >= 0 && static_cast<size_t>(ci.shield) < sectors_.size() && sectors_[ci.shield].valid) shields.insert(ci.shield);
for(int s : shields) {
TrkCrvXing x;
if(!projectXing(ks, s, sigT, x)) continue;
projected[s] = static_cast<int16_t>(m.xings_.size());
m.xings_.push_back(x);
}
}

// candidates
for(size_t ic = 0; ic < clusters_.size(); ++ic) {
auto const& ci = cinfo_[ic];
Expand All @@ -321,10 +267,7 @@ namespace mu2e {
// geometric evidence: project from the CRV crossing nearest in path to the cluster's plane (straight line outside the field)
int bestx = -1; double bds = 1e30;
bool hasPlane = ci.shield >= 0 && static_cast<size_t>(ci.shield) < sectors_.size() && sectors_[ci.shield].valid;
if(hasPlane && projectLine) { // a projected line: from the projection onto the cluster's own sector
auto ip = projected.find(ci.shield);
if(ip != projected.end()) bestx = ip->second;
} else if(hasPlane) {
if(hasPlane) {
auto const& g = sectors_[ci.shield];
for(size_t k = 0; k < m.xings_.size(); ++k) {
if(requireSameSector_ && m.xings_[k].sector_ != ci.shield) continue;
Expand All @@ -350,10 +293,8 @@ namespace mu2e {
c.du_ = r.Dot(g.u);
c.dv_ = r.Dot(g.v);
c.dtXing_ = ci.time - tp;
// track covariance on the cluster's plane: the reference crossing's covariance carried over the straight step;
// a projected crossing carries that of the intersection it was projected from, over the step from there
double dsCov = ki.surfaceId() == x.sid_ ? ds : (ci.pos - ki.position3()).Dot(g.w) / dn;
c.cov_ = errorModel(trkCrvPlaneCovariance(ki.pstate_.stateCovariance(), ki.momentum3(), g.u, g.v, g.w, dsCov));
// track covariance on the cluster's plane: the reference crossing's covariance carried over the straight step
c.cov_ = errorModel(trkCrvPlaneCovariance(ki, g, pp, tp));
double sv = resolution(sigmaV_, ci.sectorType), su = resolution(sigmaU_, ci.sectorType);
double st = ci.singleEnded ? sigmaTSingle_ : resolution(sigmaT_, ci.sectorType);
c.chi2_ = c.dtXing_*c.dtXing_/(st*st + x.sigT_*x.sigT_);
Expand Down Expand Up @@ -381,17 +322,6 @@ namespace mu2e {
m.cands_.push_back(c);
}
}
if(!projected.empty()) { // keep only the projected crossings a geometric candidate refers to, in sector order
std::vector<bool> used(m.xings_.size(), false);
for(auto const& c : m.cands_) if(c.xing_ >= 0) used[c.xing_] = true;
std::vector<int16_t> remap(m.xings_.size(), -1);
std::vector<TrkCrvXing> kept;
for(size_t k = 0; k < m.xings_.size(); ++k)
if(used[k]) { remap[k] = static_cast<int16_t>(kept.size()); kept.push_back(std::move(m.xings_[k])); }
m.xings_ = std::move(kept);
for(auto& c : m.cands_) if(c.xing_ >= 0) c.xing_ = remap[c.xing_];
}

// rank: geometric by chi2, then time-only by distance from the time centre
std::stable_sort(m.cands_.begin(), m.cands_.end(), [&](TrkCrvCandidate const& a, TrkCrvCandidate const& b) {
bool ga = a.hasFlag(TrkCrvCandidate::geomCand), gb = b.hasFlag(TrkCrvCandidate::geomCand);
Expand Down
5 changes: 2 additions & 3 deletions CRVReco/src/TrkCrvMatcher_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
// is by default the track's entry into the tracker (earliest of TT_Front, TT_Back, TT_Outer);
// - space: from the track's CRV crossing nearest to the cluster's sector plane, a straight-line projection onto that
// plane gives du, dv and dtXing, and a space+time chi2 with the full (u, v) covariance; clusters inside MaxDu,
// MaxDv and DtXingWindow are geometric candidates; a straight-line fit with no CRV crossing (its line passes
// outside every sector) is projected instead from its intersection nearest the cluster's sector onto that
// sector's extended plane, recorded as a crossing with inBounds false;
// MaxDv and DtXingWindow are geometric candidates. The crossings include the out-of-bounds near misses the
// extrapolation records (KKExtrap NearMissFraction), as crossings with inBounds false;
// - every candidate is kept: a track can have several, and a cluster can be a candidate of several tracks. They are
// ranked by chi2, then by time, with bestGeom and bestTime per track and bestInSector per sector; across the
// collection nShare, bestForCluster and a greedy one-to-one 'exclusive' assignment resolve shared clusters.
Expand Down
Loading