Skip to content

[PWGEM] Fix TTCA weight miscalculation when cfgRequireTrueAssociation is enabled#46

Closed
Copilot wants to merge 6 commits intomasterfrom
copilot/fix-track-weight-calculation
Closed

[PWGEM] Fix TTCA weight miscalculation when cfgRequireTrueAssociation is enabled#46
Copilot wants to merge 6 commits intomasterfrom
copilot/fix-track-weight-calculation

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 14, 2026

When cfgRequireTrueAssociation = true, runQCMC only fills histograms for tracks whose MC collision matches the reconstructed collision, but fillTrackWeightMap counted all ambiguous associations — assigning weight 1/N even though the track only fills histograms once. This caused systematic undercorrection for ambiguous tracks.

Changes

  • PWGEM/Dilepton/Core/SingleTrackQCMC.hfillTrackWeightMap: Added the same cfgRequireTrueAssociation guard present in runQCMC to both the kDielectron and kDimuon track collection loops:
if (cfgRequireTrueAssociation && (mctrack.emmceventId() != collision.emmceventId())) {
  continue;
}

This ensures that when cfgRequireTrueAssociation = true, only the single true-MC-matched association is counted, so the computed weight is 1 rather than 1/N — consistent with how the weight is consumed downstream.

Original prompt

Problem

In PWGEM/Dilepton/Core/SingleTrackQCMC.h, the fillTrackWeightMap function calculates TTCA (Time To Collision Association) weights without respecting the cfgRequireTrueAssociation configuration.

Issue Description

When cfgRequireTrueAssociation = TRUE is set, only tracks with correct MC collision association should be filled in histograms. However, the weight calculation in fillTrackWeightMap does not check this condition.

Scenario with the bug:

  • Track T is assigned to collisions C1, C2, C3 (ambiguous track)
  • Track T belongs to C1 (MC-wise)
  • In fillTrackWeightMap: weight = 1/3 (counts all 3 collisions)
  • In runQCMC with cfgRequireTrueAssociation = TRUE: track only fills histogram in C1
  • Result: Track contributes 1/3 instead of 1 → undercorrection

Root Cause

The fillTrackWeightMap function (lines 1025-1119) collects all tracks passing kinematic/PID cuts but does not apply the cfgRequireTrueAssociation check. This causes a mismatch between:

  1. Weight calculation (counts all ambiguous associations)
  2. Histogram filling (only counts true associations when flag is enabled)

Solution

Add the cfgRequireTrueAssociation check in fillTrackWeightMap so that:

  • When cfgRequireTrueAssociation = TRUE: Only count tracks in collisions where MC collision ID matches rec collision ID
  • Weights are calculated consistently with how they will be used in runQCMC

Expected Behavior After Fix

When both cfgRequireTrueAssociation = TRUE and cfgApplyWeightTTCA = TRUE:

  • Track contributions to histograms = 1 (not 1/3 or other fractions due to mismatched weight calculations)

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: In dem Task SingleTrackQCMC.h task gibt es die Configurable cfgRequireTrueAssociation.
Wie beeinflusst diese das füllen der Histogramme?
In dem Task kann zusätzlich die TTCAweigths angewendet werden, die den Beitrag gewichten. Erkläre mir wie diese Gewichte berechnet werden, und ob die cfgRequireTrueAssociation das Gewicht beeinflusst.

cfgRequireTrueAssociation sollte nur dann Tracks in die Histogramme füllen, wenn der Track tatsächlich zur Kollision gehört. Das weight sollte dementsprechend angepasst werden, sodass das weight nur dann von 1 verschieden ist, wenn tracks mehrfach aufgrund der Assoziierung von dem selben tracks zu unterschiedlichen Kollisionen stattfindet und dadurch mehrfach in das selbe Histogramm gefüllt wird.

Assistant: Ich werde mir den Task SingleTrackQCMC.h ansehen und die Auswirkungen von cfgRequireTrueAssociation und den TTCAweights analysieren.

Assistant: Lass mich die konkrete SingleTrackQCMC.h Datei in der Dilepton-Struktur finden:

Assistant: Jetzt habe ich den vollständigen SingleTrackQCMC.h Code. Lassen Sie mich eine detaillierte Analyse erstellen:

