Commit in GeomConverter/sandbox on MAIN
DetectorIdHelper.java+324-1461.2 -> 1.3
JM: Sandbox.  Compiles now.

GeomConverter/sandbox
DetectorIdHelper.java 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- DetectorIdHelper.java	25 Aug 2007 03:49:25 -0000	1.2
+++ DetectorIdHelper.java	28 Aug 2007 23:42:27 -0000	1.3
@@ -1,5 +1,7 @@
 package org.lcsim.detector;
 
+import java.util.HashMap;
+
 import org.lcsim.detector.identifier.ExpandedIdentifier;
 import org.lcsim.detector.identifier.IExpandedIdentifier;
 import org.lcsim.detector.identifier.IIdentifier;
@@ -8,74 +10,112 @@
 import org.lcsim.detector.identifier.IIdentifierDictionary.FieldNotFoundException;
 import org.lcsim.detector.identifier.IIdentifierDictionary.InvalidIndexException;
 
+/**
+ * <p>
+ * An {@link org.lcsim.detector.identifier.IIdentifierHelper} for decoding {@link org.lcsim.detector.identifier.IIdentifier}s
+ * from the top-level of the detector description.
+ * </p>
+ * 
+ * <p>
+ * The identifier fields used by this class.
+ * <ul>
+ * <li><code>system</code> - The system number that uniquely identifies a subdetector.</li>
+ * <li><code>barrel</code> - The flag that indicates whether a subdetector is a barrel, endcap positive, or endcap negative.</li>
+ * <li><code>layer</code> - The layer number of a subcomponent.  (Field is optional.)</li>
+ * </ul>
+ * </p> 
+ * 
+ * <p>
+ * The system numbers can be set from a {@see SysteMap} which is simply a map of 
+ * strings to integer values for system.  These could be read from a compact description.
+ * </p>
+ * 
+ * <p>
+ * The basic method signatures are as follows.
+ * <ul>
+ * <li><code>int getSomeSubdetectorValue()</code> - Get the system value of SomeSubdetector.
+ * <li><code>isSomething(IIdentifier i)</code> - Check if the id is of some subdetector type or it it has matching barrel flag. 
+ * <li><code>IIdentifier getSomeSubdetectorId()</code> - Get the id for SomeSubdetector.
+ * <li><code>int getSomeField(IIdentifier i)</code> - Get a specific field value from an id.
+ * <li><code>boolean someFieldEquals(IIdentifier i, int x)</code> - Check if field from identifier equals the argument.
+ * </ul>
+ * </p>
+ * 
+ * @author Jeremy McCormick <[log in to unmask]>
+ */
 public class DetectorIdHelper
 extends IdentifierHelper
 {	
-	// Index of system field in the dictionary
-	int systemIndex=-1;
+	// Invalid index.
+	public static final int invalidIndex=-1;
+	
+	// Index of system field in the IdentifierDictionary.  REQUIRED.
+	int systemIndex=invalidIndex;
 	
-	// Index of barrel field in the dictionary
-	int barrelIndex=-1;
+	// Index of barrel field in the IdentifierDictionary.  REQUIRED.
+	int barrelIndex=invalidIndex;
 	
-    // Barrel or endcap flag
-	public final static int	BARREL=0;
-	public final static int ENDCAP_POSITIVE=1;
-	public final static int	ENDCAP_NEGATIVE=-1;
-	
-	// Subsystem id values
-	public final static int	UNKNOWN=0;
-	public final static int	VTX=1;
-    public final static int VTK_ENDCAP=2;
-	public final static int SIT=3;
-    public final static int SIT_ENDCAP=4;
-	public final static int	TPC=5;
-	public final static int	ECAL=6;
-    public final static int ECAL_ENDCAP=7;
-	public final static int	HCAL=8;
-    public final static int HCAL_ENDCAP=9;
-	public final static int	MUON=10;
-    public final static int MUON_ENDCAP=11;
-	public final static int	FORWARD=12;
-	public final static int	LUMI=13;	
+	// Index of layer field in the IdentifierDictionary.  OPTIONAL.
+	int layerIndex=invalidIndex;
+	 	
+    // Barrel or endcap flag.
+	// These values are fixed according to an established LCSim convention.
+	private final static int barrelValue=0;
+	private final static int endcapPositiveValue=1;
+	private final static int endcapNegativeValue=2;
+	
+	// Subsystem id values.  
+	// These are set via a SystemMap, to allow the values to be read
+	// from a compact detector description.  The default SystemMap is 
+	// created if none is supplied.
+	private static int unknownValue;
+	private static int vtxBarrelValue;
+	private static int vtxEndcapValue;
+	private static int sitBarrelValue;
+	private static int sitEndcapValue;
+	private static int tpcValue;
+	private static int ecalBarrelValue;
+	private static int ecalEndcapValue;
+	private static int hcalBarrelValue;
+	private static int hcalEndcapValue;
+	private static int muonBarrelValue;
+	private static int muonEndcapValue;
+	private static int forwardValue;
+	private static int lumiValue;	
 	
-	// Barrel or endcap
+	// Barrel or endcap ids.
 	IIdentifier barrelId;
 	IIdentifier endcapPositiveId;
 	IIdentifier endcapNegativeId;
 	
-	// Vertex detector
-	IIdentifier vtxId;
+	// Vertex detector ids.
 	IIdentifier vtxBarrelId;
     IIdentifier vtxEndcapId;
 	IIdentifier vtxEndcapPositiveId;
 	IIdentifier vtxEndcapNegativeId;
 	
-	// Silicon tracker
-	IIdentifier sitId;
+	// Silicon tracker ids.
 	IIdentifier sitBarrelId;
     IIdentifier sitEndcapId;
 	IIdentifier sitEndcapPositiveId;
 	IIdentifier sitEndcapNegativeId;
 	 
-	// TPC
+	// TPC id.
 	IIdentifier tpcId;
 	
-	// Ecal
-	IIdentifier ecalId;
+	// Ecal ids.
 	IIdentifier ecalBarrelId;
     IIdentifier ecalEndcapId;
 	IIdentifier ecalEndcapPositiveId;
 	IIdentifier ecalEndcapNegativeId;
 	
-	// Hcal
-	IIdentifier hcalId;
+	// Hcal ids.
     IIdentifier hcalBarrelId;
     IIdentifier hcalEndcapId;
 	IIdentifier hcalEndcapPositiveId;
 	IIdentifier hcalEndcapNegativeId;
 	
-	// Muon
-	IIdentifier muonId;
+	// Muon ids.
 	IIdentifier muonBarrelId;
     IIdentifier muonEndcapId;
 	IIdentifier muonEndcapPositiveId;
@@ -86,49 +126,117 @@
 	
 	// Luminosity monitor
 	IIdentifier lumiId;
-		
-	DetectorIdHelper(IIdentifierDictionary dict) throws FieldNotFoundException, InvalidIndexException
+	
+	class SystemMap 
+	extends HashMap<String,Integer>
+	{}
+	
+	private static SystemMap defaultSystemMap;
+	
+	private SystemMap getDefaultSystemMap()
 	{
-		super(dict);
+		if (defaultSystemMap == null)
+		{
+			defaultSystemMap = new SystemMap();
+			
+			SystemMap s = defaultSystemMap;
+			
+			s.put("unknown",0);
+			s.put("vtxBarrel",1);
+			s.put("vtxEndcap",2);
+			s.put("sitBarrel",3);
+			s.put("sitEndcap",4);
+			s.put("tpc",5);
+			s.put("ecalBarrel",6);
+			s.put("ecalEndcap",7);
+			s.put("hcalBarrel",8);
+			s.put("hcalEndcap",9);
+			s.put("muonBarrel",10);
+			s.put("muonEndcap",11);
+			s.put("forwardValue",12);
+			s.put("lumi",13);
+		}
+		return defaultSystemMap;		
+	}
+
+	private void setSystemValues(SystemMap s)
+	{
+		if (s != null)
+		{
+			unknownValue = s.get("unknown");
+			vtxBarrelValue = s.get("vtxBarrel");
+			vtxEndcapValue = s.get("vtxEndcap");
+			sitBarrelValue = s.get("sitBarrel");
+			sitEndcapValue = s.get("sitEndcap");
+			tpcValue = s.get("tpc");
+			ecalBarrelValue = s.get("ecalBarrel");
+			ecalEndcapValue = s.get("ecalEndcap");
+			hcalBarrelValue = s.get("hcalBarrel");
+			hcalEndcapValue = s.get("hcalEndcap");
+			muonBarrelValue = s.get("muonBarrel");
+			muonEndcapValue = s.get("muonEndcap");
+			forwardValue = s.get("forward");
+			lumiValue = s.get("lumi");					
+		}
+	}
+	
+	private void setup(IIdentifierDictionary dict, SystemMap systemMap) throws InvalidIndexException, FieldNotFoundException
+	{
+		if (systemMap == null)
+			systemMap = getDefaultSystemMap();
 		
-		systemIndex = dict.getFieldIndex("system");
-		barrelIndex = dict.getFieldIndex("barrel");
+		setSystemValues(systemMap);
+				
+		if (dict.hasField("system"))
+			systemIndex = dict.getFieldIndex("system");
+		else
+			throw new RuntimeException("system field is missing from iddict: " + dict.getName());
+					
+		if (dict.hasField("barrel"))
+			barrelIndex = dict.getFieldIndex("barrel");
+		else
+			throw new RuntimeException("barrel field is missing from iddict: " + dict.getName());			
+		
+		// Optional.
+		if (dict.hasField("layer"))
+			layerIndex = dict.getFieldIndex("layer");		
+		
+		barrelId         = makeBarrelId(barrelValue);
+		endcapPositiveId = makeBarrelId(endcapPositiveValue);
+		endcapNegativeId = makeBarrelId(endcapNegativeValue);
+		
+		vtxBarrelId         = makeSubsysId(vtxBarrelValue,barrelValue);
+		vtxEndcapPositiveId = makeSubsysId(vtxEndcapValue,endcapPositiveValue);
+		vtxEndcapNegativeId = makeSubsysId(vtxEndcapValue,endcapNegativeValue);
+		
+		sitBarrelId         = makeSubsysId(sitBarrelValue,barrelValue);
+		sitEndcapPositiveId = makeSubsysId(sitEndcapValue,endcapPositiveValue);
+		sitEndcapNegativeId = makeSubsysId(sitEndcapValue,endcapNegativeValue);
+		
+		tpcId = makeSubsysId(tpcValue);
+		
+		ecalBarrelId         = makeSubsysId(ecalBarrelValue,barrelValue);
+		ecalEndcapPositiveId = makeSubsysId(ecalEndcapValue,endcapPositiveValue);
+		ecalEndcapNegativeId = makeSubsysId(ecalEndcapValue,endcapNegativeValue);
+		
+		hcalBarrelId         = makeSubsysId(hcalBarrelValue,barrelValue);
+		hcalEndcapPositiveId = makeSubsysId(hcalEndcapValue,endcapPositiveValue);
+		hcalEndcapNegativeId = makeSubsysId(hcalEndcapValue,endcapNegativeValue);
+		
+		muonBarrelId         = makeSubsysId(muonBarrelValue,barrelValue);
+		muonEndcapPositiveId = makeSubsysId(muonEndcapValue,endcapPositiveValue);
+		muonEndcapNegativeId = makeSubsysId(muonEndcapValue,endcapNegativeValue);
 		
-		barrelId         = makeBarrelId(BARREL);
-		endcapPositiveId = makeBarrelId(ENDCAP_POSITIVE);
-		endcapNegativeId = makeBarrelId(ENDCAP_NEGATIVE);
-		
-		vtxId               = makeSubsysId(VTX);
-		vtxBarrelId         = makeSubsysId(VTX,BARREL);
-		vtxEndcapPositiveId = makeSubsysId(VTX,ENDCAP_POSITIVE);
-		vtxEndcapNegativeId = makeSubsysId(VTX,ENDCAP_NEGATIVE);
-		
-		sitId               = makeSubsysId(SIT);
-		sitBarrelId         = makeSubsysId(SIT,BARREL);
-		sitEndcapPositiveId = makeSubsysId(SIT,ENDCAP_POSITIVE);
-		sitEndcapNegativeId = makeSubsysId(SIT,ENDCAP_NEGATIVE);
-		
-		tpcId = makeSubsysId(TPC);
-		
-		ecalId               = makeSubsysId(ECAL);
-		ecalBarrelId         = makeSubsysId(ECAL,BARREL);
-		ecalEndcapPositiveId = makeSubsysId(ECAL,ENDCAP_POSITIVE);
-		ecalEndcapNegativeId = makeSubsysId(ECAL,ENDCAP_NEGATIVE);
-		
-		hcalId = makeSubsysId(HCAL);
-		hcalBarrelId         = makeSubsysId(HCAL,BARREL);
-		hcalEndcapPositiveId = makeSubsysId(HCAL,ENDCAP_POSITIVE);
-		hcalEndcapNegativeId = makeSubsysId(HCAL,ENDCAP_NEGATIVE);
-		
-		muonId = makeSubsysId(MUON);
-		muonId = makeSubsysId(HCAL);
-		muonBarrelId         = makeSubsysId(MUON,BARREL);
-		muonEndcapPositiveId = makeSubsysId(MUON,ENDCAP_POSITIVE);
-		muonEndcapNegativeId = makeSubsysId(MUON,ENDCAP_NEGATIVE);
+		forwardId = makeSubsysId(forwardValue);
 		
-		forwardId = makeSubsysId(FORWARD);
+		lumiId = makeSubsysId(lumiValue);
 		
-		lumiId = makeSubsysId(LUMI);
+	}
+		
+	DetectorIdHelper(IIdentifierDictionary dict, SystemMap systemMap) throws FieldNotFoundException, InvalidIndexException
+	{
+		super(dict);
+		setup(dict,systemMap);		
 	}	
 		
 	private IIdentifier makeSubsysId(int system) throws InvalidIndexException
@@ -168,6 +276,53 @@
 			throw new RuntimeException(x);
 		}
 	}	
