From 29ee375916c6896e97b3c5ffb8b1769746ba8cf0 Mon Sep 17 00:00:00 2001
From: Andre Sailer <andre.philippe.sailer@cern.ch>
Date: Wed, 17 Jun 2015 16:15:16 +0000
Subject: [PATCH] DDSegmentation: cellDimensions for PolarGridRPhi[2]

Implement cellDimensions Functions for PolarGridRPhi and PolarGridRPhi2
Implement unit tests
Correct documentation string in CartesianGrid[XY]Z
---
 .../include/DDSegmentation/CartesianGridXZ.h  |   2 +-
 .../include/DDSegmentation/CartesianGridYZ.h  |   2 +-
 .../include/DDSegmentation/PolarGridRPhi.h    |  10 ++
 .../include/DDSegmentation/PolarGridRPhi2.h   |  10 ++
 DDSegmentation/src/PolarGridRPhi.cpp          |  13 ++
 DDSegmentation/src/PolarGridRPhi2.cpp         |  21 +++
 DDTest/CMakeLists.txt                         |   9 +
 DDTest/src/test_cellDimensionsRPhi2.cc        | 166 ++++++++++++++++++
 8 files changed, 231 insertions(+), 2 deletions(-)
 create mode 100644 DDTest/src/test_cellDimensionsRPhi2.cc

diff --git a/DDSegmentation/include/DDSegmentation/CartesianGridXZ.h b/DDSegmentation/include/DDSegmentation/CartesianGridXZ.h
index 9a13ccbbb..0719bcbf8 100644
--- a/DDSegmentation/include/DDSegmentation/CartesianGridXZ.h
+++ b/DDSegmentation/include/DDSegmentation/CartesianGridXZ.h
@@ -77,7 +77,7 @@ public:
 
 	    Returns a vector of the cellDimensions of the given cell ID
 	    \param cellID is ignored as all cells have the same dimension
-	    \return std::vector<double> size:
+	    \return std::vector<double> size 2:
 	    -# size in x
 	    -# size in z
 	*/
diff --git a/DDSegmentation/include/DDSegmentation/CartesianGridYZ.h b/DDSegmentation/include/DDSegmentation/CartesianGridYZ.h
index 1a9aa13bf..a9f3fba8e 100644
--- a/DDSegmentation/include/DDSegmentation/CartesianGridYZ.h
+++ b/DDSegmentation/include/DDSegmentation/CartesianGridYZ.h
@@ -79,7 +79,7 @@ public:
 
 	    Returns a vector of the cellDimensions of the given cell ID
 	    \param cellID is ignored as all cells have the same dimension
-	    \return std::vector<double> size:
+	    \return std::vector<double> size 2:
 	    -# size in y
 	    -# size in z
 	*/
diff --git a/DDSegmentation/include/DDSegmentation/PolarGridRPhi.h b/DDSegmentation/include/DDSegmentation/PolarGridRPhi.h
index 46d2d698e..508d32286 100644
--- a/DDSegmentation/include/DDSegmentation/PolarGridRPhi.h
+++ b/DDSegmentation/include/DDSegmentation/PolarGridRPhi.h
@@ -73,6 +73,16 @@ public:
 	void setFieldNamePhi(const std::string& fieldName) {
 		_phiId = fieldName;
 	}
+	/** \brief Returns a vector<double> of the cellDimensions of the given cell ID
+	    in natural order of dimensions: dr, r*dPhi
+
+	    Returns a vector of the cellDimensions of the given cell ID
+	    \param cID cellID
+	    \return std::vector<double> size 2:
+	    -# size in r
+	    -# size of r*dPhi at the radial centre of the pad
+	*/
+	virtual std::vector<double> cellDimensions(const CellID& cID) const;
 
 protected:
 	/// the grid size in R
