View Javadoc

1   package org.lcsim.geometry.compact;
2   
3   import java.net.URL;
4   import java.util.ArrayList;
5   import java.util.HashMap;
6   import java.util.List;
7   import java.util.Map;
8   import java.util.Set;
9   
10  import org.jdom.Element;
11  import org.lcsim.detector.IDetectorElement;
12  import org.lcsim.detector.IPhysicalVolume;
13  import org.lcsim.detector.IPhysicalVolumeNavigator;
14  import org.lcsim.detector.PhysicalVolumeNavigator;
15  import org.lcsim.geometry.Calorimeter;
16  import org.lcsim.material.MaterialManager;
17  import org.lcsim.material.XMLMaterialManager;
18  
19  import Jama.Matrix;
20  
21  /**
22   * Default class created to represent the parsed detector.
23   * 
24   * @author tonyj
25   * @author jeremym
26   */
27  public class Detector {
28      
29      private Header header;
30      private Map<String, Constant> constants = new HashMap<String, Constant>();
31      private Map<String, Subdetector> subdetectors = new HashMap<String, Subdetector>();
32      private Map<String, Readout> readouts = new HashMap<String, Readout>();
33      private Map<String, Field> fields = new HashMap<String, Field>();
34      private Map<String, LimitSet> limits = new HashMap<String, LimitSet>();
35      private Map<String, Region> regions = new HashMap<String, Region>();
36      private Map<String, VisAttributes> display = new HashMap<String, VisAttributes>();
37      private Map<String, Matrix> matrices = new HashMap<String, Matrix>();
38      private XMLMaterialManager materialMgr; // Setup externally by CompactReader.
39      private List<URL> gdmlReferences = new ArrayList<URL>();
40  
41      private SystemIDMap idmap = new SystemIDMap();
42  
43      private IPhysicalVolume worldVolume;
44      private IPhysicalVolume trackingVolume;
45      private IPhysicalVolumeNavigator navigator;
46  
47      private IDetectorElement de;
48  
49      String name;
50  
51      /**
52       * Called by the reader to create a new Detector
53       * 
54       * @param element The JDOM element corresponding to the detector definition in the XML file.
55       */
56      protected Detector(Element element) {
57          if (element != null) {
58              name = element.getChild("info").getAttributeValue("name");
59          }
60      }
61  
62      protected void setXMLMaterialManager(XMLMaterialManager xmat) {
63          materialMgr = xmat;
64      }
65  
66      public String getName() {
67          return name;
68      }
69  
70      public IPhysicalVolume getWorldVolume() {
71          return worldVolume;
72      }
73  
74      public void setWorldVolume(IPhysicalVolume worldVolume) {
75          this.worldVolume = worldVolume;
76          navigator = new PhysicalVolumeNavigator("world", worldVolume);
77          trackingVolume = navigator.getPath("/tracking_region").getLeafVolume();
78      }
79  
80      public IPhysicalVolume getTrackingVolume() {
81          return trackingVolume;
82      }
83  
84      public IPhysicalVolumeNavigator getNavigator() {
85          return navigator;
86      }
87  
88      /**
89       * Called by the reader to associate a header with this detector
90       * 
91       * @param header The header.
92       */
93      protected void setHeader(Header header) {
94          this.header = header;
95      }
96  
97      /**
98       * Get the header associated with this detector.
99       * 
100      * @return The header
101      */
102     public Header getHeader() {
103         return header;
104     }
105 
106     /**
107      * Get the detector name from the header.
108      * 
109      * @return the detector name
110      */
111     public String getDetectorName() {
112         return getHeader().getDetectorName();
113     }
114 
115     /**
116      * Called by the reader to add a constant to this detector.
117      * 
118      * @param c The constant to add.
119      */
120     protected void addConstant(Constant c) {
121         constants.put(c.getName(), c);
122     }
123 
124     /**
125      * Get the constants associated with this detector.
126      * 
127      * @return A map of containing all of the constants, indexed by name.
128      */
129     public Map<String, Constant> getConstants() {
130         return constants;
131     }
132     
133     /**
134      * Add a matrix associated with this Detector, e.g. for material properties.
135      * @param name The name of the matrix.
136      * @param matrix The matrix object.
137      */
138     public void addMatrix(String name, Matrix matrix) {
139         this.matrices.put(name, matrix);
140     }
141     
142     /**
143      * Get a matrix associated with this Detector.
144      * @param name The name of the matrix.
145      * @return The matrix or null if it does not exist.
146      */
147     public Matrix getMatrix(String name) {
148         return this.matrices.get(name);
149     }
150     
151     /**
152      * Get the map of named matrices.
153      * @return The map of matrices for this detector.
154      */
155     public Map<String, Matrix> getMatrices() {
156         return this.matrices;
157     }
158 
159     /**
160      * Called by the reader to add a new Readout to this detector.
161      * 
162      * @param r The readout to add.
163      */
164     protected void addReadout(Readout r) {
165         // System.out.println("geometry.compact.Detector.addReadout() - " + r.getName() );
166         readouts.put(r.getName(), r);
167     }
168 
169     /**
170      * Get the readouts associated with this detector.
171      * 
172      * @return A map of containing all of the readouts, indexed by name.
173      */
174     public Map<String, Readout> getReadouts() {
175         return readouts;
176     }
177 
178     /**
179      * Convenience method to get readout by name.
180      **/
181     public Readout getReadout(String rn) {
182         return readouts.get(rn);
183     }
184 
185     /**
186      * Called by the reader to add a sub-detector to this detector.
187      * 
188      * @param sub The sub-detector to add.
189      */
190     protected void addSubdetector(Subdetector sub) {
191         // Check for a duplicate name.
192         if (subdetectors.get(sub.getName()) != null) {
193             throw new RuntimeException("The subdetector " + sub.getName() + " in detector " + this.getDetectorName() + " has the same name as another subdetector, which is not allowed.");
194         }
195         subdetectors.put(sub.getName(), sub);
196 
197         // Check for a duplicate system id.
198         if (sub.getSystemID() != 0 && idmap.get(sub.getSystemID()) != null) {
199             throw new RuntimeException("The subdetector " + sub.getName() + " in detector " + this.getDetectorName() + " duplicates the system ID " + sub.getSystemID() + " of the existing subdetector " + idmap.get(sub.getSystemID()).getName());
200         }
201 
202         idmap.add(sub.getSystemID(), sub);
203     }
204 
205     /**
206      * Get a Subdetector by system ID.
207      * 
208      * @param sysid The system identifier.
209      */
210     public Subdetector getSubdetector(int sysid) {
211         return idmap.get(sysid);
212     }
213 
214     /**
215      * Convenience method to retrieve subdetector by name.
216      * 
217      * @param subdetector with this name (null if doesn't exist)
218      */
219     public Subdetector getSubdetector(String name) {
220         return subdetectors.get(name);
221     }
222 
223     /**
224      * Convenience method to retrieve set of detector names.
225      * 
226      * @return set of subdetector name strings
227      */
228     public Set<String> getSubdetectorNames() {
229         return getSubdetectors().keySet();
230     }
231 
232     /**
233      * Get the sub-detectors associated with this detector.
234      * 
235      * @return A map of containing all of the sub-detectors, indexed by name.
236      */
237     public Map<String, Subdetector> getSubdetectors() {
238         return subdetectors;
239     }
240 
241     protected void addField(Field field) {
242         fields.put(field.getName(), field);
243     }
244 
245     public Map<String, Field> getFields() {
246         return fields;
247     }
248 
249     public final XMLMaterialManager getXMLMaterialManager() {
250         return materialMgr;
251     }
252 
253     public MaterialManager getMaterialManager() {
254         return MaterialManager.instance();
255     }
256 
257     public void addLimitSet(LimitSet limitset) {
258         limits.put(limitset.getName(), limitset);
259     }
260 
261     public LimitSet getLimitSet(String name) {
262         return limits.get(name);
263     }
264 
265     public Map<String, LimitSet> getLimits() {
266         return limits;
267     }
268 
269     public void addRegion(Region region) {
270         regions.put(region.getName(), region);
271     }
272 
273     public Region getRegion(String name) {
274         return regions.get(name);
275     }
276 
277     public Map<String, Region> getRegions() {
278         return regions;
279     }
280 
281     public void addVisAttributes(VisAttributes vis) {
282         display.put(vis.getName(), vis);
283     }
284 
285     public Map<String, VisAttributes> getVisAttributes() {
286         return display;
287     }
288 
289     public IDetectorElement getDetectorElement() {
290         return de;
291     }
292 
293     public void setDetectorElement(IDetectorElement de) {
294         this.de = de;
295     }
296 
297     public void addGDMLReference(URL url) {
298         gdmlReferences.add(url);
299     }
300 
301     public List<URL> getGDMLReferences() {
302         return gdmlReferences;
303     }
304 
305     public Calorimeter getCalorimeterByType(Calorimeter.CalorimeterType calType) {
306         for (Subdetector subdet : this.subdetectors.values()) {
307             if (subdet.isCalorimeter()) {
308                 Calorimeter cal = (Calorimeter) subdet;
309                 if (cal.getCalorimeterType() == calType)
310                     return (Calorimeter) subdet;
311             }
312         }
313         return null;
314     }
315 
316     public List<Subdetector> getSubdetectorList() {
317         return new ArrayList<Subdetector>(getSubdetectors().values());
318     }
319 }