From c649d98b37a850c3f6821312f5462c882520ab15 Mon Sep 17 00:00:00 2001
From: sgorbuno <se.gorbunov@gsi.de>
Date: Mon, 2 Aug 2021 08:47:56 +0000
Subject: [PATCH] L1: clean up the reading of the MC data

---
 reco/L1/CMakeLists.txt       |  30 ++++--
 reco/L1/CbmL1.cxx            |  79 +++------------
 reco/L1/CbmL1.h              |   4 +-
 reco/L1/CbmL1Hit.h           |   2 -
 reco/L1/CbmL1Performance.cxx |  37 +++----
 reco/L1/CbmL1ReadEvent.cxx   | 186 ++++++++++++++---------------------
 6 files changed, 127 insertions(+), 211 deletions(-)

diff --git a/reco/L1/CMakeLists.txt b/reco/L1/CMakeLists.txt
index 5088bb30fc..b9a745afa3 100644
--- a/reco/L1/CMakeLists.txt
+++ b/reco/L1/CMakeLists.txt
@@ -46,7 +46,11 @@ ${CBMROOT_SOURCE_DIR}/reco/KF
 ${CBMROOT_SOURCE_DIR}/reco/KF/KFQA
 ${CBMROOT_SOURCE_DIR}/reco/KF/Interface
 ${CBMROOT_SOURCE_DIR}/run
+${CBMROOT_SOURCE_DIR}/sim/transport/steer
+${CBMROOT_SOURCE_DIR}/sim/transport/geosetup
+
 ${CBMDATA_DIR}
+${CBMDATA_DIR}/base
 
   ${CBMDETECTORBASE_DIR}/sts
 
@@ -212,15 +216,27 @@ ELSE (SSE_FOUND)
   "-O3")
 ENDIF (SSE_FOUND)
 
-set(LINKDEF L1LinkDef.h)
+Set(LINKDEF L1LinkDef.h)
+
 Set(LIBRARY_NAME L1)
+
+Set(DEPENDENCIES
+    KF
+    Base
+    CbmBase
+    CbmData
+#    CbmSimSteer
+#    CbmGeoSetup
+    CbmMuchBase
+    CbmTrdBase
+    CbmStsBase
+    CbmRecoBase
+    CbmRecoSts
+    boost_regex
+   )
+
 if (OPENMP_FOUND AND APPLE)
-  Set(DEPENDENCIES
-    KF CbmMuchBase CbmTrdBase CbmStsBase CbmRecoBase CbmBase CbmData CbmRecoSts Base boost_regex ${OpenMP_CXX_LIBRARIES}
-  )
-else()
-  Set(DEPENDENCIES
-    KF CbmMuchBase CbmTrdBase CbmStsBase CbmRecoBase CbmBase CbmData CbmRecoSts Base boost_regex
+  Set(DEPENDENCIES ${DEPENDENCIES} ${OpenMP_CXX_LIBRARIES}
   )
 endif()
 
diff --git a/reco/L1/CbmL1.cxx b/reco/L1/CbmL1.cxx
index 1e120171c4..aa7ae33598 100644
--- a/reco/L1/CbmL1.cxx
+++ b/reco/L1/CbmL1.cxx
@@ -29,6 +29,8 @@
 #include "CbmMuchStation.h"
 #include "CbmMvdDetector.h"
 #include "CbmMvdStationPar.h"
+// TODO: include of CbmSetup.h creates problems on Mac
+// #include "CbmSetup.h"
 #include "CbmStsFindTracks.h"
 #include "CbmStsParSetModule.h"
 #include "CbmStsParSetSensor.h"
@@ -174,6 +176,10 @@ InitStatus CbmL1::Init()
     fUseMVD                    = 1;
     CbmStsFindTracks* FindTask = L1_DYNAMIC_CAST<CbmStsFindTracks*>(Run->GetTask("STSFindTracks"));
     if (FindTask) fUseMVD = FindTask->MvdUsage();
+    // TODO: include of CbmSetup.h creates problems on Mac
+    // if (!CbmSetup::Instance()->IsActive(ECbmModuleId::kMvd)) { fUseMVD = false; }
+    // N Mvd stations is read from the KF material
+    if (CbmKF::Instance()->vMvdMaterial.size() == 0) { fUseMVD = false; }
   }
 
   fHistoDir = gROOT->mkdir("L1");
