00001
00002 #include <vector>
00003 #include "DetectorDescription/Core/interface/DDMaterial.h"
00004
00005
00006
00007
00008 bool DDCheckMaterial(DDMaterial& mip, std::pair<std::string,DDName> & result)
00009 {
00010 std::string no_composites = " NO-COMPOSITES ";
00011 std::string no_density = " NO-DENSITY ";
00012 std::string costit_nok = " CONSTITUENT NOK ";
00013 std::string no_z = " NO-Z ";
00014 std::string no_a = " NO-A ";
00015 static int rlevel = 0;
00016
00017 std::string curr_err = "";
00018 bool err = false;
00019
00020 if (mip.isDefined().first == 0) {
00021 err=true;
00022 curr_err += "material not declared; unknown material!";
00023
00024 result.first = curr_err;
00025 return err;
00026 }
00027
00028 if (mip.isDefined().second == 0) {
00029 err=true;
00030 curr_err += "material name=" + mip.name().ns() + ":" + mip.name().name()
00031 + " is declared but not defined";
00032 result.first = curr_err;
00033 return err;
00034 }
00035
00036
00037
00038
00039
00040
00041 DDMaterial & mp = mip;
00042 result.second=mp.ddname();
00043
00044 if (!mp.density()) {
00045 err=true;
00046 curr_err += no_density;
00047 }
00048
00049 if ( ( !mp.z() || !mp.a() ) && !mp.noOfConstituents() ) {
00050 err=true;
00051 curr_err += no_z;
00052 curr_err += "or";
00053 curr_err += no_a;
00054 }
00055
00056 if ( ( !mp.z() && !mp.a() ) && !mp.noOfConstituents() ) {
00057 err=true;
00058 curr_err += no_composites;
00059 }
00060
00061 if ( !mp.z() && !mp.a() && !mp.density() && !mp.noOfConstituents() ){
00062 err=true;
00063 curr_err = " NOT-DEFINED ";
00064 }
00065
00066 if (err) {
00067 result.first=curr_err;
00068 }
00069
00070
00071
00072 int loop = mp.noOfConstituents() - 1;
00073
00074 for (; loop>=0; --loop) {
00075 std::pair<std::string,DDName> res("","");
00076 DDMaterial mat(mp.ddname());
00077 DDMaterial mimpl = mat.constituent(loop).first;
00078 ++rlevel;
00079 bool c_err = DDCheckMaterial(mimpl,res);
00080 if (c_err) {
00081 err = err | c_err;
00082 curr_err = curr_err + std::string(" constituents have errors:\n") + std::string(4*rlevel,' ')
00083
00084 + std::string(" ") + res.first;
00085 result.first=curr_err;
00086 }
00087 --rlevel;
00088 }
00089
00090 return err;
00091 }
00092
00093
00095 bool DDCheckMaterials(std::ostream & os, std::vector<std::pair<std::string,DDName> > * res)
00096 {
00097 bool result = false;
00098 std::vector<std::pair<std::string,DDName> > errors;
00099
00100
00101
00102
00103 typedef DDBase<DDName,DDI::Material*>::StoreT RegT;
00104 RegT::value_type& mr = RegT::instance();
00105 RegT::value_type::iterator i = mr.begin();
00106
00107 for(; i != mr.end(); ++i) {
00108 std::pair<std::string,DDName> error("","");
00109 DDMaterial tmat(i->first);
00110
00111 if (DDCheckMaterial(tmat,error)) {
00112 errors.push_back(error);
00113 }
00114 }
00115
00116 std::string s(" ");
00117 os << "[DDCore:Report] Materials " << std::endl;
00118 os << s << mr.size() << " Materials declared" << std::endl;
00119 os << s << "detected errors:" << errors.size() << std::endl;
00120 std::vector<std::pair<std::string,DDName> >::iterator j = errors.begin();
00121 for (;j!=errors.end();++j) {
00122 os << std::endl << s << j->second << " " << j->first << std::endl;
00123 result = true;
00124 }
00125 if(res) *res = errors;
00126 return result;
00127 }