File indexing completed on 2025-01-30 09:17:43
0001 #include "DDSegmentation/Segmentation.h"
0002 #include "DDSegmentation/PolarGridRPhi2.h"
0003 #include "DDSegmentation/PolarGridRPhi.h"
0004 #include "DD4hep/Printout.h"
0005 #include "DD4hep/DDTest.h"
0006
0007 #include <iostream>
0008 #include <iomanip>
0009 #include <vector>
0010 #include <algorithm>
0011 #include <exception>
0012 #include <cmath>
0013
0014 static dd4hep::DDTest test( "CellDimensions" ) ;
0015
0016 using dd4hep::DDSegmentation::Segmentation;
0017 using dd4hep::DDSegmentation::CellID;
0018
0019 Segmentation* createPolarGridRPhi2();
0020 Segmentation* createPolarGridRPhi(double rSize, double phiSize);
0021 CellID getCellID(Segmentation* seg, long long rId, long long pId);
0022
0023 class TestTuple {
0024 public:
0025 double _r;
0026 double _p;
0027 CellID _cid;
0028 TestTuple( Segmentation* seg, double r, double p, long long rB, long long rP): _r(r), _p(p), _cid(getCellID(seg, rB, rP)) {}
0029 };
0030
0031 void testRPhi2();
0032 void testRPhi();
0033
0034 int main() {
0035 using namespace dd4hep;
0036 try {
0037 testRPhi2();
0038 testRPhi();
0039 }
0040 catch(const std::exception& e) {
0041 printout(ERROR,"CellDimensions","+++ Caught unhandled exception: %s",e.what());
0042 }
0043 catch(...) {
0044 printout(ERROR,"CellDimensions","+++ Caught UNKNOWN unhandled exception.");
0045 }
0046 return 0;
0047 }
0048
0049
0050 Segmentation* createPolarGridRPhi(double rSize, double phiSize) {
0051
0052 dd4hep::DDSegmentation::PolarGridRPhi* seg = new dd4hep::DDSegmentation::PolarGridRPhi("system:8,barrel:3,layer:8,slice:13,r:16,phi:16");
0053
0054 seg->setGridSizeR(rSize);
0055 seg->setGridSizePhi(phiSize);
0056 return seg;
0057
0058 }
0059
0060 Segmentation* createPolarGridRPhi2() {
0061
0062 dd4hep::DDSegmentation::PolarGridRPhi2* seg = new dd4hep::DDSegmentation::PolarGridRPhi2("system:8,barrel:3,layer:8,slice:13,r:16,phi:16");
0063
0064 std::vector<double> rValues, phiValues;
0065
0066 rValues.push_back( 10);
0067 rValues.push_back( 30);
0068 rValues.push_back( 35);
0069 rValues.push_back( 40);
0070 rValues.push_back( 50);
0071 rValues.push_back( 60);
0072 rValues.push_back( 70);
0073 rValues.push_back( 80);
0074 rValues.push_back( 90);
0075 rValues.push_back(100);
0076 rValues.push_back(110);
0077 rValues.push_back(120);
0078 rValues.push_back(130);
0079 rValues.push_back(140);
0080 rValues.push_back(150);
0081 rValues.push_back(153);
0082
0083 const double DegToRad(M_PI/180.0);
0084
0085 phiValues.push_back( 10*DegToRad);
0086 phiValues.push_back( 20*DegToRad);
0087 phiValues.push_back( 30*DegToRad);
0088 phiValues.push_back( 40*DegToRad);
0089 phiValues.push_back( 50*DegToRad);
0090 phiValues.push_back( 60*DegToRad);
0091 phiValues.push_back( 70*DegToRad);
0092 phiValues.push_back( 80*DegToRad);
0093 phiValues.push_back( 90*DegToRad);
0094 phiValues.push_back(100*DegToRad);
0095 phiValues.push_back(110*DegToRad);
0096 phiValues.push_back(120*DegToRad);
0097 phiValues.push_back(130*DegToRad);
0098 phiValues.push_back(140*DegToRad);
0099 phiValues.push_back(150*DegToRad);
0100
0101
0102
0103 seg->setGridRValues(rValues);
0104 seg->setGridPhiValues(phiValues);
0105 seg->setOffsetPhi(-M_PI);
0106
0107 return seg;
0108 }
0109
0110 CellID getCellID(dd4hep::DDSegmentation::Segmentation* seg, long long rB, long long pB){
0111 CellID cID { 0 };
0112 seg->decoder()->set(cID,"r",rB) ;
0113 seg->decoder()->set(cID,"phi",pB);
0114 return cID;
0115 }
0116
0117 void testRPhi2(){
0118 std::vector<TestTuple> tests;
0119
0120 dd4hep::DDSegmentation::Segmentation* seg = createPolarGridRPhi2();
0121 const double DegToRad = M_PI/180.0;
0122 tests.push_back( TestTuple( seg, 20.0, 20*10*DegToRad, 0, 1 ) );
0123 tests.push_back( TestTuple( seg, 5.0, 32.5*20*DegToRad, 1, 1 ) );
0124 tests.push_back( TestTuple( seg, 3.0, 151.5*150*DegToRad, 14, 1 ) );
0125
0126 try {
0127
0128 for (std::vector<TestTuple>::iterator it = tests.begin(); it != tests.end(); ++it) {
0129
0130 CellID cellid = (*it)._cid;
0131 double rSize = (*it)._r;
0132 double rPhiSize = (*it)._p;
0133
0134 test( fabs(seg->cellDimensions(cellid)[0] - rSize ) < 1e-11, " Seg: RPhi2: Dimension for R" );
0135 test( fabs(seg->cellDimensions(cellid)[1] - rPhiSize ) < 1e-11, " Seg: RPhi2: Dimension for RPhi" );
0136
0137 }
0138 } catch( std::exception &e ){
0139
0140 test.log( e.what() );
0141 test.error( "exception occurred" );
0142 }
0143
0144 }
0145
0146 void testRPhi(){
0147 std::vector<TestTuple> tests;
0148
0149 const double rSizeGrid = 10.0;
0150 const double phiSizeGrid = M_PI/36;
0151 dd4hep::DDSegmentation::Segmentation* seg = createPolarGridRPhi(rSizeGrid, phiSizeGrid);
0152
0153 tests.push_back( TestTuple( seg, rSizeGrid, rSizeGrid*(0) *phiSizeGrid, 0, 1 ) );
0154 tests.push_back( TestTuple( seg, rSizeGrid, rSizeGrid*(1) *phiSizeGrid, 1, 1 ) );
0155 tests.push_back( TestTuple( seg, rSizeGrid, rSizeGrid*(14)*phiSizeGrid, 14, 1 ) );
0156
0157 try {
0158
0159 for (std::vector<TestTuple>::iterator it = tests.begin(); it != tests.end(); ++it) {
0160
0161 CellID cellid = (*it)._cid;
0162 double rSize = (*it)._r;
0163 double rPhiSize = (*it)._p;
0164
0165 test( fabs(seg->cellDimensions(cellid)[0] - rSize ) < 1e-11, " Seg: RPhi: Dimension for R" );
0166 test( fabs(seg->cellDimensions(cellid)[1] - rPhiSize ) < 1e-11, " Seg: RPhi: Dimension for RPhi" );
0167
0168 }
0169 } catch( std::exception &e ){
0170
0171 test.log( e.what() );
0172 test.error( "exception occurred" );
0173 }
0174
0175 }