hps-java/src/main/java/org/lcsim/hps/conditions/ecal
diff -N EcalConditionsLoader.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ EcalConditionsLoader.java 16 Oct 2013 23:14:34 -0000 1.8
@@ -0,0 +1,67 @@
+package org.lcsim.hps.conditions.ecal;
+
+import java.util.List;
+
+import org.lcsim.detector.converter.compact.EcalCrystal;
+import org.lcsim.geometry.Detector;
+
+/**
+ * Load {@link SvtConditions} data onto <code>HpsSiSensor</code> objects.
+ * @author Jeremy McCormick <[log in to unmask]>
+ */
+public class EcalConditionsLoader {
+
+ /**
+ * Load ECal conditions data onto a detector object.
+ * @param detector The detector object.
+ * @param conditions The conditions object.
+ */
+ public void load(Detector detector, EcalConditions conditions) {
+
+ // Find EcalCrystal objects.
+ List<EcalCrystal> crystals = detector.getDetectorElement().findDescendants(EcalCrystal.class);
+
+ // Get the full channel map created by the conditions system.
+ EcalChannelMap channelMap = conditions.getChannelMap();
+
+ // Loop over crystals.
+ for (EcalCrystal crystal : crystals) {
+
+ // Reset possibly existing conditions data.
+ crystal.reset();
+
+ // Find the corresponding entry in the channel map for this crystal.
+ EcalChannel channel = channelMap.find(crystal.getX(), crystal.getY());
+ if (channel == null) {
+ throw new RuntimeException("EcalChannel not found for crystal: " + crystal.getName());
+ }
+
+ // Set the crate.
+ crystal.setCrate(channel.getCrate());
+
+ // Set the slot.
+ crystal.setSlot(channel.getSlot());
+
+ // Set the channel number.
+ crystal.setChannel(channel.getChannel());
+
+ // Get the channel constants.
+ EcalChannelConstants constants = conditions.getChannelConstants(channel);
+ if (constants == null) {
+ throw new RuntimeException("EcalChannelConstants not found for crystal: " + crystal.getName());
+ }
+
+ // Set bad channel.
+ crystal.setBadChannel(constants.isBadChannel());
+
+ // Set pedestal.
+ crystal.setPedestal(constants.getCalibration().getPedestal());
+
+ // Set noise.
+ crystal.setNoise(constants.getCalibration().getNoise());
+
+ // Set gain.
+ crystal.setGain(constants.getGain().getGain());
+ }
+ }
+}
\ No newline at end of file
hps-java/src/test/java/org/lcsim/hps/conditions/ecal
diff -N EcalConditionsLoaderTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ EcalConditionsLoaderTest.java 16 Oct 2013 23:14:34 -0000 1.6
@@ -0,0 +1,110 @@
+package org.lcsim.hps.conditions.ecal;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.lcsim.conditions.ConditionsManager;
+import org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException;
+import org.lcsim.detector.converter.compact.EcalCrystal;
+import org.lcsim.geometry.Detector;
+import org.lcsim.hps.conditions.ConnectionManager;
+import org.lcsim.util.loop.LCSimConditionsManagerImplementation;
+
+/**
+ * This test loads ECal conditions data onto the detector and checks the results
+ * for basic validity.
+ * @author Jeremy McCormick <[log in to unmask]>
+ */
+public class EcalConditionsLoaderTest 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 = 1351;
+
+ /** Expected number of crystals. */
+ private static final int CRYSTAL_COUNT_ANSWER = 442;
+
+ /** Expected number of bad channels. */
+ private static final int BAD_CHANNELS_ANSWER = 44;
+
+ /** Valid minimum and maximum values for DAQ setup parameters. */
+ private static final int MIN_CRATE_ANSWER = 1;
+ private static final int MAX_CRATE_ANSWER = 2;
+ private static final int MIN_SLOT_ANSWER = 3;
+ private static final int MAX_SLOT_ANSWER = 19;
+ private static final int MIN_CHANNEL_ANSWER = 0;
+ private static final int MAX_CHANNEL_ANSWER = 19;
+
+ /**
+ * Load SVT conditions data onto the detector and perform basic checks afterwards.
+ */
+ 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);
+ }
+
+ // Get the detector.
+ Detector detector = manager.getCachedConditions(Detector.class, "compact.xml").getCachedData();
+
+ // Get conditions.
+ EcalConditions conditions = manager.getCachedConditions(EcalConditions.class, null).getCachedData();
+
+ // Load conditions onto detector.
+ EcalConditionsLoader loader = new EcalConditionsLoader();
+ loader.load(detector, conditions);
+
+ // Get crystals from detector.
+ List<EcalCrystal> crystals = detector.getDetectorElement().findDescendants(EcalCrystal.class);
+
+ // Check number of crystals.
+ assertEquals("Wrong number of crystals.", CRYSTAL_COUNT_ANSWER, crystals.size());
+
+ // Counter for bad channels.
+ int badChannelCount = 0;
+
+ // Loop over crystals.
+ for (EcalCrystal crystal : crystals) {
+
+ // Get DAQ information.
+ int crate = crystal.getCrate();
+ int slot = crystal.getSlot();
+ int channel = crystal.getChannel();
+
+ // Check basic validity of DAQ setup information.
+ assertTrue("Crate number is out of range.", crate >= MIN_CRATE_ANSWER && crate <= MAX_CRATE_ANSWER);
+ assertTrue("Slot number is out of range.", slot >= MIN_SLOT_ANSWER && slot <= MAX_SLOT_ANSWER);
+ assertTrue("Channel number is out of range.", MIN_CHANNEL_ANSWER >=0 && channel <= MAX_CHANNEL_ANSWER);
+
+ // Get time dependent conditions.
+ double pedestal = crystal.getPedestal();
+ double noise = crystal.getNoise();
+ double gain = crystal.getGain();
+ boolean badChannel = crystal.isBadChannel();
+
+ // Check basic validity of conditions. They should all be non-zero.
+ assertTrue("Pedestal value is zero.", pedestal != 0);
+ assertTrue("Noise value is zero.", noise != 0);
+ assertTrue("Gain value is zero.", gain != 0);
+
+ // Increment bad channel count.
+ if (badChannel)
+ ++badChannelCount;
+
+ }
+
+ // Check total number of bad channels.
+ assertEquals("Wrong number of bad channels.", BAD_CHANNELS_ANSWER, badChannelCount);
+
+ // Cleanup the database connection.
+ ConnectionManager.getConnectionManager().disconnect();
+ }
+}