From 90c743b89776e8a261ade1ae97a96dde0a5607ca Mon Sep 17 00:00:00 2001
From: myliu <201916234@mail.sdu.edu.cn>
Date: Sun, 27 Sep 2020 09:09:00 +0800
Subject: [PATCH] Add segmentation and get cellIDD

---
 Detector/DetDriftChamber/CMakeLists.txt       |   3 +-
 Detector/DetDriftChamber/compact/det.xml      |   4 +-
 .../src/driftchamber/DriftChamber.cpp         |   2 +
 .../DetSegmentation/.GridDriftChamber.h.swp   | Bin 0 -> 16384 bytes
 Detector/DetSegmentation/CMakeLists.txt       |  33 +++++++
 .../DetSegmentation/GridDriftChamber.h        |  90 ++++++++++++++++++
 .../DetSegmentation/src/GridDriftChamber.cpp  |  55 +++++++++++
 .../src/plugins/SegmentationFactories.cpp     |  22 +++++
 8 files changed, 206 insertions(+), 3 deletions(-)
 create mode 100644 Detector/DetSegmentation/.GridDriftChamber.h.swp
 create mode 100644 Detector/DetSegmentation/CMakeLists.txt
 create mode 100644 Detector/DetSegmentation/DetSegmentation/GridDriftChamber.h
 create mode 100644 Detector/DetSegmentation/src/GridDriftChamber.cpp
 create mode 100644 Detector/DetSegmentation/src/plugins/SegmentationFactories.cpp

diff --git a/Detector/DetDriftChamber/CMakeLists.txt b/Detector/DetDriftChamber/CMakeLists.txt
index 4a95aeec..b6802769 100644
--- a/Detector/DetDriftChamber/CMakeLists.txt
+++ b/Detector/DetDriftChamber/CMakeLists.txt
@@ -4,7 +4,8 @@
 ################################################################################
 gaudi_subdir(DetDriftChamber v0r0)
 
-gaudi_depends_on_subdirs(GaudiKernel)
+gaudi_depends_on_subdirs(Detector/DetSegmentation)
+#gaudi_depends_on_subdirs(GaudiKernel)
 
 
 find_package(DD4hep COMPONENTS DDRec DDG4 DDParsers REQUIRED)
diff --git a/Detector/DetDriftChamber/compact/det.xml b/Detector/DetDriftChamber/compact/det.xml
index 3920c12a..84a1ec7b 100644
--- a/Detector/DetDriftChamber/compact/det.xml
+++ b/Detector/DetDriftChamber/compact/det.xml
@@ -57,9 +57,9 @@
 
   <readouts>
     <readout name="DriftChamberHitsCollection">
-      <segmentation type="NoSegmentation"/>
+      <segmentation type="GridDriftChamber" delta_phi="8*deg" identifier_phi="phi"/>
 
-      <id>system:8,chamber:1,layer:8</id>
+      <id>system:8,chamber:1,layer:8,phi:16</id>
     </readout>
   </readouts>
 
diff --git a/Detector/DetDriftChamber/src/driftchamber/DriftChamber.cpp b/Detector/DetDriftChamber/src/driftchamber/DriftChamber.cpp
index 5ac10182..c7101215 100644
--- a/Detector/DetDriftChamber/src/driftchamber/DriftChamber.cpp
+++ b/Detector/DetDriftChamber/src/driftchamber/DriftChamber.cpp
@@ -12,6 +12,7 @@
 #include "XML/Utilities.h"
 #include "DDRec/DetectorData.h"
 #include "DDSegmentation/Segmentation.h"
+#include "DetSegmentation/GridDriftChamber.h"
 
 using namespace dd4hep;
 using namespace dd4hep::detail;
@@ -74,6 +75,7 @@ static dd4hep::Ref_t create_detector(dd4hep::Detector& theDetector,
     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);
 
