Commit in hps-java/src/main/java/org/lcsim/hps/conditions on MAIN | |||
Calibration.java | +72 | added 1.1 | |
CalibrationConverter.java | +61 | added 1.1 | |
ExampleDriver.java | +68 | added 1.1 | |
HPSConditionsReader.java | +147 | added 1.1 | |
package-info.java | +7 | added 1.1 | |
+355 |
conditions package
diff -N Calibration.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ Calibration.java 19 Mar 2012 20:58:10 -0000 1.1 @@ -0,0 +1,72 @@
+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(); + } + +}
diff -N CalibrationConverter.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ CalibrationConverter.java 19 Mar 2012 20:58:10 -0000 1.1 @@ -0,0 +1,61 @@
+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; + } + +}
diff -N ExampleDriver.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ExampleDriver.java 19 Mar 2012 20:58:11 -0000 1.1 @@ -0,0 +1,68 @@
+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(); + } + +}
diff -N HPSConditionsReader.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ HPSConditionsReader.java 19 Mar 2012 20:58:11 -0000 1.1 @@ -0,0 +1,147 @@
+package org.lcsim.hps.conditions; + +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: HPSConditionsReader.java,v 1.1 2012/03/19 20:58:11 onoprien Exp $ + */ +public class HPSConditionsReader 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 HPSConditionsReader(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); + } + } + +// ----------------------------------------------------------------------------- +}
diff -N package-info.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ package-info.java 19 Mar 2012 20:58:11 -0000 1.1 @@ -0,0 +1,7 @@
+/** + * Classes adding HPS-specific functionality to the <tt>org.lcsim</tt> conditions framework. + * + * @see org.lcsim.conditions + */ +package org.lcsim.hps.conditions; +
Use REPLY-ALL to reply to list
To unsubscribe from the LCD-CVS list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=LCD-CVS&A=1