Print

Print


Commit in GeomConverter on MAIN
src/org/lcsim/detector/converter/compact/DetectorConverter.java+8-121.16 -> 1.17
src/org/lcsim/detector/identifier/ExpandedIdentifier.java+34-31.3 -> 1.4
                                 /IExpandedIdentifier.java+8-11.6 -> 1.7
                                 /IIdentifierDictionary.java+24-41.3 -> 1.4
                                 /IIdentifierField.java+45-31.2 -> 1.3
                                 /IdentifierDictionary.java+27-71.3 -> 1.4
                                 /IdentifierField.java+90-61.2 -> 1.3
                                 /IdentifierUtil.java+21-81.3 -> 1.4
test/org/lcsim/detector/identifier/IdentifierHelperTest.java+26-111.3 -> 1.4
+283-55
9 modified files
JM: Add more capabilities to id system.  Packing and unpacking from IdentifierField.  Make mask computation incorporate starting position.

GeomConverter/src/org/lcsim/detector/converter/compact
DetectorConverter.java 1.16 -> 1.17
diff -u -r1.16 -r1.17
--- DetectorConverter.java	9 May 2007 00:59:58 -0000	1.16
+++ DetectorConverter.java	18 May 2007 23:47:33 -0000	1.17
@@ -157,18 +157,13 @@
     
     /**
      * Creates an {@link IIdentifierDictionary} from an existing 
-     * {@link org.lcsim.geometry.compact.Subdetector}.
+     * {@link org.lcsim.geometry.compact.Subdetector}'s 
+     * {@link org.lcsim.geometry.IDDecoder}.
      * 
-     * @link org.lcsim.geometry.Readout
-     * @link org.lcsim.detector.identifier
-     * @link org.lcsim.detector.identifier.IIdentifierDictionary
-     * @link org.lcsim.detector.identifier.IIdentifierDictionaryManager
-     * @link org.lcsim.detector.identifier.IIdentifierField
-     * 
-     * @param subdet The Subdetector.
+     * @param subdet The compact Subdetector.
      */
     protected static IIdentifierDictionary createIdentifierDictionary(Subdetector subdet)
-    {
+    {        
         Readout ro = subdet.getReadout();
         IIdentifierDictionary iddict=null;
         if ( ro != null )
@@ -179,11 +174,12 @@
             IDDescriptor desc = ro.getIDDescriptor();
             for ( int i=0; i<desc.fieldCount(); i++ )
             {
-                int nbits = desc.fieldLength(i);                
+                int nbits = Math.abs(desc.fieldLength(i));
                 int start = desc.fieldStart(i);
                 boolean signed = desc.isSigned(i);
-                String name = desc.fieldName(i);
-                IIdentifierField field = new IdentifierField(name,nbits,start,signed);
+                String name = desc.fieldName(i);                
+                
+                IIdentifierField field = new IdentifierField(name,nbits,start,signed,i);
                 iddict.addField(field);
             }                                    
         }   

GeomConverter/src/org/lcsim/detector/identifier
ExpandedIdentifier.java 1.3 -> 1.4
diff -u -r1.3 -r1.4
--- ExpandedIdentifier.java	4 May 2007 10:23:38 -0000	1.3
+++ ExpandedIdentifier.java	18 May 2007 23:47:33 -0000	1.4
@@ -7,7 +7,7 @@
  * Implementation of {@link IExpandedIdentifier}.
  *
  * @author Jeremy McCormick
- * @version $Id: ExpandedIdentifier.java,v 1.3 2007/05/04 10:23:38 jeremy Exp $
+ * @version $Id: ExpandedIdentifier.java,v 1.4 2007/05/18 23:47:33 jeremy Exp $
  */
 
 public class ExpandedIdentifier
@@ -26,11 +26,37 @@
         }
     }
     
+    public ExpandedIdentifier( int[] values, int start )
+    {
+        if ( start >= values.length )
+        {
+            throw new IllegalArgumentException("Start index <" + start + "> is invalid.");
+        }
+        
+        for ( int i=start; i<values.length; i++ )
+        {
+            this.values.add( values[start] );            
+        }        
+    }
+    
     public ExpandedIdentifier( List<Integer> values )
     {
         this.values.addAll( values );
     }
     
+    public ExpandedIdentifier( List<Integer> values, int start )
+    {
+        if ( start >= values.size() )
+        {
+            throw new IllegalArgumentException("Start index<" + start + "> is invalid.");
+        }
+        
+        for ( int i=start; i<values.size(); i++ )
+        {
+            this.values.add( values.get( i ) );
+        }
+    }
+    
     public ExpandedIdentifier( String values )
     {
         String[] buffer = values.split("/");
@@ -115,7 +141,12 @@
     {
         return size() - 1;
     }
-
+    
+    public boolean isValidIndex(int i)
+    {
+        return i <= getMaxIndex();
+    }
+     
     public int compareField( IExpandedIdentifier id, int idx )
     {
         if ( idx > getMaxIndex() || idx > id.getMaxIndex() )
@@ -124,4 +155,4 @@
         }
         return Integer.valueOf( getValue( idx ) ).compareTo( id.getValue( idx ) );
     }
-}
+}
\ No newline at end of file

GeomConverter/src/org/lcsim/detector/identifier
IExpandedIdentifier.java 1.6 -> 1.7
diff -u -r1.6 -r1.7
--- IExpandedIdentifier.java	11 May 2007 00:21:09 -0000	1.6
+++ IExpandedIdentifier.java	18 May 2007 23:47:33 -0000	1.7
@@ -10,7 +10,7 @@
  * see which indices correspond to which field labels. 
  * 
  * @author  jeremym
- * @version $Id: IExpandedIdentifier.java,v 1.6 2007/05/11 00:21:09 jeremy Exp $
+ * @version $Id: IExpandedIdentifier.java,v 1.7 2007/05/18 23:47:33 jeremy Exp $
  */
 
 public interface IExpandedIdentifier
@@ -39,6 +39,13 @@
      * Get the maximum index which is {@link #size} - 1 .
      */
     public int getMaxIndex();
+    
+    /**
+     * True if the index is valid.
+     * @param i The index.
+     * @return True if index is valid.
+     */
+    public boolean isValidIndex(int i);
 
     /**
      * Clear the identifier of all field values.

GeomConverter/src/org/lcsim/detector/identifier
IIdentifierDictionary.java 1.3 -> 1.4
diff -u -r1.3 -r1.4
--- IIdentifierDictionary.java	11 May 2007 00:21:09 -0000	1.3
+++ IIdentifierDictionary.java	18 May 2007 23:47:33 -0000	1.4
@@ -6,7 +6,7 @@
  * Holds the specification for the fields of an identifier.
  *
  * @author Jeremy McCormick
- * @version $Id: IIdentifierDictionary.java,v 1.3 2007/05/11 00:21:09 jeremy Exp $
+ * @version $Id: IIdentifierDictionary.java,v 1.4 2007/05/18 23:47:33 jeremy Exp $
  */
 
 public interface IIdentifierDictionary
@@ -34,6 +34,12 @@
     public IIdentifierField getField(String fieldName);
     
     /**
+     * True if this <code>IdentifierDictionary</code> contains the field.
+     * @param fieldName The name of the field.
+     */
+    public boolean hasField(String fieldName);
+    
+    /**
      * Get the index of a named field.
      * 
      * @param fieldName The name of the field.
@@ -50,11 +56,18 @@
     public IIdentifierField getField(int index);    
     
     /**
-     * Get the fields as a map.
+     * Get the mapping of names to fields.
      * 
-     * @return The fields as a map.
+     * @return Map of names to fields.
      */
-    public Map<String,IIdentifierField> getFields();    
+    public Map<String,IIdentifierField> getFieldNameMap();
+    
+    /**
+     * Get the mapping of indices to fields.
+     * 
+     * @return Map of indices to fields.
+     */
+    public Map<Integer,IIdentifierField> getFieldIndexMap();
     
     /**
      * Get the number of fields in this dictionary.
@@ -68,4 +81,11 @@
      * equal to {{@link #getNumberOfFields()} - 1.
      */
     public int getMaxIndex();
+    
+    /**
+     * True if contains index.
+     * @param i The index.
+     * @return True if has index.
+     */
+    public boolean hasIndex(int i);    
 }
