Print

Print


Author: [log in to unmask]
Date: Mon Feb  2 19:01:58 2015
New Revision: 2027

Log:
Add a simple fit to example monitoring plots.

Modified:
    java/trunk/monitoring-drivers/src/main/java/org/hps/monitoring/drivers/example/ExamplePlotDriver.java

Modified: java/trunk/monitoring-drivers/src/main/java/org/hps/monitoring/drivers/example/ExamplePlotDriver.java
 =============================================================================
--- java/trunk/monitoring-drivers/src/main/java/org/hps/monitoring/drivers/example/ExamplePlotDriver.java	(original)
+++ java/trunk/monitoring-drivers/src/main/java/org/hps/monitoring/drivers/example/ExamplePlotDriver.java	Mon Feb  2 19:01:58 2015
@@ -2,6 +2,11 @@
 
 import hep.aida.ICloud1D;
 import hep.aida.ICloud2D;
+import hep.aida.IFitFactory;
+import hep.aida.IFitResult;
+import hep.aida.IFitter;
+import hep.aida.IFunction;
+import hep.aida.IFunctionFactory;
 import hep.aida.IHistogram1D;
 import hep.aida.IHistogram2D;
 import hep.aida.IPlotter;
@@ -9,6 +14,8 @@
 import hep.aida.IPlotterStyle;
 
 import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import org.lcsim.detector.identifier.IIdentifier;
 import org.lcsim.detector.identifier.IIdentifierHelper;
@@ -17,91 +24,141 @@
 import org.lcsim.event.Cluster;
 import org.lcsim.event.EventHeader;
 import org.lcsim.event.RawCalorimeterHit;
+import org.lcsim.geometry.Detector;
 import org.lcsim.util.Driver;
 import org.lcsim.util.aida.AIDA;
 
+/**
+ * Example monitoring plots, currently only using ECAL data.
+ * 
+ * @author Jeremy McCormick <[log in to unmask]>
+ */
 /* 
 
-TODO: 
+TODO List 
 
 Add the following example plot types...
  
 [X] Histogram1D 
 
-[X] overlaid Histogram1D  
+[X] Histogram1D overlay  
 
 [X] Cloud1D
 
-[X] overlaid Cloud1D 
+[X] Cloud1D overlay 
 
 [X] Histogram2D color map
 
 [X] Histogram2D box plot
  
-[X] overlaid Histogram2D box plot
+[X] Histogram2D overlay
 
 [ ] Cloud2D as scatter plot
 
-[ ] overlaid Cloud2D scatter plots
-
-[ ] Cloud2D convertered to histogram 
+[ ] Cloud2D scatter plot overlay
+
+[ ] Cloud2D converted to histogram
  
 [ ] Profile1D
 
 [ ] Profile2D
 
-[ ] IFunction
+[X] IFunction
 
 [ ] IDataPointSet
 
-see "Aida to JFree" doc for complete list
+See this link for complete list.
 
 https://docs.google.com/spreadsheets/d/1bqKvriNOEaeTrpTrk38kBGXM8oC_F5QIZeLcSSa3JsQ/
 
 */