diff --git a/DDSegmentation/include/DDSegmentation/PolarGridRPhi2.h b/DDSegmentation/include/DDSegmentation/PolarGridRPhi2.h
index eb55a11d2..985ab80dd 100644
--- a/DDSegmentation/include/DDSegmentation/PolarGridRPhi2.h
+++ b/DDSegmentation/include/DDSegmentation/PolarGridRPhi2.h
@@ -115,6 +115,16 @@ public:
 	void setFieldNamePhi(const std::string& fieldName) {
 		_phiId = fieldName;
 	}
+	/** \brief Returns a vector<double> of the cellDimensions of the given cell ID
+	    in natural order of dimensions: dr, r*dPhi
+
+	    Returns a vector of the cellDimensions of the given cell ID
+	    \param cellID is ignored as all cells have the same dimension
+	    \return std::vector<double> size 2:
+	    -# size in r
+	    -# size of r*dPhi at the radial centre of the pad
+	*/
+	virtual std::vector<double> cellDimensions(const CellID& cellID) const;
 
 protected:
 	/// the grid boundaries in R
diff --git a/DDSegmentation/src/PolarGridRPhi.cpp b/DDSegmentation/src/PolarGridRPhi.cpp
index 250a62f52..5ef8cae4c 100644
--- a/DDSegmentation/src/PolarGridRPhi.cpp
+++ b/DDSegmentation/src/PolarGridRPhi.cpp
@@ -55,6 +55,19 @@ Vector3D PolarGridRPhi::position(const CellID& cID) const {
 	return _decoder->getValue();
 }
 
+std::vector<double> PolarGridRPhi::cellDimensions(const CellID& cID) const {
+  _decoder->setValue(cID);
+  const double rPhiSize = binToPosition((*_decoder)[_rId].value(), _gridSizeR, _offsetR)*_gridSizePhi;
+#ifdef DD4HEP_USE_CXX11
+  return {_gridSizeR, rPhiSize};
+#else
+  std::vector<double> cellDims(2,0.0);
+  cellDims[0] = _gridSizeR;
+  cellDims[1] = rPhiSize;
+  return cellDims;
+#endif
+}
+
 REGISTER_SEGMENTATION(PolarGridRPhi)
 
 } /* namespace DDSegmentation */
diff --git a/DDSegmentation/src/PolarGridRPhi2.cpp b/DDSegmentation/src/PolarGridRPhi2.cpp
index dc6282a6e..71e648cef 100644
--- a/DDSegmentation/src/PolarGridRPhi2.cpp
+++ b/DDSegmentation/src/PolarGridRPhi2.cpp
@@ -67,6 +67,27 @@ Vector3D PolarGridRPhi2::position(const CellID& cID) const {
 	return _decoder->getValue();
 }
 
+
+std::vector<double> PolarGridRPhi2::cellDimensions(const CellID& cID) const {
+
+  _decoder->setValue(cID);
+
+  const int rBin = (*_decoder)[_rId].value();
+  const double rCenter = binToPosition(rBin, _gridRValues, _offsetR);
+
+  const double rPhiSize = _gridPhiValues[rBin]*rCenter;
+  const double rSize = _gridRValues[rBin+1]-_gridRValues[rBin];
+
+#ifdef DD4HEP_USE_CXX11
+  return {rSize, rPhiSize};
+#else
+  std::vector<double> cellDims(2,0.0);
+  cellDims[0] = rSize;
+  cellDims[1] = rPhiSize;
+  return cellDims;
+#endif
+}
+
 REGISTER_SEGMENTATION(PolarGridRPhi2)
 
 } /* namespace DDSegmentation */