@@ -207,6 +213,11 @@ InitStatus CbmL1::Init()
   fMCTracks   = 0;
 
 
+  listMvdHitMatches  = 0;
+  fTrdHitMatches     = 0;
+  listMuchHitMatches = 0;
+  fTofHitDigiMatches = 0;
+
   listStsClusters = 0;
   listStsDigi.clear();
   vFileEvent.clear();
@@ -263,8 +274,6 @@ InitStatus CbmL1::Init()
     fTofHits           = 0;
   }
   else {
-
-
     fTofHits = (TClonesArray*) fManger->GetObject("TofHit");
   }
 
@@ -273,29 +282,21 @@ InitStatus CbmL1::Init()
     if (NULL == mcManager) LOG(fatal) << GetName() << ": No CbmMCDataManager!";
 
     fStsPoints = mcManager->InitBranch("StsPoint");
-    fMvdPoints = mcManager->InitBranch("MvdPoint");
-    fMCTracks  = mcManager->InitBranch("MCTrack");
+
+    fMCTracks = mcManager->InitBranch("MCTrack");
     if (NULL == fStsPoints) LOG(fatal) << GetName() << ": No StsPoint data!";
     if (NULL == fMCTracks) LOG(fatal) << GetName() << ": No MCTrack data!";
 
-    listStsPts = L1_DYNAMIC_CAST<TClonesArray*>(fManger->GetObject("StsPoint"));
-
     if (fTimesliceMode) {
       fEventList = (CbmMCEventList*) fManger->GetObject("MCEventList.");
       if (NULL == fEventList) LOG(fatal) << GetName() << ": No MCEventList data!";
     }
 
-    if (!fUseMVD) {
-      listMvdPts        = 0;
-      listMvdHitMatches = 0;
-    }
-    else {
-      listMvdPts         = L1_DYNAMIC_CAST<TClonesArray*>(fManger->GetObject("MvdPoint"));
+    if (fUseMVD) {
+      fMvdPoints         = mcManager->InitBranch("MvdPoint");
       listMvdDigiMatches = L1_DYNAMIC_CAST<TClonesArray*>(fManger->GetObject("MvdDigiMatch"));
       listMvdHitMatches  = L1_DYNAMIC_CAST<TClonesArray*>(fManger->GetObject("MvdHitMatch"));
-
-      if (!listMvdHitMatches && listMvdPts)
-        LOG(error) << "No listMvdHitMatches provided, performance is not done correctly";
+      if (!listMvdHitMatches) { LOG(error) << "No listMvdHitMatches provided, performance is not done correctly"; }
     }
 
     if (!fUseTRD) {
@@ -331,15 +332,6 @@ InitStatus CbmL1::Init()
     }
   }
   else {
-    listMvdPts         = 0;
-    listMvdHitMatches  = 0;
-    fTrdPoints         = 0;
-    fTrdHitMatches     = 0;
-    fTrdPoints         = 0;
-    fMuchPoints        = 0;
-    listMuchHitMatches = 0;
-    fTofPoints         = 0;
-    fTofHitDigiMatches = 0;
   }
   if (!fUseMVD) { listMvdHits = 0; }
   else {
@@ -1219,27 +1211,6 @@ void CbmL1::Reconstruct(CbmEvent* event)
 #endif
     }
 
