Print

Print


Author: [log in to unmask]
Date: Fri Nov 20 18:58:08 2015
New Revision: 3969

Log:
move stuff around to match the tech note

Modified:
    java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtBiasConditionsLoader.java
    java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtBiasMyaDataReader.java
    java/trunk/users/src/main/java/org/hps/users/meeg/SvtChargeIntegrator.java

Modified: java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtBiasConditionsLoader.java
 =============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtBiasConditionsLoader.java	(original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtBiasConditionsLoader.java	Fri Nov 20 18:58:08 2015
@@ -6,22 +6,15 @@
 import hep.aida.IPlotter;
 import hep.aida.IPlotterStyle;
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
 import java.sql.SQLException;
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
 import java.util.GregorianCalendar;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
-import java.util.TimeZone;
 import java.util.logging.Logger;
 
 import org.apache.commons.cli.CommandLine;
@@ -180,7 +173,7 @@
         options.addOption(new Option("t", false, "use run table format (from crawler) for bias"));
         options.addOption(new Option("d", false, "discard first line of MYA data (for myaData output)"));
         options.addOption(new Option("g", false, "Actually load stuff into DB"));
-        options.addOption(new Option("b", true, "beam current file"));
+//        options.addOption(new Option("b", true, "beam current file"));
         options.addOption(new Option("s", false, "Show plots"));
 
         final CommandLineParser parser = new DefaultParser();
@@ -261,9 +254,9 @@
             }
         }
 
-        if (cl.hasOption("b") && cl.hasOption("m") && cl.hasOption("p")) {
-            readBeamData(new File(cl.getOptionValue("b")), runList, positionRunRanges, biasRunRanges);
-        }
+//        if (cl.hasOption("b") && cl.hasOption("m") && cl.hasOption("p")) {
+//            readBeamData(new File(cl.getOptionValue("b")), runList, positionRunRanges, biasRunRanges);
+//        }
 
         // load to DB
         if (cl.hasOption("g")) {
@@ -427,111 +420,111 @@
         }
     }
 
