From b066193e59be15f716aa8562e2a7989c33f5c606 Mon Sep 17 00:00:00 2001
From: myliu <201916234@mail.sdu.edu.cn>
Date: Mon, 8 Mar 2021 14:47:12 +0800
Subject: [PATCH] Use segmentation to divide layer

---
 Detector/DetDriftChamber/compact/det.xml      | 24 +++++-----
 .../src/driftchamber/DriftChamber.cpp         | 35 ++++++---------
 .../DetSegmentation/GridDriftChamber.h        | 16 ++++---
 .../DetSegmentation/src/GridDriftChamber.cpp  | 45 +++++++++----------
 4 files changed, 60 insertions(+), 60 deletions(-)

diff --git a/Detector/DetDriftChamber/compact/det.xml b/Detector/DetDriftChamber/compact/det.xml
index ece9ae23..bf4e197d 100644
--- a/Detector/DetDriftChamber/compact/det.xml
+++ b/Detector/DetDriftChamber/compact/det.xml
@@ -35,12 +35,14 @@
     <constant name="SDT_length" value="SDT_half_length*2"/>
     <constant name="DC_length" value="SDT_length-DC_Endcap_dz*2"/>
 
-    <constant name="SDT_inner_chamber_radius_min" value="235*mm"/>
+    <constant name="DC_safe_distance" value="0.02*mm"/>
+
+    <constant name="SDT_inner_chamber_radius_min" value="235*mm-DC_safe_distance"/>
     <constant name="SDT_inner_chamber_radius_max" value="906*mm"/>
     <constant name="SDT_inner_chamber_length" value="DC_length"/>
 
-    <constant name="SDT_outer_chamber_radius_min" value="1085*mm"/>
-    <constant name="SDT_outer_chamber_radius_max" value="1715*mm"/>
+    <constant name="SDT_outer_chamber_radius_min" value="1085*mm-DC_safe_distance"/>
+    <constant name="SDT_outer_chamber_radius_max" value="1715*mm+DC_safe_distance"/>
     <constant name="SDT_outer_chamber_length" value="DC_length"/>
 
     <constant name="SDT_inner_chamber_layer_number" value="67"/>
@@ -48,14 +50,14 @@
     <constant name="SDT_chamber_layer_width" value="10*mm"/>
     <constant name="Epsilon" value="0*deg"/>
 
-    <constant name="SDT_inner_chamber_inner_wall_radius_min" value="234.8*mm"/>
-    <constant name="SDT_inner_chamber_inner_wall_radius_max" value="235*mm"/>
+    <constant name="SDT_inner_chamber_inner_wall_radius_min" value="234.6*mm"/>
+    <constant name="SDT_inner_chamber_inner_wall_radius_max" value="234.8*mm"/>
     <constant name="SDT_inner_chamber_outer_wall_radius_min" value="906*mm"/>
     <constant name="SDT_inner_chamber_outer_wall_radius_max" value="908.8*mm"/>
-    <constant name="SDT_outer_chamber_inner_wall_radius_min" value="1084.8*mm"/>
-    <constant name="SDT_outer_chamber_inner_wall_radius_max" value="1085*mm"/>
-    <constant name="SDT_outer_chamber_outer_wall_radius_min" value="1715*mm"/>
-    <constant name="SDT_outer_chamber_outer_wall_radius_max" value="1717.8*mm"/>
+    <constant name="SDT_outer_chamber_inner_wall_radius_min" value="1084*mm"/>
+    <constant name="SDT_outer_chamber_inner_wall_radius_max" value="1084.2*mm"/>
+    <constant name="SDT_outer_chamber_outer_wall_radius_min" value="1716*mm"/>
+    <constant name="SDT_outer_chamber_outer_wall_radius_max" value="1718.8*mm"/>
 
     <constant name="DC_Endcap_rmin" value="SDT_radius_min"/>
     <constant name="DC_Endcap_rmax" value="SDT_radius_max"/>
@@ -86,7 +88,7 @@
       <envelope vis="SeeThrough">
         <shape type="BooleanShape" operation="Union" material="Air">
           <shape type="Tube" rmin="SDT_radius_min" rmax="909*mm" dz="SDT_half_length" />
