From 08c5e7e080e0b2b4e6a55569042b4b80816642fc Mon Sep 17 00:00:00 2001
From: Chengdong Fu <fucd@ihep.ac.cn>
Date: Tue, 2 Feb 2021 08:53:39 +0800
Subject: [PATCH] fix difference of id and offset

---
 .../compact/SEcal05_siw_ECRing_01.xml         |  3 +-
 .../compact/SHcalRpc01_Barrel_01.xml          |  4 +-
 .../compact/SHcalRpc01_EndcapRing_01.xml      |  6 +-
 .../compact/SHcalRpc01_Endcaps_01.xml         |  2 +-
 Detector/DetCEPCv4/compact/ecal_defs.xml      |  3 +-
 .../src/calorimeter/SHcalRpc01_Barrel.cpp     | 83 +++++++++++--------
 .../src/calorimeter/SHcalRpc01_EndcapRing.cpp | 34 ++++----
 .../src/calorimeter/SHcalRpc01_Endcaps.cpp    | 36 ++++----
 8 files changed, 94 insertions(+), 77 deletions(-)

diff --git a/Detector/DetCEPCv4/compact/SEcal05_siw_ECRing_01.xml b/Detector/DetCEPCv4/compact/SEcal05_siw_ECRing_01.xml
index 80b21fe4..25e125ea 100644
--- a/Detector/DetCEPCv4/compact/SEcal05_siw_ECRing_01.xml
+++ b/Detector/DetCEPCv4/compact/SEcal05_siw_ECRing_01.xml
@@ -51,7 +51,8 @@
 
   <readouts>
     <readout name="EcalEndcapRingCollection">
-      <segmentation type="CartesianGridXY" grid_size_x="Ecal_cells_size" grid_size_y="Ecal_cells_size"/>
+      <segmentation type="CartesianGridXY" grid_size_x="Ecal_cells_size" grid_size_y="Ecal_cells_size"
+		    offset_x="-0.5*Ecal_ECRing_Siplate_Size+0.5*Ecal_cells_size" offset_y="-0.5*Ecal_ECRing_Siplate_Size+0.5*Ecal_cells_size"/>
       <id>system:5,module:3,stave:4,tower:3,layer:6,x:32:-16,y:-16</id>
     </readout>
   </readouts>
diff --git a/Detector/DetCEPCv4/compact/SHcalRpc01_Barrel_01.xml b/Detector/DetCEPCv4/compact/SHcalRpc01_Barrel_01.xml
index 427dbc64..81ed917c 100644
--- a/Detector/DetCEPCv4/compact/SHcalRpc01_Barrel_01.xml
+++ b/Detector/DetCEPCv4/compact/SHcalRpc01_Barrel_01.xml
@@ -33,8 +33,8 @@
 
   <readouts>
     <readout name="HcalBarrelCollection">
-      <segmentation type="CartesianGridYZ" grid_size_y="Hcal_cells_size" grid_size_z="Hcal_cells_size"/>
-      <id>system:5,module:3,stave:3,tower:5,layer:6,slice:4,y:32:-16,z:-16</id>
+      <segmentation type="TiledLayerGridXY" grid_size_x="Hcal_cells_size" grid_size_y="Hcal_cells_size"/>
+      <id>system:5,module:3,stave:3,tower:5,layer:6,slice:4,x:32:-16,y:-16</id>
     </readout>
   </readouts>
 
diff --git a/Detector/DetCEPCv4/compact/SHcalRpc01_EndcapRing_01.xml b/Detector/DetCEPCv4/compact/SHcalRpc01_EndcapRing_01.xml
index ab41b9af..e598f6cd 100644
--- a/Detector/DetCEPCv4/compact/SHcalRpc01_EndcapRing_01.xml
+++ b/Detector/DetCEPCv4/compact/SHcalRpc01_EndcapRing_01.xml
@@ -10,7 +10,7 @@
                    dz="HcalEndcapRing_max_z + 2.0*env_safety"/>
             <shape type="PolyhedraRegular" numsides="Hcal_ring_inner_symmetry" rmin="HcalEndcapRing_inner_radius - env_safety"
                    rmax="HcalEndcapRing_outer_radius*cos(pi/Hcal_ring_outer_symmetry) + env_safety" dz="2.0*HcalEndcapRing_max_z + env_safety" material="Air"/>
