Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:05:34

0001 #include "DetectorDescription/RegressionTest/interface/DDCompareTools.h"
0002 
0003 #include <cstddef>
0004 #include <cmath>
0005 #include <iomanip>
0006 #include <iostream>
0007 #include <string>
0008 #include <utility>
0009 
0010 #include "DetectorDescription/Core/interface/DDCompactView.h"
0011 #include "DetectorDescription/Core/interface/DDExpandedView.h"
0012 #include "DetectorDescription/Core/interface/DDName.h"
0013 #include "DetectorDescription/Core/interface/DDPosData.h"
0014 #include "DetectorDescription/Core/interface/DDSolidShapes.h"
0015 #include "Math/GenVector/Rotation3D.h"
0016 
0017 bool DDCompareEPV(DDExpandedView& lhs, DDExpandedView& rhs, const DDCompOptions& ddco) {
0018   bool ret(true);
0019 
0020   std::cout << "*********FIRST BY firstChild, firstChild, nextSibling, nextSibling*********" << std::endl;
0021   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno() << " !=? " << rhs.logicalPart().name().name()
0022             << ":" << rhs.copyno() << std::endl;
0023   lhs.firstChild();
0024   rhs.firstChild();
0025   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno() << " !=? " << rhs.logicalPart().name().name()
0026             << ":" << rhs.copyno() << std::endl;
0027   lhs.firstChild();
0028   rhs.firstChild();
0029   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno() << " !=? " << rhs.logicalPart().name().name()
0030             << ":" << rhs.copyno() << std::endl;
0031   lhs.nextSibling();
0032   rhs.nextSibling();
0033   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno() << " !=? " << rhs.logicalPart().name().name()
0034             << ":" << rhs.copyno() << std::endl;
0035   lhs.nextSibling();
0036   rhs.nextSibling();
0037   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno() << " !=? " << rhs.logicalPart().name().name()
0038             << ":" << rhs.copyno() << std::endl;
0039   lhs.parent();
0040   rhs.parent();
0041   std::cout << "*********THEN BY next, next, next, next*********" << std::endl;
0042   lhs.parent();
0043   rhs.parent();
0044   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno() << " !=? " << rhs.logicalPart().name().name()
0045             << ":" << rhs.copyno() << std::endl;
0046   lhs.next();
0047   rhs.next();
0048   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno() << " !=? " << rhs.logicalPart().name().name()
0049             << ":" << rhs.copyno() << std::endl;
0050   lhs.next();
0051   rhs.next();
0052   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno() << " !=? " << rhs.logicalPart().name().name()
0053             << ":" << rhs.copyno() << std::endl;
0054   std::cout << lhs.depth() << " depth " << rhs.depth() << std::endl;
0055   lhs.next();
0056   rhs.next();
0057   std::cout << lhs.depth() << " depth " << rhs.depth() << std::endl;
0058   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno() << " !=? " << rhs.logicalPart().name().name()
0059             << ":" << rhs.copyno() << std::endl;
0060   return ret;
0061 }
0062 
0063 bool DDCompareCPV(const DDCompactView& lhs, const DDCompactView& rhs, const DDCompOptions& ddco) {
0064   bool ret(true);
0065 
0066   const auto& g1 = lhs.graph();
0067   const auto& g2 = rhs.graph();
0068 
0069   using Graph = DDCompactView::Graph;
0070   using adjl_iterator = Graph::const_adj_iterator;
0071 
0072   adjl_iterator git1 = g1.begin();
0073   adjl_iterator gend1 = g1.end();
0074   adjl_iterator git2 = g2.begin();
0075   adjl_iterator gend2 = g2.end();
0076 
0077   Graph::index_type i = 0;
0078 
0079   while (git1 != gend1 && git2 != gend2 && ret) {
0080     const DDLogicalPart& ddLP1 = g1.nodeData(git1);
0081     const DDLogicalPart& ddLP2 = g2.nodeData(git2);
0082     std::cout << ++i << " P " << ddLP1.name() << " " << ddLP2.name() << std::endl;
0083     if (!DDCompareLP(ddLP1, ddLP2, ddco)) {
0084       ret = false;
0085       break;
0086     } else if (!git1->empty() && !git2->empty()) {
0087       auto cit1 = git1->begin();
0088       auto cend1 = git1->end();
0089       auto cit2 = git2->begin();
0090       auto cend2 = git2->end();
0091 
0092       while (cit1 != cend1 && cit2 != cend2) {
0093         const DDLogicalPart& ddcurLP1 = g1.nodeData(cit1->first);
0094         const DDLogicalPart& ddcurLP2 = g2.nodeData(cit2->first);
0095         std::cout << ++i << " c1--> " << g1.edgeData(cit1->second)->copyno() << " " << ddcurLP1.name().fullname()
0096                   << std::endl;
0097         std::cout << ++i << " c2--> " << g2.edgeData(cit2->second)->copyno() << " " << ddcurLP2.name().fullname()
0098                   << std::endl;
0099         const DDPosData* p1(g1.edgeData(cit1->second));
0100         const DDPosData* p2(g2.edgeData(cit2->second));
0101 
0102         if (p1->copyno() != p2->copyno() || !DDCompareLP(ddcurLP1, ddcurLP2, ddco)) {
0103           std::cout << "Failed to match node (fullname:copy_no): 1: " << ddcurLP1.name().fullname() << ":"
0104                     << p1->copyno() << " 2: " << ddcurLP2.name().fullname() << ":" << p2->copyno() << std::endl;
0105           ret = false;
0106           break;
0107         } else if (!DDCompareDDTrans(p1->trans(), p2->trans())) {
0108           std::cout << "Failed to match translation " << std::endl;
0109           ret = false;
0110           break;
0111         } else if (!DDCompareDDRot(p1->ddrot(), p2->ddrot(), ddco)) {
0112           std::cout << "Failed to match rotation " << std::endl;
0113           ret = false;
0114           break;
0115         }
0116         ++cit1;
0117         ++cit2;
0118       }
0119     } else if (git1->size() != git2->size()) {
0120       ret = false;
0121       std::cout << "DDCompactViews are different because number of children do not match" << std::endl;
0122       std::cout << "graph1 size of edge_list: " << git1->size() << " and graph2 size of edge_list: " << git2->size()
0123                 << std::endl;
0124       break;
0125     }
0126     ++git1;
0127     ++git2;
0128   }
0129   return ret;
0130 }
0131 
0132 bool DDCompareLP(const DDLogicalPart& lhs, const DDLogicalPart& rhs, const DDCompOptions& ddco) {
0133   bool ret(true);
0134   // for a logical part to be equal, the solid must be equal and the name must be equal.
0135   if (lhs.name().fullname() != rhs.name().fullname()) {
0136     ret = false;
0137     std::cout << "LogicalPart names do not match " << lhs.name().fullname() << " and " << rhs.name().fullname()
0138               << std::endl;
0139   } else if (!DDCompareSolid(lhs.solid(), rhs.solid(), ddco)) {
0140     ret = false;
0141     std::cout << "LogicalPart Solids do not match " << lhs.name().fullname() << " and " << rhs.name().fullname()
0142               << std::endl;
0143   }
0144   return ret;
0145 }
0146 
0147 bool DDCompareSolid(const DDSolid& lhs, const DDSolid& rhs, const DDCompOptions& ddco) {
0148   bool ret(true);
0149   switch (lhs.shape()) {
0150     case DDSolidShape::dd_not_init:
0151     case DDSolidShape::ddbox:
0152     case DDSolidShape::ddtubs:
0153     case DDSolidShape::ddcuttubs:
0154     case DDSolidShape::ddtrap:
0155     case DDSolidShape::ddcons:
0156     case DDSolidShape::ddpolycone_rz:
0157     case DDSolidShape::ddpolyhedra_rz:
0158     case DDSolidShape::ddpolycone_rrz:
0159     case DDSolidShape::ddpolyhedra_rrz:
0160     case DDSolidShape::ddextrudedpolygon:
0161     case DDSolidShape::ddtorus:
0162     case DDSolidShape::ddpseudotrap:
0163     case DDSolidShape::ddtrunctubs:
0164     case DDSolidShape::ddsphere:
0165     case DDSolidShape::ddellipticaltube:
0166     case DDSolidShape::ddshapeless: {
0167       if (lhs.name().fullname() != rhs.name().fullname()) {
0168         ret = false;
0169         std::cout << "Solid names do not match for solid " << lhs.name().fullname() << " and " << rhs.name().fullname()
0170                   << std::endl;
0171       } else if (lhs.shape() != rhs.shape()) {
0172         ret = false;
0173         std::cout << "Shape types do not match for solids " << lhs.name().fullname() << " and " << rhs.name().fullname()
0174                   << " even though their names match " << std::endl;
0175       } else if (!DDCompareDBLVEC(lhs.parameters(), rhs.parameters())) {
0176         ret = false;
0177         std::cout << "Parameters do not match for solids " << lhs.name().fullname() << " and " << rhs.name().fullname()
0178                   << " even though their names and shape type match." << std::endl;
0179         std::cout << "size: " << lhs.parameters().size() << " " << rhs.parameters().size() << std::endl;
0180       }
0181       break;
0182     }
0183     case DDSolidShape::ddunion:
0184     case DDSolidShape::ddsubtraction:
0185     case DDSolidShape::ddintersection: {
0186       if (!DDCompareBoolSol(lhs, rhs, ddco)) {
0187         ret = false;
0188       }
0189       break;
0190     }
0191     default:
0192       break;
0193   }
0194   return ret;
0195 }
0196 
0197 // Default tolerance was 0.0004
0198 bool DDCompareDBLVEC(const std::vector<double>& lhs, const std::vector<double>& rhs, double tol) {
0199   bool ret(true);
0200   std::ios_base::fmtflags originalFlags = std::cout.flags();
0201   int originalPrecision = std::cout.precision();
0202   if (lhs.size() != rhs.size()) {
0203     ret = false;
0204     std::cout << "Size of vectors do not match." << std::endl;
0205   } else {
0206     for (size_t i = 0; i < lhs.size(); ++i) {
0207       if (std::fabs(lhs[i] - rhs[i]) > tol) {
0208         ret = false;
0209         std::cout << "Vector content at index " << i << " does not match ";
0210         std::cout << std::setw(12) << std::fixed << std::setprecision(4) << lhs[i] << " != " << rhs[i] << std::endl;
0211         break;
0212       }
0213     }
0214   }
0215   // Now set everything back to defaults
0216   std::cout.flags(originalFlags);
0217   std::cout.precision(originalPrecision);
0218   return ret;
0219 }
0220 
0221 bool DDCompareBoolSol(const DDBooleanSolid& lhs, const DDBooleanSolid& rhs, const DDCompOptions& ddco) {
0222   bool ret(true);
0223   if (lhs.name().fullname() != rhs.name().fullname()) {
0224     ret = false;
0225     std::cout << "BooleanSolid names do not match ";
0226   } else if (lhs.shape() != rhs.shape()) {
0227     ret = false;
0228     std::cout << "BooleanSolid shape types do not match ";
0229   } else if (!DDCompareDBLVEC(lhs.parameters(), rhs.parameters(), ddco.distTol_)) {
0230     ret = false;
0231     std::cout << "BooleanSolid parameters do not match ";
0232   } else if (!DDCompareSolid(lhs.solidA(), rhs.solidA(), ddco)) {
0233     ret = false;
0234     std::cout << "BooleanSolid SolidA solids do not match ";
0235   } else if (!DDCompareSolid(lhs.solidB(), rhs.solidB(), ddco)) {
0236     ret = false;
0237     std::cout << "BooleanSolid SolidB solids do not match ";
0238   } else if (!DDCompareDDTrans(lhs.translation(), rhs.translation(), ddco.distTol_)) {
0239     ret = false;
0240     std::cout << "BooleanSolid Translations do not match ";
0241   } else if (!DDCompareDDRot(lhs.rotation(), rhs.rotation(), ddco)) {
0242     ret = false;
0243     std::cout << "BooleanSolid Rotations do not match ";
0244   }
0245   if (!ret) {
0246     std::cout << "for boolean solids " << lhs.name().fullname() << " and " << rhs.name().fullname() << std::endl;
0247   }
0248   return ret;
0249 }
0250 
0251 // Default tolerance was 0.0004
0252 bool DDCompareDDTrans(const DDTranslation& lhs, const DDTranslation& rhs, double tol) {
0253   bool ret(true);
0254   if (std::fabs(lhs.x() - rhs.x()) > tol || std::fabs(lhs.y() - rhs.y()) > tol || std::fabs(lhs.z() - rhs.z()) > tol) {
0255     ret = false;
0256   }
0257   return ret;
0258 }
0259 
0260 bool DDCompareDDRot(const DDRotation& lhs, const DDRotation& rhs, const DDCompOptions& ddco) {
0261   bool ret(true);
0262   if (ddco.compRotName_ && lhs.name().fullname() != rhs.name().fullname()) {
0263     ret = false;
0264     std::cout << "DDRotation names do not match " << lhs.name().fullname() << " and " << rhs.name().fullname()
0265               << std::endl;
0266   } else if (!DDCompareDDRotMat(lhs.rotation(), rhs.rotation())) {
0267     ret = false;
0268     std::cout << "DDRotationMatrix values do not match " << lhs.name().fullname() << " and " << rhs.name().fullname()
0269               << std::endl;
0270   }
0271   return ret;
0272 }
0273 
0274 // Default tolerance was 0.0004
0275 bool DDCompareDDRotMat(const DDRotationMatrix& lhs, const DDRotationMatrix& rhs, double tol) {
0276   bool ret(true);
0277   // manual way to do it... postponed.  Tested with Distance method from root::math
0278   //DD3Vector x1, y1, z1;
0279   //lhs.GetComponents(x1,y1,z1);
0280   //DD3Vector x2, y2, z2;
0281   //rhs.GetComponents(x2,y2,z2);
0282   double dist = Distance(lhs, rhs);
0283   if (std::fabs(dist) > tol) {
0284     std::cout << "Rotation matrices do not match." << std::endl;
0285     ret = false;
0286     DD3Vector x, y, z;
0287     std::cout << "FIRST" << std::endl;
0288     lhs.GetComponents(x, y, z);
0289     std::cout << std::setw(12) << std::fixed << std::setprecision(4) << x.X();
0290     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << y.X();
0291     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << z.X();
0292     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << x.Y();
0293     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << y.Y();
0294     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << z.Y();
0295     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << x.Z();
0296     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << y.Z();
0297     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << z.Z() << std::endl;
0298     std::cout << "SECOND" << std::endl;
0299     rhs.GetComponents(x, y, z);
0300     std::cout << std::setw(12) << std::fixed << std::setprecision(4) << x.X();
0301     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << y.X();
0302     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << z.X();
0303     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << x.Y();
0304     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << y.Y();
0305     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << z.Y();
0306     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << x.Z();
0307     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << y.Z();
0308     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << z.Z() << std::endl;
0309   }
0310 
0311   return ret;
0312 }