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
3 changes: 3 additions & 0 deletions Fit/Material.hh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ namespace KinKal {
if((tdir == TimeDir::forwards && ptraj.back().range().begin() > time()) ||
(tdir == TimeDir::backwards && ptraj.front().range().end() < time()) )
throw std::invalid_argument("Material: Can't append piece");
// an inactive xing (ie an out-of-bounds crossing) has no effect: don't split the trajectory. As with process and append,
// it is still kept as an effect, and will contribute if it becomes active when the track is refit
if(!exingptr_->active()) return;
auto ktrajptr = ptraj.nearestTraj(time());
// convert parameters at this material to momentum space representation
auto pstate = ktrajptr->stateEstimate(time());
Expand Down
343 changes: 232 additions & 111 deletions Geometry/Intersect.hh

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Geometry/IntersectFlag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ std::ostream& operator <<(std::ostream& ost, KinKal::IntersectFlag const& iflag)
ost << "On surface ";
if(iflag.inbounds_)ost << " in bounds ";
if(iflag.inrange_)ost << " in range ";
if(iflag.hascov_)ost << " with covariance ";
}else {
ost << "No Intersection";
}
Expand Down
6 changes: 4 additions & 2 deletions Geometry/IntersectFlag.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ namespace KinKal {
bool onsurface_ = false; // intersection is on the surface
bool inbounds_ = false; // intersection is inside the surface boundaries
bool inrange_ = false; // intersection is inside the time range
bool operator ==(IntersectFlag const& other) const { return other.onsurface_ == onsurface_ && other.inbounds_ == inbounds_ && other.inrange_ == inrange_;}
bool operator !=(IntersectFlag const& other) const { return other.onsurface_ != onsurface_ || other.inbounds_ != inbounds_ || other.inrange_ != inrange_;}
bool hascov_ = false; // the projected spatial covariance (Intersection::pcov_) is filled. This is optional, and independent of good()
bool gap_ = false; // intersection is in a piecewise-trajectory gap
bool operator ==(IntersectFlag const& other) const { return other.onsurface_ == onsurface_ && other.inbounds_ == inbounds_ && other.inrange_ == inrange_ && other.hascov_ == hascov_;}
bool operator !=(IntersectFlag const& other) const { return other.onsurface_ != onsurface_ || other.inbounds_ != inbounds_ || other.inrange_ != inrange_ || other.hascov_ != hascov_;}
bool good() const { return onsurface_ && inbounds_ && inrange_; }
friend std::ostream& operator <<(std::ostream& ost, KinKal::IntersectFlag const& iflag);
};
Expand Down
1 change: 1 addition & 0 deletions Geometry/Intersection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace KinKal {
ost << "Intersection on surface ";
if(inter.inbounds_) ost << " in bounds ";
if(inter.inrange_) ost << " in time range ";
if(inter.hascov_) ost << " with covariance ";
} else {
ost << "No Intersection";
}
Expand Down
11 changes: 10 additions & 1 deletion Geometry/Intersection.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ namespace KinKal {
VEC3 norm_; // surface normal at intersection
VEC3 pdir_; // particle direction at intersection
double time_ = 0.0; // time at intersection (from particle)
bool gap_ = false; // intersection is in a piecewise-trajectory gap
// local plane (tangent to the surface at the intersection) coordinate system; W = norm_
VEC3 udir_; // local plane U direction
VEC3 vdir_; // local plane V direction
// projected spatial covariance of the trajectory at the intersection onto the local plane, in UV coordinates.
// This is only computed on request (the pcov argument of intersect), and is then filled for onsurface intersections (in bounds or not)
// as long as the trajectory isn't parallel to the plane. hascov_ records if it was filled; otherwise it is 0.
PMAT pcov_;
// approximate (~1%) fraction of the 2D Gaussian described by pcov_ that lies within the surface boundaries. For curved surfaces
// the boundaries are projected onto the local plane along the surface normal. Only meaningful if hascov_ is true
double inbfrac_ = 0.0;
Ray ray() const { return Ray(pdir_,pos_); } // linear particle trajectory at intersection
friend std::ostream& operator << (std::ostream& ost, KinKal::Intersection const& inter);
};
Expand Down
8 changes: 5 additions & 3 deletions Geometry/ParticleTrajectoryIntersect.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
#include "KinKal/Geometry/Intersection.hh"
#include "KinKal/Geometry/Intersect.hh"
namespace KinKal {
// find intersection by stepping over pieces. This works when the pieces have small curvature
template <class KTRAJ, class SURF> Intersection intersect(ParticleTrajectory<KTRAJ> const& ptraj, SURF const& surf, TimeRange trange, double tol,TimeDir tdir = TimeDir::forwards) {
// find intersection by stepping over pieces. This works when the pieces have small curvature.
// If pcov is true the projected spatial covariance is filled, using the piece containing the final intersection only
template <class KTRAJ, class SURF> Intersection intersect(ParticleTrajectory<KTRAJ> const& ptraj, SURF const& surf, TimeRange trange, double tol,TimeDir tdir = TimeDir::forwards, bool pcov=false) {
Intersection retval;
double tstart = (tdir == TimeDir::forwards) ? trange.begin() : trange.end();
double tend = (tdir == TimeDir::forwards) ? trange.end() : trange.begin();
size_t istart = ptraj.nearestIndex(tstart);
size_t iend = ptraj.nearestIndex(tend);
if(istart == iend){
retval = intersect(ptraj.piece(istart),surf,trange,tol,tdir);
retval = intersect(ptraj.piece(istart),surf,trange,tol,tdir,pcov);
} else {
int istep = (tdir == TimeDir::forwards) ? 1 : -1;
do {
Expand Down Expand Up @@ -46,6 +47,7 @@ namespace KinKal {
auto tinter = intersect(*ttraj,surf,srange,tol,tdir);
if(tinter.good()){
retval = tinter;
if(pcov)fillPlaneCovariance(*ttraj,surf,retval,tol);
break;
}
}
Expand Down
3 changes: 3 additions & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ set( TEST_SOURCE_FILES
Geometry_unit.cc
Intersection_unit.cc
IntersectReturn_unit.cc
IntersectionCovariance_unit.cc
IntersectionBounds_unit.cc
IntersectTilt_unit.cc
MatEnv_unit.cc
Truncate_unit.cc
)
Expand Down
13 changes: 5 additions & 8 deletions Tests/CentralHelixFit_unit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ int main(int argc, char **argv) {
cout << "Testing gradient field, correction 2" << endl;
std::vector<std::string> arguments;
arguments.push_back(argv[0]);
// arguments.push_back("--Bgrad");
// arguments.push_back("-0.036"); // mu2e-like field gradient
arguments.push_back("--Bgrad");
arguments.push_back("-0.036"); // mu2e-like field gradient
arguments.push_back("--Schedule");
arguments.push_back("MCAmbigFixedField.txt");
// arguments.push_back("seedfit.txt");
// arguments.push_back("--extend");
// arguments.push_back("driftextend.txt");
// arguments.push_back("--ttree");
// arguments.push_back("1");
arguments.push_back("seedfit.txt");
arguments.push_back("--extend");
arguments.push_back("driftextend.txt");
std::vector<char*> myargv;
for (const auto& arg : arguments)
myargv.push_back((char*)arg.data());
Expand Down
146 changes: 146 additions & 0 deletions Tests/IntersectTilt_unit.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
//
// Test helix intersections with cylinders and frustra when the nominal BField is tilted from the surface axis.
// Random queries are compared against a reference found by stepping over the whole time range; intersect must not
// miss a crossing the reference finds, nor return a later one. This exercises canIntersect (KFTrack/KinKal#238).
// original author: David Brown (LBNL) 2026
//
#include "KinKal/Geometry/Intersection.hh"
#include "KinKal/Geometry/Intersect.hh"
#include "KinKal/Geometry/Cylinder.hh"
#include "KinKal/Geometry/Frustrum.hh"
#include <iostream>
#include <string>
#include <random>
#include <cmath>
#include <getopt.h>

using namespace KinKal;
using std::cout;
using std::endl;

static struct option long_options[] = {
{"nquery", required_argument, 0, 'n' },
{"seed", required_argument, 0, 's' },
{"verbose", required_argument, 0, 'v' },
{NULL, 0,0,0}
};

void print_usage() {
printf("Usage: IntersectTilt --nquery (number of random queries per configuration) --seed (random seed) --verbose (verbosity)\n");
}

enum SurfType {cylout=0, cylin, fruout, fruin};
static const char* surfName[4] = {"Cylinder (helix circle inside)","Cylinder (helix circle around)","Frustrum (helix circle inside)","Frustrum (helix circle around)"};

// reference: the first in-bounds crossing found by stepping, restarting past any out-of-bounds crossings
template <class HELIX, class SURF> Intersection refIntersect(HELIX const& helix, SURF const& surf, TimeRange trange, double tol, TimeDir tdir){
static const double eps(1.0e-4); // ns: restart offset
for(unsigned icross=0; icross < 100; ++icross){
auto inter = stepIntersect(helix,surf,trange,tol,tdir);
if(!inter.onsurface_ || !inter.inrange_ || inter.inbounds_) return inter;
if(tdir == TimeDir::forwards){
if(inter.time_+eps >= trange.end()) break;
trange = TimeRange(inter.time_+eps,trange.end());
} else {
if(inter.time_-eps <= trange.begin()) break;
trange = TimeRange(trange.begin(),inter.time_-eps);
}
}
return Intersection();
}

template <class HELIX, class SURF> void compare(HELIX const& helix, SURF const& surf, TimeRange const& trange, double tol, TimeDir tdir,
unsigned& nref, unsigned& nmiss, unsigned& nlate, unsigned& nextra, int verbose) {
static const double dttol(1.0e-5); // ns
auto ref = refIntersect(helix,surf,trange,tol,tdir);
auto inter = intersect(helix,surf,trange,tol,tdir);
if(ref.good()){
++nref;
if(!inter.good()){
++nmiss;
if(verbose > 1) cout << "Missed crossing: reference " << ref << endl;
} else if((inter.time_-ref.time_)*timeDirSign(tdir) > dttol){
++nlate;
if(verbose > 1) cout << "Later crossing: reference " << ref << " intersect " << inter << endl;
}
} else if(inter.good()) {
++nextra; // the reference stepping can miss grazing crossings; this is not a failure
}
}

template <class HELIX> int testTilt(std::string const& hname, SurfType stype, double maxtilt, unsigned nquery, unsigned seed, int verbose) {
static const double tol(1.0e-5); // mm
std::mt19937 gen(seed);
std::uniform_real_distribution<double> uni(0.0,1.0);
unsigned nref(0), nmiss(0), nlate(0), nextra(0);
for(unsigned iquery=0; iquery < nquery; ++iquery){
// bnom tilted from z, the surface axis
double tilt = maxtilt*uni(gen);
double tphi = 2*M_PI*uni(gen);
VEC3 bnom(sin(tilt)*cos(tphi),sin(tilt)*sin(tphi),cos(tilt));
// low-momentum particle so that the helix loops within the query range
double mom = 30.0+120.0*uni(gen);
double cost = -0.9+1.8*uni(gen);
double sint = sqrt(1.0-cost*cost);
double phi = 2*M_PI*uni(gen);
MOM4 mom4(mom*sint*cos(phi),mom*sint*sin(phi),mom*cost,0.511);
VEC4 pos(-400.0+800.0*uni(gen),-400.0+800.0*uni(gen),-500.0+1000.0*uni(gen),0.0);
HELIX helix(pos,mom4,-1,bnom,TimeRange(-50.0,50.0));
// surface radius near the helix extremes, so that crossings are marginal
double dcent = helix.center(0.0).Rho();
double rbend = helix.bendRadius();
double rsurf = (stype == cylin || stype == fruin) ? rbend - dcent : dcent + (uni(gen) < 0.5 ? 1.0 : -1.0)*rbend;
rsurf += -30.0 + 60.0*uni(gen);
double t0 = -10.0+10.0*uni(gen);
TimeRange trange(t0,t0+2.0+18.0*uni(gen));
auto tdir = uni(gen) < 0.5 ? TimeDir::forwards : TimeDir::backwards;
bool isfru = stype == fruout || stype == fruin;
double dr = -200.0 + 400.0*uni(gen); // frustrum radius change
if(rsurf < 10.0 || (isfru && rsurf-fabs(dr) < 10.0)) continue;
VEC3 zdir(0.0,0.0,1.0), origin(0.0,0.0,0.0);
if(isfru){
Frustrum fru(zdir,origin,rsurf-dr,rsurf+dr,2000.0);
compare(helix,fru,trange,tol,tdir,nref,nmiss,nlate,nextra,verbose);
} else {
Cylinder cyl(zdir,origin,rsurf,2000.0);
compare(helix,cyl,trange,tol,tdir,nref,nmiss,nlate,nextra,verbose);
}
}
bool fail = nmiss > 0 || nlate > 0;
if(verbose > 0 || fail)
cout << hname << " " << surfName[stype] << " tilt < " << maxtilt << " rad: " << nref << " reference crossings, "
<< nmiss << " missed, " << nlate << " later, " << nextra << " extra" << (fail ? " FAILED" : "") << endl;
return fail ? -1 : 0;
}

int main(int argc, char** argv) {
unsigned nquery(500), seed(238);
int verbose(0);
int opt;
int long_index =0;
while ((opt = getopt_long_only(argc, argv,"",
long_options, &long_index )) != -1) {
switch (opt) {
case 'n' : nquery = atoi(optarg);
break;
case 's' : seed = atoi(optarg);
break;
case 'v' : verbose = atoi(optarg);
break;
default: print_usage();
exit(EXIT_FAILURE);
}
}
int status(0);
for(auto stype : {cylout, cylin, fruout, fruin}){
for(double maxtilt : {0.0, 0.05, 0.45}){
status |= testTilt<LoopHelix>("LoopHelix",stype,maxtilt,nquery,seed,verbose);
status |= testTilt<CentralHelix>("CentralHelix",stype,maxtilt,nquery,seed,verbose);
}
}
if(status == 0)
cout << "IntersectTilt test passed" << endl;
else
cout << "IntersectTilt test FAILED" << endl;
return status;
}
Loading
Loading