Print

Print


Commit in GeomConverter/src/org/lcsim/geometry/segmentation on MAIN
NonprojectiveCylinder.java+72-241.13 -> 1.14
Fix neighbor finder method

GeomConverter/src/org/lcsim/geometry/segmentation
NonprojectiveCylinder.java 1.13 -> 1.14
diff -u -r1.13 -r1.14
--- NonprojectiveCylinder.java	20 Jul 2005 20:57:53 -0000	1.13
+++ NonprojectiveCylinder.java	29 Jul 2005 23:08:48 -0000	1.14
@@ -17,6 +17,8 @@
 import org.lcsim.geometry.layer.LayerStack;
 import org.lcsim.geometry.subdetector.CylindricalCalorimeter;
 import org.lcsim.geometry.util.IDDescriptor;
+import org.lcsim.geometry.util.IDDecoder;
+import org.lcsim.geometry.util.IDEncoder;
 
 /**
  * @author jeremym
@@ -128,8 +130,13 @@
      */
     public long[] getNeighbourIDs(int layerRange, int zRange, int phiRange)
     {
-      encoder.setValues(values);
-      long saveID = encoder.getID();
+//       System.out.println("Entering getNeighbourIDs: dl="+layerRange
+// 			 +", dz="+zRange+", dphi="+phiRange+", id="+printID(decoder,encoder) );
+      IDEncoder gnEncoder = new IDEncoder( descriptor );
+      IDDecoder gnDecoder = new IDDecoder( descriptor );
+      gnEncoder.setValues(values);
+      gnDecoder.setID( gnEncoder.getID() );
+
       int nMax = (2*layerRange + 1)*(2*zRange + 1)*(2*phiRange + 1) - 1;
       long[] result = new long[nMax];
       
@@ -144,7 +151,7 @@
 	int ilay = values[layerIndex] + i;
 
 	if (ilay<0 || ilay>=detector.getLayering().getLayerCount()) continue;
-	encoder.setValue(layerIndex,ilay);
+	gnEncoder.setValue(layerIndex,ilay);
 
 	double dphi = this.computeDeltaPhiForLayer(ilay);
 	int phiBins =  (int)Math.floor(2*Math.PI / dphi);
@@ -158,40 +165,36 @@
 	if(id==0) continue;
 
 	// save indices in a new array, as values[] keeps the original ref
-	decoder.setID(id);
-	int[] tmpval = new int[ values.length ];
-        decoder.getValues(tmpval);
-
+	gnDecoder.setID(id);
 	for (int j=-zRange; j<=zRange; ++j) {
-          int iz = tmpval[zIndex] + j;
+          int iz = gnDecoder.getValue(zIndex) + j;
 
 	  if (iz<-zBins/2 || iz>=zBins/2) continue;
-	  encoder.setValue(zIndex,iz);
+	  gnEncoder.setValue(zIndex,iz);
 
 	  for (int k=-phiRange; k<=phiRange; ++k) {
-	    // skip reference cell
+	    // skip reference cell (not a neighbor of its own)
 	    if (i==0 && j==0 && k==0) continue;
 
-	    int iphi = tmpval[phiIndex] + k;
+	    int iphi = gnDecoder.getValue(phiIndex) + k;
+	    // phi is cyclic
 	    if(iphi<0) iphi += phiBins;
 	    if(iphi>=phiBins) iphi -= phiBins;
-	    if (iphi<0 || iphi>=phiBins) continue; 
 
-	    result[size++] = encoder.setValue(phiIndex,iphi);
+	    if(iphi<0 || iphi>=phiBins) continue;
+	    gnEncoder.setValue(phiIndex,iphi);
+	    result[size++] = gnEncoder.getID();
 	  }
 	}
       }
 
-      // reset encoder and decoder
-      encoder.setValues(values);
-      decoder.setID(saveID);
       if(size < result.length) {
 	  long[] temp = new long[size];
 	  System.arraycopy(result,0,temp,0,size);
 	  result = temp;
       }
  
-     return result;
+      return result;
     }
 
     // Not for public use, this is needed to calculate positions
@@ -214,13 +217,14 @@
 
     /**
      * Return the cell which contains a given point (x,y,z), or null.
-     * Side effect: encoder will be reset to the ID returned by this method.
+     * Side effect: encoder will be reset to the ID returned by this method.(not anymore...???)
      *
      * @param x,y,z cartesian coordinates of the point
      * @return ID of cell containing the point (maybe either in
      * absorber or live material)
      */
     public long findCellContainingXYZ(double x, double y, double z) {
+
 	// validate point
 	if(z<getZMin()) {
 	    System.out.println("find: fail z<zmin: z="+z);
@@ -248,11 +252,13 @@
 	if(phi<0) phi += 2*Math.PI;
 	int iphi = getPhiBin(ilay,phi);
 
-	encoder.setValue("layer",ilay);
-	encoder.setValue(zIndex,iz);
-	encoder.setValue(phiIndex,iphi);
+	IDEncoder enc = new IDEncoder(descriptor);
+	enc.setValue("layer",ilay);
+	enc.setValue(zIndex,iz);
+	enc.setValue(phiIndex,iphi);
+	long resultID = enc.getID();
 
-	return encoder.getID();
+	return resultID;
     }
 
     public int getLayerBin(double r) {
@@ -296,8 +302,9 @@
     public int getZBin(double z) {
       // zc is z at center of cells with iz=0
       int numz = (int)Math.floor((getZMax()-getZMin()) / gridSizeZ);
-      double zc = 0;  // for odd numz
-      if( numz%2 == 0 ) zc = gridSizeZ / 2;
+//       double zc = 0;  // for odd numz
+//       if( numz%2 == 0 ) zc = gridSizeZ / 2;
+      double zc = gridSizeZ / 2;
       int iz = (int)Math.floor((z-zc)/gridSizeZ + 0.5);
       return iz;
     }
@@ -315,4 +322,45 @@
 
 	return this.getRMin() + preLayers + layer.getThicknessToSensitiveMid();
     }
+
+    private String printID(IDDecoder dec, IDEncoder enc) {
+	String ret = "decoder: <"+dec.getValue("layer")
+	    +" "+dec.getValue("z")
+	    +" "+dec.getValue("phi")+">";
+	ret += " --- encoder: "+printID(enc.getID());
+	return ret;
+    }
+
+    public static String printID(long id) {
+	return new String("<"+Long.toHexString(id)
+			  +": "+Integer.toString( getSysBE(id) )
+			  +" "+Integer.toString( getLayer(id) )
+			  +" "+Integer.toString( getThetaBin(id))
+			  +" "+Integer.toString( getPhiBin(id) )
+			  +">");
+    }
+
+    public static int getSysBE( long cellid ) {
+	return (int)((cellid>>16) & 0x3f);
+    }
+
+    public static int getLayer( long cellid ) {
+	int layer = (int)((cellid>>0) & 0xffff);
+	return layer;
+    }
+
+    public static int getThetaBin( long cellid ) {
+	int max = 1<<16;  // for 11 bits
+	int thetabin = (int)((cellid>>48) & 0xffff);
+	if( thetabin > max/2-1 ) thetabin = thetabin - max;
+	return thetabin;
+    }
+
+    public static int getPhiBin( long cellid ) {
+// 	int max = 1<<11;  // for 11 bits
+// 	int phibin = (int)((cellid>>43) & 0x7ff);
+// 	if( phibin > max/2-1 ) phibin = phibin - max;
+// 	return phibin;
+ 	return (int)((cellid>>32) & 0xffff);
+    }
 }
CVSspam 0.2.8