Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sim): Add check for multiple sensitive volumes with same name #1596

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

karabowi
Copy link
Collaborator

Both the transport engines and FairRoot allow registering of multiple nodes with the same volume/same volume name (copy mechanism). However currently such workflow logs errors from FairVolumeList::addVolume().

The commit reintroduces the check for same volume names into the FairModule::AddSensitiveVolume() function thus preventing registration of copy volumes.

Fixes the issue #1595.


Checklist:

Copy link

coderabbitai bot commented Nov 22, 2024

📝 Walkthrough

Walkthrough

The changes in this pull request involve modifications to the FairVolumeList and FairModule classes. In FairVolumeList, the addVolume method has been updated to enhance error handling for duplicate volume registrations, now terminating the program with a fatal log message instead of logging an error. In FairModule, the AddSensitiveVolume method has been modified to include an additional parameter in the FairVolume constructor and introduces a new logging statement for failed volume registrations. The core functionality and structure of both classes remain intact.

Changes

File Path Change Summary
fairroot/base/sim/FairVolumeList.cxx Updated addVolume method to include fatal error handling for duplicate volume names, removing the previous error log.
fairroot/base/sim/FairModule.cxx Modified AddSensitiveVolume method to include an additional parameter in FairVolume constructor and added logging for volume registration failures.

Possibly related PRs

  • fix(base): Do not copy FairModule::vList #1534: The changes in FairModule.cxx regarding the handling of volumes are related to the modifications in the FairVolumeList class, as both PRs involve volume management within the FairModule context.
  • docs: breaking change of FairModule::svList #1555: The removal of FairModule::svList and the introduction of fAllSensitiveVolumes in FairModule are relevant as they pertain to the internal handling of sensitive volumes, which connects to the changes made in the FairVolumeList class regarding volume registration and management.

Suggested reviewers

  • dennisklein

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ee84b67 and 3fcd915.

📒 Files selected for processing (2)
  • fairroot/base/sim/FairModule.cxx (1 hunks)
  • fairroot/base/sim/FairVolumeList.cxx (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • fairroot/base/sim/FairVolumeList.cxx
  • fairroot/base/sim/FairModule.cxx

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
fairroot/base/sim/FairModule.cxx (1)

283-289: Consider adding debug logging for duplicate volumes.

Adding debug logging when a duplicate volume is found would help with troubleshooting without impacting performance.

 if (!vList->findObject(volName)) {
     auto addedVol = vList->addVolume(std::make_unique<FairVolume>(volName, fNbOfVolumes));
     if (!addedVol) {
+        LOG(debug2) << "Failed to add volume " << volName << " to the volume list";
         return;
     }
     ++fNbOfVolumes;
     RegisterSensitiveVolume(*addedVol);
+} else {
+    LOG(debug2) << "Skipping duplicate volume " << volName;
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 23a39ba and e351b22.

📒 Files selected for processing (1)
  • fairroot/base/sim/FairModule.cxx (1 hunks)
🔇 Additional comments (2)
fairroot/base/sim/FairModule.cxx (2)

283-289: LGTM! The implementation effectively prevents duplicate volume registration.

The changes correctly address the issue by:

  1. Checking for existing volumes before adding
  2. Using modern C++ practices with smart pointers
  3. Properly handling the failure case

283-289: Verify impact on existing detector configurations.

Let's analyze the usage patterns of AddSensitiveVolume across detectors to ensure this change doesn't break existing configurations.

✅ Verification successful

Change is safe for existing detector configurations

The analysis of AddSensitiveVolume usage patterns shows that the change is safe because:

  1. All detector implementations follow a consistent pattern where volumes are first created and added to the geometry tree, then registered as sensitive volumes via AddSensitiveVolume.
  2. No detector implementation attempts to register the same volume multiple times, which is the scenario this change protects against.
  3. The change maintains backward compatibility by:
    • Still allowing registration of new volumes
    • Only preventing duplicate registrations
    • Incrementing volume counter only for successful additions

The usage patterns in template detectors and examples confirm that the new duplicate-prevention logic aligns with existing detector implementation practices.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Analyze AddSensitiveVolume usage patterns across detectors

# Find all detector implementations
echo "=== Detector implementations ==="
rg -l "class.*:.*public.*FairDetector" 

# Find AddSensitiveVolume calls
echo -e "\n=== AddSensitiveVolume usage patterns ==="
rg -B 2 -A 2 "AddSensitiveVolume\(" 

# Look for potential duplicate volume registrations
echo -e "\n=== Potential duplicate volume patterns ==="
ast-grep --pattern 'AddSensitiveVolume($VOLUME)'

Length of output: 12575

Copy link
Member

@dennisklein dennisklein left a comment

Choose a reason for hiding this comment

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

Strong veto.

fairroot/base/sim/FairModule.cxx Outdated Show resolved Hide resolved
@karabowi karabowi force-pushed the fix_multi_sensVolumes branch from e351b22 to 43fc099 Compare November 27, 2024 14:50
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
fairroot/base/sim/FairVolumeList.cxx (1)

Line range hint 26-45: Consider improving error handling for duplicate volumes

The current implementation silently returns nullptr for duplicate volumes, which could lead to subtle runtime issues if the caller doesn't properly handle the null case. Consider:

  1. Making the duplicate volume case more explicit in the method's signature
  2. Adding documentation about the nullptr return case
  3. Considering whether debug level is appropriate for duplicate volumes, as this might indicate a configuration issue

Here's a suggested improvement:

+ /**
+  * Add a volume to the list.
+  * @param vol The volume to add (takes ownership)
+  * @return Pointer to the added volume, or nullptr if a volume with the same name already exists
+  */
 FairVolume* FairVolumeList::addVolume(std::unique_ptr<FairVolume> vol)
 {
     auto vol_found = findObject(vol->GetName());
 
     if (vol_found) {
-        LOG(debug) << "FairVolumeList element: " << vol->GetName() << " VolId : " << vol->getVolumeId()
-                   << " already defined " << vol_found->getVolumeId();
+        LOG(warning) << "Duplicate volume name detected: " << vol->GetName() 
+                    << " (new_id=" << vol->getVolumeId() 
+                    << ", existing_id=" << vol_found->getVolumeId() << ")";
         return nullptr;
     }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e351b22 and 43fc099.

📒 Files selected for processing (1)
  • fairroot/base/sim/FairVolumeList.cxx (1 hunks)

@ChristianTackeGSI
Copy link
Member

Change log level for multiple sensitive volumes with same name

Both the transport engines and FairRoot allow registering of multiple nodes with the same volume/same volume name (copy mechanism). However currently such workflow logs errors from FairVolumeList::addVolume().

Note: All current users of addVolume only call RegisterSensitiveVolume IF the addVolume succeeds. So "registering" a sensitive volume with the same name does not seem to work this way.

If the current behaviour is okay, then please rephrase your commit message.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
fairroot/base/sim/FairModule.cxx (1)

285-286: Consider adding documentation about volume name handling.

Since both transport engines and FairRoot allow multiple nodes with the same volume name, it would be helpful to add a comment explaining this behavior above the debug message. This would help future maintainers understand why duplicate volumes are logged at debug level rather than being treated as errors.

Consider adding a comment like this:

 void FairModule::AddSensitiveVolume(TGeoVolume* vol)
 {
     auto volName = vol->GetName();
     LOG(debug2) << "AddSensitiveVolume " << volName;
 
     auto addedVol = vList->addVolume(std::make_unique<FairVolume>(volName, fNbOfVolumes));
     if (!addedVol) {
+        // Both transport engines and FairRoot allow multiple nodes with same volume name
+        // as part of copy mechanism, so we log duplicates at debug level
         LOG(debug) << "FairVolumeList element: " << vol->GetName() << " VolId : " << vol->getVolumeId()
                    << " already defined " << vol_found->getVolumeId() << "// vol = " << vol;
         return;
     }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 43fc099 and ee84b67.

📒 Files selected for processing (2)
  • fairroot/base/sim/FairModule.cxx (1 hunks)
  • fairroot/base/sim/FairVolumeList.cxx (0 hunks)
💤 Files with no reviewable changes (1)
  • fairroot/base/sim/FairVolumeList.cxx
🔇 Additional comments (1)
fairroot/base/sim/FairModule.cxx (1)

285-286: LGTM! The debug logging aligns with the PR objectives.

The change to use LOG(debug) for duplicate volume names is appropriate and matches the discussed approach. This maintains the functionality while making the logging less intrusive.

Let's verify the impact on simulation workflows by checking for any existing error handling patterns:

✅ Verification successful

Let me gather more information about the volume handling and logging patterns to verify this change thoroughly.


Let me check the test file and related volume handling code to make a final determination.


Based on the test implementation and FairVolumeList code, I can now make a definitive assessment:

The debug logging for duplicate volumes is appropriate and safe

The code change is verified to be correct because:

  • The test suite explicitly verifies that duplicate volumes are handled gracefully by returning nullptr
  • FairVolumeList's implementation shows that duplicate volumes are intentionally rejected
  • The debug log message provides valuable diagnostic information without affecting the control flow
  • This aligns with the existing pattern where volume-related logging is done at debug level in detector implementations
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check how duplicate volumes are handled in simulation workflows
rg -A 3 "FairVolumeList.*addVolume" 

Length of output: 308


Script:

#!/bin/bash
# Check for volume-related error handling and logging patterns
rg -A 5 "LOG\((debug|info|warn|error).*volume" --type cpp

# Check the FairVolumeList implementation
ast-grep --pattern 'class FairVolumeList {
  $$$
  addVolume($$$) {
    $$$
  }
  $$$
}'

# Check for volume-related tests
fd "test.*volume" --type f

Length of output: 4175


Script:

#!/bin/bash
# Check the volume list test implementation
cat tests/base/sim/test_FairVolumeList.cxx

# Check the actual implementation of volume handling
cat fairroot/base/sim/FairVolumeList.cxx

# Look for any related error handling patterns
rg -A 3 "duplicate.*volume|volume.*duplicate" --type cpp

Length of output: 3408

@karabowi karabowi force-pushed the fix_multi_sensVolumes branch from ee84b67 to 3fcd915 Compare December 18, 2024 16:16
@karabowi
Copy link
Collaborator Author

I have changed the behavior of the check.
If it detects same name volumes in one detector, FairModule::AddSensitiveVolume() will spit out LOG(debug)
as we assume it is a standard copy mechanism procedure.
In case if the code tries to add sensitive volume for different detector, a LOG(fatal) will be issued and the execution will terminate.

Copy link
Member

@ChristianTackeGSI ChristianTackeGSI left a comment

Choose a reason for hiding this comment

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

Just some ideas.

fairroot/base/sim/FairVolumeList.cxx Outdated Show resolved Hide resolved
fairroot/base/sim/FairVolumeList.cxx Outdated Show resolved Hide resolved
fairroot/base/sim/FairVolumeList.cxx Outdated Show resolved Hide resolved
fairroot/base/sim/FairModule.cxx Outdated Show resolved Hide resolved
@karabowi karabowi force-pushed the fix_multi_sensVolumes branch from 3fcd915 to 718e712 Compare January 22, 2025 15:15
Copy link
Member

@ChristianTackeGSI ChristianTackeGSI left a comment

Choose a reason for hiding this comment

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

Looks good to me now.

Maybe one could move the LOG(debug) as an else branch of the LOG(fatal), but really, I don't care much.

Please take a look at the clang-format messages from the CI.

@dennisklein might have some ideas?

The transport engines allow registering of multiple nodes
with the same volume/same volume name (copy mechanism).
In `FairRoot` this mechanism works provided unique volume name
over the whole geometry setup of different detectors.

The commit clarifies the situtation:
1. for same volume names in one detector, it quenches the log message
by moving `LOG` and changing it severity
from `LOG(error)` in `FairVolumeList::addVolume()`
to `LOG(debug)` in `FairModule::AddSensitiveVolume()`.
2. for same volume names accross different detectors,
the program prints appropriate `LOG(fatal)`.

Fixes the issue FairRootGroup#1595.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants