From c97f184ba6f8b06a6c174bc65dbbb39f4ed63186 Mon Sep 17 00:00:00 2001
From: "se.gorbunov" <se.gorbunov@gsi.de>
Date: Thu, 30 Nov 2023 19:29:08 +0000
Subject: [PATCH] Ca: add tolerance to the Time miscalibration

---
 algo/ca/TrackingChain.cxx                    |  6 +--
 algo/ca/core/pars/CaInitManager.cxx          |  1 +
 algo/ca/core/pars/CaInitManager.h            |  8 ++++
 algo/ca/core/pars/CaParameters.h             | 27 ++++++++++++--
 algo/ca/core/tracking/CaFramework.h          |  2 +-
 macro/beamtime/mcbm2022/mcbm_event_reco_L1.C |  6 +--
 macro/beamtime/mcbm2022/mcbm_reco.C          |  6 +--
 reco/L1/CbmL1.cxx                            |  3 +-
 reco/L1/CbmL1.h                              | 14 +++----
 reco/L1/CbmL1ReadEvent.cxx                   | 39 +++++++++++---------
 10 files changed, 73 insertions(+), 39 deletions(-)

diff --git a/algo/ca/TrackingChain.cxx b/algo/ca/TrackingChain.cxx
index a5b67748ce..b2110b58b8 100644
--- a/algo/ca/TrackingChain.cxx
+++ b/algo/ca/TrackingChain.cxx
@@ -235,10 +235,10 @@ void TrackingChain::ReadHits(PartitionedSpan<const ca::HitTypes_t::at<DetID>> hi
       caHit.SetY(hit.Y());
       caHit.SetZ(hit.Z());
       caHit.SetT(hit.Time());
-      caHit.SetDx2(hit.Dx() * hit.Dx());
-      caHit.SetDy2(hit.Dy() * hit.Dy());
+      caHit.SetDx2(hit.Dx() * hit.Dx() + fCaFramework.GetParameters().GetMisalignmentXsq(DetID));
+      caHit.SetDy2(hit.Dy() * hit.Dy() + fCaFramework.GetParameters().GetMisalignmentYsq(DetID));
       if constexpr (IsSts) caHit.SetDxy(hit.fDxy);
-      caHit.SetDt2(hit.TimeError() * hit.TimeError());
+      caHit.SetDt2(hit.TimeError() * hit.TimeError() + fCaFramework.GetParameters().GetMisalignmentTsq(DetID));
       /// FIXME: Define ranges from the hit, when will be available
       caHit.SetRangeX(3.5 * hit.Dx());
       caHit.SetRangeY(3.5 * hit.Dy());
diff --git a/algo/ca/core/pars/CaInitManager.cxx b/algo/ca/core/pars/CaInitManager.cxx
index e792814c42..9e573d36cc 100644
--- a/algo/ca/core/pars/CaInitManager.cxx
+++ b/algo/ca/core/pars/CaInitManager.cxx
@@ -87,6 +87,7 @@ void InitManager::ClearSetupInfo()
 
   fParameters.fMisalignmentX.fill(0.);
   fParameters.fMisalignmentY.fill(0.);
+  fParameters.fMisalignmentT.fill(0.);
 
   fParameters.fDevIsIgnoreHitSearchAreas     = false;
   fParameters.fDevIsUseOfOriginalField       = false;
diff --git a/algo/ca/core/pars/CaInitManager.h b/algo/ca/core/pars/CaInitManager.h
index 57391e2b69..8fc4d01785 100644
--- a/algo/ca/core/pars/CaInitManager.h
+++ b/algo/ca/core/pars/CaInitManager.h
@@ -251,6 +251,14 @@ namespace cbm::algo::ca
       fParameters.fMisalignmentY[static_cast<int>(detectorId)] = value;
     }
 
+    /// \brief Sets miscalibration of Time
+    /// \param  detectorId  Index of the detector system
+    /// \param  value  Miscalibration value
+    void SetMisalignmentT(EDetectorID detectorId, float value)
+    {
+      fParameters.fMisalignmentT[static_cast<int>(detectorId)] = value;
+    }
+
     /// \brief  Takes parameters object from the init-manager instance
     /// \return A parameter object
     Parameters&& TakeParameters();
