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
72 changes: 31 additions & 41 deletions offline/packages/trackreco/PHActsTrkFitter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -543,17 +543,21 @@ void PHActsTrkFitter::loopTracks(Acts::Logging::Level logLevel)
continue;
}

// filter sourcelinks to remove detectors that we don't want to include in the fit
sourceLinks = filterSourceLinks( sourceLinks );

Comment on lines +546 to +548
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Unconditional filterSourceLinks call changes behaviour for non-default flag combinations.

Previously, all filtering lived inside the old getSurfaceVector, which was only reached when m_fitSiliconMMs || m_directNavigation. After this change, the micromegas exclusion (!m_useMicromegas) and the silicon-only cut (m_forceSiOnlyFit) are applied to every fit path, including the standard TPC fit.

For the default settings (m_useMicromegas = true, m_forceSiOnlyFit = false) there is no change. However, any workflow that sets m_useMicromegas = false without enabling directed navigation will now silently drop micromegas clusters from the fit, which was not the case before.

Given the PR's stated intent ("filtering can be applied regardless of directed fit"), this appears deliberate, but it warrants a brief comment near line 547 to make the scope of the change explicit for future maintainers.

💬 Suggested in-code clarification
-      // filter sourcelinks to remove detectors that we don't want to include in the fit
+      // filter sourcelinks to remove detectors excluded from the fit;
+      // applied unconditionally (i.e. independent of directed-navigation mode)
       sourceLinks = filterSourceLinks( sourceLinks );

if (sourceLinks.empty())
{
continue;
}

