LISTSERV mailing list manager LISTSERV 16.5

Help for HPS-SVN Archives


HPS-SVN Archives

HPS-SVN Archives


HPS-SVN@LISTSERV.SLAC.STANFORD.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

HPS-SVN Home

HPS-SVN Home

HPS-SVN  May 2015

HPS-SVN May 2015

Subject:

r3067 - /java/trunk/record-util/src/main/java/org/hps/record/evio/EvioEventSkimmer.java

From:

[log in to unmask]

Reply-To:

Notification of commits to the hps svn repository <[log in to unmask]>

Date:

Fri, 29 May 2015 22:35:37 -0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (248 lines)

Author: [log in to unmask]
Date: Fri May 29 15:35:29 2015
New Revision: 3067

Log:
Add command line utility class for skimming EVIO events.

Added:
    java/trunk/record-util/src/main/java/org/hps/record/evio/EvioEventSkimmer.java

Added: java/trunk/record-util/src/main/java/org/hps/record/evio/EvioEventSkimmer.java
 =============================================================================
--- java/trunk/record-util/src/main/java/org/hps/record/evio/EvioEventSkimmer.java	(added)
+++ java/trunk/record-util/src/main/java/org/hps/record/evio/EvioEventSkimmer.java	Fri May 29 15:35:29 2015
@@ -0,0 +1,232 @@
+package org.hps.record.evio;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
+import org.jlab.coda.jevio.EventWriter;
+import org.jlab.coda.jevio.EvioEvent;
+import org.jlab.coda.jevio.EvioReader;
+import org.lcsim.util.log.DefaultLogFormatter;
+import org.lcsim.util.log.LogUtil;
+
+/**
+ * Skim EVIO events into a new file based on a list of event numbers to include.
+ * <p>
+ * The EVIO event numbers come from the event ID bank, and not from calling 
+ * <code>EvioEvent.getEventNumber()</code>.  These never match, unfortunately!
+ * 
+ * @author Jeremy McCormick
+ * @author Norman Graf
+ */
+public class EvioEventSkimmer {
+
+    /**
+     * Setup the logger.
+     */
+    private static Logger LOGGER = LogUtil.create(EvioEventSkimmer.class, new DefaultLogFormatter(), Level.CONFIG);
+
+    /**
+     * Define command line options.
+     */
+    private static Options OPTIONS = new Options();
+    static {
+        OPTIONS.addOption("s", "skim-file", true, "list of event numbers to include in skim (text file)");
+        OPTIONS.addOption("o", "output-file", true, "output EVIO file with skimmed events");
+        OPTIONS.addOption("e", "evio-list", true, "input EVIO files to process (text file)");
+        OPTIONS.addOption("L", "log-level", true, "set log level (Java conventions)");
+        OPTIONS.addOption("n", "max-events", true, "max number of events to read");
+    }
+
+    /**
+     * Run the skim from the command line.
+     * 
+     * @param args the command line arguments (parsed using Apache CLI)
+     */
+    public static void main(String[] args) {
+
+        PosixParser parser = new PosixParser();
+
+        CommandLine commandLine = null;
+        try {
+            commandLine = parser.parse(OPTIONS, args);
+        } catch (ParseException e) {
+            throw new RuntimeException(e);
+        }
+        
+        if (commandLine.hasOption("L")) {
+            Level newLevel = Level.parse(commandLine.getOptionValue("L"));
+            LOGGER.config("setting new log level to " + newLevel);
+            LOGGER.setLevel(newLevel);
+        }
+
+        // Get list of EVIO files to process.
+        String evioTxtFile = null;
+        if (commandLine.hasOption("e")) {
+            evioTxtFile = commandLine.getOptionValue("e");
+        } else {
+            throw new RuntimeException("missing -e argument");
+        }
+        List<String> evioFilePaths = getEvioFilePaths(evioTxtFile);
+
+        // Get the EVIO output file path.
+        String evioFileOutPath = null;
+        if (commandLine.hasOption("o")) {
+            evioFileOutPath = commandLine.getOptionValue("o");
+            if (new File(evioFileOutPath).exists()) {
+                throw new RuntimeException("output file already exists: " + evioFileOutPath);
+            }
+        } else {
+            throw new RuntimeException("misisng -o argument");
+        }        
+        LOGGER.config("output will be written to " + evioFileOutPath);
+        
+        // Get the max number of events to read.
+        int maxEvents = Integer.MAX_VALUE;
+        if (commandLine.hasOption("n")) {
+            maxEvents = Integer.parseInt(commandLine.getOptionValue("n"));
+            LOGGER.config("max events set to " + maxEvents);
+        }
+        
+        // Get the list of events to include in the skim.
+        String skimFilePath = null;
+        if (commandLine.hasOption("s")) {
+            skimFilePath = commandLine.getOptionValue("s");
+            LOGGER.config("skim events will be read from " + skimFilePath);
+        } else {
+            throw new RuntimeException("missing -s argument with skim events");
+        }
+        Set<Integer> skimEvents = getSkimEvents(skimFilePath);
+        LOGGER.config("got " + skimEvents.size() + " event numbers for skim");
+
+        EventWriter writer = null;
+        EvioReader reader = null;
+        try {
+
+            // Open writer with EVIO output path.
+            writer = new EventWriter(evioFileOutPath, false);
+            
+            int nEventsRead = 0;
+
+            // Loop over input files.
+            fileLoop: for (String evioFileInPath : evioFilePaths) {
+
+                LOGGER.info("opening " + evioFileInPath + " for reading");
+                reader = new EvioReader(evioFileInPath, false, true);
+                
+                // Read all events and include in the skim if they are in the event list.
+                EvioEvent evioEvent = null;
+                while ((evioEvent = reader.parseNextEvent()) != null) {                    
+                    LOGGER.finer("read EVIO event " + evioEvent.getEventNumber());
+                    
+                    if (nEventsRead >= maxEvents) {
+                        LOGGER.info("max events " + maxEvents + " was reached");
+                        break fileLoop;
+                    }
+
+                    // Set event number from event ID bank.
+                    EvioEventUtilities.setEventNumber(evioEvent);
+
+                    LOGGER.finest("event number set to " + evioEvent.getEventNumber() + " from event ID");
+                    
+                    if (skimEvents.contains(evioEvent.getEventNumber())) {
+                        // Event is accepted.
+                        LOGGER.info("including event " + evioEvent.getEventNumber() + " in skim");
+                        writer.writeEvent(evioEvent);
+                        LOGGER.finer("wrote " + writer.getEventsWritten() + " events so far");
+                    } else {
+                        // Event is rejected.
+                        LOGGER.finer("event " + evioEvent.getEventNumber() + " rejected");
+                    }
+                    ++nEventsRead;
+                }
+                
+                // Close reader.
+                LOGGER.info("closing reader");
+                reader.close();
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            writer.close();
+            try {
+                reader.close();
+            } catch (IOException e) {
+                LOGGER.log(Level.SEVERE, e.getMessage(), e);
+            }
+        }
+        
+        LOGGER.info("Done!");
+    }
+
+    /**
+     * Get the list of EVIO files to process from a text file list.
+     * 
+     * @param txtFilePath the text file with EVIO file list
+     * @return a list of paths to EVIO files
+     */
+    static List<String> getEvioFilePaths(String txtFilePath) {
+        List<String> evioFilePaths = new ArrayList<String>();
+        BufferedReader br = null;
+        try {
+            br = new BufferedReader(new FileReader(txtFilePath));
+            String currentLine = null;
+            while ((currentLine = br.readLine()) != null) {
+                evioFilePaths.add(currentLine.trim());
+            }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        } finally {
+            try {
+                br.close();
+            } catch (IOException e) {
+                LOGGER.log(Level.SEVERE, e.getMessage(), e);
+            }
+        }
+        return evioFilePaths;
+    }
+    
+    /**
+     * Get a list of event numbers to include in the skim.
+     * 
+     * @param txtFilePath text file with list of events to include (one per line)
+     * @return the list of event numbers to include
+     */
+    static Set<Integer> getSkimEvents(String txtFilePath) {
+        Set<Integer> skimEvents = new LinkedHashSet<Integer>();
+        BufferedReader br = null;
+        String currentLine = null;
+        try {
+            br = new BufferedReader(new FileReader(txtFilePath));            
+            while ((currentLine = br.readLine()) != null) {
+                String fileName = currentLine.trim();
+                LOGGER.config("including " + fileName + " in EVIO input files");
+                Integer skimEvent = Integer.parseInt(fileName);
+                skimEvents.add(skimEvent);
+            }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        } catch (NumberFormatException e) {
+            LOGGER.log(Level.SEVERE, "bad number format in skim list: " + currentLine);
+            throw new RuntimeException(e);
+        } finally {
+            try {
+                br.close();
+            } catch (IOException e) {
+                LOGGER.log(Level.SEVERE, e.getMessage(), e);
+            }
+        }
+        return skimEvents;
+    }
+}

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

November 2017
August 2017
July 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013

ATOM RSS1 RSS2



LISTSERV.SLAC.STANFORD.EDU

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager

Privacy Notice, Security Notice and Terms of Use