diff --git a/algo/ca/core/pars/CaParameters.h b/algo/ca/core/pars/CaParameters.h
index 2ff1ed7db0..988941163c 100644
--- a/algo/ca/core/pars/CaParameters.h
+++ b/algo/ca/core/pars/CaParameters.h
@@ -197,11 +197,26 @@ namespace cbm::algo::ca
     /// \brief Gets default mass
     float GetDefaultMass() const { return fDefaultMass; }
 
-    /// \brief Gets misalignment of the detector systems in X
-    float GetMisalignmentXsq(int iDet) const { return fMisalignmentX[iDet] * fMisalignmentX[iDet]; }
+    /// \brief Gets misalignment of the detector systems in X, squared rms
+    float GetMisalignmentXsq(EDetectorID detId) const
+    {
+      auto iDet = static_cast<DetectorID_t>(detId);
+      return fMisalignmentX[iDet] * fMisalignmentX[iDet];
+    }
+
+    /// \brief Gets misalignment of the detector systems in Y, squared rms
+    float GetMisalignmentYsq(EDetectorID detId) const
+    {
+      auto iDet = static_cast<DetectorID_t>(detId);
+      return fMisalignmentY[iDet] * fMisalignmentY[iDet];
+    }
 
-    /// \brief Gets misalignment of the detector systems in Y
-    float GetMisalignmentYsq(int iDet) const { return fMisalignmentY[iDet] * fMisalignmentY[iDet]; }
+    /// \brief Gets miscalibration of the detector systems in Time, squared rms
+    float GetMisalignmentTsq(EDetectorID detId) const
+    {
+      auto iDet = static_cast<DetectorID_t>(detId);
+      return fMisalignmentT[iDet] * fMisalignmentT[iDet];
+    }
 
     /// \brief Class invariant checker
     void CheckConsistency() const;
@@ -301,6 +316,9 @@ namespace cbm::algo::ca
     /// misalignment of the detector systems in Y
     std::array<float, constants::size::MaxNdetectors> fMisalignmentY{0.};
 
+    /// miscalibration of the detector systems in Time
+    std::array<float, constants::size::MaxNdetectors> fMisalignmentT{0.};
+
     // ***************************
     // ** Flags for development **
     // ***************************
@@ -347,6 +365,7 @@ namespace cbm::algo::ca
 
       ar& fMisalignmentX;
       ar& fMisalignmentY;
+      ar& fMisalignmentT;
 
       ar& fDevIsIgnoreHitSearchAreas;
       ar& fDevIsUseOfOriginalField;
diff --git a/algo/ca/core/tracking/CaFramework.h b/algo/ca/core/tracking/CaFramework.h
index 449a17cc78..fe73fa7784 100644
--- a/algo/ca/core/tracking/CaFramework.h
+++ b/algo/ca/core/tracking/CaFramework.h
@@ -187,7 +187,7 @@ namespace cbm::algo::ca
 
    public:
     /// Gets number of stations before the pipe (MVD stations in CBM)
-    int GetNstationsBeforePipe() const { return fNstationsBeforePipe; }
+    // int GetNstationsBeforePipe() const { return fNstationsBeforePipe; }
 
     /// Gets number of stations situated in field region (MVD + STS in CBM)
     int GetNfieldStations() const { return fNfieldStations; }
