Print

Print


Author: [log in to unmask]
Date: Tue May  5 15:28:45 2015
New Revision: 3601

Log:
Use Reflections to lookup subdetector converters so that ones in external packages will be found at runtime.

Modified:
    projects/lcsim/branches/LCSIM-245/detector-framework/pom.xml
    projects/lcsim/branches/LCSIM-245/detector-framework/src/main/java/org/lcsim/detector/converter/compact/DetectorConverter.java

Modified: projects/lcsim/branches/LCSIM-245/detector-framework/pom.xml
 =============================================================================
--- projects/lcsim/branches/LCSIM-245/detector-framework/pom.xml	(original)
+++ projects/lcsim/branches/LCSIM-245/detector-framework/pom.xml	Tue May  5 15:28:45 2015
@@ -105,6 +105,10 @@
             <artifactId>commons-lang3</artifactId>
             <version>3.3.2</version>
         </dependency>
+        <dependency>
+            <groupId>org.reflections</groupId>
+            <artifactId>reflections</artifactId>
+            <version>0.9.9</version>
+        </dependency>
     </dependencies>
-    
 </project>

Modified: projects/lcsim/branches/LCSIM-245/detector-framework/src/main/java/org/lcsim/detector/converter/compact/DetectorConverter.java
 =============================================================================
--- projects/lcsim/branches/LCSIM-245/detector-framework/src/main/java/org/lcsim/detector/converter/compact/DetectorConverter.java	(original)
+++ projects/lcsim/branches/LCSIM-245/detector-framework/src/main/java/org/lcsim/detector/converter/compact/DetectorConverter.java	Tue May  5 15:28:45 2015
@@ -1,13 +1,16 @@
 package org.lcsim.detector.converter.compact;
 
 import java.io.IOException;
+import java.lang.reflect.Modifier;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 
 import org.jdom.Document;
 import org.jdom.Element;
 import org.jdom.JDOMException;
 import org.lcsim.detector.DetectorElement;
+import org.lcsim.detector.DetectorIdentifierHelper.SystemMap;
 import org.lcsim.detector.DetectorStore;
 import org.lcsim.detector.IDetectorElement;
 import org.lcsim.detector.ILogicalVolume;
@@ -16,7 +19,6 @@
 import org.lcsim.detector.ParametersStore;
 import org.lcsim.detector.PhysicalVolume;
 import org.lcsim.detector.PhysicalVolumeNavigatorStore;
-import org.lcsim.detector.DetectorIdentifierHelper.SystemMap;
 import org.lcsim.detector.converter.lcdd.MaterialElementConverter;
 import org.lcsim.detector.converter.lcdd.MaterialMixtureConverter;
 import org.lcsim.detector.converter.lcdd.MaterialsConverter;
@@ -29,9 +31,9 @@
 import org.lcsim.geometry.compact.Constant;
 import org.lcsim.geometry.compact.Subdetector;
 import org.lcsim.geometry.subdetector.PolyconeSupport;
