-
Notifications
You must be signed in to change notification settings - Fork 3
feat(lint): fence unit conventions with prek hooks #74
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
Merged
+116
−18
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <!-- | ||
| SPDX-FileCopyrightText: 2026 CERN for the benefit of the SHiP Collaboration | ||
|
|
||
| SPDX-License-Identifier: LGPL-3.0-or-later | ||
| --> | ||
|
|
||
| # Unit handling | ||
|
|
||
| FairShip was plagued by unit-mismatch bugs; aegir prevents them | ||
| structurally. The rules: | ||
|
|
||
| ## Canonical units and types | ||
|
|
||
| The framework-wide vocabulary lives in `SHiP/Units.hpp` (shipdatamodel, | ||
| `SHiP::SHiPUnits` target), built on [mp-units](https://mpusz.github.io/mp-units/): | ||
|
|
||
| | Quantity | Type | Storage unit | | ||
| |-----------------|-----------------------|--------------| | ||
| | Length/position | `ship::Length` | mm | | ||
| | Time | `ship::Time` | ns | | ||
| | Energy | `ship::Energy` | GeV | | ||
| | Momentum | `ship::Momentum` | GeV/c | | ||
| | Mass | `ship::Mass` | GeV/c² | | ||
| | Angle | `ship::Angle` | rad | | ||
| | Magnetic field | `ship::MagneticField` | T | | ||
|
|
||
| `GeV/c` and `mm/c` are real derived units built from the exact SI | ||
| definition of c, so `energy + momentum` is a compile error and | ||
| conversions such as Pythia's `tProd()` (mm/c → ns) are derived rather | ||
| than hand-typed. Persisted data-model structs stay plain doubles in the | ||
| storage units (ROOT cannot stream quantity types); `SHiP/QuantityView.hpp` | ||
| provides typed views over them. | ||
|
|
||
| ## The one rule at boundaries | ||
|
|
||
| **A raw double acquires its unit on the line it enters, and only leaves | ||
| a quantity via an explicit conversion.** | ||
|
|
||
| - Geant4/CLHEP: only through `src/units/clhep_bridge.hpp` — the single | ||
| file allowed to reference CLHEP unit constants. `G4SystemOfUnits.hh` | ||
| is banned repo-wide (it injects global `mm`, `GeV`, `tesla`, ... that | ||
| collide with the mp-units vocabulary). | ||
| - Pythia8 (GeV, mm, mm/c), GENIE (SI): values are wrapped at the read | ||
| site (`p.tProd() * su::mm_per_c`, `vtx[0] * su::m`). | ||
| - Configuration: workflow files stay plain numbers; C++ reads them with | ||
| `aegir::get_quantity(config, "beam_energy", 400.0 * su::GeV)`, making | ||
| the parameter definition the single source of truth for the unit. | ||
|
|
||
| Never `using namespace ship::units` in a translation unit that includes | ||
| CLHEP or Geant4 headers — always qualify. | ||
|
|
||
| ## Enforcement | ||
|
|
||
| prek hooks (see `.pre-commit-config.yaml`) fail on: hardcoded c | ||
| (299792458, 299.792458 or 2.99792458e8, digit separators allowed), any | ||
| repo-wide `G4SystemOfUnits.hh` include, `CLHEP/Units/` includes or | ||
| `CLHEP::` symbols outside `src/units/clhep_bridge.hpp`, and | ||
| `1e±3`/`1e±9`-style factors next to unit conversion comments. Review rule: no raw double crosses a function | ||
| boundary with an implicit unit — either the parameter is a quantity | ||
| type, or the conversion happens on the line the value is read. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.