+	
+	public int getBarrelValue()
+	{
+		return barrelValue;
+	}
+	
+	public int getEndcapPositiveValue()
+	{
+		return endcapPositiveValue;
+	}
+	
+	public int getEndcapNegativeValue()
+	{
+		return endcapNegativeValue;
+	}
+	
+	public int getUnknownValue()
+	{
+		return unknownValue;
+	}
+	
+	public int getVtxBarrelValue()
+	{
+		return vtxBarrelValue;
+	}
+	public int getVtxEndcapValue()
+	{
+		return vtxEndcapValue;
+	}	
+
+	public int getSitBarrelValue()
+	{
+		return sitBarrelValue;
+	}
+	public int getSitEndcapValue()
+	{
+		return sitEndcapValue;
+	}
+			
+	public int getEcalBarrelValue()
+	{
+		return ecalBarrelValue;
+	}
+	public int getEcalEndcapValue()
+	{
+		return ecalEndcapValue;
+	}		
     
     public IIdentifier getBarrelId()
     {
@@ -182,14 +337,8 @@
     public IIdentifier getEndcapNegativeId()
     {
     	return endcapNegativeId;
-    }
-    
-    
-    public IIdentifier getVtxId()
-    {
-    	return vtxId;
-    }
-    
+    }    
+        
     public IIdentifier getVtxBarrelId()
     {
     	return vtxBarrelId;
@@ -207,13 +356,8 @@
     public IIdentifier getVtxEndcapNegativeId()
     {
     	return vtxEndcapNegativeId;
-    }
-    
+    }   
  
