CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/DetectorDescription/RegressionTest/src/DDCompareTools.cc

Go to the documentation of this file.
00001 #include "DetectorDescription/RegressionTest/interface/DDCompareTools.h"
00002 #include "DetectorDescription/Core/interface/DDSolidShapes.h"
00003 
00004 DDCompareEPV::DDCompareEPV() : ddco_() { }
00005 
00006 DDCompareEPV::DDCompareEPV(const DDCompOptions& ddco) : ddco_(ddco) { }
00007 
00008 bool DDCompareEPV::operator()(DDExpandedView& lhs, DDExpandedView& rhs) const {
00009   bool ret(true);
00010   /* struct DDCompareCPVGraph : public std::binary_function<DDCompactView::graph_type, DDCompactView::graph_type, bool> { */
00011   /*   bool operator()(const DDCompactView::graph_type& lhs, const DDCompactView::graph_type& rhs) const { */
00012   /*     bool ret; */
00013   //    const graph<DDLogicalPart, DDPosData*>& g1= lhs;
00014   //    graph<DDLogicalPart, DDPosData*>::const_iterator beg1 = g1.begin();
00015   //DDCompactView::graph_type::const_iterator beg1 = lhs.begin();
00016   /*     std::cout << "size of lhs = " << lhs.edge_size() << std::endl;     */
00017   /*     std::cout << "size of rhs = " << rhs.edge_size() << std::endl;     */
00018   /*     std::cout << "iterating over lhs edges?" << std::endl; */
00019   /*     graph<DDLogicalPart, DDPosData*>::const_iterator beg1(lhs), end1(lhs); */
00020   /*     beg1 = lhs.begin_iter(); */
00021   /*     end1 = lhs.end_iter(); */
00022   /*     size_t countem(0); */
00023   /*     for ( ; beg1 != end1 ; ++beg1 ) { */
00024   /*       ++countem; */
00025   /*     } */
00026   /*     std::cout << "between lhs.begin_iter() and lhs.end_iter() there are " << countem << std::endl; */
00027   /*     graph<DDLogicalPart,DDPosData*>::const_edge_range cer = lhs.graph().edges(lhs.root()); */
00028   /*     graph<DDLogicalPart,DDPosData*>::adj_iterator i = const_edge_range.first */
00029   //    DDExpandedView v1(lhs), v2(rhs);
00030   //    if (v1.next()) std::cout << "v1 is true" << std::endl;
00031   //    if (v2.next()) std::cout << "v2 is true" << std::endl;
00032   //    DDNodeComparator ddnc;
00033   //if ( ddnc (v1, v2) ) { 
00034   //  ret = true;
00035   //}
00036   /*     while (v1.next()) { */
00037   /*       if (v2.next()) { */
00038   /*    std::cout << v1.logicalPart().name().name() << ":" << v1.copyno() */
00039   /*              << " !=? " << v2.logicalPart().name().name() << ":" << v2.copyno() */
00040   /*              << std::endl; */
00041         
00042   /*    if ( v1.logicalPart().name().name() != v2.logicalPart().name().name() ) { */
00043   /*      std::cout << v1.logicalPart().name().name() << ":" << v1.copyno() */
00044   /*                << " !=? " << v2.logicalPart().name().name() << ":" << v2.copyno() */
00045   /*                << std::endl; */
00046   /*    //        std::cout << v1.logicalPart().name().name() << " !=? " << v2.logicalPart().name().name() << std::endl; */
00047   /*      ret=false; */
00048   /*      break; */
00049   /*    } */
00050   /*       } */
00051   /*     } */
00052   std::cout <<"*********FIRST BY firstChild, firstChild, nextSibling, nextSibling*********" << std::endl;
00053   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno()
00054             << " !=? " << rhs.logicalPart().name().name() << ":" << rhs.copyno()
00055             << std::endl;
00056   lhs.firstChild();
00057   rhs.firstChild();
00058   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno()
00059             << " !=? " << rhs.logicalPart().name().name() << ":" << rhs.copyno()
00060             << std::endl;
00061   lhs.firstChild();
00062   rhs.firstChild();
00063   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno()
00064             << " !=? " << rhs.logicalPart().name().name() << ":" << rhs.copyno()
00065             << std::endl;
00066   lhs.nextSibling();
00067   rhs.nextSibling();
00068   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno()
00069             << " !=? " << rhs.logicalPart().name().name() << ":" << rhs.copyno()
00070             << std::endl;
00071   lhs.nextSibling();
00072   rhs.nextSibling();
00073   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno()
00074             << " !=? " << rhs.logicalPart().name().name() << ":" << rhs.copyno()
00075             << std::endl;
00076   lhs.parent();
00077   rhs.parent();
00078   std::cout <<"*********THEN BY next, next, next, next*********" << std::endl;
00079   lhs.parent();
00080   rhs.parent();
00081   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno()
00082             << " !=? " << rhs.logicalPart().name().name() << ":" << rhs.copyno()
00083             << std::endl;
00084   lhs.next();
00085   rhs.next();
00086   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno()
00087             << " !=? " << rhs.logicalPart().name().name() << ":" << rhs.copyno()
00088             << std::endl;
00089   lhs.next();
00090   rhs.next();
00091   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno()
00092             << " !=? " << rhs.logicalPart().name().name() << ":" << rhs.copyno()
00093             << std::endl;
00094   std::cout << lhs.depth() << " depth " << rhs.depth() << std::endl;
00095   lhs.next();
00096   rhs.next();
00097   std::cout << lhs.depth() << " depth " << rhs.depth() << std::endl;
00098   std::cout << lhs.logicalPart().name().name() << ":" << lhs.copyno()
00099             << " !=? " << rhs.logicalPart().name().name() << ":" << rhs.copyno()
00100             << std::endl;
00101   return ret;
00102 }
00103 
00104 DDCompareCPV::DDCompareCPV() : ddco_() { }
00105 
00106 DDCompareCPV::DDCompareCPV(const DDCompOptions& ddco) : ddco_(ddco) { }
00107 
00108 bool DDCompareCPV::operator()(const DDCompactView& lhs, const DDCompactView& rhs) const {
00109   bool ret(true);
00110 
00111   const DDCompactView::graph_type & g1 = lhs.graph();
00112   const DDCompactView::graph_type & g2 = rhs.graph();
00113 
00114   typedef DDCompactView::graph_type::const_adj_iterator adjl_iterator;
00115   adjl_iterator git1 = g1.begin();
00116   adjl_iterator gend1 = g1.end();
00117   adjl_iterator git2 = g2.begin();
00118   adjl_iterator gend2 = g2.end();
00119   /*     std::cout << "uniqueness test: " << &(*git1) << " != " << &(*git2)  */
00120   /*          << " and " << &(*gend1) << " != " << &(*gend2) << std::endl; */
00121   //    DDCompactView::graph_type::const_iterator bit = g1.begin_iter();
00122   DDCompactView::graph_type::index_type i=0;
00123   //    for (; git1 != gend1; ++git1) {
00124   while ( git1 != gend1 && git2 != gend2 && ret ) {
00125     const DDLogicalPart & ddLP1 = g1.nodeData(git1);
00126     const DDLogicalPart & ddLP2 = g2.nodeData(git2);
00127     std::cout << ++i << " P " << ddLP1.name() << " " << ddLP2.name() << std::endl;
00128     if ( ! DDCompareLP(ddco_)(ddLP1, ddLP2) ) {
00129       ret = false;
00130       break;
00131     } else if (git1->size() && git2->size() ) { 
00132       DDCompactView::graph_type::edge_list::const_iterator cit1  = git1->begin();
00133       DDCompactView::graph_type::edge_list::const_iterator cend1 = git1->end();
00134       DDCompactView::graph_type::edge_list::const_iterator cit2  = git2->begin();
00135       DDCompactView::graph_type::edge_list::const_iterator cend2 = git2->end();
00136       //for (; cit != cend; ++cit) {
00137       while ( cit1 != cend1 && cit2 != cend2 ) {
00138         const DDLogicalPart & ddcurLP1 = g1.nodeData(cit1->first);
00139         const DDLogicalPart & ddcurLP2 = g2.nodeData(cit2->first);
00140         std::cout << ++i << " c1--> " << g1.edgeData(cit1->second)->copyno_ << " " << ddcurLP1.name().fullname() << std::endl;
00141         std::cout << ++i << " c2--> " << g2.edgeData(cit2->second)->copyno_ << " " << ddcurLP2.name().fullname() << std::endl;
00142         const DDPosData* p1(g1.edgeData(cit1->second));
00143         const DDPosData* p2(g2.edgeData(cit2->second));
00144         //        if ( g1.edgeData(cit1->second)->copyno_ != g2.edgeData(cit2->second)->copyno_
00145 //      if ( p1->copyno_ != p2->copyno_
00146 //           || ddcurLP1.name().fullname() != ddcurLP2.name().fullname() ) {
00147 //        std::cout << "Failed to match node (fullname:copy_no): 1: " 
00148 //                  << ddcurLP1.name().fullname() << ":" << p1->copyno_ << " 2: " 
00149 //                  << ddcurLP2.name().fullname() << ":" << p2->copyno_ << std::endl;
00150 //        ret = false;
00151 //        break;
00152         if ( p1->copyno_ != p2->copyno_ || 
00153              ! DDCompareLP(ddco_)(ddcurLP1,ddcurLP2) ) {
00154           std::cout << "Failed to match node (fullname:copy_no): 1: " 
00155                     << ddcurLP1.name().fullname() << ":" << p1->copyno_ << " 2: " 
00156                     << ddcurLP2.name().fullname() << ":" << p2->copyno_ << std::endl;
00157           ret = false;
00158           break;
00159         } else if ( ! DDCompareDDTrans()(p1->trans_, p2->trans_) ) {
00160           std::cout << "Failed to match translation " << std::endl;
00161 //        std::cout << "1: " << std::setw(12) << std::fixed << std::setprecision(4) << p1->trans_.x();
00162 //        std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << p1->trans_.y();
00163 //        std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << p1->trans_.z() << std::endl;
00164 //        std::cout << "2: " << std::setw(12) << std::fixed << std::setprecision(4) << p2->trans_.x();
00165 //        std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << p2->trans_.y();
00166 //        std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << p2->trans_.z() << std::endl;
00167           ret = false;
00168           break;
00169         } else if ( ! DDCompareDDRot(ddco_)(p1->rot_, p2->rot_) ) {
00170           std::cout << "Failed to match rotation " << std::endl;
00171           ret = false;
00172           break;
00173         }
00174         ++cit1;
00175         ++cit2;
00176       }
00177     } else if ( git1->size() != git2->size() ) {
00178       ret = false;
00179       std::cout << "DDCompactViews are different because number of children do not match" << std::endl;
00180       std::cout << "graph1 size of edge_list: " << git1->size() << " and graph2 size of edge_list: " << git2->size() << std::endl;
00181       break;
00182     }
00183     ++git1;
00184     ++git2;
00185   }
00186   return ret;
00187 }
00188 
00189 DDCompareLP::DDCompareLP() : ddco_() { }
00190 
00191 DDCompareLP::DDCompareLP(const DDCompOptions& ddco) : ddco_(ddco) { }
00192 
00193 bool DDCompareLP::operator()(const DDLogicalPart& lhs, const DDLogicalPart& rhs) const {
00194   bool ret(true);
00195   // for a logical part to be equal, the solid must be equal and the name must be equal.
00196   if ( lhs.name().fullname() != rhs.name().fullname() ) {
00197     ret = false;
00198     std::cout << "LogicalPart names do not match " << lhs.name().fullname() 
00199               << " and " << rhs.name().fullname() << std::endl;
00200   } else if ( ! DDCompareSolid(ddco_)(lhs.solid(), rhs.solid()) ){
00201     ret = false;
00202     std::cout << "LogicalPart Solids do not match " << lhs.name().fullname() 
00203               << " and " << rhs.name().fullname() << std::endl;
00204   }
00205   return ret;
00206 }
00207 
00208 DDCompareSolid::DDCompareSolid() : ddco_() { }
00209 
00210 DDCompareSolid::DDCompareSolid(const DDCompOptions& ddco) : ddco_(ddco) { }
00211 
00212 bool DDCompareSolid::operator()(const DDSolid& lhs, const DDSolid& rhs) const {
00213   bool ret(true);
00214     switch ( lhs.shape() ) {
00215     case dd_not_init:
00216     case ddbox:
00217     case ddtubs:
00218     case ddtrap: 
00219     case ddcons:
00220     case ddpolycone_rz:
00221     case ddpolyhedra_rz:
00222     case ddpolycone_rrz:
00223     case ddpolyhedra_rrz:
00224     case ddtorus:
00225     case ddpseudotrap:
00226     case ddtrunctubs:
00227     case ddsphere:
00228     case ddorb:
00229     case ddellipticaltube:
00230     case ddellipsoid:
00231     case ddparallelepiped:
00232     case ddshapeless: 
00233       {
00234         if ( lhs.name().fullname() != rhs.name().fullname() ) {
00235           ret = false;
00236           std::cout << "Solid names do not match for solid " << lhs.name().fullname() << " and " << rhs.name().fullname() << std::endl;
00237         } else if ( lhs.shape() != rhs.shape() ) {
00238           ret = false;
00239           std::cout << "Shape types do not match for solids " << lhs.name().fullname() 
00240                     << " and " << rhs.name().fullname() 
00241                     << " even though their names match " << std::endl;
00242         } else if ( ! DDCompareDBLVEC()(lhs.parameters(), rhs.parameters()) ) {
00243           ret = false;
00244           std::cout << "Parameters do not match for solids " << lhs.name().fullname() 
00245                     << " and " << rhs.name().fullname() 
00246                     << " even though their names and shape type match." << std::endl;
00247           std::cout << "size: " << lhs.parameters().size() << " " << rhs.parameters().size() << std::endl;
00248         } 
00249         break;
00250       }
00251     case ddunion:
00252     case ddsubtraction:
00253     case ddintersection: 
00254       {
00255         if ( ! DDCompareBoolSol(ddco_)(lhs, rhs) ) {
00256           ret = false;
00257         }
00258         break;
00259       }
00260     case ddreflected:
00261       {
00262         DDReflectionSolid rs1(lhs);
00263         DDReflectionSolid rs2(rhs);
00264         if ( ! DDCompareSolid(ddco_)( rs1.unreflected(), rs2.unreflected()) ) {
00265           ret = false;
00266           std::cout << "Unreflected volumes of DDReflections do not match. Reflections are " 
00267                     << lhs.name().fullname() << " and " << rhs.name().fullname() << std::endl;
00268         }
00269         break;
00270       }
00271     default:
00272       break;
00273     }
00274     return ret;
00275 }
00276 
00277 DDCompareDBLVEC::DDCompareDBLVEC() : tol_(0.0004) { }
00278 
00279 DDCompareDBLVEC::DDCompareDBLVEC(double tol) : tol_(tol) { }
00280 
00281 bool
00282 DDCompareDBLVEC::operator() ( const std::vector<double>& lhs, const std::vector<double>& rhs ) const
00283 {
00284   bool ret(true);
00285   std::ios_base::fmtflags originalFlags = std::cout.flags();
00286   int originalPrecision = std::cout.precision();
00287   if( lhs.size() != rhs.size())
00288   {
00289     ret = false;
00290     std::cout << "Size of vectors do not match." << std::endl;
00291   }
00292   else
00293   {
00294     for( size_t i = 0; i < lhs.size() ; ++i )
00295     {
00296       if( std::fabs( lhs[i] - rhs[i] ) > tol_ )
00297       {
00298         ret = false;
00299         std::cout << "Vector content at index " << i << " does not match " ;
00300         std::cout << std::setw(12) << std::fixed << std::setprecision(4) << lhs[i] << " != " << rhs[i] << std::endl;
00301         break;
00302       }
00303     }
00304   }
00305   // Now set everything back to defaults
00306   std::cout.flags( originalFlags );
00307   std::cout.precision( originalPrecision );
00308   return ret;
00309 }
00310 
00311 DDCompareBoolSol::DDCompareBoolSol() : ddco_() { }
00312 
00313 DDCompareBoolSol::DDCompareBoolSol(const DDCompOptions& ddco) : ddco_(ddco) { }
00314 bool DDCompareBoolSol::operator() ( const DDBooleanSolid& lhs, const DDBooleanSolid& rhs ) const {
00315   bool ret(true);
00316   if ( lhs.name().fullname() != rhs.name().fullname() ) {
00317     ret = false;
00318     std::cout << "BooleanSolid names do not match ";
00319   } else if ( lhs.shape() != rhs.shape() ) {
00320     ret = false;
00321     std::cout << "BooleanSolid shape types do not match ";
00322   } else if ( ! DDCompareDBLVEC(ddco_.distTol_)(lhs.parameters(), rhs.parameters()) ) {
00323     ret = false;
00324     std::cout << "BooleanSolid parameters do not match ";
00325   } else if ( ! DDCompareSolid(ddco_)(lhs.solidA(), rhs.solidA()) ) {
00326     ret = false;
00327     std::cout << "BooleanSolid SolidA solids do not match ";
00328   } else if ( ! DDCompareSolid(ddco_)(lhs.solidB(), rhs.solidB()) ) {
00329     ret= false;
00330     std::cout << "BooleanSolid SolidB solids do not match ";
00331   } else if ( ! DDCompareDDTrans(ddco_.distTol_)(lhs.translation(), rhs.translation()) ) {
00332     ret = false;
00333     std::cout << "BooleanSolid Translations do not match ";
00334   } else if ( ! DDCompareDDRot(ddco_)(lhs.rotation(), rhs.rotation()) ) {
00335     ret = false;
00336     std::cout << "BooleanSolid Rotations do not match ";
00337   }
00338   if ( ! ret ) {
00339     std::cout << "for boolean solids " 
00340               << lhs.name().fullname() << " and " 
00341               << rhs.name().fullname() << std::endl;
00342   }
00343   return ret;
00344 }
00345 
00346 DDCompareDDTrans::DDCompareDDTrans() : tol_(0.0004) { }
00347 
00348 DDCompareDDTrans::DDCompareDDTrans(double tol) : tol_(tol) { }
00349 
00350 bool DDCompareDDTrans::operator() ( const DDTranslation& lhs, const DDTranslation& rhs ) const {
00351   bool ret(true);
00352   if ( std::fabs(lhs.x() - rhs.x()) > tol_
00353        || std::fabs(lhs.y() - rhs.y()) > tol_
00354        || std::fabs(lhs.z() - rhs.z()) > tol_ ) {  
00355     ret=false;
00356   }
00357   return ret;
00358 }
00359 
00360 DDCompareDDRot::DDCompareDDRot( ) : ddco_() { }
00361 
00362 DDCompareDDRot::DDCompareDDRot( const DDCompOptions& ddco ) : ddco_(ddco) { }
00363 
00364 bool DDCompareDDRot::operator() ( const DDRotation& lhs, const DDRotation& rhs ) const {
00365   bool ret(true);
00366   if ( ddco_.compRotName_ && lhs.name().fullname() != rhs.name().fullname() ) {
00367     ret = false;
00368     std::cout << "DDRotation names do not match " 
00369               << lhs.name().fullname() << " and " 
00370               << rhs.name().fullname() << std::endl;
00371   } else if ( ! DDCompareDDRotMat()(*lhs.rotation(), *rhs.rotation()) ) {
00372     ret = false;
00373     std::cout << "DDRotationMatrix values do not match " 
00374               << lhs.name().fullname() << " and " 
00375               << rhs.name().fullname() << std::endl;
00376   }
00377   return ret;
00378 }
00379 
00380 DDCompareDDRotMat::DDCompareDDRotMat() : tol_(0.0004) { }
00381 
00382 DDCompareDDRotMat::DDCompareDDRotMat(double tol) : tol_(tol) { }
00383 
00384 bool DDCompareDDRotMat::operator() ( const DDRotationMatrix& lhs, const DDRotationMatrix& rhs ) const {
00385   bool ret(true);
00386   // manual way to do it... postponed.  Tested with Distance method from root::math
00387   //DD3Vector x1, y1, z1;
00388   //lhs.GetComponents(x1,y1,z1);
00389   //DD3Vector x2, y2, z2;
00390   //rhs.GetComponents(x2,y2,z2);
00391   double dist = Distance(lhs,rhs);
00392   if ( std::fabs(dist) > tol_ ) {
00393     std::cout << "Rotation matrices do not match." << std::endl;
00394     ret = false;
00395     DD3Vector x, y, z;
00396     std::cout << "FIRST" << std::endl;
00397     lhs.GetComponents(x,y,z);
00398     std::cout << std::setw(12) << std::fixed << std::setprecision(4) << x.X();
00399     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << y.X();
00400     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << z.X();
00401     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << x.Y();
00402     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << y.Y();
00403     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << z.Y();
00404     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << x.Z();
00405     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << y.Z();
00406     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << z.Z() << std::endl;
00407     std::cout << "SECOND" << std::endl;
00408     rhs.GetComponents(x,y,z);
00409     std::cout << std::setw(12) << std::fixed << std::setprecision(4) << x.X();
00410     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << y.X();
00411     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << z.X();
00412     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << x.Y();
00413     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << y.Y();
00414     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << z.Y();
00415     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << x.Z();
00416     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << y.Z();
00417     std::cout << "," << std::setw(12) << std::fixed << std::setprecision(4) << z.Z() << std::endl;
00418   }
00419 
00420   return ret;
00421 }
00422 
00423