From 61bd31663ab6746c32bcf615badc9661a085cf54 Mon Sep 17 00:00:00 2001 From: Rob Mina Date: Fri, 6 Nov 2026 13:42:28 -0600 Subject: [PATCH] Truncate trajectory for ranged-out particles. --- Fit/Domain.hh | 1 + Fit/Track.hh | 45 +++++++ Tests/CMakeLists.txt | 1 + Tests/Truncate_unit.cc | 295 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 342 insertions(+) create mode 100644 Tests/Truncate_unit.cc diff --git a/Fit/Domain.hh b/Fit/Domain.hh index f3c4cde6..13e22fbd 100644 --- a/Fit/Domain.hh +++ b/Fit/Domain.hh @@ -24,6 +24,7 @@ namespace KinKal { double mid() const { return range_.mid();} auto const& bnom() const { return bnom_; } void updateBNom( VEC3 const& bnom) { bnom_ = bnom; }; // used in DomainWall updating + void setEdge(TimeDir tdir, double time) { range_.extend(time,tdir); } // move the end (forwards) or begin (backwards) to time; used to truncate a track's outermost domain private: TimeRange range_; // range of this domain VEC3 bnom_; // nominal BField for this domain diff --git a/Fit/Track.hh b/Fit/Track.hh index 00e92d60..221efb2c 100644 --- a/Fit/Track.hh +++ b/Fit/Track.hh @@ -114,6 +114,11 @@ namespace KinKal { template bool extrapolate(TimeDir tdir, XTEST const& XTest); // extrapolate the fit through a material xing. Return value is true if the effect is added and fit updated. bool extrapolate(EXINGPTR const& exing,TimeDir const& tdir); + // end the fit trajectory at time in direction tdir, e.g. where an extrapolation finds the particle stops. The domains beyond time are + // removed with the DomainWalls into them, and the domain containing time ends there. Requires a usable fit, time inside the trajectory + // and no active hit or material effect at or beyond time (inactive ones are kept, each with its own reference piece); otherwise + // nothing changes and the return value is false. + bool truncate(TimeDir tdir, double time); // accessors std::vector const& history() const { return history_; } Status const& fitStatus() const { return history_.back(); } // most recent status @@ -1076,6 +1081,46 @@ namespace KinKal { } } + template bool Track::truncate(TimeDir tdir, double time) { + if(!hasTraj() || !fitStatus().usable() || !std::isfinite(time)) return false; + bool forwards = tdir == TimeDir::forwards; + auto trange = fittraj_->range(); + if(time <= trange.begin() || time >= trange.end()) return false; + auto beyond = [forwards,time](double t){ return forwards ? t >= time : t <= time; }; + // every active hit or material effect must stay on the kept part, so no piece the fit uses is removed or shortened past it. + // Inactive effects change nothing in the fit, and each hit or crossing holds its own reference piece, so they may lie beyond + // time. DomainWalls only link the field domains: those beyond time go with their domains + for(auto const& eff : effects_) + if(eff->active() && beyond(eff->time()) && dynamic_cast(eff.get()) == nullptr) return false; + if(!domains_.empty()){ + auto removed = [forwards,time](DOMAINPTR const& dom){ return forwards ? dom->begin() >= time : dom->end() <= time; }; + // the domain containing time is kept, so at least one domain remains + if(std::all_of(domains_.begin(),domains_.end(),removed)) return false; + effects_.remove_if([&removed,forwards](std::unique_ptr const& eff){ + auto dw = dynamic_cast(eff.get()); + return dw != nullptr && removed(forwards ? dw->nextPtr() : dw->prevPtr()); }); + for(auto idom = domains_.begin(); idom != domains_.end();) idom = removed(*idom) ? domains_.erase(idom) : std::next(idom); + auto const& outer = forwards ? *domains_.crbegin() : *domains_.cbegin(); + if(forwards ? outer->end() > time : outer->begin() < time) outer->setEdge(tdir,time); + } + if(forwards){ + fittraj_->setRange(TimeRange(trange.begin(),time),true); + } else { + // setRange(range,true) keeps the last piece before the range at the front, so the pieces that end at or before time are + // dropped here: the trajectory is rebuilt, in place, from the first piece reaching beyond time (the pieces are shared) + auto const& pieces = fittraj_->pieces(); + size_t first(0); + while(pieces[first]->range().end() <= time) ++first; // time < trange.end(), so a piece reaches beyond it + if(first > 0){ + PKTRAJ kept; + for(size_t ipiece = first; ipiece < pieces.size(); ++ipiece) kept.add(pieces[ipiece],TimeDir::forwards); + *fittraj_ = kept; + } + fittraj_->setRange(TimeRange(time,trange.end())); + } + return true; + } + template bool Track::extrapolationFailed(Status::status stat, const char* comment) { history_.emplace_back(0,0,stat,comment); return false; diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index f874658d..030631eb 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -28,6 +28,7 @@ set( TEST_SOURCE_FILES Geometry_unit.cc Intersection_unit.cc MatEnv_unit.cc + Truncate_unit.cc ) # Generate unit test targets diff --git a/Tests/Truncate_unit.cc b/Tests/Truncate_unit.cc new file mode 100644 index 00000000..942c6441 --- /dev/null +++ b/Tests/Truncate_unit.cc @@ -0,0 +1,295 @@ +// +// Test Track::truncate: fit a ToyMC track, extrapolate it past its fitted range in each direction, truncate it inside +// the outermost extrapolated domain and, after extrapolating again, halfway back to the last active hit or material (with +// field correction this removes domains and their DomainWalls), and check the trajectory, the domains, the effects and a +// later extrapolation. Then make the last hits of a track inactive and check that a cut before them, but after every +// active hit and material, is accepted. +// +#include "KinKal/Trajectory/LoopHelix.hh" +#include "KinKal/Trajectory/CentralHelix.hh" +#include "KinKal/Trajectory/ParticleTrajectory.hh" +#include "KinKal/General/BFieldMap.hh" +#include "KinKal/Fit/Config.hh" +#include "KinKal/Fit/Track.hh" +#include "KinKal/Examples/StrawXing.hh" +#include "KinKal/Examples/SimpleWireHit.hh" +#include "KinKal/Fit/Measurement.hh" +#include "KinKal/Fit/DomainWall.hh" +#include "KinKal/Tests/ToyMC.hh" +#include +#include +#include +#include +#include +#include + +using namespace KinKal; + +namespace { + // extend in steps of step_ until the trajectory reaches tstop_ in the requested direction + struct ReachTime { + double tstop_; + double step_ = 1.0; + template bool needsExtrapolation(ParticleTrajectory const& ptraj, TimeDir tdir) const { + return tdir == TimeDir::forwards ? ptraj.range().end() < tstop_ : ptraj.range().begin() > tstop_; + } + double maxDt() const { return 1000.0; } + double maxDtStep() const { return step_; } + double dpTolerance() const { return 1.0e-4; } + }; + + int nfail(0); + void check(bool ok, std::string const& what) { + if(!ok){ ++nfail; std::cout << "FAILED: " << what << std::endl; } + } + + template bool contiguous(Track const& trk) { + double prev = std::nan(""); + for(auto const& dom : trk.domains()){ + if(!std::isnan(prev) && dom->begin() != prev) return false; + prev = dom->end(); + } + return true; + } + + template bool isWall(std::unique_ptr> const& eff) { + return dynamic_cast const*>(eff.get()) != nullptr; + } + + // the outermost time of the active hits and materials (DomainWalls excluded): a cut must stay beyond it + template double lastActive(Track const& trk, bool fwd) { + double edge = fwd ? trk.fitTraj().range().begin() : trk.fitTraj().range().end(); + for(auto const& eff : trk.effects()) + if(eff->active() && !isWall(eff)) edge = fwd ? std::max(edge,eff->time()) : std::min(edge,eff->time()); + return edge; + } + + // truncate at cut and check the trajectory, the domains beyond the cut (removed with the DomainWalls into them), the effects and the state + template void checkCut(Track& trk, TimeDir tdir, double cut, std::string const& label) { + bool fwd = tdir == TimeDir::forwards; + auto const& ftraj = trk.fitTraj(); + auto before = ftraj.range(); + auto removed = [fwd,cut](Domain const& dom){ return fwd ? dom.begin() >= cut : dom.end() <= cut; }; + size_t npieces = ftraj.pieces().size(), neff = trk.effects().size(), ndom = trk.domains().size(), ndomcut(0), nwallcut(0); + for(auto const& dom : trk.domains()) ndomcut += removed(*dom); + for(auto const& eff : trk.effects()){ + auto dw = dynamic_cast const*>(eff.get()); + if(dw != nullptr) nwallcut += removed(fwd ? dw->nextDomain() : dw->prevDomain()); + } + auto pos = ftraj.position3(cut); + auto mom = ftraj.momentum3(cut); + auto stat = trk.fitStatus().status_; + check(trk.truncate(tdir,cut),label + " truncation"); + check((fwd ? ftraj.range().end() : ftraj.range().begin()) == cut,label + " trajectory ends at the cut"); + check((fwd ? ftraj.range().begin() : ftraj.range().end()) == (fwd ? before.begin() : before.end()),label + " other end unchanged"); + check(ftraj.pieces().size() <= npieces,label + " no pieces added"); + check(nwallcut == ndomcut,label + " one DomainWall into each domain beyond the cut"); + check(trk.effects().size() == neff - nwallcut && trk.domains().size() == ndom - ndomcut,label + " only the domains beyond the cut and their DomainWalls removed"); + for(auto const& eff : trk.effects()){ + auto dw = dynamic_cast const*>(eff.get()); + if(dw != nullptr) check(!removed(fwd ? dw->nextDomain() : dw->prevDomain()),label + " no DomainWall into a removed domain"); + } + check((ftraj.position3(cut) - pos).R() == 0.0 && (ftraj.momentum3(cut) - mom).R() == 0.0,label + " state at the cut unchanged"); + check(trk.fitStatus().status_ == stat,label + " fit status unchanged"); + if(!trk.domains().empty()){ + auto const& outer = fwd ? *trk.domains().crbegin() : *trk.domains().cbegin(); + check(fwd ? outer->end() <= cut : outer->begin() >= cut,label + " no domain beyond the cut"); + check(contiguous(trk),label + " domains contiguous after truncation"); + } + } + + template void testDirection(Track& trk, TimeDir tdir, std::string const& label, bool bfcorr) { + bool fwd = tdir == TimeDir::forwards; + auto const& ftraj = trk.fitTraj(); + double tend = fwd ? ftraj.range().end() : ftraj.range().begin(); + ReachTime xtest{fwd ? tend + 20.0 : tend - 20.0}; + check(trk.extrapolate(tdir,xtest),label + " extrapolation"); + tend = fwd ? ftraj.range().end() : ftraj.range().begin(); + check(fwd ? tend >= xtest.tstop_ : tend <= xtest.tstop_,label + " extrapolation reaches the target time"); + if(bfcorr) check(!trk.domains().empty(),label + " extrapolation adds domains"); + double edge = lastActive(trk,fwd); + // refused: at the last active hit or material, outside the trajectory, non-finite + auto before = ftraj.range(); + size_t neff = trk.effects().size(), ndom = trk.domains().size(); + check(!trk.truncate(tdir,edge),label + " refuses a cut at the last active effect"); + check(!trk.truncate(tdir,fwd ? tend + 1.0 : tend - 1.0),label + " refuses a cut outside the trajectory"); + check(!trk.truncate(tdir,std::nan("")),label + " refuses a non-finite cut"); + check(ftraj.range().begin() == before.begin() && ftraj.range().end() == before.end() && trk.effects().size() == neff && trk.domains().size() == ndom, + label + " refused cuts change nothing"); + // 1: inside the outermost domain (no domain removed), else halfway to the end + double cut = 0.5*(edge + tend); + if(!trk.domains().empty()){ + auto const& outer = fwd ? *trk.domains().crbegin() : *trk.domains().cbegin(); + if(fwd ? (outer->begin() >= edge && outer->end() > outer->begin()) : (outer->end() <= edge && outer->end() > outer->begin())) + cut = std::clamp(outer->mid(),std::min(edge,tend),std::max(edge,tend)); + } + checkCut(trk,tdir,cut,label + " outermost domain"); + // 2: extrapolate again, then cut halfway between the last active hit or material and the end: with field correction this + // removes several domains + check(trk.extrapolate(tdir,xtest),label + " extrapolation after truncation"); + check(fwd ? ftraj.range().end() >= xtest.tstop_ : ftraj.range().begin() <= xtest.tstop_,label + " re-extrapolation reaches the target time"); + check(contiguous(trk),label + " domains contiguous after re-extrapolation"); + tend = fwd ? ftraj.range().end() : ftraj.range().begin(); + cut = 0.5*(edge + tend); + if(bfcorr){ + size_t nbeyond(0); + for(auto const& dom : trk.domains()) nbeyond += fwd ? dom->begin() >= cut : dom->end() <= cut; + check(nbeyond >= 2,label + " several domains beyond the halfway cut"); + } + checkCut(trk,tdir,cut,label + " halfway"); + // a later extrapolation continues from the cut + check(trk.extrapolate(tdir,xtest),label + " extrapolation after the second truncation"); + check(fwd ? ftraj.range().end() >= xtest.tstop_ : ftraj.range().begin() <= xtest.tstop_,label + " second re-extrapolation reaches the target time"); + check(contiguous(trk),label + " domains contiguous after the second re-extrapolation"); + } + + // the fit configuration of the Tests schedule files (MCAmbig.txt, MCAmbigFixedField.txt): convergence and divergence + // limits loose enough for a seed smeared by 10 sigma, no end processing + Config fitConfig(bool bfcorr) { + Config config; + config.maxniter_ = 10; + config.dwt_ = 1.0e6; + config.convdchisq_ = 1.0; + config.divdchisq_ = 1.0e8; + config.pdchisq_ = 1.0e8; + config.divgap_ = 100.0; + config.tol_ = 1.0e-4; + config.minndof_ = 5; + config.bfcorr_ = bfcorr; + config.ends_ = false; + for(double temp : {2.0,1.0,0.0}){ + MetaIterConfig miconfig(temp); + miconfig.addUpdater(StrawXingConfig(0.3,5.0,10.0,true)); + config.schedule_.push_back(miconfig); + } + return config; + } + + template void testTraj(DVEC const& sigmas, bool bfcorr) { + std::string label = KTRAJ::trajName() + (bfcorr ? " bfcorr" : " no bfcorr"); + // the uniform field of the Tests fit units: the ToyMC fits diverge in a gradient field (LoopHelixFit --Bgrad, KinKal v3.9.1); + // with bfcorr the extrapolation still adds maxDtStep domains, so the domain truncation is exercised + UniformBFieldMap bfield(VEC3(0.0,0.0,1.0)); + KKTest::ToyMC toy(bfield,105.0,-1,3000.0,123421,40,true,true,0.25,0.511); + toy.setTolerance(1.0e-5); + Config config = fitConfig(bfcorr); + // a few events, so a seed whose fit fails does not decide the test + unsigned ntested(0); + for(unsigned ievt=0; ievt < 10 && ntested < 3; ++ievt){ + typename KKTest::ToyMC::HITCOL hits; + typename KKTest::ToyMC::EXINGCOL xings; + ParticleTrajectory tptraj; + toy.simulateParticle(tptraj,hits,xings,true); + double tmid = tptraj.range().mid(); + auto const& midhel = tptraj.nearestPiece(tmid); + auto seedmom = midhel.momentum4(tmid); + auto seedpos = midhel.position4(tmid); + KTRAJ seed(seedpos,seedmom,midhel.charge(),bfield.fieldVect(seedpos.Vect()),TimeRange(hits.front()->time(),hits.back()->time())); + toy.createSeed(seed,sigmas,10.0); + Track trk(config,bfield,seed,hits,xings); + if(!trk.fitStatus().usable()){ + std::cout << label << " event " << ievt << " skipped, fit not usable: " << trk.fitStatus() << std::endl; + continue; + } + ++ntested; + std::string elabel = label + " event " + std::to_string(ievt); + testDirection(trk,TimeDir::forwards,elabel + " forwards",bfcorr); + testDirection(trk,TimeDir::backwards,elabel + " backwards",bfcorr); + } + check(ntested == 3,label + " has 3 usable fits in 10 events"); + std::cout << label << ": " << ntested << " fits tested" << std::endl; + } + // The last NINACTIVE hits of a ToyMC track (no scintillator hit) are made inactive, and the straw crossings after the + // last active hit are dropped, so only inactive effects follow it. After one long extrapolation step past the inactive + // hits, a cut after every active hit and material but before an inactive hit must be accepted, + // keep the inactive effects with usable reference pieces, and a cut at the last active effect must still be refused. + template void testInactive(DVEC const& sigmas, bool bfcorr) { + using WIREHIT = SimpleWireHit; + static const size_t NINACTIVE = 4; + std::string label = KTRAJ::trajName() + (bfcorr ? " bfcorr" : " no bfcorr") + " inactive hits"; + UniformBFieldMap bfield(VEC3(0.0,0.0,1.0)); + KKTest::ToyMC toy(bfield,105.0,-1,3000.0,123421,40,true,false,0.25,0.511); + toy.setTolerance(1.0e-5); + Config config = fitConfig(bfcorr); + unsigned ntested(0); + for(unsigned ievt=0; ievt < 10 && ntested < 3; ++ievt){ + typename KKTest::ToyMC::HITCOL hits; + typename KKTest::ToyMC::EXINGCOL xings; + ParticleTrajectory tptraj; + toy.simulateParticle(tptraj,hits,xings,true); + if(hits.size() < 10) continue; + std::sort(hits.begin(),hits.end(),[](auto const& a, auto const& b){ return a->time() < b->time(); }); + double tactive = hits[hits.size()-NINACTIVE-1]->time(); + bool wire(true); + for(size_t ih = hits.size()-NINACTIVE; ih < hits.size(); ++ih){ + auto whit = std::dynamic_pointer_cast(hits[ih]); + if(!whit){ wire = false; break; } + // the same wire, drift and TOT, in the inactive state + typename WIREHIT::PCA pca(tptraj,whit->wire(),CAHint(whit->time(),whit->time()),whit->precision()); + hits[ih] = std::make_shared(bfield,pca,WireHitState(WireHitState::inactive),whit->minDOCA(),whit->driftVelocity(), + whit->timeVariance(),whit->tot(),whit->totVariance(),whit->cellRadius(),whit->id()); + } + if(!wire) continue; + xings.erase(std::remove_if(xings.begin(),xings.end(),[tactive](auto const& xing){ return xing->time() > tactive; }),xings.end()); + double tmid = tptraj.range().mid(); + auto const& midhel = tptraj.nearestPiece(tmid); + auto seedpos = midhel.position4(tmid); + KTRAJ seed(seedpos,midhel.momentum4(tmid),midhel.charge(),bfield.fieldVect(seedpos.Vect()),TimeRange(hits.front()->time(),tactive)); + toy.createSeed(seed,sigmas,10.0); + Track trk(config,bfield,seed,hits,xings); + std::string elabel = label + " event " + std::to_string(ievt); + if(!trk.fitStatus().usable()){ + std::cout << elabel << " skipped, fit not usable: " << trk.fitStatus() << std::endl; + continue; + } + ++ntested; + auto const& ftraj = trk.fitTraj(); + ReachTime xtest{hits.back()->time() + 5.0, 50.0}; + check(trk.extrapolate(TimeDir::forwards,xtest),elabel + " extrapolation past the inactive hits"); + check(ftraj.range().end() > hits.back()->time(),elabel + " the trajectory reaches past the inactive hits"); + // after the extrapolation: the last active hit or material (DomainWalls do not block a cut) and the inactive hits + double lastactive = lastActive(trk,true); + std::vector tinact; + for(auto const& eff : trk.effects()) + if(!eff->active() && dynamic_cast const*>(eff.get()) != nullptr) tinact.push_back(eff->time()); + check(tinact.size() == NINACTIVE,elabel + " the inactive hits are effects of the fit"); + std::sort(tinact.begin(),tinact.end()); + auto next = std::upper_bound(tinact.begin(),tinact.end(),lastactive); + if(next == tinact.end()){ + check(false,elabel + " an inactive hit lies beyond every active hit and material"); + std::cout << " last active hit or material " << lastactive << " last active hit " << tactive + << " inactive hits " << (tinact.empty() ? 0.0 : tinact.front()) << " .. " << (tinact.empty() ? 0.0 : tinact.back()) << std::endl; + continue; + } + double cut = 0.5*(lastactive + *next); + size_t neff = trk.effects().size(); + check(!trk.truncate(TimeDir::forwards,lastactive),elabel + " refuses a cut at the last active effect"); + check(trk.truncate(TimeDir::forwards,cut),elabel + " truncation before the inactive hits"); + check(ftraj.range().end() == cut,elabel + " trajectory ends at the cut"); + check(trk.effects().size() == neff,elabel + " effects kept"); + for(auto const& eff : trk.effects()){ + if(eff->active() || eff->time() <= cut) continue; + auto meas = dynamic_cast const*>(eff.get()); + check(meas != nullptr && meas->hit()->refTrajPtr() && std::isfinite(meas->hit()->referenceTrajectory().momentum(eff->time())), + elabel + " an inactive hit beyond the cut keeps a usable reference piece"); + } + if(!trk.domains().empty()) check((*trk.domains().crbegin())->end() <= cut && contiguous(trk),elabel + " domains end at the cut"); + check(trk.fitStatus().usable(),elabel + " fit status still usable"); + } + check(ntested == 3,label + " has 3 usable fits in 10 events"); + std::cout << label << ": " << ntested << " fits tested" << std::endl; + } +} + +int main(int, char**) { + testTraj(DVEC(0.5, 0.5, 0.5, 0.5, 0.005, 0.5),true); + testTraj(DVEC(0.5, 0.5, 0.5, 0.5, 0.005, 0.5),false); + testTraj(DVEC(0.5, 0.003, 0.00001, 3.0 , 0.004, 0.1),true); + testTraj(DVEC(0.5, 0.003, 0.00001, 3.0 , 0.004, 0.1),false); + testInactive(DVEC(0.5, 0.5, 0.5, 0.5, 0.005, 0.5),true); + testInactive(DVEC(0.5, 0.5, 0.5, 0.5, 0.005, 0.5),false); + testInactive(DVEC(0.5, 0.003, 0.00001, 3.0 , 0.004, 0.1),true); + testInactive(DVEC(0.5, 0.003, 0.00001, 3.0 , 0.004, 0.1),false); + std::cout << (nfail == 0 ? "Truncate test passed" : "Truncate test FAILED: " + std::to_string(nfail) + " checks") << std::endl; + return nfail == 0 ? 0 : 1; +}