-    public IIdentifier getSitId()
-    {
-    	return sitId;
-    }
     public IIdentifier getSitEndcapId()
     {
         return sitEndcapId;
@@ -236,13 +380,8 @@
     public IIdentifier getTpcId()
     {
     	return tpcId;
-    }
-    
+    }    
     
-    public IIdentifier getEcalId()
-    {
-    	return ecalId;
-    }
     public IIdentifier getEcalEndcapId()
     {
         return ecalEndcapId;
@@ -261,14 +400,8 @@
     public IIdentifier getEcalEndcapPositiveId()    
     {
     	return ecalEndcapPositiveId;
-    }
-    
-    
-    public IIdentifier getHcalId()
-    {
-    	return hcalId;
-    }
-    
+    }    
+        
     public IIdentifier getHcalBarrelId()
     {
     	return hcalBarrelId;
@@ -282,14 +415,8 @@
     public IIdentifier getHcalEndcapPositiveId()
     {
     	return hcalEndcapPositiveId;
-    }
-       
- 
-    public IIdentifier getMuonId()
-    {
-    	return muonId;
-    }
-    
+    }       
+     
     public IIdentifier getMuonBarrelId()
     {
     	return muonBarrelId;
@@ -306,25 +433,22 @@
     public IIdentifier getMuonEndcapPositiveId()
     {
     	return muonEndcapPositiveId;
-    }
-       
+    }       
  
     public IIdentifier getFowardId()
     {
     	return forwardId;
-    }
-    
+    }    
     
     public IIdentifier getLumiId()
     {
     	return lumiId;
-    }
-    
+    }    
     
     public boolean isBarrel(IIdentifier i)
     {
     	try {
-    		return unpack(i).getValue(barrelIndex) == BARREL;
+    		return unpack(i).getValue(barrelIndex) == barrelValue;
     	}
     	catch (InvalidIndexException x)
     	{
@@ -340,7 +464,7 @@
     public boolean isEndcapPositive(IIdentifier i)
     {
     	try {
-    		return unpack(i).getValue(barrelIndex) == ENDCAP_POSITIVE;
+    		return unpack(i).getValue(barrelIndex) == endcapPositiveValue;
     	}
     	catch (InvalidIndexException x)
     	{
@@ -351,14 +475,13 @@
     public boolean isEndcapNegative(IIdentifier i)
     {
     	try {
-    		return unpack(i).getValue(barrelIndex) == ENDCAP_NEGATIVE;
+    		return unpack(i).getValue(barrelIndex) == endcapNegativeValue;
     	}
     	catch (InvalidIndexException x)
     	{
     		throw new RuntimeException(x);
     	}
-    }
-    
+    }    
     
     public boolean isTracker(IIdentifier i)
     {
@@ -370,6 +493,11 @@
     	return isTracker(i) && isBarrel(i);
     }
     
+    public boolean isTrackerEndcap(IIdentifier i)
+    {
+    	return isTracker(i) && isEndcap(i);
+    }
+    
     public boolean isTrackerEndcapPositive(IIdentifier i)
     {
     	return isTracker(i) && isEndcapPositive(i);
@@ -378,19 +506,23 @@
     public boolean isTrackerEndcapNegative(IIdentifier i)
     {
     	return isTracker(i) && isEndcapNegative(i);
-    }
+    }            
     
-  
     public boolean isCalorimeter(IIdentifier i)
     {
     	return isEcal(i) || isHcal(i) || isMuon(i) || isForward(i) || isLumi(i);
     }
-    
+            
     public boolean isCalorimeterBarrel(IIdentifier i)
     {
     	return isCalorimeter(i) && isBarrel(i);
     }
     
+    public boolean isCalorimeterEndcap(IIdentifier i)
+    {
+    	return isCalorimeter(i) && isEndcap(i);
+    }
+    
     public boolean isCalorimeterEndcapPositive(IIdentifier i)
     {
     	return isCalorimeter(i) && isEndcapPositive(i);
@@ -399,11 +531,11 @@
     public boolean isCalorimeterEndcapNegative(IIdentifier i)
     {
     	return isCalorimeter(i) && isEndcapNegative(i);
-    }    
+    }
     
     public boolean isVtx(IIdentifier i)
     {
-    	return compareSystem(i,VTX);
+    	return isVtxBarrel(i) || isVtxEndcap(i);
     }
     
     public boolean isVtxBarrel(IIdentifier i)
@@ -413,7 +545,7 @@
         
     public boolean isVtxEndcap(IIdentifier i)
     {
-    	return compareSystem(i,VTX) && isEndcap(i);
+    	return compareSystem(i,vtxEndcapValue) && isEndcap(i);
     }
     
     public boolean isVtxEndcapPositive(IIdentifier i)
@@ -424,12 +556,11 @@
     public boolean isVtxEndcapNegative(IIdentifier i)
     {
     	return isVtx(i) && isEndcapNegative(i);
-    }
-
+    }    
     
     public boolean isSit(IIdentifier i)
     {
-    	return compareSystem(i,SIT);
+    	return isSitBarrel(i) || isSitEndcap(i);
     }
     
     public boolean isSitBarrel(IIdentifier i)
@@ -450,17 +581,16 @@
     public boolean isSitEndcapNegative(IIdentifier i)
     {
     	return isSit(i) && isEndcapNegative(i);
-    }    
+    }      
     
     public boolean isTpc(IIdentifier i)
     {
-    	return compareSystem(i,TPC);
-    }
-    
+    	return compareSystem(i,tpcValue);
+    }        
     
     public boolean isEcal(IIdentifier i)
     {
-    	return compareSystem(i,ECAL);
+    	return isEcalBarrel(i) || isEcalEndcap(i);
     }
     
     public boolean isEcalBarrel(IIdentifier i)
