Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:17:43

0001 #include "DDSegmentation/PolarGridRPhi2.h"
0002 #include "DD4hep/DDTest.h"
0003 
0004 #include <iostream>
0005 #include <iomanip>
0006 #include <vector>
0007 #include <algorithm>
0008 #include <exception>
0009 
0010 class TestTuple {
0011 public:
0012   double    _r;
0013   double    _p; //phi
0014   long long _rB;
0015   long long _pB;
0016   TestTuple( double r, double p, long long rB, long long pB): _r(r), _p(p), _rB(rB), _pB(pB) {}
0017 };
0018 
0019 int main() {
0020 
0021   dd4hep::DDTest test( "PolarGridRPhi2" );
0022 
0023   try{
0024 
0025     dd4hep::DDSegmentation::PolarGridRPhi2 seg("system:8,layer:8,barrel:3,layer:8,slice:5,r:16,phi:16");
0026     const int rBitOffset   = 32; //sum of all the others before
0027     const int phiBitOffset = 16+32; //sum of all the others before
0028 
0029     std::vector<double> rValues, phiValues;
0030 
0031     rValues.push_back( 10);//  0
0032     rValues.push_back( 20);//  1
0033     rValues.push_back( 30);//  2
0034     rValues.push_back( 40);//  3
0035     rValues.push_back( 50);//  4
0036     rValues.push_back( 60);//  5
0037     rValues.push_back( 70);//  6
0038     rValues.push_back( 80);//  7
0039     rValues.push_back( 90);//  8
0040     rValues.push_back(100);//  9
0041     rValues.push_back(110);// 10
0042     rValues.push_back(120);// 11
0043     rValues.push_back(130);// 12
0044     rValues.push_back(140);// 13
0045     rValues.push_back(150);// 14
0046     rValues.push_back(160);// 15
0047 
0048     const double DegToRad(M_PI/180.0);
0049 
0050     phiValues.push_back( 10*DegToRad);//  0
0051     phiValues.push_back( 20*DegToRad);//  1
0052     phiValues.push_back( 30*DegToRad);//  2
0053     phiValues.push_back( 40*DegToRad);//  3
0054     phiValues.push_back( 50*DegToRad);//  4
0055     phiValues.push_back( 60*DegToRad);//  5
0056     phiValues.push_back( 70*DegToRad);//  6
0057     phiValues.push_back( 80*DegToRad);//  7
0058     phiValues.push_back( 90*DegToRad);//  8
0059     phiValues.push_back(100*DegToRad);//  9
0060     phiValues.push_back(110*DegToRad);// 10
0061     phiValues.push_back(120*DegToRad);// 11
0062     phiValues.push_back(130*DegToRad);// 12
0063     phiValues.push_back(140*DegToRad);// 13
0064     phiValues.push_back(150*DegToRad);// 14
0065     //need one less phiValue than radial segments
0066     //phiValues.push_back(160*DegToRad);// 15
0067 
0068     seg.setGridRValues(rValues);
0069     seg.setGridPhiValues(phiValues);
0070     seg.setOffsetPhi(-M_PI);
0071 
0072     std::vector<TestTuple> tests;
0073     tests.push_back( TestTuple( 10.0, -180*DegToRad,   0,   0 ) );
0074     tests.push_back( TestTuple( 10.1,  175*DegToRad,   0,  35 ) );
0075     tests.push_back( TestTuple( 20.0,   22*DegToRad,   1,  10 ) );
0076     tests.push_back( TestTuple( 20.1,   40*DegToRad,   1,  11 ) );
0077     tests.push_back( TestTuple( 15.0,  359*DegToRad,   0,  17 ) );
0078     tests.push_back( TestTuple( 42.0,   42*DegToRad,   3,   5 ) );
0079     tests.push_back( TestTuple( 22.0,    0*DegToRad,   1,   9 ) );
0080     tests.push_back( TestTuple( 22.0,  180*DegToRad,   1,  18 ) );
0081     tests.push_back( TestTuple( 22.0,  -180*DegToRad,   1,  0 ) );
0082     tests.push_back( TestTuple(  10.1, -179*DegToRad,     0,   0 ) );
0083     tests.push_back( TestTuple(  20.1, -179*DegToRad,     1,   0 ) );
0084     tests.push_back( TestTuple(  30.1, -179*DegToRad,     2,   0 ) );
0085     tests.push_back( TestTuple(  40.1, -179*DegToRad,     3,   0 ) );
0086     tests.push_back( TestTuple(  50.1, -179*DegToRad,     4,   0 ) );
0087     tests.push_back( TestTuple(  60.1, -179*DegToRad,     5,   0 ) );
0088     tests.push_back( TestTuple(  70.1, -179*DegToRad,     6,   0 ) );
0089     tests.push_back( TestTuple(  80.1, -179*DegToRad,     7,   0 ) );
0090     tests.push_back( TestTuple(  90.1, -179*DegToRad,     8,   0 ) );
0091     tests.push_back( TestTuple( 100.1, -179*DegToRad,     9,   0 ) );
0092     tests.push_back( TestTuple( 110.1, -179*DegToRad,    10,   0 ) );
0093     tests.push_back( TestTuple( 120.1, -179*DegToRad,    11,   0 ) );
0094     tests.push_back( TestTuple( 130.1, -179*DegToRad,    12,   0 ) );
0095     tests.push_back( TestTuple( 140.1, -179*DegToRad,    13,   0 ) );
0096     tests.push_back( TestTuple( 150.1, -179*DegToRad,    14,   0 ) );
0097     tests.push_back( TestTuple( 160.0, -179*DegToRad,    14,   0 ) );
0098 
0099 
0100     dd4hep::DDSegmentation::VolumeID volID { 0 };
0101 
0102     //Test from position to cellID
0103     for(std::vector<TestTuple>::const_iterator it = tests.begin(); it != tests.end(); ++it) {
0104 
0105       const double r     = (*it)._r;
0106       const double phi   = (*it)._p;
0107       const long long rB = (*it)._rB;
0108       const long long pB = (*it)._pB;
0109 
0110       dd4hep::DDSegmentation::Vector3D locPos ( r*cos(phi), r*sin(phi), 0.0);
0111       dd4hep::DDSegmentation::Vector3D globPos( r*cos(phi), r*sin(phi), 0.0);
0112 
0113       dd4hep::DDSegmentation::CellID cid = seg.cellID(locPos, globPos, volID);
0114 
0115       const dd4hep::FieldID phiShifted(pB << phiBitOffset);
0116       const dd4hep::FieldID rShifted  (rB << rBitOffset);
0117       const dd4hep::CellID  expectedID(rShifted + phiShifted);
0118 
0119       test( expectedID , cid , " Test get ID From Position" );
0120 
0121       std::cout << std::setw(20) <<  " "
0122                 << std::setw(20) <<  "rBin     "
0123                 << std::setw(20) <<  "pBin     "
0124                 << std::endl;
0125 
0126       std::cout << std::setw(20) <<  "Expected"
0127                 << std::setw(20) <<  rB
0128                 << std::setw(20) <<  pB
0129                 << std::endl;
0130 
0131       std::cout << std::setw(20) <<  "Calculated"
0132                 << std::setw(20) <<  seg.decoder()->get(cid,"r")
0133                 << std::setw(20) <<  seg.decoder()->get(cid,"phi")
0134                 << std::endl;
0135 
0136     }
0137 
0138 
0139 
0140     std::vector<TestTuple> testPositions;
0141     testPositions.push_back( TestTuple( 15.0, -175*DegToRad,   0,   0 ) );
0142     testPositions.push_back( TestTuple( 15.0,  175*DegToRad,   0,  35 ) );
0143     testPositions.push_back( TestTuple( 25.0, -150*DegToRad,   1,  1 ) );
0144     testPositions.push_back( TestTuple( 35.0, -135*DegToRad,   2,  1 ) );
0145     testPositions.push_back( TestTuple( 45.0, -120*DegToRad,   3,  1 ) );
0146     testPositions.push_back( TestTuple( 55.0, -105*DegToRad,   4,  1 ) );
0147     testPositions.push_back( TestTuple( 65.0,  -90*DegToRad,   5,  1 ) );
0148     testPositions.push_back( TestTuple( 75.0,  -75*DegToRad,   6,  1 ) );
0149     testPositions.push_back( TestTuple( 85.0,  -60*DegToRad,   7,  1 ) );
0150     testPositions.push_back( TestTuple( 95.0,  -45*DegToRad,   8,  1 ) );
0151     testPositions.push_back( TestTuple( 105.0, -30*DegToRad,   9,  1 ) );
0152     testPositions.push_back( TestTuple( 115.0, -15*DegToRad,  10,  1 ) );
0153     testPositions.push_back( TestTuple( 125.0,  -0*DegToRad,  11,  1 ) );
0154     testPositions.push_back( TestTuple( 135.0,  15*DegToRad,  12,  1 ) );
0155     testPositions.push_back( TestTuple( 145.0,  30*DegToRad,  13,  1 ) );
0156     testPositions.push_back( TestTuple( 155.0,  45*DegToRad,  14,  1 ) );
0157     testPositions.push_back( TestTuple(155.0, -105*DegToRad,  14,   0 ) );
0158 
0159     //Test from cellID to position
0160     for(std::vector<TestTuple>::const_iterator it = testPositions.begin(); it != testPositions.end(); ++it) {
0161 
0162       const double r     = (*it)._r;
0163       const double phi   = (*it)._p;
0164       const long long rB = (*it)._rB;
0165       const long long pB = (*it)._pB;
0166 
0167       dd4hep::DDSegmentation::CellID cellID  { 0 };
0168 
0169       seg.decoder()->set(cellID,"r"  , rB);
0170       seg.decoder()->set(cellID,"phi", pB);
0171 
0172       std::cout << "CellID: " << cellID  << std::endl;
0173 
0174       dd4hep::DDSegmentation::Vector3D expectedPosition( r*cos(phi), r*sin(phi), 0.0);
0175       dd4hep::DDSegmentation::Vector3D calculatedPosition = seg.position(cellID);
0176 
0177       test( fabs(expectedPosition.x() - calculatedPosition.x())  < 1e-11, " Test get Position from ID: X" );
0178       test( fabs(expectedPosition.y() - calculatedPosition.y())  < 1e-11, " Test get Position from ID: Y" );
0179       test( fabs(expectedPosition.z() - calculatedPosition.z())  < 1e-11, " Test get Position from ID: Z" );
0180 
0181       std::cout << std::setw(20) <<  " "
0182                 << std::setw(20) <<  "r     "
0183                 << std::setw(20) <<  "phi     "
0184                 << std::endl;
0185 
0186       std::cout << std::setw(20) <<  "Expected"
0187                 << std::setw(20) <<  r
0188                 << std::setw(20) <<  phi/DegToRad
0189                 << std::endl;
0190 
0191       const double rCalc =
0192         sqrt( calculatedPosition.x() * calculatedPosition.x() +
0193               calculatedPosition.y() * calculatedPosition.y() );
0194       const double pCalc = atan2( calculatedPosition.y(), calculatedPosition.x() );
0195 
0196       std::cout << std::setw(20) <<  "Calculated"
0197                 << std::setw(20) <<  rCalc
0198                 << std::setw(20) <<  pCalc/DegToRad
0199                 << std::endl;
0200 
0201     }
0202 
0203 
0204 
0205   } catch( std::exception &e ){
0206     //} catch( ... ){
0207 
0208     test.log( e.what() );
0209     test.error( "exception occurred" );
0210   }
0211   return 0;
0212 }