Author: [log in to unmask] Date: Tue Sep 22 12:11:46 2015 New Revision: 3666 Log: Added AIDA plot formatting framework. Added: java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/PlotsFormatter.java java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/ java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/InvariantMassPlotsFormatter.java java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/MTEPlotFormatter.java java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/MTETriggerPlotsFormatter.java java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/ParticleMCAnalysisPlotsFormatter.java (with props) java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/SingleTriggerPlotsFormatter.java (with props) java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/TridentTrackFormatter.java java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/TriggerPlotsFormat.java Added: java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/PlotsFormatter.java ============================================================================= --- java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/PlotsFormatter.java (added) +++ java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/PlotsFormatter.java Tue Sep 22 12:11:46 2015 @@ -0,0 +1,127 @@ +package org.hps.users.kmccarty.plots; + +import hep.aida.ref.plotter.PlotterRegion; +import jas.hist.JASHist1DHistogramStyle; +import jas.hist.JASHist2DHistogramStyle; + +import java.awt.Color; +import java.awt.Font; + +public class PlotsFormatter { + // Define plot fonts. + public static final Font BASIC_FONT = new Font("Calibri", Font.PLAIN, 30); + public static final Font AXIS_FONT = new Font("Calibri", Font.BOLD, 35); + public static final Font TITLE_FONT = new Font("Calibri", Font.BOLD, 45); + + // Defines the color style options for plot data. + public static enum ColorStyle { + MS_BLUE(new Color( 79, 129, 189), new Color( 36, 64, 97)), MS_ORANGE(new Color(247, 150, 70), new Color(152, 72, 6)), + MS_RED(new Color(192, 80, 77), new Color( 99, 36, 35)), GREY(new Color(166, 166, 166), new Color( 89, 89, 89)), + MS_GREEN(new Color(155, 187, 89), new Color( 79, 98, 40)), CRIMSON(new Color(161, 0, 0), new Color(104, 0, 0)), + RUST(new Color(161, 80, 0), new Color(105, 80, 0)), YELLOW(new Color(161, 161, 0), new Color(122, 109, 8)), + FOREST(new Color( 65, 102, 0), new Color( 37, 79, 0)), GREEN(new Color( 7, 132, 70), new Color( 7, 82, 30)), + TEAL(new Color( 0, 130, 130), new Color( 0, 90, 100)), CERULEAN(new Color( 0, 86, 130), new Color( 0, 28, 83)), + BLUE(new Color( 0, 33, 203), new Color( 0, 0, 137)), INDIGO(new Color( 68, 10, 127), new Color( 0, 0, 61)), + PURPLE(new Color(106, 0, 106), new Color( 63, 0, 56)), FUSCHIA(new Color(119, 0, 60), new Color( 60, 0, 60)); + + private final Color fillColor; + private final Color lineColor; + + private ColorStyle(Color fillColor, Color lineColor) { + this.fillColor = fillColor; + this.lineColor = lineColor; + } + + public Color getFillColor() { return fillColor; } + + public Color getLineColor() { return lineColor; } + }; + + /** + * Sets the plot display formatting for 1D plots. + * @param region - The plotter region to format. + * @param color - The data color settings to use. + */ + public static final void setDefault1DStyle(PlotterRegion region, ColorStyle[] color) { + // Get the names of each plot on in the region. + String[] dataNames = region.getAllDataNames(); + + // Check whether this is an overlay plot. Overlay plots contain + // more than one data name. + boolean overlay = (dataNames.length > 1 ? true : false); + + // Iterate over each plot in the region. + for(int i = 0; i < dataNames.length; i++) { + // Set the overlay style if needed. + if(overlay) { + // Get the fill style for the current data type. + JASHist1DHistogramStyle fillStyle = (JASHist1DHistogramStyle) region.getDataForName(dataNames[i]).getStyle(); + + // Set the histogram style to display thick-lined bars + // with no fill. The color is set by the "color" argument. + fillStyle.setHistogramFill(false); + fillStyle.setHistogramBarLineWidth(3); + fillStyle.setHistogramBarLineColor(color[i].getFillColor()); + + // Set the legend text style. + region.getPlot().getLegend().setFont(new Font("Calibri", Font.PLAIN, 20)); + } + + // Otherwise, set the fill style for a single plot. + else { + // Get the fill style for the current data type. + JASHist1DHistogramStyle fillStyle = (JASHist1DHistogramStyle) region.getDataForName(dataNames[i]).getStyle(); + + // Set the histogram style to display thick-lined bars + // with a fill color. The colors are defined by the + // "color" argument. + fillStyle.setHistogramBarLineWidth(3); + fillStyle.setHistogramBarColor(color[i].getFillColor()); + fillStyle.setHistogramBarLineColor(color[i].getLineColor()); + } + + // Set the statistics box style. + region.getPlot().getStats().setVisible(true); + region.getPlot().getStats().setFont(BASIC_FONT); + + // Set the title font. + region.getPlot().getTitleObject().setFont(TITLE_FONT); + + // Set the axis tick-mark fonts. + region.getPlot().getXAxis().setFont(BASIC_FONT); + region.getPlot().getYAxis().setFont(BASIC_FONT); + region.getPlot().getXAxis().getLabelObject().setFont(AXIS_FONT); + region.getPlot().getYAxis().getLabelObject().setFont(AXIS_FONT); + } + } + + /** + * Sets the plot display formatting for 1D plots. + * @param region - The plotter region to format. + */ + public static final void setDefault2DStyle(PlotterRegion region, boolean logarithmic) { + // Get the fill style object. 2D plots should never be overlay + // plots, so there should only ever be one data name. + JASHist2DHistogramStyle fillStyle = (JASHist2DHistogramStyle) region.getDataForName(region.getAllDataNames()[0]).getStyle(); + + // Set the fill style for a two-dimensional plot. + if(logarithmic) { fillStyle.setLogZ(true); } + fillStyle.setHistStyle(JASHist2DHistogramStyle.STYLE_COLORMAP); + fillStyle.setColorMapScheme(JASHist2DHistogramStyle.COLORMAP_RAINBOW); + + // Make the statistics box invisible. + region.getPlot().getStats().setVisible(false); + + // Set the general plot font (which is also the z-axis font). + region.getPlot().setFont(BASIC_FONT); + + // Set the title font. + region.getPlot().getTitleObject().setFont(TITLE_FONT); + + // Set the axis tick-mark fonts. + region.getPlot().getXAxis().setFont(BASIC_FONT); + region.getPlot().getYAxis().setFont(BASIC_FONT); + region.getPlot().getXAxis().getLabelObject().setFont(AXIS_FONT); + region.getPlot().getYAxis().getLabelObject().setFont(AXIS_FONT); + } +} Added: java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/InvariantMassPlotsFormatter.java ============================================================================= --- java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/InvariantMassPlotsFormatter.java (added) +++ java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/InvariantMassPlotsFormatter.java Tue Sep 22 12:11:46 2015 @@ -0,0 +1,335 @@ +package org.hps.users.kmccarty.plots.formatter; + +import jas.hist.JASHist1DHistogramStyle; +import jas.hist.JASHist2DHistogramStyle; + +import java.awt.Color; +import java.awt.Font; +import java.io.IOException; + +import org.lcsim.util.aida.AIDA; + +import hep.aida.IAnalysisFactory; +import hep.aida.IHistogram; +import hep.aida.IHistogram1D; +import hep.aida.IHistogram2D; +import hep.aida.IPlotter; +import hep.aida.IPlotterFactory; +import hep.aida.ITree; +import hep.aida.ref.plotter.PlotterRegion; + +public class InvariantMassPlotsFormatter { + // Define plot fonts. + private static final Font BASIC_FONT = new Font("Calibri", Font.PLAIN, 20); + private static final Font AXIS_FONT = new Font("Calibri", Font.BOLD, 25); + private static final Font TITLE_FONT = new Font("Calibri", Font.BOLD, 35); + + // Defines the color style options for plot data. + private enum ColorStyle { + MS_BLUE(new Color( 79, 129, 189), new Color( 36, 64, 97)), MS_ORANGE(new Color(247, 150, 70), new Color(152, 72, 6)), + MS_RED(new Color(192, 80, 77), new Color( 99, 36, 35)), GREY(new Color(166, 166, 166), new Color( 89, 89, 89)), + MS_GREEN(new Color(155, 187, 89), new Color( 79, 98, 40)), CRIMSON(new Color(161, 0, 0), new Color(104, 0, 0)), + RUST(new Color(161, 80, 0), new Color(105, 80, 0)), YELLOW(new Color(161, 161, 0), new Color(122, 109, 8)), + FOREST(new Color( 65, 102, 0), new Color( 37, 79, 0)), GREEN(new Color( 7, 132, 70), new Color( 7, 82, 30)), + TEAL(new Color( 0, 130, 130), new Color( 0, 90, 100)), CERULEAN(new Color( 0, 86, 130), new Color( 0, 28, 83)), + BLUE(new Color( 0, 33, 203), new Color( 0, 0, 137)), INDIGO(new Color( 68, 10, 127), new Color( 0, 0, 61)), + PURPLE(new Color(106, 0, 106), new Color( 63, 0, 56)), FUSCHIA(new Color(119, 0, 60), new Color( 60, 0, 60)); + + private final Color fillColor; + private final Color lineColor; + + private ColorStyle(Color fillColor, Color lineColor) { + this.fillColor = fillColor; + this.lineColor = lineColor; + } + + public Color getFillColor() { return fillColor; } + + public Color getLineColor() { return lineColor; } + }; + + /** + * Loads all plots in a file and formats them according to the + * indicated style. + * @param args - Unused default executable parameter. + * @throws IOException Occurs if there is an issue opening the file. + */ + public static void main(String[] args) throws IOException { + // Define the root directory for the plots. + String rootDir = "D:\\cygwin64\\home\\Kyle\\"; + + // Define the new name of the file containing the trigger plots. + String[] plotFile = { + rootDir + "temp.aida" + }; + + // Define the run numbers for each file. + String[] runNumber = { "1 Hits", "2 Hits" }; + + // Define the scaling factors for each plot. + double scaleFactor = 13.254; + + // Get the plots file and open it. + IAnalysisFactory af = IAnalysisFactory.create(); + ITree[] tree = new ITree[plotFile.length]; + for(int i = 0; i < plotFile.length; i++) { + tree[i] = af.createTreeFactory().create(plotFile[i]); + if(tree[i] == null) { throw new IllegalArgumentException("Unable to load plot file."); } + } + + // Get the histograms. + IHistogram1D[] invariantMassPlots = new IHistogram1D[3]; + invariantMassPlots[0] = (IHistogram1D) tree[0].find("Trident Analysis/Particle Invariant Mass (1 Hit)"); + invariantMassPlots[1] = (IHistogram1D) tree[0].find("Trident Analysis/Particle Invariant Mass (2 Hit)"); + IHistogram1D electronEnergyPlot = (IHistogram1D) tree[0].find("Trident Analysis/Electron Energy"); + IHistogram1D positronEnergyPlot = (IHistogram1D) tree[0].find("Trident Analysis/Positron Energy"); + IHistogram1D energySumPlot = (IHistogram1D) tree[0].find("Trident Analysis/Energy Sum Distribution"); + IHistogram2D energySum2DPlot = (IHistogram2D) tree[0].find("Trident Analysis/2D Energy Distribution"); + IHistogram1D tridentElectronEnergyPlot = (IHistogram1D) tree[0].find("Trident Analysis/Trident Electron Energy"); + IHistogram1D tridentPositronEnergyPlot = (IHistogram1D) tree[0].find("Trident Analysis/Trident Positron Energy"); + + // Define the plot titles and arrays. + IHistogram[] plots = { electronEnergyPlot, positronEnergyPlot, energySumPlot, tridentElectronEnergyPlot, tridentPositronEnergyPlot }; + String[] titles = { "Electron Energy", "Positron Energy", "Energy Sum", "Trident Electron Energy", "Trident Positron Energy" }; + String[] xTitles = { "Energy (GeV)", "Energy (GeV)", "Energy Sum (GeV)", "Energy (GeV)", "Energy (GeV)" }; + + // Re-bin the histograms to have 5-times larger bins. First, + // get the bin count and upper and lower bounds of the plot. + int bins = invariantMassPlots[0].axis().bins(); + double low = invariantMassPlots[0].axis().binLowerEdge(0); + double high = invariantMassPlots[0].axis().binUpperEdge(invariantMassPlots[0].axis().bins() - 1); + + // Create new plots with the larger bin sizes. + AIDA aida = AIDA.defaultInstance(); + IHistogram1D[] newPlot = new IHistogram1D[2]; + newPlot[0] = aida.histogram1D("Particle Invariant Mass (1 Hit)", bins / 5, low, high); + newPlot[1] = aida.histogram1D("Particle Invariant Mass (2 Hit)", bins / 5, low, high); + + // Populate the new plots with the data from the old ones. + for(int j = 0; j < 2; j++) { + for(int i = 0; i < bins; i++) { + int entries = invariantMassPlots[j].binEntries(i); + double center = invariantMassPlots[j].axis().binCenter(i); + for(int k = 0; k < entries; k++) { + newPlot[j].fill(center); + } + } + } + + // Replace the old plots. + invariantMassPlots = newPlot; + + // Create a plotter factory. + IPlotterFactory plotterFactory = af.createPlotterFactory(); + + // Format and display the basic histograms. + for(int i = 0; i < plots.length; i++) { + // Scale the histogram by the appropriate scaling factor. + plots[i].scale(1.0 / scaleFactor); + + // Create a plotter and plotting region for the plot. + IPlotter plotter = plotterFactory.create(titles[i]); + plotter.createRegions(1); + plotter.region(0).plot(plots[i]); + + // Format the axis labels. + PlotterRegion region = (PlotterRegion) plotter.region(0); + region.getPlot().setTitle(titles[i]); + region.getPlot().getXAxis().setLabel(xTitles[i]); + region.getPlot().getYAxis().setLabel("Rate (Hz)"); + + // Format the fonts and general plot presentation. + setDefault1DStyle(region, new ColorStyle[] { ColorStyle.GREY }); + + // Show the plot. + plotter.setParameter("plotterWidth", "2000"); + plotter.setParameter("plotterHeight", "1200"); + plotter.show(); + } + + // Format and display the 2D histogram. + energySum2DPlot.scale(1.0 / scaleFactor); + IPlotter plotter2D = plotterFactory.create("2D Energy Sum"); + plotter2D.createRegions(1); + plotter2D.region(0).plot(energySum2DPlot); + + // Format the axis labels. + PlotterRegion region2D = (PlotterRegion) plotter2D.region(0); + region2D.getPlot().setTitle("2D Energy Sum"); + region2D.getPlot().getXAxis().setLabel("Electron Energy (GeV)"); + region2D.getPlot().getYAxis().setLabel("Positron Energy (GeV)"); + + // Format the fonts and general plot presentation. + setDefault2DStyle(region2D, false); + + // Show the plot. + plotter2D.setParameter("plotterWidth", "2000"); + plotter2D.setParameter("plotterHeight", "1200"); + plotter2D.show(); + + // Format and display the histograms. + for(int i = 0; i < 2; i++) { + // Scale the histogram by the appropriate scaling factor. + invariantMassPlots[i].scale(1.0 / scaleFactor); + + // Create a plotter and plotting region for the plot. + IPlotter plotter = plotterFactory.create("Particle Invariant Mass (" + runNumber[i] + ")"); + plotter.createRegions(1); + plotter.region(0).plot(invariantMassPlots[i]); + + // Format the axis labels. + PlotterRegion region = (PlotterRegion) plotter.region(0); + region.getPlot().setTitle("Particle Invariant Mass (" + runNumber[i] + ")"); + region.getPlot().getXAxis().setLabel("Invariant Mass (GeV)"); + region.getPlot().getYAxis().setLabel("Rate (Hz)"); + + // Format the fonts and general plot presentation. + setDefault1DStyle(region, new ColorStyle[] { ColorStyle.GREY }); + + // Show the plot. + plotter.setParameter("plotterWidth", "2000"); + plotter.setParameter("plotterHeight", "1200"); + plotter.show(); + } + + // Note which plot is the numerator and which is the denominator. + int numerator = 0; + int denominator = 1; + + // Create a new histogram to display the ratios of the rates. + IHistogram1D ratioPlot = AIDA.defaultInstance().histogram1D("Invariant Mass Ratio (" + runNumber[numerator] + " / " + + runNumber[denominator] + ")", invariantMassPlots[0].axis().bins(), + invariantMassPlots[0].axis().lowerEdge(), invariantMassPlots[0].axis().upperEdge()); + + // Iterate over each bin. + for(int bin = 0; bin < invariantMassPlots[0].axis().bins(); bin++) { + // Calculate the ratio. + double ratio = invariantMassPlots[numerator].binHeight(bin) / invariantMassPlots[denominator].binHeight(bin); + + // If the ratio is either not a number of infinite, skip + // this bin. + if(Double.isNaN(ratio) || Double.isInfinite(ratio)) { continue; } + + // Populate the ratio plot bin. + ratioPlot.fill(invariantMassPlots[0].axis().binCenter(bin), ratio); + } + + // Create a plotter and plotting region for the plot. + IPlotter plotter = plotterFactory.create("Invariant Mass Ratio (5411 / 5554)"); + plotter.createRegions(1); + plotter.region(0).plot(ratioPlot); + + // Format the axis labels. + PlotterRegion region = (PlotterRegion) plotter.region(0); + region.getPlot().setTitle("Invariant Mass Ratio (" + runNumber[numerator] + " / " + runNumber[denominator] + ")"); + region.getPlot().getXAxis().setLabel("Invariant Mass (GeV)"); + region.getPlot().getXAxis().setMin(0.010); + region.getPlot().getXAxis().setMax(0.060); + region.getPlot().getYAxis().setLabel("Ratio"); + + // Format the fonts and general plot presentation. + setDefault1DStyle(region, new ColorStyle[] { ColorStyle.GREY }); + + // Disable the error bars. + JASHist1DHistogramStyle fillStyle = (JASHist1DHistogramStyle) region.getDataForName(region.getAllDataNames()[0]).getStyle(); + fillStyle.setShowErrorBars(false); + + // Show the plot. + plotter.setParameter("plotterWidth", "2000"); + plotter.setParameter("plotterHeight", "1200"); + plotter.show(); + + // Close the trees. + for(int i = 0; i < plotFile.length; i++) { + tree[i].close(); + } + } + + /** + * Sets the plot display formatting for 1D plots. + * @param region - The plotter region to format. + * @param color - The data color settings to use. + */ + private static final void setDefault1DStyle(PlotterRegion region, ColorStyle[] color) { + // Get the names of each plot on in the region. + String[] dataNames = region.getAllDataNames(); + + // Check whether this is an overlay plot. Overlay plots contain + // more than one data name. + boolean overlay = (dataNames.length > 1 ? true : false); + + // Iterate over each plot in the region. + for(int i = 0; i < dataNames.length; i++) { + // Set the overlay style if needed. + if(overlay) { + // Get the fill style for the current data type. + JASHist1DHistogramStyle fillStyle = (JASHist1DHistogramStyle) region.getDataForName(dataNames[i]).getStyle(); + + // Set the histogram style to display thick-lined bars + // with no fill. The color is set by the "color" argument. + fillStyle.setHistogramFill(false); + fillStyle.setHistogramBarLineWidth(3); + fillStyle.setHistogramBarLineColor(color[i].getFillColor()); + + // Set the legend text style. + region.getPlot().getLegend().setFont(new Font("Calibri", Font.PLAIN, 20)); + } + + // Otherwise, set the fill style for a single plot. + else { + // Get the fill style for the current data type. + JASHist1DHistogramStyle fillStyle = (JASHist1DHistogramStyle) region.getDataForName(dataNames[i]).getStyle(); + + // Set the histogram style to display thick-lined bars + // with a fill color. The colors are defined by the + // "color" argument. + fillStyle.setHistogramBarLineWidth(3); + fillStyle.setHistogramBarColor(color[i].getFillColor()); + fillStyle.setHistogramBarLineColor(color[i].getLineColor()); + } + + // Set the statistics box style. + region.getPlot().getStats().setVisible(true); + region.getPlot().getStats().setFont(BASIC_FONT); + + // Set the title font. + region.getPlot().getTitleObject().setFont(TITLE_FONT); + + // Set the axis tick-mark fonts. + region.getPlot().getXAxis().setFont(BASIC_FONT); + region.getPlot().getYAxis().setFont(BASIC_FONT); + region.getPlot().getXAxis().getLabelObject().setFont(AXIS_FONT); + region.getPlot().getYAxis().getLabelObject().setFont(AXIS_FONT); + } + } + + /** + * Sets the plot display formatting for 1D plots. + * @param region - The plotter region to format. + */ + private static final void setDefault2DStyle(PlotterRegion region, boolean logarithmic) { + // Get the fill style object. 2D plots should never be overlay + // plots, so there should only ever be one data name. + JASHist2DHistogramStyle fillStyle = (JASHist2DHistogramStyle) region.getDataForName(region.getAllDataNames()[0]).getStyle(); + + // Set the fill style for a two-dimensional plot. + if(logarithmic) { fillStyle.setLogZ(true); } + fillStyle.setHistStyle(JASHist2DHistogramStyle.STYLE_COLORMAP); + fillStyle.setColorMapScheme(JASHist2DHistogramStyle.COLORMAP_RAINBOW); + + // Make the statistics box invisible. + region.getPlot().getStats().setVisible(false); + + // Set the general plot font (which is also the z-axis font). + region.getPlot().setFont(BASIC_FONT); + + // Set the title font. + region.getPlot().getTitleObject().setFont(TITLE_FONT); + + // Set the axis tick-mark fonts. + region.getPlot().getXAxis().setFont(BASIC_FONT); + region.getPlot().getYAxis().setFont(BASIC_FONT); + region.getPlot().getXAxis().getLabelObject().setFont(AXIS_FONT); + region.getPlot().getYAxis().getLabelObject().setFont(AXIS_FONT); + } +} Added: java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/MTEPlotFormatter.java ============================================================================= --- java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/MTEPlotFormatter.java (added) +++ java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/MTEPlotFormatter.java Tue Sep 22 12:11:46 2015 @@ -0,0 +1,326 @@ +package org.hps.users.kmccarty.plots.formatter; + +import jas.hist.JASHist1DHistogramStyle; +import jas.hist.JASHist2DHistogramStyle; + +import java.awt.Color; +import java.awt.Font; +import java.io.IOException; +import java.util.List; + +import org.lcsim.util.aida.AIDA; + +import hep.aida.IAnalysisFactory; +import hep.aida.IHistogram1D; +import hep.aida.IHistogram2D; +import hep.aida.IPlotter; +import hep.aida.IPlotterFactory; +import hep.aida.ITree; +import hep.aida.ref.plotter.PlotterRegion; + +public class MTEPlotFormatter { + // Define plot fonts. + private static final Font BASIC_FONT = new Font("Calibri", Font.PLAIN, 20); + private static final Font AXIS_FONT = new Font("Calibri", Font.BOLD, 25); + private static final Font TITLE_FONT = new Font("Calibri", Font.BOLD, 35); + + // Defines the color style options for plot data. + private enum ColorStyle { + MS_BLUE(new Color( 79, 129, 189), new Color( 36, 64, 97)), MS_ORANGE(new Color(247, 150, 70), new Color(152, 72, 6)), + MS_RED(new Color(192, 80, 77), new Color( 99, 36, 35)), GREY(new Color(166, 166, 166), new Color( 89, 89, 89)), + MS_GREEN(new Color(155, 187, 89), new Color( 79, 98, 40)), CRIMSON(new Color(161, 0, 0), new Color(104, 0, 0)), + RUST(new Color(161, 80, 0), new Color(105, 80, 0)), YELLOW(new Color(161, 161, 0), new Color(122, 109, 8)), + FOREST(new Color( 65, 102, 0), new Color( 37, 79, 0)), GREEN(new Color( 7, 132, 70), new Color( 7, 82, 30)), + TEAL(new Color( 0, 130, 130), new Color( 0, 90, 100)), CERULEAN(new Color( 0, 86, 130), new Color( 0, 28, 83)), + BLUE(new Color( 0, 33, 203), new Color( 0, 0, 137)), INDIGO(new Color( 68, 10, 127), new Color( 0, 0, 61)), + PURPLE(new Color(106, 0, 106), new Color( 63, 0, 56)), FUSCHIA(new Color(119, 0, 60), new Color( 60, 0, 60)); + + private final Color fillColor; + private final Color lineColor; + + private ColorStyle(Color fillColor, Color lineColor) { + this.fillColor = fillColor; + this.lineColor = lineColor; + } + + public Color getFillColor() { return fillColor; } + + public Color getLineColor() { return lineColor; } + }; + + /** + * Loads all plots in a file and formats them according to the + * indicated style. + * @param args - Unused default executable parameter. + * @throws IOException Occurs if there is an issue opening the file. + */ + public static void main(String[] args) throws IOException { + // Define the root directory for the plots. + String rootDir = "D:\\cygwin64\\home\\Kyle\\"; + + // Define the new name of the file containing the trigger plots. + String plotFile = rootDir + "temp.aida"; + + // Define the scaling factors for each plot. + double scaleFactor = 1; + + // Get the plots file and open it. + IAnalysisFactory af = IAnalysisFactory.create(); + ITree tree = af.createTreeFactory().create(plotFile); + if(tree == null) { throw new IllegalArgumentException("Unable to load plot file."); } + + // Define index references for each event type. + int MOLLER = 0; + int TRIDENT = 1; + int ELASTIC = 2; + + // Get the histograms. + IHistogram1D[] trackCountPlots = new IHistogram1D[3]; + trackCountPlots[MOLLER] = (IHistogram1D) tree.find("MTE Analysis/Møller Event Tracks"); + trackCountPlots[TRIDENT] = (IHistogram1D) tree.find("MTE Analysis/Trident Event Tracks"); + trackCountPlots[ELASTIC] = (IHistogram1D) tree.find("MTE Analysis/Elastic Event Tracks"); + + IHistogram1D[] energyPlots = new IHistogram1D[3]; + energyPlots[MOLLER] = (IHistogram1D) tree.find("MTE Analysis/Møller Electron Energy Distribution"); + energyPlots[TRIDENT] = (IHistogram1D) tree.find("MTE Analysis/Trident Electron Energy Distribution"); + energyPlots[ELASTIC] = (IHistogram1D) tree.find("MTE Analysis/Elastic Energy Distribution"); + + IHistogram1D[] energySumPlots = new IHistogram1D[2]; + energySumPlots[MOLLER] = (IHistogram1D) tree.find("MTE Analysis/Møller Energy Sum Distribution"); + energySumPlots[TRIDENT] = (IHistogram1D) tree.find("MTE Analysis/Trident Energy Sum Distribution"); + + IHistogram2D[] energy2DPlots = new IHistogram2D[2]; + energy2DPlots[MOLLER] = (IHistogram2D) tree.find("MTE Analysis/Møller 2D Energy Distribution"); + energy2DPlots[TRIDENT] = (IHistogram2D) tree.find("MTE Analysis/Trident 2D Energy Distribution"); + + // Create a plotter factory. + IPlotterFactory plotterFactory = af.createPlotterFactory(); + + // Format the track count plots. + for(IHistogram1D trackCountPlot : trackCountPlots) { + // Create a plotter and plotting region for the plot. + IPlotter plotter = plotterFactory.create(trackCountPlot.title()); + plotter.createRegions(1); + plotter.region(0).plot(trackCountPlot); + + // Format the axis labels. + PlotterRegion region = (PlotterRegion) plotter.region(0); + region.getPlot().setTitle(trackCountPlot.title()); + region.getPlot().getXAxis().setLabel("Number of Tracks"); + region.getPlot().getYAxis().setLabel("Count"); + + // Format the fonts and general plot presentation. + setDefault1DStyle(region, new ColorStyle[] { ColorStyle.GREY }); + + // Show the plot. + plotter.setParameter("plotterWidth", "2000"); + plotter.setParameter("plotterHeight", "1200"); + plotter.show(); + } + + // Format the electron energy plots. + for(IHistogram1D energyPlot : energyPlots) { + // Create a plotter and plotting region for the plot. + IPlotter plotter = plotterFactory.create(energyPlot.title()); + plotter.createRegions(1); + plotter.region(0).plot(energyPlot); + + // Format the axis labels. + PlotterRegion region = (PlotterRegion) plotter.region(0); + region.getPlot().setTitle(energyPlot.title()); + region.getPlot().getXAxis().setLabel("Track Energy (GeV)"); + region.getPlot().getYAxis().setLabel("Count"); + + // Format the fonts and general plot presentation. + setDefault1DStyle(region, new ColorStyle[] { ColorStyle.GREY }); + + // Show the plot. + plotter.setParameter("plotterWidth", "2000"); + plotter.setParameter("plotterHeight", "1200"); + plotter.show(); + } + + // Format the energy sum plots. + for(IHistogram1D energySumPlot : energySumPlots) { + // Create a plotter and plotting region for the plot. + IPlotter plotter = plotterFactory.create(energySumPlot.title()); + plotter.createRegions(1); + plotter.region(0).plot(energySumPlot); + + // Format the axis labels. + PlotterRegion region = (PlotterRegion) plotter.region(0); + region.getPlot().setTitle(energySumPlot.title()); + region.getPlot().getXAxis().setLabel("Track Energy (GeV)"); + region.getPlot().getYAxis().setLabel("Count"); + + // Format the fonts and general plot presentation. + setDefault1DStyle(region, new ColorStyle[] { ColorStyle.GREY }); + + // Show the plot. + plotter.setParameter("plotterWidth", "2000"); + plotter.setParameter("plotterHeight", "1200"); + plotter.show(); + } + + // Format the 2D energy sum plots. + for(IHistogram2D energy2DPlot : energy2DPlots) { + // Create a plotter and plotting region for the plot. + IPlotter plotter = plotterFactory.create(energy2DPlot.title()); + plotter.createRegions(1); + plotter.region(0).plot(energy2DPlot); + + // Format the axis labels. + PlotterRegion region = (PlotterRegion) plotter.region(0); + region.getPlot().setTitle(energy2DPlot.title()); + region.getPlot().getXAxis().setLabel("First Track Energy (GeV)"); + region.getPlot().getYAxis().setLabel("Second Track Energy (GeV)"); + + + // Format the fonts and general plot presentation. + setDefault2DStyle(region, false); + + // Show the plot. + plotter.setParameter("plotterWidth", "2000"); + plotter.setParameter("plotterHeight", "1200"); + plotter.show(); + } + + // Disable the error bars. + //JASHist1DHistogramStyle fillStyle = (JASHist1DHistogramStyle) region.getDataForName(region.getAllDataNames()[0]).getStyle(); + //fillStyle.setShowErrorBars(false); + + // Close the tree. + tree.close(); + } + + /** + * Sets the plot display formatting for 1D plots. + * @param region - The plotter region to format. + * @param color - The data color settings to use. + */ + private static final void setDefault1DStyle(PlotterRegion region, ColorStyle[] color) { + // Get the names of each plot on in the region. + String[] dataNames = region.getAllDataNames(); + + // Check whether this is an overlay plot. Overlay plots contain + // more than one data name. + boolean overlay = (dataNames.length > 1 ? true : false); + + // Iterate over each plot in the region. + for(int i = 0; i < dataNames.length; i++) { + // Set the overlay style if needed. + if(overlay) { + // Get the fill style for the current data type. + JASHist1DHistogramStyle fillStyle = (JASHist1DHistogramStyle) region.getDataForName(dataNames[i]).getStyle(); + + // Set the histogram style to display thick-lined bars + // with no fill. The color is set by the "color" argument. + fillStyle.setHistogramFill(false); + fillStyle.setHistogramBarLineWidth(3); + fillStyle.setHistogramBarLineColor(color[i].getFillColor()); + + // Set the legend text style. + region.getPlot().getLegend().setFont(new Font("Calibri", Font.PLAIN, 20)); + } + + // Otherwise, set the fill style for a single plot. + else { + // Get the fill style for the current data type. + JASHist1DHistogramStyle fillStyle = (JASHist1DHistogramStyle) region.getDataForName(dataNames[i]).getStyle(); + + // Set the histogram style to display thick-lined bars + // with a fill color. The colors are defined by the + // "color" argument. + fillStyle.setHistogramBarLineWidth(3); + fillStyle.setHistogramBarColor(color[i].getFillColor()); + fillStyle.setHistogramBarLineColor(color[i].getLineColor()); + } + + // Set the statistics box style. + region.getPlot().getStats().setVisible(true); + region.getPlot().getStats().setFont(BASIC_FONT); + + // Set the title font. + region.getPlot().getTitleObject().setFont(TITLE_FONT); + + // Set the axis tick-mark fonts. + region.getPlot().getXAxis().setFont(BASIC_FONT); + region.getPlot().getYAxis().setFont(BASIC_FONT); + region.getPlot().getXAxis().getLabelObject().setFont(AXIS_FONT); + region.getPlot().getYAxis().getLabelObject().setFont(AXIS_FONT); + } + } + + /** + * Sets the plot display formatting for 1D plots. + * @param region - The plotter region to format. + */ + private static final void setDefault2DStyle(PlotterRegion region, boolean logarithmic) { + // Get the fill style object. 2D plots should never be overlay + // plots, so there should only ever be one data name. + JASHist2DHistogramStyle fillStyle = (JASHist2DHistogramStyle) region.getDataForName(region.getAllDataNames()[0]).getStyle(); + + // Set the fill style for a two-dimensional plot. + if(logarithmic) { fillStyle.setLogZ(true); } + fillStyle.setHistStyle(JASHist2DHistogramStyle.STYLE_COLORMAP); + fillStyle.setColorMapScheme(JASHist2DHistogramStyle.COLORMAP_RAINBOW); + + // Make the statistics box invisible. + region.getPlot().getStats().setVisible(false); + + // Set the general plot font (which is also the z-axis font). + region.getPlot().setFont(BASIC_FONT); + + // Set the title font. + region.getPlot().getTitleObject().setFont(TITLE_FONT); + + // Set the axis tick-mark fonts. + region.getPlot().getXAxis().setFont(BASIC_FONT); + region.getPlot().getYAxis().setFont(BASIC_FONT); + region.getPlot().getXAxis().getLabelObject().setFont(AXIS_FONT); + region.getPlot().getYAxis().getLabelObject().setFont(AXIS_FONT); + } + + /** + * Recursive method that gets all object names from a tree that + * are not directories. Method should not be called directly, but + * rather called only through the <code>getHistograms(ITree)</code> + * method. + * @param tree - The tree from which to obtain the object names. + * @param directory - The directory in which to search for objects. + * @param list - The list in which to place the objects. + * @return Returns the <code>List</code> collection that was given + * as an argument. + */ + private static final List<String> getHistograms(ITree tree, String directory, List<String> list) { + // Get the list of objects in the directory. + String[] treeObjects = tree.listObjectNames(directory); + + // Print the objects. + for(String objectName : treeObjects) { + // Check if the object is a directory. + boolean isDirectory = isDirectory(objectName); + + // If the object is a directory, get the histograms from it. + if(isDirectory) { + getHistograms(tree, objectName, list); + } + + // If the object is a plot, add it to the list. + else { list.add(objectName); } + } + + // Return the list. + return list; + } + + /** + * Checks whether a tree object is a directory. + * @param object - The object to check. + * @return Returns <code>true</code> if the object is a directory + * and <code>false</code> otherwise. + */ + private static final boolean isDirectory(String object) { + return (object.toCharArray()[object.length() - 1] == '/'); + } +} Added: java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/MTETriggerPlotsFormatter.java ============================================================================= --- java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/MTETriggerPlotsFormatter.java (added) +++ java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/MTETriggerPlotsFormatter.java Tue Sep 22 12:11:46 2015 @@ -0,0 +1,178 @@ +package org.hps.users.kmccarty.plots.formatter; + +import java.io.IOException; + +import org.hps.users.kmccarty.plots.FormattedPlot1D; +import org.hps.users.kmccarty.plots.FormattedPlot2D; +import org.hps.users.kmccarty.plots.PlotFormatModule; +import org.hps.users.kmccarty.plots.PlotsFormatter; +import org.hps.users.kmccarty.plots.PlotsFormatter.ColorStyle; + +import hep.aida.IAnalysisFactory; +import hep.aida.IHistogram1D; +import hep.aida.IHistogram2D; +import hep.aida.ITree; + + +public class MTETriggerPlotsFormatter { + public static void main(String[] args) throws IllegalArgumentException, IOException { + // Define the root directory for the plots. + String rootDir = "D:\\cygwin64\\home\\Kyle\\"; + + // Define the new name of the file containing the trigger plots. + String plotFile = rootDir + "5772-ana.aida"; + + // Get the plots file and open it. + IAnalysisFactory af = IAnalysisFactory.create(); + ITree tree = af.createTreeFactory().create(plotFile); + if(tree == null) { throw new IllegalArgumentException("Unable to load plot file."); } + + // Define the 1D trigger plot names for Møllers and tridents. + String[] plotNames1D = { "Cluster Hit Count", "Cluster Seed Energy", "Cluster Total Energy", + "Pair Coplanarity", "Pair Energy Difference", "Pair Energy Slope", "Pair Energy Sum" }; + String[] displayNames1D = { "Cluster Hit Count", "Cluster Seed Energy", "Cluster Total Energy", + "Pair Coplanarity", "Pair Energy Difference", "Pair Energy Slope", "Pair Energy Sum" }; + String[] xAxisNames1D = { "Hit Count", "Seed Energy (GeV)", "Total Energy (GeV)", + "Coplanarity (Degrees)", "Energy Difference (GeV)", "Energy Slope (GeV)", "Energy Sum (GeV)" }; + String yAxisName1D = "Count"; + + // Define the 2D trigger plot names for Møllers and tridents. + String[] plotNames2D = { "Cluster Seed", "Pair Energy Sum 2D" }; + String[] displayNames2D = { "Cluster Seed Distribution", "2D Energy Sum" }; + String[] xAxisNames2D = { "x-Index", "Second Cluster Energy (GeV)" }; + String[] yAxisNames2D = { "y-Index", "First Cluster Energy (GeV)" }; + + // Define the 1D trigger plot names for elastics. + String[] plotNamesElastic1D = { "Cluster Hit Count", "Cluster Seed Energy", "Cluster Total Energy" }; + String[] displayNamesElastic1D = { "Cluster Hit Count", "Cluster Seed Energy", "Cluster Total Energy" }; + String[] xAxisNamesElastic1D = { "Hit Count", "Seed Energy (GeV)", "Total Energy (GeV)" }; + String yAxisNameElastic1D = "Count"; + + // Define the 2D trigger plot names for elastics. + String[] plotNamesElastic2D = { "Cluster Seed" }; + String[] displayNamesElastic2D = { "Cluster Seed Distribution" }; + String[] xAxisNamesElastic2D = { "x-Index" }; + String[] yAxisNamesElastic2D = { "y-Index" }; + + // Define the Møller, trident, and elastic prefixes. + String allPrefix = "All Trigger Plots/Pair Plots/"; + String møllerPrefix = "Møller Trigger Plots/Pair Plots/"; + String tridentPrefix = "Trident Trigger Plots/Pair Plots/"; + String elasticPrefix = "Elastic Trigger Plots/Singles Plots/"; + String allSinglesPrefix = "All Trigger Plots/Singles Plots/"; + + // Define the plot type prefix. + String allTypeName = "All Pairs - "; + String møllerTypeName = "Møller - "; + String tridentTypeName = "Trident - "; + String elasticTypeName = "Elastic - "; + String allSinglesTypeName = "All Singles - "; + + // Define the plot type colors. + ColorStyle allColor = PlotsFormatter.ColorStyle.GREY; + ColorStyle møllerColor = PlotsFormatter.ColorStyle.MS_BLUE; + ColorStyle tridentColor = PlotsFormatter.ColorStyle.MS_ORANGE; + ColorStyle elasticColor = PlotsFormatter.ColorStyle.MS_GREEN; + + // Create a plot formatting module. + PlotFormatModule module = new PlotFormatModule(); + + // Get the histograms and add them to the module. Start with the + // trident and Møller plots. + for(int i = 0; i < plotNames1D.length; i++) { + // Get the Møller and trident plots. + IHistogram1D allPlot = (IHistogram1D) tree.find(allPrefix + plotNames1D[i]); + IHistogram1D møllerPlot = (IHistogram1D) tree.find(møllerPrefix + plotNames1D[i]); + IHistogram1D tridentPlot = (IHistogram1D) tree.find(tridentPrefix + plotNames1D[i]); + + // Make a formatted plot for each. + FormattedPlot1D allFormattedPlot = new FormattedPlot1D(allPlot, allColor, xAxisNames1D[i], yAxisName1D, allTypeName + displayNames1D[i]); + FormattedPlot1D møllerFormattedPlot = new FormattedPlot1D(møllerPlot, møllerColor, xAxisNames1D[i], yAxisName1D, møllerTypeName + displayNames1D[i]); + FormattedPlot1D tridentFormattedPlot = new FormattedPlot1D(tridentPlot, tridentColor, xAxisNames1D[i], yAxisName1D, tridentTypeName + displayNames1D[i]); + + // Add them to the module. + module.addPlot1D(allFormattedPlot); + module.addPlot1D(møllerFormattedPlot); + module.addPlot1D(tridentFormattedPlot); + } + for(int i = 0; i < plotNames2D.length; i++) { + // Get the Møller and trident plots. + IHistogram2D allPlot = (IHistogram2D) tree.find(allPrefix + plotNames2D[i]); + IHistogram2D møllerPlot = (IHistogram2D) tree.find(møllerPrefix + plotNames2D[i]); + IHistogram2D tridentPlot = (IHistogram2D) tree.find(tridentPrefix + plotNames2D[i]); + + // Make a formatted plot for each. + FormattedPlot2D allFormattedPlot = new FormattedPlot2D(allPlot, i == 0 ? true : false, xAxisNames2D[i], yAxisNames2D[i], allTypeName + displayNames2D[i]); + FormattedPlot2D møllerFormattedPlot = new FormattedPlot2D(møllerPlot, i == 0 ? true : false, xAxisNames2D[i], yAxisNames2D[i], møllerTypeName + displayNames2D[i]); + FormattedPlot2D tridentFormattedPlot = new FormattedPlot2D(tridentPlot, i == 0 ? true : false, xAxisNames2D[i], yAxisNames2D[i], tridentTypeName + displayNames2D[i]); + + // Add them to the module. + module.addPlot2D(allFormattedPlot); + module.addPlot2D(møllerFormattedPlot); + module.addPlot2D(tridentFormattedPlot); + } + + // Get the histograms for the elastic plots and add them to the module. + for(int i = 0; i < plotNamesElastic1D.length; i++) { + // Get the Møller and trident plots. + IHistogram1D allPlot = (IHistogram1D) tree.find(allSinglesPrefix + plotNames1D[i]); + IHistogram1D elasticPlot = (IHistogram1D) tree.find(elasticPrefix + plotNames1D[i]); + + // Make a formatted plot for each. + FormattedPlot1D allFormattedPlot = new FormattedPlot1D(allPlot, allColor, xAxisNamesElastic1D[i], yAxisNameElastic1D, + allSinglesTypeName + displayNamesElastic1D[i]); + FormattedPlot1D elasticFormattedPlot = new FormattedPlot1D(elasticPlot, elasticColor, xAxisNamesElastic1D[i], yAxisNameElastic1D, + elasticTypeName + displayNamesElastic1D[i]); + + // Add them to the module. + module.addPlot1D(allFormattedPlot); + module.addPlot1D(elasticFormattedPlot); + } + for(int i = 0; i < plotNamesElastic2D.length; i++) { + // Get the Møller and trident plots. + IHistogram2D allPlot = (IHistogram2D) tree.find(allPrefix + plotNamesElastic2D[i]); + IHistogram2D elasticPlot = (IHistogram2D) tree.find(møllerPrefix + plotNamesElastic2D[i]); + + // Make a formatted plot for each. + FormattedPlot2D allFormattedPlot = new FormattedPlot2D(allPlot, i == 0 ? true : false, xAxisNamesElastic2D[i], yAxisNamesElastic2D[i], + allSinglesTypeName + plotNames2D[i]); + FormattedPlot2D elasticFormattedPlot = new FormattedPlot2D(elasticPlot, i == 0 ? true : false, xAxisNamesElastic2D[i], yAxisNamesElastic2D[i], + elasticTypeName + displayNamesElastic2D[i]); + + // Add them to the module. + module.addPlot2D(allFormattedPlot); + module.addPlot2D(elasticFormattedPlot); + } + + // Add the MTE plots to the module. + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MTE Analysis/Elastic Energy Distribution"), elasticColor, + "Momentum (GeV)", "Count", "Elastic - Momentum")); + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MTE Analysis/Elastic Event Tracks"), elasticColor, + "Tracks", "Count", "Elastic - Tracks in Event")); + + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MTE Analysis/Møller Energy Sum Distribution"), møllerColor, + "Momentum Sum (GeV)", "Count", "Møller - Momentum Sum")); + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MTE Analysis/Møller Electron Energy Distribution"), møllerColor, + "Momentum (GeV)", "Count", "Møller - Momentum (Electron)")); + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MTE Analysis/Møller Time Coincidence Distribution (All Møller Cuts)"), møllerColor, + "Time (ns)", "Count", "Møller - Time Coincidence")); + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MTE Analysis/Møller Event Tracks"), møllerColor, + "Tracks", "Count", "Møller - Tracks in Event")); + module.addPlot2D(new FormattedPlot2D((IHistogram2D) tree.find("MTE Analysis/Møller 2D Energy Distribution"), false, + "First Track Momentum (GeV)", "Second Track Momentum (GeV)", "Møller - 2D Momentum Sum")); + + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MTE Analysis/Trident Energy Sum Distribution"), tridentColor, + "Momentum Sum (GeV)", "Count", "Trident - Momentum Sum")); + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MTE Analysis/Trident Electron Energy Distribution"), tridentColor, + "Momentum (GeV)", "Count", "Trident - Momentum (Electron)")); + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MTE Analysis/Trident Positron Energy Distribution"), tridentColor, + "Momentum (GeV)", "Count", "Trident - Momentum (Positron)")); + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MTE Analysis/Trident Event Tracks"), tridentColor, + "Tracks", "Count", "Trident - Tracks in Event")); + module.addPlot2D(new FormattedPlot2D((IHistogram2D) tree.find("MTE Analysis/Trident 2D Energy Distribution"), false, + "First Track Momentum (GeV)", "Second Track Momentum (GeV)", "Trident - 2D Momentum Sum")); + + // Display the plots. + module.savePlots("C:\\Users\\Kyle\\Desktop\\EnergyShift\\TestPrint\\"); + } +} Added: java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/ParticleMCAnalysisPlotsFormatter.java ============================================================================= --- java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/ParticleMCAnalysisPlotsFormatter.java (added) +++ java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/ParticleMCAnalysisPlotsFormatter.java Tue Sep 22 12:11:46 2015 @@ -0,0 +1,55 @@ +package org.hps.users.kmccarty.plots.formatter; + +import java.io.IOException; + +import hep.aida.IAnalysisFactory; +import hep.aida.IHistogram1D; +import hep.aida.IHistogram2D; +import hep.aida.ITree; + +import org.hps.users.kmccarty.plots.FormattedPlot1D; +import org.hps.users.kmccarty.plots.FormattedPlot2D; +import org.hps.users.kmccarty.plots.PlotFormatModule; +import org.hps.users.kmccarty.plots.PlotsFormatter.ColorStyle; + +public class ParticleMCAnalysisPlotsFormatter { + public static void main(String[] args) throws IllegalArgumentException, IOException { + // Define the root directory for the plots. + String rootDir = "D:\\cygwin64\\home\\Kyle\\"; + + // Define the new name of the file containing the trigger plots. + String plotFile = rootDir + "moller-mc-out_triggerPlots.aida"; + + // Get the plots file and open it. + IAnalysisFactory af = IAnalysisFactory.create(); + ITree tree = af.createTreeFactory().create(plotFile); + if(tree == null) { throw new IllegalArgumentException("Unable to load plot file."); } + + // Create a plot formatting module. + PlotFormatModule module = new PlotFormatModule(); + + // Define the plot color. + ColorStyle plotColor = ColorStyle.MS_BLUE; + + // Define the plots to be read. + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MC Analysis/Electron Energy Distribution"), + plotColor, "Electron Energy (GeV)", "Count", "Electron Energy Distribution")); + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MC Analysis/Electron\\Electron Momentum Sum Distribution"), + plotColor, "Momentum Sum (GeV)", "Count", "Momentum Sum Distribution")); + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MC Analysis/Electron\\Electron Pair Angle Distribution"), + plotColor, "Momentum Sum (GeV)", "Count", "Pair Angle Distribution")); + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MC Analysis/Particle x-Momentum Distribution"), + plotColor, "Momentum (GeV)", "Count", "Particle x-Momentum Distribution")); + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MC Analysis/Particle y-Momentum Distribution"), + plotColor, "Momentum (GeV)", "Count", "Particle y-Momentum Distribution")); + module.addPlot1D(new FormattedPlot1D((IHistogram1D) tree.find("MC Analysis/Particle z-Momentum Distribution"), + plotColor, "Momentum (GeV)", "Count", "Particle z-Momentum Distribution")); + module.addPlot2D(new FormattedPlot2D((IHistogram2D) tree.find("MC Analysis/Electron\\Electron 2D Momentum Distribution"), + true, "Particle 1 Momentum (GeV)", "Particle 2 Momentum (GeV)", "2D Momentum Sum Distribution")); + module.addPlot2D(new FormattedPlot2D((IHistogram2D) tree.find("MC Analysis/Particle Momentum Distribution"), + true, "px (GeV)", "py (GeV)", "Particle x/y Momentum Distribution")); + + // Display the plots. + module.displayPlots(); + } +} Added: java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/SingleTriggerPlotsFormatter.java ============================================================================= --- java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/SingleTriggerPlotsFormatter.java (added) +++ java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/SingleTriggerPlotsFormatter.java Tue Sep 22 12:11:46 2015 @@ -0,0 +1,163 @@ +package org.hps.users.kmccarty.plots.formatter; + +import java.io.IOException; + +import org.hps.users.kmccarty.plots.FormattedPlot1D; +import org.hps.users.kmccarty.plots.FormattedPlot2D; +import org.hps.users.kmccarty.plots.PlotFormatModule; +import org.hps.users.kmccarty.plots.PlotsFormatter; + +import hep.aida.IAnalysisFactory; +import hep.aida.IHistogram1D; +import hep.aida.IHistogram2D; +import hep.aida.ITree; + +public class SingleTriggerPlotsFormatter { + + public static void main(String[] args) throws IllegalArgumentException, IOException { + // Define the root directory for the plots. + String rootDir = "D:\\cygwin64\\home\\Kyle\\"; + + // Define the new name of the file containing the trigger plots. + String plotFile = rootDir + "trident-readout-full.aida"; + + // Get the plots file and open it. + IAnalysisFactory af = IAnalysisFactory.create(); + ITree tree = af.createTreeFactory().create(plotFile); + if(tree == null) { throw new IllegalArgumentException("Unable to load plot file."); } + + // Define plots variables. + int UNCUT = 0; + int TRIGGERED = 1; + String[] plotsDir = { "NoCuts/", "PassedAll/" }; + int PLOT_HIT_COUNT = 0; + int PLOT_SEED_ENERGY = 1; + int PLOT_CLUSTER_ENERGY = 2; + int PLOT_COPLANARITY = 3; + int PLOT_ENERGY_SUM = 4; + int PLOT_ENERGY_DIFF = 5; + int PLOT_ENERGY_SLOPE = 6; + int PLOT_SEED_DIST = 0; + int PLOT_ENERGY_SUM_2D = 1; + + // Define the internal plot names. + String[] plotNameInternal1D = new String[7]; + String[] plotNameInternal2D = new String[2]; + plotNameInternal1D[PLOT_HIT_COUNT] = "Cluster Hit Count"; + plotNameInternal1D[PLOT_SEED_ENERGY] = "Cluster Seed Energy"; + plotNameInternal1D[PLOT_CLUSTER_ENERGY] = "Cluster Total Energy"; + plotNameInternal1D[PLOT_COPLANARITY] = "Pair Coplanarity"; + plotNameInternal1D[PLOT_ENERGY_SUM] = "Pair Energy Sum"; + plotNameInternal1D[PLOT_ENERGY_DIFF] = "Pair Energy Difference"; + plotNameInternal1D[PLOT_ENERGY_SLOPE] = "Pair Energy Slope"; + plotNameInternal2D[PLOT_SEED_DIST] = "Cluster Seed"; + plotNameInternal2D[PLOT_ENERGY_SUM_2D] = "Pair Energy Sum 2D"; + + // Define the plot display names. + String[] plotName1D = new String[7]; + String[] plotName2D = new String[2]; + for(int j = 0; j < plotNameInternal1D.length; j++) { + plotName1D[j] = plotNameInternal1D[j]; + } + for(int j = 0; j < plotNameInternal2D.length; j++) { + plotName2D[j] = plotNameInternal2D[j]; + } + plotName1D[PLOT_ENERGY_SUM] = "1D Pair Energy Sum"; + plotName2D[PLOT_SEED_DIST] = "Cluster Seed Distribution"; + plotName2D[PLOT_ENERGY_SUM_2D] = "2D Pair Energy Sum"; + + String[] xTitles1D = new String[plotName1D.length]; + String[] xTitles2D = new String[plotName2D.length]; + xTitles1D[PLOT_HIT_COUNT] = "Hit Count"; + xTitles1D[PLOT_SEED_ENERGY] = "Seed Energy (GeV)"; + xTitles1D[PLOT_CLUSTER_ENERGY] = "Cluster Energy (GeV)"; + xTitles1D[PLOT_COPLANARITY] = "Coplanarity Angle (Degrees)"; + xTitles1D[PLOT_ENERGY_SUM] = "Energy Sum (GeV)"; + xTitles1D[PLOT_ENERGY_DIFF] = "Energy Difference (GeV)"; + xTitles1D[PLOT_ENERGY_SLOPE] = "Energy Slope (GeV)"; + xTitles2D[PLOT_SEED_DIST] = "x-Index"; + xTitles2D[PLOT_ENERGY_SUM_2D] = "First Cluster Energy (GeV)"; + String yTitle1D = "Count"; + String[] yTitles2D = new String[plotName2D.length]; + yTitles2D[PLOT_SEED_DIST] = "y-Index"; + yTitles2D[PLOT_ENERGY_SUM_2D] = "Second Cluster Energy (GeV)"; + + // Define axis ranges. + double[] axisRanges1D = new double[plotName1D.length]; + axisRanges1D[PLOT_HIT_COUNT] = -1; + axisRanges1D[PLOT_SEED_ENERGY] = 1.1; + axisRanges1D[PLOT_CLUSTER_ENERGY] = 1.1; + axisRanges1D[PLOT_COPLANARITY] = 180; + axisRanges1D[PLOT_ENERGY_SUM] = 2.2; + axisRanges1D[PLOT_ENERGY_DIFF] = 1.1; + axisRanges1D[PLOT_ENERGY_SLOPE] = 2.4; + double[] xAxisRanges2D = new double[plotName2D.length]; + double[] yAxisRanges2D = new double[plotName2D.length]; + xAxisRanges2D[PLOT_SEED_DIST] = -1; + xAxisRanges2D[PLOT_ENERGY_SUM_2D] = 1.1; + yAxisRanges2D[PLOT_SEED_DIST] = -1; + yAxisRanges2D[PLOT_ENERGY_SUM_2D] = 1.1; + + // Define the plot names. + String[][] plotLocations1D = new String[plotsDir.length][plotNameInternal1D.length]; + String[][] plotLocations2D = new String[plotsDir.length][plotNameInternal2D.length]; + for(int i = 0; i < plotsDir.length; i++) { + for(int j = 0; j < plotNameInternal1D.length; j++) { + plotLocations1D[i][j] = plotsDir[i] + plotNameInternal1D[j]; + } + } + for(int i = 0; i < plotsDir.length; i++) { + for(int j = 0; j < plotNameInternal2D.length; j++) { + plotLocations2D[i][j] = plotsDir[i] + plotNameInternal2D[j]; + } + } + + // Create a plot formatting module. + PlotFormatModule module = new PlotFormatModule(); + + // Load the plot objects. + for(int i = 0; i < plotName1D.length; i++) { + // Get the uncut and triggered plots. + IHistogram1D uncutPlot = (IHistogram1D) tree.find(plotLocations1D[UNCUT][i]); + IHistogram1D triggeredPlot = (IHistogram1D) tree.find(plotLocations1D[TRIGGERED][i] + " (Passed All Cuts)"); + + // Make a formatted plot for each. + FormattedPlot1D uncutFormattedPlot; + FormattedPlot1D triggeredFormattedPlot; + if(axisRanges1D[i] != -1) { + uncutFormattedPlot = new FormattedPlot1D(uncutPlot, PlotsFormatter.ColorStyle.GREY, xTitles1D[i], yTitle1D, plotName1D[i] + " (No Cuts)", axisRanges1D[i]); + triggeredFormattedPlot = new FormattedPlot1D(triggeredPlot, PlotsFormatter.ColorStyle.MS_GREEN, xTitles1D[i], yTitle1D, plotName1D[i] + " (Triggered)", axisRanges1D[i]); + } else { + uncutFormattedPlot = new FormattedPlot1D(uncutPlot, PlotsFormatter.ColorStyle.GREY, xTitles1D[i], yTitle1D, plotName1D[i] + " (No Cuts)"); + triggeredFormattedPlot = new FormattedPlot1D(triggeredPlot, PlotsFormatter.ColorStyle.MS_GREEN, xTitles1D[i], yTitle1D, plotName1D[i] + " (Triggered)"); + } + + // Add the plots to the module. + module.addPlot1D(uncutFormattedPlot); + module.addPlot1D(triggeredFormattedPlot); + } + for(int i = 0; i < plotName2D.length; i++) { + // Get the uncut and triggered plots. + IHistogram2D uncutPlot = (IHistogram2D) tree.find(plotLocations2D[UNCUT][i]); + IHistogram2D triggeredPlot = (IHistogram2D) tree.find(plotLocations2D[TRIGGERED][i] + " (Passed All Cuts)"); + + // Make a formatted plot for each. + FormattedPlot2D uncutFormattedPlot; + FormattedPlot2D triggeredFormattedPlot; + if(xAxisRanges2D[i] != -1) { + uncutFormattedPlot = new FormattedPlot2D(uncutPlot, true, xTitles2D[i], yTitles2D[i], plotName2D[i] + " (No Cuts)", xAxisRanges2D[i], yAxisRanges2D[i]); + triggeredFormattedPlot = new FormattedPlot2D(triggeredPlot, true, xTitles2D[i], yTitles2D[i], plotName2D[i] + " (Triggered)", xAxisRanges2D[i], yAxisRanges2D[i]); + } else { + uncutFormattedPlot = new FormattedPlot2D(uncutPlot, true, xTitles2D[i], yTitles2D[i], plotName2D[i] + " (No Cuts)"); + triggeredFormattedPlot = new FormattedPlot2D(triggeredPlot, true, xTitles2D[i], yTitles2D[i], plotName2D[i] + " (Triggered)"); + } + + // Add the plots to the module. + module.addPlot2D(uncutFormattedPlot); + module.addPlot2D(triggeredFormattedPlot); + } + + // Save the plots. + module.savePlots("C:\\Users\\Kyle\\Desktop\\EnergyShift\\MonteCarlo\\Trident\\Trigger\\"); + } +} Added: java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/TridentTrackFormatter.java ============================================================================= --- java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/TridentTrackFormatter.java (added) +++ java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/TridentTrackFormatter.java Tue Sep 22 12:11:46 2015 @@ -0,0 +1,202 @@ +package org.hps.users.kmccarty.plots.formatter; + +import java.io.IOException; + +import org.hps.users.kmccarty.plots.PlotsFormatter; +import org.hps.users.kmccarty.plots.PlotsFormatter.ColorStyle; +import org.lcsim.util.aida.AIDA; + +import hep.aida.IAnalysisFactory; +import hep.aida.IHistogram; +import hep.aida.IHistogram1D; +import hep.aida.IHistogram2D; +import hep.aida.IPlotter; +import hep.aida.IPlotterFactory; +import hep.aida.ITree; +import hep.aida.ref.plotter.PlotterRegion; + +public class TridentTrackFormatter { + /** + * Loads all plots in a file and formats them according to the + * indicated style. + * @param args - Unused default executable parameter. + * @throws IOException Occurs if there is an issue opening the file. + */ + public static void main(String[] args) throws IOException { + // Define the root directory for the plots. + String rootDir = "D:\\cygwin64\\home\\Kyle\\tmp\\"; + + // Define the new name of the file containing the trigger plots. + String plotFile = rootDir + "trident-out.aida"; + + // Get the plots file and open it. + IAnalysisFactory af = IAnalysisFactory.create(); + ITree tree = af.createTreeFactory().create(plotFile); + if(tree == null) { throw new IllegalArgumentException("Unable to load plot file."); } + + // Declare the histogram names. + String trackName = "Tracks in Event (All)"; + String posTrackName = "Tracks in Event (Positive)"; + String negTrackName = "Tracks in Event (Negative)"; + String posMomentumName = "Momentum (Positive)"; + String negMomentumName = "Momentum (Negative)"; + String energySumName = "Energy Sum"; + String momentumSumName = "Momentum Sum"; + String energyMomentumDiffName = "Energy-Momentum Difference"; + String invariantMassName = "Invariant Mass"; + String energySum2DName = "2D Energy Sum"; + String momentumSum2DName = "2D Momentum Sum"; + String positionName = "Track Cluster Position"; + + // Get the histograms. + IHistogram1D[] tracks = { + (IHistogram1D) tree.find("Trident Analysis/All/" + trackName), + (IHistogram1D) tree.find("Trident Analysis/Cluster/" + trackName) + }; + IHistogram1D[] posTracks = { + (IHistogram1D) tree.find("Trident Analysis/All/" + posTrackName), + (IHistogram1D) tree.find("Trident Analysis/Cluster/" + posTrackName) + }; + IHistogram1D[] negTracks = { + (IHistogram1D) tree.find("Trident Analysis/All/" + negTrackName), + (IHistogram1D) tree.find("Trident Analysis/Cluster/" + negTrackName) + }; + IHistogram1D[] posMomentum = { + (IHistogram1D) tree.find("Trident Analysis/All/" + posMomentumName), + (IHistogram1D) tree.find("Trident Analysis/Cluster/" + posMomentumName) + }; + IHistogram1D[] negMomentum = { + (IHistogram1D) tree.find("Trident Analysis/All/" + negMomentumName), + (IHistogram1D) tree.find("Trident Analysis/Cluster/" + negMomentumName) + }; + IHistogram1D[] energySum = { + (IHistogram1D) tree.find("Trident Analysis/All/" + energySumName), + (IHistogram1D) tree.find("Trident Analysis/Cluster/" + energySumName) + }; + IHistogram1D[] momentumSum = { + (IHistogram1D) tree.find("Trident Analysis/All/" + momentumSumName), + (IHistogram1D) tree.find("Trident Analysis/Cluster/" + momentumSumName) + }; + IHistogram1D[] energyMomentumDiff = { + (IHistogram1D) tree.find("Trident Analysis/All/" + energyMomentumDiffName), + (IHistogram1D) tree.find("Trident Analysis/Cluster/" + energyMomentumDiffName) + }; + IHistogram1D[] invariantMass = { + (IHistogram1D) tree.find("Trident Analysis/All/" + invariantMassName), + (IHistogram1D) tree.find("Trident Analysis/Cluster/" + invariantMassName) + }; + IHistogram2D[] energySum2D = { + (IHistogram2D) tree.find("Trident Analysis/All/" + energySum2DName), + (IHistogram2D) tree.find("Trident Analysis/Cluster/" + energySum2DName) + }; + IHistogram2D[] momentumSum2D = { + (IHistogram2D) tree.find("Trident Analysis/All/" + momentumSum2DName), + (IHistogram2D) tree.find("Trident Analysis/Cluster/" + momentumSum2DName) + }; + IHistogram2D[] position = { + (IHistogram2D) tree.find("Trident Analysis/All/" + positionName), + (IHistogram2D) tree.find("Trident Analysis/Cluster/" + positionName) + }; + + // Re-bin the histograms to have 5-times larger bins. First, + // get the bin count and upper and lower bounds of the plot. + int bins = invariantMass[0].axis().bins(); + double low = invariantMass[0].axis().binLowerEdge(0); + double high = invariantMass[0].axis().binUpperEdge(invariantMass[0].axis().bins() - 1); + + // Create new plots with the larger bin sizes. + AIDA aida = AIDA.defaultInstance(); + IHistogram1D[] newPlot = new IHistogram1D[2]; + newPlot[0] = aida.histogram1D(invariantMassName, bins / 5, low, high); + newPlot[1] = aida.histogram1D("Cluster " + invariantMassName, bins / 5, low, high); + + // Populate the new plots with the data from the old ones. + for(int j = 0; j < 2; j++) { + for(int i = 0; i < bins; i++) { + int entries = invariantMass[j].binEntries(i); + double center = invariantMass[j].axis().binCenter(i); + for(int k = 0; k < entries; k++) { + newPlot[j].fill(center); + } + } + } + + // Replace the old plots. + invariantMass = newPlot; + + // Define the scaling factors for each plot. + double scaleFactor = 1; + + // Define the plot titles and arrays for 1D plots. + IHistogram[][] plots = { tracks, posTracks, negTracks, posMomentum, negMomentum, energySum, momentumSum, energyMomentumDiff, invariantMass }; + String[] titles = { trackName, posTrackName, negTrackName, posMomentumName, negMomentumName, energySumName, momentumSumName, + energyMomentumDiffName, invariantMassName }; + String[] xTitles = { "Tracks", "Tracks", "Tracks", "Momentum (GeV)", "Momentum (GeV)", "Energy Sum (GeV)", "Momentum Sum (GeV)", + "|E_Cluster - P_Track| (GeV)", "Invariant Mass (GeV)" }; + String yTitle = "Count"; + + // Define the plot titles and arrays for 2D plots. + IHistogram2D[][] plots2D = { energySum2D, momentumSum2D, position }; + String[] titles2D = { energySum2DName, momentumSum2DName, positionName }; + String[] xTitles2D = { "Positive Cluster Energy", "Positive Track Momentum", "x-Index" }; + String[] yTitles2D = { "Negative Cluster Energy", "Negative Track Momentum", "y-Index" }; + String zTitle2D = "Count"; + + // Create a plotter factory. + IPlotterFactory plotterFactory = af.createPlotterFactory(); + + // Format and display the basic histograms. + for(int i = 0; i < plots.length; i++) { + for(int j = 0; j < 2; j++) { + // Scale the histogram by the appropriate scaling factor. + plots[i][j].scale(1.0 / scaleFactor); + + // Create a plotter and plotting region for the plot. + IPlotter plotter = plotterFactory.create((j == 1 ? "Cluster " : "") + titles[i]); + plotter.createRegions(1); + plotter.region(0).plot(plots[i][j]); + + // Format the axis labels. + PlotterRegion region = (PlotterRegion) plotter.region(0); + region.getPlot().setTitle((j == 1 ? "Cluster " : "") + titles[i]); + region.getPlot().getXAxis().setLabel(xTitles[i]); + region.getPlot().getYAxis().setLabel(yTitle); + + // Format the fonts and general plot presentation. + PlotsFormatter.setDefault1DStyle(region, new ColorStyle[] { ColorStyle.GREY }); + + // Show the plot. + plotter.setParameter("plotterWidth", "2000"); + plotter.setParameter("plotterHeight", "1200"); + plotter.show(); + } + } + + // Format and display the 2D histogram. + for(int i = 0; i < plots2D.length; i++) { + for(int j = 0; j < 2; j++) { + plots2D[i][j].scale(1.0 / scaleFactor); + IPlotter plotter2D = plotterFactory.create((j == 1 ? "Cluster " : "") + titles2D[i]); + plotter2D.createRegions(1); + plotter2D.region(0).plot(plots2D[i][j]); + + // Format the axis labels. + PlotterRegion region2D = (PlotterRegion) plotter2D.region(0); + region2D.getPlot().setTitle((j == 1 ? "Cluster " : "") + titles2D[i]); + region2D.getPlot().getXAxis().setLabel(xTitles2D[i]); + region2D.getPlot().getYAxis().setLabel(yTitles2D[i]); + + // Format the fonts and general plot presentation. + PlotsFormatter.setDefault2DStyle(region2D, true); + + // Show the plot. + plotter2D.setParameter("plotterWidth", "2000"); + plotter2D.setParameter("plotterHeight", "1200"); + plotter2D.show(); + } + } + + // Close the tree. + tree.close(); + } +} Added: java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/TriggerPlotsFormat.java ============================================================================= --- java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/TriggerPlotsFormat.java (added) +++ java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/formatter/TriggerPlotsFormat.java Tue Sep 22 12:11:46 2015 @@ -0,0 +1,345 @@ +package org.hps.users.kmccarty.plots.formatter; + +import jas.hist.JASHist1DHistogramStyle; +import jas.hist.JASHist2DHistogramStyle; + +import java.awt.Color; +import java.awt.Font; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import hep.aida.IAnalysisFactory; +import hep.aida.IBaseHistogram; +import hep.aida.IHistogram1D; +import hep.aida.IHistogram2D; +import hep.aida.IManagedObject; +import hep.aida.IPlotter; +import hep.aida.IPlotterFactory; +import hep.aida.ITree; +import hep.aida.ref.plotter.PlotterRegion; + +public class TriggerPlotsFormat { + // Define plot fonts. + private static final Font BASIC_FONT = new Font("Calibri", Font.PLAIN, 20); + private static final Font AXIS_FONT = new Font("Calibri", Font.BOLD, 25); + private static final Font TITLE_FONT = new Font("Calibri", Font.BOLD, 35); + + // Defines the color style options for plot data. + private enum ColorStyle { + MS_BLUE(new Color( 79, 129, 189), new Color( 36, 64, 97)), MS_ORANGE(new Color(247, 150, 70), new Color(152, 72, 6)), + MS_RED(new Color(192, 80, 77), new Color( 99, 36, 35)), GREY(new Color(166, 166, 166), new Color( 89, 89, 89)), + MS_GREEN(new Color(155, 187, 89), new Color( 79, 98, 40)), CRIMSON(new Color(161, 0, 0), new Color(104, 0, 0)), + RUST(new Color(161, 80, 0), new Color(105, 80, 0)), YELLOW(new Color(161, 161, 0), new Color(122, 109, 8)), + FOREST(new Color( 65, 102, 0), new Color( 37, 79, 0)), GREEN(new Color( 7, 132, 70), new Color( 7, 82, 30)), + TEAL(new Color( 0, 130, 130), new Color( 0, 90, 100)), CERULEAN(new Color( 0, 86, 130), new Color( 0, 28, 83)), + BLUE(new Color( 0, 33, 203), new Color( 0, 0, 137)), INDIGO(new Color( 68, 10, 127), new Color( 0, 0, 61)), + PURPLE(new Color(106, 0, 106), new Color( 63, 0, 56)), FUSCHIA(new Color(119, 0, 60), new Color( 60, 0, 60)); + + private final Color fillColor; + private final Color lineColor; + + private ColorStyle(Color fillColor, Color lineColor) { + this.fillColor = fillColor; + this.lineColor = lineColor; + } + + public Color getFillColor() { return fillColor; } + + public Color getLineColor() { return lineColor; } + }; + + /** + * Loads all plots in a file and formats them according to the + * indicated style. + * @param args - Unused default executable parameter. + * @throws IOException Occurs if there is an issue opening the file. + */ + public static void main(String[] args) throws IOException { + // Define the root directory for the plots. + String rootDir = "D:\\cygwin64\\home\\Kyle\\"; //plots\\no-cuts\\2-hit\\"; + + // Define the new name of the file containing the trigger plots. + String[] plotFile = { + rootDir + "5568-ana.aida" + //rootDir + "background-ana_triggerPlots.aida" + //rootDir + "15MeV-ana_triggerPlots.aida", + //rootDir + "30MeV-ana_triggerPlots.aida", + //rootDir + "40MeV-ana_triggerPlots.aida", + //rootDir + "50MeV-ana_triggerPlots.aida" + }; + + // Define the names of each plot. This will be used for the + // legend in the case of multiple plots. + String[] treeName = { + "Background", + "15 MeV A'", + "30 MeV A'", + "40 MeV A'", + "50 MeV A'" + }; + + // Define the color style for the plots. + ColorStyle[] dataColorStyle = { + ColorStyle.GREY, + ColorStyle.MS_GREEN, + ColorStyle.MS_BLUE, + ColorStyle.MS_ORANGE, + ColorStyle.MS_RED + }; + + // Get the plots file and open it. + IAnalysisFactory af = IAnalysisFactory.create(); + ITree[] tree = new ITree[plotFile.length]; + for(int i = 0; i < plotFile.length; i++) { + tree[i] = af.createTreeFactory().create(plotFile[i]); + if(tree[i] == null) { throw new IllegalArgumentException("Unable to load plot file."); } + } + + // Get a list of all the histograms in the file. + List<List<String>> treeHistograms = new ArrayList<List<String>>(plotFile.length); + for(int i = 0; i < plotFile.length; i++) { + treeHistograms.add(getHistograms(tree[i]));//, "/PassedAll/")); + } + + // Create a plotter factory. + IPlotterFactory plotterFactory = af.createPlotterFactory(); + + // Plot each histogram and format it. + for(String histogram : treeHistograms.get(0)) { + // Get the plot from the tree and verify that it is a 1D + // or 2D histogram. Other types are not supported. + IManagedObject histObject = tree[0].find(histogram); + if(!(histObject instanceof IHistogram1D) && !(histObject instanceof IHistogram2D)) { + continue; + } + + // Obtain the histogram object. + IBaseHistogram hist; + if(histObject instanceof IHistogram1D) { hist = (IHistogram1D) histObject; } + else { hist = (IHistogram2D) histObject; } + + // Define whether this is an overlay plot and whether + // this is a one or two dimensional plot. + boolean overlay = plotFile.length > 1; + boolean twoDimensional = hist instanceof IHistogram2D; + + // Generate the plotter and set its title. The plotter will + // use the title of the first tree's plot. + String plotTitle = hist.title(); + IPlotter plotter = plotterFactory.create(plotTitle); + + // For single plots and one-dimensional overlay plots, + // there should only be a single plotter region. + if(!twoDimensional || !overlay) { plotter.createRegions(1); } + + // For two-dimensional overlay plots, create a region for + // each plot individually. + else { plotter.createRegions(2, (int) Math.ceil(plotFile.length / 2.0)); } + + // Find the histogram in each of the trees and plot them + // all on the same region. + for(int i = 0; i < plotFile.length; i++) { + // Get the histogram from the tree. + IManagedObject treeObject = tree[i].find(histogram); + IBaseHistogram treeHist; + if(treeObject instanceof IHistogram1D) { treeHist = (IHistogram1D) treeObject; } + else { treeHist = (IHistogram2D) treeObject; } + + // Display the plot. + if(treeHist != null) { + // Set the title of plot to the name associated with + // its tree. This ensures that the correct name will + // appear on the legend. + if(plotFile.length > 1) { + treeHist.setTitle(treeName[i]); + } + + // Plot the tree's data in the plotter region. + if(!twoDimensional || !overlay) { plotter.region(0).plot(treeHist); } + else { + plotter.region(i).plot(treeHist); + setDefault2DStyle(((PlotterRegion) plotter.region(i)), dataColorStyle); + } + } + } + + // Format the plot region. + if(!twoDimensional) { setDefault1DStyle(((PlotterRegion) plotter.region(0)), dataColorStyle); } + else { setDefault2DStyle(((PlotterRegion) plotter.region(0)), dataColorStyle); } + + // Show the plotter. + plotter.region(0).setTitle(plotTitle); + //plotter.setParameter("plotterWidth", "1600"); + //plotter.setParameter("plotterHeight", "1550"); + plotter.setParameter("plotterWidth", "2000"); + plotter.setParameter("plotterHeight", "1200"); + plotter.show(); + } + + // Close the trees. + for(int i = 0; i < plotFile.length; i++) { + tree[i].close(); + } + } + + /** + * Sets the plot display formatting for 1D plots. + * @param region - The plotter region to format. + * @param color - The data color settings to use. + */ + private static final void setDefault1DStyle(PlotterRegion region, ColorStyle[] color) { + // Get the names of each plot on in the region. + String[] dataNames = region.getAllDataNames(); + + // Check whether this is an overlay plot. Overlay plots contain + // more than one data name. + boolean overlay = (dataNames.length > 1 ? true : false); + + // Iterate over each plot in the region. + for(int i = 0; i < dataNames.length; i++) { + // Set the overlay style if needed. + if(overlay) { + // Get the fill style for the current data type. + JASHist1DHistogramStyle fillStyle = (JASHist1DHistogramStyle) region.getDataForName(dataNames[i]).getStyle(); + + // Set the histogram style to display thick-lined bars + // with no fill. The color is set by the "color" argument. + fillStyle.setHistogramFill(false); + fillStyle.setHistogramBarLineWidth(3); + fillStyle.setHistogramBarLineColor(color[i].getFillColor()); + + // Set the legend text style. + region.getPlot().getLegend().setFont(new Font("Calibri", Font.PLAIN, 20)); + } + + // Otherwise, set the fill style for a single plot. + else { + // Get the fill style for the current data type. + JASHist1DHistogramStyle fillStyle = (JASHist1DHistogramStyle) region.getDataForName(dataNames[i]).getStyle(); + + // Set the histogram style to display thick-lined bars + // with a fill color. The colors are defined by the + // "color" argument. + fillStyle.setHistogramBarLineWidth(3); + fillStyle.setHistogramBarColor(color[i].getFillColor()); + fillStyle.setHistogramBarLineColor(color[i].getLineColor()); + } + + // Set the statistics box style. + region.getPlot().getStats().setVisible(true); + region.getPlot().getStats().setFont(BASIC_FONT); + + // Set the title font. + region.getPlot().getTitleObject().setFont(TITLE_FONT); + + // Set generic axis titles. + region.getPlot().getXAxis().setLabel("Data Label (Unit)"); + region.getPlot().getYAxis().setLabel("Count"); + + // Set the axis tick-mark fonts. + region.getPlot().getXAxis().setFont(BASIC_FONT); + region.getPlot().getYAxis().setFont(BASIC_FONT); + region.getPlot().getXAxis().getLabelObject().setFont(AXIS_FONT); + region.getPlot().getYAxis().getLabelObject().setFont(AXIS_FONT); + } + } + + /** + * Sets the plot display formatting for 1D plots. + * @param region - The plotter region to format. + * @param color - The data color settings to use. + */ + private static final void setDefault2DStyle(PlotterRegion region, ColorStyle[] color) { + // Get the fill style object. 2D plots should never be overlay + // plots, so there should only ever be one data name. + JASHist2DHistogramStyle fillStyle = (JASHist2DHistogramStyle) region.getDataForName(region.getAllDataNames()[0]).getStyle(); + + // Set the fill style for a two-dimensional plot. + fillStyle.setLogZ(true); + fillStyle.setHistStyle(JASHist2DHistogramStyle.STYLE_COLORMAP); + fillStyle.setColorMapScheme(JASHist2DHistogramStyle.COLORMAP_RAINBOW); + + // Make the statistics box invisible. + region.getPlot().getStats().setVisible(false); + + // Set the general plot font (which is also the z-axis font). + region.getPlot().setFont(BASIC_FONT); + + // Set the title font. + region.getPlot().getTitleObject().setFont(TITLE_FONT); + + // Set generic axis titles. + region.getPlot().getXAxis().setLabel("Data Label (Unit)"); + region.getPlot().getYAxis().setLabel("Data Label (Unit)"); + + // Set the axis tick-mark fonts. + region.getPlot().getXAxis().setFont(BASIC_FONT); + region.getPlot().getYAxis().setFont(BASIC_FONT); + region.getPlot().getXAxis().getLabelObject().setFont(AXIS_FONT); + region.getPlot().getYAxis().getLabelObject().setFont(AXIS_FONT); + } + + /** + * Gets a list of all objects that are not directories in a tree. + * @param tree - The tree from which to extract the object names. + * @return Returns the object names as <code>String</code> objects + * in a <code>List</code> collection. + */ + private static final List<String> getHistograms(ITree tree) { + return getHistograms(tree, "/"); + } + + /** + * Gets a list of all objects that are not directories in a tree. + * @param tree - The tree from which to extract the object names. + * @return Returns the object names as <code>String</code> objects + * in a <code>List</code> collection. + */ + private static final List<String> getHistograms(ITree tree, String rootDir) { + return getHistograms(tree, rootDir, new ArrayList<String>()); + } + + /** + * Recursive method that gets all object names from a tree that + * are not directories. Method should not be called directly, but + * rather called only through the <code>getHistograms(ITree)</code> + * method. + * @param tree - The tree from which to obtain the object names. + * @param directory - The directory in which to search for objects. + * @param list - The list in which to place the objects. + * @return Returns the <code>List</code> collection that was given + * as an argument. + */ + private static final List<String> getHistograms(ITree tree, String directory, List<String> list) { + // Get the list of objects in the directory. + String[] treeObjects = tree.listObjectNames(directory); + + // Print the objects. + for(String objectName : treeObjects) { + // Check if the object is a directory. + boolean isDirectory = isDirectory(objectName); + + // If the object is a directory, get the histograms from it. + if(isDirectory) { + getHistograms(tree, objectName, list); + } + + // If the object is a plot, add it to the list. + else { list.add(objectName); } + } + + // Return the list. + return list; + } + + /** + * Checks whether a tree object is a directory. + * @param object - The object to check. + * @return Returns <code>true</code> if the object is a directory + * and <code>false</code> otherwise. + */ + private static final boolean isDirectory(String object) { + return (object.toCharArray()[object.length() - 1] == '/'); + } +}