From 24496bc717aacd7cb9c6336716e6af7a6982a520 Mon Sep 17 00:00:00 2001
From: Florian Uhlig <florian.uhlig@googlemail.com>
Date: Mon, 15 Jul 2024 06:58:36 +0000
Subject: [PATCH] Fix matching

---
 core/base/CbmMatchRecoToMC.cxx | 42 +++++++++++++++++++---------------
 core/data/CbmLink.h            | 14 ++++++++----
 2 files changed, 33 insertions(+), 23 deletions(-)

diff --git a/core/base/CbmMatchRecoToMC.cxx b/core/base/CbmMatchRecoToMC.cxx
index 2b01da99d4..a673144d59 100644
--- a/core/base/CbmMatchRecoToMC.cxx
+++ b/core/base/CbmMatchRecoToMC.cxx
@@ -557,9 +557,9 @@ void CbmMatchRecoToMC::MatchHitsFsd(const TClonesArray* hits, TClonesArray* hitM
   if (!(hits && hitMatches)) return;
   Int_t nofHits = hits->GetEntriesFast();
   for (Int_t iHit = 0; iHit < nofHits; iHit++) {
-    CbmPixelHit* hit          = static_cast<CbmPixelHit*>(hits->At(iHit));
-    CbmMatch* hitMatch        = new ((*hitMatches)[iHit]) CbmMatch();
-    Int_t nofDigis            = fDigiManager->GetNofDigis(ECbmModuleId::kFsd);
+    CbmPixelHit* hit   = static_cast<CbmPixelHit*>(hits->At(iHit));
+    CbmMatch* hitMatch = new ((*hitMatches)[iHit]) CbmMatch();
+    Int_t nofDigis     = fDigiManager->GetNofDigis(ECbmModuleId::kFsd);
     for (Int_t iDigi = 0; iDigi < nofDigis; iDigi++) {
       const CbmFsdDigi* digi = fDigiManager->Get<const CbmFsdDigi>(iDigi);
       Int_t adresa_digi      = digi->GetAddress();
@@ -592,7 +592,8 @@ void CbmMatchRecoToMC::MatchHitsTof(const TClonesArray* HitDigiMatches, const TC
 
     Int_t iNbDigisHit = hitDigiMatch->GetNofLinks();
     for (Int_t iDigi = 0; iDigi < iNbDigisHit; iDigi++) {
-      CbmLink lDigi  = hitDigiMatch->GetLink(iDigi);
+      CbmLink lDigi = hitDigiMatch->GetLink(iDigi);
+      if (lDigi.IsNoise()) continue;
       Int_t iDigiIdx = lDigi.GetIndex();
 
       if (iNbTofDigis <= iDigiIdx) {
@@ -615,11 +616,13 @@ void CbmMatchRecoToMC::MatchHitsTof(const TClonesArray* HitDigiMatches, const TC
         LOG(error) << "                                  Digi address: 0x" << std::setw(8) << std::hex
                    << digiTof.GetAddress() << std::dec;
         continue;
-      }                                                     // if( iNbTofDigis <= iDigiIdx )
-      CbmLink lTruePoint  = matchDigiPnt.GetMatchedLink();  // Point generating the Digi
+      }                                                    // if( iNbTofDigis <= iDigiIdx )
+      CbmLink lTruePoint = matchDigiPnt.GetMatchedLink();  // Point generating the Digi
+      if (lTruePoint.IsNoise()) continue;
       Int_t iTruePointIdx = lTruePoint.GetIndex();
       for (Int_t iPoint = 0; iPoint < iNbPointsDigi; iPoint++) {
-        CbmLink lPoint  = matchDigiPnt.GetLink(iPoint);
+        CbmLink lPoint = matchDigiPnt.GetLink(iPoint);
+        if (lPoint.IsNoise()) continue;
         Int_t iPointIdx = lPoint.GetIndex();
 
         if (iPointIdx == iTruePointIdx)
@@ -757,7 +760,8 @@ void CbmMatchRecoToMC::MatchStsTracks(const TClonesArray* mvdHitMatches, const T
       const CbmMatch* hitMatch = static_cast<CbmMatch*>(stsHitMatches->At(track->GetStsHitIndex(iHit)));
       Int_t nofLinks           = hitMatch->GetNofLinks();
       for (Int_t iLink = 0; iLink < nofLinks; iLink++) {
-        const CbmLink& link      = hitMatch->GetLink(iLink);
+        const CbmLink& link = hitMatch->GetLink(iLink);
+        if (link.IsNoise()) continue;
         const FairMCPoint* point = static_cast<const FairMCPoint*>(stsPoints->Get(link));
         assert(point);
         /// fix low energy cut case on STS
@@ -780,7 +784,8 @@ void CbmMatchRecoToMC::MatchStsTracks(const TClonesArray* mvdHitMatches, const T
       const CbmMatch* hitMatch = static_cast<CbmMatch*>(mvdHitMatches->At(track->GetMvdHitIndex(iHit)));
       Int_t nofLinks           = hitMatch->GetNofLinks();
       for (Int_t iLink = 0; iLink < nofLinks; iLink++) {
-        const CbmLink& link      = hitMatch->GetLink(iLink);
+        const CbmLink& link = hitMatch->GetLink(iLink);
+        if (link.IsNoise()) continue;
         const FairMCPoint* point = static_cast<const FairMCPoint*>(mvdPoints->Get(link));
         assert(point);
         trackMatch->AddLink(CbmLink(1., point->GetTrackID(), link.GetEntry(), link.GetFile()));
@@ -869,8 +874,9 @@ void CbmMatchRecoToMC::MatchRichRings(const TClonesArray* richRings, const TClon
 
     if (ringMatch->GetNofLinks() != 0) {
       CbmLink bestTrackLink = ringMatch->GetMatchedLink();
-      Int_t trueCounter     = 0;
-      Int_t wrongCounter    = 0;
+      if (bestTrackLink.IsNoise()) continue;
+      Int_t trueCounter  = 0;
+      Int_t wrongCounter = 0;
       for (Int_t iHit = 0; iHit < nHits; iHit++) {
         const CbmRichHit* hit = static_cast<const CbmRichHit*>(richHits->At(static_cast<Int_t>(ring->GetHit(iHit))));
         if (!hit) continue;
@@ -906,8 +912,7 @@ vector<CbmLink> CbmMatchRecoToMC::GetMcTrackMotherIdsForRichHit(CbmDigiManager*
 
   vector<CbmLink> links = digiMatch->GetLinks();
   for (const auto& link : links) {
-    Int_t pointId = link.GetIndex();
-    if (pointId < 0) continue;  // noise signal from digitizer
+    if (link.IsNoise()) continue;
     const CbmRichPoint* point = static_cast<const CbmRichPoint*>(richPoints->Get(link));
     if (!point) continue;
     Int_t mcTrackId = point->GetTrackID();
@@ -943,9 +948,9 @@ vector<pair<Int_t, Int_t>> CbmMatchRecoToMC::GetMcTrackMotherIdsForRichHit(CbmDi
 
   vector<CbmLink> links = digiMatch->GetLinks();
   for (UInt_t i = 0; i < links.size(); i++) {
-    Int_t pointId = links[i].GetIndex();
-    Int_t eventId = links[i].GetEntry();
-    if (pointId < 0) continue;  // noise hit
+    if (links[i].IsNoise()) continue;
+    Int_t pointId             = links[i].GetIndex();
+    Int_t eventId             = links[i].GetEntry();
     const CbmRichPoint* pMCpt = static_cast<const CbmRichPoint*>(richPoints->Get(0, eventId, pointId));
 
     if (nullptr == pMCpt) continue;
@@ -983,9 +988,8 @@ vector<Int_t> CbmMatchRecoToMC::GetMcTrackMotherIdsForRichHit(CbmDigiManager* di
 
   vector<CbmLink> links = digiMatch->GetLinks();
   for (UInt_t i = 0; i < links.size(); i++) {
-    Int_t pointId = links[i].GetIndex();
-    if (pointId < 0) continue;  // noise hit
-
+    if (links[i].IsNoise()) continue;
+    Int_t pointId             = links[i].GetIndex();
     const CbmRichPoint* pMCpt = static_cast<const CbmRichPoint*>(richPoints->At(pointId));
     if (nullptr == pMCpt) continue;
     Int_t mcTrackIndex = pMCpt->GetTrackID();
diff --git a/core/data/CbmLink.h b/core/data/CbmLink.h
index 0692ca7925..b0c9015cc9 100644
--- a/core/data/CbmLink.h
+++ b/core/data/CbmLink.h
@@ -13,14 +13,14 @@
 #ifndef CBMLINK_H_
 #define CBMLINK_H_
 
-#include <Rtypes.h>      // for THashConsistencyHolder, ClassDef
-#include <TObject.h>     // for TObject
+#include <Rtypes.h>   // for THashConsistencyHolder, ClassDef
+#include <TObject.h>  // for TObject
 
 #include <cstdint>
 #include <string>  // for string
 
 class CbmLink : public TObject {
-public:
+ public:
   /**
     * \brief Constructor.
     */
@@ -82,8 +82,14 @@ public:
     return l.GetFile() > r.GetFile();
   }
 
+  bool IsNoise() const
+  {
+    bool retval{false};
+    if (fFile < 0 || fEntry < 0 || fIndex < 0) retval = true;
+    return retval;
+  }
 
-private:
+ private:
   int32_t fFile;   // File ID
   int32_t fEntry;  // Entry number
   int32_t fIndex;  // Index in array
-- 
GitLab