\ No newline at end of file

GeomConverter/src/org/lcsim/detector/identifier
IIdentifierField.java 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- IIdentifierField.java	11 May 2007 00:21:09 -0000	1.2
+++ IIdentifierField.java	18 May 2007 23:47:33 -0000	1.3
@@ -6,7 +6,7 @@
  * an ordered collection of these fields.
  *
  * @author Jeremy McCormick
- * @version $Id: IIdentifierField.java,v 1.2 2007/05/11 00:21:09 jeremy Exp $
+ * @version $Id: IIdentifierField.java,v 1.3 2007/05/18 23:47:33 jeremy Exp $
  */
 public interface IIdentifierField
 {
@@ -29,14 +29,56 @@
     public int getOffset();
     
     /**
-     * Get a bit mask for this field.
-     * @return The mask for this field.
+     * Get a bit mask for this field with all other bits set to 0.
+     * 
+     * @return The on mask for this field.
      */
     public long getMaskOn();
     
     /**
+     * Get a zeroing bit mask for this field, with this fields bits
+     * set to 0, and all other bits set to 1.
+     * 
+     * @return The off bit mask.
+     */
+    public long getMaskOff();
+    
+    /**
      * True if field is capable of holding signed values.
      * @return True if field is signed.
      */
     public boolean isSigned();
+    
+    /**
+     * Get the order of this field in the id, or -1 if the 
+     * ordering is unknown.
+     * 
+     * @return The order this field in the id.
+     */
+    public int getOrder();
+    
+    /**
+     * Given a 64-bit {@link IIdentifier}, unpack and return 
+     * the value of this field to an <code>int</code>.
+     * 
+     * @return Field's int value. 
+     */
+    public int unpack(IIdentifier id);
+    
+    /**
+     * Pack a single value of this field into a {@link IIdentifier}.
+     * 
+     * @param value
+     * @return
+     */
+    public IIdentifier pack( int value );
+    
+    /**
+     * Get the field value from an {@link IExpandedIdentifier}.
+     * 
+     * @param id The <code>ExpandedIdentifier</code>.
+     * @return The field value.
+     * @throw IllegalArgumentException if field index is not valid for the id.
+     */
+    public int getFieldValue( IExpandedIdentifier id );    
 }
\ No newline at end of file

GeomConverter/src/org/lcsim/detector/identifier
IdentifierDictionary.java 1.3 -> 1.4
diff -u -r1.3 -r1.4
--- IdentifierDictionary.java	11 May 2007 00:21:09 -0000	1.3
+++ IdentifierDictionary.java	18 May 2007 23:47:33 -0000	1.4
@@ -8,7 +8,7 @@
  * Implementation of {@link IIdentifierDictionary}.
  *
  * @author Jeremy McCormick
- * @version $Id: IdentifierDictionary.java,v 1.3 2007/05/11 00:21:09 jeremy Exp $
+ * @version $Id: IdentifierDictionary.java,v 1.4 2007/05/18 23:47:33 jeremy Exp $
  */
 
 public class IdentifierDictionary implements IIdentifierDictionary
@@ -36,13 +36,13 @@
         }
         
         // Put into the field map.
-        fields.put(field.getLabel(),field);
+        fields.put(field.getLabel(), field);
         
         // Store the order of this field by index.
-        fieldOrder.put( fields.size()-1, field );
+        fieldOrder.put( field.getOrder(), field );
         
         // Store the order of this field by name.
-        nameOrder.put( field.getLabel(), fields.size()-1 );
+        nameOrder.put( field.getLabel(), field.getOrder() );
     }
 
     public IIdentifierField getField(String fieldName)
@@ -60,16 +60,21 @@
         return (IIdentifierField)fields.values().toArray()[index];
     }
 