@@ -470,7 +600,7 @@
         
     public boolean isEcalEndcap(IIdentifier i)
     {
-    	return compareSystem(i,ECAL) && isEndcap(i);
+    	return isEcalEndcap(i) && isEndcap(i);
     }
     
     public boolean isEcalEndcapPositive(IIdentifier i)
@@ -481,11 +611,11 @@
     public boolean isEcalEndcapNegative(IIdentifier i)
     {
     	return isEcal(i) && isEndcapNegative(i);
-    }        
+    }            
     
     public boolean isHcal(IIdentifier i)
     {
-    	return compareSystem(i,HCAL);
+    	return isHcalBarrel(i) || isHcalEndcap(i);
     }
     
     public boolean isHcalBarrel(IIdentifier i)
@@ -506,11 +636,11 @@
     public boolean isHcalEndcapNegative(IIdentifier i)
     {
     	return isHcal(i) && isEndcapNegative(i);
-    }            
+    }                
     
     public boolean isMuon(IIdentifier i)
     {
-    	return compareSystem(i,MUON);
+    	return isMuonBarrel(i) || isMuonEndcap(i);
     }
     
     public boolean isMuonBarrel(IIdentifier i)
@@ -530,17 +660,65 @@
     public boolean isMuonEndcapNegative(IIdentifier i)
     {
     	return isMuon(i) && isMuonEndcapNegative(i);
+    }       
+    
+    public boolean isForward(IIdentifier i)
+    {
+    	return compareSystem(i,forwardValue);
+    }     
+    
+    public boolean isLumi(IIdentifier i)
+    {
+    	return compareSystem(i,lumiValue);
+    }        
+    
+    public int getSystem(IIdentifier i)
+    {
+    	try {
+    		return unpack(i).getValue(systemIndex);
+    	}
+    	catch (InvalidIndexException x)
+    	{
+    		throw new RuntimeException(x);
+    	}
     }
     
