lcsim/src/org/lcsim/services
diff -N DetectorService.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DetectorService.java 21 Sep 2007 01:42:12 -0000 1.1
@@ -0,0 +1,62 @@
+package org.lcsim.services;
+
+import org.lcsim.conditions.DetectorLocator;
+import org.lcsim.detector.IDetectorElement;
+import org.lcsim.geometry.Detector;
+
+/**
+ * A service for locating a {@link org.lcsim.detector.IDetectorElement} given a
+ * fully qualified path string.
+ *
+ * @author jeremym
+ * @version $Id: DetectorService.java,v 1.1 2007/09/21 01:42:12 jeremy Exp $
+ */
+public class DetectorService
+{
+ /**
+ * Find an {@link org.lcsim.detector.IDetectorElement} given a fully qualified
+ * path string.
+ *
+ * The path string should in the format "/FullDetectorName/Subsys/Subcomponent",
+ * for instance, "/sid01/VertexBarrel/VertexBarrel_layer0".
+ *
+ * This method uses the conditions system to locate the full detector
+ * from the first portion of the path. This has the side effect of setting
+ * the "current" detector and triggering the conditions listeners
+ * of the default ConditionsManager that the detector has changed. For this
+ * reason, it is not advisable to call this method in the event loop, as there
+ * is quite a lot of overhead to looking up and building the full detector
+ * from scratch.
+ *
+ * @param pathString A fully qualified path string including the name of the detector.
+ * @return A {@link org.lcsim.detector.IDetectorElement} matching the <code>pathString</code>.
+ */
+ public IDetectorElement getDetectorElement(String pathString)
+ {
+ // Strip out leading '/' char.
+ if (pathString.charAt(0) == '/')
+ pathString = pathString.substring(1);
+
+ // Make a path to lookup.
+ String path[] = pathString.split("/");
+
+ // Path cannot be empty.
+ if (path.length == 0)
+ throw new IllegalArgumentException("Path string is empty!");
+
+ // Lookup the detector by name.
+ String detectorName = path[0];
+ Detector detector = DetectorLocator.findDetector(detectorName);
+ IDetectorElement de = detector.getDetectorElement();
+
+ // Find the detector subcomponent if there is one in the path.
+ if (path.length > 1)
+ {
+ String subpath[] = new String[path.length - 1];
+ System.arraycopy(path, 1, subpath, 0, path.length - 1);
+ de = de.findDetectorElement(subpath);
+ }
+
+ return de;
+ }
+}
\ No newline at end of file