-    public Map<String,IIdentifierField> getFields()
+    public Map<String,IIdentifierField> getFieldNameMap()
     {
         return fields;
-    }      
+    }
+    
+    public Map<Integer,IIdentifierField> getFieldIndexMap()
+    {
+        return fieldOrder;
+    }    
     
     public String toString()
     {
         StringBuffer str = new StringBuffer();
         str.append(getName() + '\n');
-        for ( IIdentifierField field : getFields().values() )
+        for ( IIdentifierField field : getFieldNameMap().values() )
         {
             str.append("    " + field.toString());
         }
@@ -85,4 +90,19 @@
     {
         return getNumberOfFields() - 1;
     }
+    
+    public boolean isValidIndex(int i)
+    {
+        return i <= getMaxIndex();
+    }
+    
+    public boolean hasField(String fieldName)
+    {
+        return fields.containsKey(fieldName);
+    }
+    
+    public boolean hasIndex(int i)
+    {
+        return fieldOrder.containsKey(i);
+    }    
 }
\ No newline at end of file

GeomConverter/src/org/lcsim/detector/identifier
IdentifierField.java 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- IdentifierField.java	11 May 2007 00:21:09 -0000	1.2
+++ IdentifierField.java	18 May 2007 23:47:33 -0000	1.3
@@ -4,7 +4,7 @@
  * Implementation of {@link IIdentifierField}.
  *
  * @author Jeremy McCormick
- * @version $Id: IdentifierField.java,v 1.2 2007/05/11 00:21:09 jeremy Exp $
+ * @version $Id: IdentifierField.java,v 1.3 2007/05/18 23:47:33 jeremy Exp $
  */
 
 public class IdentifierField implements IIdentifierField
@@ -13,13 +13,52 @@
     private int offset;
     private boolean isSigned;
     private String label;
+    private int order;
+    long maskOn;
+    long maskOff;
     
-    public IdentifierField(String label, int numberOfBits, int offset, boolean isSigned)
-    {
+    private static final int MAX_OFFSET=64;
+    private static final int MIN_OFFSET=0;
+    private static final int MAX_BITS=32;
+    private static final int MIN_BITS=1;
+    private static final long BITS32 = 0xFFFFFFFF;
+           
+    public IdentifierField(String label, int numberOfBits, int offset, boolean isSigned, int order)
+    {
+        if ( label == null )
+        {
+            throw new IllegalArgumentException(label + " - label is null.");
+        }
+        
         this.label = label;
+        
+        if ( numberOfBits > MAX_BITS || numberOfBits < MIN_BITS )
+        {
+            throw new IllegalArgumentException(label + " - number of bits is not between " + MIN_BITS + " and " + MAX_BITS);
+        }
+        
         this.numberOfBits = numberOfBits;
+        
+        if ( offset > MAX_OFFSET || offset < MIN_OFFSET )
+        {
+            throw new IllegalArgumentException(label + " - offset <" + offset + "> is not between " + MIN_OFFSET + " and " + MAX_OFFSET);
+        }
+        
+        if ( (offset + numberOfBits) > MAX_OFFSET )
+        {
+            throw new IllegalArgumentException(label + " - offset + numberOfBits <" + (offset + numberOfBits) + "> is greater than " + MAX_OFFSET);
+        }
+        
         this.offset = offset;
-        this.isSigned = isSigned;        
+        this.isSigned = isSigned;
+                        
+        this.order = order;
+        
+        // Compute the mask.
+        this.maskOn = ((1L<<numberOfBits) - 1) << offset;
+        
+        // Compute the zeroing mask.
+        this.maskOff = (maskOn) ^ BITS32;
     }
     
     public String getLabel()
@@ -29,7 +68,12 @@
 
     public long getMaskOn()
     {
-        return (1L << getNumberOfBits()) - 1;
+        return maskOn;
+    }
+    
+    public long getMaskOff()
+    {
+        return maskOff;
     }
 
     public int getNumberOfBits()
@@ -47,8 +91,48 @@
         return isSigned;
     }
     
