View Javadoc

1   package org.lcsim.detector.converter.lcdd;
2   
3   import static org.lcsim.units.clhep.SystemOfUnits.cm3;
4   import static org.lcsim.units.clhep.SystemOfUnits.g;
5   
6   import org.jdom.Element;
7   import org.jdom.JDOMException;
8   import org.lcsim.detector.converter.XMLConverter;
9   import org.lcsim.detector.material.IMaterial;
10  import org.lcsim.detector.material.MaterialElement;
11  import org.lcsim.detector.material.MaterialMixture;
12  import org.lcsim.detector.material.MaterialStore;
13  
14  /**
15   * 
16   * This converter takes a GDML element and converts
17   * it to a MaterialMixture.
18   * 
19   * @author Jeremy McCormick <jeremym@slac.stanford.edu>
20   */
21  public class MaterialMixtureConverter 
22  implements XMLConverter
23  {	
24  	private static final String compositeStr = "composite";
25  	private static final String fractionStr = "fraction";
26  	
27  	public void convert(Element element) throws JDOMException
28  	{
29  		if ( element.getName().equals("material"))
30  		{
31  			String name = element.getAttributeValue("name");
32  						
33  			if ( element.getChild("D") != null)
34  			{
35  				Element D = element.getChild("D");
36  				//double unit = g / cm3;
37  				//if ( D.getAttribute("unit") != null)
38  				//{
39  				//	unit = D.getAttribute("unit").getDoubleValue();
40  				//}
41  				
42  				if ( D.getAttribute("value") != null )
43  				{
44  					double density = D.getAttribute("value").getDoubleValue();
45  					//density *= unit;
46  														
47  					String defType = "";
48  					
49  					boolean hasComposite = ( element.getChild("composite") != null);
50  					boolean hasFraction = ( element.getChild("fraction") != null);
51  					
52  					if ( hasComposite && hasFraction )
53  					{
54  						throw new JDOMException("The material <"+name+"> has both <composite> and <fraction> components, which is not allowed!");
55  					}
56  					
57  					if ( hasComposite )
58  					{
59  						
60  						defType = compositeStr;
61  					}
62  					else if ( hasFraction )
63  					{
64  						defType = fractionStr;
65  					}
66  					else {
67  						throw new JDOMException("MaterialMixture <" + name + "> is missing at least one <composite> or <fraction> component.");
68  					}
69  					
70  					int ncomponents = element.getChildren(defType).size();
71  					
72  					MaterialMixture material = 
73  						new MaterialMixture(
74  				    		name,
75  				            ncomponents,
76  				            density,
77  				            IMaterial.Unknown
78  				            );				   						
79  					
80  					// Add by number of atoms.
81  					if ( hasComposite )
82  					{
83  						for ( Object obj : element.getChildren("composite"))
84  						{
85  							Element composite = (Element)obj;
86  							IMaterial matlkp = 
87  								MaterialStore.getInstance().get(composite.getAttributeValue("ref"));
88                                                          
89  							if ( matlkp == null )
90  							{
91  								throw new JDOMException("The material <" + composite.getAttributeValue("ref") + "> was not found!");
92  							}
93  							
94  							int n = composite.getAttribute("n").getIntValue();
95  							
96  							if ( matlkp instanceof MaterialElement )
97  							{
98  								material.addElement((MaterialElement)matlkp, n);
99  							}
100 							else if ( matlkp instanceof MaterialMixture )
101 							{
102 								material.addMaterial((MaterialMixture)matlkp, n);
103 							}							
104 						}
105 					}  
106 					// Add by mass fraction.
107 					// Already checked for neither or both.
108 					else {
109 						for ( Object obj : element.getChildren("fraction"))
110 						{
111 							Element fraction = (Element)obj;
112                             
113 							IMaterial matlkp =
114 								MaterialStore.getInstance().get(fraction.getAttributeValue("ref"));                                                      
115                                                                                     
116 							if ( matlkp == null )
117 							{
118 								throw new JDOMException("The material <" + fraction.getAttributeValue("ref") + "> was not found!");
119 							}
120 							
121 							double f = fraction.getAttribute("n").getDoubleValue();
122 							
123 							if ( matlkp instanceof MaterialElement )
124 							{
125 								material.addElement((MaterialElement)matlkp, f);
126 							}
127 							else if ( matlkp instanceof MaterialMixture )
128 							{
129 								material.addElement((MaterialMixture)matlkp, f);
130 							}
131 						}
132 					}
133 				}
134 				else {
135 					throw new JDOMException("The material <" + name + " is missing a density value.");
136 				}
137 				
138 			}
139 			else {
140 				throw new JDOMException("The material <" + name + "> is missing <D>.");
141 			}					
142 		}
143 		else {
144 			throw new JDOMException("Invalid element <" + element.getName() + "> for MaterialMixtureConverter.");
145 		}
146 	}	
147 }
148 
149 /*
150 
151 --Examples--
152 
153 <material name="Air">
154 <D type="density" unit="g/cm3" value="0.0012"/>
155 <fraction n="0.754" ref="N"/>
156 <fraction n="0.234" ref="O"/>
157 <fraction n="0.012" ref="Ar"/>
158 </material>
159 
160 <material name="Actinium">
161 <D type="density" unit="g/cm3" value="10.07" />
162 <composite n="1" ref="Ac" />
163 </material>
164 
165 */