-
-public class DetectorConverter implements IDetectorConverter
-{
+import org.reflections.Reflections;
+
+public class DetectorConverter implements IDetectorConverter {
     // Map of class to converter.
     Map<Class, ISubdetectorConverter> subdetectorConverters = new HashMap<Class, ISubdetectorConverter>();
 
@@ -46,9 +48,7 @@
     // The SystemMap for setting up the IdentifierHelpers.
     SystemMap sysMap;
 
-    public IPhysicalVolume convert(Detector detector, Document doc) throws JDOMException,
-            IOException
-    {
+    public IPhysicalVolume convert(Detector detector, Document doc) throws JDOMException, IOException {
         // Clear out old DetectorStore store before building new detector.
         DetectorStore.getInstance().clear();
 
@@ -84,106 +84,76 @@
         return pvWorld;
     }
 
-    public DetectorConverter()
-    {
-        // Simple cylindrical detectors.
-        addSubdetectorConverter(new CylindricalBarrelCalorimeterConverter());
-        addSubdetectorConverter(new CylindricalEndcapCalorimeterConverter());
-        addSubdetectorConverter(new MultiLayerTrackerConverter());
-        addSubdetectorConverter(new DiskTrackerConverter());
-
-        // Detailed tracker models.
-        addSubdetectorConverter(new SiTrackerBarrelConverter());
-        addSubdetectorConverter(new SiTrackerEndcapConverter());
-        addSubdetectorConverter(new SiTrackerEndcap2Converter());
-        addSubdetectorConverter(new SiTrackerFixedTargetConverter());
-        addSubdetectorConverter(new SiTrackerFixedTarget2Converter());
-        addSubdetectorConverter(new SiTrackerSpectrometerConverter());
-
-        // Detailed calorimeter models.
-        addSubdetectorConverter(new PolyhedraBarrelCalorimeterConverter());
-        addSubdetectorConverter(new PolyhedraBarrelCalorimeter2Converter());
-        addSubdetectorConverter(new PolyhedraEndcapCalorimeter2Converter());
-        addSubdetectorConverter(new PolyhedraEndcapCalorimeterConverter());
-        addSubdetectorConverter(new EcalBarrelConverter());
-
-        // HPS
-        addSubdetectorConverter(new HPSTrackerConverter());
-        addSubdetectorConverter(new HPSTracker2Converter());
-        addSubdetectorConverter(new HPSEcalConverter());
-        addSubdetectorConverter(new HPSEcal2Converter());
-        addSubdetectorConverter(new HPSEcal3Converter());
-        addSubdetectorConverter(new HPSMuonCalorimeterConverter());
-        //addSubdetectorConverter(new HPSTestRunTracker2014Converter());
-        //addSubdetectorConverter(new HPSTracker2014Converter());
-        //addSubdetectorConverter(new HPSTracker2014v1Converter());
-
-        // Support structures.
-        addSubdetectorConverter(new PolyconeSupportConverter());
-        addSubdetectorConverter(new TubeSegmentConverter());        
-    }
-
-    private void addSubdetectorConverter(ISubdetectorConverter s)
-    {
-        if (subdetectorConverters.get(s.getSubdetectorType()) != null)
-        {
-            throw new IllegalArgumentException("Already have converter for <"
-                    + s.getSubdetectorType().getCanonicalName() + "> !");
-        }
-
+    public DetectorConverter() {
+
+        System.out.println("DetectorConverter initializing ...");
+
+        Reflections reflect = new Reflections("org.lcsim.detector.converter.compact");
+        Set<Class<? extends AbstractSubdetectorConverter>> converters = reflect
+                .getSubTypesOf(AbstractSubdetectorConverter.class);
+
+        if (converters.size() == 0) {
+            throw new RuntimeException("No subdetector converter classes were found.");
+        }
+
+        for (Class<? extends AbstractSubdetectorConverter> converter : converters) {
+            try {
+                if (!Modifier.isAbstract(converter.getModifiers())) {
+                    System.out.println("adding subdet converter: " + converter);
+                    this.addSubdetectorConverter(converter.newInstance());
+                }
+            } catch (InstantiationException | IllegalAccessException e) {
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    private void addSubdetectorConverter(ISubdetectorConverter s) {
+        // if (subdetectorConverters.get(s.getSubdetectorType()) != null) {
+        // throw new IllegalArgumentException("Already have converter for <"
+        // + s.getSubdetectorType().getCanonicalName() + "> !");
+        // }
         subdetectorConverters.put(s.getSubdetectorType(), s);
     }
 
-    private ISubdetectorConverter getSubdetectorConverter(Class klass)
-    {
+    private ISubdetectorConverter getSubdetectorConverter(Class klass) {
         return subdetectorConverters.get(klass);
     }
 
-    public IPhysicalVolume convert(Detector detector, String resource) throws JDOMException,
-            IOException
-    {
+    public IPhysicalVolume convert(Detector detector, String resource) throws JDOMException, IOException {
         return convert(detector, CompactDocumentBuilder.build(resource));
     }
 
-    private void convertMaterials(Document doc) throws JDOMException
-    {
+    private void convertMaterials(Document doc) throws JDOMException {
         materialCnv.convert(doc);
     }
 
-    private void convertMaterials(String resource) throws JDOMException, IOException
-    {
+    private void convertMaterials(String resource) throws JDOMException, IOException {
         Document doc = CompactDocumentBuilder.build(resource);
 
-        for (Object obj : doc.getRootElement().getChildren())
-        {
+        for (Object obj : doc.getRootElement().getChildren()) {
             Element e = (Element) obj;
-            if (e.getName().equals("element"))
-            {
+            if (e.getName().equals("element")) {
                 elemCnv.convert(e);
-            }
-            else if (e.getName().equals("material"))
-            {
+            } else if (e.getName().equals("material")) {
                 matCnv.convert(e);
             }
         }
     }
 
-    private void convertSubdetectors(Detector detector)
-    {
+    private void convertSubdetectors(Detector detector) {
         // Process all Subdetectors in the Detector.
-        for (Subdetector subdetector : detector.getSubdetectors().values())
-        {
+        for (Subdetector subdetector : detector.getSubdetectors().values()) {
             // System.out.println("subdetector: " + subdetector.getName());
 
             // Find a converter for this type.
             ISubdetectorConverter cnv = getSubdetectorConverter(subdetector.getClass());
 
-            if (cnv != null)
-            {
+            if (cnv != null) {
                 // System.out.println("found cnv handling: " +
                 // cnv.getSubdetectorType().getCanonicalName());
-                DetectorElement subdetectorDE = (DetectorElement) cnv
-                        .makeSubdetectorDetectorElement(detector, subdetector);
+                DetectorElement subdetectorDE = (DetectorElement) cnv.makeSubdetectorDetectorElement(detector,
+                        subdetector);
 
                 // System.out.println("made subdet DE: " +
                 // subdetectorDE.getName());
@@ -192,12 +162,9 @@
                 IIdentifierHelper helper = cnv.makeIdentifierHelper(subdetector, sysMap);
 
                 // Convert the parameters.
-                try
-                {
+                try {
                     paramCnv.convert(subdetector.getNode());
-                }
-                catch (JDOMException x)
-                {
+                } catch (JDOMException x) {
                     throw new RuntimeException(x);
                 }
 
@@ -216,8 +183,7 @@
                 // are not really detectors but dead material so this check is
                 // necessary
                 // to avoid errors.
-                if (subdetDE != null)
-                {
+                if (subdetDE != null) {
                     // Make the Parameters from the compact detector element
                     // and assign to the Subdetector's DetectorElement.
                     subdetDE.setParameters(ParametersStore.getInstance().get(subdetector.getName()));
@@ -240,13 +206,10 @@
         }
     }
 
-    private void buildTrackingVolume(ILogicalVolume world, Detector detector)
-    {
+    private void buildTrackingVolume(ILogicalVolume world, Detector detector) {
         Map<String, Constant> constants = detector.getConstants();
 
-        if (constants.get("tracking_region_zmax") == null
-                || constants.get("tracking_region_radius") == null)
-        {
+        if (constants.get("tracking_region_zmax") == null || constants.get("tracking_region_radius") == null) {
             throw new RuntimeException("Missing parameters for defining tracking region!");
         }
 
@@ -255,19 +218,16 @@
 
         Tube trackingTube = new Tube("tracking_region_tube", 0, radius, zmax);
 
-        LogicalVolume trackingLV = new LogicalVolume("tracking_region", trackingTube, MaterialStore
-                .getInstance().get("Air"));
+        LogicalVolume trackingLV = new LogicalVolume("tracking_region", trackingTube, MaterialStore.getInstance().get(
+                "Air"));
 
         new PhysicalVolume(null, "tracking_region", trackingLV, world, 0);
     }
 
-    private IPhysicalVolume buildWorldVolume(Detector detector)
-    {
+    private IPhysicalVolume buildWorldVolume(Detector detector) {
         Map<String, Constant> constants = detector.getConstants();
 
-        if (constants.get("world_x") == null || constants.get("world_y") == null
-                || constants.get("world_z") == null)
-        {
+        if (constants.get("world_x") == null || constants.get("world_y") == null || constants.get("world_z") == null) {
             throw new RuntimeException("Missing world_x, world_y, or world_z!");
         }
 
@@ -290,73 +250,43 @@
     // id.
     // TODO: Must be a better way to setup these associations in the
     // DetectorIdentifierHelper.
-    public static final SystemMap makeSystemMap(Detector d)
-    {
+    public static final SystemMap makeSystemMap(Detector d) {
         SystemMap m = new SystemMap();
 
-        for (Subdetector subdet : d.getSubdetectors().values())
-        {
+        for (Subdetector subdet : d.getSubdetectors().values()) {
             String name = subdet.getName();
 
             int sys = subdet.getSystemID();
 
             // Based on naming conventions from sid01 and sid02.
-            if (!name.contains("Support") && !(subdet instanceof PolyconeSupport))
-            {
-                if (name.contains("VertexBarrel"))
-                {
+            if (!name.contains("Support") && !(subdet instanceof PolyconeSupport)) {
+                if (name.contains("VertexBarrel")) {
                     m.put("vtxBarrel", sys);
-                }
-                else if (name.contains("VertexEndcap"))
-                {
+                } else if (name.contains("VertexEndcap")) {
                     m.put("vtxEndcap", sys);
-                }
-                else if (name.contains("TrackerBarrel"))
-                {
+                } else if (name.contains("TrackerBarrel")) {
                     m.put("sitBarrel", sys);
-                }
-                else if (name.contains("TrackerEndcap"))
-                {
+                } else if (name.contains("TrackerEndcap")) {
                     m.put("sitEndcap", sys);
-                }
-                else if (name.contains("TrackerForward"))
-                {
+                } else if (name.contains("TrackerForward")) {
                     m.put("sitForward", sys);
-                }
-                else if (name.contains("TPC"))
-                {
+                } else if (name.contains("TPC")) {
                     m.put("tpc", sys);
-                }
-                else if (name.contains("EMBarrel"))
-                {
+                } else if (name.contains("EMBarrel")) {
                     m.put("ecalBarrel", sys);
-                }
-                else if (name.contains("EMEndcap") && !name.contains("Forward"))
-                {
+                } else if (name.contains("EMEndcap") && !name.contains("Forward")) {
                     m.put("ecalEndcap", sys);
-                }
-                else if (name.contains("HADBarrel"))
-                {
+                } else if (name.contains("HADBarrel")) {
                     m.put("hcalBarrel", sys);
-                }
-                else if (name.contains("HADEndcap"))
-                {
+                } else if (name.contains("HADEndcap")) {
                     m.put("hcalEndcap", sys);
-                }
-                else if (name.contains("MuonBarrel"))
-                {
+                } else if (name.contains("MuonBarrel")) {
                     m.put("muonBarrel", sys);
-                }
-                else if (name.contains("MuonEndcap"))
-                {
+                } else if (name.contains("MuonEndcap")) {
                     m.put("muonEndcap", sys);
-                }
-                else if (name.contains("LuminosityMonitor") || name.contains("LumiCal"))
-                {
+                } else if (name.contains("LuminosityMonitor") || name.contains("LumiCal")) {
                     m.put("lumi", sys);
-                }
-                else if (name.contains("ForwardEMEndcap"))
-                {
+                } else if (name.contains("ForwardEMEndcap")) {
                     m.put("ecalForward", sys);
                 }
             }

########################################################################
Use REPLY-ALL to reply to list

To unsubscribe from the LCDET-SVN list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=LCDET-SVN&A=1