-    private static void readBeamData(File file, List<RunData> runList, List<SvtPositionRunRange> positionRanges, List<SvtBiasRunRange> biasRanges) {
-        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
-        dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
-
-        Map<Integer, SvtPositionRunRange> positionRangeMap = new HashMap<Integer, SvtPositionRunRange>();
-        for (SvtPositionRunRange range : positionRanges) {
-            positionRangeMap.put(range.getRun().getRun(), range);
-        }
-        Map<Integer, SvtBiasRunRange> biasRangeMap = new HashMap<Integer, SvtBiasRunRange>();
-        for (SvtBiasRunRange range : biasRanges) {
-            biasRangeMap.put(range.getRun().getRun(), range);
-        }
-
-        try {
-            BufferedReader br = new BufferedReader(new FileReader(file));
-            String line;
-            System.out.println("myaData header: " + br.readLine()); //discard the first line
-            System.out.println("run\ttotalQ\ttotalQBias\tfracBias\ttotalQNom\tfracNom\ttotalQ1pt5\tfrac1pt5\ttotalGatedQ\ttotalGatedQBias\tfracGatedBias\ttotalGatedQNom\tfracGatedNom\ttotalGatedQ1pt5\tfracGated1pt5");
-
-            for (RunData run : runList) {
-                double totalCharge = 0;
-                double totalChargeWithBias = 0;
-                double totalChargeWithBiasAtNominal = 0;
-                double totalChargeWithBiasAt1pt5 = 0;
-                double totalGatedCharge = 0;
-                double totalGatedChargeWithBias = 0;
-                double totalGatedChargeWithBiasAtNominal = 0;
-                double totalGatedChargeWithBiasAt1pt5 = 0;
-                Date lastDate = null;
-
-                while ((line = br.readLine()) != null) {
-                    String arr[] = line.split(" +");
-
-                    if (arr.length != 4) {
-                        throw new java.text.ParseException("this line is not correct.", 0);
-                    }
-                    Date date = dateFormat.parse(arr[0] + " " + arr[1]);
-                    if (date.after(run.getEndDate())) {
-                        break;
-                    }
-                    if (date.before(run.getStartDate())) {
-                        continue;
-                    }
-
-                    double current, livetime;
-                    if (arr[2].equals("<undefined>")) {
-                        current = 0;
-                    } else {
-                        current = Double.parseDouble(arr[2]);
-                    }
-                    if (arr[3].equals("<undefined>")) {
-                        livetime = 0;
-                    } else {
-                        livetime = Math.min(100.0, Math.max(0.0, Double.parseDouble(arr[3]))) / 100.0;
-                    }
-
-                    if (date.after(run.getStartDate())) {
-                        if (lastDate != null) {
-                            double dt = (date.getTime() - lastDate.getTime()) / 1000.0;
-                            double dq = dt * current; // nC
-                            double dqGated = dt * current * livetime; // nC
-
-                            totalCharge += dq;
-                            totalGatedCharge += dqGated;
-                            SvtBiasRunRange biasRunRange = biasRangeMap.get(run.getRun());
-                            if (biasRunRange != null) {
-                                for (SvtBiasMyaRange biasRange : biasRunRange.getRanges()) {
-                                    if (biasRange.includes(date)) {
-                                        totalChargeWithBias += dq;
-                                        totalGatedChargeWithBias += dqGated;
-
-                                        SvtPositionRunRange positionRunRange = positionRangeMap.get(run.getRun());
-                                        if (positionRunRange != null) {
-                                            for (SvtPositionMyaRange positionRange : positionRunRange.getRanges()) {
-                                                if (positionRange.includes(date)) {
-                                                    if (Math.abs(positionRange.getBottom()) < 0.0001 && Math.abs(positionRange.getTop()) < 0.0001) {
-                                                        totalChargeWithBiasAtNominal += dq;
-                                                        totalGatedChargeWithBiasAtNominal += dqGated;
-                                                    } else if (Math.abs(positionRange.getBottom() - 0.0033) < 0.0001 && Math.abs(positionRange.getTop() - 0.0031) < 0.0001) {
-                                                        totalChargeWithBiasAt1pt5 += dq;
-                                                        totalGatedChargeWithBiasAt1pt5 += dqGated;
-                                                    }
-                                                    break;
-                                                }
-                                            }
-                                        }
-
-                                        break;
-                                    }
-                                }
-                            }
-
-                        }
-                    }
-                    lastDate = date;
-                }
-//                System.out.format("run\t%d\ttotalQ\t%.0f\ttotalQBias\t%.0f\tfracBias\t%f\ttotalQNom\t%.0f\tfracNom\t%f\ttotalQ1pt5\t%.0f\tfrac1pt5\t%f\ttotalGatedQ\t%.0f\ttotalGatedQBias\t%.0f\tfracGatedBias\t%f\ttotalGatedQNom\t%.0f\tfracGatedNom\t%f\ttotalGatedQ1pt5\t%.0f\tfracGated1pt5\t%f\n", run.getRun(), totalCharge, totalChargeWithBias, totalChargeWithBias / totalCharge, totalChargeWithBiasAtNominal, totalChargeWithBiasAtNominal / totalCharge, totalChargeWithBiasAt1pt5, totalChargeWithBiasAt1pt5 / totalCharge, totalGatedCharge, totalGatedChargeWithBias, totalGatedChargeWithBias / totalGatedCharge, totalGatedChargeWithBiasAtNominal, totalGatedChargeWithBiasAtNominal / totalGatedCharge, totalGatedChargeWithBiasAt1pt5, totalGatedChargeWithBiasAt1pt5 / totalGatedCharge);
-                System.out.format("%d\t%.0f\t%.0f\t%f\t%.0f\t%f\t%.0f\t%f\t%.0f\t%.0f\t%f\t%.0f\t%f\t%.0f\t%f\n", run.getRun(), totalCharge, totalChargeWithBias, totalChargeWithBias / totalCharge, totalChargeWithBiasAtNominal, totalChargeWithBiasAtNominal / totalCharge, totalChargeWithBiasAt1pt5, totalChargeWithBiasAt1pt5 / totalCharge, totalGatedCharge, totalGatedChargeWithBias, totalGatedChargeWithBias / totalGatedCharge, totalGatedChargeWithBiasAtNominal, totalGatedChargeWithBiasAtNominal / totalGatedCharge, totalGatedChargeWithBiasAt1pt5, totalGatedChargeWithBiasAt1pt5 / totalGatedCharge);
-            }
-            br.close();
-
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        } catch (java.text.ParseException e) {
-            throw new RuntimeException(e);
-        }
-    }
+//    private static void readBeamData(File file, List<RunData> runList, List<SvtPositionRunRange> positionRanges, List<SvtBiasRunRange> biasRanges) {
+//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+//        dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
+//
+//        Map<Integer, SvtPositionRunRange> positionRangeMap = new HashMap<Integer, SvtPositionRunRange>();
+//        for (SvtPositionRunRange range : positionRanges) {
+//            positionRangeMap.put(range.getRun().getRun(), range);
+//        }
+//        Map<Integer, SvtBiasRunRange> biasRangeMap = new HashMap<Integer, SvtBiasRunRange>();
+//        for (SvtBiasRunRange range : biasRanges) {
+//            biasRangeMap.put(range.getRun().getRun(), range);
+//        }
+//
+//        try {
+//            BufferedReader br = new BufferedReader(new FileReader(file));
+//            String line;
+//            System.out.println("myaData header: " + br.readLine()); //discard the first line
+//            System.out.println("run\ttotalQ\ttotalQBias\tfracBias\ttotalQNom\tfracNom\ttotalQ1pt5\tfrac1pt5\ttotalGatedQ\ttotalGatedQBias\tfracGatedBias\ttotalGatedQNom\tfracGatedNom\ttotalGatedQ1pt5\tfracGated1pt5");
+//
+//            for (RunData run : runList) {
+//                double totalCharge = 0;
+//                double totalChargeWithBias = 0;
+//                double totalChargeWithBiasAtNominal = 0;
+//                double totalChargeWithBiasAt1pt5 = 0;
+//                double totalGatedCharge = 0;
+//                double totalGatedChargeWithBias = 0;
+//                double totalGatedChargeWithBiasAtNominal = 0;
+//                double totalGatedChargeWithBiasAt1pt5 = 0;
+//                Date lastDate = null;
+//
+//                while ((line = br.readLine()) != null) {
+//                    String arr[] = line.split(" +");
+//
+//                    if (arr.length != 4) {
+//                        throw new java.text.ParseException("this line is not correct.", 0);
+//                    }
+//                    Date date = dateFormat.parse(arr[0] + " " + arr[1]);
+//                    if (date.after(run.getEndDate())) {
+//                        break;
+//                    }
+//                    if (date.before(run.getStartDate())) {
+//                        continue;
+//                    }
+//
+//                    double current, livetime;
+//                    if (arr[2].equals("<undefined>")) {
+//                        current = 0;
+//                    } else {
+//                        current = Double.parseDouble(arr[2]);
+//                    }
+//                    if (arr[3].equals("<undefined>")) {
+//                        livetime = 0;
+//                    } else {
+//                        livetime = Math.min(100.0, Math.max(0.0, Double.parseDouble(arr[3]))) / 100.0;
+//                    }
+//
+//                    if (date.after(run.getStartDate())) {
+//                        if (lastDate != null) {
+//                            double dt = (date.getTime() - lastDate.getTime()) / 1000.0;
+//                            double dq = dt * current; // nC
+//                            double dqGated = dt * current * livetime; // nC
+//
+//                            totalCharge += dq;
+//                            totalGatedCharge += dqGated;
+//                            SvtBiasRunRange biasRunRange = biasRangeMap.get(run.getRun());
+//                            if (biasRunRange != null) {
+//                                for (SvtBiasMyaRange biasRange : biasRunRange.getRanges()) {
+//                                    if (biasRange.includes(date)) {
+//                                        totalChargeWithBias += dq;
+//                                        totalGatedChargeWithBias += dqGated;
+//
+//                                        SvtPositionRunRange positionRunRange = positionRangeMap.get(run.getRun());
+//                                        if (positionRunRange != null) {
+//                                            for (SvtPositionMyaRange positionRange : positionRunRange.getRanges()) {
+//                                                if (positionRange.includes(date)) {
+//                                                    if (Math.abs(positionRange.getBottom()) < 0.0001 && Math.abs(positionRange.getTop()) < 0.0001) {
+//                                                        totalChargeWithBiasAtNominal += dq;
+//                                                        totalGatedChargeWithBiasAtNominal += dqGated;
+//                                                    } else if (Math.abs(positionRange.getBottom() - 0.0033) < 0.0001 && Math.abs(positionRange.getTop() - 0.0031) < 0.0001) {
+//                                                        totalChargeWithBiasAt1pt5 += dq;
+//                                                        totalGatedChargeWithBiasAt1pt5 += dqGated;
+//                                                    }
+//                                                    break;
+//                                                }
+//                                            }
+//                                        }
+//
+//                                        break;
+//                                    }
+//                                }
+//                            }
+//
+//                        }
+//                    }
+//                    lastDate = date;
+//                }
+////                System.out.format("run\t%d\ttotalQ\t%.0f\ttotalQBias\t%.0f\tfracBias\t%f\ttotalQNom\t%.0f\tfracNom\t%f\ttotalQ1pt5\t%.0f\tfrac1pt5\t%f\ttotalGatedQ\t%.0f\ttotalGatedQBias\t%.0f\tfracGatedBias\t%f\ttotalGatedQNom\t%.0f\tfracGatedNom\t%f\ttotalGatedQ1pt5\t%.0f\tfracGated1pt5\t%f\n", run.getRun(), totalCharge, totalChargeWithBias, totalChargeWithBias / totalCharge, totalChargeWithBiasAtNominal, totalChargeWithBiasAtNominal / totalCharge, totalChargeWithBiasAt1pt5, totalChargeWithBiasAt1pt5 / totalCharge, totalGatedCharge, totalGatedChargeWithBias, totalGatedChargeWithBias / totalGatedCharge, totalGatedChargeWithBiasAtNominal, totalGatedChargeWithBiasAtNominal / totalGatedCharge, totalGatedChargeWithBiasAt1pt5, totalGatedChargeWithBiasAt1pt5 / totalGatedCharge);
+//                System.out.format("%d\t%.0f\t%.0f\t%f\t%.0f\t%f\t%.0f\t%f\t%.0f\t%.0f\t%f\t%.0f\t%f\t%.0f\t%f\n", run.getRun(), totalCharge, totalChargeWithBias, totalChargeWithBias / totalCharge, totalChargeWithBiasAtNominal, totalChargeWithBiasAtNominal / totalCharge, totalChargeWithBiasAt1pt5, totalChargeWithBiasAt1pt5 / totalCharge, totalGatedCharge, totalGatedChargeWithBias, totalGatedChargeWithBias / totalGatedCharge, totalGatedChargeWithBiasAtNominal, totalGatedChargeWithBiasAtNominal / totalGatedCharge, totalGatedChargeWithBiasAt1pt5, totalGatedChargeWithBiasAt1pt5 / totalGatedCharge);
+//            }
+//            br.close();
+//
+//        } catch (IOException e) {
+//            throw new RuntimeException(e);
+//        } catch (java.text.ParseException e) {
+//            throw new RuntimeException(e);
+//        }
+//    }
 }

