Commit in GeomConverter on MAIN
src/org/lcsim/geometry/compact/CompactReader.java+17-171.46 -> 1.47
                              /Detector.java+8-91.31 -> 1.32
src/org/lcsim/material/XMLMaterialManager.java+168-1501.21 -> 1.22
test/org/lcsim/geometry/layer/LayerStackTest.java+11.7 -> 1.8
test/org/lcsim/material/RadiationLengthTest.java+4-21.2 -> 1.3
                       /XMLMaterialManagerTest.java+31.8 -> 1.9
+201-178
6 modified files
few more changes and refactorings in org.lcsim.material; GeomConv and LCSim tests should all pass

GeomConverter/src/org/lcsim/geometry/compact
CompactReader.java 1.46 -> 1.47
diff -u -r1.46 -r1.47
--- CompactReader.java	11 Mar 2011 19:22:20 -0000	1.46
+++ CompactReader.java	12 Mar 2011 00:37:28 -0000	1.47
@@ -28,7 +28,7 @@
  * org.lcsim.geometry.GeometryReader class, which extends this.
  *
  * @author tonyj
- * @version $Id: CompactReader.java,v 1.46 2011/03/11 19:22:20 jeremy Exp $
+ * @version $Id: CompactReader.java,v 1.47 2011/03/12 00:37:28 jeremy Exp $
  *
  */
 public class CompactReader
@@ -88,7 +88,7 @@
         readConstants(lccdd, det);
         readRegions(lccdd, det);
         readLimits(lccdd, det);
-        //readMaterials(lccdd, det);
+        readMaterials(lccdd, det);
         Map<String,Readout> readoutMap = readReadouts(lccdd, det);
         readVisAttributes(lccdd, det);
         readSubdetectors(lccdd, det, readoutMap);        
@@ -230,26 +230,26 @@
             }
         }
     }    
-    
-    /*
-    private void readMaterials(org.jdom.Element compact)
+        
+    private void readMaterials(org.jdom.Element compact, Detector det)
     {
+        // Setup XMLMatMgr's default data.  This needs to be called before
+        // the compact materials are loaded in order to resolve references.
+        XMLMaterialManager.setup();
+        
+        // Create XMLMatMgr for this detector's materials.
         if (compact.getChild("materials") != null)
         {
             xmat = new XMLMaterialManager(compact.getChild("materials"));
+            
+            // FIXME Need to call this here???
+            //matmgr.addReferencesFromCompact(lccdd);
         }
-    }
-    */
-    
-    /** Create impl objects of materials and their references. */
-/*
-    private void readMaterials(org.jdom.Element lccdd, Detector det) throws JDOMException
-    {
-        XMLMaterialManager matmgr = det.getMaterialManager();
-        matmgr.addReferencesFromCompact(lccdd);
-        matmgr.makeMaterials(null);
-    }
-*/
+        
+        // Set the detector's XMLMaterialManager, so it is accessible to 
+        // clients such as the LCDD converter.
+        det.setXMLMaterialManager(xmat);
+    }        
     
     /**
      * Create the VisAttributes objects from vis elements in the display block.

GeomConverter/src/org/lcsim/geometry/compact
Detector.java 1.31 -> 1.32
diff -u -r1.31 -r1.32
--- Detector.java	11 Mar 2011 19:22:20 -0000	1.31
+++ Detector.java	12 Mar 2011 00:37:28 -0000	1.32
@@ -32,7 +32,7 @@
     private Map<String,LimitSet> limits = new HashMap<String,LimitSet>();
     private Map<String,Region> regions = new HashMap<String,Region>();
     private Map<String,VisAttributes> display = new HashMap<String,VisAttributes>();
-    private XMLMaterialManager materialMgr;
+    private XMLMaterialManager materialMgr; // Setup externally by CompactReader.
     private List<URL> gdmlReferences = new ArrayList<URL>();
     
     private SystemIDMap idmap = new SystemIDMap();
@@ -51,18 +51,17 @@
      */
     protected Detector(Element element)
     {
-        if ( element != null )
+        if (element != null)
         {
-            name = element.getChild("info").getAttributeValue("name");
-                
-            // Create XMLMaterialManager for this Detector.
-            if (element.getChild("materials") != null)
-            {
-                materialMgr = new XMLMaterialManager(element.getChild("materials"));
-            }
+            name = element.getChild("info").getAttributeValue("name");                
         }        
     }
     