Analyse: cfgRequireTrueAssociation und TTCAweights in SingleTrackQCMC

1. Wirkung von cfgRequireTrueAssociation auf die Histogrammfüllung

Die Configurable cfgRequireTrueAssociation (Zeile 101) steuert, ob nur Tracks akzeptiert werden, deren MC-Partikel zur gleichen MC-Collision gehört wie die rekonstruierte Collision.

In der runQCMC-Funktion (Zeile 862-864):

if (cfgRequireTrueAssociation && (mctrack.emmceventId() != collision.emmceventId())) {
  continue;  // Track wird NICHT in Histogramme gefüllt
}

Effekt:

  • Wenn FALSE (default): Tracks werden unabhängig davon akzeptiert, ob sie zur richtigen Collision gehören. Dies kann zu Kontamination führen, wenn ein Track mehreren Collisionen zugeordnet wird.
  • Wenn TRUE: Nur Tracks, deren MC-Herkunfts-Collision mit der rekonstruierten Collision übereinstimmt, werden verarbeitet. Dies eliminiert falsch assoziierte Tracks.

2. TTCA Weights - Berechnung und Zweck

TTCA = "Time To Collision Association"

Die Weights werden in der Funktion fillTrackWeightMap (Zeile 1025-1119) berechnet:

std::unordered_map<int, float> map_weight;  // globalIndex -> weight

void fillTrackWeightMap(...) {
  std::vector<int> passed_trackIds;  // Alle Tracks, die cuts bestehen
  
  // ... Tracks sammeln, die Cuts erfüllen ...
  passed_trackIds.emplace_back(track.globalIndex());
  
  // WICHTIG: Für jeden Track alle "ambiguous" (mehrdeutig zugeordneten) Tracks finden
  for (const auto& trackId : passed_trackIds) {
    auto track = tracks.rawIteratorAt(trackId);
    auto ambIds = track.ambiguousElectronsIds();  // oder ambiguousMuonsIds()
    
    float n = 1.f;  // Zähle mich selbst mit
    for (const auto& ambId : ambIds) {
      if (std::find(passed_trackIds.begin(), passed_trackIds.end(), ambId) 
          != passed_trackIds.end()) {
        n += 1.f;  // Zähle jeden Ambiguity, der auch Cuts erfüllt
      }
    }
    map_weight[trackId] = 1.f / n;  // Weight = 1 / Anzahl der Ambiguities
  }
}

Beispiel:

  • Ein Track T ist zu 3 verschiedenen Collisionen zugeordnet → n = 3
  • Weight = 1/3 ≈ `0.33...

This pull request was created from Copilot chat.

Copilot AI changed the title [WIP] Fix track weight calculation to respect true association config Fix TTCA weight miscalculation when cfgRequireTrueAssociation is enabled Apr 14, 2026
Copilot AI requested a review from feisenhu April 14, 2026 11:15
@feisenhu
Copy link
Copy Markdown
Owner

@copilot resolve the merge conflicts in this pull request

Co-authored-by: feisenhu <53603353+feisenhu@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Apr 14, 2026

@copilot resolve the merge conflicts in this pull request

Resolved. The conflict in SingleTrackQCMC.h was between the PR's new TOF nsigma Pi/Ka/Pr histograms and master's switch to o2::framework::kTH2F. The merge keeps all histograms and uses the fully-qualified namespace. Merge commit: f47268b.

@github-actions github-actions bot added the pwgem label Apr 14, 2026
@github-actions github-actions bot changed the title Fix TTCA weight miscalculation when cfgRequireTrueAssociation is enabled [PWGEM] Fix TTCA weight miscalculation when cfgRequireTrueAssociation is enabled Apr 14, 2026
@feisenhu
Copy link
Copy Markdown
Owner

@copilot revert the previous commit which has introduced the merge conflict.
Those changes are not relevant.

This reverts commit f47268b, reversing
changes made to 76e64b5.

Co-authored-by: feisenhu <53603353+feisenhu@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Apr 14, 2026

@copilot revert the previous commit which has introduced the merge conflict.
Those changes are not relevant.

Done. Reverted the merge commit in eed1373 — the PR branch now only contains the fillTrackWeightMap fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants