Commit in hps-java/src/main/java/org/lcsim/hps/users/phansson on MAIN | |||
mergeSimpleAIDA.java | +171 | added 1.1 |
Merge aida files.
diff -N mergeSimpleAIDA.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ mergeSimpleAIDA.java 23 Jul 2012 21:14:52 -0000 1.1 @@ -0,0 +1,171 @@
+/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.lcsim.hps.users.phansson; + +import hep.aida.*; +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.regex.Pattern; +import org.apache.commons.cli.*; +import org.lcsim.util.aida.AIDA; + +/** + * + * @author phansson + */ + + + +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"); + return option; + } + + public static void main(String[] args) { + // TODO code application logic here + + IAnalysisFactory af = IAnalysisFactory.create(); + + Options opts = createCommandLineOptions(); + if(args.length == 0) { + HelpFormatter help = new HelpFormatter(); + help.printHelp(" ", opts); + System.exit(1); + } + CommandLineParser parser = new PosixParser(); + CommandLine cmd = null; + try { + cmd = parser.parse(opts, args); + } catch (ParseException ex) { + Logger.getLogger(mergeSimpleAIDA.class.getName()).log(Level.SEVERE, null, ex); + } + String regexp = ""; + if(cmd.hasOption("r")) { + regexp = cmd.getOptionValue("r"); + } + + String path = ""; + if(cmd.hasOption("d")) { + path = cmd.getOptionValue("d"); + } + String outfile = ""; + if(cmd.hasOption("o")) { + outfile = cmd.getOptionValue("o"); + } + + 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.toString() + " matching files"); + + + mergeFiles(files,path+"/"+outfile); + + + + + + } + + + + + 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) { + + String fname = f.getName(); + System.out.println("Processing file f " + f.toString()); + ITree tree=null; + try { + tree = af.createTreeFactory().create(fname); + } catch (IllegalArgumentException ex) { + Logger.getLogger(mergeSimpleAIDA.class.getName()).log(Level.SEVERE, null, ex); + } catch (IOException ex) { + Logger.getLogger(mergeSimpleAIDA.class.getName()).log(Level.SEVERE, null, ex); + } + for( String str : tree.listObjectNames()) { + if(!str.contains("Cluster energy x")) continue; + IHistogram1D h = (IHistogram1D)tree.find(str); + if(h==null) { + System.out.println("Error " + str + " had problems to be cast to 1D?"); + continue; + } + String name = h.title() + " merged"; + IHistogram1D hc = null; + try { + hc = (IHistogram1D) aida.tree().find(name); + } catch (IllegalArgumentException ex) { + System.out.println(" creating " + name); + } + //IHistogram1D hc = aida.histogram1D(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); + File dir = new File(path); + 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() { + @Override + public boolean accept(File file,String name) { + boolean match = p.matcher(name).matches(); + System.out.println("accepting file " + name + ": " + match); + return match; + } + + + + }); + } +}
Use REPLY-ALL to reply to list
To unsubscribe from the LCD-CVS list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=LCD-CVS&A=1