View Javadoc

1   package org.lcsim.util;
2   
3   import java.io.IOException;
4   import org.freehep.jas.plugin.console.ConsoleOutputStream;
5   import org.freehep.jas.plugin.console.ConsoleService;
6   import org.freehep.record.loop.AbstractLoopListener;
7   import org.freehep.record.loop.LoopEvent;
8   import org.freehep.record.loop.RecordEvent;
9   import org.freehep.record.loop.RecordListener;
10  import org.lcsim.event.EventHeader;
11  import org.lcsim.geometry.Detector;
12  
13  
14  /**
15   * Drive a Driver from a Record loop
16   * @author Tony Johnson
17   * @version $Id: DriverAdapter.java,v 1.8 2012/06/15 05:24:20 onoprien Exp $
18   */
19  public class DriverAdapter extends AbstractLoopListener implements RecordListener
20  {
21     private Driver driver;
22     // The console stuff is here to fix LCSMI-128. More logically this stuff should
23     // probably be part of the JAS3 record loop adapter (which currently handles
24     // redirection of the recordSupplied method)
25     private ConsoleService cs;
26     private ConsoleOutputStream out;
27     private Detector detector;
28     
29     public DriverAdapter(Driver driver)
30     {
31        this.driver = driver;
32     }
33     public DriverAdapter(Driver driver, ConsoleService cs) throws IOException
34     {
35        this.driver = driver;
36        this.cs = cs;
37        if (cs != null) out = cs.getConsoleOutputStream("Record Loop", null);
38     }
39     
40     public void finish(LoopEvent event)
41     {
42        if (cs != null) cs.redirectStandardOutputOnThreadToConsole(Thread.currentThread(),out);
43        driver.endOfData();
44        if (cs != null) cs.redirectStandardOutputOnThreadToConsole(Thread.currentThread(),null);
45     }
46     
47     public void suspend(LoopEvent event)
48     {
49        if (cs != null) cs.redirectStandardOutputOnThreadToConsole(Thread.currentThread(),out);
50        driver.suspend();
51        if (cs != null) cs.redirectStandardOutputOnThreadToConsole(Thread.currentThread(),null);
52     }
53     
54     public void resume(LoopEvent event)
55     {
56        if (cs != null) cs.redirectStandardOutputOnThreadToConsole(Thread.currentThread(),out);
57        driver.resume();
58        if (cs != null) cs.redirectStandardOutputOnThreadToConsole(Thread.currentThread(),null);
59     }
60     
61     public void recordSupplied(RecordEvent rse)
62     {
63        try
64        {
65           Object event = rse.getRecord();
66           if (event instanceof EventHeader)
67           {
68              EventHeader evt = (EventHeader) event;
69              
70              if (detector != evt.getDetector())
71              {
72                 detectorChanged(evt.getDetector());
73              }
74              driver.process(evt);
75           }
76        }
77        catch (Driver.NextEventException x)
78        {
79           // OK, just continue with next event.
80        }
81     }
82     
83     private void detectorChanged(Detector detector)
84     {
85        this.detector = detector;
86        driver.detectorChanged(detector);
87     }
88     
89     public void start(LoopEvent event)
90     {
91        if (cs != null) cs.redirectStandardOutputOnThreadToConsole(Thread.currentThread(),out);
92        driver.startOfData();
93        if (cs != null) cs.redirectStandardOutputOnThreadToConsole(Thread.currentThread(),null);
94     }
95     
96     public Driver getDriver()
97     {
98        return driver;
99     }
100 }