+    public int getOrder()
+    {
+        return order;
+    }      
+    
     public String toString()
     {
-        return getLabel() + ":" + getOffset() + ":" + getNumberOfBits() + ":" + isSigned() + "\n";
+        return getOrder() + ":" + getLabel() + ":" + getOffset() + ":" + getNumberOfBits() + ":" + isSigned() + "\n";
+    }
+    
+    public int unpack( IIdentifier compact )
+    {                
+        int length = getNumberOfBits();
+        long mask = getMaskOn();
+                
+        int result = (int) ((compact.getValue()) & mask) >> offset;
+        
+        if ( isSigned() ) 
+        {
+            int signBit = 1<<(length-1);
+            if ((result & signBit) != 0) result -= (1<<length);
+        }
+        return result;        
+    }    
+    
+    public IIdentifier pack( int value )
+    {
+        long result=0;
+        int offset = getOffset();
+        int length = getNumberOfBits();
+        long mask = ((1L<<length) - 1) << offset;
+        result &= ~mask;
+        result |= (((long) value )<<offset) & mask;
+        return new Identifier( result );
+    }
+    
+    public int getFieldValue( IExpandedIdentifier id )
+    {
+        if ( !id.isValidIndex(order))
+        {
+            throw new IllegalArgumentException("Index of the field <" + getLabel() + "> is not valid for this id.");
+        }
+        return id.getValue(order);
     }
 }
\ No newline at end of file

GeomConverter/src/org/lcsim/detector/identifier
IdentifierUtil.java 1.3 -> 1.4
diff -u -r1.3 -r1.4
--- IdentifierUtil.java	11 May 2007 00:21:09 -0000	1.3
+++ IdentifierUtil.java	18 May 2007 23:47:33 -0000	1.4
@@ -15,7 +15,7 @@
  * @see IExpandedIdentifier
  *
  * @author Jeremy McCormick
