From cf040fac83c082f673f919639c8031e7b14486da Mon Sep 17 00:00:00 2001
From: Patrick Scholer <patrick.scholer@cern.ch>
Date: Tue, 1 Jun 2021 22:44:23 +0200
Subject: [PATCH 01/11] adding the local position to the calibrated strip
 struct

---
 .../NSWCalibTools/INSWCalibTool.h             |  4 +-
 .../NSWCalibTools/src/NSWCalibTool.cxx        | 51 ++++++++++++++++++-
 .../NSWCalib/NSWCalibTools/src/NSWCalibTool.h |  2 +
 3 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/NSWCalibTools/INSWCalibTool.h b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/NSWCalibTools/INSWCalibTool.h
index 0b7e78c0cafc..49dc27e6bc84 100644
--- a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/NSWCalibTools/INSWCalibTool.h
+++ b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/NSWCalibTools/INSWCalibTool.h
@@ -10,6 +10,7 @@
 
 #include <cmath>
 #include <vector>
+#include "float.h"
 
 #include "TF1.h"
 
@@ -26,6 +27,7 @@ namespace NSWCalib {
     double resTransDistDrift = 0;
     double resLongDistDrift = 0;
     double dx = 0;
+    Amg::Vector2D locPos = Amg::Vector2D(-FLT_MAX,-FLT_MAX);
     Identifier identifier;
   };
 
