Skip to content
Merged
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
4 changes: 3 additions & 1 deletion 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")};
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> 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; the last entry serves higher types); 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 @@ -81,6 +81,8 @@ namespace mu2e {
double resolution(std::vector<double> const& tab, int sectorType) const;
TrkCrvPlaneCov errorModel(ROOT::Math::SMatrix<double,2,2,ROOT::Math::MatRepSym<double,2>> 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
3 changes: 2 additions & 1 deletion CRVReco/inc/TrkCrvPlaneError.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace mu2e {
using TrkCrvCov2 = ROOT::Math::SMatrix<double,2,2,ROOT::Math::MatRepSym<double,2>>;
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 with normal w, in the in-plane axes (a, b), after a straight step of
// signed path length s from a state (position, momentum p) with 6x6 covariance C (position first). With d = p/|p|
Expand All @@ -23,7 +24,7 @@ namespace mu2e {
if(pm <= 0) return out;
KinKal::VEC3 d = p / pm;
double dw = d.Dot(w);
if(std::fabs(dw) < 1e-3) return out;
if(std::fabs(dw) < trkCrvMinCosToNormal) return out;
double g[2][6];
KinKal::VEC3 const* axes[2] = {&a, &b};
for(int k = 0; k < 2; ++k) {
Expand Down
76 changes: 72 additions & 4 deletions CRVReco/src/TrkCrvMatchTool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,44 @@ 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 @@ -227,6 +265,21 @@ 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) {
Expand All @@ -243,14 +296,17 @@ 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) {
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) {
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;
auto const& xk = m.xings_[k];
KinKal::VEC3 pk(xk.pos_.X(), xk.pos_.Y(), xk.pos_.Z()), dk(xk.dir_.X(), xk.dir_.Y(), xk.dir_.Z());
double dn = dk.Dot(g.w);
if(std::fabs(dn) < 1e-3) continue;
if(std::fabs(dn) < trkCrvMinCosToNormal) continue;
double ds = std::fabs((ci.pos - pk).Dot(g.w) / dn);
if(ds < bds) { bds = ds; bestx = static_cast<int>(k); }
}
Expand All @@ -269,8 +325,10 @@ 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
c.cov_ = errorModel(trkCrvPlaneCovariance(ki.pstate_.stateCovariance(), ki.momentum3(), g.u, g.v, g.w, ds));
// 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));
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 @@ -298,6 +356,16 @@ 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) {
Expand Down
4 changes: 3 additions & 1 deletion CRVReco/src/TrkCrvMatcher_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// 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;
// 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;
// - 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
29 changes: 18 additions & 11 deletions CRVResponse/src/TrkCrvMatchMCMaker_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ namespace mu2e {
unsigned trueCluBest = 0, trueCluProj = 0; // ... best in its sector; projected from another sector
double chi2nd = 0, chi2ndProj = 0; // sums of chi2/ndof of those geometric candidates
unsigned trueX = 0, trueFound = 0, trueMissed = 0, trueNoGeom = 0;
unsigned recoProj = 0, recoProjTrue = 0, trueMissedProj = 0; // projections of line fits with no CRV crossing
unsigned trueCluNoXing = 0; // own cluster geometric through such a projection
double chi2ndNoXing = 0; // ... their chi2/ndof sum, kept out of chi2nd
};
art::InputTag matchTag_, ksmcTag_, spTag_, trajTag_, stepTag_, ccmcTag_;
bool requireSteps_;
Expand Down Expand Up @@ -382,8 +385,9 @@ namespace mu2e {
bool crosses = false;
for(size_t ir = 0; ir < m.xings_.size(); ++ir) {
auto& c = counts_[m.xings_[ir].sector_];
++c.recoX;
int t = mc.recoXingTruth_[ir];
if(!m.xings_[ir].inBounds_) { ++c.recoProj; if(t >= 0) ++c.recoProjTrue; continue; } // not a crossing the extrapolation found
++c.recoX;
if(t < 0) { ++c.recoFake; if(m.xings_[ir].edgeSig_ < edgeSigCut_) ++c.recoFakeEdge; if(mc.recoXingNeighbor_[ir] >= 0) ++c.recoFakeNbr; }
else if(mc.xings_[t].rel_ != MCRelationship::same) ++c.recoTrueAnc;
else if(mc.xings_[t].edep_ > 0) ++c.recoTrueDep;
Expand All @@ -395,11 +399,12 @@ namespace mu2e {
if(!xm.geom_) { if(xm.edep_ > 0) ++c.trueNoGeom; continue; }
crosses = true;
++c.trueX;
if(xm.recoXing_ >= 0) ++c.trueFound;
if(xm.recoXing_ >= 0 && m.xings_[xm.recoXing_].inBounds_) ++c.trueFound;
else {
++c.trueMissed;
if(xm.recoXing_ >= 0) ++c.trueMissedProj;
for(auto const& rx : m.xings_)
if(rx.sector_ >= 0 && xm.sector_ >= 0 && planeGroup_[rx.sector_] >= 0 && planeGroup_[rx.sector_] == planeGroup_[xm.sector_]) { ++c.trueMissedNbr; break; }
if(rx.inBounds_ && rx.sector_ >= 0 && xm.sector_ >= 0 && planeGroup_[rx.sector_] >= 0 && planeGroup_[rx.sector_] == planeGroup_[xm.sector_]) { ++c.trueMissedNbr; break; }
}
}
for(auto const& xm : mc.xings_) { // is the particle's own cluster in each sector associated geometrically?
Expand All @@ -414,7 +419,8 @@ namespace mu2e {
excl = cand.hasFlag(TrkCrvCandidate::exclusive);
if(cand.hasFlag(TrkCrvCandidate::bestInSector)) ++c.trueCluBest;
double cn = cand.ndof_ > 0 ? cand.chi2_ / cand.ndof_ : 0.0;
c.chi2nd += cn;
if(cand.xing_ >= 0 && !m.xings_[cand.xing_].inBounds_) { ++c.trueCluNoXing; c.chi2ndNoXing += cn; }
else c.chi2nd += cn;
if(!cand.hasFlag(TrkCrvCandidate::sameSector)) { ++c.trueCluProj; c.chi2ndProj += cn; }
}
}
Expand Down Expand Up @@ -446,11 +452,11 @@ namespace mu2e {
auto const& rect = *sectors_[xm.sector_].rect;
auto const& rx = m.xings_[c.xing_];
auto const& ki = m.track_->intersections()[rx.inter_];
KinKal::VEC3 px(rx.pos_.X(), rx.pos_.Y(), rx.pos_.Z());
KinKal::VEC3 px = ki.position3(); // the state the error is carried from; a projection's crossing is already on the plane
KinKal::VEC3 mom = ki.momentum3(), d = mom.Unit();
KinKal::VEC3 const& w = rect.normal();
double dw = d.Dot(w);
if(std::fabs(dw) < 1e-3) continue;
if(std::fabs(dw) < trkCrvMinCosToNormal) continue;
double s = (rect.center() - px).Dot(w) / dw;
KinKal::VEC3 r = KinKal::VEC3(xm.pos_.X(), xm.pos_.Y(), xm.pos_.Z()) - (px + s * d);
auto const& cov = ki.pstate_.stateCovariance();
Expand All @@ -476,18 +482,19 @@ namespace mu2e {
log << "TrkCrvMatchMC summary (" << moduleDescription().moduleLabel() << "): tracks " << nTracks_ << ", with MC " << nTracksMC_
<< ", whose particle crosses a CRV sector " << nTracksCrossing_ << "\n"
<< std::setw(10) << "sector" << std::setw(8) << "reco" << std::setw(9) << "true:dep" << std::setw(9) << "true:gap"
<< std::setw(9) << "true:anc" << std::setw(7) << "fake" << std::setw(10) << "fake:edge" << std::setw(9) << "fake:nbr" << " |" << std::setw(7) << "truth" << std::setw(7) << "found"
<< std::setw(8) << "missed" << std::setw(9) << "miss:nbr" << std::setw(12) << "dep-no-geom";
<< std::setw(9) << "true:anc" << std::setw(7) << "fake" << std::setw(10) << "fake:edge" << std::setw(9) << "fake:nbr" << std::setw(6) << "proj" << std::setw(11) << "proj:true" << " |" << std::setw(7) << "truth" << std::setw(7) << "found"
<< std::setw(8) << "missed" << std::setw(9) << "miss:nbr" << std::setw(10) << "miss:proj" << std::setw(12) << "dep-no-geom";
for(auto const& [s, c] : counts_) {
std::string n = (s >= 0 && static_cast<size_t>(s) < shieldNames_.size()) ? shieldNames_[s] : std::to_string(s);
log << "\n" << std::setw(10) << n << std::setw(8) << c.recoX << std::setw(9) << c.recoTrueDep << std::setw(9) << c.recoTrueGap
<< std::setw(9) << c.recoTrueAnc << std::setw(7) << c.recoFake << std::setw(10) << c.recoFakeEdge << std::setw(9) << c.recoFakeNbr << " |" << std::setw(7) << c.trueX << std::setw(7) << c.trueFound
<< std::setw(8) << c.trueMissed << std::setw(9) << c.trueMissedNbr << std::setw(12) << c.trueNoGeom;
<< std::setw(9) << c.recoTrueAnc << std::setw(7) << c.recoFake << std::setw(10) << c.recoFakeEdge << std::setw(9) << c.recoFakeNbr << std::setw(6) << c.recoProj << std::setw(11) << c.recoProjTrue << " |" << std::setw(7) << c.trueX << std::setw(7) << c.trueFound
<< std::setw(8) << c.trueMissed << std::setw(9) << c.trueMissedNbr << std::setw(10) << c.trueMissedProj << std::setw(12) << c.trueNoGeom;
}
for(auto const& [s, c] : counts_) {
std::string n = (s >= 0 && static_cast<size_t>(s) < shieldNames_.size()) ? shieldNames_[s] : std::to_string(s);
log << "\nclusters " << n << " true " << c.trueClu << " geom " << c.trueCluGeom << " exclusive " << c.trueCluExcl
<< " best " << c.trueCluBest << " chi2nd " << c.chi2nd << " proj " << c.trueCluProj << " chi2ndProj " << c.chi2ndProj;
<< " best " << c.trueCluBest << " chi2nd " << c.chi2nd << " proj " << c.trueCluProj << " chi2ndProj " << c.chi2ndProj
<< " noxing " << c.trueCluNoXing << " chi2ndNoXing " << c.chi2ndNoXing;
}
log << "\nbest geometric candidate:";
for(int i = 0; i < nRel; ++i) log << " " << relName[i] << " " << bestGeomRel_[i];
Expand Down
11 changes: 6 additions & 5 deletions RecoDataProducts/inc/TrkCrvMatch.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,20 @@ namespace mu2e {
float uTdiv_ = NAN; // position along the bar from the two-end time difference [mm], NaN if one-ended
};

// one CRV sector crossing of the track's extrapolation (a CRV KalIntersection)
// one CRV sector crossing of the track's extrapolation (a CRV KalIntersection); for a straight-line fit with no CRV
// KalIntersection, the line's out-of-bounds crossing of the extended plane of a sector holding a candidate
struct TrkCrvXing {
SurfaceId sid_; // CRV sector surface (CRV_EX ... CRV_M8)
int16_t inter_ = -1; // index into KalSeed::intersections()
int16_t inter_ = -1; // index into KalSeed::intersections(): the crossing, or the intersection a projection starts from
int16_t sector_ = -1; // CRSScintillatorShield index
XYZVectorF pos_; // crossing position on the sector mid-plane, detector frame [mm]
XYZVectorF dir_; // unit direction of the track at the crossing
float time_ = 0; // crossing time [ns]
TrkCrvPlaneCov cov_; // track position covariance in the sector plane (u, v), error model applied [mm^2]
float sigT_ = 0; // track time error [ns]
float mom_ = 0; // track momentum magnitude at the crossing [MeV/c]
float mom_ = 0; // track momentum magnitude at the crossing, or at the starting intersection of a projection [MeV/c]
float momErr_ = 0; // its error [MeV/c]
bool inBounds_ = false; // crossing inside the sector rectangle
bool inBounds_ = false; // crossing inside the sector rectangle; false only for a projection, whose candidates run to larger chi2
float dEdgeU_ = 0; // distance inside the sector edge along the bars [mm], negative outside
float dEdgeV_ = 0; // distance inside the sector edge across the bars [mm], negative outside
float edgeSig_ = 0; // min(dEdgeU_/sigU(), dEdgeV_/sigV()): edge distance in units of the track error
Expand All @@ -74,7 +75,7 @@ namespace mu2e {
float dtXing_ = 0; // cluster avgHitTime - t_track at the cluster plane [ns]
float du_ = 0; // cluster - track along the bars, at the cluster plane [mm]
float dv_ = 0; // cluster - track across the bars, at the cluster plane [mm]
TrkCrvPlaneCov cov_; // track covariance of (du, dv), carried from the reference crossing over the step, error model applied [mm^2]
TrkCrvPlaneCov cov_; // track covariance of (du, dv), carried over the step from the reference crossing (from its starting intersection for a projection), error model applied [mm^2]
float chi2_ = -1; // space + time chi2 (geometric candidates only), with the full (u, v) covariance
uint8_t ndof_ = 0; // chi2 degrees of freedom: 2 for single-ended clusters (no along-bar term), else 3
uint8_t nShare_ = 0; // tracks of this collection holding this cluster as a candidate
Expand Down
3 changes: 3 additions & 0 deletions Validation/inc/ValTrkCrvMatch.hh
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ class ValTrkCrvMatch {
TH1D* _hNCand;
TH1D* _hNGeom;
TH1D* _hNXing;
TH1D* _hNXingProj;
TH1D* _hDtRef;
TH1D* _hDtRefBest;
TH1D* _hDtRefToF;
TH1D* _hChi2;
TH1D* _hChi2Proj;
TH1D* _hNShare;
TH1D* _hFlags;
TH1D* _hXingShield;
TH1D* _hXingGeomShield;
TH1D* _hXingProjShield;
TH2D* _hDtXing;
TH2D* _hDu;
TH2D* _hDv;
Expand Down
Loading