diff --git a/DDTest/CMakeLists.txt b/DDTest/CMakeLists.txt
index 2531d55b1..9570cc953 100644
--- a/DDTest/CMakeLists.txt
+++ b/DDTest/CMakeLists.txt
@@ -71,6 +71,15 @@ ADD_TEST( t_${test_name} "${CMAKE_INSTALL_PREFIX}/bin/run_test.sh"
 SET_TESTS_PROPERTIES( t_${test_name} PROPERTIES PASS_REGULAR_EXPRESSION "TEST_PASSED" )
 SET_TESTS_PROPERTIES( t_${test_name} PROPERTIES FAIL_REGULAR_EXPRESSION "TEST_FAILED" )
 
+
+SET( test_name "test_cellDimensionsRPhi2" )
+ADD_EXECUTABLE( ${test_name} ./src/${test_name}.cc )
+TARGET_LINK_LIBRARIES(${test_name} ${test_link_libraries})
+ADD_TEST( t_${test_name} "${CMAKE_INSTALL_PREFIX}/bin/run_test.sh"
+  ${EXECUTABLE_OUTPUT_PATH}/${test_name}  )
+SET_TESTS_PROPERTIES( t_${test_name} PROPERTIES PASS_REGULAR_EXPRESSION "TEST_PASSED" )
+SET_TESTS_PROPERTIES( t_${test_name} PROPERTIES FAIL_REGULAR_EXPRESSION "TEST_FAILED" )
+
 #--------------------------------------------------
 
 
diff --git a/DDTest/src/test_cellDimensionsRPhi2.cc b/DDTest/src/test_cellDimensionsRPhi2.cc
new file mode 100644
index 000000000..8ad906bfb
--- /dev/null
+++ b/DDTest/src/test_cellDimensionsRPhi2.cc
@@ -0,0 +1,166 @@
+#include "DDSegmentation/Segmentation.h"
+#include "DDSegmentation/PolarGridRPhi2.h"
+#include "DDSegmentation/PolarGridRPhi.h"
+#include "DD4hep/DDTest.h"
+
+#include <iostream>
+#include <iomanip>
+#include <vector>
+#include <algorithm>
+#include <exception>
+#include <cmath>
+
+DD4hep::DDTest test = DD4hep::DDTest( "CellDimensions" ) ;
+
+using DD4hep::DDSegmentation::Segmentation;
+using DD4hep::DDSegmentation::CellID;
+
+Segmentation* createPolarGridRPhi2();
+Segmentation* createPolarGridRPhi(double rSize, double phiSize);
+CellID getCellID(Segmentation* seg, long long rId, long long pId);
+
+class TestTuple {
+public:
+  double    _r;
+  double    _p; ///phi
+  CellID _cid; ///cellID
+  TestTuple( Segmentation* seg, double r, double p, long long rB, long long rP): _r(r), _p(p), _cid(getCellID(seg, rB, rP)) {}
+};
+
+void testRPhi2();
+void testRPhi();
+
+int main() {
+
+  testRPhi2();
+  testRPhi();
+
+  return 0;
+}
+
+
+Segmentation* createPolarGridRPhi(double rSize, double phiSize) {
+
+  DD4hep::DDSegmentation::PolarGridRPhi* seg = new DD4hep::DDSegmentation::PolarGridRPhi("system:8,barrel:3,layer:8,slice:13,r:16,phi:16");
+
+  seg->setGridSizeR(rSize);
+  seg->setGridSizePhi(phiSize);
+  return seg;
+
+}
+
+Segmentation* createPolarGridRPhi2() {
+
+  DD4hep::DDSegmentation::PolarGridRPhi2* seg = new DD4hep::DDSegmentation::PolarGridRPhi2("system:8,barrel:3,layer:8,slice:13,r:16,phi:16");
+
+  std::vector<double> rValues, phiValues;
+
+  rValues.push_back( 10);//  0
+  rValues.push_back( 30);//  1
+  rValues.push_back( 35);//  2
+  rValues.push_back( 40);//  3
+  rValues.push_back( 50);//  4
+  rValues.push_back( 60);//  5
+  rValues.push_back( 70);//  6
+  rValues.push_back( 80);//  7
+  rValues.push_back( 90);//  8
+  rValues.push_back(100);//  9
+  rValues.push_back(110);// 10
+  rValues.push_back(120);// 11
+  rValues.push_back(130);// 12
+  rValues.push_back(140);// 13
+  rValues.push_back(150);// 14
+  rValues.push_back(153);// 15
+
+  const double DegToRad(M_PI/180.0);
+
+  phiValues.push_back( 10*DegToRad);//  0
+  phiValues.push_back( 20*DegToRad);//  1
+  phiValues.push_back( 30*DegToRad);//  2
+  phiValues.push_back( 40*DegToRad);//  3
+  phiValues.push_back( 50*DegToRad);//  4
+  phiValues.push_back( 60*DegToRad);//  5
+  phiValues.push_back( 70*DegToRad);//  6
+  phiValues.push_back( 80*DegToRad);//  7
+  phiValues.push_back( 90*DegToRad);//  8
+  phiValues.push_back(100*DegToRad);//  9
+  phiValues.push_back(110*DegToRad);// 10
+  phiValues.push_back(120*DegToRad);// 11
+  phiValues.push_back(130*DegToRad);// 12
+  phiValues.push_back(140*DegToRad);// 13
+  phiValues.push_back(150*DegToRad);// 14
+  //need one less phiValue than radial segments
+  //phiValues.push_back(160*DegToRad);// 15
+
+  seg->setGridRValues(rValues);
+  seg->setGridPhiValues(phiValues);
+  seg->setOffsetPhi(-M_PI);
+
+  return seg;
+}
+
+CellID getCellID(DD4hep::DDSegmentation::Segmentation* seg, long long rB, long long pB){
+  (*seg->decoder())["r"] = rB;
+  (*seg->decoder())["phi"] = pB;
+  return (*seg->decoder()).getValue();
+}
+
+void testRPhi2(){
+  std::vector<TestTuple> tests;
+
+  DD4hep::DDSegmentation::Segmentation* seg = createPolarGridRPhi2();
+  const double DegToRad = M_PI/180.0;
+  tests.push_back( TestTuple( seg, 20.0, 20*10*DegToRad,  0,   1 ) );
+  tests.push_back( TestTuple( seg, 5.0, 32.5*20*DegToRad,  1,   1 ) );
+  tests.push_back( TestTuple( seg, 3.0, 151.5*150*DegToRad,  14, 1 ) );
+
+  try {
+
+    for (std::vector<TestTuple>::iterator it = tests.begin(); it != tests.end(); ++it) {
+
+      CellID cellid   = (*it)._cid;
+      double rSize    = (*it)._r;
+      double rPhiSize = (*it)._p;
+
+      test( fabs(seg->cellDimensions(cellid)[0] - rSize )  < 1e-11, " Seg: RPhi2: Dimension for R" );
+      test( fabs(seg->cellDimensions(cellid)[1] - rPhiSize )  < 1e-11, " Seg: RPhi2: Dimension for RPhi" );
+
+    }
+  } catch( std::exception &e ){
+
+    test.log( e.what() );
+    test.error( "exception occurred" );
+  }
+
+}
+
+void testRPhi(){
+  std::vector<TestTuple> tests;
+
+  const double rSizeGrid = 10.0;
+  const double phiSizeGrid = M_PI/36;
+  DD4hep::DDSegmentation::Segmentation* seg = createPolarGridRPhi(rSizeGrid, phiSizeGrid);
+
+  tests.push_back( TestTuple( seg, rSizeGrid, rSizeGrid*(0) *phiSizeGrid,   0, 1 ) );
+  tests.push_back( TestTuple( seg, rSizeGrid, rSizeGrid*(1) *phiSizeGrid,   1, 1 ) );
+  tests.push_back( TestTuple( seg, rSizeGrid, rSizeGrid*(14)*phiSizeGrid,  14, 1 ) );
+
+  try {
+
+    for (std::vector<TestTuple>::iterator it = tests.begin(); it != tests.end(); ++it) {
+
+      CellID cellid   = (*it)._cid;
+      double rSize    = (*it)._r;
+      double rPhiSize = (*it)._p;
+
+      test( fabs(seg->cellDimensions(cellid)[0] - rSize )  < 1e-11, " Seg: RPhi: Dimension for R" );
+      test( fabs(seg->cellDimensions(cellid)[1] - rPhiSize )  < 1e-11, " Seg: RPhi: Dimension for RPhi" );
+
+    }
+  } catch( std::exception &e ){
+
+    test.log( e.what() );
+    test.error( "exception occurred" );
+  }
+
+}
-- 
GitLab