Modified: java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtBiasMyaDataReader.java
 =============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtBiasMyaDataReader.java	(original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtBiasMyaDataReader.java	Fri Nov 20 18:58:08 2015
@@ -141,10 +141,6 @@
 
             records = parser.getRecords();
 
-            // Remove first two rows of headers.
-            records.remove(0);
-            records.remove(0);
-
             parser.close();
         } catch (FileNotFoundException ex) {
             Logger.getLogger(SvtBiasMyaDataReader.class.getName()).log(Level.SEVERE, null, ex);

Modified: java/trunk/users/src/main/java/org/hps/users/meeg/SvtChargeIntegrator.java
 =============================================================================
--- java/trunk/users/src/main/java/org/hps/users/meeg/SvtChargeIntegrator.java	(original)
+++ java/trunk/users/src/main/java/org/hps/users/meeg/SvtChargeIntegrator.java	Fri Nov 20 18:58:08 2015
@@ -12,10 +12,11 @@
 import java.util.logging.Logger;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
-import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.csv.CSVFormat;
 import org.apache.commons.csv.CSVParser;
 import org.apache.commons.csv.CSVRecord;
@@ -33,36 +34,20 @@
  */
 public class SvtChargeIntegrator {
 
-    /**
-     * Initialize the logger.
-     */
-    private static Logger LOGGER = Logger.getLogger(SvtChargeIntegrator.class.getPackage().getName());
-
     private static final double angleTolerance = 1e-4;
-
-    /**
-     * Default constructor
-     */
-    public SvtChargeIntegrator() {
-    }
 
     /**
      * Load SVT HV bias constants into the conditions database.
      *
-     * @param args the command line arguments (requires a CVS run log file and a
-     * MYA dump file.)
+     * @param args the command line arguments (requires a CSV run/file log file
+     * and a MYA dump file.)
      */
     public static void main(String[] args) {
 
         Options options = new Options();
-//        options.addOption(new Option("c", true, "CSV run file"));
-//        options.addOption(new Option("m", true, "MYA dump file for bias"));
-//        options.addOption(new Option("p", true, "MYA dump file for motor positions"));
-//        options.addOption(new Option("t", false, "use run table format (from crawler) for bias"));
-//        options.addOption(new Option("d", false, "discard first line of MYA data (for myaData output)"));
-//        options.addOption(new Option("g", false, "Actually load stuff into DB"));
-//        options.addOption(new Option("b", true, "beam current file"));
-//        options.addOption(new Option("s", false, "Show plots"));
+        options.addOption(new Option("r", false, "use per-run CSV log file (default is per-file)"));
+        options.addOption(new Option("t", false, "use TI timestamp instead of Unix time (higher precision, but requires TI time offset in run DB)"));
+        options.addOption(new Option("c", false, "get TI time offset from CSV log file instead of run DB"));
 
         final CommandLineParser parser = new DefaultParser();
         CommandLine cl = null;
@@ -72,23 +57,22 @@
             throw new RuntimeException("Cannot parse.", e);
         }
 
-//        if (!cl.hasOption("c") || (!cl.hasOption("m") && !cl.hasOption("p"))) {
-//            printUsage(options);
-//            return;
-//        }
+        boolean perRun = cl.hasOption("r");
+        boolean useTI = cl.hasOption("t");
+        boolean useCrawlerTI = cl.hasOption("c");
+
+        if (cl.getArgs().length != 2) {
+            printUsage(options);
+            return;
+        }
+
         List<CSVRecord> records = null;
         try {
             FileReader reader = new FileReader(cl.getArgs()[0]);
-            final CSVFormat format = CSVFormat.DEFAULT;
-
-            final CSVParser csvParser;
-            csvParser = new CSVParser(reader, format);
+            final CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT);
 
             records = csvParser.getRecords();
 
-//            // Remove first two rows of headers.
-//            records.remove(0);
-//            records.remove(0);
             csvParser.close();
         } catch (FileNotFoundException ex) {
             Logger.getLogger(SvtChargeIntegrator.class.getName()).log(Level.SEVERE, null, ex);
@@ -96,18 +80,6 @@
             Logger.getLogger(SvtChargeIntegrator.class.getName()).log(Level.SEVERE, null, ex);
         }
 
-//        for (CSVRecord record : records) {
-//            int runNum = Integer.parseInt(record.get(0));
-//            int fileNum = Integer.parseInt(record.get(1));
-////            int nEvents = Integer.parseInt(record.get(2));
-////            int badEvents = Integer.parseInt(record.get(3));
-////            int firstTimestamp = Integer.parseInt(record.get(4));
-////            int lastTimestamp = Integer.parseInt(record.get(5));
-//            long firstTI = Long.parseLong(record.get(6));
-//            long lastTI = Long.parseLong(record.get(7));
-////            long tiOffset = Long.parseLong(record.get(8));
-////            data.add(new FileData(runNum, fileNum, firstTI, lastTI, record));
-//        }
         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
         dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
 
@@ -115,8 +87,11 @@
             BufferedReader br = new BufferedReader(new FileReader(cl.getArgs()[1]));
             String line;
             System.err.println("myaData header: " + br.readLine()); //discard the first line
-//            System.out.println("run\ttotalQ\ttotalQBias\tfracBias\ttotalQNom\tfracNom\ttotalQ1pt5\tfrac1pt5\ttotalGatedQ\ttotalGatedQBias\tfracGatedBias\ttotalGatedQNom\tfracGatedNom\ttotalGatedQ1pt5\tfracGated1pt5");
-            System.out.println("run_num\tfile_num\tnominal_position\tnEvents\ttotalQ\ttotalQ_withbias\ttotalQ_atnom\tgatedQ\tgatedQ_withbias\tgatedQ_atnom");
+            if (perRun) {
+                System.out.println("run_num\tnominal_position\tnEvents\ttotalQ\ttotalQ_withbias\ttotalQ_atnom\tgatedQ\tgatedQ_withbias\tgatedQ_atnom");
+            } else {
+                System.out.println("run_num\tfile_num\tnominal_position\tnEvents\ttotalQ\ttotalQ_withbias\ttotalQ_atnom\tgatedQ\tgatedQ_withbias\tgatedQ_atnom");
+            }
 
             int currentRun = 0;
             double nominalAngleTop = -999;
@@ -127,54 +102,98 @@
             SvtMotorPositionCollection svtPositionConstants = null;
             SvtAlignmentConstant.SvtAlignmentConstantCollection alignmentConstants = null;
             Date date = null;
-            Date lastDate = null;
+            Date lastDate;
 
             for (CSVRecord record : records) {
                 int runNum = Integer.parseInt(record.get(0));
-                int fileNum = Integer.parseInt(record.get(1));
-                long firstTI = Long.parseLong(record.get(6));
-                long lastTI = Long.parseLong(record.get(7));
+                if (useCrawlerTI) {
+                    if (perRun) {
+                        tiTimeOffset = Long.parseLong(record.get(12));
+                    } else {
+                        tiTimeOffset = Long.parseLong(record.get(8));
+                    }
+                    if (tiTimeOffset == 0) {
+                        continue;
+                    }
+                }
 
                 if (runNum != currentRun) {
-                    RunManager.getRunManager().setRun(runNum);
-                    if (!RunManager.getRunManager().runExists() || RunManager.getRunManager().getTriggerConfig().getTiTimeOffset() == null) {
-                        continue;
-                    }
+                    if (useTI && !useCrawlerTI) {
+                        RunManager.getRunManager().setRun(runNum);
+                        if (!RunManager.getRunManager().runExists() || RunManager.getRunManager().getTriggerConfig().getTiTimeOffset() == null) {
+                            continue;
+                        }
+                        tiTimeOffset = RunManager.getRunManager().getTriggerConfig().getTiTimeOffset();
+                    }
+
                     try {
                         DatabaseConditionsManager.getInstance().setDetector("HPS-EngRun2015-Nominal-v3", runNum);
-
-                        svtBiasConstants = DatabaseConditionsManager.getInstance().getCachedConditions(SvtBiasConstant.SvtBiasConstantCollection.class, "svt_bias_constants").getCachedData();
-                        svtPositionConstants = DatabaseConditionsManager.getInstance().getCachedConditions(SvtMotorPosition.SvtMotorPositionCollection.class, "svt_motor_positions").getCachedData();
-                        alignmentConstants = DatabaseConditionsManager.getInstance().getCachedConditions(SvtAlignmentConstant.SvtAlignmentConstantCollection.class, "svt_alignments").getCachedData();
                     } catch (Exception ex) {
                         continue;
                     }
 
-                    tiTimeOffset = RunManager.getRunManager().getTriggerConfig().getTiTimeOffset();
-
-                    for (final SvtAlignmentConstant constant : alignmentConstants) {
-                        switch (constant.getParameter()) {
-                            case 13100:
-                                nominalAngleTop = constant.getValue();
-                                break;
-                            case 23100:
-                                nominalAngleBottom = -constant.getValue();
-                                break;
-                        }
-                    }
-
-                    if (Math.abs(nominalAngleBottom) < angleTolerance && Math.abs(nominalAngleTop) < angleTolerance) {
-                        nominalPosition = "0pt5";
-                    } else if (Math.abs(nominalAngleBottom - 0.0033) < angleTolerance && Math.abs(nominalAngleTop - 0.0031) < angleTolerance) {
-                        nominalPosition = "1pt5";
-                    } else {
+                    try {
+                        svtBiasConstants = DatabaseConditionsManager.getInstance().getCachedConditions(SvtBiasConstant.SvtBiasConstantCollection.class, "svt_bias_constants").getCachedData();
+                    } catch (Exception ex) {
+                        svtBiasConstants = null;
+                    }
+                    try {
+                        svtPositionConstants = DatabaseConditionsManager.getInstance().getCachedConditions(SvtMotorPosition.SvtMotorPositionCollection.class, "svt_motor_positions").getCachedData();
+                    } catch (Exception ex) {
+                        svtPositionConstants = null;
+                    }
+
+                    try {
+                        alignmentConstants = DatabaseConditionsManager.getInstance().getCachedConditions(SvtAlignmentConstant.SvtAlignmentConstantCollection.class, "svt_alignments").getCachedData();
+                        for (final SvtAlignmentConstant constant : alignmentConstants) {
+                            switch (constant.getParameter()) {
+                                case 13100:
+                                    nominalAngleTop = constant.getValue();
+                                    break;
+                                case 23100:
+                                    nominalAngleBottom = -constant.getValue();
+                                    break;
+                            }
+                        }
+                        if (Math.abs(nominalAngleBottom) < angleTolerance && Math.abs(nominalAngleTop) < angleTolerance) {
+                            nominalPosition = "0pt5";
+                        } else if (Math.abs(nominalAngleBottom - 0.0033) < angleTolerance && Math.abs(nominalAngleTop - 0.0031) < angleTolerance) {
+                            nominalPosition = "1pt5";
+                        } else {
+                            nominalPosition = "unknown";
+                        }
+                    } catch (Exception ex) {
+                        alignmentConstants = null;
                         nominalPosition = "unknown";
                     }
+
                     currentRun = runNum;
                 }
 
-                Date startDate = new Date((long) ((firstTI + tiTimeOffset) / 1e6));
-                Date endDate = new Date((long) ((lastTI + tiTimeOffset) / 1e6));
+                Date startDate, endDate;
+                long firstTime, lastTime;//Unix time from head bank
+                long firstTI, lastTI;//TI timestamp from TI bank
+
+                if (perRun) {
+                    firstTime = Long.parseLong(record.get(7));
+                    lastTime = Long.parseLong(record.get(8));
+                    firstTI = Long.parseLong(record.get(10));
+                    lastTI = Long.parseLong(record.get(11));
+
+                } else {
+                    firstTime = Long.parseLong(record.get(4));
+                    lastTime = Long.parseLong(record.get(5));
+                    firstTI = Long.parseLong(record.get(6));
+                    lastTI = Long.parseLong(record.get(7));
+                }
+
+                if (useTI) {
+                    startDate = new Date((long) ((firstTI + tiTimeOffset) / 1e6));
+                    endDate = new Date((long) ((lastTI + tiTimeOffset) / 1e6));
+                } else {
+                    startDate = new Date(firstTime * 1000);
+                    endDate = new Date(lastTime * 1000);
+                }
 
                 double totalCharge = 0;
                 double totalChargeWithBias = 0;
@@ -210,14 +229,23 @@
 
                     boolean biasGood = false;
                     boolean positionGood = false;
-
-                    SvtBiasConstant biasConstant = svtBiasConstants.find(date);
-                    if (biasConstant != null) {
-                        biasGood = true;
-                    }
+                    SvtBiasConstant biasConstant = null;
+                    if (svtBiasConstants != null) {
+                        biasConstant = svtBiasConstants.find(date);
+                        if (biasConstant == null && lastDate != null) {
+                            biasConstant = svtBiasConstants.find(lastDate);
+                        }
+                        if (biasConstant != null) {
+                            biasGood = true;
+                        }
+                    }
+                    SvtMotorPosition positionConstant = null;
                     if (svtPositionConstants != null) {
-                        SvtMotorPosition positionConstant = svtPositionConstants.find(date);
-                        if (positionConstant != null) {
+                        positionConstant = svtPositionConstants.find(date);
+                        if (positionConstant == null && lastDate != null) {
+                            positionConstant = svtPositionConstants.find(lastDate);
+                        }
+                        if (positionConstant != null && alignmentConstants != null) {
 //                    System.out.format("%f %f %f %f\n", positionConstant.getBottom(), nominalAngleBottom, positionConstant.getTop(), nominalAngleTop);
                             if (Math.abs(positionConstant.getBottom() - nominalAngleBottom) < angleTolerance && Math.abs(positionConstant.getTop() - nominalAngleTop) < angleTolerance) {
                                 positionGood = true;
@@ -226,18 +254,32 @@
                     }
 
                     if (lastDate != null) {
-                        double dt = (Math.min(date.getTime(), endDate.getTime()) - Math.max(startDate.getTime(), lastDate.getTime())) / 1000.0;
+                        double biasDt = 0;
+                        double positionDt = 0;
+                        long dtStart = Math.max(startDate.getTime(), lastDate.getTime());
+                        long dtEnd = Math.min(date.getTime(), endDate.getTime());
+                        double dt = (dtEnd - dtStart) / 1000.0;
+                        if (biasConstant != null) {
+                            long biasStart = Math.max(dtStart, biasConstant.getStart());
+                            long biasEnd = Math.min(dtEnd, biasConstant.getEnd());
+                            biasDt = (biasEnd - biasStart) / 1000.0;
+                            if (positionConstant != null) {
+                                long positionStart = Math.max(biasStart, positionConstant.getStart());
+                                long positionEnd = Math.min(biasEnd, positionConstant.getEnd());
+                                positionDt = (positionEnd - positionStart) / 1000.0;
+                            }
+                        }
                         double dq = dt * current; // nC
                         double dqGated = dt * current * livetime; // nC
 //                        System.out.format("start %d end %d date %d lastDate %d current %f dt %f\n", startDate.getTime(), endDate.getTime(), date.getTime(), lastDate.getTime(), current, dt);
                         totalCharge += dq;
                         totalGatedCharge += dqGated;
                         if (biasGood) {
-                            totalChargeWithBias += dq;
-                            totalGatedChargeWithBias += dqGated;
+                            totalChargeWithBias += biasDt * current;
+                            totalGatedChargeWithBias += biasDt * current * livetime;
                             if (positionGood) {
-                                totalChargeWithBiasAtNominal += dq;
-                                totalGatedChargeWithBiasAtNominal += dqGated;
+                                totalChargeWithBiasAtNominal += positionDt * current;
+                                totalGatedChargeWithBiasAtNominal += positionDt * current * livetime;
                             }
                         }
                     }
@@ -248,8 +290,14 @@
                     }
                     br.mark(1000);
                 }
-                int nEvents = Integer.parseInt(record.get(2));
-                System.out.format("%d\t%d\t%s\t%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n", runNum, fileNum, nominalPosition, nEvents, totalCharge, totalChargeWithBias, totalChargeWithBiasAtNominal, totalGatedCharge, totalGatedChargeWithBias, totalGatedChargeWithBiasAtNominal);
+                if (perRun) {
+                    int nEvents = Integer.parseInt(record.get(9));
+                    System.out.format("%d\t%s\t%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n", runNum, nominalPosition, nEvents, totalCharge, totalChargeWithBias, totalChargeWithBiasAtNominal, totalGatedCharge, totalGatedChargeWithBias, totalGatedChargeWithBiasAtNominal);
+                } else {
+                    int fileNum = Integer.parseInt(record.get(1));
+                    int nEvents = Integer.parseInt(record.get(2));
+                    System.out.format("%d\t%d\t%s\t%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n", runNum, fileNum, nominalPosition, nEvents, totalCharge, totalChargeWithBias, totalChargeWithBiasAtNominal, totalGatedCharge, totalGatedChargeWithBias, totalGatedChargeWithBiasAtNominal);
+                }
             }
         } catch (Exception ex) {
             Logger.getLogger(SvtChargeIntegrator.class.getName()).log(Level.SEVERE, null, ex);
@@ -259,7 +307,7 @@
 
     private static void printUsage(Options options) {
         HelpFormatter formatter = new HelpFormatter();
+        System.err.println("SvtChargeIntegrator <CSV log file> <MYA dump file>");
         formatter.printHelp("Need to adhere to these options", options);
-
     }
 }