Author: omoreno Date: Mon Oct 27 11:20:27 2014 New Revision: 1309 Log: Add a generic method that allows obtaining any type of conditions collection from the conditions database. Remove the getter methods that were being used before. Modified: java/branches/hps_java_trunk_HPSJAVA-255/conditions/src/main/java/org/hps/conditions/svt/AbstractSvtConditionsConverter.java Modified: java/branches/hps_java_trunk_HPSJAVA-255/conditions/src/main/java/org/hps/conditions/svt/AbstractSvtConditionsConverter.java ============================================================================= --- java/branches/hps_java_trunk_HPSJAVA-255/conditions/src/main/java/org/hps/conditions/svt/AbstractSvtConditionsConverter.java (original) +++ java/branches/hps_java_trunk_HPSJAVA-255/conditions/src/main/java/org/hps/conditions/svt/AbstractSvtConditionsConverter.java Mon Oct 27 11:20:27 2014 @@ -1,12 +1,4 @@ package org.hps.conditions.svt; - -import static org.hps.conditions.TableConstants.SVT_BAD_CHANNELS; -import static org.hps.conditions.TableConstants.SVT_CALIBRATIONS; -import static org.hps.conditions.TableConstants.SVT_CHANNELS; -import static org.hps.conditions.TableConstants.SVT_DAQ_MAP; -import static org.hps.conditions.TableConstants.SVT_GAINS; -import static org.hps.conditions.TableConstants.SVT_PULSE_PARAMETERS; -import static org.hps.conditions.TableConstants.SVT_TIME_SHIFTS; import org.lcsim.conditions.ConditionsConverter; import org.lcsim.conditions.ConditionsManager; @@ -14,7 +6,6 @@ import org.hps.conditions.TableMetaData; import org.hps.conditions.svt.SvtBadChannel.SvtBadChannelCollection; import org.hps.conditions.svt.SvtCalibration.SvtCalibrationCollection; -import org.hps.conditions.svt.SvtChannel.SvtChannelCollection; import org.hps.conditions.svt.SvtGain.SvtGainCollection; import org.hps.conditions.svt.SvtShapeFitParameters.SvtShapeFitParametersCollection; import org.hps.conditions.svt.SvtT0Shift.SvtT0ShiftCollection; @@ -26,13 +17,10 @@ * * @author Omar Moreno <[log in to unmask]> * - * @param <T> SVT conditions object type + * @param <T extends AbstractSvtConditions> SVT conditions object type */ public abstract class AbstractSvtConditionsConverter<T extends AbstractSvtConditions> implements ConditionsConverter<T> { - private TableMetaData metaData = null; - private String tableName = null; - protected T conditions; /** @@ -43,146 +31,67 @@ public T getData(ConditionsManager manager, String name){ DatabaseConditionsManager dbConditionsManager = (DatabaseConditionsManager) manager; - - SvtChannelCollection channels = this.getSvtChannelMap(dbConditionsManager); - - // Create the SVT conditions object to use to encapsulate SVT condition collections - conditions.setChannelMap(channels); - - SvtCalibrationCollection calibrations = this.getCalibrations(dbConditionsManager); + + // Get the SVT calibrations (baseline, noise) from the conditions database + SvtCalibrationCollection calibrations = this.getCollection(SvtCalibrationCollection.class, dbConditionsManager); for (SvtCalibration calibration : calibrations.getObjects()) { - SvtChannel channel = conditions.getChannelMap().findChannel(calibration.getChannelID()); + AbstractSvtChannel channel = conditions.getChannelMap().findChannel(calibration.getChannelID()); conditions.getChannelConstants(channel).setCalibration(calibration); } - SvtShapeFitParametersCollection shapeFitParametersCollection = this.getShapeFitParameters(dbConditionsManager); + // Get the Channel pulse fit parameters from the conditions database + SvtShapeFitParametersCollection shapeFitParametersCollection + = this.getCollection(SvtShapeFitParametersCollection.class, dbConditionsManager); for (SvtShapeFitParameters shapeFitParameters : shapeFitParametersCollection.getObjects()) { - SvtChannel channel = conditions.getChannelMap().findChannel(shapeFitParameters.getChannelID()); + AbstractSvtChannel channel = conditions.getChannelMap().findChannel(shapeFitParameters.getChannelID()); conditions.getChannelConstants(channel).setShapeFitParameters(shapeFitParameters); } - + + // Get the bad channels from the conditions database. If there aren't any bad channels, + // notify the user and move on. try { - - SvtBadChannelCollection badChannels = this.getBadChannels(dbConditionsManager); + SvtBadChannelCollection badChannels = this.getCollection(SvtBadChannelCollection.class, dbConditionsManager); for (SvtBadChannel badChannel : badChannels.getObjects()) { - SvtChannel channel = conditions.getChannelMap().findChannel(badChannel.getChannelId()); + AbstractSvtChannel channel = conditions.getChannelMap().findChannel(badChannel.getChannelId()); conditions.getChannelConstants(channel).setBadChannel(true); } } catch (RuntimeException e) { System.out.println("[ " + conditions.getClass().getSimpleName() + "]: A set of bad channels were not found!"); } - SvtGainCollection channelGains = this.getChannelGains(dbConditionsManager); + // Get the gains and offsets from the conditions database + SvtGainCollection channelGains = this.getCollection(SvtGainCollection.class, dbConditionsManager); for (SvtGain channelGain : channelGains.getObjects()) { int channelId = channelGain.getChannelID(); - SvtChannel channel = conditions.getChannelMap().findChannel(channelId); + AbstractSvtChannel channel = conditions.getChannelMap().findChannel(channelId); conditions.getChannelConstants(channel).setGain(channelGain); } - SvtT0ShiftCollection t0Shifts = this.getT0Shits(dbConditionsManager); - conditions.setTimeShifts(t0Shifts); + // Get the collection of T0 shifts from the conditions database + SvtT0ShiftCollection t0Shifts = this.getCollection(SvtT0ShiftCollection.class, dbConditionsManager); + conditions.setT0Shifts(t0Shifts); return conditions; } - - protected SvtChannelCollection getSvtChannelMap(DatabaseConditionsManager dbConditionsManager){ - - // Get the table name containing the SVT channel map from the - // database configuration. If it doesn't exist, use the default value. - metaData = dbConditionsManager.findTableMetaData(SvtChannelCollection.class); - if(metaData != null){ - tableName = metaData.getTableName(); - } else { - tableName = SVT_CHANNELS; - } - // Get the SVT channel map from the conditions database - SvtChannelCollection channels - = dbConditionsManager.getCachedConditions(SvtChannelCollection.class, tableName).getCachedData(); - - return channels; - } - - protected SvtCalibrationCollection getCalibrations(DatabaseConditionsManager dbConditionsManager){ - - // Get the table name containing the SVT calibrations (baseline, noise) - // from the database configuration. If it doesn't exist, use the - // default value. - metaData = dbConditionsManager.findTableMetaData(SvtCalibrationCollection.class); - if(metaData != null){ - tableName = metaData.getTableName(); - } else { - tableName = SVT_CALIBRATIONS; - } - // Get the calibrations from the conditions database - SvtCalibrationCollection calibrations = dbConditionsManager.getCachedConditions(SvtCalibrationCollection.class, tableName).getCachedData(); - - return calibrations; - } - - protected SvtShapeFitParametersCollection getShapeFitParameters(DatabaseConditionsManager dbConditionsManager){ - // Get the table name containing the SVT pulse shape parameters from - // the database configuration. If it doesn't exist, use the default value. - metaData = dbConditionsManager.findTableMetaData(SvtShapeFitParametersCollection.class); - if(metaData != null){ - tableName = metaData.getTableName(); - } else { - tableName = SVT_PULSE_PARAMETERS; - } - // Add pulse parameters by channel. - SvtShapeFitParametersCollection shapeFitParametersCollection = dbConditionsManager.getCachedConditions(SvtShapeFitParametersCollection.class, tableName).getCachedData(); - - return shapeFitParametersCollection; - } - - // FIXME: This should be changed to catch a conditions record not found exception instead of - // a runtime exception. - protected SvtBadChannelCollection getBadChannels(DatabaseConditionsManager dbConditionsManager) - throws RuntimeException { - // Get the table name containing the SVT bad channel map from the - // database configuration. If it doesn't exist, use the default value. - metaData = dbConditionsManager.findTableMetaData(SvtBadChannelCollection.class); - if(metaData != null){ - tableName = metaData.getTableName(); - } else { - tableName = SVT_BAD_CHANNELS; - } - - // Add bad channels. - SvtBadChannelCollection badChannels = dbConditionsManager.getCachedConditions(SvtBadChannelCollection.class, tableName).getCachedData(); - - return badChannels; - } - - protected SvtGainCollection getChannelGains(DatabaseConditionsManager dbConditionsManager){ - - // Get the table name containing the SVT gains from the database - // configuration. If it doesn't exist, use the default value. - metaData = dbConditionsManager.findTableMetaData(SvtGainCollection.class); - if(metaData != null){ - tableName = metaData.getTableName(); - } else { - tableName = SVT_GAINS; - } - - // Add gains by channel. - SvtGainCollection gains = dbConditionsManager.getCachedConditions(SvtGainCollection.class, tableName).getCachedData(); - - return gains; - } - - protected SvtT0ShiftCollection getT0Shits(DatabaseConditionsManager dbConditionsManager){ - // Get the table name containing the SVT t0 shifts. If it doesn't - // exist, use the default value. - metaData = dbConditionsManager.findTableMetaData(SvtT0ShiftCollection.class); - if(metaData != null){ - tableName = metaData.getTableName(); - } else { - tableName = SVT_TIME_SHIFTS; - } - // Set the t0 shifts by sensor. - SvtT0ShiftCollection t0Shifts = dbConditionsManager.getCachedConditions(SvtT0ShiftCollection.class, tableName).getCachedData(); - - return t0Shifts; - } -} + /** + * Get a given collection of the given type from the conditions database. + * + * @param type Class type + * @param dbConditionsManager The database conditions manager + * @return A collection of objects of the given type from the conditions database + */ + protected <U> U getCollection(Class<U> type, DatabaseConditionsManager dbConditionsManager){ + + // Get the table name from the database configuration + TableMetaData metaData = dbConditionsManager.findTableMetaData(type); + if(metaData == null) + throw new RuntimeException("Table name data for condition of type " + type.getSimpleName() + " was not found."); + String tableName = metaData.getTableName(); + + // FIXME: This should be changed to catch a conditions record not found exception instead of + // a runtime exception. + U conditionsCollection = dbConditionsManager.getCachedConditions(type, tableName).getCachedData(); + return conditionsCollection; + } +}