hps-java/src/main/java/org/lcsim/hps/users/phansson
diff -N DumpAIDATextFiles.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DumpAIDATextFiles.java 24 Aug 2012 16:00:18 -0000 1.1
@@ -0,0 +1,155 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.lcsim.hps.users.phansson;
+
+import hep.aida.IAnalysisFactory;
+import hep.aida.IHistogram1D;
+import hep.aida.IManagedObject;
+import hep.aida.ITree;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.apache.commons.cli.*;
+
+/**
+ *
+ * @author phansson
+ * Dumps all histograms in AIDA files to a standard text format
+ */
+
+
+
+
+public class DumpAIDATextFiles {
+
+ private static Options createCommandLineOptions() {
+
+ Options options = new Options();
+ //options.addOption(new Option("p", true, "The pattern to match. [NOT IMPLEMENTED YET]"));
+ options.addOption(new Option("f", true, "The file to use."));
+ options.addOption(new Option("d", true, "The directory with files."));
+ return options;
+
+ }
+ /**
+ * @param args the command line arguments
+ */
+
+ public static class TextFileUtil {
+
+ public TextFileUtil() {
+
+ }
+ public void createTextFile(String fileName, ITree tree) {
+ //System.out.println("-----\nCreating text file "+fileName+" from tree with " + tree.listObjectNames().length + " objects");
+ FileWriter fWriter=null;
+ PrintWriter pWriter=null;
+ try {
+ fWriter = new FileWriter(fileName);
+ pWriter = new PrintWriter(fWriter);
+
+ for( String name : tree.listObjectNames()) {
+ //System.out.println(str);
+ IManagedObject obj = tree.find(name);
+ if(obj instanceof IHistogram1D ) {
+ //System.out.println(name + " is a histogram");
+ IHistogram1D h = (IHistogram1D)obj;
+ String htxt = convertHist1D(h);
+ pWriter.println(htxt);
+ }
+ }
+
+
+
+ if(fWriter!=null) fWriter.close();
+ if(pWriter!=null) pWriter.close();
+ } catch (IOException ex) {
+ Logger.getLogger(DumpAIDATextFiles.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ //System.out.println("-----");
+
+
+ }
+
+ public String convertHist1D(IHistogram1D h) {
+ //Type title bincontent
+ String htxt = "IHistogram1D \""+h.title()+"\" " + h.axis().bins() + " " + h.axis().lowerEdge() + " " + h.axis().upperEdge();
+ for(int ibin=0;ibin<h.axis().bins();++ibin) {
+ htxt += " " + h.binEntries(ibin);
+ }
+ return htxt;
+ }
+
+ }
+
+ public static void main(String[] args) {
+ // TODO code application logic here
+
+ Options options = createCommandLineOptions();
+ if (args.length == 0) {
+ System.out.println("TestRunEvioToLcio [options] [evioFiles]");
+ HelpFormatter help = new HelpFormatter();
+ help.printHelp(" ", options);
+ System.exit(1);
+ }
+ CommandLineParser parser = new PosixParser();
+ CommandLine cl = null;
+ try {
+ cl = parser.parse(options, args);
+ } catch (ParseException e) {
+ throw new RuntimeException("Problem parsing command line options.", e);
+ }
+ List<String> fileList = new ArrayList<String>();
+ if(cl.hasOption("f")) {
+ fileList.add(cl.getOptionValue("f"));
+ } else if(cl.hasOption("d")) {
+ //check if pattern is to be used or simply use run all aida files
+ String dirName = cl.getOptionValue("d");
+ if(cl.hasOption("p")) {
+ System.out.println("The pattern option is not implemented. Please do it!");
+ System.exit(1);
+ } else {
+ File dir = new File(dirName);
+ for(File f : dir.listFiles()) {
+ if(f.isFile() && f.getName().contains(".aida")) {
+ fileList.add(f.getAbsolutePath());
+ }
+ }
+ }
+ }
+ IAnalysisFactory af = IAnalysisFactory.create();
+ TextFileUtil util = new TextFileUtil();
+ for(String file : fileList) {
+ System.out.println("Converting file " + file);
+ if(!file.contains(".aida")) {
+ System.out.println("This is not an AIDA file?!");
+ continue;
+ }
+ ITree tree=null;
+ try {
+ tree = af.createTreeFactory().create(file);
+ } catch (IllegalArgumentException ex) {
+ Logger.getLogger(DumpAIDATextFiles.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (IOException ex) {
+ Logger.getLogger(DumpAIDATextFiles.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ if(tree==null) {
+ System.out.println("Couldn't create \"tree\" for file: "+file);
+ continue;
+ }
+ String txtFileName = file.replaceAll(".aida", ".histtxt");
+ util.createTextFile(txtFileName, tree);
+
+ }
+
+ }
+
+
+}