-
 public class ExamplePlotDriver extends Driver {
     
     AIDA aida = AIDA.defaultInstance();
     
     IPlotterFactory plotterFactory;
+    IFunctionFactory functionFactory;
+    IFitFactory fitFactory;
     
     IHistogram1D calRawHitH1D, calClusterH1D, calHitH1D, calClusterEnergyH1D;   
     IHistogram2D calHitMapH2D, calRawHitMapH2D;    
     ICloud1D calRawHitsC1D, calClustersC1D;
     ICloud2D calHitsVsEnergyC2D;
+    IFunction fittedFunction;
     
     final static String ECAL_READOUT_HITS = "EcalReadoutHits";
     final static String ECAL_CAL_HITS = "EcalCalHits";
     final static String ECAL_CLUSTERS = "EcalClusters";
+    
+    IIdentifierHelper helper;
+    
+    private static final Logger minuitLogger = Logger.getLogger("org.freehep.math.minuit");
+    static {
+        minuitLogger.setLevel(Level.OFF);
+    }
         
     public ExamplePlotDriver() {
+    }
+    
+    public void detectorChanged(Detector detector) {
+        helper = detector.getSubdetector("Ecal").getDetectorElement().getIdentifierHelper();
     }
     
     public void startOfData() {                
                
+        // AIDA boilerplate stuff.
         plotterFactory = aida.analysisFactory().createPlotterFactory("Example Plots");
+        functionFactory = aida.analysisFactory().createFunctionFactory(null);
+        fitFactory = aida.analysisFactory().createFitFactory();        
+        
         IPlotter plotter = null;
         IPlotterStyle style = null;
         
-        //
-        // Example of single Histogram1D plot.
-        //        
-        calClusterEnergyH1D = aida.histogram1D("Cal Cluster Energy", 100, 0., 10.);        
+        // Define all of the AIDA objects to be used for example plotting.
+        calClusterEnergyH1D = aida.histogram1D("Cal Cluster Energy", 100, 0., 10.);
+        calRawHitH1D = aida.histogram1D("CalRawHit Count H1D", 20, 0., 20.);
+        calHitH1D = aida.histogram1D("CalHit Count H1D", 20, 0., 20.);
+        calClusterH1D = aida.histogram1D("CalCluster Count H1D", 20, 0., 20.);
+        calHitMapH2D = aida.histogram2D("CalHit Map H2D", 47, -23.5, 23.5, 11, -5.5, 5.5);
+        calRawHitMapH2D = aida.histogram2D("CalRawHit Map H2D", 47, -23.5, 23.5, 11, -5.5, 5.5);
+        calRawHitsC1D = aida.cloud1D("CalRawHit Count C1D", 500);
+        calClustersC1D = aida.cloud1D("CalCluster Count C1D", 500);
+        calHitsVsEnergyC2D = aida.cloud2D("CalHits vs Energy C2D", 1000000);
+        fittedFunction = functionFactory.createFunctionByName("Gaussian", "G");
+        fittedFunction.setTitle("Example Fit");
+        
+        /*
+         * Histogram1D 
+         */                       
         plotter = plotterFactory.create("IHistogram1D");
+        plotter.createRegion();
         style = createDefaultPlotterStyle();
         style.dataStyle().fillStyle().setColor("blue");
-        plotter.createRegion();        
         plotter.region(0).setTitle("IHistogram1D");
         plotter.region(0).plot(calClusterEnergyH1D, style);
         plotter.show();
         
-        //
-        // Example of overlaid Histogram1D plots.
-        //        
-        calRawHitH1D = aida.histogram1D("CalRawHit Count H1D", 20, 0., 20.);
-        calHitH1D = aida.histogram1D("CalHit Count H1D", 20, 0., 20.);
-        calClusterH1D = aida.histogram1D("CalCluster Count H1D", 20, 0., 20.);                
+        /*
+         * Fit of Histogram1D
+         */
+        plotter = plotterFactory.create("IHistogram1D Fit");
+        plotter.createRegion();
+        style = createDefaultPlotterStyle();
+        style.dataStyle().fillStyle().setColor("blue");
+        plotter.region(0).setTitle("IHistogram1D");
+        plotter.region(0).plot(calHitH1D, style);                
+        IPlotterStyle functionStyle = plotterFactory.createPlotterStyle();
+        functionStyle.dataStyle().lineStyle().setColor("red");
+        functionStyle.dataStyle().markerStyle().setVisible(true);
+        functionStyle.dataStyle().markerStyle().setColor("black");
+        functionStyle.dataStyle().markerStyle().setShape("dot");
+        functionStyle.dataStyle().markerStyle().setSize(2);
+        plotter.region(0).plot(fittedFunction, functionStyle);
+        plotter.show();                
+                                                    
+        /*
+         * Histogram1D overlay
+         */                         
         plotter = plotterFactory.create("Overlayed IHistogram1D");
         plotter.createRegion();
         plotter.region(0).setTitle("Overlayed IHistogram1D");
@@ -122,8 +179,9 @@
         plotter.region(0).plot(calClusterH1D, style);
         plotter.show();           
         
-        // Example of Histogram2D displayed as a color map.
-        calHitMapH2D = aida.histogram2D("CalHit Map H2D", 47, -23.5, 23.5, 11, -5.5, 5.5);
+        /*
+         * Histogram2D as color map.
+         */                 
         plotter = plotterFactory.create("Histogram2D Color Map");
         style = createDefaultPlotterStyle();
         style.setParameter("hist2DStyle", "colorMap");
@@ -132,7 +190,9 @@
         plotter.region(0).plot(calHitMapH2D, style);
         plotter.show();        
         
-        // Example of Histogram2D displayed as box plot.       
+        /*
+         * Histogram2D as box plot.
+         */       
         plotter = plotterFactory.create("Histogram2D Box Plot");
         style = createDefaultPlotterStyle();
         style.dataStyle().fillStyle().setVisible(false);
@@ -142,8 +202,9 @@
         plotter.region(0).plot(calHitMapH2D, style);
         plotter.show();
         
-        // Example of overlaid Histogram2D box plots. 
-        calRawHitMapH2D = aida.histogram2D("CalRawHit Map H2D", 47, -23.5, 23.5, 11, -5.5, 5.5);
+        /*
+         * Histogram2D overlay box plots
+         */                
         plotter = plotterFactory.create("Overlaid IHistogram2D Box Plots");
         plotter.createRegion();
         plotter.region(0).setTitle("Overlaid Histogram2D Box Plots");
@@ -157,8 +218,9 @@
         plotter.region(0).plot(calHitMapH2D, style);
         plotter.show();       
         
-        // Example of Cloud1D which will convert to a histogram.
-        calRawHitsC1D = aida.cloud1D("CalRawHit Count C1D", 500);
+        /*
+         * Cloud1D which will convert to a histogram on the fly
+         */                
         plotter = plotterFactory.create("Cloud1D");
         style = createDefaultPlotterStyle();
         plotter.createRegion();
@@ -166,8 +228,9 @@
         plotter.region(0).plot(calRawHitsC1D, style);
         plotter.show();
         
-        // Example of overlaid Cloud1D plots.
-        calClustersC1D = aida.cloud1D("CalCluster Count C1D", 500);
+        /*
+         * Cloud1D overlay
+         */                
         plotter = plotterFactory.create("Overlayed Cloud1D");        
         plotter.createRegion();
         plotter.region(0).setTitle("Overlayed Cloud1D");
@@ -183,8 +246,9 @@
         plotter.region(0).plot(calClustersC1D, style);
         plotter.show();        
         
-        // Example of Cloud2D displayed as scatter.
-        calHitsVsEnergyC2D = aida.cloud2D("CalHits vs Energy C2D", 1000000);
+        /*
+         * Cloud2D as scatter plot.
+         */        
         plotter = plotterFactory.create("Cloud2D Scatter Plot");
         plotter.createRegion();
         plotter.region(0).setTitle("Cloud2D Scatter Plot");
@@ -193,18 +257,13 @@
         style.dataStyle().markerStyle().setColor("purple");
         style.dataStyle().markerStyle().setSize(2);
         style.dataStyle().markerStyle().setShape("diamond");
-        plotter.region(0).plot(calHitsVsEnergyC2D, style);
-        plotter.show();
+        plotter.region(0).plot(calHitsVsEnergyC2D, style);        
+        plotter.show();                      
     }
     
     public void process(EventHeader event) {
         //printCollectionSummary(event);
-        
-        // FIXME: This is ugly.
-        IIdentifierHelper helper = 
-                event.getMetaData(event.get(CalorimeterHit.class, ECAL_CAL_HITS))
-                .getIDDecoder().getSubdetector().getDetectorElement().getIdentifierHelper();
-        
+               
         if (event.hasCollection(RawCalorimeterHit.class, ECAL_READOUT_HITS)) {
             List<RawCalorimeterHit> hits = event.get(RawCalorimeterHit.class, ECAL_READOUT_HITS);
             int nHits = hits.size();
@@ -215,8 +274,7 @@
                 int ix = helper.getValue(id, "ix");
                 int iy = helper.getValue(id, "iy");
                 calRawHitMapH2D.fill(ix, iy);
-            }
-            
+            }            
         }
         
         if (event.hasCollection(CalorimeterHit.class, ECAL_CAL_HITS)) {
@@ -239,24 +297,32 @@
             for (Cluster cluster : clusters) {
                 calClusterEnergyH1D.fill(cluster.getEnergy());
             }
-        }               
-    }
-    
-    private IPlotterStyle createDefaultPlotterStyle() {
+        }    
+        
+        // Fit an H1D.
+        IFunction currentFitFunction = performGaussianFit(calHitH1D).fittedFunction();
+        
+        // Copy function parameters into the plotted function which will trigger an update.
+        fittedFunction.setParameters(currentFitFunction.parameters());
+    }
+    
+    IPlotterStyle createDefaultPlotterStyle() {
         IPlotterStyle style = plotterFactory.createPlotterStyle();
         style.gridStyle().setVisible(false);      
         style.legendBoxStyle().setVisible(true);
         return style;
     }
     
-    /*
-    private void printCollectionSummary(EventHeader event) {
-        System.out.println("Collections in event #" + event.getEventNumber() + " ...");
-        Collection<LCMetaData> metaData = event.getMetaData();
-        for (LCMetaData meta : metaData) {
-            System.out.println("  " + meta.getName() + " has " + event.get(meta.getType()).size() + " objects");
-        }
-    }
-    */
-
-}
+    IFitResult performGaussianFit(IHistogram1D histogram) {
+        IFunction function = functionFactory.createFunctionByName("Example Fit", "G");        
+        IFitter fitter = fitFactory.createFitter("chi2", "jminuit");
+        double[] parameters = new double[3];        
+        parameters[0] = histogram.maxBinHeight();
+        parameters[1] = histogram.mean();
+        parameters[2] = histogram.rms();
+        function.setParameters(parameters);
+        IFitResult fitResult = fitter.fit(histogram, function);
+        return fitResult;
+    }
+    
+}