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