lcsim/src/org/lcsim/contrib/timb/util/lcio
diff -N LCIOWriter.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ LCIOWriter.java 26 May 2006 07:21:43 -0000 1.1
@@ -0,0 +1,170 @@
+package org.lcsim.util.lcio;
+
+import hep.lcd.io.sio.SIOOutputStream;
+import hep.lcd.io.sio.SIOWriter;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Logger;
+import org.lcsim.event.EventHeader;
+import org.lcsim.event.EventHeader.LCMetaData;
+
+
+/**
+ *
+ * @author Tony Johnson
+ */
+public class LCIOWriter
+{
+ private Logger log = Logger.getLogger(LCIOWriter.class.getName());
+ private SIOWriter writer;
+ private HandlerManager manager = HandlerManager.instance();
+ private int lastRunNumber = -1;
+ private String lastDetectorName = "";
+ private List<String> listIgnore = new ArrayList<String>();
+
+ public LCIOWriter(File file) throws IOException
+ {
+ writer = new SIOWriter(new FileOutputStream(file));
+ }
+ public LCIOWriter(String file) throws IOException
+ {
+ writer = new SIOWriter(new FileOutputStream(file));
+ }
+ public LCIOWriter(File file, List<String> listIgnore) throws IOException
+ {
+ this(file);
+ this.listIgnore = listIgnore;
+ }
+ public LCIOWriter(String file, List<String> listIgnore) throws IOException
+ {
+ this(file);
+ this.listIgnore = listIgnore;
+ }
+ public void close() throws IOException
+ {
+ writer.close();
+ }
+
+ private void writeData(EventHeader event, boolean headerOnly) throws IOException
+ {
+ if (headerOnly)
+ {
+ SIOOutputStream out = writer.createBlock(LCIOConstants.eventHeaderBlockName, LCIOConstants.MAJORVERSION, LCIOConstants.MINORVERSION);
+ out.writeInt(event.getRunNumber());
+ out.writeInt(event.getEventNumber());
+ out.writeLong(event.getTimeStamp());
+ out.writeString(event.getDetectorName());
+
+ Map<String,String> blocks = new HashMap<String,String>();
+ List<List<Object>> collections = event.get(Object.class);
+ for (List<Object> collection : collections)
+ {
+ LCMetaData md = event.getMetaData(collection);
+ Class type = md.getType();
+ // System.out.println(" LCIOWriter headerOnly md.getName()= "+md.getName()+" type.getName()= "+type.getName());
+ LCIOBlockHandler bh = manager.handlerForClass(type);
+ if (bh == null) log.warning("No handler found for block "+md.getName()+" of class "+type.getName());
+ else if(!listIgnore.contains(md.getName())) blocks.put(md.getName(),bh.getType());
+ }
+
+ out.writeInt(blocks.size());
+ for (Map.Entry<String,String> entry : blocks.entrySet() )
+ {
+ // System.out.println(" LCIOWriter entry.getKey()= "+entry.getKey()+" entry.getValue()= "+entry.getValue());
+ if(!listIgnore.contains(entry.getKey())) {
+ out.writeString(entry.getKey());
+ out.writeString(entry.getValue());
+ }
+ }
+ out.writeInt(0);
+ out.writeInt(0);
+ out.writeInt(0);
+ out.close();
+ }
+ else
+ {
+ List<List<Object>> collections = event.get(Object.class);
+ for (List<Object> collection : collections)
+ {
+ LCMetaData md = event.getMetaData(collection);
+ Class type = md.getType();
+ // System.out.println(" LCIOWriter md.getName()= "+md.getName()+" type.getName()= "+type.getName());
+ if(!listIgnore.contains(md.getName())) {
+ LCIOBlockHandler bh = manager.handlerForClass(type);
+ // System.out.println(" LCIOWriter bh= "+bh);
+ if (bh != null) bh.writeBlock(writer,collection,md);
+ }
+ }
+ }
+ }
+ private void writeData(LCIORunHeader header) throws IOException
+ {
+ SIOOutputStream out = writer.createBlock(LCIOConstants.runBlockName, LCIOConstants.MAJORVERSION, LCIOConstants.MINORVERSION);
+ out.writeInt(header.getRunNumber());
+ out.writeString(header.getDetectorName());
+ out.writeString(header.getDescription());
+ String[] active = header.getActiveSubdetectors();
+ out.writeInt(active.length);
+ for (int i=0; i<active.length; i++) out.writeString(active[i]);
+ out.writeInt(0);
+ out.writeInt(0);
+ out.writeInt(0);
+ out.close();
+ }
+ public void write(EventHeader event) throws IOException
+ {
+ if (event.getRunNumber() != lastRunNumber || !lastDetectorName.equals(event.getDetectorName()))
+ {
+ lastRunNumber = event.getRunNumber();
+ lastDetectorName = event.getDetectorName();
+ if (lastDetectorName == null) lastDetectorName = "";
+ write(new DefaultRunHeader(lastRunNumber,lastDetectorName));
+ }
+ writer.createRecord(LCIOConstants.eventHeaderRecordName,true);
+ writeData(event,true);
+ writer.createRecord(LCIOConstants.eventRecordName,true);
+ writeData(event,false);
+ }
+ public void write(LCIORunHeader header) throws IOException
+ {
+ writer.createRecord(LCIOConstants.runRecordName,true);
+ writeData(header);
+ }
+ private static class DefaultRunHeader implements LCIORunHeader
+ {
+ private int run;
+ private String name;
+ private final static String[] noDetectors = new String[0];
+ DefaultRunHeader(int run, String name)
+ {
+ this.run = run;
+ this.name = name;
+ }
+
+ public String[] getActiveSubdetectors()
+ {
+ return noDetectors;
+ }
+
+ public String getDescription()
+ {
+ return "";
+ }
+
+ public String getDetectorName()
+ {
+ return name;
+ }
+
+ public int getRunNumber()
+ {
+ return run;
+ }
+
+ }
+}