hps-java/src/main/java/org/lcsim/hps/users/phansson
diff -u -r1.2 -r1.3
--- mergeSimpleAIDA.java 24 Jul 2012 23:28:10 -0000 1.2
+++ mergeSimpleAIDA.java 11 Dec 2012 01:01:39 -0000 1.3
@@ -1,7 +1,3 @@
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
package org.lcsim.hps.users.phansson;
import hep.aida.*;
@@ -17,32 +13,27 @@
/**
*
* @author phansson
+ * @version $Id: mergeSimpleAIDA.java,v 1.3 2012/12/11 01:01:39 meeg Exp $
*/
-
-
-
public class mergeSimpleAIDA {
/**
* @param args the command line arguments
*/
-
- private static Options createCommandLineOptions() {
- Options option = new Options();
- option.addOption("r", true, "Regular expression to match files");
- option.addOption("d", true, "File directory");
- option.addOption("o", true, "Merged file name");
- option.addOption("t", false, "Print files to be merged only");
- return option;
+ private static Options createCommandLineOptions() {
+ Options option = new Options();
+ option.addOption("r", true, "Regular expression to match files");
+ option.addOption("d", true, "File directory");
+ option.addOption("o", true, "Merged file name");
+ option.addOption("t", false, "Print files to be merged only");
+ return option;
}
-
+
public static void main(String[] args) {
// TODO code application logic here
- IAnalysisFactory af = IAnalysisFactory.create();
-
Options opts = createCommandLineOptions();
- if(args.length == 0) {
+ if (args.length == 0) {
HelpFormatter help = new HelpFormatter();
help.printHelp(" ", opts);
System.exit(1);
@@ -54,63 +45,60 @@
} catch (ParseException ex) {
Logger.getLogger(mergeSimpleAIDA.class.getName()).log(Level.SEVERE, null, ex);
}
- String regexp = "";
- if(cmd.hasOption("r")) {
+ String regexp = ".*aida$";
+ if (cmd.hasOption("r")) {
regexp = cmd.getOptionValue("r");
}
-
- String path = "";
- if(cmd.hasOption("d")) {
+
+ String path = ".";
+ if (cmd.hasOption("d")) {
path = cmd.getOptionValue("d");
}
- String outfile = "";
- if(cmd.hasOption("o")) {
+ String outfile = "merged";
+ if (cmd.hasOption("o")) {
outfile = cmd.getOptionValue("o");
}
boolean testOnly = false;
- if(cmd.hasOption("t")) {
+ if (cmd.hasOption("t")) {
testOnly = true;
}
-
- File[] files = listFilesMatching(path,regexp);
-
- if(files==null || files.length==0) {
+
+ File[] files = listFilesMatching(path, regexp);
+
+ if (files == null || files.length == 0) {
System.out.println("No files matched " + regexp + " in " + path);
System.exit(1);
}
-
+
System.out.println("Found " + files.length + " matching files");
-
- if(testOnly) {
-
- for(File f : files) {
+
+ if (testOnly) {
+
+ for (File f : files) {
System.out.println(f.getName());
}
} else {
- mergeFiles(files,path+"/"+outfile);
+ mergeFiles(files, path + "/" + outfile);
}
-
-
-
-
+
+
+
+
}
-
-
-
-
- public static void mergeFiles(File[] files,String outFileName) {
-
+
+ public static void mergeFiles(File[] files, String outFileName) {
+
System.out.println("Merging " + files.length + " into " + outFileName);
AIDA aida = AIDA.defaultInstance();
IAnalysisFactory af = aida.analysisFactory();
IHistogramFactory hf = aida.histogramFactory();
int count = 0;
-
- for(File f: files) {
-
+
+ for (File f : files) {
+
String fname = f.getName();
System.out.println("Processing file f " + f.toString());
- ITree tree=null;
+ ITree tree = null;
try {
tree = af.createTreeFactory().create(fname);
} catch (IllegalArgumentException ex) {
@@ -118,70 +106,87 @@
} catch (IOException ex) {
Logger.getLogger(mergeSimpleAIDA.class.getName()).log(Level.SEVERE, null, ex);
}
- for( String str : tree.listObjectNames()) {
+ for (String str : tree.listObjectNames()) {
//if(!str.contains("Cluster energy x")) continue;
IManagedObject obj = tree.find(str);
- if(!IHistogram1D.class.isInstance(obj)) continue;
- IHistogram1D h = (IHistogram1D)obj;
- if(h==null) {
- System.out.println("Error " + str + " had problems to be cast to 1D?");
- continue;
+ if (IHistogram1D.class.isInstance(obj)) {
+ IHistogram1D h = (IHistogram1D) obj;
+ if (h == null) {
+ System.out.println("Error " + str + " had problems to be cast to 1D?");
+ continue;
+ }
+ String name = h.title();// + " merged";
+ IHistogram1D hc = null;// = aida.histogram1D(name);
+ try {
+ hc = (IHistogram1D) aida.tree().find(name);
+ } catch (IllegalArgumentException ex) {
+ System.out.println(" creating " + name);
+ }
+ if (hc == null) {
+
+ hc = hf.createCopy(name, h);
+ } else {
+ hc = hf.add(hc.title(), hc, h);
+ }
+ if (name.contains("x=1 y=1")) {
+ System.out.println("Now " + hc.entries() + " entries (<m>=" + hc.mean() + " RMS=" + h.rms() + ")");
+ }
}
- String name = h.title();// + " merged";
- IHistogram1D hc = null;// = aida.histogram1D(name);
- try {
- hc = (IHistogram1D) aida.tree().find(name);
- } catch (IllegalArgumentException ex) {
- System.out.println(" creating " + name);
- }
- if(hc==null) {
-
- hc = hf.createCopy(name, h);
- } else {
- hc = hf.add(hc.title(), hc, h);
- }
- if(name.contains("x=1 y=1")) {
- System.out.println("Now " + hc.entries() + " entries (<m>="+hc.mean()+" RMS="+h.rms()+")");
+ if (IHistogram2D.class.isInstance(obj)) {
+ IHistogram2D h = (IHistogram2D) obj;
+ if (h == null) {
+ System.out.println("Error " + str + " had problems to be cast to 1D?");
+ continue;
+ }
+ String name = h.title();// + " merged";
+ IHistogram2D hc = null;// = aida.histogram1D(name);
+ try {
+ hc = (IHistogram2D) aida.tree().find(name);
+ } catch (IllegalArgumentException ex) {
+ System.out.println(" creating " + name);
+ }
+ if (hc == null) {
+
+ hc = hf.createCopy(name, h);
+ } else {
+ hc = hf.add(hc.title(), hc, h);
+ }
}
}
-
+
++count;
-
-
-
+
+
+
}
try {
aida.saveAs(outFileName);
} catch (IOException ex) {
Logger.getLogger(mergeSimpleAIDA.class.getName()).log(Level.SEVERE, null, ex);
}
-
+
}
-
-
-
+
public static File[] listFilesMatching(String path, String regexp) {
- //Find files matching the reg exp
- System.out.println("Find files in "+path+" matching the reg exp " + regexp);
+ //Find files matching the reg exp
+ System.out.println("Find files in " + path + " matching the reg exp " + regexp);
File dir = new File(path);
- if(!dir.isDirectory()) {
- throw new IllegalArgumentException(path+" is no directory.");
+ if (!dir.isDirectory()) {
+ throw new IllegalArgumentException(path + " is no directory.");
//System.out.println(path+" is not a dir?!");
}
final Pattern p = Pattern.compile(regexp);
System.out.println("pattern " + p.toString());
- return dir.listFiles(new FilenameFilter() {
+ return dir.listFiles(new FilenameFilter() {
+
@Override
- public boolean accept(File file,String name) {
+ public boolean accept(File file, String name) {
boolean match = p.matcher(name).matches();
//System.out.println("accepting file " + name + ": " + match);
return match;
}
-
-
-
});
}
}