-    for (L1Vector<CbmL1MCTrack>::iterator i = vMCTracks.begin(); i != vMCTracks.end(); ++i) {
-      CbmL1MCTrack& MC = *i;
-
-      if (!MC.IsReconstructable()) continue;
-      if (!(MC.ID >= 0)) continue;
-
-      if (MC.StsHits.size() < 4) continue;
-      L1Vector<int> hitIndices("hitIndices", algo->NStations, -1);
-
-      for (unsigned int iH = 0; iH < MC.StsHits.size(); iH++) {
-        const int hitI = MC.StsHits[iH];
-        CbmL1Hit& hit  = const_cast<CbmL1Hit&>(vStsHits[hitI]);
-
-        hit.event = MC.iEvent;
-
-        // const int iStation = vMCPoints[hit.mcPointIds[0]].iStation;
-        // hitIndices[iStation] = hitI;
-      }
-    }
-
-
     if (fVerbose > 1) { cout << "L1 Track finder..." << endl; }
     algo->CATrackFinder();
     // IdealTrackFinder();
@@ -1430,24 +1401,6 @@ void CbmL1::Reconstruct(CbmEvent* event)
 #endif
   }
 
-  for (L1Vector<CbmL1MCTrack>::iterator i = vMCTracks.begin(); i != vMCTracks.end(); ++i) {
-    CbmL1MCTrack& MC = *i;
-
-    if (!MC.IsReconstructable()) continue;
-    if (!(MC.ID >= 0)) continue;
-
-    if (MC.StsHits.size() < 4) continue;
-    L1Vector<int> hitIndices("CbmL1::hitIndices", algo->NStations, -1);
-
-    for (unsigned int iH = 0; iH < MC.StsHits.size(); iH++) {
-      const int hitI = MC.StsHits[iH];
-      CbmL1Hit& hit  = const_cast<CbmL1Hit&>(vStsHits[hitI]);
-
-      hit.event = MC.iEvent;
-    }
-  }
-
-
   // output performance
   if (fPerformance) {
     if (fVerbose > 1) { cout << "Performance..." << endl; }
diff --git a/reco/L1/CbmL1.h b/reco/L1/CbmL1.h
index a2efa047f3..f1c9769486 100644
--- a/reco/L1/CbmL1.h
+++ b/reco/L1/CbmL1.h
@@ -282,20 +282,18 @@ private:
   CbmMCDataArray* fMvdPoints {nullptr};
   CbmMCDataArray* fMCTracks {nullptr};
 
-  TClonesArray* listStsPts {nullptr};  // Sts MC points
   TClonesArray* listStsDigiMatch {nullptr};
   TClonesArray* listStsClusters {nullptr};
   TClonesArray* listStsHits {nullptr};
   TClonesArray* listStsHitMatch {nullptr};
   TClonesArray* listStsClusterMatch {nullptr};
 
-  TClonesArray* listMvdPts {nullptr};  // Mvd MC points
   TClonesArray* listMvdHits {nullptr};
   TClonesArray* listMvdDigiMatches {nullptr};
   TClonesArray* listMvdHitMatches {nullptr};
 
   //MuCh
-  int nMuchPoints {0};
+
   CbmMCDataArray* fMuchPoints {nullptr};
   TClonesArray* listMuchHitMatches {nullptr};  // Output CbmMatch array
   TClonesArray* fDigiMatchesMuch {nullptr};
diff --git a/reco/L1/CbmL1Hit.h b/reco/L1/CbmL1Hit.h
index c6add884a9..45957ddfff 100644
--- a/reco/L1/CbmL1Hit.h
+++ b/reco/L1/CbmL1Hit.h
@@ -25,8 +25,6 @@ struct CbmL1Hit {
   int f     = 0;              // front strip index
   int b     = 0;              // back strip index
   int ID    = 0;              // TODO: check if this ID is redundant
-  int file  = 0;              // TODO: ??
-  int event = 0;              // TODO: ??
   L1Vector<int> mcPointIds {"CbmL1Hit::mcPointIds"};  // indices of CbmL1MCPoint in L1->vMCPoints array
 };
 
diff --git a/reco/L1/CbmL1Performance.cxx b/reco/L1/CbmL1Performance.cxx
index 1dfe8ca5c9..39fc37db21 100644
--- a/reco/L1/CbmL1Performance.cxx
+++ b/reco/L1/CbmL1Performance.cxx
@@ -1806,8 +1806,6 @@ void CbmL1::InputPerformance()
   //  std::map<unsigned int, unsigned int> stripFToNMCMap,stripBToNMCMap;
 
   map<unsigned int, unsigned int>::iterator it;
-  Int_t nMC = -1;
-  if (listStsPts) { nMC = listStsPts->GetEntriesFast(); }
 
   if (listStsHits && listStsHitMatch) {
     for (unsigned int iH = 0; iH < vStsHits.size(); iH++) {
@@ -1904,37 +1902,24 @@ void CbmL1::InputPerformance()
       CbmMvdHit* sh = L1_DYNAMIC_CAST<CbmMvdHit*>(listMvdHits->At(j));
       CbmMatch* hm  = L1_DYNAMIC_CAST<CbmMatch*>(listMvdHitMatches->At(j));
 
-      int iMC = -1;
-      //       float mcWeight = -1.f;
-      //       for(int iDigiLink=0; iDigiLink<hm->GetNofLinks(); iDigiLink++)
-      //       {
-      //         if( hm->GetLink(iDigiLink).GetWeight() > mcWeight)
-      //         {
-      //           mcWeight = hm->GetLink(iDigiLink).GetWeight();
-      //           iMC = hm->GetLink(iDigiLink).GetIndex();
-      //         }
-      //       }
-      if (hm->GetNofLinks() > 0) iMC = hm->GetLink(0).GetIndex();
-
+      CbmMvdPoint* pt = nullptr;
+      {
+        float mcWeight = -1.f;
+        for (int iLink = 0; iLink < hm->GetNofLinks(); iLink++) {
+          const CbmLink& link = hm->GetLink(iLink);
+          if (link.GetWeight() < mcWeight) continue;
+          mcWeight = link.GetWeight();
+          pt       = dynamic_cast<CbmMvdPoint*>(fMvdPoints->Get(&link));
+        }
+      }
+      if (!pt) continue;
 
-      if (iMC < 0) continue;
       // hit pulls and residuals
 
-
       TVector3 hitPos, mcPos, hitErr;
       sh->Position(hitPos);
       sh->PositionError(hitErr);
 
-      CbmMvdPoint* pt = 0;
-      nMC             = listMvdPts->GetEntriesFast();
-
-      if (iMC >= 0 && iMC < nMC) pt = L1_DYNAMIC_CAST<CbmMvdPoint*>(listMvdPts->At(iMC));
-
-      if (!pt) {
-        //         cout << " No MC points! " << "iMC=" << iMC << endl;
-        continue;
-      }
-
       mcPos.SetX((pt->GetX() + pt->GetXOut()) / 2.);
       mcPos.SetY((pt->GetY() + pt->GetYOut()) / 2.);
       mcPos.SetZ(hitPos.Z());
diff --git a/reco/L1/CbmL1ReadEvent.cxx b/reco/L1/CbmL1ReadEvent.cxx
index 92c72ca14c..caa4f4c91a 100644
--- a/reco/L1/CbmL1ReadEvent.cxx
+++ b/reco/L1/CbmL1ReadEvent.cxx
@@ -69,10 +69,10 @@ struct TmpHit {  // used for sort Hits before writing in the normal arrays
   double dx, dy, dxy;
   double du, dv;
   int iMC;  // index of MCPoint in the vMCPoints array
-  double time, dt;
+  double time = 0., dt = 1.e10;
   int Det;
   int id;
-  int track;
+
   static bool Compare(const TmpHit& a, const TmpHit& b)
   {
     return (a.iStation < b.iStation) || ((a.iStation == b.iStation) && (a.y < b.y));
@@ -130,8 +130,13 @@ void CbmL1::ReadEvent(L1AlgoInputData* fData_, float& TsStart, float& TsLength,
     fData_->StsHitsStopIndex[i]  = 0;
   }
 
-  //Int_t nMvdPoints = 0;
-  nMvdPoints = 0;
+
+  nMvdPoints      = 0;
+  int nStsPoints  = 0;
+  int nTrdPoints  = 0;
+  int nMuchPoints = 0;
+  int nTofPoints  = 0;
+
   // get MVD hits
   Int_t nMvdHits  = 0;
   Int_t nMuchHits = 0;
@@ -140,11 +145,6 @@ void CbmL1::ReadEvent(L1AlgoInputData* fData_, float& TsStart, float& TsLength,
   // get STS hits
   int nStsHits = 0;
 
-  int nStsPoints = 0;
-  int nTrdPoints = 0;
-  nMuchPoints    = 0;
-  int nTofPoints = 0;
-
   L1Vector<CbmLink*> ToFPointsMatch("CbmL1ReadEvent::ToFPointsMatch");
 
   if (fPerformance) {
@@ -159,93 +159,74 @@ void CbmL1::ReadEvent(L1AlgoInputData* fData_, float& TsStart, float& TsLength,
       Int_t iEvent = set_it->second;
 
 
-      if (fMvdPoints && listMvdPts) {
+      if (fMvdPoints) {
         Int_t nMvdPointsInEvent = fMvdPoints->Size(iFile, iEvent);
         for (Int_t iMC = 0; iMC < nMvdPointsInEvent; iMC++) {
           CbmL1MCPoint MC;
-
-          MC.event = iEvent;
-
           if (!ReadMCPoint(&MC, iMC, iFile, iEvent, 1)) {
             MC.iStation    = -1;
             L1Station* sta = algo->vStations;
             for (Int_t iSt = 0; iSt < NStsStations; iSt++) {
-              MC.iStation = (MC.z > sta[iSt].z[0] - 1) ? iSt : MC.iStation;
+              if (MC.z > sta[iSt].z[0] - 1) { MC.iStation = iSt; }
             }
-
+            assert(MC.iStation >= 0);
             Double_t dtrck          = dFEI(iFile, iEvent, MC.ID);
             DFEI2I::iterator trk_it = dFEI2vMCTracks.find(dtrck);
-            if (trk_it == dFEI2vMCTracks.end()) continue;
-            Int_t IND_Track = trk_it->second;
-            vMCTracks[IND_Track].Points.push_back_no_warning(vMCPoints.size());
-
+            assert(trk_it != dFEI2vMCTracks.end());
             MC.ID = trk_it->second;
-
-            //           vMCTracks[MC.ID].Points.push_back(vMCPoints.size());
-
+            vMCTracks[MC.ID].Points.push_back_no_warning(vMCPoints.size());
+            dFEI2vMCPoints.insert(DFEI2I::value_type(dFEI(iFile, iEvent, iMC), vMCPoints.size()));
             vMCPoints.push_back(MC);
             vMCPoints_in_Time_Slice.push_back(0);
-
-            dFEI2vMCPoints.insert(DFEI2I::value_type(dFEI(iFile, iEvent, iMC), vMCPoints.size() - 1));
             nMvdPoints++;
           }
         }
       }
 
-      Int_t nMC = fStsPoints->Size(iFile, iEvent);
-
-      for (Int_t iMC = 0; iMC < nMC; iMC++) {
-        CbmL1MCPoint MC;
-
-        MC.event = iEvent;
-
-        if (!ReadMCPoint(&MC, iMC, iFile, iEvent, 0)) {
-          MC.iStation    = -1;
-          L1Station* sta = algo->vStations + NMvdStations;
-          for (Int_t iSt = 0; iSt < NStsStations; iSt++)
-            MC.iStation = (MC.z > sta[iSt].z[0] - 2.5) ? (NMvdStations + iSt) : MC.iStation;
-
-          Double_t dtrck          = dFEI(iFile, iEvent, MC.ID);
-          DFEI2I::iterator trk_it = dFEI2vMCTracks.find(dtrck);
-          if (trk_it == dFEI2vMCTracks.end()) continue;
-          Int_t IND_Track = trk_it->second;
-          vMCTracks[IND_Track].Points.push_back_no_warning(vMCPoints.size());
-
-          MC.ID = trk_it->second;
-          vMCPoints.push_back(MC);
-          vMCPoints_in_Time_Slice.push_back(0);
-
-          dFEI2vMCPoints.insert(DFEI2I::value_type(dFEI(iFile, iEvent, iMC + nMvdPoints), vMCPoints.size() - 1));
-          nStsPoints++;
+      if (fStsPoints) {
+        Int_t nMC = fStsPoints->Size(iFile, iEvent);
+        for (Int_t iMC = 0; iMC < nMC; iMC++) {
+          CbmL1MCPoint MC;
+          if (!ReadMCPoint(&MC, iMC, iFile, iEvent, 0)) {
+            MC.iStation    = -1;
+            L1Station* sta = algo->vStations + NMvdStations;
+            for (Int_t iSt = 0; iSt < NStsStations; iSt++) {
+              if (MC.z > sta[iSt].z[0] - 2.5) { MC.iStation = NMvdStations + iSt; }
+            }
+            assert(MC.iStation >= 0);
+            Double_t dtrck          = dFEI(iFile, iEvent, MC.ID);
+            DFEI2I::iterator trk_it = dFEI2vMCTracks.find(dtrck);
+            assert(trk_it != dFEI2vMCTracks.end());
+            MC.ID = trk_it->second;
+            vMCTracks[MC.ID].Points.push_back_no_warning(vMCPoints.size());
+            dFEI2vMCPoints.insert(DFEI2I::value_type(dFEI(iFile, iEvent, iMC + nMvdPoints), vMCPoints.size()));
+            vMCPoints.push_back(MC);
+            vMCPoints_in_Time_Slice.push_back(0);
+            nStsPoints++;
+          }
         }
       }
 
       if (fMuchPoints) {
-
-        for (Int_t iMC = 0; iMC < fMuchPoints->Size(iFile, iEvent); iMC++) {
+        Int_t nMC = fMuchPoints->Size(iFile, iEvent);
+        for (Int_t iMC = 0; iMC < nMC; iMC++) {
           CbmL1MCPoint MC;
-
-          MC.event = iEvent;
-
           if (!ReadMCPoint(&MC, iMC, iFile, iEvent, 2)) {
             MC.iStation    = -1;
             L1Station* sta = algo->vStations + NMvdStations + NStsStations;
-            for (Int_t iSt = 0; iSt < NMuchStations; iSt++)
-              MC.iStation = (MC.z > sta[iSt].z[0] - 2.5) ? (NMvdStations + NStsStations + iSt) : MC.iStation;
-
+            for (Int_t iSt = 0; iSt < NMuchStations; iSt++) {
+              if (MC.z > sta[iSt].z[0] - 2.5) { MC.iStation = NMvdStations + NStsStations + iSt; }
+            }
+            assert(MC.iStation >= 0);
             Double_t dtrck          = dFEI(iFile, iEvent, MC.ID);
             DFEI2I::iterator trk_it = dFEI2vMCTracks.find(dtrck);
-            if (trk_it == dFEI2vMCTracks.end()) continue;
-            Int_t IND_Track = trk_it->second;
-
-            vMCTracks[IND_Track].Points.push_back_no_warning(vMCPoints.size());
-
+            assert(trk_it != dFEI2vMCTracks.end());
             MC.ID = trk_it->second;
+            vMCTracks[MC.ID].Points.push_back_no_warning(vMCPoints.size());
+            dFEI2vMCPoints.insert(
+              DFEI2I::value_type(dFEI(iFile, iEvent, iMC + nMvdPoints + nStsPoints), vMCPoints.size()));
             vMCPoints.push_back(MC);
             vMCPoints_in_Time_Slice.push_back(0);
-
-            dFEI2vMCPoints.insert(
-              DFEI2I::value_type(dFEI(iFile, iEvent, iMC + nMvdPoints + nStsPoints), vMCPoints.size() - 1));
             nMuchPoints++;
           }
         }
@@ -255,32 +236,22 @@ void CbmL1::ReadEvent(L1AlgoInputData* fData_, float& TsStart, float& TsLength,
       if (fTrdPoints)
         for (Int_t iMC = 0; iMC < fTrdPoints->Size(iFile, iEvent); iMC++) {
           CbmL1MCPoint MC;
-
-          MC.event = iEvent;
-
-
           if (!ReadMCPoint(&MC, iMC, iFile, iEvent, 3)) {
-
             MC.iStation    = -1;
             L1Station* sta = algo->vStations + NMvdStations + NStsStations + NMuchStations;
-            for (Int_t iSt = 0; iSt < NTrdStations; iSt++)
-              MC.iStation =
-                (MC.z > sta[iSt].z[0] - 4.0) ? (NMvdStations + NStsStations + NMuchStations + iSt) : MC.iStation;
-
-
+            for (Int_t iSt = 0; iSt < NTrdStations; iSt++) {
+              if (MC.z > sta[iSt].z[0] - 4.0) { MC.iStation = NMvdStations + NStsStations + NMuchStations + iSt; }
+            }
+            assert(MC.iStation >= 0);
             Double_t dtrck          = dFEI(iFile, iEvent, MC.ID);
             DFEI2I::iterator trk_it = dFEI2vMCTracks.find(dtrck);
-            if (trk_it == dFEI2vMCTracks.end()) continue;
-            Int_t IND_Track = trk_it->second;
-
-            vMCTracks[IND_Track].Points.push_back_no_warning(vMCPoints.size());
-
+            assert(trk_it != dFEI2vMCTracks.end());
             MC.ID = trk_it->second;
+            vMCTracks[MC.ID].Points.push_back_no_warning(vMCPoints.size());
+            dFEI2vMCPoints.insert(
+              DFEI2I::value_type(dFEI(iFile, iEvent, iMC + nMvdPoints + nStsPoints + nMuchPoints), vMCPoints.size()));
             vMCPoints.push_back(MC);
             vMCPoints_in_Time_Slice.push_back(0);
-
-            dFEI2vMCPoints.insert(DFEI2I::value_type(dFEI(iFile, iEvent, iMC + nMvdPoints + nStsPoints + nMuchPoints),
-                                                     vMCPoints.size() - 1));
             nTrdPoints++;
           }
         }
@@ -288,6 +259,8 @@ void CbmL1::ReadEvent(L1AlgoInputData* fData_, float& TsStart, float& TsLength,
       ToFPointsMatch.clear();
 
       if (fTofPoints) {
+        // TOF data arrays also contain fake beam-counter data. Select the real TOF points here.
+
         for (int j = 0; j < fTofHits->GetEntriesFast(); j++) {
 
           CbmLink* link = 0;
@@ -333,9 +306,6 @@ void CbmL1::ReadEvent(L1AlgoInputData* fData_, float& TsStart, float& TsLength,
 
 
         for (UInt_t iMC = 0; iMC < ToFPointsMatch.size(); iMC++) {
-          CbmL1MCPoint MC;
-
-          MC.event = iEvent;
 
           if (ToFPointsMatch[iMC] == 0) continue;
 
@@ -343,6 +313,8 @@ void CbmL1::ReadEvent(L1AlgoInputData* fData_, float& TsStart, float& TsLength,
 
           if (eventNr != iEvent) continue;
 
+          CbmL1MCPoint MC;
+
           if (!ReadMCPoint(&MC, ToFPointsMatch[iMC]->GetIndex(), ToFPointsMatch[iMC]->GetFile(),
                            ToFPointsMatch[iMC]->GetEntry(), 4)) {
 
@@ -399,12 +371,6 @@ void CbmL1::ReadEvent(L1AlgoInputData* fData_, float& TsStart, float& TsLength,
 
   if (listMvdHits) {
 
-    //     if(listMvdPts)
-    //     {
-    //       isUsedMvdPoint.resize(nMC);
-    //       for(int iMc=0; iMc<nMC; iMc++) isUsedMvdPoint[iMc]=0;
-    //     }
-
     int firstDetStrip = NStrips;
 
     for (int j = 0; j < listMvdHits->GetEntriesFast(); j++) {
@@ -437,17 +403,14 @@ void CbmL1::ReadEvent(L1AlgoInputData* fData_, float& TsStart, float& TsLength,
         th.u_front    = th.x * st.frontInfo.cos_phi[0] + th.y * st.frontInfo.sin_phi[0];
         th.u_back     = th.x * st.backInfo.cos_phi[0] + th.y * st.backInfo.sin_phi[0];
       }
-      th.Det  = 0;
-      th.iMC  = -1;
-      int iMC = -1;
+      th.Det = 0;
+      th.iMC = -1;
       if (fPerformance) {
         if (listMvdHitMatches) {
           CbmMatch* mvdHitMatch = L1_DYNAMIC_CAST<CbmMatch*>(listMvdHitMatches->At(j));
           if (mvdHitMatch->GetNofLinks() > 0)
             if (mvdHitMatch->GetLink(0).GetIndex() < nMvdPoints) {
-              iMC    = mvdHitMatch->GetLink(0).GetIndex();
-              th.iMC = iMC;
-              //    th.track = iMC;
+              th.iMC = mvdHitMatch->GetLink(0).GetIndex();
 #ifdef MVDIDEALHITS
 //TODO
 #endif
@@ -898,6 +861,10 @@ void CbmL1::ReadEvent(L1AlgoInputData* fData_, float& TsStart, float& TsLength,
 
   if (fVerbose >= 10) cout << "ReadEvent: sts hits are gotten." << endl;
 
+  if (fVerbose > 1) {
+    LOG(info) << "L1 ReadEvent: nhits mvd " << nMvdHits << " sts " << nStsHits << " much " << nMuchHits << " trd "
+              << nTrdHits << " tof " << nTofHits << endl;
+  }
 
   // sort hits
   int nHits = nMvdHits + nStsHits + nMuchHits + nTrdHits + nTofHits;
@@ -1307,35 +1274,34 @@ void CbmL1::HitMatch()
         Float_t bestWeight  = 0.f;
         Float_t totalWeight = 0.f;
         for (Int_t iLink = 0; iLink < stsHitMatch.GetNofLinks(); iLink++) {
-          Int_t iFile  = stsHitMatch.GetLink(iLink).GetFile();
-          Int_t iEvent = stsHitMatch.GetLink(iLink).GetEntry();
-          Int_t iIndex = stsHitMatch.GetLink(iLink).GetIndex();
+          const CbmLink& link = stsHitMatch.GetLink(iLink);
+          Int_t iFile         = link.GetFile();
+          Int_t iEvent        = link.GetEntry();
+          Int_t iIndex        = link.GetIndex();
 
           if (!fTimesliceMode) {
             iFile  = vFileEvent.begin()->first;
             iEvent = vFileEvent.begin()->second;
           }
 
-          Double_t dtrck          = dFEI(iFile, iEvent, nMvdPoints + iIndex);
-          DFEI2I::iterator trk_it = dFEI2vMCPoints.find(dtrck);
+          Double_t dpnt           = dFEI(iFile, iEvent, nMvdPoints + iIndex);
+          DFEI2I::iterator pnt_it = dFEI2vMCPoints.find(dpnt);
 
-          if (trk_it == dFEI2vMCPoints.end()) continue;
+          assert(pnt_it != dFEI2vMCPoints.end());
 
-          totalWeight += stsHitMatch.GetLink(iLink).GetWeight();
-          if (stsHitMatch.GetLink(iLink).GetWeight() > bestWeight) {
-            bestWeight = stsHitMatch.GetLink(iLink).GetWeight();
-            iP         = trk_it->second;
+          totalWeight += link.GetWeight();
+          if (link.GetWeight() > bestWeight) {
+            bestWeight = link.GetWeight();
+            iP         = pnt_it->second;
           }
         }
       }  //mach cluster
 
       if (iP >= 0) {
-        hit.event = vMCPoints[iP].event;
         hit.mcPointIds.push_back_no_warning(iP);
         vMCPoints[iP].hitIds.push_back_no_warning(iH);
       }
       else {
-        hit.event   = -1;
         int idPoint = vHitMCRef[iH];
         if (idPoint >= 0) {
           hit.mcPointIds.push_back_no_warning(idPoint);
-- 
GitLab