From 39f33745d3214cb52c23839dc83fee34d44c9062 Mon Sep 17 00:00:00 2001
From: myliu <201916234@mail.sdu.edu.cn>
Date: Wed, 28 Oct 2020 15:45:41 +0800
Subject: [PATCH] Fixed the phiFromXY() function to subtract a pai

---
 Detector/DetDriftChamber/compact/det.xml        |  2 +-
 .../src/driftchamber/DriftChamber.cpp           |  5 +++--
 .../DetSegmentation/GridDriftChamber.h          | 17 +++++++++--------
 .../DetSegmentation/src/GridDriftChamber.cpp    | 10 +++++++---
 4 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/Detector/DetDriftChamber/compact/det.xml b/Detector/DetDriftChamber/compact/det.xml
index b791565d..1ff377f0 100644
--- a/Detector/DetDriftChamber/compact/det.xml
+++ b/Detector/DetDriftChamber/compact/det.xml
@@ -59,7 +59,7 @@
 
   <readouts>
     <readout name="DriftChamberHitsCollection">
-      <segmentation type="GridDriftChamber" cell_size="10*mm" offset_phi="0." epsilon0="Epsilon" detector_length="SDT_half_length" identifier_phi="cellID" />
+      <segmentation type="GridDriftChamber" cell_size="10*mm" offset_phi="0." epsilon0="Epsilon" detector_length="SDT_length" identifier_phi="cellID" />
 
       <id>system:8,chamber:1,layer:8,cellID:16</id>
     </readout>
diff --git a/Detector/DetDriftChamber/src/driftchamber/DriftChamber.cpp b/Detector/DetDriftChamber/src/driftchamber/DriftChamber.cpp
index 07b95b98..bc0c3e38 100644
--- a/Detector/DetDriftChamber/src/driftchamber/DriftChamber.cpp
+++ b/Detector/DetDriftChamber/src/driftchamber/DriftChamber.cpp
@@ -114,11 +114,12 @@ static dd4hep::Ref_t create_detector(dd4hep::Detector& theDetector,
         double rmid = delta_a_func(rmin,rmax);
         double ilayer_cir = 2 * M_PI * rmid;
         double ncell = ilayer_cir / chamber_layer_width;
-        int ncell_layer = ceil(ncell);
+        int ncell_layer = floor(ncell);
         int numWire = ncell_layer;
         double layer_Phi = 2*M_PI / ncell_layer;
+
         if(layer_id %2 ==0){ offset = 0.; }
-        else { offset = 0.5 * layer_Phi; }
+        else { offset = 0.;}//0.5 * layer_Phi; }
 
         DCHseg->setGeomParams(layer_id, layer_Phi, rmid, epsilon, offset);
         DCHseg->setWiresInLayer(layer_id, numWire);
diff --git a/Detector/DetSegmentation/DetSegmentation/GridDriftChamber.h b/Detector/DetSegmentation/DetSegmentation/GridDriftChamber.h
index fc01bf11..e5d1461b 100644
--- a/Detector/DetSegmentation/DetSegmentation/GridDriftChamber.h
+++ b/Detector/DetSegmentation/DetSegmentation/GridDriftChamber.h
@@ -46,6 +46,7 @@ public:
                         const VolumeID& aVolumeID) const;
   virtual double distanceTrackWire(const CellID& cID, const TVector3& hit_start, const TVector3& hit_end) const;
 
+  double phi(const CellID& cID) const;
   inline double cell_Size() const { return m_cellSize; }
   inline double offset_phi() const { return m_offsetPhi; }
   inline double epsilon0() const { return m_epsilon0; }
@@ -54,10 +55,10 @@ public:
   // Setters
 
   inline double phiFromXY(const Vector3D& aposition) const {
-    return std::atan2(aposition.Y, aposition.X) + M_PI ;
+    return std::atan2(aposition.Y, aposition.X) ;
   }
 
