From 799899a20ab9023e90bbe7d71673f9bee92dd491 Mon Sep 17 00:00:00 2001
From: P-A Loizeau <p.-a.loizeau@gsi.de>
Date: Thu, 13 Jul 2023 17:45:11 +0200
Subject: [PATCH] In MUCH tracking interface class, fix TrkLayer to (station,
 layer) conversion

Old assumption of exactly 3 layers in all stations was valid only for the CBM geometries and may be subject to change.
It worked for the 2019-2020 mCBM geometries only because there MCH was at mot 1 station with a single layer.
---
 .../much/CbmMuchTrackingInterface.cxx         |  5 ++++
 .../detectors/much/CbmMuchTrackingInterface.h | 25 +++++++++++++++++--
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/core/detectors/much/CbmMuchTrackingInterface.cxx b/core/detectors/much/CbmMuchTrackingInterface.cxx
index 33446c0ffe..3f58aadf97 100644
--- a/core/detectors/much/CbmMuchTrackingInterface.cxx
+++ b/core/detectors/much/CbmMuchTrackingInterface.cxx
@@ -80,6 +80,11 @@ InitStatus CbmMuchTrackingInterface::Init()
   // Init conversion matrices
   InitConversionMatrices();
 
+  // Keep Track of how many layers are present in each station (may be heterogenous)
+  for (int iMuchSt = 0; iMuchSt < fGeoScheme->GetNStations(); ++iMuchSt) {
+    fNbLayersPerStation.push_back(fGeoScheme->GetStation(iMuchSt)->GetNLayers());
+  }
+
   // Check the validity of the parameters
   if (!this->Check()) {
     LOG(error)
diff --git a/core/detectors/much/CbmMuchTrackingInterface.h b/core/detectors/much/CbmMuchTrackingInterface.h
index aa9a5440a5..5ba7677dba 100644
--- a/core/detectors/much/CbmMuchTrackingInterface.h
+++ b/core/detectors/much/CbmMuchTrackingInterface.h
@@ -139,16 +139,37 @@ private:
   }
 
   /// Calculates MuCh layer ID from tracker station ID
-  __attribute__((always_inline)) int GetMuchLayerId(int stationId) const { return stationId % 3; }
+  __attribute__((always_inline)) int GetMuchLayerId(int stationId) const
+  {
+    int layersCount  = 0;
+    auto itStatNbLay = fNbLayersPerStation.cbegin();
+    while ((layersCount + *itStatNbLay) <= stationId) {
+      layersCount += *itStatNbLay;
+      itStatNbLay++;
+    }
+    return stationId - layersCount;
+  }
 
   /// Calculates MuCh station ID from tracker station ID
-  __attribute__((always_inline)) int GetMuchStationId(int stationId) const { return stationId / 3; }
+  __attribute__((always_inline)) int GetMuchStationId(int stationId) const
+  {
+    int layersCount   = 0;
+    int stationsCount = 0;
+    // Assume that never station ID as input bigger than total number of stations
+    while ((layersCount + fNbLayersPerStation[stationsCount]) <= stationId) {
+      layersCount += fNbLayersPerStation[stationsCount];
+      stationsCount++;
+    }
+    return stationsCount;
+  }
 
 
   static CbmMuchTrackingInterface* fpInstance;  ///< Instance of the class
 
   CbmMuchGeoScheme* fGeoScheme {nullptr};  ///< MuCh geometry scheme instance
 
+  std::vector<uint16_t> fNbLayersPerStation = {};
+
   ClassDef(CbmMuchTrackingInterface, 0);
 };
 
-- 
GitLab