CMS 3D CMS Logo

DDCheckMaterials.cc
Go to the documentation of this file.
1 #include <ostream>
2 #include <string>
3 #include <utility>
4 #include <vector>
5 
10 
11 namespace DDI {
12 class Material;
13 } // namespace DDI
14 
15 // internal usage
16 bool DDCheckMaterial(DDMaterial& mip, std::pair<std::string,DDName> & result, int rlevel = 0)
17 {
18  std::string no_composites = " NO-COMPOSITES ";
19  std::string no_density = " NO-DENSITY ";
20  std::string costit_nok = " CONSTITUENT NOK ";
21  std::string no_z = " NO-Z ";
22  std::string no_a = " NO-A ";
23 
24  std::string curr_err = "";
25  bool err = false;
26 
27  if (mip.isDefined().first == 0) {
28  err=true;
29  curr_err += "material not declared; unknown material!";
30  //edm::LogError("DDCheckMaterials") << "material not declared!" << std::endl; //exit(1);
31  result.first = curr_err;
32  return err;
33  }
34 
35  if (mip.isDefined().second == 0) {
36  err=true;
37  curr_err += "material name=" + mip.name().ns() + ":" + mip.name().name()
38  + " is declared but not defined";
39  result.first = curr_err;
40  return err;
41  }
42 
43  DDMaterial & mp = mip;
44  result.second=mp.ddname();
45 
46  if (!mp.density()) {
47  err=true;
48  curr_err += no_density;
49  }
50 
51  if ( ( !mp.z() || !mp.a() ) && !mp.noOfConstituents() ) {
52  err=true;
53  curr_err += no_z;
54  curr_err += "or";
55  curr_err += no_a;
56  }
57 
58  if ( ( !mp.z() && !mp.a() ) && !mp.noOfConstituents() ) {
59  err=true;
60  curr_err += no_composites;
61  }
62 
63  if ( !mp.z() && !mp.a() && !mp.density() && !mp.noOfConstituents() ){
64  err=true;
65  curr_err = " NOT-DEFINED ";
66  }
67 
68  if (err) {
69  result.first=curr_err;
70  }
71 
72  // recursive checking of constituents
73  // the composite material is not ok if only one constituent is not ok
74  int loop = mp.noOfConstituents() - 1;
75 
76  for (; loop>=0; --loop) {
77  std::pair<std::string,DDName> res("","");
78  DDMaterial mat(mp.ddname()); // bit slow but comfortable ...
79  DDMaterial mimpl = mat.constituent(loop).first;
80  ++rlevel; // recursion level
81  bool c_err = DDCheckMaterial(mimpl,res, rlevel);
82  if (c_err) {
83  err = err | c_err;
84  curr_err = curr_err + std::string(" constituents have errors:\n") + std::string(4*rlevel,' ')
85  + std::string(" ") + res.first;
86  result.first=curr_err;
87  }
88  --rlevel;
89  }
90 
91  return err;
92 }
93 
94 
96 bool DDCheckMaterials(std::ostream & os, std::vector<std::pair<std::string,DDName> > * res)
97 {
98  bool result = false;
99  std::vector<std::pair<std::string,DDName> > errors;
100 
102 
103  for( const auto& i : mr ) {
104  std::pair<std::string,DDName> error("","");
105  DDMaterial tmat(i.first);
106 
107  if (DDCheckMaterial(tmat,error)) {
108  errors.push_back(error);
109  }
110  }
111 
112  std::string s(" ");
113  os << "[DDCore:Report] Materials " << std::endl;
114  os << s << mr.size() << " Materials declared" << std::endl;
115  os << s << "detected errors:" << errors.size() << std::endl;
116  for( auto j : errors ) {
117  os << std::endl << s << j.second << " " << j.first << std::endl;
118  result = true;
119  }
120  if(res) *res = errors;
121  return result;
122 }
double a() const
returns the atomic mass
Definition: DDMaterial.cc:97
def_type isDefined() const
Definition: DDBase.h:110
const N & name() const
Definition: DDBase.h:78
DDMaterial is used to define and access material information.
Definition: DDMaterial.h:41
bool DDCheckMaterials(std::ostream &os, std::vector< std::pair< std::string, DDName > > *res)
Checks all registered materials and sends a report /p os.
const std::string & ns() const
Returns the namespace.
Definition: DDName.cc:104
Definition: Electron.h:4
static value_type & instance()
double z() const
retruns the atomic number
Definition: DDMaterial.cc:103
FractionV::value_type constituent(int i) const
returns the i-th compound material and its fraction-mass
Definition: DDMaterial.cc:91
double density() const
returns the density
Definition: DDMaterial.cc:109
int noOfConstituents() const
returns the number of compound materials or 0 for elementary materials
Definition: DDMaterial.cc:85
const std::string & name() const
Returns the name.
Definition: DDName.cc:90
bool DDCheckMaterial(DDMaterial &mip, std::pair< std::string, DDName > &result, int rlevel=0)
const N & ddname() const
Definition: DDBase.h:80