-          <shape type="Tube" rmin="1084.8*mm" rmax="SDT_radius_max" dz="SDT_half_length" />
+          <shape type="Tube" rmin="SDT_outer_chamber_inner_wall_radius_min" rmax="SDT_radius_max" dz="SDT_half_length" />
         </shape>
       </envelope>
 
@@ -108,7 +110,7 @@
 
   <readouts>
     <readout name="DriftChamberHitsCollection">
-      <segmentation type="GridDriftChamber" cell_size="10*mm" epsilon0="Epsilon" detector_length="DC_length" identifier_phi="cellID" />
+      <segmentation type="GridDriftChamber" cell_size="10*mm" epsilon0="Epsilon" detector_length="DC_length" identifier_phi="cellID" DC_inner_rmin="SDT_inner_chamber_radius_min" DC_inner_rmax="SDT_inner_chamber_radius_max" DC_outer_rmin="SDT_outer_chamber_radius_min" DC_outer_rmax="SDT_outer_chamber_radius_max" layerID="layer"/>
 
       <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 7842b474..411edaa8 100644
--- a/Detector/DetDriftChamber/src/driftchamber/DriftChamber.cpp
+++ b/Detector/DetDriftChamber/src/driftchamber/DriftChamber.cpp
@@ -41,8 +41,6 @@ static dd4hep::Ref_t create_detector(dd4hep::Detector& theDetector,
     dd4hep::SensitiveDetector sd = sens;
 
     // - global
-//    double chamber_radius_min = theDetector.constant<double>("SDT_radius_min");
-//    double chamber_radius_max = theDetector.constant<double>("SDT_radius_max");
     double chamber_length     = theDetector.constant<double>("DC_length");
 
     // - inner chamber
@@ -83,17 +81,18 @@ static dd4hep::Ref_t create_detector(dd4hep::Detector& theDetector,
 
 
     dd4hep::Material det_mat(theDetector.material("Air"));
+    dd4hep::Material chamber_mat(theDetector.material("GasHe_90Isob_10"));
 
     // - global
     Assembly det_vol( det_name+"_assembly" ) ;
 
     // - inner
     dd4hep::Tube det_inner_chamber_solid(inner_chamber_radius_min, inner_chamber_radius_max, inner_chamber_length*0.5);
-    dd4hep::Volume det_inner_chamber_vol(det_name+"_inner_chamber_vol", det_inner_chamber_solid, det_mat);
+    dd4hep::Volume det_inner_chamber_vol(det_name+"_inner_chamber_vol", det_inner_chamber_solid, chamber_mat);
 
     // - outer
     dd4hep::Tube det_outer_chamber_solid(outer_chamber_radius_min, outer_chamber_radius_max, outer_chamber_length*0.5);
-    dd4hep::Volume det_outer_chamber_vol(det_name+"_outer_chamber_vol", det_outer_chamber_solid, det_mat);
+    dd4hep::Volume det_outer_chamber_vol(det_name+"_outer_chamber_vol", det_outer_chamber_solid, chamber_mat);
 
     // - wall
     double inner_chamber_inner_wall_rmin = theDetector.constant<double>("SDT_inner_chamber_inner_wall_radius_min");
@@ -169,12 +168,12 @@ static dd4hep::Ref_t create_detector(dd4hep::Detector& theDetector,
         dd4hep::Material layer_mat(theDetector.material("GasHe_90Isob_10"));
         if(inner_chamber_enabled && (layer_id < inner_chamber_layer_number)) {
            current_vol_ptr = &det_inner_chamber_vol;
-           rmin = inner_chamber_radius_min+(layer_id*chamber_layer_width);
+           rmin = inner_chamber_radius_min+safe_diatance+(layer_id*chamber_layer_width);
            rmax = rmin+chamber_layer_width;
            layer_name = det_name+"_inner_chamber_vol"+_toString(layer_id,"_layer%d");
         } else if(outer_chamber_enabled && (layer_id > (inner_chamber_layer_number-1))) {
            current_vol_ptr = &det_outer_chamber_vol;
-           rmin = outer_chamber_radius_min+((layer_id-inner_chamber_layer_number)*chamber_layer_width);
+           rmin = outer_chamber_radius_min+safe_diatance+((layer_id-inner_chamber_layer_number)*chamber_layer_width);
            rmax = rmin+chamber_layer_width;
            layer_name = det_name+"_outer_chamber_vol"+_toString(layer_id,"_layer%d");
         } else continue;
@@ -193,10 +192,6 @@ static dd4hep::Ref_t create_detector(dd4hep::Detector& theDetector,
         DCHseg->setGeomParams(layer_id, layer_Phi, rmid, epsilon, offset);
         DCHseg->setWiresInLayer(layer_id, numWire);
 
-        dd4hep::Tube layer_solid(rmin,rmax,chamber_length*0.5);
-        dd4hep::Volume layer_vol(layer_name,layer_solid,layer_mat);
-        layer_vol.setAttributes(theDetector,x_det.regionStr(),x_det.limitsStr(),x_det.visStr());
-
         // - wire vol
         //phi <-------------------> -phi
         //    |   F8    F7   F6   F5|  Only on the outermost layer.
@@ -210,29 +205,23 @@ static dd4hep::Ref_t create_detector(dd4hep::Detector& theDetector,
             double wire_phi = (icell+0.5)*layer_Phi + offset;
             // - signal wire
             dd4hep::Transform3D transform_module(dd4hep::Rotation3D(),dd4hep::Position(rmid*std::cos(wire_phi),rmid*std::sin(wire_phi),0.));
-            dd4hep::PlacedVolume module_phy = layer_vol.placeVolume(module_vol,transform_module);
+            dd4hep::PlacedVolume module_phy = (*current_vol_ptr).placeVolume(module_vol,transform_module);
            // - Field wire
             dd4hep::PlacedVolume Module_phy;
             double radius[9] = {rmid-chamber_layer_width*0.5+safe_diatance,rmid-chamber_layer_width*0.5+safe_diatance,rmid-chamber_layer_width*0.5+safe_diatance,rmid-chamber_layer_width*0.5+safe_diatance,rmid,rmid+chamber_layer_width*0.5-safe_diatance,rmid+chamber_layer_width*0.5-safe_diatance,rmid+chamber_layer_width*0.5-safe_diatance,rmid+chamber_layer_width*0.5-safe_diatance};
             double phi[9] = {wire_phi+layer_Phi*0.25,wire_phi,wire_phi-layer_Phi*0.25,wire_phi-layer_Phi*0.5,wire_phi-layer_Phi*0.5,wire_phi-layer_Phi*0.5,wire_phi-layer_Phi*0.25,wire_phi,wire_phi+layer_Phi*0.25};
             int num = 5;
-            if(layer_id==(inner_chamber_layer_number-1)||layer_id==(outer_chamber_layer_number-1)) { num = 9; }
+            if(layer_id==(inner_chamber_layer_number-1)||layer_id==(inner_chamber_layer_number+outer_chamber_layer_number-1)) { num = 9; }
             for(int i=0; i<num ; i++) {
                 dd4hep::Position tr3D = Position(radius[i]*std::cos(phi[i]),radius[i]*std::sin(phi[i]),0.);
+
                 dd4hep::Transform3D transform_Module(dd4hep::Rotation3D(),tr3D);
-                Module_phy = layer_vol.placeVolume(Module_vol,transform_Module);
+                Module_phy = (*current_vol_ptr).placeVolume(Module_vol,transform_Module);
             }
         }
 //  }
 
-        dd4hep::Transform3D transform_layer(dd4hep::Rotation3D(),dd4hep::Position(0.,0.,0.));
-        dd4hep::PlacedVolume layer_phy = (*current_vol_ptr).placeVolume(layer_vol, transform_layer);
-        layer_phy.addPhysVolID("layer",layer_id);
-
-        //Set drift chamber layers to sensitive detector
-        layer_vol.setSensitiveDetector(sens);
-        sd.setType("tracker");
-      }
+    }
 
     // - place in det
     // inner
@@ -243,6 +232,8 @@ static dd4hep::Ref_t create_detector(dd4hep::Detector& theDetector,
                  transform_inner_chamber);
 
          det_inner_chamber_phy.addPhysVolID("chamber", 0);
+         det_inner_chamber_vol.setSensitiveDetector(sens);
+         sd.setType("tracker");
     }
     // outer
     dd4hep::Transform3D transform_outer_chamber(dd4hep::Rotation3D(),
@@ -252,6 +243,8 @@ static dd4hep::Ref_t create_detector(dd4hep::Detector& theDetector,
                transform_inner_chamber);
 
        det_outer_chamber_phy.addPhysVolID("chamber", 1);
+       det_outer_chamber_vol.setSensitiveDetector(sens);
+       sd.setType("tracker");
     }
     // - place in world
     dd4hep::Transform3D transform(dd4hep::Rotation3D(),
diff --git a/Detector/DetSegmentation/include/DetSegmentation/GridDriftChamber.h b/Detector/DetSegmentation/include/DetSegmentation/GridDriftChamber.h
index 82a4e8f0..28ad791c 100644
--- a/Detector/DetSegmentation/include/DetSegmentation/GridDriftChamber.h
+++ b/Detector/DetSegmentation/include/DetSegmentation/GridDriftChamber.h
@@ -42,6 +42,7 @@ public:
   virtual ~GridDriftChamber() = default;
 
   virtual Vector3D position(const CellID& aCellID) const;
+//  virtual int LayerID(const Vector3D& aGlobalPosition) const;
   virtual CellID cellID(const Vector3D& aLocalPosition, const Vector3D& aGlobalPosition,
                         const VolumeID& aVolumeID) const;
   virtual double distanceTrackWire(const CellID& cID, const TVector3& hit_start, const TVector3& hit_end) const;
@@ -51,7 +52,12 @@ public:
   inline double cell_Size() const { return m_cellSize; }
   inline double epsilon0() const { return m_epsilon0; }
   inline double detectorLength() const { return m_detectorLength; }
+  inline double DC_inner_rmin() const { return m_DC_inner_rmin; }
+  inline double DC_inner_rmax() const { return m_DC_inner_rmax; }
+  inline double DC_outer_rmin() const { return m_DC_outer_rmin; }
+  inline double DC_outer_rmax() const { return m_DC_outer_rmax; }
   inline const std::string& fieldNamePhi() const { return m_phiID; }
+  inline const std::string& Layerid() const { return layer_id; }
   // Setters
 
   inline double phiFromXY(const Vector3D& aposition) const {
@@ -113,11 +119,6 @@ public:
     m_offset = offset;
  }
 
-// inline double returnAlpha() const {
-//   double alpha = 2 * std::asin(m_detectorLength * std::tan(m_epsilon0)/(2 * _currentRadius));
-//   return alpha;
-// }
-
 protected:
 
   double phi(const CellID& cID) const;
@@ -140,7 +141,12 @@ protected:
   double m_cellSize;
   double m_epsilon0;
   double m_detectorLength;
+  double m_DC_inner_rmin;
+  double m_DC_inner_rmax;
+  double m_DC_outer_rmin;
+  double m_DC_outer_rmax;
   std::string m_phiID;
+  std::string layer_id;
 
   // Current parameters of the layer: sizePhi
   mutable double _currentLayerphi;
diff --git a/Detector/DetSegmentation/src/GridDriftChamber.cpp b/Detector/DetSegmentation/src/GridDriftChamber.cpp
index d4facaae..6cff5423 100644
--- a/Detector/DetSegmentation/src/GridDriftChamber.cpp
+++ b/Detector/DetSegmentation/src/GridDriftChamber.cpp
@@ -13,6 +13,11 @@ GridDriftChamber::GridDriftChamber(const std::string& cellEncoding) : Segmentati
   registerParameter("cell_size", "cell size", m_cellSize, 0., SegmentationParameter::LengthUnit);
   registerParameter("detector_length", "Length of the wire", m_detectorLength, 1., SegmentationParameter::LengthUnit);
   registerIdentifier("identifier_phi", "Cell ID identifier for phi", m_phiID, "cellID");
+  registerIdentifier("layerID", "layer id", layer_id, "layer");
+  registerParameter("DC_inner_rmin", "DC_inner_rmin", m_DC_inner_rmin, 0., SegmentationParameter::LengthUnit);
+  registerParameter("DC_inner_rmax", "DC_inner_rmax", m_DC_inner_rmax, 0., SegmentationParameter::LengthUnit);
+  registerParameter("DC_outer_rmin", "DC_outer_rmin", m_DC_outer_rmin, 0., SegmentationParameter::LengthUnit);
+  registerParameter("DC_outer_rmax", "DC_outer_rmax", m_DC_outer_rmax, 0., SegmentationParameter::LengthUnit);
 }
 
 GridDriftChamber::GridDriftChamber(const BitFieldCoder* decoder) : Segmentation(decoder) {
@@ -24,6 +29,11 @@ GridDriftChamber::GridDriftChamber(const BitFieldCoder* decoder) : Segmentation(
   registerParameter("epsilon0", "epsilon", m_epsilon0, 0., SegmentationParameter::AngleUnit, true);
   registerParameter("detector_length", "Length of the wire", m_detectorLength, 1., SegmentationParameter::LengthUnit);
   registerIdentifier("identifier_phi", "Cell ID identifier for phi", m_phiID, "cellID");
+  registerIdentifier("layerID", "layer id", layer_id, "layer");
+  registerParameter("DC_inner_rmin", "DC_inner_rmin", m_DC_inner_rmin, 0., SegmentationParameter::LengthUnit);
+  registerParameter("DC_inner_rmax", "DC_inner_rmax", m_DC_inner_rmax, 0., SegmentationParameter::LengthUnit);
+  registerParameter("DC_outer_rmin", "DC_outer_rmin", m_DC_outer_rmin, 0., SegmentationParameter::LengthUnit);
+  registerParameter("DC_outer_rmax", "DC_outer_rmax", m_DC_outer_rmax, 0., SegmentationParameter::LengthUnit);
 }
 
 Vector3D GridDriftChamber::position(const CellID& /*cID*/) const {
@@ -31,17 +41,24 @@ Vector3D GridDriftChamber::position(const CellID& /*cID*/) const {
   return cellPosition;
 }
 
-
 CellID GridDriftChamber::cellID(const Vector3D& /*localPosition*/, const Vector3D& globalPosition,
                                 const VolumeID& vID) const {
 
   CellID cID = vID;
-  unsigned int layerID = _decoder->get(vID, "layer");
-  updateParams(layerID);
 
-  double phi_hit = phiFromXY(globalPosition);
   double posx = globalPosition.X;
   double posy = globalPosition.Y;
+  double radius = sqrt(posx*posx+posy*posy);
+
+  int layerid;
+  if(radius > m_DC_inner_rmin && radius < m_DC_inner_rmax) layerid = floor(radius-m_DC_inner_rmin);
+  else if (radius > m_DC_outer_rmin && radius < m_DC_outer_rmax)
+           layerid = floor(radius-m_DC_outer_rmin)+floor(m_DC_inner_rmax-m_DC_inner_rmin);
+  else return -1;
+
+  updateParams(layerid);
+
+  double phi_hit = phiFromXY(globalPosition);
   double offsetphi= m_offset;
   int _lphi;
 
@@ -53,17 +70,9 @@ CellID GridDriftChamber::cellID(const Vector3D& /*localPosition*/, const Vector3
   }
 
   int lphi = _lphi;
+  _decoder->set(cID, layer_id, layerid);
   _decoder->set(cID, m_phiID, lphi);
 
-
-// std::cout << "#######################################: " 
-//           <<  " offset : " << m_offset
-//           << " offsetphi: " << offsetphi
-//           << " layerID: " << layerID
-//           << " r: " << _currentRadius
-//           << " layerphi: " << _currentLayerphi
-//           << std::endl;
-
   return cID;
 }
 
@@ -91,14 +100,6 @@ void GridDriftChamber::cellposition(const CellID& cID, TVector3& Wstart,
 double GridDriftChamber::distanceTrackWire(const CellID& cID, const TVector3& hit_start,
                                            const TVector3& hit_end) const {
 
-//  auto layerIndex = _decoder->get(cID, "layer");
-//  updateParams(layerIndex);
-//
-//  double phi_start = phi(cID);
-//  double phi_end = phi_start + returnAlpha();
-
-//  TVector3 Wstart = returnWirePosition(phi_start, -1); // The default centimeter unit in DD4hep
-//  TVector3 Wend = returnWirePosition(phi_end, 1);   // The default centimeter unit in DD4hep
   TVector3 Wstart = {0,0,0};
   TVector3 Wend = {0,0,0};
   cellposition(cID,Wstart,Wend);
@@ -109,8 +110,6 @@ double GridDriftChamber::distanceTrackWire(const CellID& cID, const TVector3& hi
 
   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