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
1 change: 1 addition & 0 deletions Fit/Domain.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions Fit/Track.hh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ namespace KinKal {
template <class XTEST> 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<Status> const& history() const { return history_; }
Status const& fitStatus() const { return history_.back(); } // most recent status
Expand Down Expand Up @@ -1076,6 +1081,46 @@ namespace KinKal {
}
}

template <class KTRAJ> bool Track<KTRAJ>::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<KKDW const*>(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<KKEFF> const& eff){
auto dw = dynamic_cast<KKDW const*>(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 <class KTRAJ> bool Track<KTRAJ>::extrapolationFailed(Status::status stat, const char* comment) {
history_.emplace_back(0,0,stat,comment);
return false;
Expand Down
1 change: 1 addition & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set( TEST_SOURCE_FILES
Geometry_unit.cc
Intersection_unit.cc
MatEnv_unit.cc
Truncate_unit.cc
)

# Generate unit test targets
Expand Down
Loading
Loading