@@ -47,7 +49,7 @@ namespace Muon {
     
     virtual StatusCode calibrateClus(const Muon::MMPrepData* prepRawData, const Amg::Vector3D& globalPos, std::vector<NSWCalib::CalibratedStrip>& calibClus) const = 0;
     virtual StatusCode calibrateStrip(const Muon::MM_RawData* mmRawData, NSWCalib::CalibratedStrip& calibStrip) const = 0;
-    virtual StatusCode calibrateStrip(const double time,  const double charge, const double lorentzAngle, NSWCalib::CalibratedStrip&calibStrip) const = 0;
+    virtual StatusCode calibrateStrip(const Identifier id, const double time,  const double charge, const double lorentzAngle, NSWCalib::CalibratedStrip&calibStrip) const = 0;
     virtual StatusCode calibrateStrip(const Muon::STGC_RawData* sTGCRawData, NSWCalib::CalibratedStrip& calibStrip) const = 0;
     virtual StatusCode mmGasProperties(float &vDrift, float &longDiff, float &transDiff, float &interactionDensityMean, float &interactionDensitySigma, TF1* &lorentzAngleFunction) const = 0;
     virtual float peakTime() const = 0;
diff --git a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx
index 75b401902c71..5bfc299ee281 100644
--- a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx
+++ b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx
@@ -130,10 +130,11 @@ StatusCode Muon::NSWCalibTool::calibrateClus(const Muon::MMPrepData* prepData, c
 
   /// loop over prepData strips
   for (unsigned int i = 0; i < prepData->stripNumbers().size(); i++){
+    Identifier id = prepData->rdoList().at(i);
     double time = prepData->stripTimes().at(i);
     double charge = prepData->stripCharges().at(i);
     NSWCalib::CalibratedStrip calibStrip;
-    ATH_CHECK(calibrateStrip(time, charge, lorentzAngle, calibStrip));
+    ATH_CHECK(calibrateStrip(id,time, charge, lorentzAngle, calibStrip));
     calibClus.push_back(calibStrip);
   }
   return StatusCode::SUCCESS;
@@ -141,7 +142,16 @@ StatusCode Muon::NSWCalibTool::calibrateClus(const Muon::MMPrepData* prepData, c
 
 
 
-StatusCode Muon::NSWCalibTool::calibrateStrip(const double time, const double charge, const double lorentzAngle, NSWCalib::CalibratedStrip& calibStrip) const {
+StatusCode Muon::NSWCalibTool::calibrateStrip(const Identifier id, const double time, const double charge, const double lorentzAngle, NSWCalib::CalibratedStrip& calibStrip) const {
+  //get local positon
+  Amg::Vector2D locPos;
+  if(!localStripPosition(id,locPos)) {
+    ATH_MSG_WARNING("Failed to retrieve local strip position");
+    return StatusCode::FAILURE;
+  }
+  
+  calibStrip.identifier = id;
+
   calibStrip.charge = charge;
   calibStrip.time = time;
 
@@ -153,6 +163,7 @@ StatusCode Muon::NSWCalibTool::calibrateStrip(const double time, const double ch
   calibStrip.resLongDistDrift = std::pow(m_ionUncertainty * vDriftCorrected, 2)
     + std::pow(m_longDiff * calibStrip.distDrift, 2);
   calibStrip.dx = std::sin(lorentzAngle) * calibStrip.time * m_vDrift;
+  calibStrip.locPos = Amg::Vector2D(locPos.x() + calibStrip.dx, locPos.y()); 
   return StatusCode::SUCCESS;
 }
 
@@ -170,6 +181,16 @@ StatusCode Muon::NSWCalibTool::calibrateStrip(const Muon::MM_RawData* mmRawData,
   const MuonGM::MMReadoutElement* detEl = muDetMgr->getMMReadoutElement(rdoId);
   detEl->stripGlobalPosition(rdoId,globalPos);
 
+  //get local postion
+  Amg::Vector2D locPos;
+  if(!localStripPosition(rdoId,locPos)) {
+    ATH_MSG_WARNING("Failed to retrieve local strip position");
+    return StatusCode::FAILURE;
+  }
+
+  //get stripWidth
+  detEl->getDesign(rdoId)->channelWidth(locPos); // positon is not used for strip width 
+
   calibStrip.charge = mmRawData->charge();
   calibStrip.time = mmRawData->time() - globalPos.norm() * reciprocalSpeedOfLight - m_peakTime;
   calibStrip.identifier = mmRawData->identify();
@@ -179,6 +200,8 @@ StatusCode Muon::NSWCalibTool::calibrateStrip(const Muon::MM_RawData* mmRawData,
   calibStrip.resLongDistDrift = std::pow(m_ionUncertainty * m_vDrift, 2)
     + std::pow(m_longDiff * calibStrip.distDrift, 2);
 
+  calibStrip.locPos = locPos;
+
   return StatusCode::SUCCESS;
 }
 
@@ -194,10 +217,18 @@ StatusCode Muon::NSWCalibTool::calibrateStrip(const Muon::STGC_RawData* sTGCRawD
   Amg::Vector3D globalPos;
   const MuonGM::sTgcReadoutElement* detEl = muDetMgr->getsTgcReadoutElement(rdoId);
   detEl->stripGlobalPosition(rdoId,globalPos);
+  
+  //get local postion
+  Amg::Vector2D locPos;
+  if(!localStripPosition(rdoId,locPos)) {
+    ATH_MSG_WARNING("Failed to retrieve local strip position");
+    return StatusCode::FAILURE;
+  }
 
   calibStrip.charge =sTGCRawData->charge();
   calibStrip.time = sTGCRawData->time() - globalPos.norm() * reciprocalSpeedOfLight - m_peakTime;
   calibStrip.identifier = sTGCRawData->identify();
+  calibStrip.locPos = locPos;
 
   return StatusCode::SUCCESS;
 }
@@ -247,3 +278,19 @@ StatusCode Muon::NSWCalibTool::mmGasProperties(float &vDrift, float &longDiff, f
   lorentzAngleFunction = m_lorentzAngleFunction;
   return StatusCode::SUCCESS;
 }
+
+
+bool Muon::NSWCalibTool::localStripPosition(const Identifier& id, Amg::Vector2D &locPos) const {
+  if(m_idHelperSvc->isMM(id)){
+    const MuonGM::MMReadoutElement* detEl = m_muonMgr->getMMReadoutElement(id);
+    return detEl->stripPosition(id,locPos);
+
+  } else if(m_idHelperSvc->issTgc(id)){
+    const MuonGM::sTgcReadoutElement* detEl = m_muonMgr->getsTgcReadoutElement(id);
+    return detEl->stripPosition(id,locPos);
+
+  } else {
+    return false;
+  }
+  
+}
diff --git a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h
index 60d44d2f5dad..1a895f52b526 100644
--- a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h
+++ b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h
@@ -66,6 +66,8 @@ namespace Muon {
     float m_ionUncertainty;
     double m_peakTime;
 
+    bool localStripPosition(const Identifier& id, Amg::Vector2D &locPos) const;
+
     std::string m_gasMixture;
   };
 
-- 
GitLab


From 7bc1c0b98956d0cf231c159f1abd4bd9b13381d4 Mon Sep 17 00:00:00 2001
From: Patrick Scholer <patrick.scholer@cern.ch>
Date: Tue, 1 Jun 2021 22:45:09 +0200
Subject: [PATCH 02/11] implement getCalibratedClusterPosition for MM uTPC

---
 .../src/UTPCMMClusterBuilderTool.cxx          | 106 ++++++++++++++----
 .../src/UTPCMMClusterBuilderTool.h            |   9 +-
 2 files changed, 91 insertions(+), 24 deletions(-)

diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx
index 8d66a7bfd980..eeb9e76a5ec1 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx
@@ -129,21 +129,13 @@ StatusCode Muon::UTPCMMClusterBuilderTool::getClusters(std::vector<Muon::MMPrepD
             while(!applyCrossTalkCut(idx_goodStrips,MMprdsOfLayer,flag,nStripsCutByCrosstalk).isFailure()){}
             ATH_MSG_DEBUG("After cutting CT idx_goddStrips " << idx_goodStrips<< " flag " << flag );
                 
-            double localClusterPosition=-9999;
-            double sigmaLocalClusterPosition=0;
-            double finalFitAngle,finalFitChiSqProb;
-            sc=finalFit(MMprdsOfLayer,idx_goodStrips,localClusterPosition, sigmaLocalClusterPosition,finalFitAngle,finalFitChiSqProb);
-            ATH_MSG_DEBUG("final fit done"); 
-            
-            if(sc.isFailure()) break;
-            ATH_MSG_DEBUG("Did final fit successfull");
             std::vector<Identifier> stripsOfCluster;
             std::vector<uint16_t> stripsOfClusterChannels;
             std::vector<short int> stripsOfClusterTimes;
             std::vector<int> stripsOfClusterCharges;
             std::vector<float> stripsOfClusterDriftDists;
             std::vector<Amg::MatrixX> stripsOfClusterDriftDistErrors;
-
+            std::vector<float> stripsOfClusterLocalPos; // needed for the final fit function
             stripsOfCluster.reserve(idx_goodStrips.size());
             if (m_writeStripProperties) {
                 stripsOfClusterChannels.reserve(idx_goodStrips.size());
@@ -152,6 +144,7 @@ StatusCode Muon::UTPCMMClusterBuilderTool::getClusters(std::vector<Muon::MMPrepD
             }
             stripsOfClusterDriftDists.reserve(idx_goodStrips.size());
             stripsOfClusterDriftDistErrors.reserve(idx_goodStrips.size());
+            stripsOfClusterLocalPos.reserve(idx_goodStrips.size());
 
             ATH_MSG_DEBUG("Found good Strips: "<< idx_goodStrips.size());
 
@@ -168,16 +161,41 @@ StatusCode Muon::UTPCMMClusterBuilderTool::getClusters(std::vector<Muon::MMPrepD
                 }
                 stripsOfClusterDriftDists.push_back(MMprdsOfLayer.at(idx).driftDist());
                 stripsOfClusterDriftDistErrors.push_back(MMprdsOfLayer.at(idx).localCovariance());
+                stripsOfClusterLocalPos.push_back(MMprdsOfLayer.at(idx).localPosition().x());
             }
+<<<<<<< HEAD
             auto covN = Amg::MatrixX(1,1);
             covN.coeffRef(0,0)=sigmaLocalClusterPosition;
+=======
+            
+            double localClusterPosition=-9999;
+            double sigmaLocalClusterPosition=0;
+            double finalFitAngle,finalFitChiSqProb;
+
+            //sc=finalFit(MMprdsOfLayer,idx_goodStrips,localClusterPosition, sigmaLocalClusterPosition,finalFitAngle,finalFitChiSqProb);
+            sc=finalFit(stripsOfCluster, stripsOfClusterLocalPos, stripsOfClusterDriftDists, stripsOfClusterDriftDistErrors, localClusterPosition, sigmaLocalClusterPosition,finalFitAngle,finalFitChiSqProb);
+            
+            ATH_MSG_DEBUG("final fit done");
+            
+            if(sc.isFailure()) break;
+            ATH_MSG_DEBUG("Did final fit successfull");
+        
+
+
+            Amg::MatrixX* covN = new Amg::MatrixX(1,1);
+            covN->coeffRef(0,0)=sigmaLocalClusterPosition;
+>>>>>>> 5aa2e54ad6f... implement getCalibratedClusterPosition for MM uTPC
             ATH_MSG_DEBUG("Did set covN Matrix");
             int idx = idx_goodStrips[0];
             ATH_MSG_DEBUG("idx_goodStrips[0]: "<<idx << " size: " <<MMprdsOfLayer.size());
             Amg::Vector2D localClusterPositionV(localClusterPosition,MMprdsOfLayer.at(idx).localPosition().y()); // y position is the same for all strips
             ATH_MSG_DEBUG("Did set local position");
 
+<<<<<<< HEAD
             float driftDist = 0.0;
+=======
+	          float driftDist = 0.0;
+>>>>>>> 5aa2e54ad6f... implement getCalibratedClusterPosition for MM uTPC
 
             std::unique_ptr<Muon::MMPrepData> prdN = std::make_unique<MMPrepData>(MMprdsOfLayer.at(idx).identify(),
 					    MMprdsOfLayer.at(idx).collectionHash(),
@@ -382,27 +400,37 @@ StatusCode Muon::UTPCMMClusterBuilderTool::applyCrossTalkCut(std::vector<int> &i
 }
 
 
+<<<<<<< HEAD
 StatusCode Muon::UTPCMMClusterBuilderTool::finalFit(const std::vector<Muon::MMPrepData> &mmPrd, std::vector<int>& idxSelected,double& x0, double &sigmaX0, double &fitAngle, double &chiSqProb)const{
     std::unique_ptr<TGraphErrors> fitGraph=std::make_unique<TGraphErrors>();
     std::unique_ptr<TF1> ffit=std::make_unique<TF1>("ffit","pol1");
+=======
+StatusCode Muon::UTPCMMClusterBuilderTool::finalFit(const std::vector<Identifier>& ids, const std::vector<float>& stripsPos, const std::vector<float>& driftDists, const std::vector<Amg::MatrixX> driftDistErrors, double& x0, double &sigmaX0, double &fitAngle, double &chiSqProb)const{
+    std::unique_ptr<TGraphErrors> fitGraph=std::unique_ptr<TGraphErrors>(new TGraphErrors());
+    std::unique_ptr<TF1> ffit=std::unique_ptr<TF1>(new TF1("ffit","pol1"));
+>>>>>>> 5aa2e54ad6f... implement getCalibratedClusterPosition for MM uTPC
     
     double xmin = 5000;
     double xmax = -5000;
-    double xmean = std::accumulate(mmPrd.begin(),mmPrd.end(),0.0,[&](double a,const Muon::MMPrepData &b){return a+b.localPosition().x();})/mmPrd.size(); //get mean x value
+    double xmean = std::accumulate(stripsPos.begin(),stripsPos.end(),0.0)/stripsPos.size(); //get mean x value
     
     std::unique_ptr<TLinearFitter> lf=std::make_unique<TLinearFitter>(1,"1++x");
     
-    for(int idx:idxSelected){
-        double xpos=mmPrd.at(idx).localPosition().x()-xmean; // substract mean value from x to center fit around 0
+    for(size_t idx = 0; idx < stripsPos.size(); idx++){
+        double xpos=stripsPos.at(idx)-xmean; // substract mean value from x to center fit around 0
         if(xmin>xpos) xmin = xpos;
         else if(xmax<xpos) xmax = xpos;
-        lf->AddPoint(&xpos, mmPrd.at(idx).driftDist());
-        fitGraph->SetPoint(fitGraph->GetN(),xpos,mmPrd.at(idx).driftDist());
-        fitGraph->SetPointError(fitGraph->GetN()-1, std::sqrt(mmPrd.at(idx).localCovariance()(0,0)),std::sqrt(mmPrd.at(idx).localCovariance()(1,1)));
+        lf->AddPoint(&xpos, driftDists.at(idx));
+        fitGraph->SetPoint(fitGraph->GetN(),xpos, driftDists.at(idx));
+        fitGraph->SetPointError(fitGraph->GetN()-1, std::sqrt(driftDistErrors.at(idx)(0,0)),std::sqrt(driftDistErrors.at(idx)(1,1)));
     }
     lf->Eval(); 
 
+<<<<<<< HEAD
     if(m_idHelperSvc->mmIdHelper().gasGap(mmPrd.at(0).identify())%2==1 || !m_digiHasNegativeAngles){
+=======
+    if(m_mmIdHelper->gasGap(ids.at(0))%2==1 || !m_digiHasNegativeAngles){
+>>>>>>> 5aa2e54ad6f... implement getCalibratedClusterPosition for MM uTPC
         ffit->SetParLimits(1,-11.5,-0.15); //5 to 81 degree
     }else{
         ffit->SetParLimits(1,0.15,11.5); //5 to 81 degree
@@ -438,17 +466,51 @@ StatusCode Muon::UTPCMMClusterBuilderTool::finalFit(const std::vector<Muon::MMPr
         ATH_MSG_DEBUG("Fit angle: "<<fitAngle <<" "<<fitAngle*180./M_PI);
 
         ATH_MSG_DEBUG("ChisSqProb"<< chiSqProb);
-
-        ATH_MSG_DEBUG("nStrips:"<<idxSelected.size());
-
-        ATH_MSG_DEBUG("gas gap:"<<m_idHelperSvc->mmIdHelper().gasGap(mmPrd.at(0).identify()));
-
+        ATH_MSG_DEBUG("nStrips:"<<stripsPos.size());
+        ATH_MSG_DEBUG("gas gap:"<<m_idHelperSvc->mmIdHelper().gasGap(ids.at(0)));
     }
     if(s!=0 && s!=4000){ //4000 means fit succesfull but error optimization by minos failed; fit is still usable.
         ATH_MSG_DEBUG("Final fit failed with error code "<<s);
         return StatusCode::FAILURE;
     }
-    if((m_idHelperSvc->mmIdHelper().gasGap(mmPrd.at(0).identify())%2==1 || !m_digiHasNegativeAngles) && (ffit->GetParameter(1)<=-11.49 || ffit->GetParameter(1)>=-0.151)) return StatusCode::FAILURE; // fit should have negativ slope for odd gas gaps
-    if(m_idHelperSvc->mmIdHelper().gasGap(mmPrd.at(0).identify())%2==0  && m_digiHasNegativeAngles && (ffit->GetParameter(1)>=11.49 || ffit->GetParameter(1)<=0.151)) return StatusCode::FAILURE; // fit should have positiv slope for even gas gaps
+    if((m_idHelperSvc->mmIdHelper().gasGap(ids.at(0))%2==1 || !m_digiHasNegativeAngles) && (ffit->GetParameter(1)<=-11.49 || ffit->GetParameter(1)>=-0.151)) return StatusCode::FAILURE; // fit should have negativ slope for odd gas gaps
+    if(m_idHelperSvc->mmIdHelper().gasGap(ids.at(0))%2==0  && m_digiHasNegativeAngles && (ffit->GetParameter(1)>=11.49 || ffit->GetParameter(1)<=0.151)) return StatusCode::FAILURE; // fit should have positiv slope for even gas gaps
     return StatusCode::SUCCESS;
 }
+
+StatusCode Muon::UTPCMMClusterBuilderTool::getCalibratedClusterPosition(const Muon::MMPrepData* cluster, std::vector<NSWCalib::CalibratedStrip>& strips, 
+					    Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const {
+
+    std::vector<Identifier> ids;
+    std::vector<float> stripsPos;
+    std::vector<float> driftDists;
+    std::vector<Amg::MatrixX> driftDistErrors;
+
+    for (const NSWCalib::CalibratedStrip &strip : strips)  {
+            ids.push_back(strip.identifier);
+            stripsPos.push_back(strip.locPos.x());
+            driftDists.push_back(strip.distDrift);
+
+            Amg::MatrixX cov(2,2);
+            cov.setIdentity();
+            cov(0,0) = strip.resTransDistDrift;  
+            cov(1,1) = strip.resLongDistDrift; 
+            driftDistErrors.push_back(cov);    
+    }
+
+    double localClusterPosition=-9999;
+    double sigmaLocalClusterPosition=0;
+    double finalFitAngle,finalFitChiSqProb;
+
+    StatusCode sc = finalFit(ids, stripsPos, driftDists, driftDistErrors, localClusterPosition, sigmaLocalClusterPosition,finalFitAngle,finalFitChiSqProb);
+    if(sc==StatusCode::FAILURE) return sc;
+
+    clusterLocalPosition[Trk::locX] =  localClusterPosition;
+    
+    Amg::MatrixX covN (1,1);
+    covN.coeffRef(0,0)=sigmaLocalClusterPosition;
+    covMatrix=covN;
+
+    return StatusCode::SUCCESS;
+}
+
diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h
index 1bf402b80789..a810de90d756 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h
@@ -46,7 +46,11 @@ namespace Muon
     StatusCode getClusters(std::vector<Muon::MMPrepData>& MMprds, 
 	 		   std::vector<std::unique_ptr<Muon::MMPrepData>>& clustersVec)const override ;
 
-  private:
+    StatusCode getCalibratedClusterPosition(const Muon::MMPrepData* cluster, std::vector<NSWCalib::CalibratedStrip>&, 
+					    Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const;
+
+
+  private: 
 
     /// Muon Detector Descriptor
     ServiceHandle<Muon::IMuonIdHelperSvc> m_idHelperSvc {this, "MuonIdHelperSvc", "Muon::MuonIdHelperSvc/MuonIdHelperSvc"};
@@ -72,7 +76,8 @@ namespace Muon
 
     StatusCode transformParameters(double alpha, double d, double dRMS, double& slope, double& intercept, double& interceptRMS)const;
     StatusCode applyCrossTalkCut(std::vector<int> &idxSelected,const std::vector<MMPrepData> &MMPrdsOfLayer,std::vector<int> &flag,int &nStripsCut)const;
-    StatusCode finalFit(const std::vector<Muon::MMPrepData> &mmPrd, std::vector<int>& idxSelected,double& x0, double &sigmaX0, double &fitAngle, double &chiSqProb)const;
+    //StatusCode finalFit(const std::vector<Muon::MMPrepData> &mmPrd, std::vector<int>& idxSelected,double& x0, double &sigmaX0, double &fitAngle, double &chiSqProb)const;
+    StatusCode finalFit(const std::vector<Identifier>& ids, const std::vector<float>& stripsPos, const std::vector<float>& driftDists, const std::vector<Amg::MatrixX> driftDistErrors, double& x0, double &sigmaX0, double &fitAngle, double &chiSqProb)const;
   };
 
 
-- 
GitLab


From a6180b5a95efa3e81beef935f46142080ec9dbbc Mon Sep 17 00:00:00 2001
From: Patrick Scholer <patrick.scholer@cern.ch>
Date: Mon, 10 Jan 2022 17:10:38 +0100
Subject: [PATCH 03/11] make it compile

---
 .../NSWCalibTools/src/NSWCalibTool.cxx        |  7 +++--
 .../NSWCalib/NSWCalibTools/src/NSWCalibTool.h |  2 +-
 .../src/UTPCMMClusterBuilderTool.cxx          | 27 +++----------------
 3 files changed, 10 insertions(+), 26 deletions(-)

diff --git a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx
index 5bfc299ee281..c168f5c3a963 100644
--- a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx
+++ b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx
@@ -281,12 +281,15 @@ StatusCode Muon::NSWCalibTool::mmGasProperties(float &vDrift, float &longDiff, f
 
 
 bool Muon::NSWCalibTool::localStripPosition(const Identifier& id, Amg::Vector2D &locPos) const {
+  // MuonDetectorManager from the conditions store
+  SG::ReadCondHandle<MuonGM::MuonDetectorManager> muDetMgrHandle{m_muDetMgrKey};
+  const MuonGM::MuonDetectorManager* muDetMgr = muDetMgrHandle.cptr();
   if(m_idHelperSvc->isMM(id)){
-    const MuonGM::MMReadoutElement* detEl = m_muonMgr->getMMReadoutElement(id);
+    const MuonGM::MMReadoutElement* detEl = muDetMgr->getMMReadoutElement(id);
     return detEl->stripPosition(id,locPos);
 
   } else if(m_idHelperSvc->issTgc(id)){
-    const MuonGM::sTgcReadoutElement* detEl = m_muonMgr->getsTgcReadoutElement(id);
+    const MuonGM::sTgcReadoutElement* detEl = muDetMgr->getsTgcReadoutElement(id);
     return detEl->stripPosition(id,locPos);
 
   } else {
diff --git a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h
index 1a895f52b526..af96e52253f0 100644
--- a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h
+++ b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h
@@ -34,7 +34,7 @@ namespace Muon {
     virtual ~NSWCalibTool() = default;
     
     virtual StatusCode calibrateClus(const Muon::MMPrepData* prepData, const Amg::Vector3D& globalPos, std::vector<NSWCalib::CalibratedStrip>& calibClus) const override;
-    virtual StatusCode calibrateStrip(const double time, const double charge, const double lorentzAngle, NSWCalib::CalibratedStrip& calibStrip) const override;
+    virtual StatusCode calibrateStrip(const Identifier id, const double time, const double charge, const double lorentzAngle, NSWCalib::CalibratedStrip& calibStrip) const override;
     virtual StatusCode calibrateStrip(const Muon::MM_RawData* mmRawData, NSWCalib::CalibratedStrip& calibStrip) const override;
     virtual StatusCode calibrateStrip(const Muon::STGC_RawData* sTGCRawData, NSWCalib::CalibratedStrip& calibStrip) const override;
 
diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx
index eeb9e76a5ec1..3d1484641c46 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx
@@ -163,10 +163,6 @@ StatusCode Muon::UTPCMMClusterBuilderTool::getClusters(std::vector<Muon::MMPrepD
                 stripsOfClusterDriftDistErrors.push_back(MMprdsOfLayer.at(idx).localCovariance());
                 stripsOfClusterLocalPos.push_back(MMprdsOfLayer.at(idx).localPosition().x());
             }
-<<<<<<< HEAD
-            auto covN = Amg::MatrixX(1,1);
-            covN.coeffRef(0,0)=sigmaLocalClusterPosition;
-=======
             
             double localClusterPosition=-9999;
             double sigmaLocalClusterPosition=0;
@@ -182,20 +178,15 @@ StatusCode Muon::UTPCMMClusterBuilderTool::getClusters(std::vector<Muon::MMPrepD
         
 
 
-            Amg::MatrixX* covN = new Amg::MatrixX(1,1);
-            covN->coeffRef(0,0)=sigmaLocalClusterPosition;
->>>>>>> 5aa2e54ad6f... implement getCalibratedClusterPosition for MM uTPC
+            auto covN = Amg::MatrixX(1,1);
+            covN.coeffRef(0,0)=sigmaLocalClusterPosition;
             ATH_MSG_DEBUG("Did set covN Matrix");
             int idx = idx_goodStrips[0];
             ATH_MSG_DEBUG("idx_goodStrips[0]: "<<idx << " size: " <<MMprdsOfLayer.size());
             Amg::Vector2D localClusterPositionV(localClusterPosition,MMprdsOfLayer.at(idx).localPosition().y()); // y position is the same for all strips
             ATH_MSG_DEBUG("Did set local position");
 
-<<<<<<< HEAD
             float driftDist = 0.0;
-=======
-	          float driftDist = 0.0;
->>>>>>> 5aa2e54ad6f... implement getCalibratedClusterPosition for MM uTPC
 
             std::unique_ptr<Muon::MMPrepData> prdN = std::make_unique<MMPrepData>(MMprdsOfLayer.at(idx).identify(),
 					    MMprdsOfLayer.at(idx).collectionHash(),
@@ -400,15 +391,9 @@ StatusCode Muon::UTPCMMClusterBuilderTool::applyCrossTalkCut(std::vector<int> &i
 }
 
 
-<<<<<<< HEAD
-StatusCode Muon::UTPCMMClusterBuilderTool::finalFit(const std::vector<Muon::MMPrepData> &mmPrd, std::vector<int>& idxSelected,double& x0, double &sigmaX0, double &fitAngle, double &chiSqProb)const{
+StatusCode Muon::UTPCMMClusterBuilderTool::finalFit(const std::vector<Identifier>& ids, const std::vector<float>& stripsPos, const std::vector<float>& driftDists, const std::vector<Amg::MatrixX> driftDistErrors, double& x0, double &sigmaX0, double &fitAngle, double &chiSqProb)const{
     std::unique_ptr<TGraphErrors> fitGraph=std::make_unique<TGraphErrors>();
     std::unique_ptr<TF1> ffit=std::make_unique<TF1>("ffit","pol1");
-=======
-StatusCode Muon::UTPCMMClusterBuilderTool::finalFit(const std::vector<Identifier>& ids, const std::vector<float>& stripsPos, const std::vector<float>& driftDists, const std::vector<Amg::MatrixX> driftDistErrors, double& x0, double &sigmaX0, double &fitAngle, double &chiSqProb)const{
-    std::unique_ptr<TGraphErrors> fitGraph=std::unique_ptr<TGraphErrors>(new TGraphErrors());
-    std::unique_ptr<TF1> ffit=std::unique_ptr<TF1>(new TF1("ffit","pol1"));
->>>>>>> 5aa2e54ad6f... implement getCalibratedClusterPosition for MM uTPC
     
     double xmin = 5000;
     double xmax = -5000;
@@ -426,11 +411,7 @@ StatusCode Muon::UTPCMMClusterBuilderTool::finalFit(const std::vector<Identifier
     }
     lf->Eval(); 
 
-<<<<<<< HEAD
-    if(m_idHelperSvc->mmIdHelper().gasGap(mmPrd.at(0).identify())%2==1 || !m_digiHasNegativeAngles){
-=======
-    if(m_mmIdHelper->gasGap(ids.at(0))%2==1 || !m_digiHasNegativeAngles){
->>>>>>> 5aa2e54ad6f... implement getCalibratedClusterPosition for MM uTPC
+    if(m_idHelperSvc->mmIdHelper().gasGap(ids.at(0))%2==1 || !m_digiHasNegativeAngles){
         ffit->SetParLimits(1,-11.5,-0.15); //5 to 81 degree
     }else{
         ffit->SetParLimits(1,0.15,11.5); //5 to 81 degree
-- 
GitLab


From 9d5e09a833b153855cd6115b7bd0a4c6ab0764ca Mon Sep 17 00:00:00 2001
From: Patrick Scholer <patrick.scholer@cern.ch>
Date: Tue, 18 Jan 2022 13:44:15 +0100
Subject: [PATCH 04/11] add the seed direction to the MMClusterOnTrackCreator
 calibratedCluster function

---
 .../src/MMClusterOnTrackCreator.cxx                        | 3 ++-
 .../src/MMClusterOnTrackCreator.h                          | 2 +-
 .../MuonRecToolInterfaces/IMuonClusterOnTrackCreator.h     | 7 ++++---
 .../src/MuonClusterSegmentFinderTool.cxx                   | 6 +++++-
 4 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx b/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx
index 2bfe3e3b434c..122fc74cd07f 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx
@@ -83,7 +83,8 @@ const Muon::MuonClusterOnTrack* Muon::MMClusterOnTrackCreator::correct(const Trk
 }
 
 const Muon::MuonClusterOnTrack* Muon::MMClusterOnTrackCreator::calibratedCluster(const Trk::PrepRawData& RIO,
-                                                                                 const Amg::Vector3D& GP) const {
+                                                                                 const Amg::Vector3D& GP,
+                                                                                 const Amg::Vector3D& GD) const {
     MuonClusterOnTrack* cluster = nullptr;
 
     if (!m_idHelperSvc->isMM(RIO.identify())) {
diff --git a/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.h b/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.h
index bed150796b3d..f05840600369 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.h
+++ b/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.h
@@ -76,7 +76,7 @@ namespace Muon {
             @return a pointer to a new Muon::MuonClusterOnTrack object, zero if calibration failed.
             The ownership of the new Muon::MuonClusterOnTrack is passed to the client calling the tool
         */
-        virtual const MuonClusterOnTrack* calibratedCluster(const Trk::PrepRawData& RIO, const Amg::Vector3D& GP) const override;
+        virtual const MuonClusterOnTrack* calibratedCluster(const Trk::PrepRawData& RIO, const Amg::Vector3D& GP, const Amg::Vector3D& GD) const override;
 
     private:
         ServiceHandle<Muon::IMuonIdHelperSvc> m_idHelperSvc{this, "MuonIdHelperSvc", "Muon::MuonIdHelperSvc/MuonIdHelperSvc"};
diff --git a/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonRecToolInterfaces/MuonRecToolInterfaces/IMuonClusterOnTrackCreator.h b/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonRecToolInterfaces/MuonRecToolInterfaces/IMuonClusterOnTrackCreator.h
index 871f6730f1ef..c9fbd29e9e01 100755
--- a/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonRecToolInterfaces/MuonRecToolInterfaces/IMuonClusterOnTrackCreator.h
+++ b/MuonSpectrometer/MuonReconstruction/MuonRecTools/MuonRecToolInterfaces/MuonRecToolInterfaces/IMuonClusterOnTrackCreator.h
@@ -56,14 +56,15 @@ namespace Muon {
             @return Fully calibrated Muon::MuonClusterOnTrack.
                     The memory management of the new Muon::MuonClusterOnTrack is passed to the person calling the function.
         */
-        virtual const MuonClusterOnTrack* calibratedCluster(const Trk::PrepRawData& DC, const Amg::Vector3D& GP) const;
+        virtual const MuonClusterOnTrack* calibratedCluster(const Trk::PrepRawData& DC, const Amg::Vector3D& GP, const Amg::Vector3D& GD) const;
     };
 
     inline const InterfaceID& IMuonClusterOnTrackCreator::interfaceID() { return IID_IMuonClusterOnTrackCreator; }
 
     inline const MuonClusterOnTrack* IMuonClusterOnTrackCreator::calibratedCluster(const Trk::PrepRawData& DC,
-                                                                                   const Amg::Vector3D& GP) const {
-        return createRIO_OnTrack(DC, GP);
+                                                                                   const Amg::Vector3D& GP,
+                                                                                   const Amg::Vector3D& GD) const {
+        return createRIO_OnTrack(DC, GP, GD);
     }
 
 }  // namespace Muon
diff --git a/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx
index 3f7cc9e4e864..fd6eecd4bed6 100755
--- a/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx
@@ -921,6 +921,10 @@ namespace Muon {
         std::vector<const Muon::MuonClusterOnTrack*>& clusters, std::pair<Amg::Vector3D, Amg::Vector3D>& seed) const {
         std::vector<const Muon::MuonClusterOnTrack*> calibratedClusters;
 
+        std::cout << "seed global position " << seed.first << " seed direction " << seed.second.unit() << std::endl;
+        std::cout << "seed global position theta " << seed.first.theta() << " seed direction theta " << seed.second.theta() << std::endl;
+        std::cout << "seed global position phi " << seed.first.phi() << " seed direction phi " << seed.second.phi() << std::endl;
+
         /// loop on the segment clusters and use the phi of the seed to correct them
         for (const Muon::MuonClusterOnTrack* clus : clusters) {
             const Muon::MuonClusterOnTrack* newClus = nullptr;
@@ -934,7 +938,7 @@ namespace Muon {
                 Identifier clus_id = clus->identify();
                 if (m_idHelperSvc->isMM(clus_id)) {
                     /// build a  new MM cluster on track with correct position
-                    newClus = m_mmClusterCreator->calibratedCluster(*(clus->prepRawData()), posOnSurf);
+                    newClus = m_mmClusterCreator->calibratedCluster(*(clus->prepRawData()), posOnSurf,seed.second);
                     // newClus = clus;
                     ATH_MSG_VERBOSE("Position before correction: " << clus->globalPosition().x() << " " << clus->globalPosition().y() << " "
                                                                    << clus->globalPosition().z());
-- 
GitLab


From 6ebbb55767747b0c4a7e7b45e17850b706f250f0 Mon Sep 17 00:00:00 2001
From: Patrick Scholer <patrick.scholer@cern.ch>
Date: Tue, 18 Jan 2022 21:54:16 +0100
Subject: [PATCH 05/11]  add theta to getCalibratedClusterPosition; implement
 getCalibratedClusterPosition for Cluster Time Projection Cluster Builder

---
 .../MMClusterization/IMMClusterBuilderTool.h  |   5 +-
 ...sterTimeProjectionMMClusterBuilderTool.cxx | 122 +++++++++++++-----
 ...lusterTimeProjectionMMClusterBuilderTool.h |  12 +-
 .../ConstraintAngleMMClusterBuilderTool.cxx   |   8 +-
 .../src/SimpleMMClusterBuilderTool.cxx        |  18 ++-
 .../src/SimpleMMClusterBuilderTool.h          |   2 +-
 .../src/UTPCMMClusterBuilderTool.cxx          |   9 +-
 .../src/UTPCMMClusterBuilderTool.h            |   2 +-
 .../src/MMClusterOnTrackCreator.cxx           |   2 +-
 9 files changed, 122 insertions(+), 58 deletions(-)

diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/MMClusterization/IMMClusterBuilderTool.h b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/MMClusterization/IMMClusterBuilderTool.h
index 0fdfde4b1d05..3d71c376a6c0 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/MMClusterization/IMMClusterBuilderTool.h
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/MMClusterization/IMMClusterBuilderTool.h
@@ -37,7 +37,7 @@ namespace Muon {
 					  Amg::Vector2D& clusterLocalPosition, Amg::MatrixX* covMatrix) const;
     
     virtual StatusCode getCalibratedClusterPosition(const Muon::MMPrepData* cluster, std::vector<NSWCalib::CalibratedStrip>&,
-						    Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const;
+						   const float thetaEstimate, Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const;
 
     
   };
@@ -52,9 +52,10 @@ namespace Muon {
   }
 
   inline StatusCode IMMClusterBuilderTool::getCalibratedClusterPosition(const Muon::MMPrepData* cluster, std::vector<NSWCalib::CalibratedStrip>& strips,
-									Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const
+								const float thetaEstimate	, Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const
   {
     (void)cluster;
+    (void) thetaEstimate;
     strips.clear();
     covMatrix = Amg::MatrixX(1,1);
     clusterLocalPosition = Amg::Vector2D(0.0,0.0);
diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/ClusterTimeProjectionMMClusterBuilderTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/ClusterTimeProjectionMMClusterBuilderTool.cxx
index 526cdd0d253c..1567a11bea43 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/ClusterTimeProjectionMMClusterBuilderTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/ClusterTimeProjectionMMClusterBuilderTool.cxx
@@ -37,11 +37,39 @@ StatusCode Muon::ClusterTimeProjectionMMClusterBuilderTool::getClusters(
         if (prdsOfLayer.size() < 2) continue;  // require at least two strips per layer
         std::vector<std::vector<uint>> idxClusters;  // index of strips in cluster
         StatusCode sc = clusterLayer(prdsOfLayer, idxClusters);
+
+
         if (sc.isFailure()) continue;
         for (uint i_cluster = 0; i_cluster < idxClusters.size(); i_cluster++) {
             double clusterPosition, clusterPositionErrorSq;
-            sc = getClusterPositionPRD(prdsOfLayer, idxClusters.at(i_cluster),
-				       clusterPosition, clusterPositionErrorSq);
+            std::vector<Identifier> rdoList;
+            std::vector<int> stripCharges;
+            std::vector<float> stripPos;
+            std::vector<float> stripDriftDists;
+            std::vector<Amg::MatrixX> stripDriftDistErrors;
+
+            rdoList.reserve(idxClusters.at(i_cluster).size());
+            stripCharges.reserve(idxClusters.at(i_cluster).size());
+            stripPos.reserve(idxClusters.at(i_cluster).size());
+            
+            stripDriftDists.reserve(idxClusters.at(i_cluster).size());
+            stripDriftDistErrors.reserve(idxClusters.at(i_cluster).size());
+
+            float thetaEstimate = 0;
+        
+            for(auto idx:idxClusters.at(i_cluster)){
+                Identifier id = prdsOfLayer.at(idx).identify();
+                rdoList.push_back(id);
+                stripCharges.push_back(prdsOfLayer.at(idx).charge());
+                stripPos.push_back(prdsOfLayer.at(idx).localPosition().x());              
+                thetaEstimate += std::atan(prdsOfLayer.at(idx).globalPosition().perp() / std::abs(prdsOfLayer.at(idx).globalPosition().z())) * prdsOfLayer.at(idx).charge();
+                stripDriftDists.push_back(prdsOfLayer.at(idx).driftDist());
+                stripDriftDistErrors.push_back(prdsOfLayer.at(idx).localCovariance());                     
+            }
+            thetaEstimate /= std::accumulate(stripCharges.begin(),stripCharges.end(),0.0);
+
+            sc = getClusterPositionPRD(rdoList, stripPos, stripDriftDists, stripDriftDistErrors, stripCharges, thetaEstimate , clusterPosition, clusterPositionErrorSq);
+
             if (sc.isFailure()) continue;
             sc = writeClusterPrd(prdsOfLayer, idxClusters.at(i_cluster),
                                  clusterPosition, clusterPositionErrorSq, clustersVec);
@@ -111,42 +139,37 @@ StatusCode Muon::ClusterTimeProjectionMMClusterBuilderTool::clusterLayer(
     return StatusCode::SUCCESS;
 }  // end of cluster layer
 
-StatusCode Muon::ClusterTimeProjectionMMClusterBuilderTool::getClusterPositionPRD(const std::vector<Muon::MMPrepData> &MMPrdsOfLayer,
-										  const std::vector<uint> &idxCluster, 
+StatusCode Muon::ClusterTimeProjectionMMClusterBuilderTool::getClusterPositionPRD(const std::vector<Identifier>& ids,
+                                          const std::vector<float>& stripsPos, const std::vector<float>& driftDists, 
+                                          const std::vector<Amg::MatrixX> driftDistErrors, const std::vector<int> &charges, const float thetaEstimate, 
 										  double &clusterPosition,
 										  double &clusterPositionErrorSq) const {
-  if (idxCluster.empty()) return StatusCode::FAILURE;
-  double qtot = 0;
-    double meanTheta = 0;
+    if (ids.empty()) return StatusCode::FAILURE;
+    double qtot = 0;
     double meanDriftDist = 0;
     double meanDriftDistError = 0;
     double meanPosX = 0;
     double meanPosXError = 0;
-    for (auto idx : idxCluster) {
-        double driftDist = MMPrdsOfLayer.at(idx).driftDist();
-        double charge = MMPrdsOfLayer.at(idx).charge()*Gaudi::Units::perThousand; //  divide by 1000 to avoid overflow of variables
+    for (uint i_strip=0;i_strip<ids.size(); i_strip++) {
+        double driftDist = driftDists.at(i_strip);
+        double charge = charges.at(i_strip)*Gaudi::Units::perThousand; //  divide by 1000 to avoid overflow of variables
         qtot += charge;
-        meanPosX += MMPrdsOfLayer.at(idx).localPosition().x()*charge;
-        meanPosXError += MMPrdsOfLayer.at(idx).localCovariance()(1, 1)*charge*charge;
+        meanPosX += stripsPos.at(i_strip)*charge;
+        meanPosXError += driftDistErrors.at(i_strip)(1, 1)*charge*charge;
         meanDriftDist += driftDist*charge;
-        meanDriftDistError += MMPrdsOfLayer.at(idx).localCovariance()(0, 0)*charge*charge;
+        meanDriftDistError += driftDistErrors.at(i_strip)(0, 0)*charge*charge;
         ATH_MSG_VERBOSE("Strip:"
                         <<" drift dist " << driftDist
-                        <<" +- "<<  std::sqrt(MMPrdsOfLayer.at(idx).localCovariance()(0,0))
-                        <<" xpos " << MMPrdsOfLayer.at(idx).localPosition().x()
-                        << " +- "<< std::sqrt(MMPrdsOfLayer.at(idx).localCovariance()(1,1))
+                        <<" +- "<<  std::sqrt(driftDistErrors.at(i_strip)(0,0))
+                        <<" xpos " << stripsPos.at(i_strip)
+                        << " +- "<< std::sqrt(driftDistErrors.at(i_strip)(1,1))
                         <<" xMeanPos " << meanPosX / qtot
                         <<" +- " << std::sqrt(meanPosXError) / qtot
                         << " meanPosXError " << meanPosXError
                         <<" meanDriftDist " << meanDriftDist/qtot
                         <<" meanDriftDist Error " <<  std::sqrt(meanDriftDistError)/qtot
                         <<" charge " << charge << " qtot " << qtot
-                        << " theta " << std::atan(MMPrdsOfLayer.at(idx).globalPosition().perp() /
-                        std::abs(MMPrdsOfLayer.at(idx).globalPosition().z()))
-                        << " meanTheta " << meanTheta / qtot);
-
-        meanTheta += std::abs(std::atan(MMPrdsOfLayer.at(idx).globalPosition().perp() /
-                        std::abs(MMPrdsOfLayer.at(idx).globalPosition().z())))*charge;
+                        );
     }
     meanPosX /= qtot;
     double meanPosXErrorSq = meanPosXError/(qtot*qtot);
@@ -155,20 +178,19 @@ StatusCode Muon::ClusterTimeProjectionMMClusterBuilderTool::getClusterPositionPR
                     << "qtot*qtot" << qtot*qtot);
     meanDriftDist /= qtot;
     double meanDriftDistErrorSq = meanDriftDistError/(qtot*qtot);
-    meanTheta /= qtot;
     ATH_MSG_VERBOSE("Cluster: "<< "xmean" << meanPosX);
     ATH_MSG_VERBOSE("Cluster: " << "meanPosXErrorSq" << meanPosXErrorSq
                     << " sqrt "<< sqrt(meanPosXErrorSq));
     ATH_MSG_VERBOSE("Cluster: "<< " meanDriftDist" << meanDriftDist);
     ATH_MSG_VERBOSE("Cluster: "<< " meanDriftDistErrorSq " << meanDriftDistErrorSq
                                << " sqrt " << std::sqrt(meanDriftDistErrorSq));
-    ATH_MSG_VERBOSE("Cluster " << " meanTheta " << meanTheta);
+    ATH_MSG_VERBOSE("Cluster " << " thetaEstimate " << thetaEstimate);
 
-    double correction = std::tan(meanTheta)*(meanDriftDist-halfGapWidth);
-    if (m_idHelperSvc->mmIdHelper().gasGap(MMPrdsOfLayer.at(0).identify())%2 == 0) {
+    double correction = std::tan(thetaEstimate)*(meanDriftDist-halfGapWidth);
+    if (m_idHelperSvc->mmIdHelper().gasGap(ids.at(0))%2 == 0) {
      correction = -1. * correction;  // take care of inverted drif direction for even gaps
     }
-    double correctionErrorSq = std::tan(meanTheta)*std::tan(meanTheta)*meanDriftDistErrorSq;
+    double correctionErrorSq = std::tan(thetaEstimate)*std::tan(thetaEstimate)*meanDriftDistErrorSq;
 
 
     clusterPosition = meanPosX + correction;
@@ -201,9 +223,9 @@ StatusCode Muon::ClusterTimeProjectionMMClusterBuilderTool::writeClusterPrd(
     std::vector<Amg::MatrixX> stripDriftDistErrors;
 
     rdoList.reserve(idxCluster.size());
+    stripCharges.reserve(idxCluster.size());
+    stripTimes.reserve(idxCluster.size());
     if(m_writeStripProperties) {
-        stripCharges.reserve(idxCluster.size());
-        stripTimes.reserve(idxCluster.size());
         stripNumbers.reserve(idxCluster.size());
     }
     stripDriftDists.reserve(idxCluster.size());
@@ -213,10 +235,10 @@ StatusCode Muon::ClusterTimeProjectionMMClusterBuilderTool::writeClusterPrd(
         Identifier id = MMPrdsOfLayer.at(idx).identify();
         rdoList.push_back(id);
         if(m_writeStripProperties) {
-            stripCharges.push_back(MMPrdsOfLayer.at(idx).charge());
-            stripTimes.push_back(MMPrdsOfLayer.at(idx).time());
             stripNumbers.push_back(channel(id));
         }
+        stripTimes.push_back(MMPrdsOfLayer.at(idx).time());
+        stripCharges.push_back(MMPrdsOfLayer.at(idx).charge());
         stripDriftDists.push_back(MMPrdsOfLayer.at(idx).driftDist());
         stripDriftDistErrors.push_back(MMPrdsOfLayer.at(idx).localCovariance());
     }
@@ -242,3 +264,41 @@ StatusCode Muon::ClusterTimeProjectionMMClusterBuilderTool::writeClusterPrd(
     clustersVec.push_back(std::move(prdN));
     return StatusCode::SUCCESS;
 }
+
+StatusCode Muon::ClusterTimeProjectionMMClusterBuilderTool::getCalibratedClusterPosition (const Muon::MMPrepData* cluster, std::vector<NSWCalib::CalibratedStrip>& strips,
+        const float theta, Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const {
+    
+    std::vector<Identifier> ids;
+    std::vector<float> stripsPos;
+    std::vector<float> driftDists;
+    std::vector<Amg::MatrixX> driftDistErrors;
+
+    for (const NSWCalib::CalibratedStrip &strip : strips)  {
+            ids.push_back(strip.identifier);
+            stripsPos.push_back(strip.locPos.x());
+            driftDists.push_back(strip.distDrift);
+
+            Amg::MatrixX cov(2,2);
+            cov.setIdentity();
+            cov(0,0) = strip.resTransDistDrift;  
+            cov(1,1) = strip.resLongDistDrift; 
+            driftDistErrors.push_back(cov);    
+    }
+
+    double localClusterPosition=-9999;
+    double sigmaLocalClusterPosition=0;
+
+    StatusCode sc = getClusterPositionPRD(ids, stripsPos, driftDists, driftDistErrors, cluster->stripCharges() , theta, localClusterPosition, sigmaLocalClusterPosition);
+
+    if(sc==StatusCode::FAILURE) return sc;
+
+    clusterLocalPosition[Trk::locX] =  localClusterPosition;
+    
+    Amg::MatrixX covN (1,1);
+    covN.coeffRef(0,0)=sigmaLocalClusterPosition;
+    covMatrix=covN;
+
+    return StatusCode::SUCCESS;
+
+
+    }
\ No newline at end of file
diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/ClusterTimeProjectionMMClusterBuilderTool.h b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/ClusterTimeProjectionMMClusterBuilderTool.h
index 7fb0069ab6db..c00008dc822e 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/ClusterTimeProjectionMMClusterBuilderTool.h
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/ClusterTimeProjectionMMClusterBuilderTool.h
@@ -28,6 +28,9 @@ class ClusterTimeProjectionMMClusterBuilderTool :
   StatusCode getClusters(std::vector<Muon::MMPrepData>& MMprds,
       std::vector<std::unique_ptr<Muon::MMPrepData>>& clustersVec) const override;
 
+  StatusCode getCalibratedClusterPosition(const Muon::MMPrepData* cluster, std::vector<NSWCalib::CalibratedStrip>&, 
+	   const float theta, Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const;
+
  private:
     /// Muon Detector Descriptor
     ServiceHandle<Muon::IMuonIdHelperSvc> m_idHelperSvc {this, "MuonIdHelperSvc", "Muon::MuonIdHelperSvc/MuonIdHelperSvc"};
@@ -43,10 +46,11 @@ class ClusterTimeProjectionMMClusterBuilderTool :
                             std::vector<std::vector<uint>> &idxClusters) const;
 
 
-    StatusCode getClusterPositionPRD(const std::vector<Muon::MMPrepData> &MMPrdsOfLayer,
-				     const std::vector<uint> &idxCluster,
-				     double &clustersPosition,
-				     double &clustersPositionErrorSq) const;
+    StatusCode getClusterPositionPRD(const std::vector<Identifier>& ids, 
+                                const std::vector<float>& stripsPos, const std::vector<float>& driftDists, 
+                                const std::vector<Amg::MatrixX> driftDistErrors, const std::vector<int> &charges, const float thetaEstimate, 
+								double &clusterPosition,
+								double &clusterPositionErrorSq) const;
 
     StatusCode writeClusterPrd(const std::vector<Muon::MMPrepData> &MMPrdsOfLayer,
                                const std::vector<uint> &idxCluster,
diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/ConstraintAngleMMClusterBuilderTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/ConstraintAngleMMClusterBuilderTool.cxx
index 1063597c4a0b..df33efda3069 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/ConstraintAngleMMClusterBuilderTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/ConstraintAngleMMClusterBuilderTool.cxx
@@ -263,10 +263,10 @@ const{
     uint nStrips = idxCluster.size();
 
     stripsOfCluster.reserve(nStrips);
+    stripsOfClusterCharges.reserve(nStrips);
+    stripsOfClusterTimes.reserve(nStrips);
 
     if (m_writeStripProperties) {
-        stripsOfClusterCharges.reserve(nStrips);
-        stripsOfClusterTimes.reserve(nStrips);
         stripsOfClusterChannels.reserve(nStrips);
     }
     stripDriftDists.reserve(nStrips);
@@ -283,9 +283,9 @@ const{
         fitFunc.addPoint(x,y,xerror,yerror);
 
         stripsOfCluster.push_back(prdPerLayer.at(idxCluster.at(i_strip)).identify());
+        stripsOfClusterCharges.push_back(prdPerLayer.at(idxCluster.at(i_strip)).charge());
+        stripsOfClusterTimes.push_back(prdPerLayer.at(idxCluster.at(i_strip)).time());
         if (m_writeStripProperties) {
-            stripsOfClusterCharges.push_back(prdPerLayer.at(idxCluster.at(i_strip)).charge());
-            stripsOfClusterTimes.push_back(prdPerLayer.at(idxCluster.at(i_strip)).time());
             stripsOfClusterChannels.push_back(m_idHelperSvc->mmIdHelper().channel(prdPerLayer.at(idxCluster.at(i_strip)).identify()));
         }
         stripDriftDists.push_back(prdPerLayer.at(i_strip).driftDist());
diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/SimpleMMClusterBuilderTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/SimpleMMClusterBuilderTool.cxx
index 49e54b5d0f0c..4ee847d84007 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/SimpleMMClusterBuilderTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/SimpleMMClusterBuilderTool.cxx
@@ -83,10 +83,9 @@ StatusCode Muon::SimpleMMClusterBuilderTool::getClusters(std::vector<Muon::MMPre
     MMflag[i] = 1;
     mergeIndices.push_back(i);
     mergeStrips.push_back(strip);
-    if (m_writeStripProperties) {
-      mergeStripsTime.push_back(MMprds[i].time());
-      mergeStripsCharge.push_back(MMprds[i].charge());
-    }
+    mergeStripsTime.push_back(MMprds[i].time());
+    mergeStripsCharge.push_back(MMprds[i].charge());
+    
     mergeStripsDriftDists.push_back(MMprds[i].driftDist());
     mergeStripsDriftDistErrors.push_back(MMprds[i].localCovariance());
 
@@ -110,10 +109,9 @@ StatusCode Muon::SimpleMMClusterBuilderTool::getClusters(std::vector<Muon::MMPre
 	    MMflag[j] = 1;
 	    mergeIndices.push_back(j);
 	    mergeStrips.push_back(stripN);
-	    if(m_writeStripProperties) {
-	      mergeStripsTime.push_back(MMprds[j].time());
-	      mergeStripsCharge.push_back(MMprds[j].charge());
-	    }
+	    mergeStripsTime.push_back(MMprds[j].time());
+	    mergeStripsCharge.push_back(MMprds[j].charge());
+	  
 	    mergeStripsDriftDists.push_back(MMprds[j].driftDist());
 	    mergeStripsDriftDistErrors.push_back(MMprds[j].localCovariance());
 	    nmergeStrips++;
@@ -222,10 +220,10 @@ StatusCode SimpleMMClusterBuilderTool::getClusterPosition(std::vector<Muon::MMPr
 
 
 StatusCode SimpleMMClusterBuilderTool::getCalibratedClusterPosition(const Muon::MMPrepData* cluster, std::vector<NSWCalib::CalibratedStrip>& strips, 
-								    Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const
+								 const float theta, Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const
   
 {
-
+  (void) theta; // avoid unused parameter warning 
   /// correct the precision coordinate of the local position based on the centroid calibration
   double xPosCalib = 0.0;
   double totalCharge = 0.0;
diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/SimpleMMClusterBuilderTool.h b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/SimpleMMClusterBuilderTool.h
index 89a241779007..78b1a81bc962 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/SimpleMMClusterBuilderTool.h
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/SimpleMMClusterBuilderTool.h
@@ -34,7 +34,7 @@ namespace Muon
     StatusCode getClusterPosition(std::vector<Muon::MMPrepData>& stripsVect, 
 				  Amg::Vector2D& clusterLocalPosition, Amg::MatrixX* covMatrix) const;
     
-    StatusCode getCalibratedClusterPosition(const Muon::MMPrepData* cluster, std::vector<NSWCalib::CalibratedStrip>&, 
+    StatusCode getCalibratedClusterPosition(const Muon::MMPrepData* cluster, std::vector<NSWCalib::CalibratedStrip>&, const float theta,
 					    Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const;
 
   private: 
diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx
index 3d1484641c46..5e8e9a2e7ec3 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx
@@ -139,9 +139,9 @@ StatusCode Muon::UTPCMMClusterBuilderTool::getClusters(std::vector<Muon::MMPrepD
             stripsOfCluster.reserve(idx_goodStrips.size());
             if (m_writeStripProperties) {
                 stripsOfClusterChannels.reserve(idx_goodStrips.size());
-                stripsOfClusterTimes.reserve(idx_goodStrips.size());
-                stripsOfClusterCharges.reserve(idx_goodStrips.size());
             }
+            stripsOfClusterTimes.reserve(idx_goodStrips.size());
+            stripsOfClusterCharges.reserve(idx_goodStrips.size());
             stripsOfClusterDriftDists.reserve(idx_goodStrips.size());
             stripsOfClusterDriftDistErrors.reserve(idx_goodStrips.size());
             stripsOfClusterLocalPos.reserve(idx_goodStrips.size());
@@ -460,8 +460,9 @@ StatusCode Muon::UTPCMMClusterBuilderTool::finalFit(const std::vector<Identifier
 }
 
 StatusCode Muon::UTPCMMClusterBuilderTool::getCalibratedClusterPosition(const Muon::MMPrepData* cluster, std::vector<NSWCalib::CalibratedStrip>& strips, 
-					    Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const {
-
+					   const float theta, Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const {
+    (void) cluster;
+    (void) theta; // avoid unused parameter warning
     std::vector<Identifier> ids;
     std::vector<float> stripsPos;
     std::vector<float> driftDists;
diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h
index a810de90d756..8d1b0670305f 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h
@@ -46,7 +46,7 @@ namespace Muon
     StatusCode getClusters(std::vector<Muon::MMPrepData>& MMprds, 
 	 		   std::vector<std::unique_ptr<Muon::MMPrepData>>& clustersVec)const override ;
 
-    StatusCode getCalibratedClusterPosition(const Muon::MMPrepData* cluster, std::vector<NSWCalib::CalibratedStrip>&, 
+    StatusCode getCalibratedClusterPosition(const Muon::MMPrepData* cluster, std::vector<NSWCalib::CalibratedStrip>&, const float theta ,
 					    Amg::Vector2D& clusterLocalPosition, Amg::MatrixX& covMatrix) const;
 
 
diff --git a/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx b/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx
index 122fc74cd07f..ed18881751de 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx
@@ -145,7 +145,7 @@ const Muon::MuonClusterOnTrack* Muon::MMClusterOnTrackCreator::calibratedCluster
     localposition[Trk::locY] = 0.0;
 
     /// calibrate the cluster position along the precision coordinate
-    sc = m_clusterBuilderTool->getCalibratedClusterPosition(MClus, calibratedStrips, localposition, loce);
+    sc = m_clusterBuilderTool->getCalibratedClusterPosition(MClus, calibratedStrips, GD.theta(), localposition, loce);
     if (sc != StatusCode::SUCCESS) {
         ATH_MSG_WARNING("Could not calibrate the MM Cluster in the RIO on track creator");
         return cluster;
-- 
GitLab


From e7ec44b65e9bb065ae9a88185d35e503451749c2 Mon Sep 17 00:00:00 2001
From: Patrick Scholer <patrick.scholer@cern.ch>
Date: Sun, 20 Feb 2022 21:05:14 +0100
Subject: [PATCH 06/11] remove cout

---
 .../DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx
index c90ce9a1fe99..82c9a0dccbfc 100755
--- a/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx
@@ -995,9 +995,9 @@ namespace Muon {
         std::vector<const Muon::MuonClusterOnTrack*>& clusters, std::pair<Amg::Vector3D, Amg::Vector3D>& seed) const {
         std::vector<const Muon::MuonClusterOnTrack*> calibratedClusters;
 
-        std::cout << "seed global position " << seed.first << " seed direction " << seed.second.unit() << std::endl;
-        std::cout << "seed global position theta " << seed.first.theta() << " seed direction theta " << seed.second.theta() << std::endl;
-        std::cout << "seed global position phi " << seed.first.phi() << " seed direction phi " << seed.second.phi() << std::endl;
+        ATH_MSG_VERBOSE("seed global position " << seed.first << " seed direction " << seed.second.unit());
+        ATH_MSG_VERBOSE("seed global position theta " << seed.first.theta() << " seed direction theta " << seed.second.theta());
+        ATH_MSG_VERBOSE("seed global position phi " << seed.first.phi() << " seed direction phi " << seed.second.phi());
 
         /// loop on the segment clusters and use the phi of the seed to correct them
         for (const Muon::MuonClusterOnTrack* clus : clusters) {
-- 
GitLab


From d5a5119b251257a268defb389763d562a6e8934b Mon Sep 17 00:00:00 2001
From: Patrick Scholer <patrick.scholer@cern.ch>
Date: Thu, 12 May 2022 16:01:28 +0200
Subject: [PATCH 07/11] fix merge conflict in MuonClusterSegmentFinderTool.cxx

---
 .../src/MuonClusterSegmentFinderTool.cxx      | 43 ++++++-------------
 1 file changed, 12 insertions(+), 31 deletions(-)

diff --git a/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx
index d971995ce29e..c6403006bdf9 100755
--- a/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx
@@ -1003,44 +1003,25 @@ namespace Muon {
         ATH_MSG_VERBOSE("seed global position theta " << seed.first.theta() << " seed direction theta " << seed.second.theta());
         ATH_MSG_VERBOSE("seed global position phi " << seed.first.phi() << " seed direction phi " << seed.second.phi());
 
-        /// loop on the segment clusters and use the phi of the seed to correct them
+        // loop on the segment clusters and use the phi of the seed to correct them
         for (const Muon::MuonClusterOnTrack* clus : clusters) {
             const Muon::MuonClusterOnTrack* newClus = nullptr;
-            /// get the intercept of the seed direction with the cluster surface
-            const Trk::PlaneSurface* surf = dynamic_cast<const Trk::PlaneSurface*>(&clus->associatedSurface());
-            if (surf) {
-                Amg::Vector3D posOnSurf = intersectPlane(*surf, seed.first, seed.second);
-                Amg::Vector2D lpos;
-                surf->globalToLocal(posOnSurf, posOnSurf, lpos);
-                /// correct the eta position of the MM stereo layers only, based on the
-                Identifier clus_id = clus->identify();
-                if (m_idHelperSvc->isMM(clus_id)) {
-                    /// build a  new MM cluster on track with correct position
-                    newClus = m_mmClusterCreator->calibratedCluster(*(clus->prepRawData()), posOnSurf,seed.second);
-                    // newClus = clus;
-                    ATH_MSG_VERBOSE("Position before correction: " << clus->globalPosition().x() << " " << clus->globalPosition().y() << " "
-                                                                   << clus->globalPosition().z());
-                    ATH_MSG_VERBOSE("Position after correction: " << newClus->globalPosition().x() << " " << newClus->globalPosition().y()
-                                                                  << " " << newClus->globalPosition().z());
-                }
-
-            const Muon::MuonClusterOnTrack* newClus{nullptr};
-
+            
             // get the intercept of the seed direction with the cluster surface
             Identifier hitID = clus->identify();
-            const Trk::Surface& surf = clus->associatedSurface();
+            const Trk::PlaneSurface& surf = clus->associatedSurface();
             Trk::Intersection intersect = surf.straightLineIntersection(seed.first, seed.second, false, false);
 
             if (m_idHelperSvc->isMM(hitID)) {
-                // build a new MMClusterOnTrack with correct position
-                newClus = m_mmClusterCreator->calibratedCluster(*(clus->prepRawData()), intersect.position);
-
-                if (msgLvl(MSG::VERBOSE)) {
-                    const Amg::Vector3D& gpos_hit = clus->globalPosition();
-                    const Amg::Vector3D& gpos_new = newClus->globalPosition();
-                    ATH_MSG_VERBOSE("Position before correction: " << gpos_hit.x() << " " << gpos_hit.y() << " " << gpos_hit.z());
-                    ATH_MSG_VERBOSE("Position after correction: "  << gpos_new.x() << " " << gpos_new.y() << " " << gpos_new.z());
-                }
+                 // build a  new MM cluster on track with correct position
+                 newClus = m_mmClusterCreator->calibratedCluster(*(clus->prepRawData()), intersect.position,seed.second);
+
+                 if (msgLvl(MSG::VERBOSE)) {
+                     const Amg::Vector3D& gpos_hit = clus->globalPosition();
+                     const Amg::Vector3D& gpos_new = newClus->globalPosition();
+                     ATH_MSG_VERBOSE("Position before correction: " << gpos_hit.x() << " " << gpos_hit.y() << " " << gpos_hit.z());
+                     ATH_MSG_VERBOSE("Position after correction: "  << gpos_new.x() << " " << gpos_new.y() << " " << gpos_new.z());
+                 }
 
             } else if (m_idHelperSvc->issTgc(hitID)) {
                 // calibration to be added for sTGCs
-- 
GitLab


From c8571ed8b50fbd4ef0e46f9707c796456dff4b10 Mon Sep 17 00:00:00 2001
From: Patrick Scholer <patrick.scholer@cern.ch>
Date: Thu, 12 May 2022 16:56:39 +0200
Subject: [PATCH 08/11] fix compillation

---
 .../MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx   | 2 +-
 .../DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx b/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx
index 9c77f34617fb..1ac5c31eb5e2 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonRIO_OnTrackCreators/MuonClusterOnTrackCreator/src/MMClusterOnTrackCreator.cxx
@@ -148,7 +148,7 @@ const Muon::MuonClusterOnTrack* Muon::MMClusterOnTrackCreator::calibratedCluster
     localposition2D[Trk::locY] = 0.0;
 
     /// calibrate the cluster position along the precision coordinate
-    sc = m_clusterBuilderTool->getCalibratedClusterPosition(MClus, calibratedStrips, GD.theta(), localposition, loce);
+    sc = m_clusterBuilderTool->getCalibratedClusterPosition(MClus, calibratedStrips, GD.theta(), localposition2D, loce);
     if (sc != StatusCode::SUCCESS) {
         ATH_MSG_WARNING("Could not calibrate the MM Cluster in the RIO on track creator");
         return cluster;
diff --git a/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx
index c6403006bdf9..a39d63111484 100755
--- a/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonSegmentMakers/MuonSegmentMakerTools/DCMathSegmentMaker/src/MuonClusterSegmentFinderTool.cxx
@@ -1009,7 +1009,7 @@ namespace Muon {
             
             // get the intercept of the seed direction with the cluster surface
             Identifier hitID = clus->identify();
-            const Trk::PlaneSurface& surf = clus->associatedSurface();
+            const Trk::Surface& surf = clus->associatedSurface();
             Trk::Intersection intersect = surf.straightLineIntersection(seed.first, seed.second, false, false);
 
             if (m_idHelperSvc->isMM(hitID)) {
-- 
GitLab


From aea4665c34424550a081267a490f598b31cdb31d Mon Sep 17 00:00:00 2001
From: Johannes Junggeburth <johannes.josef.junggeburth@cern.ch>
Date: Fri, 13 May 2022 10:19:49 +0200
Subject: [PATCH 09/11] appply suggestions from review

---
 .../NSWCalib/NSWCalibTools/NSWCalibTools/INSWCalibTool.h      | 4 ++--
 .../MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/NSWCalibTools/INSWCalibTool.h b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/NSWCalibTools/INSWCalibTool.h
index f2083aa3a139..59fe778ae4d5 100644
--- a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/NSWCalibTools/INSWCalibTool.h
+++ b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/NSWCalibTools/INSWCalibTool.h
@@ -27,7 +27,7 @@ namespace NSWCalib {
     double resTransDistDrift = 0;
     double resLongDistDrift = 0;
     double dx = 0;
-    Amg::Vector2D locPos = Amg::Vector2D(-FLT_MAX,-FLT_MAX);
+    Amg::Vector2D locPos{-FLT_MAX,FLT_MAX};
     Identifier identifier;
   };
 
@@ -49,7 +49,7 @@ namespace Muon {
     
     virtual StatusCode calibrateClus(const Muon::MMPrepData* prepRawData, const Amg::Vector3D& globalPos, std::vector<NSWCalib::CalibratedStrip>& calibClus) const = 0;
     virtual StatusCode calibrateStrip(const Muon::MM_RawData* mmRawData, NSWCalib::CalibratedStrip& calibStrip) const = 0;
-    virtual StatusCode calibrateStrip(const Identifier id, const double time,  const double charge, const double lorentzAngle, NSWCalib::CalibratedStrip&calibStrip) const = 0;
+    virtual StatusCode calibrateStrip(const Identifier& id, const double time,  const double charge, const double lorentzAngle, NSWCalib::CalibratedStrip&calibStrip) const = 0;
     virtual StatusCode calibrateStrip(const Muon::STGC_RawData* sTGCRawData, NSWCalib::CalibratedStrip& calibStrip) const = 0;
     virtual StatusCode distToTime(const Muon::MMPrepData* prepData, const Amg::Vector3D& globalPos,const std::vector<double>& driftDistances, std::vector<double>& driftTimes) const = 0;
     
diff --git a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx
index d27c11bbc119..05d812882a29 100644
--- a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx
+++ b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.cxx
@@ -144,7 +144,7 @@ StatusCode Muon::NSWCalibTool::calibrateClus(const Muon::MMPrepData* prepData, c
 
 
 
-StatusCode Muon::NSWCalibTool::calibrateStrip(const Identifier id, const double time, const double charge, const double lorentzAngle, NSWCalib::CalibratedStrip& calibStrip) const {
+StatusCode Muon::NSWCalibTool::calibrateStrip(const Identifier& id, const double time, const double charge, const double lorentzAngle, NSWCalib::CalibratedStrip& calibStrip) const {
   //get local positon
   Amg::Vector2D locPos;
   if(!localStripPosition(id,locPos)) {
-- 
GitLab


From 010635f4628def30d5ab479663e731acd510f3f8 Mon Sep 17 00:00:00 2001
From: Patrick Scholer <patrick.scholer@cern.ch>
Date: Fri, 13 May 2022 11:30:12 +0200
Subject: [PATCH 10/11] remove commented code

---
 .../MMClusterization/src/UTPCMMClusterBuilderTool.cxx            | 1 -
 .../MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h | 1 -
 2 files changed, 2 deletions(-)

diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx
index 5e8e9a2e7ec3..677e75456ac3 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.cxx
@@ -168,7 +168,6 @@ StatusCode Muon::UTPCMMClusterBuilderTool::getClusters(std::vector<Muon::MMPrepD
             double sigmaLocalClusterPosition=0;
             double finalFitAngle,finalFitChiSqProb;
 
-            //sc=finalFit(MMprdsOfLayer,idx_goodStrips,localClusterPosition, sigmaLocalClusterPosition,finalFitAngle,finalFitChiSqProb);
             sc=finalFit(stripsOfCluster, stripsOfClusterLocalPos, stripsOfClusterDriftDists, stripsOfClusterDriftDistErrors, localClusterPosition, sigmaLocalClusterPosition,finalFitAngle,finalFitChiSqProb);
             
             ATH_MSG_DEBUG("final fit done");
diff --git a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h
index 8d1b0670305f..b7e09d8c19dc 100644
--- a/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h
+++ b/MuonSpectrometer/MuonReconstruction/MuonDataPrep/MMClusterization/src/UTPCMMClusterBuilderTool.h
@@ -76,7 +76,6 @@ namespace Muon
 
     StatusCode transformParameters(double alpha, double d, double dRMS, double& slope, double& intercept, double& interceptRMS)const;
     StatusCode applyCrossTalkCut(std::vector<int> &idxSelected,const std::vector<MMPrepData> &MMPrdsOfLayer,std::vector<int> &flag,int &nStripsCut)const;
-    //StatusCode finalFit(const std::vector<Muon::MMPrepData> &mmPrd, std::vector<int>& idxSelected,double& x0, double &sigmaX0, double &fitAngle, double &chiSqProb)const;
     StatusCode finalFit(const std::vector<Identifier>& ids, const std::vector<float>& stripsPos, const std::vector<float>& driftDists, const std::vector<Amg::MatrixX> driftDistErrors, double& x0, double &sigmaX0, double &fitAngle, double &chiSqProb)const;
   };
 
-- 
GitLab


From 80a393e1a40d59be7d526c56417762da337f3383 Mon Sep 17 00:00:00 2001
From: Patrick Scholer <patrick.scholer@cern.ch>
Date: Fri, 13 May 2022 18:06:39 +0200
Subject: [PATCH 11/11] fix compiler warning

---
 .../MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h         | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h
index 70e0eb562f15..8c0e5bb83b93 100644
--- a/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h
+++ b/MuonSpectrometer/MuonCalib/NSWCalib/NSWCalibTools/src/NSWCalibTool.h
@@ -34,7 +34,7 @@ namespace Muon {
     virtual ~NSWCalibTool() = default;
     
     virtual StatusCode calibrateClus(const Muon::MMPrepData* prepData, const Amg::Vector3D& globalPos, std::vector<NSWCalib::CalibratedStrip>& calibClus) const override;
-    virtual StatusCode calibrateStrip(const Identifier id, const double time, const double charge, const double lorentzAngle, NSWCalib::CalibratedStrip& calibStrip) const override;
+    virtual StatusCode calibrateStrip(const Identifier& id, const double time, const double charge, const double lorentzAngle, NSWCalib::CalibratedStrip& calibStrip) const override;
     virtual StatusCode calibrateStrip(const Muon::MM_RawData* mmRawData, NSWCalib::CalibratedStrip& calibStrip) const override;
     virtual StatusCode calibrateStrip(const Muon::STGC_RawData* sTGCRawData, NSWCalib::CalibratedStrip& calibStrip) const override;
     virtual StatusCode distToTime(const Muon::MMPrepData* prepData, const Amg::Vector3D& globalPos, const std::vector<double>& driftDistances, std::vector<double>& driftTimes) const override;
-- 
GitLab