Commit in java/branches/ecal-recon_HPSJAVA-93/src/main/java/org/hps/recon/ecal on MAIN
EcalRawConverter.java+77-12573 -> 574
EcalRawConverter uses the new conditions, does not work yet

java/branches/ecal-recon_HPSJAVA-93/src/main/java/org/hps/recon/ecal
EcalRawConverter.java 573 -> 574
--- java/branches/ecal-recon_HPSJAVA-93/src/main/java/org/hps/recon/ecal/EcalRawConverter.java	2014-05-13 09:49:30 UTC (rev 573)
+++ java/branches/ecal-recon_HPSJAVA-93/src/main/java/org/hps/recon/ecal/EcalRawConverter.java	2014-05-13 18:45:18 UTC (rev 574)
@@ -1,10 +1,22 @@
 package org.hps.recon.ecal;
 
-import org.hps.conditions.deprecated.EcalConditions;
+import org.hps.conditions.ConditionsDriver;
+import org.hps.conditions.DatabaseConditionsManager;
+import org.hps.conditions.TableConstants;
+import org.hps.conditions.ecal.EcalChannel.EcalChannelCollection;
+import org.hps.conditions.ecal.EcalChannel.GeometryId;
+import org.hps.conditions.ecal.EcalChannelConstants;
+import org.hps.conditions.ecal.EcalConditions;
+import org.hps.conditions.ecal.EcalConditionsUtil;
+import org.lcsim.conditions.ConditionsManager;
+import org.lcsim.detector.identifier.IIdentifier;
+import org.lcsim.detector.identifier.IIdentifierHelper;
+import org.lcsim.detector.identifier.Identifier;
 import org.lcsim.event.CalorimeterHit;
 import org.lcsim.event.RawCalorimeterHit;
 import org.lcsim.event.RawTrackerHit;
 import org.lcsim.event.base.BaseRawCalorimeterHit;
