Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "DetectorDescription/RegressionTest/interface/DDErrorDetection.h"
0002 
0003 #include <fstream>
0004 
0005 #include "DetectorDescription/Core/interface/Store.h"
0006 //***** Explicit template instantiation of Singleton
0007 #include "DetectorDescription/Core/interface/Singleton.icc"
0008 #include "DetectorDescription/Core/interface/DDBase.h"
0009 #include "DetectorDescription/Core/interface/DDCompactView.h"
0010 #include "DetectorDescription/Core/interface/DDLogicalPart.h"
0011 #include "DetectorDescription/Core/interface/DDMaterial.h"
0012 #include "DetectorDescription/Core/interface/DDName.h"
0013 #include "DetectorDescription/Core/interface/DDSolid.h"
0014 #include "DetectorDescription/Core/interface/DDSolidShapes.h"
0015 #include "DetectorDescription/Core/interface/DDSpecifics.h"
0016 #include "DetectorDescription/Core/interface/DDTransform.h"
0017 #include "DataFormats/Math/interface/Graph.h"
0018 #include "DataFormats/Math/interface/GraphWalker.h"
0019 #include "DetectorDescription/RegressionTest/src/DDCheck.h"
0020 //**** to get rid of compile errors about ambiguous delete of Stores
0021 #include "DetectorDescription/Core/interface/LogicalPart.h"
0022 #include "DetectorDescription/Core/interface/Solid.h"
0023 #include "DetectorDescription/Core/interface/Material.h"
0024 #include "DetectorDescription/Core/interface/Specific.h"
0025 
0026 using namespace std;
0027 
0028 template class DDI::Singleton<std::map<std::string, std::set<DDLogicalPart>>>;
0029 template class DDI::Singleton<std::map<std::string, std::set<DDMaterial>>>;
0030 template class DDI::Singleton<std::map<std::string, std::set<DDSolid>>>;
0031 template class DDI::Singleton<std::map<std::string, std::set<DDRotation>>>;
0032 template class DDI::Singleton<std::map<std::string, std::set<DDSpecifics>>>;
0033 
0034 //*****
0035 
0036 DDErrorDetection::DDErrorDetection(const DDCompactView& cpv) {
0037   DDMaterial::StoreT::instance().setReadOnly(false);
0038   DDSolid::StoreT::instance().setReadOnly(false);
0039   DDLogicalPart::StoreT::instance().setReadOnly(false);
0040   DDSpecifics::StoreT::instance().setReadOnly(false);
0041   DDRotation::StoreT::instance().setReadOnly(false);
0042 
0043   scan(cpv);
0044 }
0045 
0046 DDErrorDetection::~DDErrorDetection() {
0047   DDMaterial::StoreT::instance().setReadOnly(true);
0048   DDSolid::StoreT::instance().setReadOnly(true);
0049   DDLogicalPart::StoreT::instance().setReadOnly(true);
0050   DDSpecifics::StoreT::instance().setReadOnly(true);
0051   DDRotation::StoreT::instance().setReadOnly(true);
0052 }
0053 
0054 void DDErrorDetection::scan(const DDCompactView& cpv) {
0055   std::cout << "DDErrorDetection::scan(): Scanning for DDD errors ..." << std::flush;
0056 
0057   DDLogicalPart lp_dummy;
0058   DDMaterial ma_dummy;
0059   DDRotation ro_dummy;
0060   DDSpecifics sp_dummy;
0061   DDSolid so_dummy;
0062 
0063   lp_err::instance() = dd_error_scan(lp_dummy);
0064   ma_err::instance() = dd_error_scan(ma_dummy);
0065   ro_err::instance() = dd_error_scan(ro_dummy);
0066   sp_err::instance() = dd_error_scan(sp_dummy);
0067   so_err::instance() = dd_error_scan(so_dummy);
0068 
0069   std::cout << " ... finished." << std::endl;
0070 }
0071 
0072 void DDErrorDetection::errors() { std::cout << "What does DDErrorDetection::errors() do? nothing." << std::endl; }
0073 
0074 void DDErrorDetection::warnings() { std::cout << "What does DDErrorDetection::warnings() do? nothing." << std::endl; }
0075 
0076 // ddname as std::string, std::set<edges>
0077 const std::map<std::string, std::set<DDLogicalPart>>& DDErrorDetection::lp_cpv(const DDCompactView& cpv) {
0078   static std::map<std::string, std::set<DDLogicalPart>> result_;
0079   if (!result_.empty())
0080     return result_;
0081 
0082   const auto& g = cpv.graph();
0083 
0084   std::map<std::string, std::set<DDLogicalPart>>::const_iterator it(lp_err::instance().begin()),
0085       ed(lp_err::instance().end());
0086   for (; it != ed; ++it) {
0087     std::set<DDLogicalPart>::const_iterator sit(it->second.begin()), sed(it->second.end());
0088     for (; sit != sed; ++sit) {
0089       const DDLogicalPart& lp = *sit;
0090       auto er = g.edges(lp);
0091       if (g.nodeIndex(lp).second) {
0092         result_.insert(make_pair(lp.ddname().fullname(), std::set<DDLogicalPart>()));
0093       }
0094       for (; er.first != er.second; ++er.first) {
0095         result_[lp.ddname().fullname()].insert(g.nodeData(er.first->first));
0096       }
0097     }
0098   }
0099   return result_;
0100 }
0101 
0102 const std::map<DDSolid, std::set<DDLogicalPart>>& DDErrorDetection::so_lp() {
0103   static std::map<DDSolid, std::set<DDLogicalPart>> result_;
0104   if (!result_.empty())
0105     return result_;
0106 
0107   const std::map<DDSolid, std::set<DDSolid>>& err_mat = so();
0108   std::map<DDSolid, std::set<DDSolid>>::const_iterator it(err_mat.begin()), ed(err_mat.end());
0109   for (; it != ed; ++it) {
0110     std::set<DDLogicalPart> s;
0111     DDSolid m(it->first);
0112     result_[m] = s;
0113     std::set<DDSolid>::const_iterator sit(it->second.begin()), sed(it->second.end());
0114     for (; sit != sed; ++sit) {
0115       result_[*sit] = s;
0116     }
0117     //std::cout << "insert: " << m.name() << std::endl;
0118   }
0119   DDLogicalPart::iterator<DDLogicalPart> lpit, lped;
0120   lped.end();
0121   for (; lpit != lped; ++lpit) {
0122     if (lpit->isDefined().second) {
0123       std::map<DDSolid, std::set<DDLogicalPart>>::iterator i = result_.find(lpit->solid());
0124       //std::cout << "searching: " << lpit->name() << std::endl;
0125       if (i != result_.end()) {
0126         //std::cout << std::endl << "FOUND: " << lpit->name() << std::endl << std::endl;
0127         i->second.insert(*lpit);
0128       }
0129     }
0130   }
0131   return result_;
0132 }
0133 
0134 /*
0135 const std::map<DDSpecifics, std::set<pair<DDLogicalPart, std::string> > & DDErrorDetection::sp()
0136 {
0137   static std::map<DDSpecifics, std::set<pair<DDLogicalPart, std::string> result_;
0138   if (result_.size()) return result_;
0139 }
0140 */
0141 
0142 const std::map<DDMaterial, std::set<DDLogicalPart>>& DDErrorDetection::ma_lp() {
0143   static std::map<DDMaterial, std::set<DDLogicalPart>> result_;
0144   if (!result_.empty())
0145     return result_;
0146 
0147   const std::vector<pair<std::string, std::string>>& err_mat = ma();
0148   std::vector<pair<std::string, std::string>>::const_iterator it(err_mat.begin()), ed(err_mat.end());
0149   for (; it != ed; ++it) {
0150     std::set<DDLogicalPart> s;
0151     DDMaterial m(it->second);
0152     result_[m] = s;
0153   }
0154   DDLogicalPart::iterator<DDLogicalPart> lpit, lped;
0155   lped.end();
0156   for (; lpit != lped; ++lpit) {
0157     if (lpit->isDefined().second) {
0158       std::map<DDMaterial, std::set<DDLogicalPart>>::iterator i = result_.find(lpit->material());
0159       if (i != result_.end()) {
0160         //std::cout << std::endl << "FOUND: " << lpit->name() << std::endl << std::endl;
0161         i->second.insert(*lpit);
0162       }
0163     }
0164   }
0165   return result_;
0166 }
0167 
0168 const std::vector<pair<std::string, std::string>>& DDErrorDetection::ma() {
0169   static std::vector<pair<std::string, std::string>> result_;
0170   ofstream o("/dev/null");
0171 
0172   if (!result_.empty())
0173     return result_;
0174 
0175   DDCheckMaterials(o, &result_);
0176   return result_;
0177 
0178   /*
0179   */
0180 }
0181 
0182 const std::map<DDSolid, std::set<DDSolid>>& DDErrorDetection::so() {
0183   static std::map<DDSolid, std::set<DDSolid>> result_;
0184   if (!result_.empty())
0185     return result_;
0186 
0187   // build the material dependency graph
0188   using ma_graph_t = math::Graph<DDSolid, double>;
0189   using ma_walker_t = math::GraphWalker<DDSolid, double>;
0190 
0191   ma_graph_t mag;
0192   std::vector<DDSolid> errs;
0193   DDSolid::iterator<DDSolid> it, ed;
0194   ed.end();
0195   for (; it != ed; ++it) {
0196     DDSolid ma = *it;
0197     if (ma.isDefined().second) {
0198       DDSolidShape sh = ma.shape();
0199       if ((sh == DDSolidShape::ddunion) || (sh == DDSolidShape::ddintersection) ||
0200           (sh == DDSolidShape::ddsubtraction)) {
0201         DDBooleanSolid bs(ma);
0202         DDSolid a(bs.solidA()), b(bs.solidB());
0203         //DDRotation r(bs.rotation());
0204         //DDTranslation t(bs.translation);
0205         mag.addEdge(a, ma, 0);
0206         mag.addEdge(b, ma, 0);
0207       }
0208     } else {
0209       errs.emplace_back(ma);
0210     }
0211   }
0212 
0213   std::vector<DDSolid>::const_iterator mit(errs.begin()), med(errs.end());
0214   for (; mit != med; ++mit) {
0215     ma_walker_t w(mag, *mit);
0216     while (w.next()) {
0217       result_[*mit].insert(w.current().first);
0218     }
0219     std::cout << std::endl;
0220   }
0221 
0222   return result_;
0223 }
0224 
0225 void DDErrorDetection::report(const DDCompactView& cpv, ostream& o) {
0226   o << std::endl << std::endl << "---> DDD ERROR REPORT <---" << std::endl << std::endl;
0227   o << "MISSING DEFINITIONS:" << std::endl << std::endl;
0228   o << "LogicalParts:" << std::endl << lp_err::instance() << std::endl;
0229   o << "Materials:" << std::endl << ma_err::instance() << std::endl;
0230   o << "Solids:" << std::endl << so_err::instance() << std::endl;
0231   o << "Rotations:" << std::endl << ro_err::instance() << std::endl;
0232   o << "Specifics:" << std::endl << sp_err::instance() << std::endl;
0233   o << std::endl << "IMPLICATIONS OF MISSING DEFINITIONS:" << std::endl << std::endl;
0234 
0235   o << "A) LogicalParts that have missing definitions but are used in the geometr. hierarchy (PosParts):" << std::endl
0236     << "   Format: namespace:name: [name of child]*" << std::endl;
0237   o << lp_cpv(cpv) << std::endl;
0238 
0239   o << "B) Detailed report on Materials:" << std::endl;
0240   const std::vector<pair<std::string, std::string>>& res = ma();
0241   std::vector<pair<std::string, std::string>>::const_iterator it(res.begin()), ed(res.end());
0242   for (; it != ed; ++it) {
0243     std::cout << it->second << ":  " << it->first << std::endl;
0244   }
0245   std::cout << std::endl;
0246 
0247   o << "C) Solids affected by Solids that have missing definitions:" << std::endl;
0248   o << so() << std::endl;
0249 
0250   o << "D) LogicalParts affected by Materials of B):" << std::endl;
0251   o << ma_lp() << std::endl;
0252 
0253   o << "E) LogicalParts affected by Solids of C):" << std::endl;
0254   o << so_lp() << std::endl;
0255 
0256   // Did this ever exist?
0257   //  o << "F) Parent-Child positionings affected by Rotations that have missing definitions:"  << std::endl;
0258   o << std::endl;
0259   //nix();
0260 }
0261 
0262 bool DDErrorDetection::noErrorsInTheReport(const DDCompactView& cpv) {
0263   return lp_err::instance().empty() && ma_err::instance().empty() && so_err::instance().empty() &&
0264          ro_err::instance().empty() && sp_err::instance().empty() && lp_cpv(cpv).empty() && ma().empty() &&
0265          so().empty() && ma_lp().empty() && so_lp().empty();
0266 }