View Javadoc

1   package org.lcsim.util.loop;
2   
3   import hep.io.stdhep.StdhepEvent;
4   import hep.io.stdhep.StdhepWriter;
5   import java.io.File;
6   import java.io.IOException;
7   import org.lcsim.event.EventHeader;
8   import org.lcsim.util.Driver;
9   
10  /**
11   * A driver for writing out Stdhep events.
12   * This driver only works if the event was orignially read in from a stdhep file.
13   * The driver writes out the original event, as read from the input file. Thus
14   * it is useful for filtering events from stdhep files, but not for modifying
15   * events in stdhep files.
16   * @author tonyj
17   */
18  public class StdhepDriver extends Driver
19  {
20     private StdhepWriter writer;
21     private String file;
22     private String title;
23     private String comment;
24     private int expectedEvents;
25     
26     public StdhepDriver(String file, String title, String comment, int expectedEvents)
27     {
28        this.file = file;
29        this.title = title;
30        this.comment = comment;
31        this.expectedEvents = expectedEvents;
32     }
33     protected void startOfData()
34     {
35        try
36        {
37           writer = new StdhepWriter(file, title, comment, expectedEvents);
38        }
39        catch (IOException x)
40        {
41           throw new RuntimeException("Error opening Stdhep file",x);
42        }
43     } 
44     public StdhepDriver(File file, String title, String comment, int expectedEvents)
45     {
46        try
47        {
48           writer = new StdhepWriter(file.getAbsolutePath(), title, comment, expectedEvents);
49        }
50        catch (IOException x)
51        {
52           throw new RuntimeException("Error opening Stdhep file",x);
53        }
54     }  
55  
56     protected void process(EventHeader event)
57     {
58        try
59        {
60           StdhepEvent se = (StdhepEvent) event.get("StdhepEvent");
61           writer.writeRecord(se);
62        }
63        catch (IOException x)
64        {
65           throw new RuntimeException("Error writing Stdhep file",x);
66        }
67     }   
68  
69     protected void endOfData()
70     {
71        try
72        {
73           if (writer != null)
74           {
75              writer.close();
76              writer = null;
77           }
78        }
79        catch (IOException x)
80        {
81           throw new RuntimeException("Error closing Stdhep file",x);
82        }
83     }
84     
85     protected void suspend()
86     {
87  // We can't do this yet, the flush method is not in the current version of stdhep/MCFIO
88  //      try
89  //      {
90  //         writer.flush();
91  //      }
92  //      catch (IOException x)
93  //      {
94  //         throw new RuntimeException("Error flushing Stdhep file",x);
95  //      }
96     }
97  }