+import org.lcsim.geometry.Detector;
 
 /**
  *
@@ -17,8 +29,27 @@
     private boolean constantGain = false;
     private double gain;
     private boolean use2014Gain = true;
+    
+    static EcalConditions ecalConditions = null;
+    static IIdentifierHelper helper = null;
+    static EcalChannelCollection channels = null; 
 
     public EcalRawConverter() {
+    	
+    	Detector detector = DatabaseConditionsManager.getInstance().getDetectorObject();
+    	
+        // ECAL combined conditions object.
+        ecalConditions = ConditionsManager.defaultInstance()
+                .getCachedConditions(EcalConditions.class, TableConstants.ECAL_CONDITIONS).getCachedData();
+        
+        // List of channels.
+        channels = ecalConditions.getChannelCollection();
+        
+        // ID helper.
+        helper = detector.getSubdetector("Ecal").getDetectorElement().getIdentifierHelper();
+        
+        System.out.println("You are now using the database conditions for EcalRawConverter.");
+    	
     }
 
     public void setGain(double gain) {
@@ -36,7 +67,11 @@
         if (debug) {
             System.out.println("Summing ADC for hit: " + hit.toString());
         }
-        double pedestal = EcalConditions.physicalToPedestal(hit.getCellID());
+        
+        // Get the channel data.
+        EcalChannelConstants channelData = findChannel(hit.getCellID());
+        
+        double pedestal = channelData.getCalibration().getPedestal();
         short sum = 0;
         short samples[] = hit.getADCValues();
         for (int isample = 0; isample < samples.length; ++isample) {
@@ -51,22 +86,24 @@
     public CalorimeterHit HitDtoA(RawTrackerHit hit) {
         double time = hit.getTime();
         long id = hit.getCellID();
-        double rawEnergy = adcToEnergy(sumADC(hit), id);      
+        double rawEnergy = adcToEnergy(sumADC(hit), id);
 //        double[] pos = hit.getDetectorElement().getGeometry().getPosition().v();
         CalorimeterHit h = new HPSCalorimeterHit(rawEnergy + 0.0000001, time, id, 0);
         //+0.0000001 is a horrible hack to ensure rawEnergy!=BaseCalorimeterHit.UNSET_CORRECTED_ENERGY
         return h;
     }
 
-    public CalorimeterHit HitDtoA(RawCalorimeterHit hit, int window) {
+    public CalorimeterHit HitDtoA(RawCalorimeterHit hit, int window, double timeOffset) {
         if (hit.getTimeStamp() % 64 != 0) {
             System.out.println("unexpected timestamp " + hit.getTimeStamp());
         }
         double time = hit.getTimeStamp() / 16.0;
         long id = hit.getCellID();
-        double adcSum = hit.getAmplitude() - window * EcalConditions.physicalToPedestal(id);
-        double rawEnergy = adcToEnergy(adcSum, id);  
-        CalorimeterHit h = new HPSCalorimeterHit(rawEnergy + 0.0000001, time, id, 0);
+        // Get the channel data.
+        EcalChannelConstants channelData = findChannel(id);
+        double adcSum = hit.getAmplitude() - window * channelData.getCalibration().getPedestal();
+        double rawEnergy = adcToEnergy(adcSum, id);
+        CalorimeterHit h = new HPSCalorimeterHit(rawEnergy + 0.0000001, time + timeOffset, id, 0);
         //+0.0000001 is a horrible hack to ensure rawEnergy!=BaseCalorimeterHit.UNSET_CORRECTED_ENERGY
         return h;
     }
@@ -74,11 +111,13 @@
     public RawCalorimeterHit HitAtoD(CalorimeterHit hit, int window) {
         int time = (int) (Math.round(hit.getTime() / 4.0) * 64.0);
         long id = hit.getCellID();
+        // Get the channel data.
+        EcalChannelConstants channelData = findChannel(id);
         int amplitude;
         if (constantGain) {
-            amplitude = (int) Math.round((hit.getRawEnergy() / ECalUtils.MeV) / gain + window * EcalConditions.physicalToPedestal(id));
+            amplitude = (int) Math.round((hit.getRawEnergy() / ECalUtils.MeV) / gain + window * channelData.getCalibration().getPedestal());
         } else {
-            amplitude = (int) Math.round((hit.getRawEnergy() / ECalUtils.MeV) / EcalConditions.physicalToGain(id) + window * EcalConditions.physicalToPedestal(id));
+            amplitude = (int) Math.round((hit.getRawEnergy() / ECalUtils.MeV) / channelData.getGain().getGain() + window * channelData.getCalibration().getPedestal());
         }
         RawCalorimeterHit h = new BaseRawCalorimeterHit(id, amplitude, time);
         return h;
@@ -88,17 +127,21 @@
      * return energy (units of GeV) corresponding to the ADC sum and crystal ID
      */
     private double adcToEnergy(double adcSum, long cellID) {
+    	
+        // Get the channel data.
+        EcalChannelConstants channelData = findChannel(cellID);
+    	
         if (use2014Gain) {
             if (constantGain) {
                 return adcSum * ECalUtils.gainFactor * ECalUtils.ecalReadoutPeriod;
             } else {
-                return EcalConditions.physicalToGain(cellID) * adcSum * ECalUtils.gainFactor * ECalUtils.ecalReadoutPeriod; // should not be used for the moment (2014/02)
+                return channelData.getGain().getGain() * adcSum * ECalUtils.gainFactor * ECalUtils.ecalReadoutPeriod; // should not be used for the moment (2014/02)
             }
         } else {
             if (constantGain) {
                 return gain * adcSum * ECalUtils.MeV;
             } else {
-                return EcalConditions.physicalToGain(cellID) * adcSum * ECalUtils.MeV; //gain is defined as MeV/integrated ADC
+                return channelData.getGain().getGain() * adcSum * ECalUtils.MeV; //gain is defined as MeV/integrated ADC
             }
         }
     }
@@ -115,4 +158,26 @@
      return h;
      }
      */
-}
\ No newline at end of file
+    
+    /** 
+     * Convert physical ID to gain value.
+     * @param cellID (long)
+     * @return channel constants (EcalChannelConstants)
+     */
+    private static EcalChannelConstants findChannel(long cellID) {
+        // Make an ID object from raw hit ID.
+        IIdentifier id = new Identifier(cellID);
+        
+        // Get physical field values.
+        int system = helper.getValue(id, "system");
+        int x = helper.getValue(id, "ix");
+        int y = helper.getValue(id, "iy");
+        
+        // Create an ID to search for in channel collection.
+        GeometryId geometryId = new GeometryId(helper, new int[] { system, x, y });
+                
+        // Get the channel data.
+        return ecalConditions.getChannelConstants(channels.findChannel(geometryId));    
+    }   
+    
+}
SVNspam 0.1