hps-java/src/main/java/org/lcsim/hps/users/phansson
diff -u -r1.1 -r1.2
--- DumpAIDATextFiles.java 24 Aug 2012 16:00:18 -0000 1.1
+++ DumpAIDATextFiles.java 31 Oct 2013 00:08:53 -0000 1.2
@@ -1,11 +1,8 @@
-/*
- * 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.IHistogram2D;
import hep.aida.IManagedObject;
import hep.aida.ITree;
import java.io.File;
@@ -20,13 +17,9 @@
/**
*
- * @author phansson
- * Dumps all histograms in AIDA files to a standard text format
+ * @author phansson Dumps all histograms in AIDA files to a standard text format
+ * @version $Id: DumpAIDATextFiles.java,v 1.2 2013/10/31 00:08:53 meeg Exp $
*/
-
-
-
-
public class DumpAIDATextFiles {
private static Options createCommandLineOptions() {
@@ -38,55 +31,70 @@
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;
+ FileWriter fWriter;
+ PrintWriter pWriter;
try {
fWriter = new FileWriter(fileName);
pWriter = new PrintWriter(fWriter);
-
- for( String name : tree.listObjectNames()) {
+
+ for (String name : tree.listObjectNames()) {
//System.out.println(str);
IManagedObject obj = tree.find(name);
- if(obj instanceof IHistogram1D ) {
+ if (obj instanceof IHistogram1D) {
//System.out.println(name + " is a histogram");
- IHistogram1D h = (IHistogram1D)obj;
+ IHistogram1D h = (IHistogram1D) obj;
String htxt = convertHist1D(h);
pWriter.println(htxt);
}
+ if (obj instanceof IHistogram2D) {
+ //System.out.println(name + " is a histogram");
+ IHistogram2D h = (IHistogram2D) obj;
+ String htxt = convertHist2D(h);
+ pWriter.println(htxt);
+ }
}
-
-
-
- if(fWriter!=null) fWriter.close();
- if(pWriter!=null) pWriter.close();
+
+ fWriter.close();
+ 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) {
+ 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 String convertHist2D(IHistogram2D h) {
+ //Type title bincontent
+ String htxt = "IHistogram2D \"" + h.title() + "\" " + h.xAxis().bins() + " " + h.xAxis().lowerEdge() + " " + h.xAxis().upperEdge() + h.yAxis().bins() + " " + h.yAxis().lowerEdge() + " " + h.yAxis().upperEdge();
+ for (int xbin = 0; xbin < h.xAxis().bins(); ++xbin) {
+ for (int ybin = 0; ybin < h.yAxis().bins(); ++ybin) {
+ htxt += " " + h.binEntries(xbin, ybin);
+ htxt += " " + h.binHeight(xbin, ybin);
+ }
+ }
+ return htxt;
+ }
}
public static void main(String[] args) {
@@ -107,18 +115,18 @@
throw new RuntimeException("Problem parsing command line options.", e);
}
List<String> fileList = new ArrayList<String>();
- if(cl.hasOption("f")) {
+ if (cl.hasOption("f")) {
fileList.add(cl.getOptionValue("f"));
- } else if(cl.hasOption("d")) {
+ } 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")) {
+ 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")) {
+ for (File f : dir.listFiles()) {
+ if (f.isFile() && f.getName().contains(".aida")) {
fileList.add(f.getAbsolutePath());
}
}
@@ -126,13 +134,13 @@
}
IAnalysisFactory af = IAnalysisFactory.create();
TextFileUtil util = new TextFileUtil();
- for(String file : fileList) {
+ for (String file : fileList) {
System.out.println("Converting file " + file);
- if(!file.contains(".aida")) {
+ if (!file.contains(".aida")) {
System.out.println("This is not an AIDA file?!");
continue;
}
- ITree tree=null;
+ ITree tree = null;
try {
tree = af.createTreeFactory().create(file);
} catch (IllegalArgumentException ex) {
@@ -140,16 +148,14 @@
} 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);
+ if (tree == null) {
+ System.out.println("Couldn't create \"tree\" for file: " + file);
continue;
}
String txtFileName = file.replaceAll(".aida", ".histtxt");
util.createTextFile(txtFileName, tree);
-
+
}
-
- }
-
+ }
}