Print

Print


Author: [log in to unmask]
Date: Wed Sep 23 10:06:05 2015
New Revision: 3682

Log:
Revert last commit.

Modified:
    java/trunk/users/src/main/java/org/hps/users/jeremym/LuminosityDriver.java

Modified: java/trunk/users/src/main/java/org/hps/users/jeremym/LuminosityDriver.java
 =============================================================================
--- java/trunk/users/src/main/java/org/hps/users/jeremym/LuminosityDriver.java	(original)
+++ java/trunk/users/src/main/java/org/hps/users/jeremym/LuminosityDriver.java	Wed Sep 23 10:06:05 2015
@@ -1,4 +1,7 @@
-  package org.hps.users.jeremym;
+package org.hps.users.jeremym;
+
+import java.util.ArrayList;
+import java.util.List;
 
 import org.hps.record.epics.EpicsData;
 import org.lcsim.event.EventHeader;
@@ -24,35 +27,49 @@
     private static final String EPICS_VARIABLE = "scaler_calc1";
 
     /**
-     * Conversion from scaler counts to charge.
-     */
-    private final double SCALER_COUNTS_TO_CHARGE = 905.937;
-    
-    /**
      * Calculate the luminosity in coulomb given a list of EPICS data.
      *
      * @param epicsData the list of EPICS data
      * @return the calculated luminosity
      */
-    private double calculateLuminosity() {      
-        double scalerCount = lastEpicsData.getValue(EPICS_VARIABLE) -firstEpicsData.getValue(EPICS_VARIABLE);
-        return SCALER_COUNTS_TO_CHARGE * scalerCount;
+    private static double calculateLuminosity(final List<EpicsData> epicsDataList) {
+        if (epicsDataList.isEmpty()) {
+            throw new RuntimeException("The EPICS data list is empty.");
+        }
+        double integratedLuminosity = 0;
+        for (int i = 0; i < epicsDataList.size(); i++) {
+            if (i != epicsDataList.size() - 1) {
+
+                // Get a pair of EPICS records.
+                final EpicsData epicsDataStart = epicsDataList.get(i);
+                final EpicsData epicsDataEnd = epicsDataList.get(i + 1);
+
+                // Calculate elapsed time between the EPICS events.
+                int timeLength = epicsDataEnd.getEpicsHeader().getTimestamp()
+                        - epicsDataStart.getEpicsHeader().getTimestamp();
+
+                if (timeLength == 0) {
+                    // Force at least 1 second time resolution.
+                    timeLength = 1;
+                }
+
+                // Get average current over the time period.
+                final double averageCurrent = (epicsDataStart.getValue(EPICS_VARIABLE) + epicsDataEnd
+                        .getValue(EPICS_VARIABLE)) / 2.;
+
+                // Add the current for the time period to the integrated luminosity total.
+                integratedLuminosity += timeLength * averageCurrent;
+            }
+        }
+        // Convert from nano coulomb to coulomb.
+        integratedLuminosity *= 10e-9;
+        return integratedLuminosity;
     }
 
     /**
-     * First EPICS data bank.
+     * The list of EPICS data accumulated during the job.
      */
-    private EpicsData firstEpicsData = null;
-
-    /**
-     * Last EPICS data bank.
-     */
-    private EpicsData lastEpicsData = null;
-    
-    /**
-     * Current bank used for getting the last bank in {@link #endOfData()}.
-     */
-    private EpicsData currentEpicsData = null;
+    private final List<EpicsData> epicsDataList = new ArrayList<EpicsData>();
 
     /**
      * The final measurement of integrated
@@ -64,8 +81,10 @@
      */
     @Override
     public void endOfData() {
-        lastEpicsData = currentEpicsData;
-        luminosity = calculateLuminosity();
+        if (epicsDataList.isEmpty()) {
+            throw new RuntimeException("The EPICS data list is empty.");
+        }
+        luminosity = calculateLuminosity(this.epicsDataList);
     }
 
     /**
@@ -89,10 +108,7 @@
             if (epicsData.hasKey(EPICS_VARIABLE)) {
                 System.out.println("adding EPICS data with timestamp " + epicsData.getEpicsHeader().getTimestamp()
                         + " and Faraday cup current " + epicsData.getValue(EPICS_VARIABLE));
-                if (firstEpicsData == null) {
-                    firstEpicsData = epicsData;
-                }
-                currentEpicsData = epicsData;
+                epicsDataList.add(epicsData);
             }
         }
     }