-            <rotation x="0" y="0" z="90*deg-180*deg/Hcal_ring_inner_symmetry"/>
+            <rotation x="0" y="0" z="-180*deg/Hcal_ring_inner_symmetry"/>
           </shape>
           <shape type="Box" dx="HcalEndcapRing_outer_radius + 2.0*env_safety" dy="HcalEndcapRing_outer_radius + 2.0*env_safety"
                  dz="HcalEndcapRing_min_z - env_safety"/>
@@ -32,8 +32,8 @@
 
   <readouts>
     <readout name="HcalEndcapRingCollection">
-      <segmentation type="CartesianGridXY" grid_size_x="Hcal_cells_size" grid_size_y="Hcal_cells_size"/>
-      <id>system:5,module:3,stave:4,tower:3,layer:6,x:32:-16,y:-16</id>
+      <segmentation type="CartesianGridXY" grid_size_x="Hcal_cells_size" grid_size_y="Hcal_cells_size" offset_x="Hcal_cells_size/2.0" offset_y="Hcal_cells_size/2.0"/>
+      <id>system:5,module:3,stave:4,tower:3,layer:6,y:32:-16,x:-16</id>
     </readout>
   </readouts>
   
diff --git a/Detector/DetCEPCv4/compact/SHcalRpc01_Endcaps_01.xml b/Detector/DetCEPCv4/compact/SHcalRpc01_Endcaps_01.xml
index 677a23dd..3799553f 100644
--- a/Detector/DetCEPCv4/compact/SHcalRpc01_Endcaps_01.xml
+++ b/Detector/DetCEPCv4/compact/SHcalRpc01_Endcaps_01.xml
@@ -31,7 +31,7 @@
   <readouts>
     <readout name="HcalEndcapsCollection">
       <segmentation type="CartesianGridXY" grid_size_x="Hcal_cells_size" grid_size_y="Hcal_cells_size" offset_x="Hcal_cells_size/2.0" offset_y="Hcal_cells_size/2.0" />
-      <id>system:5,module:3,stave:3,tower:5,layer:6,x:32:-16,y:-16</id>
+      <id>system:5,module:3,stave:3,tower:5,layer:6,y:32:-16,x:-16</id>
     </readout>
   </readouts>
   
diff --git a/Detector/DetCEPCv4/compact/ecal_defs.xml b/Detector/DetCEPCv4/compact/ecal_defs.xml
index 057bb5bb..d258d571 100644
--- a/Detector/DetCEPCv4/compact/ecal_defs.xml
+++ b/Detector/DetCEPCv4/compact/ecal_defs.xml
@@ -55,7 +55,8 @@
   <constant name="Ecal_Slab_Plug_length" value="0*mm"/>
   
   <!-- needed only for ecal ring driver... -->
-  <constant name="Ecal_cells_size" value="10.2105*mm"/>
+  <constant name="Ecal_ECRing_Siplate_Size" value="Ecal_endcap_center_box_size - 2*Ecal_EC_Ring_gap - 2*Ecal_lateral_face_thickness"/>
+  <constant name="Ecal_cells_size" value="Ecal_ECRing_Siplate_Size/76."/>
   <constant name="Ecal_Sc_reflector_thickness" value="0.057*mm"/>
   <constant name="Ecal_Sc_thickness" value="2.0*mm"/>
   <constant name="Ecal_Slab_Sc_PCB_thickness" value="0.8*mm"/>