+    public int getBarrel(IIdentifier i)
+    {
+    	try {
+    		return unpack(i).getValue(barrelIndex);
+    	}
+    	catch (InvalidIndexException x)
+    	{
+    		throw new RuntimeException(x);
+    	}
+    }
     
-    public boolean isForward(IIdentifier i)
+    public int getLayer(IIdentifier i)
+    {
+    	if (layerIndex == invalidIndex)
+    		throw new RuntimeException("Layer number is undecodable, because " + getIdentifierDictionary().getName() + " does not have a layer field.");
+    	try {
+    		return unpack(i).getValue(layerIndex);
+    	}
+    	catch (InvalidIndexException x)
+    	{
+    		throw new RuntimeException(x);
+    	}
+    }
+    
+    public boolean layerEquals(IIdentifier i, int layer)
     {
-    	return compareSystem(i,FORWARD);
+    	return getLayer(i) == layer;
     }
     
-   
-    public boolean isLumi(IIdentifier i)
+    public boolean systemEquals(IIdentifier i, int system)
     {
-    	return compareSystem(i,LUMI);
-    }    
+    	return getSystem(i) == system;
+    }
+    
+    public boolean barrelEquals(IIdentifier i, int barrel)
+    {
+    	return getBarrel(i) == barrel;
+    }
 }
\ No newline at end of file
CVSspam 0.2.8