+    protected void setXMLMaterialManager(XMLMaterialManager xmat)
+    {
+        materialMgr = xmat;
+    }
+    
     public String getName()
     {
         return name;

GeomConverter/src/org/lcsim/material
XMLMaterialManager.java 1.21 -> 1.22
diff -u -r1.21 -r1.22
--- XMLMaterialManager.java	11 Mar 2011 19:22:20 -0000	1.21
+++ XMLMaterialManager.java	12 Mar 2011 00:37:28 -0000	1.22
@@ -12,7 +12,7 @@
 import org.jdom.input.SAXBuilder;
 
 /**
- * This class loads material data from XML files into the global {@link MaterialManager}.
+ * This class loads GDML data from a materials XML section into the {@link MaterialManager}.  
  * 
  * It is also used to keep track of compact material data for the conversion to/from
  * compact descriptions.
@@ -21,14 +21,17 @@
  * term "node" to refer to an XML element.
  * 
  * @author jeremym
- * @version $Id: XMLMaterialManager.java,v 1.21 2011/03/11 19:22:20 jeremy Exp $
+ * @version $Id: XMLMaterialManager.java,v 1.22 2011/03/12 00:37:28 jeremy Exp $
  */
-// TODO Refactor material reference resolution spaghetti code.
-// TODO Replace parent/child structure with single manager.
-// FIXME Should not keep references to XML nodes from compact.  To copy from compact to LCDD,
-// just the MaterialManager should be used to resolve references.  XML can be constructed
-// from Material and MaterialElement objects rather than cloned from references to XML elements.
-// FIXME Move most of this logic to the CompactReader class.
+// TODO Refactor spaghetti/recursion code that does reference resolution.
+// TODO Replace parent/child structure of managers with a single manager or utility class.
+// TODO Move most of the logic for loading materials to the CompactReader class.
+// TODO Simplify the find/get methods for looking up XML elements.  There are too many similar methods.
+// TODO Where possible, move additional methods into LCDDMaterialHelper class.
+// FIXME Should not keep references to XML nodes.  To copy from compact to LCDD,
+// only the MaterialManager should be used to resolve references.  LCDD material XML 
+// can be constructed from Material and MaterialElement objects on the fly rather than cloned.
+// FIXME Deal with multiple unnecessary calls to setup() from clients.
 public class XMLMaterialManager
 {    
     // Location of elements resource in jar relative to org.lcsim.material pkg.
@@ -38,13 +41,13 @@
     private static final String materialResource = "materials.xml";
     
     // Instance of MaterialManager.
-    private static MaterialManager mgr = MaterialManager.instance();
+    private static MaterialManager mgr;
             
     // Static instance containing elements data.
-    private static XMLMaterialManager elements = createMaterialElements();
+    private static XMLMaterialManager elements;
 
     // Static instance containing materials data.
-    private static XMLMaterialManager materials = createMaterials();
+    private static XMLMaterialManager materials;
 
     // Parent manager of this one. 
     private XMLMaterialManager parent;
@@ -57,70 +60,54 @@
 
     // Map of names to material element XML nodes known by this manager.
     private HashMap<String, org.jdom.Element> elementMap = new LinkedHashMap<String, Element>();
-        
+    
     /**
-     * Reset state of static material data.
-     */
-    // TODO Call this from within compact.Detector?
-    public static void reset()
+     * Setup static material data.  This can be called to reset material data between runs
+     * with different Detectors that are run in the same Java process, such as a TestCase.  
+     * This method must be called before the XMLMaterialManager is created for a compact document.
+     */    
+    public static void setup()
     {
-        System.out.println("XMLMatMgr.reset()");
+        //System.out.println("XMLMatMgr.setup()");
         
-        // Clear the material store.
+        // Clear the global material store.
         MaterialManager.instance().clear();
+        mgr = MaterialManager.instance();
                         
-        // Build the element and material managers.
+        // Rebuild the default material data stores.
         elements = createMaterialElements();
-        materials = createMaterials();
+        materials = createMaterials();        
     }
     
     /**
-     * Get the map of XML material nodes known by this manager.
-     * @return The map of names to material nodes.
-     */
-    public Map<String, Element> getMaterialXMLMap()
-    {
-        return materialMap;
-    }
-    
-    /**
-     * Get the map of XML chemical element nodes known by this manager.
-     * @return The map of names to chemical element nodes.
-     */
-    public Map<String, Element> getElementXMLMap()
-    {
-        return elementMap;
-    }
-            
-    /**
-     * Create the static instance of the elements manager.
-     * @return The manager containing elements data.
+     * Ctor for a manager with the given root XML node, probably pointing to the
+     * materials section of a compact detector description.  Its parent is the default 
+     * material manager.
+     * @param materialsRoot The XML element pointing to a materials section.
      */
-    static private XMLMaterialManager createMaterialElements()
-    {
-        elements = new XMLMaterialManager();
-        elements.loadElementsFromResource();
-        return elements; 
-    }
-    
-    /**
-     * Create the static instance of the material manager.
-     * @return The manager containing default materials data.
-     */
-    static private XMLMaterialManager createMaterials()
+    public XMLMaterialManager(Element materialsRoot)
     {
-        materials = new XMLMaterialManager(elements);
-        materials.loadMaterialsFromResource(materialResource);
-        return materials;
+        // Bootstrap if static data is not setup yet.
+        if (elements == null)
+        {
+            setup();
+        }
+        parent = getDefaultMaterialManager();
+        setMaterialsRoot(materialsRoot);
+        loadMaterialsFromXML(materialsRoot);
     }
-     
+               
     /** 
-     * Get manager with default materials.
-     * This method can be called to "bootstrap" the static material objects. 
-     * @return The manager with references to the default materials from the embedded resource.    
+     * Get manager with default materials defined in the embedded resource file. 
+     * @return The manager with references to the default material manager.    
      **/        
     public static XMLMaterialManager getDefaultMaterialManager()
     {
+        // Bootstrap if static data is not setup yet.
+        if (elements == null)
+        {
+            setup();
+        }
         return materials;
     }    
     
@@ -130,90 +117,32 @@
      **/        
     public static XMLMaterialManager getElementsManager()
     {
+        // Bootstrap if static data is not setup yet.
+        if (elements == null)
+        {
+            setup();
+        }
         return elements;
     }
-     
-    /**
-     * The default ctor should not be used.  Any "user" managers
-     * must have a parent and/or a node pointing to a materials section.
-     */
-    private XMLMaterialManager()
-    {}
-    
+         
     /**
-     * Ctor for manager with the given root XML node.  
-     * Its parent is the default material manager.
-     * @param materialsRoot The XML element pointing to materials section.
+     * Get the map of XML material nodes known by this manager.
+     * @return The map of names to material nodes.
      */
-    public XMLMaterialManager(Element materialsRoot)
+    public Map<String, Element> getMaterialXMLMap()
     {
-        parent = getDefaultMaterialManager();
-        //MaterialXmlUtil.loadMaterials(materialsRoot);
-        setMaterialsRoot(materialsRoot);
-        loadMaterialsFromXML(materialsRoot);
+        return materialMap;
     }
     
-    /** 
-     * Ctor for XMLMaterialFactory with given parent and no initial material data.
-     * @param p The parent manager. 
-     **/
-    private XMLMaterialManager(XMLMaterialManager p)
-    {
-        parent = p;
-    }
-   
-    /** 
-     * Set current document source.
-     * @param d The Document with a materials node as its child.
-     **/    
-    private void setCurrentDocument(Document d)
-    {
-        currentDoc = d;
-    }
-
-    /** 
-     * Check if this object has a parent manager.
-     * @return True if this manager has a parent; false if not. 
-     **/
-    protected boolean hasParentManager()
-    {
-        return (parent != null);
-    }
-
     /**
-     *  Get the current XML document known by this manager.
-     *  @return The current XML document. 
-     **/
-    public Document getCurrentDocument()
+     * Get the map of XML chemical element nodes known by this manager.
+     * @return The map of names to chemical element nodes.
+     */
+    public Map<String, Element> getElementXMLMap()
     {
-        return currentDoc;
-    }
-
-    /**
-     * Attempt to find the materials section in a document using two possible cases.
-     * 
-     * 1) The materials section is the root element. 
-     * 2) The materials section is a child of the root element.
-     * 
-     * @param d Document to search for materials element.     
-     */           
-    // FIXME This should be removed.  Callers should have the materials node in hand.
-    private static org.jdom.Element getMaterialsRoot(Document d)
-    {        
-        org.jdom.Element m = null;
-
-        if (d.getRootElement().getName() == "materials")
-        {
-            m = d.getRootElement();
-        }
-        else
-        {
-            m = d.getRootElement().getChild("materials");
-        }
-
-        return m;
+        return elementMap;
     }
-
+    
     /** 
      * Add a material XML node to the lookup map.  
      * Does not create a Material object.
@@ -221,10 +150,7 @@
      **/
     public void addMaterialRef(org.jdom.Element e)
     {
-        if (materialMap.get(e.getAttributeValue("name")) == null)
-        {
-            materialMap.put(e.getAttributeValue("name"), e);
-        }
+        materialMap.put(e.getAttributeValue("name"), e);     
     }
 
     /** 
@@ -241,6 +167,7 @@
      * Find the XML element for a material.
      * @return The XML element with name <code>matName</code> or null if doesn't exist.    
      */
+    // FIXME Move to LCDDMatHelp
     public org.jdom.Element getMaterialXML(String matName)
     {
         org.jdom.Element m = materialMap.get(matName);
@@ -255,7 +182,7 @@
         }
 
         return m;
-    }
+    }    
 
     /** 
      * Find the XML node for a chemical element.
@@ -278,7 +205,7 @@
     }
 
     /** 
-     * Find a chemical element in the current document.
+     * Find a chemical element in a document.
      * @return The XML node of the chemical element called <code>elemName</code>. 
      **/
     public Element findMaterialElementXML(String elemName, Document d)
