Print

Print


Commit in GeomConverter/src/org/lcsim/geometry/compact/converter/lcdd on MAIN
LCDDDetector.java+177-1441.22 -> 1.23
add support for unsegmented cal

GeomConverter/src/org/lcsim/geometry/compact/converter/lcdd
LCDDDetector.java 1.22 -> 1.23
diff -u -r1.22 -r1.23
--- LCDDDetector.java	3 Dec 2010 01:22:18 -0000	1.22
+++ LCDDDetector.java	7 Dec 2010 20:18:24 -0000	1.23
@@ -35,251 +35,284 @@
 import org.lcsim.geometry.compact.converter.lcdd.util.Rotation;
 import org.lcsim.geometry.compact.converter.lcdd.util.SensitiveDetector;
 import org.lcsim.geometry.compact.converter.lcdd.util.Tracker;
+import org.lcsim.geometry.compact.converter.lcdd.util.UnsegmentedCalorimeter;
 import org.lcsim.geometry.compact.converter.lcdd.util.VisAttributes;
 import org.lcsim.geometry.util.IDDescriptor;
 import org.lcsim.material.XMLMaterialManager;
 import org.lcsim.util.cache.FileCache;
 
 /**
- *
+ * 
  * @author tonyj
  */
 class LCDDDetector extends org.lcsim.geometry.compact.Detector
 {
     private Element lccdd;
     private long checksum;
-    
-    LCDDDetector(Element node)
+
+    LCDDDetector( Element node )
     {
-        super(node);
+        super( node );
         this.lccdd = node;
     }
-    
-    /* TODO: Refactor into a number of separate functions based on element name.  */
-    Document writeLCDD(String filename) throws IOException, JDOMException
+
+    /* TODO: Refactor into a number of separate functions based on element name. */
+    Document writeLCDD( String filename ) throws IOException, JDOMException
     {
         LCDD lcdd = new LCDD();
-        
-        checksum = calculateChecksum(lccdd);
-        
+
+        checksum = calculateChecksum( lccdd );
+
         Detector detector = lcdd.getHeader().getDetector();
-        detector.setTitle(getHeader().getDetectorName());
-        lcdd.getHeader().setComment(getHeader().getComment());
-        
+        detector.setTitle( getHeader().getDetectorName() );
+        lcdd.getHeader().setComment( getHeader().getComment() );
+
         Generator generator = lcdd.getHeader().getGenerator();
-        generator.setTitle(getName());
-        generator.setVersion(getVersion());
-        generator.setFile(filename);
-        generator.setChecksum(getChecksum());
-        
+        generator.setTitle( getName() );
+        generator.setVersion( getVersion() );
+        generator.setFile( filename );
+        generator.setChecksum( getChecksum() );
+
         Author author = lcdd.getHeader().getAuthor();
-        author.setAuthorName(getHeader().getAuthor());
-       
+        author.setAuthorName( getHeader().getAuthor() );
+
         /* standard defines */
         Define define = lcdd.getDefine();
-        Rotation rotation = new Rotation("identity_rot");
-        define.addRotation(rotation);
-        
-        Rotation reflect = new Rotation("reflect_rot");
-        reflect.setX(Math.PI);
-        define.addRotation(reflect);
-        
-        Position position = new Position("identity_pos");
-        define.addPosition(position);
-       
+        Rotation rotation = new Rotation( "identity_rot" );
+        define.addRotation( rotation );
+
+        Rotation reflect = new Rotation( "reflect_rot" );
+        reflect.setX( Math.PI );
+        define.addRotation( reflect );
+
+        Position position = new Position( "identity_pos" );
+        define.addPosition( position );
+
         /* constants */
-        for (org.lcsim.geometry.compact.Constant c : getConstants().values())
+        for ( org.lcsim.geometry.compact.Constant c : getConstants().values() )
         {
-            define.addConstant(new Constant(c.getName(),String.valueOf(c.getValue())));
+            define
+                    .addConstant( new Constant( c.getName(), String
+                            .valueOf( c.getValue() ) ) );
         }
-        
+
         /* Setup materials. */
-        setupMaterials(lcdd);
-        
+        setupMaterials( lcdd );
+
         // Loop over the readouts
-        Map<Readout,IDSpec> idMap = new HashMap<Readout,IDSpec>();
-        for (Readout readout : getReadouts().values())
+        Map< Readout, IDSpec > idMap = new HashMap< Readout, IDSpec >();
+        for ( Readout readout : getReadouts().values() )
         {
             IDDescriptor id = readout.getIDDescriptor();
-            IDSpec idspec = new IDSpec(readout.getName());
-            idspec.setLength(id.getMaxBit());
-            
-            for (int i=0; i<id.fieldCount(); i++)
+            IDSpec idspec = new IDSpec( readout.getName() );
+            idspec.setLength( id.getMaxBit() );
+
+            for ( int i = 0; i < id.fieldCount(); i++ )
             {
                 IDField field = new IDField();
-                field.setLabel(id.fieldName(i));
-                field.setLength(id.fieldLength(i));
-                field.setStart(id.fieldStart(i));
-                field.setSigned(id.isSigned(i));
-                idspec.addIDField(field);
+                field.setLabel( id.fieldName( i ) );
+                field.setLength( id.fieldLength( i ) );
+                field.setStart( id.fieldStart( i ) );
+                field.setSigned( id.isSigned( i ) );
+                idspec.addIDField( field );
             }
-            lcdd.addIDSpec(idspec);
-            idMap.put(readout,idspec);
+            lcdd.addIDSpec( idspec );
+            idMap.put( readout, idspec );
         }
-        
+
         /* regions */
-        for (org.lcsim.geometry.compact.Region region : getRegions().values())
+        for ( org.lcsim.geometry.compact.Region region : getRegions().values() )
         {
-            Region lcddRegion = new Region(region.getName());
-            lcddRegion.setStoreSecondaries(region.getStoreSecondaries());
-            lcddRegion.setThreshold(region.getEnergyThreshold());
-            lcddRegion.setEnergyUnit(region.getEnergyUnit());
-            lcddRegion.setLengthUnit(region.getLengthUnit());
-            lcddRegion.setCut(region.getRangeCut());
-            lcdd.addRegion(lcddRegion);
+            Region lcddRegion = new Region( region.getName() );
+            lcddRegion.setStoreSecondaries( region.getStoreSecondaries() );
+            lcddRegion.setThreshold( region.getEnergyThreshold() );
+            lcddRegion.setEnergyUnit( region.getEnergyUnit() );
+            lcddRegion.setLengthUnit( region.getLengthUnit() );
+            lcddRegion.setCut( region.getRangeCut() );
+            lcdd.addRegion( lcddRegion );
         }
-        
+
         /* limits */
-        for (org.lcsim.geometry.compact.LimitSet limitset : getLimits().values() )
+        for ( org.lcsim.geometry.compact.LimitSet limitset : getLimits().values() )
         {
-            LimitSet lcddLimitSet = new LimitSet(limitset.getName());
+            LimitSet lcddLimitSet = new LimitSet( limitset.getName() );
             for ( org.lcsim.geometry.compact.Limit limit : limitset.getLimits().values() )
-            {                
-                Limit lcddLimit = new Limit(limit.getName());
-                
-                lcddLimit.setParticles(limit.getParticles());
-                lcddLimit.setUnit(limit.getUnit());
-                lcddLimit.setValue(limit.getValue());
-                
-                lcddLimitSet.addLimit(lcddLimit);
+            {
+                Limit lcddLimit = new Limit( limit.getName() );
+
+                lcddLimit.setParticles( limit.getParticles() );
+                lcddLimit.setUnit( limit.getUnit() );
+                lcddLimit.setValue( limit.getValue() );
+
+                lcddLimitSet.addLimit( lcddLimit );
             }
-            lcdd.addLimitSet(lcddLimitSet);
+            lcdd.addLimitSet( lcddLimitSet );
         }
-        
+
         // Visualization attributes.
-        for (org.lcsim.geometry.compact.VisAttributes vis : getVisAttributes().values())
+        for ( org.lcsim.geometry.compact.VisAttributes vis : getVisAttributes().values() )
         {
-        	VisAttributes lcddvis = new VisAttributes(vis.getName());
-        	
-        	float rgba[] = vis.getRGBA();
-        	lcddvis.setColor(rgba[0],rgba[1],rgba[2],rgba[3]);        	
-        	lcddvis.setDrawingStyle(vis.getDrawingStyle());
-        	lcddvis.setLineStyle(vis.getLineStyle());
-        	lcddvis.setVisible(vis.getVisible());
-        	lcddvis.setShowDaughters(vis.getShowDaughters());
-        	
-        	lcdd.add(lcddvis);
+            VisAttributes lcddvis = new VisAttributes( vis.getName() );
+
+            float rgba[] = vis.getRGBA();
+            lcddvis.setColor( rgba[ 0 ], rgba[ 1 ], rgba[ 2 ], rgba[ 3 ] );
+            lcddvis.setDrawingStyle( vis.getDrawingStyle() );
+            lcddvis.setLineStyle( vis.getLineStyle() );
+            lcddvis.setVisible( vis.getVisible() );
+            lcddvis.setShowDaughters( vis.getShowDaughters() );
+
+            lcdd.add( lcddvis );
         }
-        
+
         // Write out detectors
-        
-        for (Subdetector sub : getSubdetectors().values())
-        {        	
+
+        for ( Subdetector sub : getSubdetectors().values() )
+        {
             SensitiveDetector sens = null;
             Readout readout = sub.getReadout();
-            if (readout != null)
-            {                
-                if (sub.isTracker())
+            if ( readout != null )
+            {
+                if ( sub.isTracker() )
                 {
-                    sens = new Tracker(sub.getName());
+                    sens = new Tracker( sub.getName() );
                 }
-                else if (sub.isCalorimeter())
+                else if ( sub.isCalorimeter() )
                 {
-                    sens = new Calorimeter(sub.getName());
+                    // Standard segmented calorimeter.
+                    if ( readout.getSegmentation() != null )
+                    {
+                        sens = new Calorimeter( sub.getName() );
+                    }
+                    // Unsegmented calorimeter for detailed readout studies.
+                    else
+                    {
+                        sens = new UnsegmentedCalorimeter( sub.getName() );
+                    }
                 }
-                
-                if (sens != null)
+                // TODO Add optical cal here.
+
+                // TEST
+                // System.out.println(sens.getName() + " - " +
+                // readout.getClass().getCanonicalName());
+
+                if ( sens != null )
                 {
-                    sens.setIDSpec(idMap.get(readout));
-                    sens.setHitsCollection(readout.getName());
-                    lcdd.addSensitiveDetector(sens);
+                    sens.setIDSpec( idMap.get( readout ) );
+                    sens.setHitsCollection( readout.getName() );
+                    lcdd.addSensitiveDetector( sens );
                 }
-                
-                if (sub.isCalorimeter())
+
+                if ( sub.isCalorimeter() )
                 {
-                	try 
-                	{
-                		LCDDSegmentation seg = (LCDDSegmentation) readout.getSegmentation();
-                		seg.setSegmentation((Calorimeter) sens);
-                	}
-                	catch (Throwable x)
-                	{
-                		throw new RuntimeException("Readout " + readout.getName() + " is not a valid Segmentation object.", x);
-                	}                	                    
+                    try
+                    {
+                        if ( readout.getSegmentation() != null )
+                        {
+                            LCDDSegmentation seg = ( LCDDSegmentation ) readout.getSegmentation();
+                            seg.setSegmentation( ( Calorimeter ) sens );
+                        }
+                    }
+                    catch ( Throwable x )
+                    {                        
+                        throw new RuntimeException( "Readout " + readout.getName() + " is not a valid Segmentation object.",
+                                                    x );
+                    }
                 }
             }
-            
+
             if ( sub instanceof LCDDSubdetector )
             {
-                ((LCDDSubdetector) sub).addToLCDD(lcdd,sens);
+                ( ( LCDDSubdetector ) sub ).addToLCDD( lcdd, sens );
             }
             else
             {
-                throw new RuntimeException("Subdetector is not an instanceof LCDDSubdetector.");
+                throw new RuntimeException( "Subdetector is not an instanceof LCDDSubdetector." );
             }
         }
-        
+
         /* fields */
-        for (Field field : getFields().values())
+        for ( Field field : getFields().values() )
         {
-            ((LCDDField) field).addToLCDD(lcdd);
+            ( ( LCDDField ) field ).addToLCDD( lcdd );
         }
-        
+
         FileCache cache = new FileCache();
-        for (URL gdmlFile : getGDMLReferences())
+        for ( URL gdmlFile : getGDMLReferences() )
         {
-            lcdd.mergeGDML(new FileInputStream(cache.getCachedFile(gdmlFile)));
+            lcdd.mergeGDML( new FileInputStream( cache.getCachedFile( gdmlFile ) ) );
         }
-                
+
         lcdd.cleanUp();
-        
+
         Document doc = new Document();
-        doc.setRootElement(lcdd);
+        doc.setRootElement( lcdd );
         return doc;
     }
-    
-    private void setupMaterials(LCDD lcdd) throws JDOMException
+
+    private void setupMaterials( LCDD lcdd ) throws JDOMException
     {
         /** Get materials root element. */
-        Element materials = lccdd.getChild("materials");
-        
+        Element materials = lccdd.getChild( "materials" );
+
         /** Clone materials root from LCCD document to the manager. */
         XMLMaterialManager mgr = getMaterialManager();
-        mgr.setMaterialsRoot(materials);
-        
+        mgr.setMaterialsRoot( materials );
+
         /** Add materials plus their references from the compact into the manager. */
-        mgr.addReferencesFromCompact(lccdd);
-        
+        mgr.addReferencesFromCompact( lccdd );
+
         /** Copy only used materials and elements into the LCDD object. */
-        mgr.copyToLCDD(lcdd);
+        mgr.copyToLCDD( lcdd );
     }
-    
+
     public String getName()
     {
         return "GeomConverter";
     }
+
     private String getVersion()
     {
         return "1.0";
     }
+
     private long getChecksum()
     {
         return checksum;
     }
-    private long calculateChecksum(Element top) throws IOException
+
+    private long calculateChecksum( Element top ) throws IOException
     {
         // Write out in canonical format to calculate checksum
         // ignoring comments and whitespace
-        Iterator iter = top.getDescendants(new ContentFilter(ContentFilter.COMMENT));
-        while (iter.hasNext())
-        { iter.next(); iter.remove(); }
-        
+        Iterator iter = top.getDescendants( new ContentFilter( ContentFilter.COMMENT ) );
+        while ( iter.hasNext() )
+        {
+            iter.next();
+            iter.remove();
+        }
+
         CRC32 check = new CRC32();
-        OutputStream out = new CheckedOutputStream(new NullOutputStream(),check);
-        
+        OutputStream out = new CheckedOutputStream( new NullOutputStream(), check );
+
         XMLOutputter outputter = new XMLOutputter();
-        outputter.setFormat(Format.getCompactFormat());
-        outputter.output(top,out);
+        outputter.setFormat( Format.getCompactFormat() );
+        outputter.output( top, out );
         out.close();
         return check.getValue();
     }
+
     private static class NullOutputStream extends OutputStream
     {
-        public void write(byte[] b, int off, int len) throws IOException
-        {}
-        public void write(byte[] b) throws IOException
-        {}
-        public void write(int b) throws IOException
-        {}
+        public void write( byte[] b, int off, int len ) throws IOException
+        {
+        }
+
+        public void write( byte[] b ) throws IOException
+        {
+        }
+
+        public void write( int b ) throws IOException
+        {
+        }
     }
 }
CVSspam 0.2.8