Commit in hps-java/src on MAIN | |||
main/java/org/lcsim/hps/conditions/DatabaseConditionsReader.java | +7 | -4 | 1.5 -> 1.6 |
main/java/org/lcsim/hps/conditions/svt/ChannelConstantsCollection.java | +19 | added 1.1 | |
/SvtConditionsLoader.java | +63 | added 1.1 | |
/ChannelConstants.java | +6 | -6 | 1.2 -> 1.3 |
/SvtCalibrationConverter.java | +56 | -55 | 1.6 -> 1.7 |
/SvtConditions.java | +61 | -48 | 1.2 -> 1.3 |
test/java/org/lcsim/hps/conditions/DatabaseConditionsReaderTest.java | +5 | -7 | 1.4 -> 1.5 |
test/java/org/lcsim/hps/conditions/svt/SvtConditionsLoaderTest.java | +85 | added 1.1 | |
+302 | -120 |
restructure SVT conditions loading code; code will use svt_channels tables now too to retrieve the channel data by id
diff -u -r1.5 -r1.6 --- DatabaseConditionsReader.java 20 Sep 2013 21:56:24 -0000 1.5 +++ DatabaseConditionsReader.java 23 Sep 2013 23:09:10 -0000 1.6 @@ -38,7 +38,7 @@
* currently, but more will be added as they are created. * * @author jeremym
- * @version $Id: DatabaseConditionsReader.java,v 1.5 2013/09/20 21:56:24 jeremy Exp $
+ * @version $Id: DatabaseConditionsReader.java,v 1.6 2013/09/23 23:09:10 jeremy Exp $
*/ public class DatabaseConditionsReader extends ConditionsReader {
@@ -110,8 +110,6 @@
* @param run The run number. */ public boolean update(ConditionsManager manager, String detectorName, int run) throws IOException {
- - logger.info("update");
// Setup detector. if (this.detectorName == null) {
@@ -194,7 +192,7 @@
+ " AND run_end >= " + run;
- logger.info("query: " + query);
+ logger.info(query);
Statement statement = null; try {
@@ -244,11 +242,16 @@
* @param key The key of the conditions. (for instance "svt_calibration") * @param data The raw string data from the data_ident field in the database. * @throws IOException if the data format is wrong.
+ * @throws IllegalArgumentException if the key already exists.
*/ private void addDataKey(String key, String tableName, String columnName, int value) { logger.info("addDataKey: " + key + " => " + tableName + ":" + columnName + ":" + value);
+ if (propertyCache.get(key) != null) { + throw new IllegalArgumentException("Duplicate key found for condition: " + key); + } +
Properties p = new Properties(); p.put("table", tableName); p.put("column", columnName);
diff -N ChannelConstantsCollection.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ ChannelConstantsCollection.java 23 Sep 2013 23:09:10 -0000 1.1 @@ -0,0 +1,19 @@
+package org.lcsim.hps.conditions.svt; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class ChannelConstantsCollection extends LinkedHashMap<Integer,ChannelConstants> { + + ChannelConstants getChannelConstants(Integer channelId) { + return this.get(channelId); + } + + public String toString() { + StringBuffer buff = new StringBuffer(); + for (Map.Entry<Integer,ChannelConstants> entry : entrySet()) { + buff.append(entry.getKey() + " => " + entry.getValue()); + } + return buff.toString(); + } +}
diff -N SvtConditionsLoader.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ SvtConditionsLoader.java 23 Sep 2013 23:09:10 -0000 1.1 @@ -0,0 +1,63 @@
+package org.lcsim.hps.conditions.svt; + +import java.util.Map.Entry; + +import org.lcsim.conditions.CachedConditions; +import org.lcsim.conditions.ConditionsManager; +import org.lcsim.detector.tracker.silicon.SiSensor; +import org.lcsim.hps.recon.tracking.SvtUtils; +import org.lcsim.hps.util.Pair; + +public class SvtConditionsLoader { + + SvtConditions conditions = null; + + /** + * The name of the conditions to retrieve. + * This is not necessarily the same as the database table name. + */ + private static final String CALIBRATIONS = "svt_calibrations"; + + void load() { + + // Get the default conditions manager. This needs to be setup beforehand with the detector. + ConditionsManager manager = ConditionsManager.defaultInstance(); + + // Reset the conditions object. + conditions = new SvtConditions(); + + // Get the SVT calibrations from the database. + CachedConditions<ChannelConstantsCollection> c = + manager.getCachedConditions(ChannelConstantsCollection.class, CALIBRATIONS); + ChannelConstantsCollection channelData = c.getCachedData(); + + // Get SvtUtils reference. + SvtUtils util = SvtUtils.getInstance(); + + // Loop over all the channel data retrieved from the database. + for (Entry<Integer,ChannelConstants> entry : channelData.entrySet()) { + + // Get the constants for a single channel. + ChannelConstants channel = entry.getValue(); + + // Find the associated sensor for the FPGA and hybrid numbers. + Pair<Integer, Integer> daqPair = new Pair<Integer, Integer>(channel.getFpga(), channel.getHybrid()); + SiSensor sensor = util.getSensor(daqPair); + + // Sensor must exist! + if (sensor == null) { + throw new RuntimeException("SiSensor not found for fpga <" + channel.getFpga() + "> and hybrid <" + channel.getHybrid() + "> !"); + } + + // Set the noise value. + conditions.setNoise(sensor, channel.getChannel(), channel.getNoise()); + + // Set the pedestal value. + conditions.setPedestal(sensor, channel.getChannel(), channel.getPedestal()); + } + } + + SvtConditions getSvtConditions() { + return conditions; + } +}
diff -u -r1.2 -r1.3 --- ChannelConstants.java 20 Sep 2013 21:55:09 -0000 1.2 +++ ChannelConstants.java 23 Sep 2013 23:09:10 -0000 1.3 @@ -3,7 +3,7 @@
/** * * @author Omar Moreno <[log in to unmask]>
- * @version $Id: ChannelConstants.java,v 1.2 2013/09/20 21:55:09 jeremy Exp $
+ * @version $Id: ChannelConstants.java,v 1.3 2013/09/23 23:09:10 jeremy Exp $
*/ public class ChannelConstants {
@@ -108,11 +108,11 @@
@Override public String toString() { StringBuffer buffer = new StringBuffer();
- buffer.append("FPGA: " + this.getFpga() + "\n"); - buffer.append("Hybrid: " + this.getHybrid() + "\n"); - buffer.append("Channel: " + this.getChannel() + "\n"); - buffer.append("Pedestal: " + this.getPedestal() + "\n"); - buffer.append("Noise: " + this.getNoise() + "\n");
+ buffer.append("fpga: " + this.getFpga() + ", "); + buffer.append("hybrid: " + this.getHybrid() + ", "); + buffer.append("channel: " + this.getChannel() + ", "); + buffer.append("pedestal: " + this.getPedestal() + ", "); + buffer.append("noise: " + this.getNoise() + '\n');
return buffer.toString(); } }
diff -u -r1.6 -r1.7 --- SvtCalibrationConverter.java 20 Sep 2013 21:55:09 -0000 1.6 +++ SvtCalibrationConverter.java 23 Sep 2013 23:09:10 -0000 1.7 @@ -12,89 +12,90 @@
/** * Read from conditions database and create SVT calibration data.
- * - * @author jeremym - * @version $Id: SvtCalibrationConverter.java,v 1.6 2013/09/20 21:55:09 jeremy Exp $
+ * + * @author Omar Moreno <[log in to unmask]> + * @version $Id: SvtCalibrationConverter.java,v 1.7 2013/09/23 23:09:10 jeremy Exp $
*/
-public class SvtCalibrationConverter extends DatabaseConditionsConverter<ChannelConstants> { - - Connection connection = null; - Statement statement = null; - String database; - ResultSet resultSet = null; - ConditionsSet conditionsSet = null; - - private boolean hasNext = true; - private boolean connectionEstablished = false; -
+public class SvtCalibrationConverter extends DatabaseConditionsConverter<ChannelConstantsCollection> { + + /** + * Class constructor. + */
public SvtCalibrationConverter() {
- // Connect to the database. - connection = ConnectionManager.createConnection(); - database = ConnectionManager.getConnectionParameters().getDatabase();
}
- public ChannelConstants getData(ConditionsManager manager, String name) {
+ /** + * Get the channel constants data by name. + * @param manager The current conditions manager. + * @param name The name of the conditions set. + * @return The channel constants data. + */ + public ChannelConstantsCollection getData(ConditionsManager manager, String name) {
ChannelConstants constants = null;
+ ChannelConstantsCollection collection = new ChannelConstantsCollection(); + + Statement statement = null; + ResultSet resultSet = null; + Connection connection = null; +
try {
- - if (!connectionEstablished) {
- conditionsSet = manager.getConditions(name);
+ ConditionsSet conditionsSet = manager.getConditions(name);
+ String database = ConnectionManager.getConnectionParameters().getDatabase();
+ connection = ConnectionManager.createConnection();
+
+ String query = "SELECT channel_id, fpga, hybrid, channel, noise, pedestal FROM "
+ + database + "." + conditionsSet.getString("table") + ", "
+ + database + "." + "svt_channels"
+ + " WHERE "
+ + conditionsSet.getString("column") + " = " + conditionsSet.getString("id")
+ + " AND svt_channels.id = " + conditionsSet.getString("table") + ".channel_id "
+ + "ORDER BY svt_channels.id ASC";
+
+ System.out.println(query);
- String query = "SELECT fpga, hybrid, channel, noise, pedestal FROM " - + database - + "." - + conditionsSet.getString("table") - + " WHERE " - + conditionsSet.getString("column") - + " = " - + conditionsSet.getString("id"); - - statement = connection.createStatement(); - resultSet = statement.executeQuery(query); - hasNext = resultSet.next(); - }
+ statement = connection.createStatement(); + resultSet = statement.executeQuery(query); + + while(resultSet.next()) {
- int fpga = resultSet.getInt(1); - int hybrid = resultSet.getInt(2); - int channel = resultSet.getInt(3); - double noise = resultSet.getDouble(4); - double pedestal = resultSet.getDouble(5);
+ int channelId = resultSet.getInt(1); + int fpga = resultSet.getInt(2); + int hybrid = resultSet.getInt(3); + int channel = resultSet.getInt(4); + double noise = resultSet.getDouble(5); + double pedestal = resultSet.getDouble(6);
- constants = new ChannelConstants(fpga, hybrid, channel, pedestal, - noise, Double.NaN, Double.NaN, null);
+ constants = new ChannelConstants(fpga, hybrid, channel, pedestal, noise, Double.NaN, Double.NaN, null);
- resultSet.next(); -
+ collection.put(channelId, constants); + }
} catch (SQLException x) {
+ throw new RuntimeException("Database error", x); + } finally {
if (statement != null) { try { statement.close(); } catch (Exception xx) { }
- }
+ }
if (connection != null) { try { connection.close();
- } catch (Exception xx) {
+ } catch (Exception x) {
} }
- throw new RuntimeException("Database error", x);
}
- return constants;
+ return collection;
} /** * Get the type handled by this converter. *
- * @return The type handled by this converter.
+ * @return The type handled by this converter which is <code>ChannelConstantsCollection</code>.
*/
- public Class<ChannelConstants> getType() { - return ChannelConstants.class; - } - - public boolean hasNext() { - return hasNext; - }
+ public Class<ChannelConstantsCollection> getType() { + return ChannelConstantsCollection.class; + }
}
diff -u -r1.2 -r1.3 --- SvtConditions.java 20 Sep 2013 21:55:09 -0000 1.2 +++ SvtConditions.java 23 Sep 2013 23:09:10 -0000 1.3 @@ -2,66 +2,45 @@
import java.util.HashMap; import java.util.Map;
-import java.util.List;
-import org.lcsim.conditions.ConditionsConverter; -import org.lcsim.conditions.ConditionsManager;
import org.lcsim.detector.tracker.silicon.SiSensor;
-import org.lcsim.hps.recon.tracking.SvtUtils; -import org.lcsim.hps.util.Pair; -
/** * * @author Omar Moreno <[log in to unmask]>
- * @version $Id: SvtConditions.java,v 1.2 2013/09/20 21:55:09 jeremy Exp $
+ * @version $Id: SvtConditions.java,v 1.3 2013/09/23 23:09:10 jeremy Exp $
*/ public class SvtConditions {
- private static Map<SiSensor, double[]> noiseMap = new HashMap<SiSensor, double[]>(); - private static Map<SiSensor, double[]> pedestalMap = new HashMap<SiSensor, double[]>(); - private static Map<SiSensor, double[]> tpMap = new HashMap<SiSensor, double[]>(); - private static Map<SiSensor, Double> t0ShiftMap = new HashMap<SiSensor, Double>();
+ private Map<SiSensor, double[]> noiseMap = new HashMap<SiSensor, double[]>(); + private Map<SiSensor, double[]> pedestalMap = new HashMap<SiSensor, double[]>(); + private Map<SiSensor, double[]> tpMap = new HashMap<SiSensor, double[]>(); + private Map<SiSensor, Double> t0ShiftMap = new HashMap<SiSensor, Double>();
/** * Ctor */
- private SvtConditions() {
+ protected SvtConditions() {
+ }
+
+ public void setNoise(SiSensor sensor, int channel, Double noise) {
+ if (noiseMap.get(sensor) == null)
+ noiseMap.put(sensor, new double[640]);
+ noiseMap.get(sensor)[channel] = noise;
}
- - public static void loadSvtConditions(ConditionsConverter<ChannelConstants> converter) { - - ConditionsManager conditionsManager = ConditionsManager.defaultInstance(); - - SvtCalibrationConverter calibrationsConverter = new SvtCalibrationConverter(); - ChannelConstants channelConstants; - - while (calibrationsConverter.hasNext()) { - - channelConstants = calibrationsConverter.getData(conditionsManager, "svt_calibrations"); - - int fpga = channelConstants.getFpga(); - int hybrid = channelConstants.getHybrid(); - int channel = channelConstants.getChannel(); - - Pair<Integer, Integer> daqPair = new Pair<Integer, Integer>(fpga, - hybrid); - SiSensor sensor = SvtUtils.getInstance().getSensor(daqPair); - - if (noiseMap.get(sensor) == null) - noiseMap.put(sensor, new double[640]); - if (pedestalMap.get(sensor) == null) - pedestalMap.put(sensor, new double[640]); - if (tpMap.get(sensor) == null) - tpMap.put(sensor, new double[640]); - - noiseMap.get(sensor)[channel] = channelConstants.getNoise(); - pedestalMap.get(sensor)[channel] = channelConstants.getPedestal(); - tpMap.get(sensor)[channel] = channelConstants.getShapingTime(); - - }
+ + public void setPedestal(SiSensor sensor, int channel, double pedestal) { + if (pedestalMap.get(sensor) == null) + pedestalMap.put(sensor, new double[640]); + pedestalMap.get(sensor)[channel] = pedestal;
}
-
+ + public void setShapingTime(SiSensor sensor, int channel, double shapingTime) { + if (tpMap.get(sensor) == null) + tpMap.put(sensor, new double[640]); + tpMap.put(sensor, new double[640]); + } +
/** * * Get the noise for a given channel on a sensor.
@@ -70,7 +49,7 @@
* @param channel * @return noise */
- public static Double getNoise(SiSensor sensor, int channel) {
+ public Double getNoise(SiSensor sensor, int channel) {
double[] noise = noiseMap.get(sensor); if (noise == null || channel < 0 || channel > 640) { return null;
@@ -87,7 +66,7 @@
* @param channel * @return pedestal */
- public static Double getPedestal(SiSensor sensor, int channel) {
+ public Double getPedestal(SiSensor sensor, int channel) {
double[] pedestal = pedestalMap.get(sensor); if (pedestal == null || channel < 0 || channel > 640) { return null;
@@ -104,7 +83,7 @@
* @param channel * @return shaping time */
- public static Double getShapingTime(SiSensor sensor, int channel) {
+ public Double getShapingTime(SiSensor sensor, int channel) {
double[] tp = tpMap.get(sensor); if (tp == null || channel < 0 || channel > 640) { return null;
@@ -112,4 +91,38 @@
return tp[channel]; } }
+ + /* + public static void loadSvtConditions(ConditionsConverter<ChannelConstants> converter) { + + ConditionsManager conditionsManager = ConditionsManager.defaultInstance(); + + SvtCalibrationConverter calibrationsConverter = new SvtCalibrationConverter(); + ChannelConstants channelConstants; + + while (calibrationsConverter.hasNext()) { + + channelConstants = calibrationsConverter.getData(conditionsManager, "svt_calibrations"); + + int fpga = channelConstants.getFpga(); + int hybrid = channelConstants.getHybrid(); + int channel = channelConstants.getChannel(); + + Pair<Integer, Integer> daqPair = new Pair<Integer, Integer>(fpga, hybrid); + SiSensor sensor = SvtUtils.getInstance().getSensor(daqPair); + + if (noiseMap.get(sensor) == null) + noiseMap.put(sensor, new double[640]); + if (pedestalMap.get(sensor) == null) + pedestalMap.put(sensor, new double[640]); + if (tpMap.get(sensor) == null) + tpMap.put(sensor, new double[640]); + + noiseMap.get(sensor)[channel] = channelConstants.getNoise(); + pedestalMap.get(sensor)[channel] = channelConstants.getPedestal(); + tpMap.get(sensor)[channel] = channelConstants.getShapingTime(); + + } + } + */
}
diff -u -r1.4 -r1.5 --- DatabaseConditionsReaderTest.java 20 Sep 2013 08:52:42 -0000 1.4 +++ DatabaseConditionsReaderTest.java 23 Sep 2013 23:09:10 -0000 1.5 @@ -9,7 +9,7 @@
import org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException; import org.lcsim.conditions.ConditionsSet; import org.lcsim.hps.conditions.svt.ChannelConstants;
-import org.lcsim.hps.conditions.svt.SvtCalibrationConstants;
+import org.lcsim.hps.conditions.svt.ChannelConstantsCollection;
/** * This class tests the DatabaseConditionsReader on dummy data.
@@ -25,7 +25,7 @@
private final int runNumber = 0; /** Name of conditions set. */
- private final String conditionsSetName = "svt_calibration";
+ private final String conditionsSetName = "svt_calibrations";
/** Print output. */ private final PrintStream ps = System.out;
@@ -38,9 +38,6 @@
public void test() { ConditionsManager manager = ConditionsManager.defaultInstance(); try {
- 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);
@@ -52,10 +49,11 @@
ps.println("column: " + conditions.getString("column")); ps.println("id: " + conditions.getString("id"));
- CachedConditions<ChannelConstants> c = manager.getCachedConditions(ChannelConstants.class, conditionsSetName); - ChannelConstants calibration = c.getCachedData();
+ CachedConditions<ChannelConstantsCollection> c = manager.getCachedConditions(ChannelConstantsCollection.class, conditionsSetName); + ChannelConstantsCollection calibration = c.getCachedData();
System.out.println("Got calibration conditions ... "); System.out.println(calibration); System.out.println("Success!");
+
} }
\ No newline at end of file
diff -N SvtConditionsLoaderTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ SvtConditionsLoaderTest.java 23 Sep 2013 23:09:10 -0000 1.1 @@ -0,0 +1,85 @@
+package org.lcsim.hps.conditions.svt; + +import java.util.List; + +import junit.framework.TestCase; + +import org.lcsim.conditions.ConditionsManager; +import org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException; +import org.lcsim.detector.tracker.silicon.SiSensor; +import org.lcsim.geometry.Detector; +import org.lcsim.hps.recon.tracking.SvtUtils; +import org.lcsim.util.loop.LCSimConditionsManagerImplementation; + +/** + * This class tests that <code>SvtConditions</code> gets valid conditions from the database. + * + * @author Jeremy McCormick <[log in to unmask]> + */ +public class SvtConditionsLoaderTest extends TestCase { + + /** An example detector from hps-detectors. */ + private static final String detectorName = "HPS-conditions-test"; + + /** The run number of the conditions set in the database. */ + private static final int runNumber = 0; + + /** Maximum channel number in a sensor. */ + private static final int MAX_CHANNEL = 639; + + /** The total number of SVT channels on the detector. */ + private static final int TOTAL_CHANNELS = 12800; + + /** + * Load and check SVT channel conditions. + */ + public void test() { + + // Setup the conditions manager. + ConditionsManager.setDefaultConditionsManager(new LCSimConditionsManagerImplementation()); + ConditionsManager manager = ConditionsManager.defaultInstance(); + try { + manager.setDetector(detectorName, runNumber); + } catch (ConditionsNotFoundException e) { + throw new RuntimeException(e); + } + + // Now we need to setup the Detector for the conditions loader. + Detector detector = + manager.getCachedConditions(Detector.class, "compact.xml").getCachedData(); + SvtUtils.getInstance().setup(detector); + + // Test that the loader returns valid conditions. + SvtConditionsLoader loader = new SvtConditionsLoader(); + loader.load(); + SvtConditions conditions = loader.getSvtConditions(); + assertNotNull(conditions); + + // Get all the sensors on the detector. + List<SiSensor> sensors = detector.getDetectorElement().findDescendants(SiSensor.class); + int totalChannelsFound = 0; + + // Loop over sensors. + for (SiSensor sensor : sensors) { + System.out.println(sensor.getName()); + // Loop over channels in sensor. + for (int channel=0; channel <= MAX_CHANNEL; channel++ ) { + + // Get data for channel. + double noise = conditions.getNoise(sensor, channel); + double pedestal = conditions.getPedestal(sensor, channel); + + // Print channel data. + System.out.println(" channel: " + channel + ", noise: " + noise + ", pedestal: " + pedestal); + + // Assert values are not zero. + assertTrue("Noise value is zero.", noise != 0.0); + assertTrue("Pedestal valud is zero.", pedestal != 0.0); + + ++totalChannelsFound; + } + } + System.out.println("got conditions for " + totalChannelsFound + " channels"); + assertEquals("Wrong number of total channels.", TOTAL_CHANNELS, totalChannelsFound); + } +}
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