@@ -298,23 +225,21 @@
         // Try the parent.
         if (fnd == null && hasParentManager())
         {
-            fnd = parent.findXML(elemName);
+            fnd = parent.findMaterialElementXML(elemName);
         }
 
         return fnd;
     }
-
+    
     /**
-     * Find the XML node for the material or element called <code>elemName</code>. 
-     * @param elemName The name attribute value of the node.
-     * @return The XML node with attribute name of <code>elemName</code> or null if not found.
-     * @throws JDOMException
-     */
-    private org.jdom.Element findXML(String elemName)
+     *  Get the current XML document known by this manager.
+     *  @return The current XML document. 
+     **/
+    public Document getCurrentDocument()
     {
-        return findMaterialElementXML(elemName, currentDoc);
+        return currentDoc;
     }
-
+    
     /** 
      * Find a material in an XML document.
      * @param matName The name of the material.
@@ -344,8 +269,101 @@
 
         return fnd;
     }
+            
+    /**
+     * Create the static instance of the elements manager.
+     * @return The manager containing elements data.
+     */
+    static private XMLMaterialManager createMaterialElements()
+    {
+        elements = new XMLMaterialManager();
+        elements.loadElementsFromResource();
+        return elements; 
+    }
+    
+    /**
+     * Create the static instance of the material manager.
+     * @return The manager containing default materials data.
+     */
+    static private XMLMaterialManager createMaterials()
+    {
+        materials = new XMLMaterialManager(elements);
+        materials.loadMaterialsFromResource(materialResource);
+        return materials;
+    }
+              
+    /**
+     * The default ctor should not be used.  Any "user" managers
+     * must have a parent and/or a node pointing to a materials section.
+     */
+    private XMLMaterialManager()
+    {}
+          
+    /** 
+     * Ctor for XMLMaterialFactory with given parent and no initial material data.
+     * @param p The parent manager. 
+     **/
+    private XMLMaterialManager(XMLMaterialManager p)
+    {
+        parent = p;
+    }
+   
+    /** 
+     * Set current document source.
+     * @param d The Document with a materials node as its child.
+     **/    
+    private void setCurrentDocument(Document d)
+    {
+        currentDoc = d;
+    }
 
     /** 
+     * Check if this object has a parent manager.
+     * @return True if this manager has a parent; false if not. 
+     **/
+    protected boolean hasParentManager()
+    {
+        return (parent != null);
+    }
+
+    /**
+     * Attempt to find the materials section in a document using two possible cases.
+     * 
+     * 1) The materials section is the root element. 
+     * 2) The materials section is a child of the root element.
+     * 
+     * @param d Document to search for materials element.     
+     */           
+    // FIXME This should be removed.  Callers should have the materials node in hand.
+    private static org.jdom.Element getMaterialsRoot(Document d)
+    {        
+        org.jdom.Element m = null;
+
+        if (d.getRootElement().getName() == "materials")
+        {
+            m = d.getRootElement();
+        }
+        else
+        {
+            m = d.getRootElement().getChild("materials");
+        }
+
+        return m;
+    }
+ 
+    /**
+     * Find the XML node for the material or element called <code>elemName</code>
+     * in the current Document. 
+     * @param elemName The name attribute value of the node.
+     * @return The XML node with attribute name of <code>elemName</code> or null if not found.
+     * @throws JDOMException
+     */
+    private org.jdom.Element findMaterialElementXML(String elemName)
+    {
+        return findMaterialElementXML(elemName, currentDoc);
+    }
+   
+    /** 
      * Find a material in the manager's document.
      * @param matName The name of the material.
      * @return The node for the material called <code>matName</code> or null if not found. 
@@ -505,7 +523,7 @@
      */
     private void makeMaterialFromXML(Element matNode)
     {        
-        System.out.println("makeMaterialFromXML - " + matNode.getAttributeValue("name"));
+        //System.out.println("makeMaterialFromXML - " + matNode.getAttributeValue("name"));
         
         // Get the name of the material.
         String name = matNode.getAttributeValue("name");

GeomConverter/test/org/lcsim/geometry/layer
LayerStackTest.java 1.7 -> 1.8
diff -u -r1.7 -r1.8
--- LayerStackTest.java	11 Mar 2011 19:22:21 -0000	1.7
+++ LayerStackTest.java	12 Mar 2011 00:37:28 -0000	1.8
@@ -40,6 +40,7 @@
     {    
         // Need to make sure static material classes are setup 
         // before settings up layers.
+        XMLMaterialManager.setup();
         XMLMaterialManager.getDefaultMaterialManager();
         
         layerStack = new LayerStack();

GeomConverter/test/org/lcsim/material
RadiationLengthTest.java 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- RadiationLengthTest.java	11 Mar 2011 19:22:21 -0000	1.2
+++ RadiationLengthTest.java	12 Mar 2011 00:37:28 -0000	1.3
@@ -8,7 +8,7 @@
  * {@link http://pdg.lbl.gov/2010/AtomicNuclearProperties}.
  * 
  * @author jeremym
- * @version $Id: RadiationLengthTest.java,v 1.2 2011/03/11 19:22:21 jeremy Exp $
+ * @version $Id: RadiationLengthTest.java,v 1.3 2011/03/12 00:37:28 jeremy Exp $
  */
 public class RadiationLengthTest extends TestCase
 {
@@ -19,8 +19,10 @@
     
     public void testRadLen()
     {        
-        XMLMaterialManager.getDefaultMaterialManager();
+        XMLMaterialManager.setup(); // bootstrap materials db
+        //XMLMaterialManager.getDefaultMaterialManager();
         MaterialManager mgr = MaterialManager.instance();
+        
         System.out.println("Material radLen, radLenDens");
         for (int i=0, n=materialLookup.length; i<n; i++)
         {

GeomConverter/test/org/lcsim/material
XMLMaterialManagerTest.java 1.8 -> 1.9
diff -u -r1.8 -r1.9
--- XMLMaterialManagerTest.java	11 Mar 2011 19:22:21 -0000	1.8
+++ XMLMaterialManagerTest.java	12 Mar 2011 00:37:28 -0000	1.9
@@ -14,7 +14,10 @@
     public void setUp()
     {
         if (xmgr == null)
+        {
+            XMLMaterialManager.setup();
             xmgr = XMLMaterialManager.getDefaultMaterialManager();
+        }
     }
        
     public void testElements()
CVSspam 0.2.8