lcsim/sandbox/onoprien/org/lcsim/util
diff -N DetectorChangeListener.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DetectorChangeListener.java 24 Feb 2007 19:19:26 -0000 1.1
@@ -0,0 +1,11 @@
+package org.lcsim.util;
+
+import org.lcsim.geometry.Detector;
+
+/**
+ * @author D. Onoprienko
+ * @version $Id: DetectorChangeListener.java,v 1.1 2007/02/24 19:19:26 onoprien Exp $
+ */
+public interface DetectorChangeListener {
+ public void detectorChanged(Detector detector);
+}
lcsim/sandbox/onoprien/org/lcsim/util
diff -N Driver.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Driver.java 24 Feb 2007 19:19:26 -0000 1.1
@@ -0,0 +1,296 @@
+package org.lcsim.util;
+
+import org.lcsim.event.*;
+import org.lcsim.geometry.*;
+import org.lcsim.conditions.ConditionsManager;
+import org.freehep.application.Application;
+import org.freehep.application.studio.Studio;
+import org.openide.util.Lookup;
+import org.freehep.jas.plugin.console.ConsoleService;
+import org.freehep.jas.plugin.console.Console;
+import org.freehep.jas.plugin.console.ConsoleOutputStream;
+import javax.swing.SwingUtilities;
+import java.util.*;
+import java.io.PrintStream;
+import java.util.logging.Logger;
+//import java.util.concurrent.ConcurrentHashMap;
+
+
+/**
+ *
+ * @author D. Onoprienko
+ * @version $Id: Driver.java,v 1.1 2007/02/24 19:19:26 onoprien Exp $
+ */
+public class Driver implements Iterable<Driver>, DetectorChangeListener {
+
+// -- Fields: -----------------------------------------------------------------
+
+ static private List<DetectorChangeListener> _dcListeners = new ArrayList<DetectorChangeListener>();
+ static private Detector _detector;
+
+// static private Driver _defDriver = new Driver();
+// static private ConcurrentHashMap<Thread,Driver> _topDrivers = new ConcurrentHashMap<Thread,Driver>(2, .75f, 2);
+
+ private List<Driver> _subDrivers = new ArrayList<Driver>();
+ private Driver _parent;
+ protected int _histogramLevel = -1;
+ protected Logger _logger;
+ protected Random _random;
+
+// -- Event processing hooks : ------------------------------------------------
+
+ /** Called by the framework when event processing is suspended. */
+ protected void suspend() {
+ for (Driver driver : _subDrivers) driver.suspend();
+ }
+
+ /** Called by the framework when event processing is resumed. */
+ protected void resume() {
+ for (Driver driver : _subDrivers) driver.resume();
+ }
+
+ /** Called by the framework before the first event is processed. */
+ protected void startOfData() {
+ for (Driver driver : _subDrivers) driver.startOfData();
+ }
+
+ /** Called by the framework when all data processing is finished. */
+ protected void endOfData() {
+ for (Driver driver : _subDrivers) driver.endOfData();
+ }
+
+ /**
+ * Called by the framework to process an event.
+ * In addition the process event can call throw some special exceptions:
+ * <ul>
+ * <li>NextEventException - aborts further processing of this event</li>
+ * <li>StopRunException - causes event processing to be stopped</li>
+ * </ul>
+ * @param event The event to be processed
+ */
+ protected void process(EventHeader event) {
+ for (Driver driver : _subDrivers) driver.process(event);
+ }
+
+ /** Called by the framework when the detector geometry changes. */
+ public void detectorChanged(Detector detector) {
+ for (Driver driver : _subDrivers) driver.detectorChanged(detector);
+ }
+
+ /** Special method to be called from Jython. */
+ public void processChildren(EventHeader event) {
+ for (Driver driver : _subDrivers) driver.process(event);
+ }
+
+// -- Adding/Removing/Accessing subdrivers : ----------------------------------
+
+ /**
+ * Add a subdriver to this Driver.
+ */
+ public void add(Driver driver) {
+ _subDrivers.add(driver);
+ driver._parent = this;
+ }
+
+ /**
+ * Removes a sub-Driver from this Driver.
+ */
+ public void remove(Driver driver) {
+ _subDrivers.remove(driver);
+ driver._parent = null;
+ }
+
+ /**
+ * Returns a List of all the drivers added to this Driver.
+ */
+ public List drivers() {
+ return _subDrivers;
+ }
+
+ /**
+ * Tests to see if a given Driver is already a child of this Driver
+ *
+ *
+ * @param driver Driver to be checked
+ */
+ public boolean contains(Driver driver) {
+ return _subDrivers.contains(driver);
+ }
+
+ /**
+ * Returns iterator over all subdrivers and their decendents.
+ * At every level, ubdrivers are returned before their parent, in order
+ * in which they were added.
+ */
+ public Iterator<Driver> iterator() {
+ return new Iterator() {
+ private Driver current;
+ private Iterator<Driver> thisIt = _subDrivers.iterator();
+ private Iterator<Driver> childIt;
+ public boolean hasNext() {
+ return thisIt.hasNext() || (childIt != null) ;
+ }
+ public Driver next() throws NoSuchElementException {
+ if (childIt == null) {
+ current = thisIt.next();
+ childIt = current.iterator();
+ return next();
+ } else if (childIt.hasNext()) {
+ return childIt.next();
+ } else {
+ childIt = null;
+ return current;
+ }
+ }
+ public void remove() throws UnsupportedOperationException, IllegalStateException {
+ throw new UnsupportedOperationException("Iterator over subdrivers does not support removal operation");
+ }
+ };
+ }
+
+// -- Logging, random numbers, histogramming level : --------------------------
+
+ /** Set {@link Logger} for this driver. */
+ public void setLogger(Logger logger) {
+ _logger = logger;
+ }
+
+ /** Returns {@link Logger} associated with this driver. */
+ public Logger getLogger() {
+ return (_logger == null) ? ((_parent == null) ? Logger.getLogger("global") : _parent.getLogger()) : _logger;
+ }
+
+ /** Set default random number generator for this driver. */
+ public void setRandom(Random random) {
+ _random = random;
+ }
+
+ /** Returns default random number generator for this driver. */
+ public Random getRandom() {
+ return (_random == null) ? ((_parent == null) ? _random = new Random() : _parent.getRandom()) : _random;
+ }
+
+ /** Returns the default histogram level for this driver. */
+ public int getHistogramLevel() {
+ return (_histogramLevel < 0) ? ((_parent == null) ? 0 : _parent.getHistogramLevel()) : _histogramLevel;
+ }
+
+ /** Set the default histogramming level for this driver. */
+ public void setHistogramLevel(int level) {
+ _histogramLevel = level;
+ }
+
+// -- Other getters : ----------------------------------------------------
+
+ /** Returns {@link ConditionsManager} object that can be used to access conditions database. */
+ public ConditionsManager getConditionsManager() {
+ return (_parent == null) ? ConditionsManager.defaultInstance() : _parent.getConditionsManager();
+ }
+
+// -- Output redirection to console : -----------------------------------------
+
+ /**
+ * Returns {@link PrintStream} object that can be used to print to Jas3 "Record Loop" console.
+ */
+ static public PrintStream getConsolePrintStream() {
+ return getConsolePrintStream("Record Loop");
+ }
+
+ /**
+ * Returns {@link PrintStream} object that can be used to print to Jas3 console.
+ * @parameter name - Name of the console where the output is to be sent. If no
+ * console with this name exists, it will be created.
+ */
+ static public PrintStream getConsolePrintStream(String name) {
+ PrintStream cout;
+ try {
+ Studio studio = (Studio) Application.getApplication();
+ Lookup lookup = studio.getLookup();
+ ConsoleService cs = (ConsoleService) lookup.lookup(ConsoleService.class);
+ Console console = cs.getConsole("Record Loop");
+ if (console == null) {
+ if (SwingUtilities.isEventDispatchThread()) {
+ console = cs.createConsole(name, null);
+ } else {
+ ConsoleCreator cc = new ConsoleCreator(name, cs);
+ SwingUtilities.invokeAndWait(cc);
+ console = cc.getConsole();
+ }
+ }
+ cout = new PrintStream(console.getOutputStream(null,true), true);
+ } catch (Exception e) {
+ cout = System.out;
+ }
+ return cout;
+ }
+
+ /**
+ * Redirects everything printed to <tt>System.out</tt> and <tt>System.err</tt> to Jas3 "Record Loop" console.
+ * Redirection affects all drivers.
+ */
+ static public void redirectOutputToConsole() {
+ PrintStream cout = getConsolePrintStream();
+ System.setOut(cout);
+ System.setErr(cout);
+ }
+
+ private static class ConsoleCreator implements Runnable {
+ private String _name;
+ private ConsoleService _cs;
+ private Console _console;
+ ConsoleCreator(String name, ConsoleService cs) {_name = name; _cs = cs;}
+ public void run() {_console = _cs.createConsole(_name, null);}
+ Console getConsole() {return _console;}
+ }
+
+// -- Handling of detector change listeners : ---------------------------------
+
+ /** Add detector change listener. */
+ static public void addDetectorChangeListener(DetectorChangeListener listener) {
+ _dcListeners.add(listener);
+ if (_detector != null) listener.detectorChanged(_detector);
+ }
+
+ /** Remove detector change listener. */
+ static public void removeDetectorChangeListener(DetectorChangeListener listener) {
+ _dcListeners.remove(listener);
+ }
+
+ /** Call detector change listeners. */
+ static void fireDetectorChanged(Detector detector) {
+ if (_detector != detector) {
+ _detector = detector;
+ List<DetectorChangeListener> changeListeners = new ArrayList<DetectorChangeListener>(_dcListeners.size());
+ changeListeners.addAll(_dcListeners);
+ for (DetectorChangeListener listener : changeListeners) {
+ listener.detectorChanged(detector);
+ }
+ }
+ }
+
+// -- Exceptions : ------------------------------------------------------------
+
+ /**
+ * If thrown during the process method of a driver, causes
+ * processing of the current event to be aborted. Event processing skips
+ * immediately to the next event.
+ */
+ public static class NextEventException extends RuntimeException {
+ public NextEventException() {
+ super("Next Event");
+ }
+ }
+
+ /**
+ * If thrown during the process method of a driver, causes
+ * processing of events to be aborted.
+ */
+ public static class AbortRunException extends RuntimeException {
+ public AbortRunException() {
+ super("Abort Run");
+ }
+ }
+
+// -----------------------------------------------------------------------------
+
+}
lcsim/sandbox/onoprien/org/lcsim/util
diff -N DriverAdapter.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DriverAdapter.java 24 Feb 2007 19:19:26 -0000 1.1
@@ -0,0 +1,70 @@
+package org.lcsim.util;
+
+import org.freehep.record.loop.event.ConfigurationEvent;
+import org.freehep.record.loop.event.RecordAdapter;
+import org.freehep.record.loop.event.RecordEvent;
+import org.freehep.record.loop.event.RecordSuppliedEvent;
+import org.lcsim.event.EventHeader;
+import org.lcsim.geometry.Detector;
+
+
+/**
+ * Drive a Driver from a Record loop
+ * @author Tony Johnson
+ * @version $Id: DriverAdapter.java,v 1.1 2007/02/24 19:19:26 onoprien Exp $
+ */
+public class DriverAdapter extends RecordAdapter
+{
+ private Driver driver;
+ private Detector detector;
+ public DriverAdapter(Driver driver)
+ {
+ this.driver = driver;
+ }
+
+ public void finish(RecordEvent event)
+ {
+ driver.endOfData();
+ }
+
+ public void suspend(RecordEvent event)
+ {
+ driver.suspend();
+ }
+
+ public void resume(RecordEvent event)
+ {
+ driver.resume();
+ }
+
+ public void recordSupplied(RecordSuppliedEvent rse)
+ {
+ try
+ {
+ Object event = rse.getRecord();
+ if (event instanceof EventHeader) {
+ EventHeader evt = (EventHeader) event;
+ if (detector != evt.getDetector()) {
+ detector = evt.getDetector();
+ Driver.fireDetectorChanged(detector);
+ driver.detectorChanged(detector);
+ }
+ driver.process(evt);
+ }
+ }
+ catch (Driver.NextEventException x)
+ {
+ // OK, just continue with next event.
+ }
+ }
+
+ public void configure(ConfigurationEvent event)
+ {
+ driver.startOfData();
+ }
+
+ public void reconfigure(ConfigurationEvent event)
+ {
+ driver.startOfData();
+ }
+}