View Javadoc

1   package org.lcsim.geometry.compact.converter.lcdd;
2   
3   import org.jdom.Element;
4   import org.jdom.JDOMException;
5   import org.lcsim.geometry.compact.Subdetector;
6   import org.lcsim.geometry.compact.converter.lcdd.util.LCDD;
7   import org.lcsim.geometry.compact.converter.lcdd.util.LimitSet;
8   import org.lcsim.geometry.compact.converter.lcdd.util.Region;
9   import org.lcsim.geometry.compact.converter.lcdd.util.SensitiveDetector;
10  import org.lcsim.geometry.compact.converter.lcdd.util.Tracker;
11  import org.lcsim.geometry.compact.converter.lcdd.util.VisAttributes;
12  import org.lcsim.geometry.compact.converter.lcdd.util.Volume;
13  import org.lcsim.geometry.layer.Layering;
14  
15  /**
16   * 
17   * @author tonyj
18   */
19  public abstract class LCDDSubdetector extends Subdetector
20  {
21      protected Element node;
22      protected Layering layers;
23  
24      LCDDSubdetector(Element c) throws JDOMException
25      {
26          super(c);
27          node = c;
28  
29          /* make compact layering descr for thickness calcs in addToLCDD */
30          if (layers == null)
31          {
32          	try {
33          		layers = org.lcsim.geometry.layer.Layering.makeLayering(c);
34          	}
35          	catch (JDOMException x)
36          	{
37          		System.err.println("WARNING: " + x.getMessage());
38          	}
39          }
40      }
41  
42      abstract void addToLCDD(LCDD lcdd, SensitiveDetector sens)
43              throws JDOMException;
44  
45      public Element getElement()
46      {
47          return node;
48      }
49  
50      /**
51  	 * Set limits of an LCDD volume
52  	 * 
53  	 * @param lcdd
54  	 *            LCDD object currently being processed
55  	 * @param node
56  	 *            Current DOM node (e.g. detector, layer, slice, etc.)
57  	 * @param volume
58  	 *            The volume corresponding to the DOM node
59  	 */
60      public final static void setLimitSet(LCDD lcdd, Element node, Volume volume)
61      {
62          if (node.getAttribute("limits") != null)
63          {
64              String limitref = node.getAttributeValue("limits");
65              LimitSet limitset = lcdd.getLimitSet(limitref);
66              if (limitset != null)
67              {
68                  volume.setLimitSet(limitset);
69              }
70              else
71              {
72                  throw new RuntimeException("limitset " + limitref
73                          + " does not exist");
74              }
75          }
76      }
77  
78      /**
79  	 * 
80  	 * Set the region of an LCDD volume
81  	 * 
82  	 * @param lcdd
83  	 *            LCDD object currently being processed
84  	 * @param node
85  	 *            Current DOM node (e.g. detector, layer, slice, etc.)
86  	 * @param volume
87  	 *            The volume corresponding to the DOM node
88  	 * 
89  	 */
90      public final static void setRegion(LCDD lcdd, Element node, Volume volume)
91      {
92          if (node.getAttribute("region") != null)
93          {
94              String regionref = node.getAttributeValue("region");
95              Region region = lcdd.getRegion(regionref);
96              if (region != null)
97              {
98                  volume.setRegion(region);
99              }
100             else
101             {
102                 throw new RuntimeException("Region " + regionref
103                         + " does not exist");
104             }
105         }
106     }
107 
108     /**
109 	 * 
110 	 * Set combine_hits attribute of LCDD sensitive detector
111 	 * 
112 	 * @param node
113 	 *            detector XML node
114 	 * @param sens
115 	 *            sensitive detector which should be of type Tracker
116 	 */
117     public final static void setCombineHits(Element node, SensitiveDetector sens)
118     {
119         if (node.getAttribute("combineHits") != null)
120         {
121         	//System.out.println("combineHits=" + node.getAttributeValue("combineHits"));
122             try
123             {
124             	if (sens instanceof Tracker)
125                 {
126             		if (node.getAttribute("combineHits").getBooleanValue() == true)
127             		{                    
128                         sens.setAttribute("combine_hits", "true");                 
129             		}
130             		else 
131             		{
132             			sens.setAttribute("combine_hits", "false");
133             		}
134                 }
135             }
136             catch (Exception e)
137             {
138                 throw new RuntimeException(e);
139             }
140         }
141     }
142     
143     /**
144      * Utility method for checking and setting lcdd visualization parameters on a volume.
145      * If the XML node does not have a visualization reference, the volume is turned off 
146      * completely unless it is a detector; the showDaughters attribute is set to true
147      * so that sub-volumes are not turned off even if they have visualization settings.  
148      * Detectors are automatically added to the scene unless specifically turned off with 
149      * visualization settings, so this default does not apply when the XML node is called
150      * <detector>.
151      * 
152      * This method is used extensively by subclasses in their addToLCDD() methods for
153      * assigning visualization attributes to detector envelopes, layer envelopes,
154      * and layer slices.
155      * 
156      * @param lcdd The LCDD structure being built.
157      * @param node The XML node with the possible visualization reference.
158      * @param volume The volume to which visualization is being assigned.
159      */
160     public final static void setVisAttributes(LCDD lcdd, Element node, Volume volume)
161     {
162     	if (lcdd == null)
163     		throw new IllegalArgumentException("The lcdd object points to null.");
164     	
165     	if (node == null)
166     		throw new IllegalArgumentException("The node object points to null.");
167     	
168     	if (volume == null)
169     		throw new IllegalArgumentException("The volume object points to null.");
170     	
171         if (node.getAttribute("vis") != null)
172         {      	
173             String visref = node.getAttributeValue("vis");
174             VisAttributes vis = lcdd.getVisAttributes(visref);
175             if (vis != null)
176             {
177                 volume.setVisAttributes(vis);
178             }
179             else
180             {
181                 throw new RuntimeException("The vis reference " + visref + " does not exist.");
182             }
183         }
184         else
185         {
186         	// Slices are turned off by default.
187         	if (node.getName().equals("slice"))
188         		volume.setVisAttributes(lcdd.getVisAttributes("InvisibleNoDaughters"));
189         	// Layers are also turned off by default but daughters are left possibly visible.
190         	else if (node.getName().equals("layer"))
191         		volume.setVisAttributes(lcdd.getVisAttributes("InvisibleWithDaughters"));
192         	// Tracker modules are similar to layers.
193         	else if (node.getName().equals("module"))
194         		volume.setVisAttributes(lcdd.getVisAttributes("InvisibleWithDaughters"));
195         	// Tracker module components are turned off by default.
196         	else if (node.getName().equals("module_component"))
197         		volume.setVisAttributes(lcdd.getVisAttributes("InvisibleNoDaughters"));
198         }
199     }
200     
201     public void setAttributes(LCDD lcdd, Element node, Volume volume)
202     {
203     	setRegion(lcdd, node, volume);
204     	setLimitSet(lcdd, node, volume);
205     	setVisAttributes(lcdd, node, volume);
206     }
207 }