lcsim-contrib/src/main/java/org/lcsim/contrib/Partridge/TextFileToStdhep
diff -N TextFileToStdhep.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ TextFileToStdhep.java 7 Jan 2009 22:27:43 -0000 1.1
@@ -0,0 +1,147 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.lcsim.contrib.Partridge.TextFileToStdhep;
+
+/**
+ *
+ * @author richp
+ */
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.StreamTokenizer;
+import java.util.ArrayList;
+import java.util.List;
+
+import hep.io.stdhep.StdhepBeginRun;
+import hep.io.stdhep.StdhepEndRun;
+import hep.io.stdhep.StdhepEvent;
+import hep.io.stdhep.StdhepWriter;
+
+public class TextFileToStdhep {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) throws IOException {
+// if (args.length != 1) {
+// throw new RuntimeException("Invalid number of arguments");
+// }
+// String file = args[0];
+ String file = "c:/StdHepWriter/Minbias_14TeV_100E33_1404bunches_0";
+ String infile = file+".txt";
+ String outfile = file+".stdhep";
+
+// try {
+ FileReader fr = new FileReader(infile);
+ StreamTokenizer tok = new StreamTokenizer(fr);
+ tok.resetSyntax();
+ tok.wordChars(33, 255);
+ tok.parseNumbers();
+ tok.whitespaceChars(0, ' ');
+ tok.eolIsSignificant(true);
+ List<Double> beg = getNumbersInLine(tok);
+ if (beg.size() != 7) {
+ throw new RuntimeException("Unexpected entry in begin run record");
+ }
+ int nreq = beg.get(0).intValue();
+ int ngen = beg.get(1).intValue();
+ int nwrit = beg.get(2).intValue();
+ float ecm = beg.get(3).floatValue();
+ float xsec = beg.get(4).floatValue();
+ double rn1 = beg.get(5);
+ double rn2 = beg.get(6);
+ StdhepWriter sw = new StdhepWriter(outfile, "Imported Stdhep Events",
+ "From file " + infile, nwrit);
+ sw.setCompatibilityMode(false);
+ StdhepBeginRun sb = new StdhepBeginRun(nreq, ngen, nwrit, ecm, xsec, rn1, rn2);
+ sw.writeRecord(sb);
+ for (int icross = 0; icross < nwrit; icross++) {
+ List<Double> nums = getNumbersInLine(tok);
+ if (nums.size() != 2) {
+ throw new RuntimeException("Unexpected entry for number of particles");
+ }
+ int nevhep = nums.get(0).intValue();
+ int nhep = nums.get(1).intValue();
+ System.out.println("Number of particles for event " + nevhep + ": " + nhep);
+ int isthep[] = new int[nhep];
+ int idhep[] = new int[nhep];
+ int jmohep[] = new int[2 * nhep];
+ int jdahep[] = new int[2 * nhep];
+ double phep[] = new double[5 * nhep];
+ double vhep[] = new double[4 * nhep];
+ for (int i = 0; i < nhep; i++) {
+ List<Double> vals = getNumbersInLine(tok);
+ if (vals.size() != 16) {
+ throw new RuntimeException("Unexpected entry for a particle");
+ }
+ int ip = vals.get(0).intValue();
+ if (ip != i + 1) {
+ throw new RuntimeException("Particle numbering mismatch");
+ }
+ isthep[i] = vals.get(1).intValue();
+ idhep[i] = vals.get(2).intValue();
+ jmohep[2 * i] = vals.get(3).intValue();
+ jmohep[2 * i + 1] = vals.get(4).intValue();
+ jdahep[2 * i] = vals.get(5).intValue();
+ jdahep[2 * i + 1] = vals.get(6).intValue();
+ for (int j = 0; j < 5; j++) {
+ phep[5 * i + j] = vals.get(j + 7);
+ }
+ for (int j = 0; j < 4; j++) {
+ vhep[4 * i + j] = vals.get(j + 12);
+ }
+
+ if (i == 0 || i == nhep - 1) {
+ System.out.println(ip + " st: " + isthep[i] + " id: " + idhep[i] +
+ " jmo: " + jmohep[2 * i] + " " + jmohep[2 * i + 1] +
+ " jda: " + jdahep[2 * i] + " " + jdahep[2 * i + 1]);
+ System.out.println("p: " + phep[5 * i] + " " + phep[5 * i + 1] + " " +
+ phep[5 * i + 2] + " " + phep[5 * i + 3] + " " + phep[5 * i + 4]);
+ System.out.println("v: " + vhep[4 * i] + " " + vhep[4 * i + 1] + " " +
+ vhep[4 * i + 2] + " " + vhep[4 * i + 3]);
+ }
+ }
+ StdhepEvent ev = new StdhepEvent(nevhep, nhep, isthep, idhep, jmohep, jdahep, phep, vhep);
+ sw.writeRecord(ev);
+ }
+ List<Double> end = getNumbersInLine(tok);
+ if (end.size() != 7) {
+ throw new RuntimeException("Unexpected entry in begin run record");
+ }
+ int nreqe = end.get(0).intValue();
+ int ngene = end.get(1).intValue();
+ int nwrite = end.get(2).intValue();
+ float ecme = end.get(3).floatValue();
+ float xsece = end.get(4).floatValue();
+ double rn1e = end.get(5);
+ double rn2e = end.get(6);
+ StdhepEndRun se = new StdhepEndRun(nreqe, ngene, nwrite, ecme, xsece, rn1e, rn2e);
+ sw.writeRecord(se);
+
+ sw.close();
+ fr.close();
+// } catch (IOException e) {
+// System.out.println("Error opening " + infile);
+// }
+ }
+
+ @SuppressWarnings("static-access")
+ private static List<Double> getNumbersInLine(StreamTokenizer tok) throws IOException {
+ List<Double> nums = new ArrayList<Double>();
+ while (tok.nextToken() != tok.TT_EOF) {
+ if (tok.ttype == tok.TT_EOL) {
+ break;
+ }
+
+ if (tok.ttype != tok.TT_NUMBER) {
+ throw new RuntimeException("Non numeric data encountered");
+ }
+
+ nums.add(tok.nval);
+ }
+
+ return nums;
+ }
+}