1 package org.lcsim.util.aida;
2
3 import hep.aida.IAnalysisFactory;
4 import hep.aida.IBaseHistogram;
5 import hep.aida.IDataPointSet;
6 import hep.aida.IFunction;
7 import hep.aida.IManagedObject;
8 import hep.aida.IPlottable;
9 import hep.aida.IPlotter;
10 import hep.aida.IPlotterRegion;
11 import hep.aida.ITree;
12
13 import java.io.File;
14 import java.io.IOException;
15
16
17
18
19
20
21
22 public class GraphicsWriter {
23
24 ITree tree;
25 String graphicsFormat = "png";
26 File outputDir = new File(".");
27 IPlotter plotter;
28
29
30
31
32
33
34
35 public GraphicsWriter(File aidaFile, String graphicsFormat, File outputDir) {
36 IAnalysisFactory analysisFactory = IAnalysisFactory.create();
37 if (!aidaFile.exists())
38 throw new IllegalArgumentException("The input AIDA file does not exist.");
39 try {
40 tree = analysisFactory.createTreeFactory().create(aidaFile.getAbsolutePath());
41 } catch (IllegalArgumentException | IOException e) {
42 throw new RuntimeException(e);
43 }
44 plotter = analysisFactory.createPlotterFactory().create();
45 if (graphicsFormat != null)
46 this.graphicsFormat = graphicsFormat;
47 if (outputDir != null) {
48 this.outputDir = outputDir;
49 }
50 }
51
52
53
54
55
56
57
58 public GraphicsWriter(ITree tree, String graphicsFormat, File outputDir) {
59 if (tree != null)
60 this.tree = tree;
61 else
62 throw new IllegalArgumentException("The tree points to null.");
63 plotter = IAnalysisFactory.create().createPlotterFactory().create();
64 if (graphicsFormat != null)
65 this.graphicsFormat = graphicsFormat;
66 if (outputDir != null)
67 this.outputDir = outputDir;
68 }
69
70
71
72
73 public void writeToDir() {
74 File absDir = outputDir.getAbsoluteFile();
75 for (String path : tree.listObjectNames()) {
76 String[] objectNames = tree.listObjectNames(path);
77 for (String objectName : objectNames) {
78 IManagedObject object = tree.find(objectName);
79 plotter.createRegion();
80 IPlotterRegion region = plotter.region(0);
81 if (object instanceof IBaseHistogram)
82 region.plot((IBaseHistogram) object);
83 else if (object instanceof IDataPointSet)
84 region.plot((IDataPointSet) object);
85 else if (object instanceof IFunction)
86 region.plot((IFunction) object);
87 else if (object instanceof IPlottable)
88 region.plot((IPlottable) object);
89
90 File plotFile = new File(absDir + File.separator + objectName + "." + graphicsFormat);
91 File plotDir = plotFile.getParentFile();
92 if (!plotDir.exists())
93 plotDir.mkdirs();
94
95 try {
96 plotter.writeToFile(plotFile.getAbsolutePath(), graphicsFormat);
97 } catch (IOException e) {
98 throw new RuntimeException(e);
99 }
100 plotter.destroyRegions();
101 }
102 }
103 }
104 }