-  inline void setGeomParams(int layer, double layerphi, double R, double eps, double offset ) {
+  inline void setGeomParams(int layer, double layerphi, double R, double eps, double offset) {
     // layer_params[layer] = {layerphi,R,eps};
     layer_params.insert(std::pair<int,LAYER>(layer,LAYER(layerphi,R,eps,offset)));
    }
@@ -72,9 +73,9 @@ public:
 //    else { phi0 = 0.5 * _currentLayerphi; }
       double phi0 = m_offset;
 
-      auto phi_start = _currentLayerphi * i + phi0;
+      auto phi_start = _currentLayerphi * (i + 0.5) + phi0;
       if(phi_start > 2 * M_PI) { phi_start = phi_start - 2 * M_PI; }
-      auto phi_end = phi_start + _currentLayerphi;
+      auto phi_end = phi_start;// + _currentLayerphi;
 
       TVector3 Wstart = returnWirePosition(phi_start, 1);
       TVector3 Wend = returnWirePosition(phi_end, -1);
@@ -117,14 +118,14 @@ public:
     m_offset = offset;
  }
 
-  inline double returnAlpha() const {
-    double alpha = 2 * std::asin(m_detectorLength * std::tan(m_epsilon0)/(2 * _currentRadius));
-    return alpha;
+ inline double returnAlpha() const {
+   double alpha = 2 * std::asin(m_detectorLength * std::tan(m_epsilon0)/(2 * _currentRadius));
+   return alpha;
  }
 
 protected:
   /* *** nalipour *** */
-  double phi(const CellID& cID) const;
+//  double phi(const CellID& cID) const;
   std::map<int,LAYER> layer_params; // <layer, {layerphi, R, eps, offset}>
   std::map<int, std::vector<std::pair<TVector3, TVector3> >> m_wiresPositions; // < layer, vec<WireMidpoint, WireDirection> >
 
diff --git a/Detector/DetSegmentation/src/GridDriftChamber.cpp b/Detector/DetSegmentation/src/GridDriftChamber.cpp
index 57a6d7eb..09432fb8 100644
--- a/Detector/DetSegmentation/src/GridDriftChamber.cpp
+++ b/Detector/DetSegmentation/src/GridDriftChamber.cpp
@@ -58,9 +58,11 @@ CellID GridDriftChamber::cellID(const Vector3D& /*localPosition*/, const Vector3
     else {
       _lphi = (int) ((phi_hit - offsetphi + 2 * M_PI)/ _currentLayerphi);
     }
+// }
   int lphi = _lphi;
   _decoder->set(cID, m_phiID, lphi);
 
+
 //std::cout << "#######################################: " 
 //          <<  " offset : " << m_offset
 //          << " offsetphi: " << offsetphi
@@ -74,7 +76,7 @@ CellID GridDriftChamber::cellID(const Vector3D& /*localPosition*/, const Vector3
 
 double GridDriftChamber::phi(const CellID& cID) const {
   CellID phiValue = _decoder->get(cID, m_phiID);
-  return binToPosition(phiValue, _currentLayerphi, m_offsetPhi);
+  return (_currentLayerphi * 0.5) + binToPosition(phiValue, _currentLayerphi, m_offsetPhi);
 }
 
 double GridDriftChamber::distanceTrackWire(const CellID& cID, const TVector3& hit_start,
@@ -93,8 +95,10 @@ double GridDriftChamber::distanceTrackWire(const CellID& cID, const TVector3& hi
   TVector3 b = Wend - Wstart;
   TVector3 c = Wstart - hit_start;
 
-  double num = std::abs(c.Dot(a.Cross(b)));
-  double denum = (a.Cross(b)).Mag();
+//  double num = std::abs(c.Dot(a.Cross(b)));
+//  double denum = (a.Cross(b)).Mag();
+  double num = (b.Cross(c)).Mag();
+  double denum = b.Mag();
 
   double DCA = 0;
 
-- 
GitLab