9 added + 4 removed + 3 modified, total 16 files
hps-java/src/main/java/org/lcsim/hps/conditions
diff -N DatabaseConditionsConverter.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DatabaseConditionsConverter.java 18 Sep 2013 02:33:16 -0000 1.1
@@ -0,0 +1,15 @@
+package org.lcsim.hps.conditions;
+
+import org.lcsim.conditions.ConditionsConverter;
+
+public abstract class DatabaseConditionsConverter<T> implements ConditionsConverter<T> {
+ ConnectionParameters connectionParameters = null;
+
+ protected DatabaseConditionsConverter(ConnectionParameters parameters) {
+ this.connectionParameters = parameters;
+ }
+
+ public ConnectionParameters getConnectionParameters() {
+ return this.connectionParameters;
+ }
+}
\ No newline at end of file
hps-java/src/main/java/org/lcsim/hps/conditions
diff -u -r1.1 -r1.2
--- ConnectionParameters.java 18 Sep 2013 01:13:32 -0000 1.1
+++ ConnectionParameters.java 18 Sep 2013 02:33:16 -0000 1.2
@@ -11,7 +11,7 @@
* return a Connection object based on these parameters.
*
* @author jeremym
- * @version $Id: ConnectionParameters.java,v 1.1 2013/09/18 01:13:32 jeremy Exp $
+ * @version $Id: ConnectionParameters.java,v 1.2 2013/09/18 02:33:16 jeremy Exp $
*/
public final class ConnectionParameters {
@@ -20,10 +20,21 @@
private String password = "2jumpinphotons.";
private int port = 3306;
private String hostname = "mysql-node03.slac.stanford.edu";
+ private String database = "rd_hps_cond";
+ /**
+ * No argument constructor using defaults.
+ */
ConnectionParameters() {
}
+ /**
+ * Fully qualified constructor.
+ * @param user The user name.
+ * @param password The password.
+ * @param hostname The hostname.
+ * @param port The port number.
+ */
ConnectionParameters(String user, String password, String hostname, int port) {
this.user = user;
this.password = password;
@@ -31,26 +42,55 @@
this.port = port;
}
- Properties getConnectionProperties() {
+ /**
+ * Get Properties object for this connection.
+ * @return The Properties for this connection.
+ */
+ public Properties getConnectionProperties() {
Properties p = new Properties();
p.put("user", user);
p.put("password", password);
return p;
}
- String getHostname() {
+ /**
+ * Get the hostname.
+ * @return The hostname.
+ */
+ public String getHostname() {
return hostname;
}
- int getPort() {
+ /**
+ * Get the port number.
+ * @return The port number.
+ */
+ public int getPort() {
return port;
}
+
+ /**
+ * Get the name of the database.
+ * @return The name of the database.
+ */
+ public String getDatabase() {
+ return database;
+ }
- String getConnectionString() {
+ /**
+ * Get the connection string for these parameters.
+ * @return The connection string.
+ */
+ public String getConnectionString() {
return "jdbc:mysql://" + hostname + ":" + port + "/";
}
- Connection createConnection() {
+ /**
+ * Create a database connection from these parameters.
+ * The caller is responsible for closing it when finished.
+ * @return The Connection object.
+ */
+ public Connection createConnection() {
Properties connectionProperties = getConnectionProperties();
Connection connection = null;
try {
hps-java/src/main/java/org/lcsim/hps/conditions
diff -u -r1.1 -r1.2
--- DatabaseConditionsReader.java 27 Mar 2013 04:01:56 -0000 1.1
+++ DatabaseConditionsReader.java 18 Sep 2013 02:33:16 -0000 1.2
@@ -5,143 +5,185 @@
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
-import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.*;
-import org.lcsim.conditions.ConditionsConverter;
+import java.util.HashMap;
+import java.util.Properties;
+
import org.lcsim.conditions.ConditionsManager;
import org.lcsim.conditions.ConditionsReader;
+import org.lcsim.hps.conditions.svt.SvtCalibrationConverter;
/**
- * HPS-specific conditions reader. Wraps around any standard reader and adds an ability to
- * fetch calibration data from the database.
- * <p>
- * TEMPORARY IMPLEMENTATION - DEMO.
- *
- * @author onoprien
- * @version $Id: DatabaseConditionsReader.java,v 1.1 2013/03/27 04:01:56 meeg Exp $
+ * This a refactored version of Dima's ExampleDatabaseConditionsReader that attempts
+ * to handle the conditions and their meta data more generically. (Work in progress!)
+ *
+ * @author jeremym
+ * @version $Id: DatabaseConditionsReader.java,v 1.2 2013/09/18 02:33:16 jeremy Exp $
*/
public class DatabaseConditionsReader extends ConditionsReader {
-
-// -- Private parts : ----------------------------------------------------------
-
- static ConditionsConverter _converterCalibration = new CalibrationConverter();
-
- private final ConditionsReader _reader;
-
- private String _detectorName;
- private int _minRun = Integer.MAX_VALUE;
- private int _maxRun = Integer.MIN_VALUE;
-
- private final HashMap<String, byte[]> _propCache = new HashMap<String, byte[]>();
-
-// -- Construction and initialization : ----------------------------------------
-
- public DatabaseConditionsReader(ConditionsReader reader) {
- _reader = reader;
- }
-
-
-// -- Updating to a new detector/run : -----------------------------------------
-
- public boolean update(ConditionsManager manager, String detectorName, int run) throws IOException {
-
- if (_detectorName == null) {
- _detectorName = detectorName;
- } else {
- if (!_detectorName.equals(detectorName)) throw new IllegalArgumentException();
- }
- if ( run <= _maxRun && run >= _minRun) return false;
- manager.registerConditionsConverter(_converterCalibration);
-
- _propCache.clear();
-
- // Open connection to the db
-
- Properties connectionProps = new Properties();
- connectionProps.put("user", "rd_hps_cond_ro");
- connectionProps.put("password", "2jumpinphotons.");
- Connection con = null;
- try {
- con = DriverManager.getConnection("jdbc:mysql://mysql-node03.slac.stanford.edu:3306/", connectionProps);
- } catch (SQLException x) {
- throw new IOException("Failed to connect to database", x);
- }
-
- // Fetch whatever database-kept conditions should be accessible through ConditionsSet interface.
- // This needs to be optimized once the db structure and data requirement are known.
- // (can be templated if the db structure is standardized)
-
- String query = "SELECT data_ident, runStart, runEnd FROM rd_hps_cond.conditions_test WHERE "+
- "runStart <= "+ run +" AND runEnd >= "+ run + " AND level = 'PROD'";
-
- Statement stmt = null;
- int count = 0;
- String data = null;
- try {
- stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- while (rs.next()) {
- count++;
- data = rs.getString(1);
- _minRun = rs.getInt(2);
- _maxRun = rs.getInt(3);
- }
- } catch (SQLException x) {
- throw new IOException("Failed to execute query", x);
- } finally {
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException x) {
- throw new IOException("Failed to close statement", x);
- }
- }
- }
- // Need to get requirements from HPS people - how is this case supposed to be handled ?
- if (count != 1) throw new IOException("Found "+ count +" valid calibrations");
+ /** Database connection parameters. */
+ ConnectionParameters connectionParameters = new ConnectionParameters();
- Properties p = new Properties();
- String[] d = data.trim().split(":");
- try {
- p.put("table", d[0]);
- p.put("column", d[1]);
- p.put("id", d[2]);
- } catch (IndexOutOfBoundsException x) {
- throw new IOException("Illegal data_ident format", x);
- }
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- p.store(stream, null);
- _propCache.put("calibration", stream.toByteArray());
+ /** Database connection. */
+ Connection connection = null;
- // Close connection to the db
+ /** Base ConditionsReader for getting the Detector. */
+ private final ConditionsReader reader;
- try {
- con.close();
- } catch (SQLException x) {
- throw new IOException("Failed to close connection", x);
- }
-
- return true;
- }
-
-// -- Implementing ConditionsReader : ------------------------------------------
+ /** Name of the detector. */
+ private String detectorName;
+
+ /** Current minimum run number. This is updated as conditions are loaded. */
+ private int minRun = Integer.MAX_VALUE;
+
+ /** Current maximum run number. This is updated as conditions are loaded. */
+ private int maxRun = Integer.MIN_VALUE;
+
+ private final HashMap<String, byte[]> propertyCache = new HashMap<String, byte[]>();
+
+ /**
+ * Class constructor taking a ConditionsReader, which is probably a ZipFileConditionsReader.
+ * This constructor is automatically called by the ConditionsManager when this type has been
+ * requested via the detector properties file.
+ *
+ * @param reader The basic ConditionsReader allowing access to the detector.
+ */
+ public DatabaseConditionsReader(ConditionsReader reader) {
+ this.reader = reader;
+ }
+
+ /**
+ * Update conditions for possibly new detector and run number. This will cache the
+ * conditions meta data but will not automatically do any conversion.
+ */
+ public boolean update(ConditionsManager manager, String detectorName, int run) throws IOException {
+
+ // Setup detector.
+ if (this.detectorName == null) {
+ this.detectorName = detectorName;
+ } else {
+ if (!this.detectorName.equals(detectorName))
+ throw new IllegalArgumentException();
+ }
+
+ // Check if conditions for this run are already loaded.
+ if (run <= maxRun && run >= minRun)
+ return false;
+
+ // Clear the property cache.
+ propertyCache.clear();
+
+ // Open a connection to the database.
+ connection = this.connectionParameters.createConnection();
+
+ // Register the conditions converters.
+ registerConditionsConverters(manager);
- public void close() throws IOException {
- _reader.close();
- }
+ // Cache the meta data for the fetched conditions records.
+ try {
+ cacheMetadata(run);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+
+ // Close the database connection.
+ try {
+ connection.close();
+ connection = null;
+ } catch (SQLException x) {
+ throw new IOException("Failed to close connection", x);
+ }
+
+ return true;
+ }
- public InputStream open(String name, String type) throws IOException {
- byte[] ba = _propCache.get(name);
- if (ba == null) {
- return _reader.open(name, type);
- } else {
- return new ByteArrayInputStream(ba);
+ /**
+ * Close the base reader of the detector description.
+ */
+ public void close() throws IOException {
+ reader.close();
+ }
+
+ /**
+ * Read properties from cache, which are table, field and id.
+ * @return An InputStream with the cached properties for <code>name</code>.
+ */
+ public InputStream open(String name, String type) throws IOException {
+ byte[] ba = propertyCache.get(name);
+ if (ba == null) {
+ return reader.open(name, type);
+ } else {
+ return new ByteArrayInputStream(ba);
+ }
}
- }
+
+ /**
+ * This will cache the conditions meta data for a run so that the actual data can be fetched later.
+ * @param run The run number.
+ * @throws SQLException
+ * @throws IOException
+ */
+ private final void cacheMetadata(int run) throws SQLException, IOException {
+
+ String query = "SELECT data_ident, runStart, runEnd, calib_type FROM rd_hps_cond.conditions_test WHERE "
+ + "runStart <= "
+ + run
+ + " AND runEnd >= "
+ + run
+ + " AND level = 'PROD'";
-// -----------------------------------------------------------------------------
+ Statement statement = null;
+ String data = null;
+ try {
+ statement = connection.createStatement();
+ ResultSet resultSet = statement.executeQuery(query);
+ while (resultSet.next()) {
+
+ // String containing conditions set meta data.
+ data = resultSet.getString(1);
+
+ // The minimum run.
+ minRun = resultSet.getInt(2);
+
+ // The maximum run.
+ maxRun = resultSet.getInt(3);
+
+ // The type of the calibration.
+ String calibrationType = resultSet.getString(4);
+
+ // Cache the properties for this condition by type.
+ addProperty(calibrationType, data);
+ }
+ } finally {
+ if (statement != null) {
+ try {
+ statement.close();
+ } catch (SQLException x) {
+ throw new IOException("Failed to close statement", x);
+ }
+ }
+ }
+ }
+
+ private void addProperty(String key, String data) throws IOException {
+ Properties p = new Properties();
+ String[] d = data.trim().split(":");
+ try {
+ p.put("table", d[0]);
+ p.put("column", d[1]);
+ p.put("id", d[2]);
+ } catch (IndexOutOfBoundsException x) {
+ throw new IOException("Illegal data_ident format", x);
+ }
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ p.store(stream, null);
+ propertyCache.put(key, stream.toByteArray());
+ }
+
+ private void registerConditionsConverters(ConditionsManager manager) {
+ manager.registerConditionsConverter(new SvtCalibrationConverter(connectionParameters));
+ }
}
hps-java/src/main/java/org/lcsim/hps/conditions
diff -N Calibration.java
--- Calibration.java 19 Mar 2012 20:58:10 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,72 +0,0 @@
-package org.lcsim.hps.conditions;
-
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * Run-specific set of calibration constants.
- * <p>
- * TEMPORARY IMPLEMENTATION - DEMO.
- *
- * @author onoprien
- * @version $Id: Calibration.java,v 1.1 2012/03/19 20:58:10 onoprien Exp $
- */
-public class Calibration {
-
-// -- Private parts : ----------------------------------------------------------
-
- private double[] _data;
-
-// -- Construction : -----------------------------------------------------------
-
- Calibration(HashMap<Integer, Double> data, int maxChannel) {
- _data = new double[maxChannel+1];
- Arrays.fill(_data, -1.);
- for (Map.Entry<Integer, Double> e : data.entrySet()) {
- _data[e.getKey()] = e.getValue();
- }
- }
-
-// -- Getters : ----------------------------------------------------------------
-
- /**
- * Returns calibration value for the specified channel.
- * @throws IllegalArgumentException if the current calibration does not contain data for the given channel.
- */
- public double get(int channel) {
- double out = -1.;
- try {
- out = _data[channel];
- } catch (IndexOutOfBoundsException x) {
- throw new IllegalArgumentException();
- }
- if (out < 0.) throw new IllegalArgumentException();
- return out;
- }
-
- /**
- * Returns calibration value for the specified channel.
- * Returns the specified default value if the current calibration does not contain data for the given channel.
- */
- public double get(int channel, double defaultValue) {
- double out = -1.;
- try {
- out = _data[channel];
- } catch (IndexOutOfBoundsException x) {
- return defaultValue;
- }
- if (out < 0.) return defaultValue;
- return out;
- }
-
-// -- Overriding Object : ------------------------------------------------------
-
- public String toString() {
- StringBuilder s = new StringBuilder();
- for (double d : _data) s.append(d).append(" ");
- return s.toString();
- }
-
-}
hps-java/src/main/java/org/lcsim/hps/conditions
diff -N CalibrationConverter.java
--- CalibrationConverter.java 19 Mar 2012 20:58:10 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,61 +0,0 @@
-package org.lcsim.hps.conditions;
-
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.HashMap;
-import java.util.Properties;
-import org.lcsim.conditions.ConditionsConverter;
-import org.lcsim.conditions.ConditionsManager;
-import org.lcsim.conditions.ConditionsSet;
-
-/**
- * <tt>ConditionsConverter</tt> for {@link Calibration} class.
- * <p>
- * TEMPORARY IMPLEMENTATION - DEMO.
- *
- * @author onoprien
- * @version $Id: CalibrationConverter.java,v 1.1 2012/03/19 20:58:10 onoprien Exp $
- */
-public class CalibrationConverter implements ConditionsConverter<Calibration> {
-
- public Calibration getData(ConditionsManager manager, String name) {
- Connection con = null;
- Statement stmt = null;
- try {
- ConditionsSet cs = manager.getConditions("calibration");
- Properties connectionProps = new Properties();
- connectionProps.put("user", "rd_hps_cond_ro");
- connectionProps.put("password", "2jumpinphotons.");
- con = DriverManager.getConnection("jdbc:mysql://mysql-node03.slac.stanford.edu:3306/", connectionProps);
- String query = "SELECT channel_id, aValue FROM rd_hps_cond."+ cs.getString("table") +" WHERE "+
- cs.getString("column") +" = "+ cs.getString("id");
- stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- HashMap<Integer, Double> data = new HashMap<Integer, Double>();
- int maxChannel = -1;
- while (rs.next()) {
- int channel_id = rs.getInt(1);
- if (channel_id > maxChannel) maxChannel = channel_id;
- double aValue = rs.getDouble(2);
- data.put(channel_id, aValue);
- }
- return new Calibration(data, maxChannel);
- } catch (SQLException x) {
- if (stmt != null) {
- try {stmt.close();} catch (Exception xx) {}
- }
- if (con != null) {
- try {con.close();} catch (Exception xx) {}
- }
- throw new RuntimeException("Database error", x);
- }
- }
-
- public Class getType() {
- return Calibration.class;
- }
-
-}
hps-java/src/main/java/org/lcsim/hps/conditions
diff -N ExampleDriver.java
--- ExampleDriver.java 19 Mar 2012 20:58:11 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,68 +0,0 @@
-package org.lcsim.hps.conditions;
-
-import org.lcsim.conditions.CachedConditions;
-import org.lcsim.conditions.ConditionsManager;
-import org.lcsim.conditions.ConditionsManager.ConditionsSetNotFoundException;
-import org.lcsim.conditions.ConditionsSet;
-import org.lcsim.geometry.Detector;
-import org.lcsim.util.Driver;
-
-/**
- *
- * @author onoprien
- */
-public class ExampleDriver extends Driver {
-
- public ExampleDriver() {
-
- }
-
- protected void detectorChanged(Detector detector) {
- System.out.println("detectorChanged: "+ detector.getDetectorName());
- super.detectorChanged(detector);
- ConditionsManager condMan = getConditionsManager();
- try {
- ConditionsSet set1 = condMan.getConditions("HadronCalibration/EMBarrel");
- System.out.println("HadronCalibration/EMBarrel: hitEnergycut1 = "+ set1.getDouble("hitEnergycut1"));
- } catch (ConditionsSetNotFoundException x) {
- System.out.println("No HadronCalibration/EMBarrel: hitEnergycut1");
- }
- try {
- ConditionsSet set1 = condMan.getConditions("SamplingFractions/EMBarrel");
- System.out.println("1");
- System.out.println("SamplingFractions/EMBarrel: samplingFraction = "+ set1.getDouble("samplingFraction"));
- } catch (ConditionsSetNotFoundException x) {
- System.out.println("No SamplingFractions/EMBarrel: samplingFraction");
- }
- try {
- ConditionsSet set2 = condMan.getConditions("calibration");
- System.out.println("calibration: "+ set2.getString("table") +" "+ set2.getString("column") +" "+ set2.getString("id"));
- CachedConditions<Calibration> c = condMan.getCachedConditions(Calibration.class, "");
- Calibration cal = c.getCachedData();
- System.out.println("New calibration "+ cal);
- } catch (ConditionsSetNotFoundException x) {
- System.out.println("No calibration found "+ x);
- }
- }
-
- protected void endOfData() {
- System.out.println("endOfData");
- super.endOfData();
- }
-
- protected void resume() {
- System.out.println("resume");
- super.resume();
- }
-
- protected void suspend() {
- System.out.println("suspend");
- super.suspend();
- }
-
- protected void startOfData() {
- System.out.println("startOfData");
- super.startOfData();
- }
-
-}
hps-java/src/main/java/org/lcsim/hps/conditions
diff -N package-info.java
--- package-info.java 26 Mar 2012 21:01:50 -0000 1.2
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,20 +0,0 @@
-/**
- * Classes adding HPS-specific functionality to the <tt>org.lcsim</tt> conditions framework.
- * <p>
- * This is a demo implementation of the package. The goal was to minimize changes
- * to the <tt>org.lcsim.conditions</tt> package required to support HPS extensions, and
- * to guarantee backward compatibility.
- * <p>
- * To enable reading conditions from the HPS database:
- * <ul>
- * <li>Add the line "ConditionsReader: org.lcsim.hps.conditions.HPSConditionsReader" to
- * the <tt>detector.properties</tt> file in your conditions ZIP file or directory.</li>
- * <li>Install MySQL driver into your classpath.</li>
- * </ul>
- * Once the exact structure of the database and the calibration data to be retrieved is
- * known, more efficient and robust implementation will be coded.
- *
- * @see org.lcsim.conditions
- */
-package org.lcsim.hps.conditions;
-
hps-java/src/main/java/org/lcsim/hps/conditions/demo
diff -N Calibration.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Calibration.java 18 Sep 2013 02:33:16 -0000 1.1
@@ -0,0 +1,72 @@
+package org.lcsim.hps.conditions.demo;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+
+/**
+ * Run-specific set of calibration constants.
+ * <p>
+ * TEMPORARY IMPLEMENTATION - DEMO.
+ *
+ * @author onoprien
+ * @version $Id: Calibration.java,v 1.1 2013/09/18 02:33:16 jeremy Exp $
+ */
+public class Calibration {
+
+// -- Private parts : ----------------------------------------------------------
+
+ private double[] _data;
+
+// -- Construction : -----------------------------------------------------------
+
+ Calibration(HashMap<Integer, Double> data, int maxChannel) {
+ _data = new double[maxChannel+1];
+ Arrays.fill(_data, -1.);
+ for (Map.Entry<Integer, Double> e : data.entrySet()) {
+ _data[e.getKey()] = e.getValue();
+ }
+ }
+
+// -- Getters : ----------------------------------------------------------------
+
+ /**
+ * Returns calibration value for the specified channel.
+ * @throws IllegalArgumentException if the current calibration does not contain data for the given channel.
+ */
+ public double get(int channel) {
+ double out = -1.;
+ try {
+ out = _data[channel];
+ } catch (IndexOutOfBoundsException x) {
+ throw new IllegalArgumentException();
+ }
+ if (out < 0.) throw new IllegalArgumentException();
+ return out;
+ }
+
+ /**
+ * Returns calibration value for the specified channel.
+ * Returns the specified default value if the current calibration does not contain data for the given channel.
+ */
+ public double get(int channel, double defaultValue) {
+ double out = -1.;
+ try {
+ out = _data[channel];
+ } catch (IndexOutOfBoundsException x) {
+ return defaultValue;
+ }
+ if (out < 0.) return defaultValue;
+ return out;
+ }
+
+// -- Overriding Object : ------------------------------------------------------
+
+ public String toString() {
+ StringBuilder s = new StringBuilder();
+ for (double d : _data) s.append(d).append(" ");
+ return s.toString();
+ }
+
+}
hps-java/src/main/java/org/lcsim/hps/conditions/demo
diff -N CalibrationConverter.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ CalibrationConverter.java 18 Sep 2013 02:33:16 -0000 1.1
@@ -0,0 +1,62 @@
+package org.lcsim.hps.conditions.demo;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.HashMap;
+import java.util.Properties;
+
+import org.lcsim.conditions.ConditionsConverter;
+import org.lcsim.conditions.ConditionsManager;
+import org.lcsim.conditions.ConditionsSet;
+
+/**
+ * <tt>ConditionsConverter</tt> for {@link Calibration} class.
+ * <p>
+ * TEMPORARY IMPLEMENTATION - DEMO.
+ *
+ * @author onoprien
+ * @version $Id: CalibrationConverter.java,v 1.1 2013/09/18 02:33:16 jeremy Exp $
+ */
+public class CalibrationConverter implements ConditionsConverter<Calibration> {
+
+ public Calibration getData(ConditionsManager manager, String name) {
+ Connection con = null;
+ Statement stmt = null;
+ try {
+ ConditionsSet cs = manager.getConditions("calibration");
+ Properties connectionProps = new Properties();
+ connectionProps.put("user", "rd_hps_cond_ro");
+ connectionProps.put("password", "2jumpinphotons.");
+ con = DriverManager.getConnection("jdbc:mysql://mysql-node03.slac.stanford.edu:3306/", connectionProps);
+ String query = "SELECT channel_id, aValue FROM rd_hps_cond."+ cs.getString("table") +" WHERE "+
+ cs.getString("column") +" = "+ cs.getString("id");
+ stmt = con.createStatement();
+ ResultSet rs = stmt.executeQuery(query);
+ HashMap<Integer, Double> data = new HashMap<Integer, Double>();
+ int maxChannel = -1;
+ while (rs.next()) {
+ int channel_id = rs.getInt(1);
+ if (channel_id > maxChannel) maxChannel = channel_id;
+ double aValue = rs.getDouble(2);
+ data.put(channel_id, aValue);
+ }
+ return new Calibration(data, maxChannel);
+ } catch (SQLException x) {
+ if (stmt != null) {
+ try {stmt.close();} catch (Exception xx) {}
+ }
+ if (con != null) {
+ try {con.close();} catch (Exception xx) {}
+ }
+ throw new RuntimeException("Database error", x);
+ }
+ }
+
+ public Class getType() {
+ return Calibration.class;
+ }
+
+}
hps-java/src/main/java/org/lcsim/hps/conditions/demo
diff -N DemoDatabaseConditionsReader.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DemoDatabaseConditionsReader.java 18 Sep 2013 02:33:16 -0000 1.1
@@ -0,0 +1,148 @@
+package org.lcsim.hps.conditions.demo;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.*;
+
+import org.lcsim.conditions.ConditionsConverter;
+import org.lcsim.conditions.ConditionsManager;
+import org.lcsim.conditions.ConditionsReader;
+
+/**
+ * HPS-specific conditions reader. Wraps around any standard reader and adds an ability to
+ * fetch calibration data from the database.
+ * <p>
+ * TEMPORARY IMPLEMENTATION - DEMO.
+ *
+ * @author onoprien
+ * @version $Id: DemoDatabaseConditionsReader.java,v 1.1 2013/09/18 02:33:16 jeremy Exp $
+ */
+public class DemoDatabaseConditionsReader extends ConditionsReader {
+
+// -- Private parts : ----------------------------------------------------------
+
+ static ConditionsConverter _converterCalibration = new CalibrationConverter();
+
+ private final ConditionsReader _reader;
+
+ private String _detectorName;
+ private int _minRun = Integer.MAX_VALUE;
+ private int _maxRun = Integer.MIN_VALUE;
+
+ private final HashMap<String, byte[]> _propCache = new HashMap<String, byte[]>();
+
+// -- Construction and initialization : ----------------------------------------
+
+ public DemoDatabaseConditionsReader(ConditionsReader reader) {
+ _reader = reader;
+ }
+
+
+// -- Updating to a new detector/run : -----------------------------------------
+
+ public boolean update(ConditionsManager manager, String detectorName, int run) throws IOException {
+
+ if (_detectorName == null) {
+ _detectorName = detectorName;
+ } else {
+ if (!_detectorName.equals(detectorName)) throw new IllegalArgumentException();
+ }
+ if ( run <= _maxRun && run >= _minRun) return false;
+ manager.registerConditionsConverter(_converterCalibration);
+
+ _propCache.clear();
+
+ // Open connection to the db
+
+ Properties connectionProps = new Properties();
+ connectionProps.put("user", "rd_hps_cond_ro");
+ connectionProps.put("password", "2jumpinphotons.");
+ Connection con = null;
+ try {
+ con = DriverManager.getConnection("jdbc:mysql://mysql-node03.slac.stanford.edu:3306/", connectionProps);
+ } catch (SQLException x) {
+ throw new IOException("Failed to connect to database", x);
+ }
+
+ // Fetch whatever database-kept conditions should be accessible through ConditionsSet interface.
+ // This needs to be optimized once the db structure and data requirement are known.
+ // (can be templated if the db structure is standardized)
+
+ String query = "SELECT data_ident, runStart, runEnd FROM rd_hps_cond.conditions_test WHERE "+
+ "runStart <= "+ run +" AND runEnd >= "+ run + " AND level = 'PROD'";
+
+ Statement stmt = null;
+ int count = 0;
+ String data = null;
+ try {
+ stmt = con.createStatement();
+ ResultSet rs = stmt.executeQuery(query);
+ while (rs.next()) {
+ count++;
+ data = rs.getString(1);
+ _minRun = rs.getInt(2);
+ _maxRun = rs.getInt(3);
+ }
+ } catch (SQLException x) {
+ throw new IOException("Failed to execute query", x);
+ } finally {
+ if (stmt != null) {
+ try {
+ stmt.close();
+ } catch (SQLException x) {
+ throw new IOException("Failed to close statement", x);
+ }
+ }
+ }
+
+ // Need to get requirements from HPS people - how is this case supposed to be handled ?
+ if (count != 1) throw new IOException("Found "+ count +" valid calibrations");
+
+ Properties p = new Properties();
+ String[] d = data.trim().split(":");
+ try {
+ p.put("table", d[0]);
+ p.put("column", d[1]);
+ p.put("id", d[2]);
+ } catch (IndexOutOfBoundsException x) {
+ throw new IOException("Illegal data_ident format", x);
+ }
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ p.store(stream, null);
+ _propCache.put("calibration", stream.toByteArray());
+
+ // Close connection to the db
+
+ try {
+ con.close();
+ } catch (SQLException x) {
+ throw new IOException("Failed to close connection", x);
+ }
+
+ return true;
+ }
+
+// -- Implementing ConditionsReader : ------------------------------------------
+
+ public void close() throws IOException {
+ _reader.close();
+ }
+
+ public InputStream open(String name, String type) throws IOException {
+ byte[] ba = _propCache.get(name);
+ if (ba == null) {
+ return _reader.open(name, type);
+ } else {
+ return new ByteArrayInputStream(ba);
+ }
+ }
+
+// -----------------------------------------------------------------------------
+}
hps-java/src/main/java/org/lcsim/hps/conditions/demo
diff -N ExampleDriver.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ExampleDriver.java 18 Sep 2013 02:33:16 -0000 1.1
@@ -0,0 +1,68 @@
+package org.lcsim.hps.conditions.demo;
+
+import org.lcsim.conditions.CachedConditions;
+import org.lcsim.conditions.ConditionsManager;
+import org.lcsim.conditions.ConditionsManager.ConditionsSetNotFoundException;
+import org.lcsim.conditions.ConditionsSet;
+import org.lcsim.geometry.Detector;
+import org.lcsim.util.Driver;
+
+/**
+ *
+ * @author onoprien
+ */
+public class ExampleDriver extends Driver {
+
+ public ExampleDriver() {
+
+ }
+
+ protected void detectorChanged(Detector detector) {
+ System.out.println("detectorChanged: "+ detector.getDetectorName());
+ super.detectorChanged(detector);
+ ConditionsManager condMan = getConditionsManager();
+ try {
+ ConditionsSet set1 = condMan.getConditions("HadronCalibration/EMBarrel");
+ System.out.println("HadronCalibration/EMBarrel: hitEnergycut1 = "+ set1.getDouble("hitEnergycut1"));
+ } catch (ConditionsSetNotFoundException x) {
+ System.out.println("No HadronCalibration/EMBarrel: hitEnergycut1");
+ }
+ try {
+ ConditionsSet set1 = condMan.getConditions("SamplingFractions/EMBarrel");
+ System.out.println("1");
+ System.out.println("SamplingFractions/EMBarrel: samplingFraction = "+ set1.getDouble("samplingFraction"));
+ } catch (ConditionsSetNotFoundException x) {
+ System.out.println("No SamplingFractions/EMBarrel: samplingFraction");
+ }
+ try {
+ ConditionsSet set2 = condMan.getConditions("calibration");
+ System.out.println("calibration: "+ set2.getString("table") +" "+ set2.getString("column") +" "+ set2.getString("id"));
+ CachedConditions<Calibration> c = condMan.getCachedConditions(Calibration.class, "");
+ Calibration cal = c.getCachedData();
+ System.out.println("New calibration "+ cal);
+ } catch (ConditionsSetNotFoundException x) {
+ System.out.println("No calibration found "+ x);
+ }
+ }
+
+ protected void endOfData() {
+ System.out.println("endOfData");
+ super.endOfData();
+ }
+
+ protected void resume() {
+ System.out.println("resume");
+ super.resume();
+ }
+
+ protected void suspend() {
+ System.out.println("suspend");
+ super.suspend();
+ }
+
+ protected void startOfData() {
+ System.out.println("startOfData");
+ super.startOfData();
+ }
+
+}
hps-java/src/main/java/org/lcsim/hps/conditions/demo
diff -N package-info.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ package-info.java 18 Sep 2013 02:33:16 -0000 1.1
@@ -0,0 +1,20 @@
+/**
+ * Classes adding HPS-specific functionality to the <tt>org.lcsim</tt> conditions framework.
+ * <p>
+ * This is a demo implementation of the package. The goal was to minimize changes
+ * to the <tt>org.lcsim.conditions</tt> package required to support HPS extensions, and
+ * to guarantee backward compatibility.
+ * <p>
+ * To enable reading conditions from the HPS database:
+ * <ul>
+ * <li>Add the line "ConditionsReader: org.lcsim.hps.conditions.HPSConditionsReader" to
+ * the <tt>detector.properties</tt> file in your conditions ZIP file or directory.</li>
+ * <li>Install MySQL driver into your classpath.</li>
+ * </ul>
+ * Once the exact structure of the database and the calibration data to be retrieved is
+ * known, more efficient and robust implementation will be coded.
+ *
+ * @see org.lcsim.conditions
+ */
+package org.lcsim.hps.conditions.demo;
+
hps-java/src/main/java/org/lcsim/hps/conditions/svt
diff -N SvtCalibrationConstants.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ SvtCalibrationConstants.java 18 Sep 2013 02:33:16 -0000 1.1
@@ -0,0 +1,62 @@
+package org.lcsim.hps.conditions.svt;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Dummy class for holding SVT calibration data from conditions system.
+ *
+ * FIXME: SVT expert must provide actual implementation here.
+ *
+ * @author jeremym
+ * @version $Id: SvtCalibrationConstants.java,v 1.1 2013/09/18 02:33:16 jeremy Exp $
+ */
+public class SvtCalibrationConstants {
+
+ List<ChannelData> channelData = new ArrayList<ChannelData>();
+
+ /**
+ * Add a row of data.
+ * @param fpga The FPGA number.
+ * @param hybrid The hybrid number.
+ * @param channel The channel number.
+ * @param pedestal The pedestal value.
+ * @param noise The noise value.
+ */
+ void addData(int fpga, int hybrid, int channel, double pedestal, double noise) {
+ System.out.println(fpga + " " + hybrid + " " + channel + " " + pedestal + " " + noise);
+ ChannelData data = new ChannelData();
+ data.fpga = fpga;
+ data.hybrid = hybrid;
+ data.channel = channel;
+ data.pedestal = pedestal;
+ data.noise = noise;
+ channelData.add(data);
+ }
+
+ /**
+ * Class for holding one record of SVT calibration data.
+ */
+ public class ChannelData {
+ int fpga;
+ int hybrid;
+ int channel;
+ double pedestal;
+ double noise;
+ }
+
+ /**
+ * Convert data to string format.
+ * @return The calibration constants converted to human readable string.
+ */
+ public String toString() {
+ StringBuffer buff = new StringBuffer();
+ buff.append("fpga hybrid channel pedestal noise");
+ buff.append('\n');
+ for (ChannelData data : channelData) {
+ buff.append(data.fpga + " " + data.hybrid + " " + data.channel + " " + data.pedestal + " " + data.noise);
+ buff.append('\n');
+ }
+ return buff.toString();
+ }
+}
hps-java/src/main/java/org/lcsim/hps/conditions/svt
diff -N SvtCalibrationConverter.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ SvtCalibrationConverter.java 18 Sep 2013 02:33:16 -0000 1.1
@@ -0,0 +1,66 @@
+package org.lcsim.hps.conditions.svt;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.lcsim.conditions.ConditionsManager;
+import org.lcsim.conditions.ConditionsSet;
+import org.lcsim.hps.conditions.ConnectionParameters;
+import org.lcsim.hps.conditions.DatabaseConditionsConverter;
+
+public class SvtCalibrationConverter extends DatabaseConditionsConverter<SvtCalibrationConstants> {
+
+ public SvtCalibrationConverter(ConnectionParameters connectionParameters) {
+ super(connectionParameters);
+ }
+
+ public SvtCalibrationConstants getData(ConditionsManager manager, String name) {
+ Connection connection = null;
+ Statement statement = null;
+ SvtCalibrationConstants calibration = new SvtCalibrationConstants();
+ try {
+ // Get the meta data for this conditions set.
+ ConditionsSet cs = manager.getConditions(name);
+
+ // Connect to the database.
+ connection = this.getConnectionParameters().createConnection();
+
+ String query = "SELECT fpga, hybrid, channel, noise, pedestal FROM "
+ + getConnectionParameters().getDatabase() + "."
+ + cs.getString("table") + " WHERE "
+ + cs.getString("column") + " = "
+ + cs.getString("id");
+ statement = connection.createStatement();
+ ResultSet rs = statement.executeQuery(query);
+ while (rs.next()) {
+ int fpga = rs.getInt(1);
+ int hybrid = rs.getInt(2);
+ int channel = rs.getInt(3);
+ double noise = rs.getDouble(4);
+ double pedestal = rs.getDouble(5);
+ calibration.addData(fpga, hybrid, channel, pedestal, noise);
+ }
+ } catch (SQLException x) {
+ if (statement != null) {
+ try {
+ statement.close();
+ } catch (Exception xx) {
+ }
+ }
+ if (connection != null) {
+ try {
+ connection.close();
+ } catch (Exception xx) {
+ }
+ }
+ throw new RuntimeException("Database error", x);
+ }
+ return calibration;
+ }
+
+ public Class<SvtCalibrationConstants> getType() {
+ return SvtCalibrationConstants.class;
+ }
+}
hps-java/src/test/java/org/lcsim/hps/conditions
diff -u -r1.1 -r1.2
--- DatabaseConditionsReaderTest.java 17 Sep 2013 23:25:49 -0000 1.1
+++ DatabaseConditionsReaderTest.java 18 Sep 2013 02:33:16 -0000 1.2
@@ -4,13 +4,15 @@
import junit.framework.TestCase;
+import org.lcsim.conditions.CachedConditions;
import org.lcsim.conditions.ConditionsManager;
import org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException;
import org.lcsim.conditions.ConditionsSet;
+import org.lcsim.hps.conditions.svt.SvtCalibrationConstants;
/**
- * This class tests that the example DatabaseConditionsReader authored by Dima
- * actually works with a test detector.
+ * This class tests the DatabaseConditionsReader on dummy data.
+ *
* @author jeremym
*/
public class DatabaseConditionsReaderTest extends TestCase {
@@ -35,18 +37,24 @@
public void test() {
ConditionsManager manager = ConditionsManager.defaultInstance();
try {
- manager.setDetector(detectorName, runNumber);
+ System.out.println("Initializing conditions system ...");
+ System.out.println("detector: " + detectorName);
+ System.out.println("run: " + runNumber);
+ manager.setDetector(detectorName, runNumber);
} catch (ConditionsNotFoundException e) {
throw new RuntimeException(e);
- }
+ }
+
ConditionsSet conditions = manager.getConditions(conditionsSetName);
ps.println("Got conditions " + conditionsSetName + " of size " + conditions.size());
ps.println("table: " + conditions.getString("table"));
ps.println("column: " + conditions.getString("column"));
ps.println("id: " + conditions.getString("id"));
- CalibrationConverter calibrationConverter = new CalibrationConverter();
- Calibration calibration = calibrationConverter.getData(manager, null);
- ps.println("Fetched calibration conditions: ");
- ps.println(calibration.toString());
- }
-}
+
+ CachedConditions<SvtCalibrationConstants> c = manager.getCachedConditions(SvtCalibrationConstants.class, "svt_calibration");
+ SvtCalibrationConstants calibration = c.getCachedData();
+ System.out.println("Got calibration conditions ... ");
+ System.out.println(calibration);
+ System.out.println("Success!");
+ }
+}
\ No newline at end of file
hps-java/src/test/java/org/lcsim/hps/conditions/demo
diff -N DemoDatabaseConditionsReaderTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ DemoDatabaseConditionsReaderTest.java 18 Sep 2013 02:33:16 -0000 1.1
@@ -0,0 +1,54 @@
+package org.lcsim.hps.conditions.demo;
+
+import java.io.PrintStream;
+
+import junit.framework.TestCase;
+
+import org.lcsim.conditions.ConditionsManager;
+import org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException;
+import org.lcsim.conditions.ConditionsSet;
+import org.lcsim.hps.conditions.demo.CalibrationConverter;
+import org.lcsim.hps.conditions.demo.Calibration;
+
+/**
+ * This class tests that the example DatabaseConditionsReader authored by Dima
+ * actually works with a test detector.
+ * @author jeremym
+ */
+public class DemoDatabaseConditionsReaderTest extends TestCase {
+
+ /* Example detector from hps-detectors. */
+ private final String detectorName = "HPS-conditions-test";
+
+ /* Run number of conditions set. */
+ private final int runNumber = 0;
+
+ /* Name of conditions set. */
+ private final String conditionsSetName = "calibration";
+
+ /* Print output. */
+ private final PrintStream ps = System.out;
+
+ /**
+ * Create the manager, load the detector, and then get the conditions meta-data
+ * for the selected conditions set. Finally, use the sample converter to create
+ * a Calibration object from the database rows.
+ */
+ public void test() {
+ ConditionsManager manager = ConditionsManager.defaultInstance();
+ try {
+ manager.setDetector(detectorName, runNumber);
+ } catch (ConditionsNotFoundException e) {
+ throw new RuntimeException(e);
+ }
+ ConditionsSet conditions = manager.getConditions(conditionsSetName);
+ ps.println("Got conditions " + conditionsSetName + " of size " + conditions.size());
+ ps.println("table: " + conditions.getString("table"));
+ ps.println("column: " + conditions.getString("column"));
+ ps.println("id: " + conditions.getString("id"));
+ CalibrationConverter calibrationConverter = new CalibrationConverter();
+ Calibration calibration = calibrationConverter.getData(manager, null);
+ ps.println("Fetched calibration conditions: ");
+ ps.println(calibration.toString());
+ }
+}
CVSspam 0.2.12