7 modified files
GeomConverter/src/org/lcsim/detector/converter/compact
diff -u -r1.14 -r1.15
--- SiTrackerBarrelConverter.java 9 May 2007 00:59:58 -0000 1.14
+++ SiTrackerBarrelConverter.java 10 May 2007 00:12:24 -0000 1.15
@@ -43,9 +43,7 @@
implements ISubdetectorConverter
{
public void convert( Subdetector subdet, Detector detector)
- {
- //System.out.println("SiTrackerBarrelConverter.convert");
-
+ {
subdet.setDetectorElement( new DeSubdetector( detector, subdet ) );
Map<String, ILogicalVolume> modules = buildModules(subdet);
@@ -513,7 +511,7 @@
// Create the packed id using util method.
// No IdentifierHelper is available yet.
- IIdentifier id = IdentifierUtil.pack( expId, iddict );
+ IIdentifier id = IdentifierUtil.pack( iddict, expId );
//System.out.println(pv.getName() + " is sens");
//System.out.println("path : " + modulePath.toString() + "/" + pv.getName());
GeomConverter/src/org/lcsim/detector/identifier
diff -u -r1.1 -r1.2
--- IIdentifierDictionary.java 2 May 2007 01:58:19 -0000 1.1
+++ IIdentifierDictionary.java 10 May 2007 00:12:24 -0000 1.2
@@ -6,14 +6,66 @@
* COMMENT
*
* @author Jeremy McCormick
- * @version $Id: IIdentifierDictionary.java,v 1.1 2007/05/02 01:58:19 jeremy Exp $
+ * @version $Id: IIdentifierDictionary.java,v 1.2 2007/05/10 00:12:24 jeremy Exp $
*/
public interface IIdentifierDictionary
{
+ /**
+ * Get the name of this dictionary.
+ *
+ * @return The name of this IIdentifierDictionary.
+ */
public String getName();
+
+ /**
+ * Append a field to the end of this dictionary.
+ *
+ * @param field A field to add.
+ */
public void addField(IIdentifierField field);
- public IIdentifierField getField(String fieldName);
- public IIdentifierField getField(int index);
+
+ /**
+ * Get a field by name.
+ *
+ * @param fieldName The field.
+ * @return The field.
+ */
+ public IIdentifierField getField(String fieldName);
+
+ /**
+ * Get the index of a named field.
+ *
+ * @param fieldName The name of the field.
+ * @return The index index of the field.
+ */
+ public int getFieldIndex(String fieldName);
+
+ /**
+ * Get a field by index.
+ *
+ * @param index The index.
+ * @return The field.
+ */
+ public IIdentifierField getField(int index);
+
+ /**
+ * Get the fields as a map.
+ *
+ * @return The fields as a map.
+ */
public Map<String,IIdentifierField> getFields();
+
+ /**
+ * Get the number of fields in this dictionary.
+ *
+ * @return The number of fields in the dictionary.
+ */
+ public int getNumberOfFields();
+
+ /**
+ * Get the max index of this dictionary, which is
+ * equal to {{@link #getNumberOfFields()} - 1.
+ */
+ public int getMaxIndex();
}
\ No newline at end of file
GeomConverter/src/org/lcsim/detector/identifier
diff -u -r1.2 -r1.3
--- IIdentifierHelper.java 4 May 2007 10:23:38 -0000 1.2
+++ IIdentifierHelper.java 10 May 2007 00:12:24 -0000 1.3
@@ -1,17 +1,129 @@
package org.lcsim.detector.identifier;
/**
- * COMMENT
+ * This class has a reference to a single {@IIdentifierDictionary}
+ * that specifies the fields for a 64-bit [log in to unmask]
+ *
+ * The {{@link #pack(IExpandedIdentifier)} method is used to turn
+ * an {@link IExpandedIdentifier} of field values into a compact,
+ * or packed, identifier.
+ *
+ * The {@link #unpack(IIdentifier)} method does the reverse,
+ * taking a packed {@link IIdentifier} and turning into a
+ * list of field values.
+ *
+ * The {@link #getValue( IIdentifier compact, IIdentifierField desc )} method
+ * is used to unpack a single field value. There are similar, overloaded methods
+ * for using an index in the dictionary or a field label to retrieve a single
+ * field value.
+ *
+ * @see org.lcsim.detector.identifier;
+ * @see IIdentifier
+ * @see IExpandedIdentifier
+ * @see IIdentifierDictionary
*
* @author Jeremy McCormick
- * @version $Id: IIdentifierHelper.java,v 1.2 2007/05/04 10:23:38 jeremy Exp $
+ * @version $Id: IIdentifierHelper.java,v 1.3 2007/05/10 00:12:24 jeremy Exp $
*/
public interface IIdentifierHelper
{
+ /**
+ * Get the {@link IIdentifierDictionary} associated
+ * with this helper.
+ *
+ * @return The IdentifierDictionary.
+ */
public IIdentifierDictionary getIdentifierDictionary();
- public IIdentifier pack(IExpandedIdentifier id);
- //public IIdentifier pack(IExpandedIdentifier id, int start);
- //public IIdentifier pack(IExpandedIdentifier id, int start, int nfields);
+
+ /**
+ * Pack an {@link IExpandedIdentifier} into a compact {@link IIdentifier}.
+ *
+ * @param id The ExpandedIdentifier to be packed.
+ * @return The compact identfier.
+ */
+ public IIdentifier pack( IExpandedIdentifier id );
+
+ /**
+ * Get a single field value using an [log in to unmask]
+ *
+ * @param compact The compact Identifier.
+ * @param desc The field description.
+ * @return The field value.
+ */
+ public int getValue( IIdentifier compact, IIdentifierField desc );
+
+ /**
+ * Get a single field value by index.
+ *
+ * @param compact The compact Identifier.
+ * @param field The field index in the dictionary.
+ * @return The field value.
+ */
+ public int getValue( IIdentifier compact, int field );
+
+ /**
+ * Get a single field value by name.
+ *
+ * @param compact The compact Identifier.
+ * @param field The field index in the dictionary.
+ * @return The field value.
+ */
+ public int getValue( IIdentifier compact, String field );
+
+ /**
+ * Pack a subset of fields in an {@link IExpandedIdentifier}.
+ *
+ * @param id An ExpandedIdentifier to pack.
+ * @param start The start index in the ExpandedIdentifier.
+ * @return An Identifier with the packed fields.
+ */
+ public IIdentifier pack(IExpandedIdentifier id, int start);
+
+ /**
+ * Pack a subset of fields in an {@link IExpandedIdentifier}.
+ *
+ * @param id An ExpandedIdentifier to pack.
+ * @param startIndex Starting index.
+ * @param endIndex End index.
+ * @return An Identifier with the packed fields.
+ */
+ public IIdentifier pack(IExpandedIdentifier id, int startIndex, int endIndex );
+
+ /**
+ * Upack the {@link IIdentifier} into an {@link IExpandedIdentifer}
+ * containing field values.
+ *
+ * @param id The Identifier.
+ * @return An ExpandedIdentifier.
+ */
public IExpandedIdentifier unpack(IIdentifier id);
-}
+
+ /**
+ *
+ * Upack the {@link IIdentifier} into an {@link IExpandedIdentifer}
+ * containing the fields starting at index start and
+ * unpacking nfields fields. The {@link IExpandedIdentifier} is
+ * padded with zero values to match the full specification in the
+ * [log in to unmask]
+ *
+ * @param id The identifier.
+ * @param startIndex The start index.
+ * @param endIndex The end index.
+ * @return An ExpandedIdentifier with the unpacked values.
+ */
+ public IExpandedIdentifier unpack(IIdentifier id, int startIndex, int endIndex );
+
+ /**
+ * Upack the {@link IIdentifier} into an {@link IExpandedIdentifer}
+ * containing the fields starting at index start and
+ * unpacking all fields to the end. The {@link IExpandedIdentifier} is
+ * padded with zero values to match the full specification in the
+ * [log in to unmask]
+ *
+ * @param id The identifier.
+ * @param startIndex The start field index.
+ * @return An ExpandedIdentifier with the unpacked values.
+ */
+ public IExpandedIdentifier unpack(IIdentifier id, int startIndex);
+}
\ No newline at end of file
GeomConverter/src/org/lcsim/detector/identifier
diff -u -r1.1 -r1.2
--- IdentifierDictionary.java 2 May 2007 01:58:19 -0000 1.1
+++ IdentifierDictionary.java 10 May 2007 00:12:24 -0000 1.2
@@ -1,5 +1,6 @@
package org.lcsim.detector.identifier;
+import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -7,12 +8,14 @@
* COMMENT
*
* @author Jeremy McCormick
- * @version $Id: IdentifierDictionary.java,v 1.1 2007/05/02 01:58:19 jeremy Exp $
+ * @version $Id: IdentifierDictionary.java,v 1.2 2007/05/10 00:12:24 jeremy Exp $
*/
public class IdentifierDictionary implements IIdentifierDictionary
{
Map<String,IIdentifierField> fields = new LinkedHashMap<String,IIdentifierField>();
+ Map<Integer,IIdentifierField> fieldOrder = new HashMap<Integer, IIdentifierField>();
+ Map<String,Integer> nameOrder = new HashMap<String,Integer>();
private String name;
public IdentifierDictionary(String name)
@@ -29,9 +32,17 @@
{
if ( fields.containsKey( field.getLabel() ) )
{
- throw new IllegalArgumentException("Dictionary already has a field called <" + name + ">.");
+ throw new IllegalArgumentException("Dictionary already has a field called <" + field.getLabel() + ">.");
}
+
+ // Put into the field map.
fields.put(field.getLabel(),field);
+
+ // Store the order of this field by index.
+ fieldOrder.put( fields.size()-1, field );
+
+ // Store the order of this field by name.
+ nameOrder.put( field.getLabel(), fields.size()-1 );
}
public IIdentifierField getField(String fieldName)
@@ -39,6 +50,11 @@
return fields.get(fieldName);
}
+ public int getFieldIndex(String fieldName)
+ {
+ return nameOrder.get( fieldName );
+ }
+
public IIdentifierField getField(int index)
{
return (IIdentifierField)fields.values().toArray()[index];
@@ -59,4 +75,14 @@
}
return str.toString();
}
+
+ public int getNumberOfFields()
+ {
+ return fields.size();
+ }
+
+ public int getMaxIndex()
+ {
+ return getNumberOfFields() - 1;
+ }
}
\ No newline at end of file
GeomConverter/src/org/lcsim/detector/identifier
diff -u -r1.5 -r1.6
--- IdentifierHelper.java 9 May 2007 00:59:58 -0000 1.5
+++ IdentifierHelper.java 10 May 2007 00:12:24 -0000 1.6
@@ -11,13 +11,23 @@
* @see IdentifierUtil
*
* @author Jeremy McCormick
- * @version $Id: IdentifierHelper.java,v 1.5 2007/05/09 00:59:58 jeremy Exp $
+ * @version $Id: IdentifierHelper.java,v 1.6 2007/05/10 00:12:24 jeremy Exp $
*/
public class IdentifierHelper
implements IIdentifierHelper
{
private IIdentifierDictionary iddict=null;
+
+ public IExpandedIdentifier unpack( IIdentifier id, int start, int nfields)
+ {
+ return IdentifierUtil.unpack( iddict, id, start, nfields );
+ }
+
+ public IExpandedIdentifier unpack( IIdentifier id, int start)
+ {
+ return IdentifierUtil.unpack( iddict, id, start );
+ }
public IdentifierHelper( IIdentifierDictionary iddict )
{
@@ -31,23 +41,36 @@
public IIdentifier pack( IExpandedIdentifier id )
{
- return IdentifierUtil.pack(id, getIdentifierDictionary() );
+ return IdentifierUtil.pack(getIdentifierDictionary(), id );
}
public IExpandedIdentifier unpack( IIdentifier id )
{
- return IdentifierUtil.unpack( id, iddict );
+ return IdentifierUtil.unpack( iddict, id );
+ }
+
+ public int getValue( IIdentifier compact, IIdentifierField desc )
+ {
+ return IdentifierUtil.getValue( iddict, compact, desc );
}
- /*
-
- FIXME: Need to support the following methods from IDDecoder.
+ public int getValue( IIdentifier compact, int field )
+ {
+ return IdentifierUtil.getValue( iddict, compact, field );
+ }
- public int getValue(String field);
- public int getValue(int index);
- public int getFieldCount();
- public String getFieldName(int index);
- public int getFieldIndex(String name);
+ public int getValue( IIdentifier compact, String field )
+ {
+ return IdentifierUtil.getValue( iddict, compact, field );
+ }
- */
+ public IIdentifier pack(IExpandedIdentifier id, int start)
+ {
+ return IdentifierUtil.pack( iddict, id, start );
+ }
+
+ public IIdentifier pack(IExpandedIdentifier id, int start, int nfields)
+ {
+ return IdentifierUtil.pack( iddict, id, start, nfields );
+ }
}
\ No newline at end of file
GeomConverter/src/org/lcsim/detector/identifier
diff -u -r1.1 -r1.2
--- IdentifierUtil.java 2 May 2007 23:44:57 -0000 1.1
+++ IdentifierUtil.java 10 May 2007 00:12:24 -0000 1.2
@@ -1,59 +1,180 @@
package org.lcsim.detector.identifier;
/**
- * Identifier utility methods.
+ * Identifier utility methods for packing {@IIdentifier}s and
+ * unpacking {@IExpandedIdentifier}s using information from an
+ * [log in to unmask]
+ *
+ * Most of the methods in {@IIdentifierHelper} use the static
+ * utility functions defined here.
+ *
+ * @see org.lcsim.detector.identifier
+ * @see IIdentifierDictionary
+ * @see IIdentifierField
+ * @see IIdentifier
+ * @see IExpandedIdentifier
*
* @author Jeremy McCormick
- * @version $Id: IdentifierUtil.java,v 1.1 2007/05/02 23:44:57 jeremy Exp $
+ * @version $Id: IdentifierUtil.java,v 1.2 2007/05/10 00:12:24 jeremy Exp $
*/
-
public final class IdentifierUtil
{
+ /**
+ * Class should not be instantiated. All methods are static.
+ */
private IdentifierUtil()
{}
-
- public static IIdentifier pack( IExpandedIdentifier id, IIdentifierDictionary iddict )
- {
- long result = 0;
- int idx = 0;
- for ( int value : id.getValues() )
- {
- IIdentifierField field = iddict.getField(idx);
-
- int start = field.getOffset();
- int length = field.getNumberOfBits();
- long mask = ((1L<<length) - 1) << start;
- result &= ~mask;
- result |= (((long) value)<<start) & mask;
-
- ++idx;
- }
-
- return new Identifier( result );
+
+ public static IExpandedIdentifier unpack( IIdentifierDictionary iddict, IIdentifier compact, int startIndex )
+ {
+ return IdentifierUtil.unpack( iddict, compact, startIndex, -1 );
}
-
- public static IExpandedIdentifier unpack( IIdentifier compact, IIdentifierDictionary iddict )
+
+ public static IExpandedIdentifier unpack( IIdentifierDictionary iddict, IIdentifier compact )
+ {
+ return IdentifierUtil.unpack( iddict, compact, 0 );
+ }
+
+ public static IExpandedIdentifier unpack( IIdentifierDictionary iddict, IIdentifier compact, int startIndex, int endIndex )
{
ExpandedIdentifier buffer = new ExpandedIdentifier();
-
+
long id = compact.getValue();
- for ( IIdentifierField field : iddict.getFields().values() )
- {
- int start = field.getOffset();
- int length = field.getNumberOfBits();
- int mask = (1<<length) - 1;
-
- int result = (int) ((id >> start) & mask);
- if (field.isSigned())
+ int maxIndex = iddict.getMaxIndex();
+
+ if ( startIndex > maxIndex )
+ {
+ throw new RuntimeException("Start index <" + startIndex + "> is not a valid index in <" + iddict.getName() + ">.");
+ }
+
+ if ( endIndex > iddict.getNumberOfFields() )
+ {
+ throw new RuntimeException("End index <" + endIndex + "> is not a valid index in <" + iddict.getName() + ">.");
+ }
+
+ if ( endIndex == -1 )
+ {
+ endIndex = iddict.getMaxIndex();
+ }
+ else
+ {
+ if ( startIndex > endIndex )
+ {
+ throw new IllegalArgumentException("Start index <" + startIndex + "> is bigger than end index <" + endIndex + ">.");
+ }
+ }
+
+ for ( int i=0; i<iddict.getNumberOfFields(); i++)
+ {
+ if ( i >= startIndex && i <= endIndex )
{
- int signBit = 1<<(length-1);
- if ((result & signBit) != 0) result -= (1<<length);
+ //System.out.println(i);
+
+ IIdentifierField field = iddict.getField(i);
+
+ int start = field.getOffset();
+ int length = field.getNumberOfBits();
+ int mask = (1<<length) - 1;
+
+ int result = (int) ((id >> start) & mask);
+ if (field.isSigned())
+ {
+ int signBit = 1<<(length-1);
+ if ((result & signBit) != 0) result -= (1<<length);
+ }
+
+ buffer.addValue(result);
+ }
+ else
+ {
+ buffer.addValue(0);
}
-
- buffer.addValue(result);
}
return buffer;
- }
+ }
+
+
+ public static int getValue( IIdentifierDictionary iddict, IIdentifier compact, IIdentifierField desc )
+ {
+ int start = desc.getOffset();
+ int length = desc.getNumberOfBits();
+ long mask = desc.getMaskOn();
+
+ int result = (int) ((compact.getValue() >> start) & mask);
+
+ if ( desc.isSigned() )
+ {
+ int signBit = 1<<(length-1);
+ if ((result & signBit) != 0) result -= (1<<length);
+ }
+ return result;
+ }
+
+ public static int getValue( IIdentifierDictionary iddict, IIdentifier compact, int field )
+ {
+ return IdentifierUtil.getValue( iddict, compact, iddict.getField( field ) );
+ }
+
+ public static int getValue( IIdentifierDictionary iddict, IIdentifier compact, String field )
+ {
+ int idx = iddict.getFieldIndex( field );
+ return IdentifierUtil.getValue( iddict, compact, idx );
+ }
+
+ public static IIdentifier pack( IIdentifierDictionary iddict, IExpandedIdentifier id, int startIndex, int endIndex )
+ {
+ long result=0;
+
+ if ( startIndex > iddict.getNumberOfFields() )
+ {
+ throw new IllegalArgumentException("Start index <" + startIndex + "> is not a valid index in <" + iddict.getName() + ">.");
+ }
+
+ if ( endIndex > iddict.getMaxIndex() )
+ {
+ throw new IllegalArgumentException("End index <" + endIndex + "> is not a valid index in <" + iddict.getName() + ">.");
+ }
+
+ if ( endIndex > startIndex )
+ {
+ throw new IllegalArgumentException("Start index is bigger than end index.");
+ }
+
+ if ( endIndex == -1 )
+ {
+ endIndex = iddict.getMaxIndex();
+ }
+ else
+ {
+ if ( startIndex > endIndex )
+ {
+ throw new IllegalArgumentException("Start index <" + startIndex + "> is bigger than end index <" + endIndex + ">.");
+ }
+ }
+
+ for ( int i=startIndex; i<=endIndex; i++ )
+ {
+ IIdentifierField field = iddict.getField(i);
+
+ int offset = field.getOffset();
+ int length = field.getNumberOfBits();
+ long mask = ((1L<<length) - 1) << offset;
+ result &= ~mask;
+ result |= (((long) id.getValue(i))<<offset) & mask;
+ }
+
+ return new Identifier( result );
+ }
+
+ public static IIdentifier pack( IIdentifierDictionary iddict, IExpandedIdentifier id )
+ {
+ return IdentifierUtil.pack( iddict, id, 0 );
+ }
+
+ public static IIdentifier pack( IIdentifierDictionary iddict, IExpandedIdentifier id, int startIndex )
+ {
+ return IdentifierUtil.pack( iddict, id, startIndex, -1 );
+ }
}
\ No newline at end of file
GeomConverter/test/org/lcsim/detector/identifier
diff -u -r1.2 -r1.3
--- IdentifierHelperTest.java 2 May 2007 23:45:43 -0000 1.2
+++ IdentifierHelperTest.java 10 May 2007 00:12:24 -0000 1.3
@@ -4,10 +4,13 @@
import junit.framework.TestSuite;
/**
- * COMMENT
+ *
+ * These class is essentially an integration test for the
+ * {@link org.lcsim.detector.identifier} package using methods
+ * on {@link IIdentifierHelper}.
*
* @author Jeremy McCormick
- * @version $Id: IdentifierHelperTest.java,v 1.2 2007/05/02 23:45:43 jeremy Exp $
+ * @version $Id: IdentifierHelperTest.java,v 1.3 2007/05/10 00:12:24 jeremy Exp $
*/
public class IdentifierHelperTest
@@ -23,9 +26,68 @@
return new TestSuite(IdentifierHelperTest.class);
}
- public void testIdHelper()
+ public void 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));
+
+ IdentifierHelper helper = new IdentifierHelper( iddict );
+
+ IExpandedIdentifier expId = new ExpandedIdentifier();
+ expId.addValue(1);
+ expId.addValue(-1);
+ expId.addValue(2);
+
+ IIdentifier id = helper.pack( expId );
+
+ int val1 = helper.getValue(id, iddict.getField(0));
+ assertEquals( "Got wrong value for field1.", val1, 1 );
+ assertEquals( "Index and name values for field1 are different.",
+ helper.getValue(id, "field1"),
+ helper.getValue(id, 0) );
+ assertEquals( "Index and IdentifierField values for field1 are different.",
+ helper.getValue(id, iddict.getField(0)),
+ helper.getValue(id, 0));
+
+ int val2 = helper.getValue(id, iddict.getField(1));
+ assertEquals( "Got wrong value for field2.", val2, -1 );
+
+ int val3 = helper.getValue(id, iddict.getField(2));
+ assertEquals( "Got wrong value for field3.", val3, 2 );
+ }
+
+ public void 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));
+
+ IdentifierHelper helper = new IdentifierHelper( iddict );
+
+ IExpandedIdentifier expId = new ExpandedIdentifier();
+ expId.addValue(1);
+ expId.addValue(-1);
+ expId.addValue(2);
+
+ IIdentifier id = helper.pack( expId );
+
+ IExpandedIdentifier expIdCheck1 = helper.unpack(id);
+ assertTrue( expIdCheck1.toString().equals("/1/-1/2") );
+
+ IExpandedIdentifier expIdCheck2 = helper.unpack(id, 1);
+ 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()
{
- IIdentifierDictionary iddict = new IdentifierDictionary("test");
+ IIdentifierDictionary iddict = new IdentifierDictionary("test3");
iddict.addField(new IdentifierField("field1",16,0,false));
iddict.addField(new IdentifierField("field2",16,32,true));
CVSspam 0.2.8