ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHG4GDMLWriteMaterials.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHG4GDMLWriteMaterials.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: PHG4GDMLWriteMaterials.cc 70764 2013-06-05 12:54:37Z gcosmo $
28 //
29 // class PHG4GDMLWriteMaterials Implementation
30 //
31 // Original author: Zoltan Torzsok, November 2007
32 //
33 // --------------------------------------------------------------------
34 
36 
37 #include <Geant4/G4PhysicalConstants.hh>
38 #include <Geant4/G4SystemOfUnits.hh>
39 #include <Geant4/G4Element.hh>
40 #include <Geant4/G4Isotope.hh>
41 #include <Geant4/G4Material.hh>
42 
43 #include <sstream>
44 
45 
47  : PHG4GDMLWriteDefine(), materialsElement(0)
48 {
49 }
50 
52 {
53 }
54 
56 AtomWrite(xercesc::DOMElement* element,const G4double& a)
57 {
58  xercesc::DOMElement* atomElement = NewElement("atom");
59  atomElement->setAttributeNode(NewAttribute("unit","g/mole"));
60  atomElement->setAttributeNode(NewAttribute("value",a*mole/g));
61  element->appendChild(atomElement);
62 }
63 
65 DWrite(xercesc::DOMElement* element,const G4double& d)
66 {
67  xercesc::DOMElement* DElement = NewElement("D");
68  DElement->setAttributeNode(NewAttribute("unit","g/cm3"));
69  DElement->setAttributeNode(NewAttribute("value",d*cm3/g));
70  element->appendChild(DElement);
71 }
72 
74 PWrite(xercesc::DOMElement* element,const G4double& P)
75 {
76  xercesc::DOMElement* PElement = NewElement("P");
77  PElement->setAttributeNode(NewAttribute("unit","pascal"));
78  PElement->setAttributeNode(NewAttribute("value",P/hep_pascal));
79  element->appendChild(PElement);
80 }
81 
83 TWrite(xercesc::DOMElement* element,const G4double& T)
84 {
85  xercesc::DOMElement* TElement = NewElement("T");
86  TElement->setAttributeNode(NewAttribute("unit","K"));
87  TElement->setAttributeNode(NewAttribute("value",T/kelvin));
88  element->appendChild(TElement);
89 }
90 
92 MEEWrite(xercesc::DOMElement* element,const G4double& MEE)
93 {
94  xercesc::DOMElement* PElement = NewElement("MEE");
95  PElement->setAttributeNode(NewAttribute("unit","eV"));
96  PElement->setAttributeNode(NewAttribute("value",MEE/electronvolt));
97  element->appendChild(PElement);
98 }
99 
101 IsotopeWrite(const G4Isotope* const isotopePtr)
102 {
103  const G4String name = GenerateName(isotopePtr->GetName(),isotopePtr);
104 
105  xercesc::DOMElement* isotopeElement = NewElement("isotope");
106  isotopeElement->setAttributeNode(NewAttribute("name",name));
107  isotopeElement->setAttributeNode(NewAttribute("N",isotopePtr->GetN()));
108  isotopeElement->setAttributeNode(NewAttribute("Z",isotopePtr->GetZ()));
109  materialsElement->appendChild(isotopeElement);
110  AtomWrite(isotopeElement,isotopePtr->GetA());
111 }
112 
113 void PHG4GDMLWriteMaterials::ElementWrite(const G4Element* const elementPtr)
114 {
115  const G4String name = GenerateName(elementPtr->GetName(),elementPtr);
116 
117  xercesc::DOMElement* elementElement = NewElement("element");
118  elementElement->setAttributeNode(NewAttribute("name",name));
119 
120  const size_t NumberOfIsotopes = elementPtr->GetNumberOfIsotopes();
121 
122  if (NumberOfIsotopes>0)
123  {
124  const G4double* RelativeAbundanceVector =
125  elementPtr->GetRelativeAbundanceVector();
126  for (size_t i=0;i<NumberOfIsotopes;i++)
127  {
128  G4String fractionref = GenerateName(elementPtr->GetIsotope(i)->GetName(),
129  elementPtr->GetIsotope(i));
130  xercesc::DOMElement* fractionElement = NewElement("fraction");
131  fractionElement->setAttributeNode(NewAttribute("n",
132  RelativeAbundanceVector[i]));
133  fractionElement->setAttributeNode(NewAttribute("ref",fractionref));
134  elementElement->appendChild(fractionElement);
135  AddIsotope(elementPtr->GetIsotope(i));
136  }
137  }
138  else
139  {
140  elementElement->setAttributeNode(NewAttribute("Z",elementPtr->GetZ()));
141  AtomWrite(elementElement,elementPtr->GetA());
142  }
143 
144  materialsElement->appendChild(elementElement);
145  // Append the element AFTER all the possible components are appended!
146 }
147 
148 void PHG4GDMLWriteMaterials::MaterialWrite(const G4Material* const materialPtr)
149 {
150  G4String state_str("undefined");
151  const G4State state = materialPtr->GetState();
152  if (state==kStateSolid) { state_str = "solid"; } else
153  if (state==kStateLiquid) { state_str = "liquid"; } else
154  if (state==kStateGas) { state_str = "gas"; }
155 
156  const G4String name = GenerateName(materialPtr->GetName(), materialPtr);
157 
158  xercesc::DOMElement* materialElement = NewElement("material");
159  materialElement->setAttributeNode(NewAttribute("name",name));
160  materialElement->setAttributeNode(NewAttribute("state",state_str));
161 
162  // Write any property attached to the material...
163  //
164  if (materialPtr->GetMaterialPropertiesTable())
165  {
166  PropertyWrite(materialElement, materialPtr);
167  }
168 
169  if (materialPtr->GetTemperature() != STP_Temperature)
170  { TWrite(materialElement,materialPtr->GetTemperature()); }
171  if (materialPtr->GetPressure() != STP_Pressure)
172  { PWrite(materialElement,materialPtr->GetPressure()); }
173 
174  // Write Ionisation potential (mean excitation energy)
175  MEEWrite(materialElement,materialPtr->GetIonisation()->GetMeanExcitationEnergy());
176 
177  DWrite(materialElement,materialPtr->GetDensity());
178 
179  const size_t NumberOfElements = materialPtr->GetNumberOfElements();
180 
181  if ( (NumberOfElements>1)
182  || ( materialPtr->GetElement(0)
183  && materialPtr->GetElement(0)->GetNumberOfIsotopes()>1 ) )
184  {
185  const G4double* MassFractionVector = materialPtr->GetFractionVector();
186 
187  for (size_t i=0;i<NumberOfElements;i++)
188  {
189  const G4String fractionref =
190  GenerateName(materialPtr->GetElement(i)->GetName(),
191  materialPtr->GetElement(i));
192  xercesc::DOMElement* fractionElement = NewElement("fraction");
193  fractionElement->setAttributeNode(NewAttribute("n",
194  MassFractionVector[i]));
195  fractionElement->setAttributeNode(NewAttribute("ref",fractionref));
196  materialElement->appendChild(fractionElement);
197  AddElement(materialPtr->GetElement(i));
198  }
199  }
200  else
201  {
202  materialElement->setAttributeNode(NewAttribute("Z",materialPtr->GetZ()));
203  AtomWrite(materialElement,materialPtr->GetA());
204  }
205 
206  // Append the material AFTER all the possible components are appended!
207  //
208  materialsElement->appendChild(materialElement);
209 }
210 
212  const G4PhysicsOrderedFreeVector* const pvec)
213 {
214  const G4String matrixref = GenerateName(key, pvec);
215  xercesc::DOMElement* matrixElement = NewElement("matrix");
216  matrixElement->setAttributeNode(NewAttribute("name", matrixref));
217  matrixElement->setAttributeNode(NewAttribute("coldim", "2"));
218  std::ostringstream pvalues;
219  for (size_t i=0; i<pvec->GetVectorLength(); i++)
220  {
221  if (i!=0) { pvalues << " "; }
222  pvalues << pvec->Energy(i) << " " << (*pvec)[i];
223  }
224  matrixElement->setAttributeNode(NewAttribute("values", pvalues.str()));
225 
226  defineElement->appendChild(matrixElement);
227 }
228 
229 void PHG4GDMLWriteMaterials::PropertyWrite(xercesc::DOMElement* matElement,
230  const G4Material* const mat)
231 {
232  xercesc::DOMElement* propElement;
234 
235  const std::map< G4int, G4PhysicsOrderedFreeVector*,
236  std::less<G4int> >* pmap = ptable->GetPropertyMap();
237  const std::map< G4int, G4double,
238  std::less<G4int> >* cmap = ptable->GetConstPropertyMap();
239  std::map< G4int, G4PhysicsOrderedFreeVector*,
240  std::less<G4int> >::const_iterator mpos;
241  std::map< G4int, G4double,
242  std::less<G4int> >::const_iterator cpos;
243 
244 
245  for (mpos=pmap->begin(); mpos!=pmap->end(); mpos++)
246  {
247  propElement = NewElement("property");
248  propElement->setAttributeNode(NewAttribute("name",
249  ptable->GetMaterialPropertyNames()[mpos->first]));
250  propElement->setAttributeNode(NewAttribute("ref",
251  GenerateName(ptable->GetMaterialPropertyNames()[mpos->first],
252  mpos->second)));
253  if (mpos->second)
254  {
255  PropertyVectorWrite(ptable->GetMaterialPropertyNames()[mpos->first],
256  mpos->second);
257  matElement->appendChild(propElement);
258  }
259  else
260  {
261  G4String warn_message = "Null pointer for material property -"
262  + ptable->GetMaterialPropertyNames()[mpos->first] + "- of material -"
263  + mat->GetName() + "- !";
264  G4Exception("G4GDMLWriteMaterials::PropertyWrite()", "NullPointer",
265  JustWarning, warn_message);
266  continue;
267  }
268  }
269 
270  for (cpos=cmap->begin(); cpos!=cmap->end(); cpos++)
271  {
272  propElement = NewElement("property");
273  propElement->setAttributeNode(NewAttribute("name",
274  ptable->GetMaterialConstPropertyNames()[cpos->first]));
275  propElement->setAttributeNode(NewAttribute("ref",
276  ptable->GetMaterialConstPropertyNames()[cpos->first]));
277  xercesc::DOMElement* constElement = NewElement("constant");
278  constElement->setAttributeNode(NewAttribute("name",
279  ptable->GetMaterialConstPropertyNames()[cpos->first]));
280  constElement->setAttributeNode(NewAttribute("value", cpos->second));
281  defineElement->appendChild(constElement);
282  matElement->appendChild(propElement);
283  }
284 }
285 
286 void PHG4GDMLWriteMaterials::MaterialsWrite(xercesc::DOMElement* element)
287 {
288  std::cout << "G4GDML: Writing materials..." << std::endl;
289 
290  materialsElement = NewElement("materials");
291  element->appendChild(materialsElement);
292 
293  isotopeList.clear();
294  elementList.clear();
295  materialList.clear();
296 }
297 
298 void PHG4GDMLWriteMaterials::AddIsotope(const G4Isotope* const isotopePtr)
299 {
300  for (size_t i=0; i<isotopeList.size(); i++) // Check if isotope is
301  { // already in the list!
302  if (isotopeList[i] == isotopePtr) { return; }
303  }
304  isotopeList.push_back(isotopePtr);
305  IsotopeWrite(isotopePtr);
306 }
307 
308 void PHG4GDMLWriteMaterials::AddElement(const G4Element* const elementPtr)
309 {
310  for (size_t i=0;i<elementList.size();i++) // Check if element is
311  { // already in the list!
312  if (elementList[i] == elementPtr) { return; }
313  }
314  elementList.push_back(elementPtr);
315  ElementWrite(elementPtr);
316 }
317 
318 void PHG4GDMLWriteMaterials::AddMaterial(const G4Material* const materialPtr)
319 {
320  for (size_t i=0;i<materialList.size();i++) // Check if material is
321  { // already in the list!
322  if (materialList[i] == materialPtr) { return; }
323  }
324  materialList.push_back(materialPtr);
325  MaterialWrite(materialPtr);
326 }