Skip to content

Matching: store all pairs in CSR layout (fixes lost 1:n / n:1 pairs) - #156

Merged
lubynets merged 2 commits into
HeavyIonAnalysis:masterfrom
fjlinz:matching
Nov 11, 2026
Merged

lubynets merged 2 commits into
HeavyIonAnalysis:masterfrom
fjlinz:matching

Conversation

@fjlinz

@fjlinz fjlinz commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Motivation

Matching stored two persistent std::map<int,int> (direct and inverted). A map keeps one value per key and insert() silently ignores duplicates, so:

  • 1:n / n:1 matchings lose pairs. Example from CBM: VtxTracks2FsdHits holds ~3 pairs/event in match_ but ~120 in match_inverted_; for TofHits2SimParticles the inverted map drops hits sharing one SimParticle.
  • 1:1 matchings are stored twice (~1.8 % of the file size).
  • Reading builds two maps node by node for every entry.

Changes

New storage (CSR layout), ClassDef(Matching, 3)

  • All pairs are kept; nothing redundant on disk.
  • Direct lookup is O(1), with no work needed after GetEntry().
  • AddMatch() is O(1) for increasing id1 and inserts in place otherwise (e.g. converters looping over hits).
  • The inverse direction (id2 → id1) is transient: it is built on first use and invalidated by AddMatch(), Clear() and an I/O read rule after each read.

Backward compatibility (reading)

  • An I/O read rule converts Matching version ≤ 2 (two maps) on reading. Pairs from both legacy maps are kept, so old files also regain pairs that were lost in one of the maps.
  • ⚠ Older AnalysisTree versions cannot read files written with this version.

API

before now
all partners of an object – GetMatchesDirect(id), GetMatchesInverted(id), GetMatches(id, is_inverted) → MatchRange
first partner GetMatch(id, is_inverted) unchanged (inverted: smallest id1 instead of first inserted)
iteration helpers GetMatches(bool) → const std::map& GetNumberOfPairs(), GetNumberOfIds(is_inverted)
removed SetMatches(), constructor taking maps –

AddMatch() now throws std::invalid_argument for negative ids (ids are channel indices).

Adapted users: AnalysisEntry and infra-1.0 VarManagerEntry loop over ids with GetMatch() and keep their behaviour (first partner per object). No users of the removed map API were found in AnalysisTreeQA, cbmatqa, cbmroot/analysis, Centrality, Efficiency, EventMixer or Tools.

Tests

New tests in core/Matching.test.cpp:

  • basics and invalid ids
  • multiple partners (1:n, n:1, gaps)
  • unordered insertion
  • invalidation of the inverse index on modification
  • conversion of legacy maps
  • write/read round trip over several entries of a split branch, including rebuilding the inverse index per entry

@fjlinz

fjlinz commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@lubynets Dear Oleksii, this is an update on the matching with structural changes, there old AT versions will not be able to read newer AT files anymore. If you are fine with the changes proposed here, we would need to have an updated version for external usage. Many thanks, Frederic.

@lubynets lubynets left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi Frederic, thanks for the development!

  1. The PR is fine for me with one minor comment (in the code).
  2. It's fine that the newly created files are not readable by old AT versions, provided that the reading is possible other way around: old files with the updated AT (afaiu it is?)
  3. By updated version for external usage you mean creating a new tag, am I right?

Comment thread core/Matching.hpp
*/
class MatchRange {
public:
MatchRange() = default;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can this constructor be deleted? - since there is no use case to have member variables as nullptr (please correct me if I am wrong), and there is no way to change them once default constructor was called.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The default constructor is used: GetMatchesDirect() / GetMatchesInverted() return {} when an object has no partner (Matching.cpp, lines 32 and 42). {nullptr, nullptr} is a valid empty range (begin() == end(), size() == 0, empty() == true, range-for does nothing). And MatchRange is a read-only view by design, so it is never modified after construction anyway.

If you prefer, I can remove the default constructor and return MatchRange(nullptr, nullptr) explicitly instead. The behaviour is identical, it only makes the empty case more visible in the code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

(2) Yes, exactly. Old files (Matching version ≤ 2, two std::maps) are readable with the updated AnalysisTree. An I/O read rule in AnalysisTreeCoreLinkDef.h converts the two legacy maps into the new CSR layout during reading, so no user action is needed. The conversion even takes the pairs from both legacy maps, so old files regain pairs that were lost in one of them (e.g. 1:n matchings like VtxTracks2FsdHits). GetMatch() returns the same result as before.

The conversion logic itself is covered by the unit test Matching.LegacyMaps. Reading a real v2 file is not part of CI yet because there is no v2 reference file in the repo; I checked it manually with a cbm production file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

(3) Yes :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(1) Sorry, I overlooked that the default constructor is used. Let it be as is (provided that subtraction of two same-type nullptrs is guaranteed to be zero, which I am not 100% sure, and a quick digest of AI overview gives contradicting answers). If there is a chance that size() will be called to an empty object, it is better to foresee this case explicitly.

(2-3) OK

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Now fixed, however needed to bring back the maps for backward compatibility. So even in the new files, there are still written, but as emply maps, so its cost only few kB which I would consider acceptable.

Another option would be to have a full legacy class for that, but then reading with ROOT only (just the ttree) would results in problems and one would have to apply the legacy matching manually.

What do you think? @lubynets

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If writing of empty maps costs only few KB of memory, and does not confuse users or bring to errors, I think it's ok.

@fjlinz

fjlinz commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@lubynets I think the failed job is not an issue of my code changes. Can you rerun? It seems I do not have rights to do so...

@fjlinz

fjlinz commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@lubynets I think the failed job is not an issue of my code changes. Can you rerun? It seems I do not have rights to do so...

E: Package 'git' has no installation candidate
Error: Process completed with exit code 100.

Seems like a network problem, maybe try again later.

@lubynets

Copy link
Copy Markdown
Contributor

@lubynets I think the failed job is not an issue of my code changes. Can you rerun? It seems I do not have rights to do so...

E: Package 'git' has no installation candidate
Error: Process completed with exit code 100.

Seems like a network problem, maybe try again later.

I tried twice and it crashes again and again, but indeed the crash seems to be unrelated to the PR. I'll check it later, in personal fork.
If the PR is ready from your side (please confirm), I'll merge.

@fjlinz

fjlinz commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@lubynets Yes, from my side everything is in and working.

@lubynets
lubynets merged commit 4146ebb into HeavyIonAnalysis:master Nov 11, 2026
7 of 10 checks passed
@lubynets

Copy link
Copy Markdown
Contributor

@lubynets Yes, from my side everything is in and working.

@fjlinz new tag https://github.com/HeavyIonAnalysis/AnalysisTree/releases/tag/v2.5.0 published

@fjlinz
fjlinz deleted the matching branch November 11, 2026 14:40
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.

2 participants