+
     // - layer
     for(int layer_id=0; layer_id<(inner_chamber_layer_number+outer_chamber_layer_number-1);layer_id++) {
       double rmin,rmax;
diff --git a/Detector/DetSegmentation/.GridDriftChamber.h.swp b/Detector/DetSegmentation/.GridDriftChamber.h.swp
new file mode 100644
index 0000000000000000000000000000000000000000..51f757f081f232cc857fcd0ed4b1246ecaf06edb
GIT binary patch
literal 16384
zcmeHNO^hQ)6|N<OWPp$bZb+bFR~p$fUVA-bvM{q7GHcIHEM}L<c(StzTAZ5h^19e|
zcdLJv%tk}F0O9~A4uF&s5rHT-lqgb;2q6I{P;x*>APyYhzy%?JK=8e)YP;?6kiA4m
zP(Av_)%9MzdiA|m<?eDFoWI_^$}iMg4A*JK{`9S&|H2dOr|HKS8yyBJr~0?Kf@jmf
zyU={3u7)zM3$N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R=3Wre;8eL6GFJ#%Qh
zPHK&%rQ;r!kC~Tk(6RHW``LKbhsTW3$@8-+^n%=%{Gy7G4N{CAE!BA=kqt`~XNoCU
zAD$_-Ju}TpqwI*$M2zUcth2e-+uXXkd2MfFulw0+{jJ?@r?cC=ve$lm<Lc$j-Tvd~
zd=NtWUR3G*{~vfaZ{dBO-v9ARtgoNo{%gQ(U<eEVA0WL|z+H|3$ADwNG2j?*3^)cH
z1C9a5fMdWh;28K{XW%UM4)N<Ael>ghcX9gt?uh<q!)TOef_^KqyME)H={d4u^WSXX
luG97hF6<WB9|bsVlJi7w@3Lc-i`J!|4q#%oHFl=izX4TK8u<VK

literal 0
HcmV?d00001

diff --git a/Detector/DetSegmentation/CMakeLists.txt b/Detector/DetSegmentation/CMakeLists.txt
new file mode 100644
index 00000000..f6b2611e
--- /dev/null
+++ b/Detector/DetSegmentation/CMakeLists.txt
@@ -0,0 +1,33 @@
+#################################################################################
+##Package : DetSegmentation
+#################################################################################
+gaudi_subdir(DetSegmentation v1r0)
+
+gaudi_depends_on_subdirs(GaudiKernel)
+
+find_package(DD4hep COMPONENTS DDRec DDG4 DDParsers REQUIRED)
+set(CMAKE_MODULE_PATH  ${CMAKE_MODULE_PATH}  ${DD4hep_ROOT}/cmake )
+
+include(DD4hep)
+
+find_package(ROOT COMPONENTS MathCore Physics GenVector Geom REQUIRED)
+
+gaudi_add_library(DetSegmentation
+                 src/*.cpp
+                 INCLUDE_DIRS DD4hep ROOT
+                 LINK_LIBRARIES GaudiKernel DD4hep ROOT ${DD4hep_COMPONENT_LIBRARIES}
+                 PUBLIC_HEADERS DetSegmentation)
+
+gaudi_add_module(DetSegmentationPlugin
+                 src/plugins/*.cpp
+                 INCLUDE_DIRS DD4hep ROOT
+                 LINK_LIBRARIES GaudiKernel DD4hep ROOT ${DD4hep_COMPONENT_LIBRARIES} DetSegmentation)
+
+set(LIBRARY_OUTPUT_PATH ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
+message(STATUS "LIBRARY_OUTPUT_PATH -> ${LIBRARY_OUTPUT_PATH}")
+dd4hep_generate_rootmap(DetSegmentationPlugin)
+
+include(CTest)
+gaudi_add_test(TestSegmentationPhiEta
+               WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
+               FRAMEWORK tests/options/phiEtaSegmentation.py)
diff --git a/Detector/DetSegmentation/DetSegmentation/GridDriftChamber.h b/Detector/DetSegmentation/DetSegmentation/GridDriftChamber.h
new file mode 100644
index 00000000..379ad4a1
--- /dev/null
+++ b/Detector/DetSegmentation/DetSegmentation/GridDriftChamber.h
@@ -0,0 +1,90 @@
+#ifndef DETSEGMENTATION_GRIDDRIFTCHAMBER_H
+#define DETSEGMENTATION_GRIDDRIFTCHAMBER_H
+
+#include "DDSegmentation/Segmentation.h"
+
+#include "TVector3.h"
+#include <cmath>
+#include <iostream>
+
+/** GridDriftChamber Detector/DetSegmentation/DetSegmentation/GridDriftChamber.h GridDriftChamber.h
+ *
+ *  Segmentation for drift chamber.
+ *
+ *  @author    nalipour
+ */
+
+namespace dd4hep {
+namespace DDSegmentation {
+class GridDriftChamber : public Segmentation {
+public:
+  /// default constructor using an arbitrary type
+  GridDriftChamber(const std::string& aCellEncoding);
+  /// Default constructor used by derived classes passing an existing decoder
+  GridDriftChamber(const BitFieldCoder* decoder);
+  /// destructor
+  virtual ~GridDriftChamber() = default;
+
+  virtual Vector3D position(const CellID& aCellID) const;
+  virtual CellID cellID(const Vector3D& aLocalPosition, const Vector3D& aGlobalPosition,
+                        const VolumeID& aVolumeID) const;
+
+//  inline double innerRadius() const { return m_innerRadius; }
+//  inline double detectorLength() const { return m_detectorLength; }
+  inline double offsetPhi() const { return m_offsetPhi; }
+  inline double delta_phi() const{ return m_delta_phi; }
+  inline const std::string& fieldNamePhi() const { return m_phiID; }
+  // Setters
+
+//  inline void setGeomParams(int layer, double sizePhi) {
+//    layer_params[layer] = {sizePhi};
+// }
+
+//  void updateParams(int layer) const {
+//    auto it_end = layer_params.cend();
+//    --it_end;
+//    double size = it_end->second[0];
+//    double radius = it_end->second[1];
+//    double eps = it_end->second[2];
+//
+//    auto map_it = layer_params.find(layer);
+//    if (map_it != layer_params.cend()) {
+//      size = map_it->second[0];
+//      radius = map_it->second[1];
+//      eps = map_it->second[2];
+//    }
+//
+//    _currentGridSizePhi = size;
+//    _currentRadius = radius;
+//    m_epsilon = eps;
+//  }
+
+  inline double phiFromXY(const Vector3D& aposition) const {
+    return std::atan2(aposition.Y, aposition.X) + M_PI ;
+  }
+
+//  inline int returnLayer(double x, double y) const {
+//  // Hit R position
+//    double R = std::sqrt(x * x + y * y);
+//  // Layer
+//    int layer = int((R - m_innerRadius) / m_cellSize);
+//    return layer;
+//  }
+
+protected:
+  /* *** nalipour *** */
+  double phi(const CellID& cID) const;
+
+
+  double m_offsetPhi;
+  double m_delta_phi;
+  std::string m_phiID;
+
+  // Current parameters of the layer: sizePhi
+//  mutable double _currentGridSizePhi;  // current size Phi
+//  mutable double _currentRadius;       // current size radius
+//  mutable double m_epsilon;
+};
+}
+}
+#endif /* DETSEGMENTATION_GRIDDRIFTCHAMBER_H */
diff --git a/Detector/DetSegmentation/src/GridDriftChamber.cpp b/Detector/DetSegmentation/src/GridDriftChamber.cpp
new file mode 100644
index 00000000..4b1ed216
--- /dev/null
+++ b/Detector/DetSegmentation/src/GridDriftChamber.cpp
@@ -0,0 +1,55 @@
+#include "DetSegmentation/GridDriftChamber.h"
+
+namespace dd4hep {
+namespace DDSegmentation {
+
+/// default constructor using an encoding string
+GridDriftChamber::GridDriftChamber(const std::string& cellEncoding) : Segmentation(cellEncoding) {
+  // define type and description
+  _type = "GridDriftChamber";
+  _description = "Drift chamber segmentation in the global coordinates";
+
+  registerIdentifier("identifier_phi", "Cell ID identifier for phi", m_phiID, "phi");
+  registerParameter("delta_phi", "delta phi", m_delta_phi, 0., SegmentationParameter::LengthUnit);
+}
+
+GridDriftChamber::GridDriftChamber(const BitFieldCoder* decoder) : Segmentation(decoder) {
+  // define type and description
+  _type = "GridDriftChamber";
+  _description = "Drift chamber segmentation in the global coordinates";
+
+  registerIdentifier("identifier_phi", "Cell ID identifier for phi", m_phiID, "phi");
+  registerParameter("delta_phi", "delta phi", m_delta_phi, 0., SegmentationParameter::LengthUnit);
+}
+
+Vector3D GridDriftChamber::position(const CellID& /*cID*/) const {
+  Vector3D cellPosition = {0, 0, 0};
+  return cellPosition;
+}
+
+CellID GridDriftChamber::cellID(const Vector3D& /*localPosition*/, const Vector3D& globalPosition,
+                                const VolumeID& vID) const {
+
+  CellID cID = vID;
+
+  double phi_hit = phiFromXY(globalPosition);
+  double posx = globalPosition.X;
+  double posy = globalPosition.Y;
+
+  int lphi = (int) (phi_hit/m_delta_phi);
+  _decoder->set(cID, m_phiID, lphi);
+
+//  std::cout << " myliu: "
+//            << " x: " << posx
+//            << " y: " << posy
+////            << " pre: " << phi_pre
+//            << " phi_hit: " << phi_hit
+//            << " lphi: " << lphi
+//            << std::endl;
+  return cID;
+}
+
+
+REGISTER_SEGMENTATION(GridDriftChamber)
+}
+}
diff --git a/Detector/DetSegmentation/src/plugins/SegmentationFactories.cpp b/Detector/DetSegmentation/src/plugins/SegmentationFactories.cpp
new file mode 100644
index 00000000..9c06d383
--- /dev/null
+++ b/Detector/DetSegmentation/src/plugins/SegmentationFactories.cpp
@@ -0,0 +1,22 @@
+#include "DD4hep/Factories.h"
+#include "DD4hep/detail/SegmentationsInterna.h"
+
+namespace {
+template <typename T>
+dd4hep::SegmentationObject* create_segmentation(const dd4hep::BitFieldCoder* decoder) {
+  return new dd4hep::SegmentationWrapper<T>(decoder);
+}
+}
+
+//#include "DetSegmentation/GridEta.h"
+//DECLARE_SEGMENTATION(GridEta, create_segmentation<dd4hep::DDSegmentation::GridEta>)
+
+//#include "DetSegmentation/FCCSWGridPhiEta.h"
+//DECLARE_SEGMENTATION(FCCSWGridPhiEta, create_segmentation<dd4hep::DDSegmentation::FCCSWGridPhiEta>)
+
+//#include "DetSegmentation/GridRPhiEta.h"
+//DECLARE_SEGMENTATION(GridRPhiEta, create_segmentation<dd4hep::DDSegmentation::GridRPhiEta>)
+
+#include "DetSegmentation/GridDriftChamber.h"
+DECLARE_SEGMENTATION(GridDriftChamber, create_segmentation<dd4hep::DDSegmentation::GridDriftChamber>)
+
-- 
GitLab