hps-java/src/main/java/org/lcsim/hps/users/meeg
diff -N FilterMCBunches.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ FilterMCBunches.java 21 Jun 2012 18:53:57 -0000 1.1
@@ -0,0 +1,159 @@
+package org.lcsim.hps.users.meeg;
+
+import java.io.*;
+
+import java.util.List;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
+import org.lcsim.event.EventHeader;
+import org.lcsim.event.SimCalorimeterHit;
+import org.lcsim.event.SimTrackerHit;
+import org.lcsim.event.base.BaseLCSimEvent;
+import org.lcsim.util.lcio.LCIOReader;
+import org.lcsim.util.lcio.LCIOWriter;
+
+/**
+ * Selects LCIO events passing a cut; spaces out these events with blank events.
+ * Intended use is to clean up a photon-run MC file before running trigger and readout sim.
+ *
+ * @author Sho Uemura <[log in to unmask]>
+ * @version $Id: FilterMCBunches.java,v 1.1 2012/06/21 18:53:57 meeg Exp $
+ */
+public class FilterMCBunches {
+
+ /**
+ * Defines command line options for this program.
+ *
+ * @return The command line options.
+ */
+ private static Options createCommandLineOptions() {
+ Options options = new Options();
+
+ options.addOption(new Option("e", true, "Interval between non-empty events"));
+ options.addOption(new Option("n", true, "Number of events to read"));
+
+ return options;
+ }
+
+ /**
+ * This method will execute the EVIO to LCIO conversion and perform an
+ * intermediate LCSim job. Then the resultant LCIO events will be written to
+ * disk.
+ *
+ * @param args The command line arguments.
+ */
+ public static void main(String[] args) {
+ // Set up command line parsing.
+ Options options = createCommandLineOptions();
+ if (args.length == 0) {
+ System.out.println("FilterMCBunches <input file> <output file>");
+ HelpFormatter help = new HelpFormatter();
+ help.printHelp(" ", options);
+ System.exit(1);
+ }
+ CommandLineParser parser = new PosixParser();
+
+ // Parse command line arguments.
+ CommandLine cl = null;
+ try {
+ cl = parser.parse(options, args);
+ } catch (ParseException e) {
+ throw new RuntimeException("Problem parsing command line options.", e);
+ }
+
+ String[] parsedArgs = cl.getArgs();
+ String inFileName = parsedArgs[0];
+ String outFileName = parsedArgs[1];
+ LCIOReader reader = null;
+ try {
+ reader = new LCIOReader(new File(inFileName));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ LCIOWriter writer = null;
+ try {
+ writer = new LCIOWriter(new File(outFileName));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ int nEmpty = 0;
+ if (cl.hasOption("e")) {
+ nEmpty = Integer.valueOf(cl.getOptionValue("e"));
+ }
+
+ int nEvents = -1;
+ if (cl.hasOption("n")) {
+ nEvents = Integer.valueOf(cl.getOptionValue("n"));
+ }
+ int readEvents = 0;
+ int writtenEvents = 0;
+
+ String detectorName = null;
+ while (nEvents == -1 || readEvents < nEvents) {
+ EventHeader event;
+ try {
+ event = reader.read();
+ } catch (IOException e) {
+ break;
+ }
+ readEvents++;
+
+ if (detectorName == null) {
+ detectorName = event.getDetectorName();
+ }
+
+ if (goodEvent(event)) {
+ writtenEvents++;
+ try {
+ writer.write(event);
+ } catch (IOException e) {
+ break;
+ }
+
+ for (int i = 1; i < nEmpty; i++) {
+ event = new BaseLCSimEvent(0, 0, detectorName);
+ try {
+ writer.write(event);
+ } catch (IOException e) {
+ break;
+ }
+ }
+ }
+ }
+
+ System.out.format("Read %d events, wrote %d of them\n", readEvents, writtenEvents);
+ try {
+ reader.close();
+ writer.close();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private static boolean goodEvent(EventHeader event) {
+ List<SimCalorimeterHit> ecalHits = event.getSimCalorimeterHits("EcalHits");
+ List<SimTrackerHit> trackerHits = event.getSimTrackerHits("TrackerHits");
+
+ double maxE = 0;
+ double totalE = 0;
+ for (SimCalorimeterHit hit : ecalHits) {
+ totalE += hit.getRawEnergy();
+ if (hit.getRawEnergy() > maxE) {
+ maxE = hit.getRawEnergy();
+ }
+ }
+
+// System.out.format("%d SimCalorimeterHits, %d SimTrackerHits, maxE %f, totalE %f\n", ecalHits.size(), trackerHits.size(), maxE, totalE);
+
+
+// return (ecalHits.size() + trackerHits.size() != 0);
+ return (totalE>0.01);
+ }
+}
\ No newline at end of file