diff --git a/Detector/DetCEPCv4/src/calorimeter/SHcalRpc01_Barrel.cpp b/Detector/DetCEPCv4/src/calorimeter/SHcalRpc01_Barrel.cpp
index 9801a0a1..b49d9964 100644
--- a/Detector/DetCEPCv4/src/calorimeter/SHcalRpc01_Barrel.cpp
+++ b/Detector/DetCEPCv4/src/calorimeter/SHcalRpc01_Barrel.cpp
@@ -4,6 +4,7 @@
 #include "DD4hep/DetFactoryHelper.h"
 #include "DD4hep/DD4hepUnits.h"
 #include "DD4hep/DetType.h"
+#include "DDSegmentation/TiledLayerGridXY.h"
 
 #include "DDRec/Surface.h"
 #include "DDRec/DetectorData.h"
@@ -23,6 +24,7 @@ using dd4hep::Volume;
 using dd4hep::PlacedVolume;
 using dd4hep::Position;
 using dd4hep::RotationZYX;
+using dd4hep::RotationZ;
 using dd4hep::Transform3D;
 using dd4hep::Box;
 using dd4hep::Tube;
@@ -63,30 +65,31 @@ static Ref_t create_detector(Detector& theDetector, xml_h element, SensitiveDete
 
   Readout readout = sens.readout();
   Segmentation seg = readout.segmentation();
+  dd4hep::DDSegmentation::TiledLayerGridXY* tiledSeg = dynamic_cast<dd4hep::DDSegmentation::TiledLayerGridXY*> (seg.segmentation());
+  assert(tiledSeg && "no TiledLayerGridXY found" );
 
   std::vector<double> cellSizeVector = seg.segmentation()->cellDimensions(0);
   double cell_sizeX      = cellSizeVector[0];
   double cell_sizeZ      = cellSizeVector[1];
 
-  double      Hcal_inner_radius   = theDetector.constant<double>("Hcal_inner_radius");
-  double      Hcal_outer_radius_set   = theDetector.constant<double>("Hcal_outer_radius");
-  double      Hcal_half_length    = theDetector.constant<double>("Hcal_half_length");
-  int         Hcal_inner_symmetry = theDetector.constant<int>("Hcal_inner_symmetry");
-  int         Hcal_outer_symmetry = 0;
-  double      Hcal_lateral_plate_thickness     = theDetector.constant<double>("Hcal_lateral_structure_thickness");
-  double      Hcal_modules_gap                 = theDetector.constant<double>("Hcal_modules_gap");
-  int         Hcal_nlayers                     = theDetector.constant<int>("Hcal_nlayers");
-  double      TPC_outer_radius               = theDetector.constant<double>("TPC_outer_radius");
-  double      Ecal_outer_radius               = theDetector.constant<double>("Ecal_outer_radius");
-  int Hcal_barrel_number_modules = theDetector.constant<int>("Hcal_barrel_number_modules");
+  double Hcal_inner_radius            = theDetector.constant<double>("Hcal_inner_radius");
+  double Hcal_outer_radius_set        = theDetector.constant<double>("Hcal_outer_radius");
+  double Hcal_half_length             = theDetector.constant<double>("Hcal_half_length");
+  int    Hcal_inner_symmetry          = theDetector.constant<int>("Hcal_inner_symmetry");
+  int    Hcal_outer_symmetry          = 0;
+  double Hcal_lateral_plate_thickness = theDetector.constant<double>("Hcal_lateral_structure_thickness");
+  double Hcal_modules_gap             = theDetector.constant<double>("Hcal_modules_gap");
+  double Ecal_outer_radius            = theDetector.constant<double>("Ecal_outer_radius");
+  int    Hcal_barrel_number_modules   = theDetector.constant<int>("Hcal_barrel_number_modules");
   
-  double hPrime =    Ecal_outer_radius + theDetector.constant<double>("Hcal_Ecal_gap");
+  double hPrime   = Ecal_outer_radius + theDetector.constant<double>("Hcal_Ecal_gap");
   Hcal_inner_radius = hPrime / cos(pi/8.);
   
   double Hcal_normal_dim_z = (2*Hcal_half_length - (Hcal_barrel_number_modules-1)*Hcal_modules_gap)/Hcal_barrel_number_modules;
 
   xml_coll_t c(x_det,_U(layer));
   xml_comp_t x_layer = c;
+  int         Hcal_nlayers = x_layer.repeat();
 
   double Hcal_radiator_thickness = 0;
   double layerThickness = 0.0;
@@ -95,6 +98,7 @@ static Ref_t create_detector(Detector& theDetector, xml_h element, SensitiveDete
     layerThickness += x_slice.thickness();
     if(x_slice.materialStr()==Hcal_radiator_material) Hcal_radiator_thickness = x_slice.thickness();
   }
+  cout << " cell size xy = " <<  cell_sizeX << " cell size z = " << cell_sizeZ << endl;
   cout << " layer_thickness (from slices) = " << layerThickness << " and radiator_thickness = " << Hcal_radiator_thickness << endl;
   double Hcal_chamber_thickness = layerThickness - Hcal_radiator_thickness; 
 
@@ -149,28 +153,31 @@ static Ref_t create_detector(Detector& theDetector, xml_h element, SensitiveDete
   PlacedVolume calo_pv = envelope.placeVolume(logicCalo, Position(0,0,0));
   DetElement calo(det, "envelope", det_id);
   calo.setPlacement(calo_pv);
-
+  if(tiledSeg) tiledSeg->setOffsetY(-(Hcal_regular_chamber_dim_z/2.-RPC_EdgeWidth)+0.5*cell_sizeZ);
   for(int layer_id=1; layer_id<=Hcal_nlayers; layer_id++){
     double yn = sqrt(Hcal_outer_radius*Hcal_outer_radius - (hPrime + layer_id*layerThickness)*(hPrime + layer_id*layerThickness));
     double Yn = 0.5*d_InnerOctoSize - layer_id*layerThickness;
 
     double halfX = Hcal_chamber_thickness/2.;
-    double halfZ = (yn+Yn)/2.;
+    double halfY = (yn+Yn)/2.;
     
     LayeredCalorimeterData::Layer caloLayer ;
     caloLayer.cellSize0 = cell_sizeX;
     caloLayer.cellSize1 = cell_sizeZ;
 
-    //double halfY = Hcal_normal_dim_z / 2.;
-    double halfY = Hcal_regular_chamber_dim_z / 2.;
-
+    //double halfZ = Hcal_normal_dim_z / 2.;
+    double halfZ = Hcal_regular_chamber_dim_z / 2.;
+    
     double localXPos = hPrime + Hcal_radiator_thickness + Hcal_chamber_thickness/2. + (layer_id-1)*layerThickness;
     double localYPos = -Yn + 0.5*(Yn + yn);
-    Box chamberSolid(halfX, halfY, halfZ);
+
+    Box chamberSolid(halfY, halfZ, halfX);
     string chamberLogical_name      = name+_toString(layer_id,"_layer%d");
     Volume chamberLogical(chamberLogical_name, chamberSolid, air);
     chamberLogical.setAttributes(theDetector, x_layer.regionStr(), x_layer.limitsStr(), x_layer.visStr());
 
+    if(tiledSeg) tiledSeg->setLayerOffsetX((-(halfY-RPC_EdgeWidth)+0.5*cell_sizeX)*2/cell_sizeX);
+
     string layer_name      = name+_toString(layer_id,"_layer%d");
 
     double nRadiationLengths=0.;
@@ -196,44 +203,45 @@ static Ref_t create_detector(Detector& theDetector, xml_h element, SensitiveDete
       thickness_sum       += slice_thickness/2;
 
       // Slice volume & box
-      Box sliceSolid(slice_thickness/2., halfY, halfZ);
+      Box sliceSolid(halfY, halfZ, slice_thickness/2.);
       Volume sliceVol(slice_name, sliceSolid, slice_material);
       
       if ( x_slice.isSensitive() ) {
 	sliceVol.setSensitiveDetector(sens);
 	if(RPC_EdgeWidth>0){
-	  double RPC_GazInlet_In_Y  = halfY - RPC_EdgeWidth - RPCGazInletOuterRadius;
-	  double RPC_GazInlet_In_Z  = halfZ - RPC_EdgeWidth/2;
-	  double RPC_GazInlet_Out_Y = -RPC_GazInlet_In_Y;
-	  double RPC_GazInlet_Out_Z =  RPC_GazInlet_In_Z;
+	  double RPC_GazInlet_In_Z  = halfZ - RPC_EdgeWidth - RPCGazInletOuterRadius;
+	  double RPC_GazInlet_In_Y  = halfY - RPC_EdgeWidth/2;
+	  double RPC_GazInlet_Out_Z = -RPC_GazInlet_In_Z;
+	  double RPC_GazInlet_Out_Y =  RPC_GazInlet_In_Y;
 
 	  string mateialName = x_slice.attr<string>(_Unicode(edge_material));
 	  Material edge_material = theDetector.material(mateialName);
-	  Box solidRPCEdge1(slice_thickness/2.,halfY, halfZ);
-	  Box solidRPCEdge2(slice_thickness/2.,halfY-RPC_EdgeWidth, halfZ-RPC_EdgeWidth);
+	  Box solidRPCEdge1(halfY, halfZ, slice_thickness/2.);
+	  Box solidRPCEdge2(halfY-RPC_EdgeWidth, halfZ-RPC_EdgeWidth, slice_thickness/2.);
 	  SubtractionSolid solidRPCEdge(solidRPCEdge1, solidRPCEdge2, Position(0,0,0));
 	  Volume logicRPCEdge(slice_name+"_edge", solidRPCEdge, edge_material);
 	  logicRPCEdge.setAttributes(theDetector,x_slice.regionStr(),x_slice.limitsStr(),x_slice.visStr());
 	  sliceVol.placeVolume(logicRPCEdge);
 
+	  RotationZYX rotGaz(0, pi/2., 0);
 	  Tube solidRPCGazInlet(RPCGazInletInnerRadius,RPCGazInletOuterRadius,RPC_EdgeWidth/*RPCGazInletLength*//2);
 	  Volume logicRPCGazInlet(slice_name+"_GazInlet", solidRPCGazInlet, edge_material);
 	  logicRPCGazInlet.setAttributes(theDetector,x_slice.regionStr(),x_slice.limitsStr(),x_slice.visStr());
-	  logicRPCEdge.placeVolume(logicRPCGazInlet, Position(0,RPC_GazInlet_In_Y,RPC_GazInlet_In_Z));
-	  logicRPCEdge.placeVolume(logicRPCGazInlet, Position(0,RPC_GazInlet_Out_Y,RPC_GazInlet_Out_Z));
+	  logicRPCEdge.placeVolume(logicRPCGazInlet, Transform3D(rotGaz, Position(RPC_GazInlet_In_Y,RPC_GazInlet_In_Z, 0)));
+	  logicRPCEdge.placeVolume(logicRPCGazInlet, Transform3D(rotGaz, Position(RPC_GazInlet_Out_Y,RPC_GazInlet_Out_Z, 0)));
 	  
 	  Tube solidRPCGazInsideInlet(0,RPCGazInletInnerRadius,RPC_EdgeWidth/*RPCGazInletLength*//2);
 	  Volume logicRPCGazInsideInlet(slice_name+"_GazInsideInlet", solidRPCGazInsideInlet, slice_material);
 	  logicRPCGazInsideInlet.setAttributes(theDetector,x_slice.regionStr(),x_slice.limitsStr(),"SeeThrough");
-	  logicRPCEdge.placeVolume(logicRPCGazInsideInlet, Position(0,RPC_GazInlet_In_Y,RPC_GazInlet_In_Z));
-	  logicRPCEdge.placeVolume(logicRPCGazInsideInlet, Position(0,RPC_GazInlet_Out_Y,RPC_GazInlet_Out_Z));
+	  logicRPCEdge.placeVolume(logicRPCGazInsideInlet, Transform3D(rotGaz, Position(RPC_GazInlet_In_Y,RPC_GazInlet_In_Z, 0)));
+	  logicRPCEdge.placeVolume(logicRPCGazInsideInlet, Transform3D(rotGaz,Position(RPC_GazInlet_Out_Y,RPC_GazInlet_Out_Z, 0)));
 	}
 	if(Hcal_spacer_thickness>0){
 	  Tube solidRPCSpacer(0,Hcal_spacer_thickness/2,slice_thickness/2);
 	  Material space_material = theDetector.material(x_slice.attr<string>(_Unicode(spacer_material)));
 	  Volume logicRPCSpacer(slice_name+"_spacer", solidRPCSpacer, space_material);
 	  logicRPCSpacer.setAttributes(theDetector,x_slice.regionStr(),x_slice.limitsStr(),x_slice.visStr());
-	  RotationZYX rotSpacer(0, pi/2., 0);
+	  RotationZYX rotSpacer(0, 0, 0);
 	  
 	  double gap_hZ = halfZ-RPC_EdgeWidth;
 	  double gap_hY = halfY-RPC_EdgeWidth;
@@ -253,7 +261,7 @@ static Ref_t create_detector(Detector& theDetector, xml_h element, SensitiveDete
 	    double SpacerY = gap_hY - y_lateral_space - y_counter*Hcal_spacer_separation;
 	    for(int z_counter = 0; z_counter <=z_number_of_separations; z_counter++){
 	      double SpacerZ = gap_hZ - z_lateral_space - z_counter*Hcal_spacer_separation;
-	      PlacedVolume space_pv = sliceVol.placeVolume(logicRPCSpacer, Transform3D(rotSpacer, Position(0,SpacerY,SpacerZ)));
+	      PlacedVolume space_pv = sliceVol.placeVolume(logicRPCSpacer, Transform3D(rotSpacer, Position(SpacerY,SpacerZ,0)));
 	    }
 	  }
 	}
@@ -279,7 +287,7 @@ static Ref_t create_detector(Detector& theDetector, xml_h element, SensitiveDete
       thickness_sum += slice_thickness/2;
 
       // slice PlacedVolume
-      PlacedVolume slice_phv = chamberLogical.placeVolume(sliceVol,Position(slice_pos_z,0,0));
+      PlacedVolume slice_phv = chamberLogical.placeVolume(sliceVol,Position(0,0,slice_pos_z));
       if ( x_slice.isSensitive() ) {
 	int slice_id  = (layer_id > Hcal_nlayers)? 1:-1;
 	slice_phv.addPhysVolID("layer",layer_id).addPhysVolID("slice",slice_id);
@@ -309,14 +317,17 @@ static Ref_t create_detector(Detector& theDetector, xml_h element, SensitiveDete
     for(int stave_id = 1; stave_id <= 8; stave_id++){
       double phirot = stave_phi_offset+(stave_id-1)*pi/4.;
 
-      RotationZYX rot(0, phirot, pi*0.5);
-      RotationZYX rotInverse(0,-phirot, -pi*0.5);
+      RotationZYX rot(pi/2, pi/2, 0); //phirot);
+      RotationZ rotZ(phirot);
+      RotationZYX rotAll = rotZ*rot;
+      RotationZYX rotInverse(phirot, 0, 0);
       for(int module_id = 1; module_id <= Hcal_barrel_number_modules; module_id++){
         module_z_offset = - Hcal_half_length + Hcal_normal_dim_z/2. + (module_id-1)*(Hcal_normal_dim_z+Hcal_modules_gap);
 	
-        Position localPos(localXPos,-module_z_offset,localYPos);
+        Position localPos(localXPos,localYPos,module_z_offset);
         Position newPos = rotInverse*localPos;
-	Transform3D tran3D(rot, newPos);
+
+	Transform3D tran3D(rotAll, newPos);
 	PlacedVolume pv = logicCalo.placeVolume(chamberLogical, tran3D);
 	pv.addPhysVolID("stave",stave_id).addPhysVolID("module",module_id).addPhysVolID("layer",layer_id);
 	DetElement layer(calo, name+_toString(stave_id,"_stave%d")+_toString(module_id,"_module%d")+_toString(layer_id,"_layer%d"), det_id);
diff --git a/Detector/DetCEPCv4/src/calorimeter/SHcalRpc01_EndcapRing.cpp b/Detector/DetCEPCv4/src/calorimeter/SHcalRpc01_EndcapRing.cpp
index 3a9978cc..08e00c5a 100644
--- a/Detector/DetCEPCv4/src/calorimeter/SHcalRpc01_EndcapRing.cpp
+++ b/Detector/DetCEPCv4/src/calorimeter/SHcalRpc01_EndcapRing.cpp
@@ -197,7 +197,7 @@ static Ref_t create_detector(Detector& theDetector, xml_h element, SensitiveDete
   
   // chamber placements
   for(int stave_id = 1; stave_id <= 4; stave_id++){
-    double angle = pi/2.*(stave_id-1);
+    double angle = -pi/2.*(stave_id-1);
     RotationZYX lrot(angle,0,0);
     for (int layer_id = 1; layer_id <= number_of_chambers; layer_id++){
       double Zoff = -pDz + (layer_id-1)*layerThickness + Hcal_radiator_thickness + Hcal_chamber_thickness/2.;
@@ -217,28 +217,30 @@ static Ref_t create_detector(Detector& theDetector, xml_h element, SensitiveDete
 	cout << "Hcal_EndcapRing:  inner_thickness= " << inner_thickness << endl;
 	cout << "Hcal_EndcapRing:  outer_thickness= " << thickness_sum << endl;
       }
-      LayeredCalorimeterData::Layer caloLayer ;
-      caloLayer.cellSize0 = cell_sizeX;
-      caloLayer.cellSize1 = cell_sizeY;
-      caloLayer.inner_nRadiationLengths   = nRadiationLengthsInside;
-      caloLayer.inner_nInteractionLengths = nInteractionLengthsInside;
-      caloLayer.inner_thickness           = inner_thickness;
-      caloLayer.sensitive_thickness       = sensitive_thickness;
-      caloLayer.outer_nRadiationLengths   = nRadiationLengths;
-      caloLayer.outer_nInteractionLengths = nInteractionLengths;
-      caloLayer.outer_thickness           = thickness_sum;
+      if(stave_id==1){//only one needed, according to wenxingfang's
+	LayeredCalorimeterData::Layer caloLayer ;
+	caloLayer.cellSize0 = cell_sizeX;
+	caloLayer.cellSize1 = cell_sizeY;
+	caloLayer.inner_nRadiationLengths   = nRadiationLengthsInside;
+	caloLayer.inner_nInteractionLengths = nInteractionLengthsInside;
+	caloLayer.inner_thickness           = inner_thickness;
+	caloLayer.sensitive_thickness       = sensitive_thickness;
+	caloLayer.outer_nRadiationLengths   = nRadiationLengths;
+	caloLayer.outer_nInteractionLengths = nInteractionLengths;
+	caloLayer.outer_thickness           = thickness_sum;
+	
+	caloLayer.distance = start_z + (layer_id-1)*layerThickness;
+	caloLayer.absorberThickness = Hcal_radiator_thickness ;
       
-      caloLayer.distance = start_z + (layer_id-1)*layerThickness;
-      caloLayer.absorberThickness = Hcal_radiator_thickness ;
-      
-      caloData->layers.push_back( caloLayer ) ;
+	caloData->layers.push_back( caloLayer ) ;
+      }
     }
   }
   
   // Placements
   double endcap_z_offset = start_z + pDz;
   for(int side = 0; side <= 1; side++){
-    int module_id = (side==0) ? 0 : 6;
+    int module_id = (side==0) ? 6 : 0;
     double this_module_z_offset = (side==0) ? endcap_z_offset : -endcap_z_offset;
     // use reflect volume for z<0, therefore, same rotation
     // segmentation violation happen if EndCapRingLogical.reflect(), back to rotate Y
diff --git a/Detector/DetCEPCv4/src/calorimeter/SHcalRpc01_Endcaps.cpp b/Detector/DetCEPCv4/src/calorimeter/SHcalRpc01_Endcaps.cpp
index e55eb472..e1501158 100644
--- a/Detector/DetCEPCv4/src/calorimeter/SHcalRpc01_Endcaps.cpp
+++ b/Detector/DetCEPCv4/src/calorimeter/SHcalRpc01_Endcaps.cpp
@@ -205,7 +205,7 @@ static Ref_t create_detector(Detector& theDetector, xml_h element, SensitiveDete
   if(possible_number_of_chambers < number_of_chambers) number_of_chambers = possible_number_of_chambers;
   // chamber placements
   for(int stave_id = 1; stave_id <= 4; stave_id++){
-    double angle = pi/2.*(stave_id-1);
+    double angle = -pi/2.*(stave_id-1);
     //RotationZ lrotz(angle);
     RotationZYX lrot(angle,0,0);
     for (int layer_id = 1; layer_id <= number_of_chambers; layer_id++){
@@ -226,28 +226,30 @@ static Ref_t create_detector(Detector& theDetector, xml_h element, SensitiveDete
 	cout << "Hcal_Endcap:  inner_thickness= " << inner_thickness << endl;
 	cout << "Hcal_Endcap:  outer_thickness= " << thickness_sum << endl;
       }
-      LayeredCalorimeterData::Layer caloLayer ;
-      caloLayer.cellSize0 = cell_sizeX;
-      caloLayer.cellSize1 = cell_sizeY;
-      caloLayer.inner_nRadiationLengths = nRadiationLengthsInside;
-      caloLayer.inner_nInteractionLengths = nInteractionLengthsInside;
-      caloLayer.inner_thickness = inner_thickness;
-      caloLayer.sensitive_thickness = sensitive_thickness;
-      caloLayer.outer_nRadiationLengths = nRadiationLengths;
-      caloLayer.outer_nInteractionLengths = nInteractionLengths;
-      caloLayer.outer_thickness = thickness_sum;
-      
-      caloLayer.distance = Hcal_start_z + (layer_id-1)*layerThickness;
-      caloLayer.absorberThickness = Hcal_radiator_thickness ;
-      
-      caloData->layers.push_back( caloLayer ) ;
+      if(stave_id==1){// only for one stave is good. by wenxingfang 
+	LayeredCalorimeterData::Layer caloLayer ;
+	caloLayer.cellSize0 = cell_sizeX;
+	caloLayer.cellSize1 = cell_sizeY;
+	caloLayer.inner_nRadiationLengths = nRadiationLengthsInside;
+	caloLayer.inner_nInteractionLengths = nInteractionLengthsInside;
+	caloLayer.inner_thickness = inner_thickness;
+	caloLayer.sensitive_thickness = sensitive_thickness;
+	caloLayer.outer_nRadiationLengths = nRadiationLengths;
+	caloLayer.outer_nInteractionLengths = nInteractionLengths;
+	caloLayer.outer_thickness = thickness_sum;
+	
+	caloLayer.distance = Hcal_start_z + (layer_id-1)*layerThickness;
+	caloLayer.absorberThickness = Hcal_radiator_thickness ;
+	
+	caloData->layers.push_back( caloLayer ) ;
+      }
     }
   }
   
   // Placements
   double endcap_z_offset = Hcal_start_z + Hcal_endcap_thickness/2.;
   for(int side = 0; side <= 1; side++){
-    int module_id = (side==0) ? 0 : 6;
+    int module_id = (side==0) ? 6 : 0;
     double this_module_z_offset = (side==0) ? endcap_z_offset : -endcap_z_offset;
     // use reflect volume for z<0, therefore, same rotation
     // segmentation violation happen if EndCapRingLogical.reflect(), back to rotate Y
-- 
GitLab