diff --git a/macro/beamtime/mcbm2022/mcbm_event_reco_L1.C b/macro/beamtime/mcbm2022/mcbm_event_reco_L1.C
index b8cc93980d..e58c3d951c 100644
--- a/macro/beamtime/mcbm2022/mcbm_event_reco_L1.C
+++ b/macro/beamtime/mcbm2022/mcbm_event_reco_L1.C
@@ -606,9 +606,9 @@ Bool_t mcbm_event_reco_L1(UInt_t uRunId                   = 2570,
     l1->SetVerbose(3);
     run->AddTask(l1);
 
-    l1->SetMisalignmentSts(.2, .2);
-    l1->SetMisalignmentTrd(.2, .2);
-    l1->SetMisalignmentTof(.2, .2);
+    l1->SetMisalignmentSts(.2, .2, 10.);
+    l1->SetMisalignmentTrd(.2, .2, 10.);
+    l1->SetMisalignmentTof(.2, .2, 10.);
 
     CbmL1GlobalTrackFinder* globalTrackFinder = new CbmL1GlobalTrackFinder();
     FairTask* globalFindTracks                = new CbmL1GlobalFindTracksEvents(globalTrackFinder);
diff --git a/macro/beamtime/mcbm2022/mcbm_reco.C b/macro/beamtime/mcbm2022/mcbm_reco.C
index df1c01df36..37f254fc67 100644
--- a/macro/beamtime/mcbm2022/mcbm_reco.C
+++ b/macro/beamtime/mcbm2022/mcbm_reco.C
@@ -527,9 +527,9 @@ Bool_t mcbm_reco(UInt_t uRunId                   = 2391,
 
     CbmL1* l1 = new CbmL1("L1", 0);  // <= Disable verbose mode
     l1->SetMcbmMode();
-    l1->SetMisalignmentSts(.2, .2);
-    l1->SetMisalignmentTrd(.2, .2);
-    l1->SetMisalignmentTof(.2, .2);
+    l1->SetMisalignmentSts(.2, .2, 10.);
+    l1->SetMisalignmentTrd(.2, .2, 10.);
+    l1->SetMisalignmentTof(.2, .2, 10.);
 
     run->AddTask(l1);
 
diff --git a/reco/L1/CbmL1.cxx b/reco/L1/CbmL1.cxx
index f276e6a1f3..66deb50637 100644
--- a/reco/L1/CbmL1.cxx
+++ b/reco/L1/CbmL1.cxx
@@ -305,8 +305,9 @@ InitStatus CbmL1::Init()
     auto detId = cbm::algo::ca::EDetectorID(i);
     fInitManager.SetMisalignmentX(detId, fvMisalignment[detId][0]);
     fInitManager.SetMisalignmentY(detId, fvMisalignment[detId][1]);
+    fInitManager.SetMisalignmentT(detId, fvMisalignment[detId][2]);
     LOG(info) << "CbmL1: misalignment for " << cbm::ca::kDetName[detId] << " is set to " << fvMisalignment[detId][0]
-              << " " << fvMisalignment[detId][1];
+              << " " << fvMisalignment[detId][1] << " " << fvMisalignment[detId][2];
   }
 
   if (fPerformance) {
diff --git a/reco/L1/CbmL1.h b/reco/L1/CbmL1.h
index e4f8d1b412..54bba0ff0c 100644
--- a/reco/L1/CbmL1.h
+++ b/reco/L1/CbmL1.h
@@ -256,13 +256,13 @@ class CbmL1 : public FairTask {
   void SetGlobalMode() { fTrackingMode = ca::Framework::TrackingMode::kGlobal; }
 
   /// Sets misalignment of the detector
-  void SetMisalignment(ca::EDetectorID det, float dx, float dy) { fvMisalignment[det] = {dx, dy}; }
+  void SetMisalignment(ca::EDetectorID det, float dx, float dy, float dt) { fvMisalignment[det] = {dx, dy, dt}; }
 
-  void SetMisalignmentMvd(float dx, float dy) { SetMisalignment(ca::EDetectorID::kMvd, dx, dy); }
-  void SetMisalignmentSts(float dx, float dy) { SetMisalignment(ca::EDetectorID::kSts, dx, dy); }
-  void SetMisalignmentMuch(float dx, float dy) { SetMisalignment(ca::EDetectorID::kMuch, dx, dy); }
-  void SetMisalignmentTrd(float dx, float dy) { SetMisalignment(ca::EDetectorID::kTrd, dx, dy); }
-  void SetMisalignmentTof(float dx, float dy) { SetMisalignment(ca::EDetectorID::kTof, dx, dy); }
+  void SetMisalignmentMvd(float dx, float dy, float dt) { SetMisalignment(ca::EDetectorID::kMvd, dx, dy, dt); }
+  void SetMisalignmentSts(float dx, float dy, float dt) { SetMisalignment(ca::EDetectorID::kSts, dx, dy, dt); }
+  void SetMisalignmentMuch(float dx, float dy, float dt) { SetMisalignment(ca::EDetectorID::kMuch, dx, dy, dt); }
+  void SetMisalignmentTrd(float dx, float dy, float dt) { SetMisalignment(ca::EDetectorID::kTrd, dx, dy, dt); }
+  void SetMisalignmentTof(float dx, float dy, float dt) { SetMisalignment(ca::EDetectorID::kTof, dx, dy, dt); }
 
   ca::TrackingMonitor fMonitor{};  ///< Tracking monitor
 
@@ -474,7 +474,7 @@ class CbmL1 : public FairTask {
 
   //std::unique_ptr<CbmCaMCModule> fpMCModule = nullptr;  ///< MC-module for tracking
 
-  cbm::ca::DetIdArr_t<std::array<float, 2>> fvMisalignment{{0.}};  ///< Misalignment
+  cbm::ca::DetIdArr_t<std::array<float, 3>> fvMisalignment{{0.}};  ///< Misalignment
 
  public:
   // ** Basic data members **
diff --git a/reco/L1/CbmL1ReadEvent.cxx b/reco/L1/CbmL1ReadEvent.cxx
index a549424edf..930ff3cb3a 100644
--- a/reco/L1/CbmL1ReadEvent.cxx
+++ b/reco/L1/CbmL1ReadEvent.cxx
@@ -443,9 +443,8 @@ void CbmL1::ReadEvent(CbmEvent* event)
         h->Position(pos);
         h->PositionError(err);
 
-        th.dx2 = h->GetDx() * h->GetDx() + fpAlgo->GetParameters().GetMisalignmentXsq((int) ca::EDetectorID::kMvd);
-        th.dy2 = h->GetDy() * h->GetDy() + fpAlgo->GetParameters().GetMisalignmentYsq((int) ca::EDetectorID::kMvd);
-        ;
+        th.dx2 = h->GetDx() * h->GetDx() + fpAlgo->GetParameters().GetMisalignmentXsq(ca::EDetectorID::kMvd);
+        th.dy2 = h->GetDy() * h->GetDy() + fpAlgo->GetParameters().GetMisalignmentYsq(ca::EDetectorID::kMvd);
         th.dxy = h->GetDxy();
 
         th.x = pos.X();
@@ -453,8 +452,10 @@ void CbmL1::ReadEvent(CbmEvent* event)
         th.z = pos.Z();
 
         // Get time
-        th.time = h->GetTime();                           // currently ignored by the tracking
-        th.dt2  = h->GetTimeError() * h->GetTimeError();  // currently ignored by the tracking
+        th.time = h->GetTime();  // currently ignored by the tracking
+        th.dt2 =
+          h->GetTimeError() * h->GetTimeError()
+          + fpAlgo->GetParameters().GetMisalignmentTsq(ca::EDetectorID::kMvd);  // currently ignored by the tracking
 
         std::tie(th.rangeX, th.rangeY, th.rangeT) = CbmMvdTrackingInterface::Instance()->GetHitRanges(*h);
       }
@@ -516,7 +517,8 @@ void CbmL1::ReadEvent(CbmEvent* event)
 
         //Get time
         th.time = h->GetTime();
-        th.dt2  = h->GetTimeError() * h->GetTimeError();
+        th.dt2 =
+          h->GetTimeError() * h->GetTimeError() + fpAlgo->GetParameters().GetMisalignmentTsq(ca::EDetectorID::kSts);
 
         TVector3 pos, err;
         h->Position(pos);
@@ -526,8 +528,8 @@ void CbmL1::ReadEvent(CbmEvent* event)
         th.y = pos.Y();
         th.z = pos.Z();
 
-        th.dx2 = h->GetDx() * h->GetDx() + fpAlgo->GetParameters().GetMisalignmentXsq((int) ca::EDetectorID::kSts);
-        th.dy2 = h->GetDy() * h->GetDy() + fpAlgo->GetParameters().GetMisalignmentYsq((int) ca::EDetectorID::kSts);
+        th.dx2 = h->GetDx() * h->GetDx() + fpAlgo->GetParameters().GetMisalignmentXsq(ca::EDetectorID::kSts);
+        th.dy2 = h->GetDy() * h->GetDy() + fpAlgo->GetParameters().GetMisalignmentYsq(ca::EDetectorID::kSts);
         th.dxy = h->GetDxy();
 
         std::tie(th.rangeX, th.rangeY, th.rangeT) = CbmStsTrackingInterface::Instance()->GetHitRanges(*h);
@@ -574,7 +576,8 @@ void CbmL1::ReadEvent(CbmEvent* event)
 
         //Get time
         th.time = h->GetTime();
-        th.dt2  = h->GetTimeError() * h->GetTimeError();
+        th.dt2 =
+          h->GetTimeError() * h->GetTimeError() + fpAlgo->GetParameters().GetMisalignmentTsq(ca::EDetectorID::kMuch);
 
         //   th.iSector  = 0;
         th.iStripF = firstDetStrip + hitIndex;
@@ -587,8 +590,8 @@ void CbmL1::ReadEvent(CbmEvent* event)
         th.y = h->GetY();
         th.z = h->GetZ();
 
-        th.dx2 = h->GetDx() * h->GetDx() + fpAlgo->GetParameters().GetMisalignmentXsq((int) ca::EDetectorID::kMuch);
-        th.dy2 = h->GetDy() * h->GetDy() + fpAlgo->GetParameters().GetMisalignmentYsq((int) ca::EDetectorID::kMuch);
+        th.dx2 = h->GetDx() * h->GetDx() + fpAlgo->GetParameters().GetMisalignmentXsq(ca::EDetectorID::kMuch);
+        th.dy2 = h->GetDy() * h->GetDy() + fpAlgo->GetParameters().GetMisalignmentYsq(ca::EDetectorID::kMuch);
         th.dxy = 0;  /// FIXME: ???
 
         std::tie(th.rangeX, th.rangeY, th.rangeT) = CbmMuchTrackingInterface::Instance()->GetHitRanges(*h);
@@ -646,7 +649,8 @@ void CbmL1::ReadEvent(CbmEvent* event)
       //  if (h->GetPlaneId()==3) continue;
 
       th.time = h->GetTime();
-      th.dt2  = h->GetTimeError() * h->GetTimeError();
+      th.dt2 =
+        h->GetTimeError() * h->GetTimeError() + fpAlgo->GetParameters().GetMisalignmentTsq(ca::EDetectorID::kTrd);
 
       //   th.iSector  = 0;
       th.iStripF = NStrips;
@@ -661,8 +665,8 @@ void CbmL1::ReadEvent(CbmEvent* event)
       th.y = pos.Y();
       th.z = pos.Z();
 
-      th.dx2 = h->GetDx() * h->GetDx() + fpAlgo->GetParameters().GetMisalignmentXsq((int) ca::EDetectorID::kTrd);
-      th.dy2 = h->GetDy() * h->GetDy() + fpAlgo->GetParameters().GetMisalignmentYsq((int) ca::EDetectorID::kTrd);
+      th.dx2 = h->GetDx() * h->GetDx() + fpAlgo->GetParameters().GetMisalignmentXsq(ca::EDetectorID::kTrd);
+      th.dy2 = h->GetDy() * h->GetDy() + fpAlgo->GetParameters().GetMisalignmentYsq(ca::EDetectorID::kTrd);
       th.dxy = 0;
 
       std::tie(th.rangeX, th.rangeY, th.rangeT) = CbmTrdTrackingInterface::Instance()->GetHitRanges(*h);
@@ -713,10 +717,11 @@ void CbmL1::ReadEvent(CbmEvent* event)
       if (th.iStation < 0) continue;
 
       th.time = h->GetTime();
-      th.dt2  = h->GetTimeError() * h->GetTimeError();
+      th.dt2 =
+        h->GetTimeError() * h->GetTimeError() + fpAlgo->GetParameters().GetMisalignmentTsq(ca::EDetectorID::kTof);
 
-      th.dx2 = h->GetDx() * h->GetDx() + fpAlgo->GetParameters().GetMisalignmentXsq((int) ca::EDetectorID::kTof);
-      th.dy2 = h->GetDy() * h->GetDy() + fpAlgo->GetParameters().GetMisalignmentYsq((int) ca::EDetectorID::kTof);
+      th.dx2 = h->GetDx() * h->GetDx() + fpAlgo->GetParameters().GetMisalignmentXsq(ca::EDetectorID::kTof);
+      th.dy2 = h->GetDy() * h->GetDy() + fpAlgo->GetParameters().GetMisalignmentYsq(ca::EDetectorID::kTof);
       th.dxy = h->GetDxy();
 
       //   th.iSector  = 0;
-- 
GitLab