Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:08:21

0001 #include <ostream>
0002 #include <string>
0003 #include <utility>
0004 #include <vector>
0005 
0006 #include "DetectorDescription/Core/interface/Store.h"
0007 #include "DetectorDescription/Core/interface/DDBase.h"
0008 #include "DetectorDescription/Core/interface/DDMaterial.h"
0009 #include "DetectorDescription/Core/interface/DDName.h"
0010 
0011 namespace DDI {
0012   class Material;
0013 }
0014 
0015 // internal usage
0016 bool DDCheckMaterial(DDMaterial& mip, std::pair<std::string, std::string>& result, int rlevel = 0) {
0017   std::string no_composites = " NO-COMPOSITES ";
0018   std::string no_density = " NO-DENSITY ";
0019   std::string costit_nok = " CONSTITUENT NOK ";
0020   std::string no_z = " NO-Z ";
0021   std::string no_a = " NO-A ";
0022 
0023   std::string curr_err = "";
0024   bool err = false;
0025 
0026   if (mip.isDefined().first == nullptr) {
0027     err = true;
0028     curr_err += "material not declared; unknown material!";
0029     result.first = curr_err;
0030     return err;
0031   }
0032 
0033   if (mip.isDefined().second == 0) {
0034     err = true;
0035     curr_err += "material name=" + mip.name().ns() + ":" + mip.name().name() + " is declared but not defined";
0036     result.first = curr_err;
0037     return err;
0038   }
0039 
0040   DDMaterial& mp = mip;
0041   result.second = mp.ddname().fullname();
0042 
0043   if (!mp.density()) {
0044     err = true;
0045     curr_err += no_density;
0046   }
0047 
0048   if ((!mp.z() || !mp.a()) && !mp.noOfConstituents()) {
0049     err = true;
0050     curr_err += no_z;
0051     curr_err += "or";
0052     curr_err += no_a;
0053   }
0054 
0055   if ((!mp.z() && !mp.a()) && !mp.noOfConstituents()) {
0056     err = true;
0057     curr_err += no_composites;
0058   }
0059 
0060   if (!mp.z() && !mp.a() && !mp.density() && !mp.noOfConstituents()) {
0061     err = true;
0062     curr_err = " NOT-DEFINED ";
0063   }
0064 
0065   if (err) {
0066     result.first = curr_err;
0067   }
0068 
0069   // recursive checking of constituents
0070   // the composite material is not ok if only one constituent is not ok
0071   for (int loop = mp.noOfConstituents() - 1; loop >= 0; --loop) {
0072     std::pair<std::string, std::string> res("", "");
0073     DDMaterial mat(mp.ddname());  // bit slow but comfortable ...
0074     DDMaterial mimpl = mat.constituent(loop).first;
0075     ++rlevel;  // recursion level
0076     bool c_err = DDCheckMaterial(mimpl, res, rlevel);
0077     if (c_err) {
0078       err = err | c_err;
0079       curr_err = curr_err + std::string(" constituents have errors:\n") + std::string(4 * rlevel, ' ') +
0080                  std::string(" ") + res.first;
0081       result.first = curr_err;
0082     }
0083     --rlevel;
0084   }
0085 
0086   return err;
0087 }
0088 
0089 //! Checks all registered materials and sends a report /p os
0090 bool DDCheckMaterials(std::ostream& os, std::vector<std::pair<std::string, std::string>>* res) {
0091   bool result = false;
0092   std::vector<std::pair<std::string, std::string>> errors;
0093 
0094   auto& mr = DDBase<DDName, std::unique_ptr<DDI::Material>>::StoreT::instance();
0095 
0096   for (const auto& i : mr) {
0097     std::pair<std::string, std::string> error("", "");
0098     DDMaterial tmat(i.first);
0099 
0100     if (DDCheckMaterial(tmat, error)) {
0101       errors.emplace_back(error);
0102     }
0103   }
0104 
0105   std::string s(" ");
0106   os << "[DDCore:Report] Materials " << std::endl;
0107   os << s << mr.size() << " Materials declared" << std::endl;
0108   os << s << "detected errors:" << errors.size() << std::endl;
0109   for (const auto& j : errors) {
0110     os << std::endl << s << j.second << "  " << j.first << std::endl;
0111     result = true;
0112   }
0113   if (res)
0114     *res = errors;
0115   return result;
0116 }