View Javadoc

1   /*
2    * LayerBuilder.java
3    *
4    * Created on April 1, 2005, 4:23 PM
5    */
6   
7   package org.lcsim.geometry.layer;
8   
9   import java.util.ArrayList;
10  import java.util.Iterator;
11  import java.util.List;
12  
13  import org.jdom.Attribute;
14  import org.jdom.DataConversionException;
15  import org.jdom.Element;
16  import org.jdom.JDOMException;
17  
18  /**
19   *
20   * Converts from a compact detector description to a @see LayerStack object.
21   *
22   * @author Jeremy McCormick <jeremym@slac.stanford.edu>
23   */
24  public class LayerFromCompactCnv
25  {
26      private LayerFromCompactCnv()
27      {}
28      
29      public static LayerStack makeLayerStackFromCompact(Element e) throws JDOMException, DataConversionException
30      {        
31          if ( e == null )
32          {
33              throw new JDOMException("LayerFromCompactCnv.makeLayerStackFromCompact() - got null element");
34          }
35          
36          LayerStack layerStack = null;
37          
38          if ( e.getChildren("layer") != null)
39          {
40              layerStack = new LayerStack();
41              
42              for (Iterator i = e.getChildren("layer").iterator(); i.hasNext();)
43              {
44                  Element lyrElem = (Element) i.next();
45                  
46                  // Start a new layer.
47                  List lyrList = new ArrayList();
48                  
49                  // Default to one repetition.
50                  int repeat = 1;
51                  
52                  // Get repeat value from element if exists.
53                  Attribute repAttrib = lyrElem.getAttribute("repeat");
54                  if ( repAttrib != null )
55                  {
56                      repeat = repAttrib.getIntValue();
57                  }
58                 
59                  for (Iterator j = lyrElem.getChildren("slice").iterator(); j.hasNext(); )
60                  {
61                      Element slice = (Element) j.next();
62                      
63                      Attribute matAttrib = slice.getAttribute("material");
64                      
65                      if ( matAttrib == null )
66                      {
67                          throw new JDOMException("No material attribute found in this slice.");
68                      }
69                      
70                      String matName = slice.getAttributeValue("material");
71                      
72                      Attribute thickness = slice.getAttribute("thickness");
73                      
74                      if ( thickness == null )
75                      {
76                          throw new JDOMException("No thickness attribute found in this slice.");
77                      }
78                      
79                      Attribute sensAttrib = slice.getAttribute("sensitive");
80                      
81                      /* Default to not sensitive. */
82                      boolean isSens = false;
83                      
84                      if ( sensAttrib != null )
85                      {
86                          isSens = sensAttrib.getBooleanValue();
87                      }
88                      
89                      // Make a new slice. Ctor will look up material. 
90                      LayerSlice s = new LayerSlice(matName,
91                              thickness.getDoubleValue(),
92                              isSens);
93                      
94                      // Add slice to current layer.                    
95                      lyrList.add(s);
96                  }
97                  
98                  Layer lyr = new Layer(lyrList);                
99                  
100                 // Add layer to stack once for each repetition.
101                 for (int k = 0; k < repeat; k++)
102                 {
103                     layerStack.addLayer(lyr);
104                 }
105             }
106         }
107         else
108         {
109             System.err.println("LayerFromCompactCnv.makeLayerStackFromCompact() - no layer child elements to build");
110         }
111         
112         return layerStack;
113     }
114     
115     /** 
116      * Compute the thickness of a single layer, ignoring the repeat attribute. 
117      */
118     public static double computeSingleLayerThickness(Element node) throws JDOMException
119     {
120         if ( node.getName() != "layer" )
121         {
122             throw new JDOMException("LayerCompactCnv.computeLayerThickness() takes layer element, not " + node.getName() );
123         }
124         
125         double thickness = 0.0;
126         for ( Object o : node.getChildren("slice") )
127         {
128             Element sliceElem = (Element) o;
129             thickness += sliceElem.getAttribute("thickness").getDoubleValue();
130         }
131         return thickness;
132     }
133     
134     public static double computeDetectorTotalThickness(Element node) throws JDOMException
135     {
136         double totalThickness = 0;
137         for (Iterator i = node.getChildren("layer").iterator(); i.hasNext();)
138         {            
139             Element layer = (Element) i.next();
140             int repeat = 1;
141             if (layer.getAttribute("repeat") != null)
142             {
143             	repeat = (int)layer.getAttribute("repeat").getDoubleValue();
144             }
145             for ( int j=0; j<repeat; j++)
146             {
147                 for ( Iterator k = layer.getChildren("slice").iterator(); k.hasNext();)
148                 {
149                     Element slice = (Element) k.next();
150                     totalThickness += slice.getAttribute("thickness").getDoubleValue();
151                 }
152             }
153         }
154         return totalThickness;
155     }
156 }