When opening a stdhep file with the org.lcsim classes the file is immediately converted into an org.lcsim "EventHeader" object, after which it is hard to write the event back out as a stdhep file.
However, org.lcsim contains underlying libraries (freehep-io-stdhep and freehep-physics) which can be used to read and write stdhep files. There are two representations of stdhep events available, a true Object-Oriented approach using Particle and Event objects, and a lower level interface which reflects the Stdhep "common-block" format. If you want to read stdhep files, apply a simple filter, and write them out again, the "common-block" style interface, while a little less pretty, is probably considerably more efficient.
import hep.io.stdhep.StdhepEvent; import hep.io.stdhep.StdhepReader; import hep.io.stdhep.StdhepRecord; import hep.io.stdhep.StdhepWriter; import java.io.EOFException; import java.io.IOException; /** * A simple class for filtering stdhep events */ public class StdhepFilter { public static void main( String[] args ) throws IOException { int nRecord = 0; int nIn = 0; int nOut = 0; StdhepReader reader = new StdhepReader("inputfile.stdhep"); StdhepWriter writer = new StdhepWriter("outputfile.stdhep",reader.getTitle(),reader.getComment(),0); try { for (;;) { StdhepRecord record = reader.nextRecord(); nRecord++; if (record instanceof StdhepEvent) { StdhepEvent event = (StdhepEvent) record; nIn++; // Insert your filter code here! if (event.getNHEP() < 100) continue; } writer.writeRecord(record); nOut++; } } catch (EOFException x) { // No problem } finally { writer.close(); reader.close(); System.out.println("Records: "+nRecord+" Events: "+nIn+" Out: "+nOut); } } }
Documentation on the freehep-stdhep classes is available here:
http://java.freehep.org/freehep-stdhep/apidocs/index.html
Documentation on the freehep-physics classes (only necessary if you want the more OO access to stdhep events is here:
http://java.freehep.org/freehep-physics/apidocs/index.html
Note in particular the StdhepConverter class.