Author: [log in to unmask] Date: Mon Aug 31 17:42:25 2015 New Revision: 3477 Log: Add sample Drivers for calculating integrated luminosity for a job. Added: java/trunk/users/src/main/java/org/hps/users/jeremym/LuminosityAnalysisDriver.java java/trunk/users/src/main/java/org/hps/users/jeremym/LuminosityDriver.java java/trunk/users/src/test/java/org/hps/users/jeremym/LuminosityAnalysisDriverTest.java Modified: java/trunk/users/pom.xml Modified: java/trunk/users/pom.xml ============================================================================= --- java/trunk/users/pom.xml (original) +++ java/trunk/users/pom.xml Mon Aug 31 17:42:25 2015 @@ -14,6 +14,19 @@ <connection>scm:svn:svn://svn.freehep.org/hps/java/trunk/users/</connection> <developerConnection>scm:svn:svn://svn.freehep.org/hps/java/trunk/users/</developerConnection> </scm> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <excludes> + <exclude>org/hps/users/**</exclude> + </excludes> + </configuration> + </plugin> + </plugins> + </build> <dependencies> <dependency> <groupId>org.hps</groupId> Added: java/trunk/users/src/main/java/org/hps/users/jeremym/LuminosityAnalysisDriver.java ============================================================================= --- java/trunk/users/src/main/java/org/hps/users/jeremym/LuminosityAnalysisDriver.java (added) +++ java/trunk/users/src/main/java/org/hps/users/jeremym/LuminosityAnalysisDriver.java Mon Aug 31 17:42:25 2015 @@ -0,0 +1,46 @@ +package org.hps.users.jeremym; + +import org.lcsim.event.EventHeader; +import org.lcsim.util.Driver; + +/** + * Parent <code>Driver</code> showing how to use the {@link LuminosityDriver}. + * + * @author Jeremy McCormick, SLAC + */ +public class LuminosityAnalysisDriver extends Driver { + + /** + * Reference to the <code>Driver</code> which will perform the calculation. + */ + private final LuminosityDriver lumiDriver; + + /** + * Class constructor. + * <p> + * Adds an instance of this <code>Driver</code>. + */ + public LuminosityAnalysisDriver() { + lumiDriver = new LuminosityDriver(); + this.add(lumiDriver); + } + + /** + * End of job hook. + */ + @Override + public void endOfData() { + super.endOfData(); + System.out.println("luminosity for job was " + lumiDriver.getLuminosity() + "C"); + } + + /** + * Event processing hook. + * + * @param eventHeader the event to process + */ + @Override + public void process(final EventHeader eventHeader) { + super.process(eventHeader); + } +} Added: java/trunk/users/src/main/java/org/hps/users/jeremym/LuminosityDriver.java ============================================================================= --- java/trunk/users/src/main/java/org/hps/users/jeremym/LuminosityDriver.java (added) +++ java/trunk/users/src/main/java/org/hps/users/jeremym/LuminosityDriver.java Mon Aug 31 17:42:25 2015 @@ -0,0 +1,115 @@ +package org.hps.users.jeremym; + +import java.util.ArrayList; +import java.util.List; + +import org.hps.record.epics.EpicsData; +import org.lcsim.event.EventHeader; +import org.lcsim.util.Driver; + +/** + * Calculate integrated luminosity using EPICS data. + * <p> + * This does not calculate luminosity for time period before there is any EPICS data processed in the job, and the time + * period between the last EPICS event and the end of the job is also ignored. + * <p> + * This <code>Driver</code> would be easiest to use as a child of an analysis <code>Driver</code> so that the calculated + * luminosity can be easily retrieved in the parent's {@link org.lcsim.util.Driver#endOfData()} method. + * + * @author Jeremy McCormick, SLAC + * @see LuminosityAnalysisDriver + */ +public class LuminosityDriver extends Driver { + + /** + * EPICS variable to use for current measurement. + */ + private static final String EPICS_VARIABLE = "scaler_calc1"; + + /** + * Calculate the luminosity in coulomb given a list of EPICS data. + * + * @param epicsData the list of EPICS data + * @return the calculated luminosity + */ + 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; + } + + /** + * The list of EPICS data accumulated during the job. + */ + private final List<EpicsData> epicsDataList = new ArrayList<EpicsData>(); + + /** + * The final measurement of integrated + */ + private double luminosity; + + /** + * End of data hook which performs luminosity calculation. + */ + @Override + public void endOfData() { + if (epicsDataList.isEmpty()) { + throw new RuntimeException("The EPICS data list is empty."); + } + luminosity = calculateLuminosity(this.epicsDataList); + } + + /** + * Get the calculated luminosity for the job. + * + * @return the calculated luminosity for the job + */ + public double getLuminosity() { + return luminosity; + } + + /** + * Process an event. + * + * @param eventHeader the event header + */ + @Override + public void process(final EventHeader eventHeader) { + final EpicsData epicsData = EpicsData.read(eventHeader); + if (epicsData != null) { + if (epicsData.hasKey(EPICS_VARIABLE)) { + System.out.println("adding EPICS data with timestamp " + epicsData.getEpicsHeader().getTimestamp() + + " and Faraday cup current " + epicsData.getValue(EPICS_VARIABLE)); + epicsDataList.add(epicsData); + } + } + } +} Added: java/trunk/users/src/test/java/org/hps/users/jeremym/LuminosityAnalysisDriverTest.java ============================================================================= --- java/trunk/users/src/test/java/org/hps/users/jeremym/LuminosityAnalysisDriverTest.java (added) +++ java/trunk/users/src/test/java/org/hps/users/jeremym/LuminosityAnalysisDriverTest.java Mon Aug 31 17:42:25 2015 @@ -0,0 +1,28 @@ +package org.hps.users.jeremym; + +import java.io.File; + +import junit.framework.TestCase; + +import org.lcsim.util.loop.LCSimLoop; + +/** + * Basic test of {@link LuminosityAnalysisDriver}. + * + * @author Jeremy McCormick, SLAC + */ +public class LuminosityAnalysisDriverTest extends TestCase { + + /** + * Run the test. + * + * @throws Exception if any error occurs + */ + public void testLuminosityAnalysisDriver() throws Exception { + final LCSimLoop loop = new LCSimLoop(); + loop.add(new LuminosityAnalysisDriver()); + loop.setLCIORecordSource(new File("/u1/test/hps/recon/hps_005772_data_only.slcio")); + loop.loop(-1); + } + +}