Print

Print


Author: [log in to unmask]
Date: Sat Sep 26 15:22:04 2015
New Revision: 3719

Log:
Updated plot formatting module to allow for exporting AIDA plots to text format. Also changed the scaling on some MTE analysis plots.

Modified:
    java/trunk/users/src/main/java/org/hps/users/kmccarty/MTEAnalysis.java
    java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/PlotFormatModule.java

Modified: java/trunk/users/src/main/java/org/hps/users/kmccarty/MTEAnalysis.java
 =============================================================================
--- java/trunk/users/src/main/java/org/hps/users/kmccarty/MTEAnalysis.java	(original)
+++ java/trunk/users/src/main/java/org/hps/users/kmccarty/MTEAnalysis.java	Sat Sep 26 15:22:04 2015
@@ -27,7 +27,6 @@
 	// Define track LCIO information.
 	private String bankCollectionName = "TriggerBank";
 	private String particleCollectionName = "FinalStateParticles";
-	private boolean useGoodSVT = true;
 	private static final AIDA aida = AIDA.defaultInstance();
 	private IHistogram1D[] chargedTracksPlot = {
 			aida.histogram1D("MTE Analysis/Møller Event Tracks", 10, -0.5, 9.5),
@@ -64,14 +63,14 @@
 	private IHistogram1D trTimeCoincidenceFiducial     = aida.histogram1D("Trident/Time Coincidence (Fiducial Region)", 150, 0.0, 15.0);
 	private IHistogram1D trEnergySumAll                = aida.histogram1D("Trident/Energy Sum",                         220, 0.0,  1.1);
 	private IHistogram1D trEnergySumFiducial           = aida.histogram1D("Trident/Energy Sum (Fiducial Region)",       220, 0.0,  1.1);
-	private IHistogram2D trEnergySum2DAll              = aida.histogram2D("Trident/Top Cluster Energy vs. Bottom Cluster Energy",                   220, 0, 1.1, 220, 0, 1.1);
-	private IHistogram2D trEnergySum2DFiducial         = aida.histogram2D("Trident/Top Cluster Energy vs. Bottom Cluster Energy (Fiducial Region)", 220, 0, 1.1, 220, 0, 1.1);
-	private IHistogram2D trSumCoplanarityAll           = aida.histogram2D("Trident/Hardware Coplanarity vs. Energy Sum",                            220, 0, 1.1, 115, 0, 230);
-	private IHistogram2D trSumCoplanarityFiducial      = aida.histogram2D("Trident/Hardware Coplanarity vs. Energy Sum (Fiducial Region)",          220, 0, 1.1, 115, 0, 230);
-	private IHistogram2D trSumCoplanarityCalcAll       = aida.histogram2D("Trident/Calculated Coplanarity vs. Energy Sum",                          220, 0, 1.1, 115, 0, 230);
-	private IHistogram2D trSumCoplanarityCalcFiducial  = aida.histogram2D("Trident/Calculated Coplanarity vs. Energy Sum (Fiducial Region)",        220, 0, 1.1, 115, 0, 230);
-	private IHistogram2D trTimeEnergyAll               = aida.histogram2D("Trident/Cluster Time vs. Cluster Energy",                                220, 0, 1.1, 100, 0, 100);
-	private IHistogram2D trTimeEnergyFiducial          = aida.histogram2D("Trident/Cluster Time vs. Cluster Energy (Fiducial Region)",              220, 0, 1.1, 100, 0, 100);
+	private IHistogram2D trEnergySum2DAll              = aida.histogram2D("Trident/Top Cluster Energy vs. Bottom Cluster Energy",                   220, 0, 1.1, 220,   0, 1.1);
+	private IHistogram2D trEnergySum2DFiducial         = aida.histogram2D("Trident/Top Cluster Energy vs. Bottom Cluster Energy (Fiducial Region)", 220, 0, 1.1, 220,   0, 1.1);
+	private IHistogram2D trSumCoplanarityAll           = aida.histogram2D("Trident/Hardware Coplanarity vs. Energy Sum",                            220, 0, 1.1, 115,   0, 180);
+	private IHistogram2D trSumCoplanarityFiducial      = aida.histogram2D("Trident/Hardware Coplanarity vs. Energy Sum (Fiducial Region)",          220, 0, 1.1, 115,   0, 180);
+	private IHistogram2D trSumCoplanarityCalcAll       = aida.histogram2D("Trident/Calculated Coplanarity vs. Energy Sum",                          220, 0, 1.1, 115, 130, 230);
+	private IHistogram2D trSumCoplanarityCalcFiducial  = aida.histogram2D("Trident/Calculated Coplanarity vs. Energy Sum (Fiducial Region)",        220, 0, 1.1, 115, 130, 230);
+	private IHistogram2D trTimeEnergyAll               = aida.histogram2D("Trident/Cluster Time vs. Cluster Energy",                                220, 0, 1.1, 100,   0, 100);
+	private IHistogram2D trTimeEnergyFiducial          = aida.histogram2D("Trident/Cluster Time vs. Cluster Energy (Fiducial Region)",              220, 0, 1.1, 100,   0, 100);
 	
 	private TriggerPlotsModule allPlots = new TriggerPlotsModule("All");
 	private TriggerPlotsModule møllerPlots = new TriggerPlotsModule("Møller");
@@ -158,19 +157,6 @@
 	
 	@Override
 	public void process(EventHeader event) {
-		// Check whether the SVT was active in this event.
-		final String[] flagNames = { "svt_bias_good", "svt_burstmode_noise_good", "svt_position_good" };
-		boolean svtGood = true;
-        for(int i = 0; i < flagNames.length; i++) {
-            int[] flag = event.getIntegerParameters().get(flagNames[i]);
-            if(flag == null || flag[0] == 0) {
-                svtGood = false;
-            }
-        }
-		
-        // If the SVT is not properly running, skip the event.
-        if(!svtGood && useGoodSVT) { return; }
-		
 		if(event.hasCollection(ReconstructedParticle.class, particleCollectionName)) {
 			// Get the list of tracks.
 			List<ReconstructedParticle> trackList = event.get(ReconstructedParticle.class, particleCollectionName);
@@ -194,7 +180,7 @@
 			// Populate the all cluster plots.
 			List<Cluster> topClusters = new ArrayList<Cluster>();
 			List<Cluster> botClusters = new ArrayList<Cluster>();
-			List<Cluster> clusters = event.get(Cluster.class, "EcalClustersCorr");
+			List<Cluster> clusters = event.get(Cluster.class, "EcalClusters");
 			for(Cluster cluster : clusters) {
 				allPlots.addCluster(cluster);
 				if(cluster.getCalorimeterHits().get(0).getIdentifierFieldValue("iy") > 0) { topClusters.add(cluster); }
@@ -523,10 +509,6 @@
 		excludeNoTrackEvents = state;
 	}
 	
-	public void setUseGoodSVT(boolean state) {
-		useGoodSVT = state;
-	}
-	
 	private static final boolean inFiducialRegion(Cluster cluster) {
 		// Get the x and y indices for the cluster.
 		int ix   = TriggerModule.getClusterXIndex(cluster);

Modified: java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/PlotFormatModule.java
 =============================================================================
--- java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/PlotFormatModule.java	(original)
+++ java/trunk/users/src/main/java/org/hps/users/kmccarty/plots/PlotFormatModule.java	Sat Sep 26 15:22:04 2015
@@ -9,7 +9,9 @@
 
 import org.hps.users.kmccarty.plots.PlotsFormatter.ColorStyle;
 
+import java.io.BufferedWriter;
 import java.io.File;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
@@ -73,42 +75,6 @@
 			
 			// Format the fonts and general plot presentation.
 			PlotsFormatter.setDefault1DStyle(region, new ColorStyle[] { formattedPlot.getColorStyle() });
-			
-			// Set the plotter dimensions.
-			plotter.setParameter("plotterWidth", width);
-			plotter.setParameter("plotterHeight", height);
-			
-			// If the file path is null, display the plots. Otherwise,
-			// save them to the destination folder.
-			if(filePath == null) { plotter.show(); }
-			else { plotter.writeToFile(filePath + formattedPlot.getPlotName() + ".png"); }
-		}
-		
-		// Format and display the 2D plots.
-		for(FormattedPlot2D formattedPlot : formattedPlots2D) {
-			// Get the plot.
-			IHistogram2D plot = formattedPlot.getPlot();
-			
-			// Create a plotter and plotting region for the plot.
-			IPlotter plotter = plotterFactory.create(formattedPlot.getPlotName());
-			plotter.createRegions(1);
-			plotter.region(0).plot(plot);
-			
-			// Set the axis range.
-			PlotterRegion region = (PlotterRegion) plotter.region(0);
-			if(formattedPlot.definesXAxisRange()) {
-				region.getPlot().getXAxis().setMax(formattedPlot.getXAxisRange());
-			} if(formattedPlot.definesYAxisRange()) {
-				region.getPlot().getYAxis().setMax(formattedPlot.getYAxisRange());
-			}
-			
-			// Format the axis labels.
-			region.getPlot().setTitle(formattedPlot.getPlotName());
-			region.getPlot().getXAxis().setLabel(formattedPlot.getXAxisName());
-			region.getPlot().getYAxis().setLabel(formattedPlot.getYAxisName());
-			
-			// Format the fonts and general plot presentation.
-			PlotsFormatter.setDefault2DStyle(region, formattedPlot.isLogarithmic());
 			
 			// Set the plotter dimensions.
 			plotter.setParameter("plotterWidth", width);
@@ -121,7 +87,119 @@
 				File plotFile = new File(filePath + formattedPlot.getPlotName() + ".png");
 				if(plotFile.exists()) { plotFile.delete(); }
 				plotter.writeToFile(filePath + formattedPlot.getPlotName() + ".png");
-			}
-		}
+				System.out.printf("Saved plot \"%s\" to path: %s%n", formattedPlot.getPlotName(), filePath + formattedPlot.getPlotName() + ".png");
+			}
+		}
+		
+		// Format and display the 2D plots.
+		for(FormattedPlot2D formattedPlot : formattedPlots2D) {
+			// Get the plot.
+			IHistogram2D plot = formattedPlot.getPlot();
+			
+			// Create a plotter and plotting region for the plot.
+			IPlotter plotter = plotterFactory.create(formattedPlot.getPlotName());
+			plotter.createRegions(1);
+			plotter.region(0).plot(plot);
+			
+			// Set the axis range.
+			PlotterRegion region = (PlotterRegion) plotter.region(0);
+			if(formattedPlot.definesXAxisRange()) {
+				region.getPlot().getXAxis().setMax(formattedPlot.getXAxisRange());
+			} if(formattedPlot.definesYAxisRange()) {
+				region.getPlot().getYAxis().setMax(formattedPlot.getYAxisRange());
+			}
+			
+			// Format the axis labels.
+			region.getPlot().setTitle(formattedPlot.getPlotName());
+			region.getPlot().getXAxis().setLabel(formattedPlot.getXAxisName());
+			region.getPlot().getYAxis().setLabel(formattedPlot.getYAxisName());
+			
+			// Format the fonts and general plot presentation.
+			PlotsFormatter.setDefault2DStyle(region, formattedPlot.isLogarithmic());
+			
+			// Set the plotter dimensions.
+			plotter.setParameter("plotterWidth", width);
+			plotter.setParameter("plotterHeight", height);
+			
+			// If the file path is null, display the plots. Otherwise,
+			// save them to the destination folder.
+			if(filePath == null) { plotter.show(); }
+			else {
+				File plotFile = new File(filePath + formattedPlot.getPlotName() + ".png");
+				if(plotFile.exists()) { plotFile.delete(); }
+				plotter.writeToFile(filePath + formattedPlot.getPlotName() + ".png");
+				System.out.printf("Saved plot \"%s\" to path: %s%n", formattedPlot.getPlotName(), filePath + formattedPlot.getPlotName() + ".png");
+			}
+		}
+	}
+	
+	public void exportPlots(String filePath) throws IOException {
+		// Export the 1D plots in a text format.
+		for(FormattedPlot1D plot : formattedPlots1D) {
+			exportPlot(filePath, plot);
+		}
+		
+		// Export the 2D plots in a text format.
+		for(FormattedPlot2D plot : formattedPlots2D) {
+			exportPlot(filePath, plot);
+		}
+	}
+	
+	private static final void exportPlot(String filePath, FormattedPlot plot) throws IOException {
+		// Check if this is a one or two dimensional plot.
+		boolean is1D = plot instanceof FormattedPlot1D;
+		
+		// Create a file object for the plot.
+		String plotPath = filePath + plot.getPlotName() + (is1D ? ".aida1D" : ".aida2D");
+		File datFile = new File(plotPath);
+		
+		// If the plot file already exists, delete it.
+		if(datFile.exists()) { datFile.delete(); }
+		
+		// Create a new file for the plot to occupy.
+		datFile.createNewFile();
+		
+		// Get the textual form of the plot.
+		String plotText = null;
+		if(is1D) { plotText = toTextFormat(((FormattedPlot1D) plot).getPlot()); }
+		else { plotText = toTextFormat(((FormattedPlot2D) plot).getPlot()); }
+		
+		// Write the plot text to the file.
+		BufferedWriter writer = new BufferedWriter(new FileWriter(datFile));
+		writer.write(plotText);
+		writer.close();
+		
+		// Note that the file was written.
+		System.out.printf("Plot \"%s\" was exported to path: %s%n", plot.getPlotName(), plotPath);
+	}
+	
+	private static final String toTextFormat(IHistogram1D plot) {
+		// Create a buffer to hold the converted plot.
+		StringBuffer buffer = new StringBuffer();
+		
+		// Iterate over the bins and output the plot in the format of
+		// "[BIN_MEAN] [BIN_VALUE]" with a tab delimiter.
+		for(int bin = 0; bin < plot.axis().bins(); bin++) {
+			buffer.append(String.format("%f\t%f%n", plot.binMean(bin), plot.binHeight(bin)));
+		}
+		
+		// Return the converted file.
+		return buffer.toString();
+	}
+	
+	private static final String toTextFormat(IHistogram2D plot) {
+		// Create a buffer to hold the converted plot.
+		StringBuffer buffer = new StringBuffer();
+		
+		// Iterate over the bins and output the plot in the format of
+		// "[X_BIN_MEAN] [Y_BIN_MEAN] [BIN_VALUE]" with a tab delimiter.
+		for(int xBin = 0; xBin < plot.xAxis().bins(); xBin++) {
+			for(int yBin = 0; yBin < plot.yAxis().bins(); yBin++) {
+				buffer.append(String.format("%f\t%f\t%f%n", plot.binMeanX(xBin, yBin), plot.binMeanY(xBin, yBin), plot.binHeight(xBin, yBin)));
+			}
+		}
+		
+		// Return the converted file.
+		return buffer.toString();
 	}
 }