- * @version $Id: IdentifierUtil.java,v 1.3 2007/05/11 00:21:09 jeremy Exp $
+ * @version $Id: IdentifierUtil.java,v 1.4 2007/05/18 23:47:33 jeremy Exp $
  */
 public final class IdentifierUtil
 {
@@ -69,13 +69,19 @@
         {            
             if ( i >= startIndex  && i <= endIndex )
             {
-                //System.out.println(i);
-                
+                //System.out.println(iddict.getField(i).getLabel());
+                                                
                 IIdentifierField field = iddict.getField(i);
+                
+                int checkVal = field.unpack(compact);
+                //System.out.println("checkVal="+checkVal);
 
                 int start = field.getOffset();
                 int length = field.getNumberOfBits();
-                int mask = (1<<length) - 1;            
+                int mask = (1<<length) - 1;
+                //long mask = field.getMaskOn();
+                
+                //int result = (int) (( id ) & mask) >> start;
 
                 int result = (int) ((id >> start) & mask);
                 if (field.isSigned())
@@ -83,6 +89,9 @@
                     int signBit = 1<<(length-1);
                     if ((result & signBit) != 0) result -= (1<<length);
                 }
+                
+                //System.out.println("result="+result);
+                //System.out.println();
 
                 buffer.addValue(result);
             }
@@ -97,7 +106,9 @@
     
     
     public static int getValue( IIdentifierDictionary iddict, IIdentifier compact, IIdentifierField desc )
-    {                
+    {             
+        return desc.unpack(compact);
+        /*
         int start = desc.getOffset();
         int length = desc.getNumberOfBits();
         long mask = desc.getMaskOn();
@@ -109,7 +120,8 @@
             int signBit = 1<<(length-1);
             if ((result & signBit) != 0) result -= (1<<length);
         }
-        return result;        
+        return result;
+        */        
     }
     
     public static int getValue( IIdentifierDictionary iddict, IIdentifier compact, int field )
@@ -159,8 +171,9 @@
             IIdentifierField field = iddict.getField(i);
             
             int offset = field.getOffset();
-            int length = field.getNumberOfBits();
-            long mask = ((1L<<length) - 1) << offset;
+            //int length = field.getNumberOfBits();
+            //long mask = ((1L<<length) - 1) << offset;
+            long mask = field.getMaskOn();
             result &= ~mask;
             result |= (((long) id.getValue(i))<<offset) & mask;
         }

GeomConverter/test/org/lcsim/detector/identifier
IdentifierHelperTest.java 1.3 -> 1.4
diff -u -r1.3 -r1.4
--- IdentifierHelperTest.java	10 May 2007 00:12:24 -0000	1.3
+++ IdentifierHelperTest.java	18 May 2007 23:47:34 -0000	1.4
@@ -10,7 +10,7 @@
  * on {@link IIdentifierHelper}.
  *
  * @author Jeremy McCormick
- * @version $Id: IdentifierHelperTest.java,v 1.3 2007/05/10 00:12:24 jeremy Exp $
+ * @version $Id: IdentifierHelperTest.java,v 1.4 2007/05/18 23:47:34 jeremy Exp $
  */
 
 public class IdentifierHelperTest
@@ -28,10 +28,12 @@
     
     public void testGetValue()
     {        
+        System.out.println("testGetValue");
+        
         IIdentifierDictionary iddict = new IdentifierDictionary("test1");
-        iddict.addField(new IdentifierField("field1",8,0,false));
-        iddict.addField(new IdentifierField("field2",8,8,true));
-        iddict.addField(new IdentifierField("field3",8,16,false));
+        iddict.addField(new IdentifierField("field1",8, 0,  false,  0 ));
+        iddict.addField(new IdentifierField("field2",8, 8,  true,   1 ));
+        iddict.addField(new IdentifierField("field3",8, 16, false,  2 ));
         
         IdentifierHelper helper = new IdentifierHelper( iddict );
         
@@ -55,15 +57,26 @@
         assertEquals( "Got wrong value for field2.", val2, -1 );        
         
         int val3 = helper.getValue(id, iddict.getField(2));
-        assertEquals( "Got wrong value for field3.", val3, 2 );        
+        assertEquals( "Got wrong value for field3.", val3, 2 );    
+        
+        assertEquals( "Got wrong value for field1 from IdentifierField.unpack().", iddict.getField(0).unpack( id ), 1 );
+        assertEquals( "Got wrong value for field2 from IdentifierField.unpack().", iddict.getField(1).unpack( id ), -1 );
+
+        assertEquals("ff",               Long.toHexString(iddict.getField(0).getMaskOn()));
+        assertEquals("ffffffffffffff00", Long.toHexString(iddict.getField(0).getMaskOff()));                             
+                
+        assertEquals("ff00",             Long.toHexString(iddict.getField(1).getMaskOn()));
+        assertEquals("ffffffffffff00ff", Long.toHexString(iddict.getField(1).getMaskOff()));
     }
     
     public void testUnpack()
     {
+        System.out.println("testUnpack");
+        
         IIdentifierDictionary iddict = new IdentifierDictionary("test2");
-        iddict.addField(new IdentifierField("field1",8,0,false));
-        iddict.addField(new IdentifierField("field2",8,8,true));
-        iddict.addField(new IdentifierField("field3",8,16,false));
+        iddict.addField(new IdentifierField("field1",8,0,false, 0));
+        iddict.addField(new IdentifierField("field2",8,8,true, 1));
+        iddict.addField(new IdentifierField("field3",8,16,false, 2));
         
         IdentifierHelper helper = new IdentifierHelper( iddict );
         
@@ -81,16 +94,18 @@
         assertTrue( expIdCheck2.toString().equals("/0/-1/2") );
         
         IExpandedIdentifier expIdCheck3 = helper.unpack(id, 0, 1);
-        //System.out.println("expIdCheck3 = " + expIdCheck3.toString());
         assertTrue( expIdCheck3.toString().equals("/1/-1/0") );
     }
     
     public void testPack()
     {
+        System.out.println("testPack");
+        
         IIdentifierDictionary iddict = new IdentifierDictionary("test3");
-        iddict.addField(new IdentifierField("field1",16,0,false));
-        iddict.addField(new IdentifierField("field2",16,32,true));
         
+        iddict.addField(new IdentifierField("field1",8, 0,  false,  0 ));
+        iddict.addField(new IdentifierField("field2",8, 8,  true,   1 ));
+              
         IIdentifierHelper helper = new IdentifierHelper( iddict );
         
         IExpandedIdentifier expId = new ExpandedIdentifier();
CVSspam 0.2.8