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

feat: move MaterialTrackWriting infrastructure to Core + first use of RNTuple #4043

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

asalzburger
Copy link
Contributor

@asalzburger asalzburger commented Jan 21, 2025

This PR makes the Root based recording of Geant4 material (as input to the material mapping) to Core and puts reading and writing onto the same internal payload.

Summary: The new RNTuple backend results in a file size reduction of 35% and a per event runtime reduction (G4 recording and writing) of 17.5%.

Update: Unforunately I had to remove the RNTuple writing infrastructure for the moment as it is not yet stable thoughout our different ROOT versions, the code can be updated to it very quickly though.

Details of recording a million material tracks in the ODD are as follows:

(CURRENT MAIN) TTree backend:

- Timing:
14:31:46    Sequencer      INFO      Processed 1000 events in 80.167658 s (wall clock)
14:31:46    Sequencer      INFO      Average time per event: 79.452257 ms/event

- File Size:
1154971403 21 Jan 14:31 geant4_material_tracks.root 

(THIS PR) RNTuple backend:

- Timing:
14:16:38    Sequencer      INFO      Processed 1000 events in 66.297680 s (wall clock)
14:16:38    Sequencer      INFO      Average time per event: 66.218186 ms/event

- File Size:
754161786 21 Jan 14:16 geant4_material_tracks_rntuple.root

(THIS PR) TTree backend:

- Timing:
14:28:30    Sequencer      INFO      Processed 1000 events in 81.241190 s (wall clock)
14:28:30    Sequencer      INFO      Average time per event: 80.542661 ms/event

- File Size:
1154970434 21 Jan 11:22 geant4_material_tracks_ttree.root

--- END COMMIT MESSAGE ---

Any further description goes here, @-mentions are ok here!

  • Use a conventional commits prefix: quick summary
    • We mostly use feat, fix, refactor, docs, chore and build types.
  • A milestone will be assigned by one of the maintainers

Summary by CodeRabbit

Release Notes

  • New Features

    • Added support for ROOT RNTuple and expanded ROOT framework integration.
    • Enhanced material track reading and writing capabilities.
    • Introduced a new option to build the ROOT plugin.
  • Improvements

    • Streamlined material track data handling.
    • Simplified configuration for ROOT-based material tracking.
    • Added new ROOT plugin component.
  • Technical Updates

    • Updated CMake configurations to support new ROOT components.
    • Introduced RootMaterialTrack class for more efficient material track management.
    • Updated documentation to include new CMake option for ROOT plugin.

@asalzburger asalzburger added this to the next milestone Jan 21, 2025
Copy link

coderabbitai bot commented Jan 21, 2025

Walkthrough

Enhancements to the ROOT material track handling in the ACTS project, this pull request introduces. A new RootMaterialTrack class in the ROOT plugin, it adds. Modifications to material track reading and writing components, and updates to CMake configurations to support these changes, it makes. A more modular approach to handling ROOT-based material track interactions, the modifications streamline.

Changes

File Change Summary
CMakeLists.txt Added option for building ROOT plugin and updated required package components to RIO and ROOTDataFrame.
Examples/Io/Root/CMakeLists.txt Updated ActsExamplesIoRoot library linking to include ActsPluginRoot.
Plugins/CMakeLists.txt Added declaration for the Root plugin component.
Plugins/Root/CMakeLists.txt Created configuration for new ActsPluginRoot library.
Plugins/Root/include/Acts/Plugins/Root/RootMaterialTrack.hpp Introduced new header defining RootMaterialTrack class with configuration and payload management.
Examples/Io/Root/src/RootMaterialTrackReader.cpp Refactored to utilize Acts::RootMaterialTrack for simplified material track reading.
Examples/Io/Root/src/RootMaterialTrackWriter.cpp Streamlined writing logic to use Acts::RootMaterialTrack.
CI/codespell_ignore.txt Added "mata" to ignore list and removed multiple misspellings.
CMakePresets.json Added cache variable to enable ROOT plugin in CI builds.
docs/getting_started.md Documented new CMake option for building the ROOT plugin.

