GEMC  2.3
Geant4 Monte-Carlo Framework
gdml_materials.cc
Go to the documentation of this file.
1 // Qt headers
2 #include <QtSql>
3 
4 // gemc headers
5 #include "material_factory.h"
6 #include "gdml_materials.h"
7 #include "string_utilities.h"
8 #include "utils.h"
9 
10 // G4 headers
11 #include "G4Element.hh"
12 #include "G4NistManager.hh"
13 #include "G4OpBoundaryProcess.hh"
14 
15 // CLHEP units
16 #include "CLHEP/Units/PhysicalConstants.h"
17 using namespace CLHEP;
18 
19 map<string, G4Material*> gdml_materials::initMaterials(runConditions rc, goptions opts)
20 {
21 
22  string hd_msg = opts.optMap["LOG_MSG"].args + " GDML Materials: >> ";
23  double verbosity = opts.optMap["MATERIAL_VERBOSITY"].arg;
24 
25 
26  map<string, G4Element*> elementsMap;
27  map<string, G4Material*> materialsMap;
28 
29  // first check if there's at least one detector with MYSQL factory
31  return materialsMap;
32 
33  // building materials from GDML file
34  for(map<string, detectorCondition>::iterator it=rc.detectorConditionsMap.begin(); it != rc.detectorConditionsMap.end() ; it++)
35  {
36  if(it->second.get_factory() != "GDML" )
37  continue;
38 
39  if(verbosity)
40  cout << hd_msg << " Initializing " << it->second.get_factory() << " for detector " << it->first << endl;
41 
42  string dname = it->first;
43  string fname = dname + ".gdml";
44 
45  // If found, parse the gdml file
46  QFile gdet(fname.c_str());
47 
48  if( !gdet.exists() )
49  {
50  cout << hd_msg << " Failed to open geometry file " << fname << " for system: " << dname << ". Maybe the filename doesn't exist? Exiting." << endl;
51  exit(0);
52  }
53 
54  QDomDocument domDocument;
55  // opening gcard and filling domDocument
56  if(!domDocument.setContent(&gdet))
57  {
58  cout << hd_msg << " Failed to open geometry file " << fname << " for system: " << dname << ". Wrong XML syntax. Exiting." << endl;
59  exit(0);
60  }
61  gdet.close();
62 
63  QDomElement docElem = domDocument.documentElement(); // reading gcard file
64  QDomNode n = docElem.firstChild(); // looping over first tags
65  while(!n.isNull())
66  {
68  QDomElement e = n.toElement();
69  // if the node is an element with tag materials
70  if(!e.isNull() && e.tagName().toStdString() == "materials")
71  {
72  QDomNode nn= e.firstChild();
73 
74  while( !nn.isNull() )
75  {
76  QDomElement ee = nn.toElement();
77  //parses and adds elements to elementsMap
78  if(ee.tagName().toStdString() == "element")
79  {
80 
81  string ename = assignAttribute(ee, "name", "noname");
82  int Z = assignAttribute(ee, "Z", 0);
83  double molar_mass = 0;
84 
85  QDomNode nnn= ee.firstChild();
86 
87  while( !nnn.isNull() )
88  {
89  QDomElement eee = nnn.toElement();
90 
91  if(eee.tagName().toStdString() == "atom")
92  molar_mass = assignAttribute(eee, "value", 0.0);
93 
94  nnn=nnn.nextSibling();
95  cout << ename << " Z: "<< Z << " molar_mass: " << molar_mass << endl;
96  }
97 
98  elementsMap[ename] = new G4Element(ename, ename, Z, molar_mass*g/mole);
99  }
100 
101  if(ee.tagName().toStdString() == "material")
102  {
103  vector<double> fractions;
104  vector<int> composite;
105  vector<string> refs;
106 
107  string ename = assignAttribute(ee, "name", "noname");
108  int Z = assignAttribute(ee, "Z", 0);
109  double molar_mass = 0;
110  double density = 0;
111 
112  // filling molar mass, density and fractions
113  QDomNode nnn= ee.firstChild();
114  while( !nnn.isNull() )
115  {
116  QDomElement eee = nnn.toElement();
117 
118  if(eee.tagName().toStdString() == "D")
119  density = assignAttribute(eee, "value", 0.0);
120 
121  if(eee.tagName().toStdString() == "atom")
122  {
123  molar_mass = assignAttribute(eee, "value", 0.0);
124  }
125 
126  if(eee.tagName().toStdString() == "fraction")
127  {
128  fractions.push_back(assignAttribute(eee, "n", 0.0));
129  refs.push_back(assignAttribute(eee, "ref", "noref"));
130  }
131  if(eee.tagName().toStdString() == "composite")
132  {
133  fractions.push_back(assignAttribute(eee, "n", 0));
134  refs.push_back(assignAttribute(eee, "ref", "noref"));
135  }
136  nnn=nnn.nextSibling();
137  }
138  //If the material doesn't have any fraction node, it is an element
139  if(fractions.size() == 0)
140  elementsMap[ename] = new G4Element(ename, ename, Z, molar_mass*g/mole);
141  // defines a simple material
142  if (Z>0 && molar_mass >0)
143  materialsMap[ename]= new G4Material(ename, Z, molar_mass*g/mole, density*g/cm3);
144 
145  // defines molecules and mixtures
146  if(fractions.size() != 0)
147  {
148  cout << "fraction.size() " << fractions.size() << endl;
149  materialsMap[ename] = new G4Material(ename, density*g/cm3, (int) fractions.size());
150 
151  for(unsigned int i=0; i<fractions.size(); i++)
152  {
153  cout << refs[i] << " elementsMap : " << elementsMap[refs[i]] << endl;
154  if(fractions[i] >= 1) materialsMap[ename]->AddElement(elementsMap[refs[i]], (int)fractions[i]);
155  else materialsMap[ename]->AddElement(elementsMap[refs[i]], fractions[i]);
156  }
157  }
158 
159 
160  }
161 
162  nn=nn.nextSibling();
163  }
164  }
165  n=n.nextSibling();
166  }
167 
168  }
169 
170 
171 
172  if(verbosity>0) printMaterials(materialsMap);
173  return materialsMap;
174 }
map< string, G4Material * > initMaterials(runConditions, goptions)
int check_if_factory_is_needed(map< string, detectorCondition > dcon, string factory)
double verbosity
map< string, detectorCondition > detectorConditionsMap
void printMaterials(map< string, G4Material * > matMap)
string assignAttribute(QDomElement e, string attribute, string defaultValue)
Definition: utils.cc:294
map< string, aopt > optMap
Options map.
Definition: options.h:75