/// If using directed navigation, collect surface list to navigate
SurfacePtrVec surfaces_tmp;
SurfacePtrVec surfaces;
if (m_fitSiliconMMs || m_directNavigation)
{
sourceLinks = getSurfaceVector(sourceLinks, surfaces_tmp);

// get surfaces matching source links
const auto surfaces_tmp = getSurfaceVector(sourceLinks);

// skip if there is no surfaces
if (surfaces_tmp.empty())
Expand Down Expand Up @@ -960,59 +964,45 @@ ActsTrackFittingAlgorithm::TrackFitterResult PHActsTrkFitter::fitTrack(
}

//__________________________________________________________________________________
SourceLinkVec PHActsTrkFitter::getSurfaceVector(const SourceLinkVec& sourceLinks, SurfacePtrVec& surfaces) const
SourceLinkVec PHActsTrkFitter::filterSourceLinks(const SourceLinkVec& sourceLinks ) const
{
SourceLinkVec siliconMMSls;

// if(Verbosity() > 1)
// std::cout << "Sorting " << sourceLinks.size() << " SLs" << std::endl;

SourceLinkVec filtered;
for (const auto& sl : sourceLinks)
{
const ActsSourceLink asl = sl.get<ActsSourceLink>();
if (Verbosity() > 1)
{
std::cout << "SL available on : " << asl.geometryId() << std::endl;
}

const auto* const surf = m_tGeometry->geometry().tGeometry->findSurface(asl.geometryId());
if (m_fitSiliconMMs)
{
// skip TPC surfaces
if (m_tGeometry->maps().isTpcSurface(surf))
{
continue;
}

// also skip micromegas surfaces if not used
if (m_tGeometry->maps().isMicromegasSurface(surf) && !m_useMicromegas)
{
continue;
}
}
// skip TPC surfaces for fitSilicon MMs
if (m_tGeometry->maps().isTpcSurface(surf) && m_fitSiliconMMs)
{ continue; }

if (m_forceSiOnlyFit)
{
if (m_tGeometry->maps().isMicromegasSurface(surf) || m_tGeometry->maps().isTpcSurface(surf))
{
continue;
}
}
// skip micromegas surfaces if not used
if (m_tGeometry->maps().isMicromegasSurface(surf) && !m_useMicromegas)
{ continue; }

// skip everything but silicons if only silicon fit is required
if (m_forceSiOnlyFit && (m_tGeometry->maps().isMicromegasSurface(surf) || m_tGeometry->maps().isTpcSurface(surf)) )
{ continue; }
Comment on lines +969 to +985
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# Locate and examine isTpcSurface and isMicromegasSurface implementations
rg -n "void.*isTpcSurface|bool.*isTpcSurface" --type=cpp -A10 -B2

Repository: sPHENIX-Collaboration/coresoftware

Length of output: 2240


🏁 Script executed:

# Also search for isMicromegasSurface
rg -n "void.*isMicromegasSurface|bool.*isMicromegasSurface" --type=cpp -A10 -B2

Repository: sPHENIX-Collaboration/coresoftware

Length of output: 2202


🏁 Script executed:

# Check the class containing these methods - likely in maps
fd -e h -e hpp | xargs rg -l "isTpcSurface|isMicromegasSurface" | head -5

Repository: sPHENIX-Collaboration/coresoftware

Length of output: 122


🏁 Script executed:

# Search for findSurface return type and behavior
rg -n "findSurface" --type=cpp --type=hpp -B2 -A5 | head -60

Repository: sPHENIX-Collaboration/coresoftware

Length of output: 105


🏁 Script executed:

# Search for findSurface definition in Acts geometry code
rg -n "findSurface" --type=cpp --type=h -B5 -A10 | grep -A10 "findSurface.*{" | head -50

Repository: sPHENIX-Collaboration/coresoftware

Length of output: 60


🏁 Script executed:

# Check if findSurface is documented or used with null checks elsewhere
rg -n "findSurface" offline/packages/trackreco/PHActsTrkFitter.cc -B3 -A3

Repository: sPHENIX-Collaboration/coresoftware

Length of output: 674


🏁 Script executed:

# Search Acts library to understand findSurface behavior
fd -type f -name "*.h" | xargs rg -l "findSurface" | head -5

Repository: sPHENIX-Collaboration/coresoftware

Length of output: 355


🏁 Script executed:

# Look for TrackingGeometry or tGeometry type to understand findSurface
rg -n "tGeometry" offline/packages/trackreco/PHActsTrkFitter.cc | head -10

Repository: sPHENIX-Collaboration/coresoftware

Length of output: 501


🏁 Script executed:

# Check if there are any examples in the codebase where findSurface result is checked for null
rg -n "findSurface" -B2 -A2 | grep -A2 -B2 "if.*nullptr\|if.*!.*surf\|if.*surf.*==" | head -30

Repository: sPHENIX-Collaboration/coresoftware

Length of output: 60


Add null-check after findSurface to prevent undefined behavior.

isTpcSurface and isMicromegasSurface both dereference their argument without null-checking (lines 34 and 39 of ActsSurfaceMaps.cc). Since findSurface returns nullptr when a geometry ID is absent from the Acts geometry (e.g., stale clusters or geometry mismatches), passing the unchecked result to these functions at lines 976, 980, and 984 causes undefined behavior.

Suggested fix
     const auto* const surf = m_tGeometry->geometry().tGeometry->findSurface(asl.geometryId());
+    if (!surf) { continue; }

     // skip TPC surfaces for fitSilicon MMs
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
SourceLinkVec filtered;
for (const auto& sl : sourceLinks)
{
const ActsSourceLink asl = sl.get<ActsSourceLink>();
if (Verbosity() > 1)
{
std::cout << "SL available on : " << asl.geometryId() << std::endl;
}
const auto* const surf = m_tGeometry->geometry().tGeometry->findSurface(asl.geometryId());
if (m_fitSiliconMMs)
{
// skip TPC surfaces
if (m_tGeometry->maps().isTpcSurface(surf))
{
continue;
}
// also skip micromegas surfaces if not used
if (m_tGeometry->maps().isMicromegasSurface(surf) && !m_useMicromegas)
{
continue;
}
}
// skip TPC surfaces for fitSilicon MMs
if (m_tGeometry->maps().isTpcSurface(surf) && m_fitSiliconMMs)
{ continue; }
if (m_forceSiOnlyFit)
{
if (m_tGeometry->maps().isMicromegasSurface(surf) || m_tGeometry->maps().isTpcSurface(surf))
{
continue;
}
}
// skip micromegas surfaces if not used
if (m_tGeometry->maps().isMicromegasSurface(surf) && !m_useMicromegas)
{ continue; }
// skip everything but silicons if only silicon fit is required
if (m_forceSiOnlyFit && (m_tGeometry->maps().isMicromegasSurface(surf) || m_tGeometry->maps().isTpcSurface(surf)) )
{ continue; }
SourceLinkVec filtered;
for (const auto& sl : sourceLinks)
{
const ActsSourceLink asl = sl.get<ActsSourceLink>();
const auto* const surf = m_tGeometry->geometry().tGeometry->findSurface(asl.geometryId());
if (!surf) { continue; }
// skip TPC surfaces for fitSilicon MMs
if (m_tGeometry->maps().isTpcSurface(surf) && m_fitSiliconMMs)
{ continue; }
// skip micromegas surfaces if not used
if (m_tGeometry->maps().isMicromegasSurface(surf) && !m_useMicromegas)
{ continue; }
// skip everything but silicons if only silicon fit is required
if (m_forceSiOnlyFit && (m_tGeometry->maps().isMicromegasSurface(surf) || m_tGeometry->maps().isTpcSurface(surf)) )
{ continue; }


// update vectors
siliconMMSls.push_back(sl);
surfaces.push_back(surf);
filtered.push_back(sl);
}

if (Verbosity() > 10)
return filtered;
}

//__________________________________________________________________________________
SurfacePtrVec PHActsTrkFitter::getSurfaceVector(const SourceLinkVec& sourceLinks) const
{
SurfacePtrVec surfaces;
for (const auto& sl : sourceLinks)
{
for (const auto& surf : surfaces)
{
std::cout << "Surface vector : " << surf->geometryId() << std::endl;
}
const ActsSourceLink asl = sl.get<ActsSourceLink>();
const auto* const surf = m_tGeometry->geometry().tGeometry->findSurface(asl.geometryId());
surfaces.push_back(surf);
}
Comment on lines +1001 to 1003
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Null Surface* can be silently inserted into the directed-navigation sequence.

findSurface returns nullptr for unrecognised geometry IDs, and the result is pushed into surfaces without any guard. The directed Kalman fitter dereferences every element of surfSequence, so a single null entry will crash.

🛡️ Suggested guard
     const auto* const surf = m_tGeometry->geometry().tGeometry->findSurface(asl.geometryId());
+    if (!surf) { continue; }   // drop source links whose surface is absent in the geometry
     surfaces.push_back(surf);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const auto* const surf = m_tGeometry->geometry().tGeometry->findSurface(asl.geometryId());
surfaces.push_back(surf);
}
const auto* const surf = m_tGeometry->geometry().tGeometry->findSurface(asl.geometryId());
if (!surf) { continue; } // drop source links whose surface is absent in the geometry
surfaces.push_back(surf);
}


return siliconMMSls;
return surfaces;
}

void PHActsTrkFitter::checkSurfaceVec(SurfacePtrVec& surfaces) const
Expand Down
26 changes: 14 additions & 12 deletions offline/packages/trackreco/PHActsTrkFitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,20 @@ class PHActsTrkFitter : public SubsysReco
/// Helper function to call either the regular navigation or direct
/// navigation, depending on m_fitSiliconMMs
ActsTrackFittingAlgorithm::TrackFitterResult fitTrack(
const std::vector<Acts::SourceLink>& sourceLinks,
const ActsTrackFittingAlgorithm::TrackParameters& seed,
const ActsTrackFittingAlgorithm::GeneralFitterOptions&
kfOptions,
const SurfacePtrVec& surfSequence,
const CalibratorAdapter& calibrator,
ActsTrackFittingAlgorithm::TrackContainer& tracks);

/// Functions to get list of sorted surfaces for direct navigation, if
/// applicable
SourceLinkVec getSurfaceVector(const SourceLinkVec& sourceLinks,
SurfacePtrVec& surfaces) const;
const std::vector<Acts::SourceLink>& sourceLinks,
const ActsTrackFittingAlgorithm::TrackParameters& seed,
const ActsTrackFittingAlgorithm::GeneralFitterOptions& kfOptions,
const SurfacePtrVec& surfSequence,
const CalibratorAdapter& calibrator,
ActsTrackFittingAlgorithm::TrackContainer& tracks);

// remove all source links for detectors that we don't want to include in the fit
SourceLinkVec filterSourceLinks(const SourceLinkVec& sourceLinks ) const;

/// get list of sorted surfaces for direct navigation, if applicable
SurfacePtrVec getSurfaceVector(const SourceLinkVec& sourceLinks) const;

/// check ordering of the surfaces
void checkSurfaceVec(SurfacePtrVec& surfaces) const;

bool getTrackFitResult(const FitResult& fitOutput, TrackSeed* seed,
Expand Down