Suggested Labels

Component - Core, Track Finding, Changes Performance, automerge

Suggested Reviewers

  • paulgessinger
  • andiwand

Poem

In the realm of ROOT, tracks do flow,
A plugin's magic, watch it grow! 🌱
With each new line, a dance of code,
Material whispers, on this road.
Simplified paths, in data's light,
A coder's joy, oh what a sight! 🚀

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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 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.

@github-actions github-actions bot added Component - Examples Affects the Examples module Component - Plugins Affects one or more Plugins labels Jan 21, 2025
@asalzburger asalzburger marked this pull request as draft January 21, 2025 13:47
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: 1

🧹 Nitpick comments (6)
Plugins/Root/include/Acts/Plugins/Root/RootMaterialTrack.hpp (5)

35-37: Typographical error in comment, there is.

In the comment on line 37, "aut" should be corrected to "out" or "both". Clearer, it will be.


37-39: "Correspond", the correct spelling is.

On lines 39 and 40, "correpond" should be "correspond". Improve clarity, this will.


179-179: Misspelled "method", you have.

In the comments on lines 179 and 184, "mehtod" should be "method". Attention to detail, important it is.

Also applies to: 184-184


181-181: Typo in parameter description, there is.

On line 181, "wread" should be "read". Clear communication, ensure you must.


210-210: Correct the spelling of "Payload", you should.

In the comment on line 210, "Paylod" should be "Payload". Precision, value highly we do.

Examples/Io/Root/src/RootMaterialTrackWriter.cpp (1)

61-61: Update the comment to reflect RNTupleModel initialization, you must.

The comment mentions RDataFrame, but initializing RNTupleModel, the code is.

Apply this diff to correct the comment:

-    /// Initialize the RDataFrame
+    /// Initialize the RNTupleModel
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between fc18b59 and 9f4cafb.

📒 Files selected for processing (10)
  • CMakeLists.txt (1 hunks)
  • Examples/Io/Root/CMakeLists.txt (1 hunks)
  • Examples/Io/Root/include/ActsExamples/Io/Root/RootMaterialTrackReader.hpp (3 hunks)
  • Examples/Io/Root/include/ActsExamples/Io/Root/RootMaterialTrackWriter.hpp (5 hunks)
  • Examples/Io/Root/src/RootMaterialTrackReader.cpp (4 hunks)
  • Examples/Io/Root/src/RootMaterialTrackWriter.cpp (4 hunks)
  • Plugins/CMakeLists.txt (1 hunks)
  • Plugins/Root/CMakeLists.txt (1 hunks)
  • Plugins/Root/include/Acts/Plugins/Root/RootMaterialTrack.hpp (1 hunks)
  • Plugins/Root/src/RootMaterialTrack.cpp (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: missing_includes
  • GitHub Check: build_debug
🔇 Additional comments (19)
Examples/Io/Root/include/ActsExamples/Io/Root/RootMaterialTrackReader.hpp (3)

13-13: Include RootMaterialTrack.hpp, wisely you have.

Necessary for utilizing Acts::RootMaterialTrack, it is.


41-41: Inheritance from Acts::RootMaterialTrack::Config, beneficial it is.

Streamlines configuration, this change does.


101-102: Member m_rootMaterialTrack, introduced you have.

Ensure proper initialization and usage, you must. Verify that all methods utilize this member correctly.

Examples/Io/Root/include/ActsExamples/Io/Root/RootMaterialTrackWriter.hpp (5)

11-16: Essential includes, added they are.

Dependencies for Acts::Definitions::Algebra and WriterT, required they are.


28-32: Forward declaration of RNTupleWriter, cautiously proceed you should.

Ensure that forward declaration suffices and no incomplete type issues arise.


60-60: Config inherits from Acts::RootMaterialTrack::Config, harmonized it is.

Enhances code reuse and consistency, this change does.


69-70: Configuration option rnTuple, introduced you have.

Controls RNTuple writing, this addition does. Default value set to false, it is.


108-111: Member variables for RNTuple writing and material track, added they are.

Ensure proper initialization and cleanup, you must. Manage resources correctly, important it is.

Examples/Io/Root/src/RootMaterialTrackReader.cpp (4)

32-33: Initialize m_rootMaterialTrack with config, correctly you have.

Configures material track reader appropriately, this does.


51-51: Reading initialized with initializeRead, well done this is.

Prepares input chain for reading, this method does.


74-74: Destructor simplified, but mindful you must be.

Ensure all resources and memory are properly released. Verify that no memory leaks occur.


108-109: Material track read with m_rootMaterialTrack.read(), simplified it is.

Streamlines the reading process, this change does.

Plugins/Root/include/Acts/Plugins/Root/RootMaterialTrack.hpp (1)

1-212: Code structure and implementation, commendable it is.

Well-designed and consistent the class appears. Good work, you have done.

Plugins/Root/src/RootMaterialTrack.cpp (1)

1-471: Implementation, strong it is.

Thoroughly reviewed, I have. Functionality correct and efficient, it seems.

Plugins/Root/CMakeLists.txt (1)

1-20: CMake configuration, proper it appears.

No issues found in your build setup. Proceed, you may.

Plugins/CMakeLists.txt (1)

18-18: Correct, the placement is, but verify dependencies we must!

Before TGeo plugin, the Root plugin stands. Wise this ordering seems, but confirm the dependency chain we should.

Run this command to verify plugin dependencies:

Examples/Io/Root/CMakeLists.txt (1)

42-42: Wise addition to dependencies, this is!

Public linking of ActsPluginRoot, proper it is. Harmony with the new Root plugin architecture, it brings.

CMakeLists.txt (1)

414-414: Additional ROOT components, required they are. But verify their presence in minimum version, we must!

RIO and ROOTDataFrame components, added they were. Essential for material track handling, they are. But ensure their availability in minimum ROOT version ${_acts_root_version}, we should.

Run this command to verify component availability:

Examples/Io/Root/src/RootMaterialTrackWriter.cpp (1)

77-77: Ensure destructor handles resource cleanup, you should.

Empty the destructor is. If resources need explicit release, handle them here, you must. Confirm that automatic cleanup sufficient is.

Examples/Io/Root/src/RootMaterialTrackWriter.cpp Outdated Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
@github-actions github-actions bot added the Component - Documentation Affects the documentation label Feb 5, 2025
@github-actions github-actions bot added the Infrastructure Changes to build tools, continous integration, ... label Feb 5, 2025
@asalzburger asalzburger marked this pull request as ready for review February 5, 2025 15:22
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: 3

♻️ Duplicate comments (2)
Examples/Io/Root/src/RootMaterialTrackWriter.cpp (2)

66-73: ⚠️ Potential issue

Handle RNTuple case in finalize(), you must.

The finalize method, handle both TTree and RNTuple cases it should.


64-64: ⚠️ Potential issue

Handle cleanup in destructor, you must.

Empty destructor, I see. Resources properly released, they must be. In destructor or finalize method, cleanup should occur.

Apply this diff to ensure proper cleanup:

-RootMaterialTrackWriter::~RootMaterialTrackWriter() {}
+RootMaterialTrackWriter::~RootMaterialTrackWriter() {
+  if (m_outputFile != nullptr) {
+    m_outputFile->Close();
+    m_outputFile = nullptr;
+  }
+}
🧹 Nitpick comments (7)
Plugins/Root/src/RootMaterialTrack.cpp (5)

363-366: Replace dynamic_cast with type() check, you should.

Performance impact, dynamic_cast has. More efficient approach using surface->type(), available it is.

Apply this diff for better performance:

-        auto radialBounds = dynamic_cast<const RadialBounds*>(&surfaceBounds);
-        auto cylinderBounds =
-            dynamic_cast<const CylinderBounds*>(&surfaceBounds);
-        if (radialBounds != nullptr) {
+        if (surface->type() == Surface::RadialBounds) {
+          const auto& radialBounds = static_cast<const RadialBounds&>(surfaceBounds);

379-379: Magic number -1, replace with named constant you must.

Clear meaning to magic number -1, give we should. Understanding and maintenance, easier it becomes.

Add this constant at the top of the file:

+  static constexpr int UNKNOWN_SURFACE_TYPE = -1;
-        m_payload.surfaceType->emplace_back(-1);
+        m_payload.surfaceType->emplace_back(UNKNOWN_SURFACE_TYPE);

432-435: Early continue with zero step length, document you should.

Reason for skipping zero step length, clear it must be. Documentation helps understanding.

Add comment explaining the skip:

     double s = m_payload.stepLength->at(is);
+    // Skip material interactions with zero thickness as they don't contribute
     if (s == 0) {
       continue;
     }

363-377: Safer handling of surface bounds, implement you should.

Dynamic casting for bounds checking, risky it is. Visitor pattern, a better approach would be.

Consider implementing a visitor pattern:

-auto radialBounds = dynamic_cast<const RadialBounds*>(&surfaceBounds);
-auto cylinderBounds = dynamic_cast<const CylinderBounds*>(&surfaceBounds);
-if (radialBounds != nullptr) {
-  m_payload.surfaceRangeMin->emplace_back(radialBounds->rMin());
-  m_payload.surfaceRangeMax->emplace_back(radialBounds->rMax());
-} else if (cylinderBounds != nullptr) {
+class BoundsVisitor {
+public:
+  void visit(const RadialBounds& bounds) {
+    min = bounds.rMin();
+    max = bounds.rMax();
+  }
+  void visit(const CylinderBounds& bounds) {
+    min = -bounds.get(CylinderBounds::eHalfLengthZ);
+    max = bounds.get(CylinderBounds::eHalfLengthZ);
+  }
+  double min = 0;
+  double max = 0;
+};

363-366: Use type() instead of dynamic_cast, you should.

Performance impact, dynamic_cast has. Better approach using surface->type() and switch statement would be.

Consider this alternative approach:

-        auto radialBounds = dynamic_cast<const RadialBounds*>(&surfaceBounds);
-        auto cylinderBounds =
-            dynamic_cast<const CylinderBounds*>(&surfaceBounds);
-        if (radialBounds != nullptr) {
+        switch(surface->type()) {
+        case Surface::Disc:
CMakeLists.txt (1)

174-177: Dependency alignment between ROOT and TGeo, prudent it is!
Using set_option_if to tie ACTS_BUILD_PLUGIN_ROOT to ACTS_BUILD_PLUGIN_TGEO ensures that your configuration flows smoothly. Verify that this dependency meets your design intentions, you should.

Examples/Io/Root/include/ActsExamples/Io/Root/RootMaterialTrackWriter.hpp (1)

56-65: Document configuration options better, you should.

Clear documentation for configuration options, missing it is. Help future Padawans understand the code better, it would.

Add documentation for each configuration option:

 struct Config : public Acts::RootMaterialTrack::Config {
+    /// @brief Collection of material tracks to write
+    /// @note Used as input for the writer
     std::string inputMaterialTracks = "material_tracks";
+    /// @brief Path where the output file will be written
     std::string filePath = "";
+    /// @brief Mode for opening the file (e.g., "RECREATE", "UPDATE")
     std::string fileMode = "RECREATE";
+    /// @brief Name of the tree within the ROOT file
     std::string treeName = "material_tracks";
 };
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 9f4cafb and eba478b.

📒 Files selected for processing (11)
  • CI/codespell_ignore.txt (1 hunks)
  • CMakeLists.txt (3 hunks)
  • CMakePresets.json (1 hunks)
  • Examples/Io/Root/include/ActsExamples/Io/Root/RootMaterialTrackReader.hpp (3 hunks)
  • Examples/Io/Root/include/ActsExamples/Io/Root/RootMaterialTrackWriter.hpp (4 hunks)
  • Examples/Io/Root/src/RootMaterialTrackWriter.cpp (3 hunks)
  • Plugins/CMakeLists.txt (1 hunks)
  • Plugins/Root/CMakeLists.txt (1 hunks)
  • Plugins/Root/include/Acts/Plugins/Root/RootMaterialTrack.hpp (1 hunks)
  • Plugins/Root/src/RootMaterialTrack.cpp (1 hunks)
  • docs/getting_started.md (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • Plugins/CMakeLists.txt
  • Plugins/Root/CMakeLists.txt
  • Plugins/Root/include/Acts/Plugins/Root/RootMaterialTrack.hpp
👮 Files not reviewed due to content moderation or server errors (4)
  • Examples/Io/Root/src/RootMaterialTrackWriter.cpp
  • Examples/Io/Root/include/ActsExamples/Io/Root/RootMaterialTrackReader.hpp
  • Examples/Io/Root/include/ActsExamples/Io/Root/RootMaterialTrackWriter.hpp
  • Plugins/Root/src/RootMaterialTrack.cpp
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: macos
  • GitHub Check: build_debug
🔇 Additional comments (15)
Examples/Io/Root/include/ActsExamples/Io/Root/RootMaterialTrackReader.hpp (3)

41-48: Well structured, this code is.

Clean inheritance from RootMaterialTrack::Config and clear configuration parameters, I see. Good practices, followed they are.


41-48: Wise changes in naming convention, these are.

From hyphen to underscore, the names have changed. Consistency with modern C++ practices, this brings.


41-48: Well structured the code is, hmmmm.

Thread-safe and memory-safe, this implementation is. Good practices it follows.

Examples/Io/Root/include/ActsExamples/Io/Root/RootMaterialTrackWriter.hpp (2)

56-65: Consistent with reader implementation, this is.

Good symmetry between reader and writer implementations, I observe. Clear configuration structure, maintained it is.


56-65: Symmetry with Reader changes, maintained it is.

Mirror the Reader's changes, this does. Consistency across codebase, it brings.

CI/codespell_ignore.txt (1)

22-22: Addition of 'mata', wise decision it is!
The new word "mata" now ignored by codespell, ensuring false alerts vanish from your path. Approved, this change is.

CMakePresets.json (1)

69-69: New cache variable for the ROOT plugin, you have enabled!
Setting "ACTS_BUILD_PLUGIN_ROOT" to "ON" in the ci-common preset supports the new functionality well. Consistency with the broader configuration, it maintains.

CMakeLists.txt (4)

89-89: New ROOT plugin option introduced, it is!
Declaring "ACTS_BUILD_PLUGIN_ROOT" with a default of OFF wisely allows toggling the new plugin functionality. Trust in this configuration, you may.


178-181: Linking ROOT plugin with Analysis Apps, smart the dependency chain is!
The clause making ACTS_BUILD_PLUGIN_ROOT depend on ACTS_BUILD_ANALYSIS_APPS is a logical move. Confirm that this relation reflects the intended behavior in your project design, you must.


417-424: Updated find_package for the ROOT plugin, correct it appears!
By requiring the ROOT components RIO and ROOTDataFrame, this block now supports the new backend effectively. Ensure that your tests confirm compatibility with the designated ROOT version, you will.


427-436: Legacy TGeo plugin block remains, it does.
While the new ROOT plugin uses updated components, the TGeo block still uses Geom and Graf. Advise users to choose the proper plugin flag, for clarity in configuration, you must.

docs/getting_started.md (1)

289-289: Documentation of the new ROOT plugin option, clear it is!
Adding the ACTS_BUILD_PLUGIN_ROOT entry to the build options table enlightens users about this new feature nicely. Approved this documentation update, I have.

Examples/Io/Root/src/RootMaterialTrackWriter.cpp (2)

75-93: Well implemented, the writeT method is.

Thread safety with mutex, it maintains. Material track data, correctly it fills. Good practices, I observe.


66-73: Close all resources in finalize(), you must.

In finalize() method, RNTuple resources not properly closed they are. Memory leaks this may cause.

Plugins/Root/src/RootMaterialTrack.cpp (1)

246-280: Wise memory management, implemented it is.

Reserve vectors before filling, good practice this is. Memory allocations reduced, performance improved.

m_outputFile->Close();
}
}
RootMaterialTrackWriter::~RootMaterialTrackWriter() {}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle resource cleanup in destructor, you must.

Raw pointers m_outputFile and m_outputTree, managed properly they must be. Memory leaks, prevent we should.

Apply this diff to ensure proper cleanup:

-RootMaterialTrackWriter::~RootMaterialTrackWriter() {}
+RootMaterialTrackWriter::~RootMaterialTrackWriter() {
+  if (m_outputFile) {
+    m_outputFile->Close();
+    m_outputFile = nullptr;
+  }
+  m_outputTree = nullptr;  // TTree owned by TFile, delete not needed
+}
📝 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
RootMaterialTrackWriter::~RootMaterialTrackWriter() {}
RootMaterialTrackWriter::~RootMaterialTrackWriter() {
if (m_outputFile) {
m_outputFile->Close();
m_outputFile = nullptr;
}
m_outputTree = nullptr; // TTree owned by TFile, delete not needed
}

⚠️ Potential issue

Handle cleanup in destructor, you must.

Empty destructor, resource leaks it may cause. Clean up resources properly in destructor, you should.

Apply this diff to ensure proper cleanup:

-RootMaterialTrackWriter::~RootMaterialTrackWriter() {}
+RootMaterialTrackWriter::~RootMaterialTrackWriter() {
+  if (m_outputFile != nullptr) {
+    m_outputFile->Close();
+    m_outputFile = nullptr;
+  }
+}
📝 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
RootMaterialTrackWriter::~RootMaterialTrackWriter() {}
RootMaterialTrackWriter::~RootMaterialTrackWriter() {
if (m_outputFile != nullptr) {
m_outputFile->Close();
m_outputFile = nullptr;
}
}

Comment on lines +206 to +414

// Record pre/post step
if (m_cfg.prePostStep) {
Vector3 prePos = mint.position - 0.5 * mint.pathCorrection * direction;
Vector3 posPos = mint.position + 0.5 * mint.pathCorrection * direction;

m_payload.stepXs->emplace_back(prePos.x());
m_payload.stepYs->emplace_back(prePos.y());
m_payload.stepZs->emplace_back(prePos.z());
m_payload.stepXe->emplace_back(posPos.x());
m_payload.stepYe->emplace_back(posPos.y());
m_payload.stepZe->emplace_back(posPos.z());
}

// Store surface information
if (m_cfg.storeSurface) {
const Surface* surface = mint.surface;
if (mint.intersectionID.value() != 0) {
m_payload.surfaceId->emplace_back(mint.intersectionID.value());
m_payload.surfacePathCorrection->emplace_back(mint.pathCorrection);
m_payload.surfaceX->emplace_back(mint.intersection.x());
m_payload.surfaceY->emplace_back(mint.intersection.y());
m_payload.surfaceZ->emplace_back(mint.intersection.z());
m_payload.surfaceR->emplace_back(
VectorHelpers::perp(mint.intersection));
m_payload.surfaceDistance->emplace_back(
(mint.position - mint.intersection).norm());
} else if (surface != nullptr) {
auto sfIntersection =
surface
->intersect(gctx, mint.position, mint.direction,
BoundaryTolerance::None())
.closest();
m_payload.surfaceId->emplace_back(surface->geometryId().value());
m_payload.surfacePathCorrection->emplace_back(1.0);
m_payload.surfaceX->emplace_back(sfIntersection.position().x());
m_payload.surfaceY->emplace_back(sfIntersection.position().y());
m_payload.surfaceZ->emplace_back(sfIntersection.position().z());
} else {
m_payload.surfaceId->emplace_back(GeometryIdentifier().value());
m_payload.surfaceX->emplace_back(0);
m_payload.surfaceY->emplace_back(0);
m_payload.surfaceZ->emplace_back(0);
m_payload.surfacePathCorrection->emplace_back(1.0);
}
if (surface != nullptr) {
m_payload.surfaceType->emplace_back(surface->type());
const SurfaceBounds& surfaceBounds = surface->bounds();
auto radialBounds = dynamic_cast<const RadialBounds*>(&surfaceBounds);
auto cylinderBounds =
dynamic_cast<const CylinderBounds*>(&surfaceBounds);
if (radialBounds != nullptr) {
m_payload.surfaceRangeMin->emplace_back(radialBounds->rMin());
m_payload.surfaceRangeMax->emplace_back(radialBounds->rMax());
} else if (cylinderBounds != nullptr) {
m_payload.surfaceRangeMin->emplace_back(
-cylinderBounds->get(CylinderBounds::eHalfLengthZ));
m_payload.surfaceRangeMax->emplace_back(
cylinderBounds->get(CylinderBounds::eHalfLengthZ));
} else {
m_payload.surfaceRangeMin->emplace_back(0);
m_payload.surfaceRangeMax->emplace_back(0);
}
} else {
m_payload.surfaceType->emplace_back(-1);
m_payload.surfaceRangeMin->emplace_back(0);
m_payload.surfaceRangeMax->emplace_back(0);
}
}

// store volume information
if (m_cfg.storeVolume) {
GeometryIdentifier vlayerID;
if (!mint.volume.empty()) {
vlayerID = mint.volume.geometryId();
m_payload.volumeId->emplace_back(vlayerID.value());
} else {
vlayerID.setVolume(0);
vlayerID.setBoundary(0);
vlayerID.setLayer(0);
vlayerID.setApproach(0);
vlayerID.setSensitive(0);
m_payload.volumeId->emplace_back(vlayerID.value());
}
}

// the material information
const auto& mprops = mint.materialSlab;
m_payload.stepLength->emplace_back(mprops.thickness());
m_payload.matX0->emplace_back(mprops.material().X0());
m_payload.matL0->emplace_back(mprops.material().L0());
m_payload.matA->emplace_back(mprops.material().Ar());
m_payload.matZ->emplace_back(mprops.material().Z());
m_payload.matRho->emplace_back(mprops.material().massDensity());
// re-calculate if defined to do so
if (m_cfg.recalculateTotals) {
(*m_payload.tX0) += mprops.thicknessInX0();
(*m_payload.tL0) += mprops.thicknessInL0();
}
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Break down large fill method, you must.

Too many responsibilities the fill method has. Harder to maintain and understand it becomes. Split into smaller, focused methods you should.

Consider extracting these methods:

+void fillTrackInfo(const RecordedMaterialTrack& rmTrack, const Auxiliaries& aux);
+void fillMaterialProperties(const MaterialInteraction& mint);
+void fillSurfaceInfo(const MaterialInteraction& mint, const GeometryContext& gctx);
+void fillVolumeInfo(const MaterialInteraction& mint);

Committable suggestion skipped: line range outside the PR's diff.

Copy link

sonarqubecloud bot commented Feb 5, 2025

Copy link

github-actions bot commented Feb 5, 2025

📊: Physics performance monitoring for eba478b

Full contents

physmon summary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component - Documentation Affects the documentation Component - Examples Affects the Examples module Component - Plugins Affects one or more Plugins Infrastructure Changes to build tools, continous integration, ...
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant