Author: [log in to unmask] Date: Mon Jul 6 15:27:19 2015 New Revision: 3230 Log: Hand merge some trunk changes that are causing conflicts. Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/run/RunRange.java java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/run/RunSpreadsheet.java java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/svt/SvtBiasConditionsLoader.java java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/svt/SvtBiasConstant.java java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/svt/SvtBiasMyaDumpReader.java Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/run/RunRange.java ============================================================================= --- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/run/RunRange.java (original) +++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/run/RunRange.java Mon Jul 6 15:27:19 2015 @@ -11,51 +11,24 @@ import org.apache.commons.csv.CSVRecord; /** - * Used with the {@link RunSpreadsheet} to find ranges of runs where columns have the same values so they can be - * assigned a conditions record with a run start and end range. + * Used with the {@link RunSpreadsheet} to find ranges of runs where columns have the same values so they can be assigned + * a conditions record with a run start and end range. * <p> * Bad rows such as ones without run numbers or with invalid data values are skipped and not included in a range. - * - * @author Jeremy McCormick, SLAC + * + * @author Jeremy McCormick */ public final class RunRange { /** - * Return <code>true</code> if the values are already in the unique values list. - * - * @param values the list of field values for a row - * @param uniqueValuesList the unique values list - * @return <code>true</code> if the values are already in the unique values list - */ - private static boolean contains(final Collection<String> values, final List<Collection<String>> uniqueValuesList) { - for (final Collection<String> uniqueValues : uniqueValuesList) { - final Iterator<String> valuesIterator = values.iterator(); - final Iterator<String> uniqueValuesIterator = uniqueValues.iterator(); - boolean equals = true; - while (valuesIterator.hasNext() && uniqueValuesIterator.hasNext()) { - final String value = valuesIterator.next(); - final String uniqueValue = uniqueValuesIterator.next(); - if (!value.equals(uniqueValue)) { - equals = false; - break; - } - } - if (equals) { - return true; - } - } - return false; - } - - /** - * Find run ranges for conditions data given a set of column names and the full run spreadsheet. - * + * Find run ranges for conditions data given a set of column names and the full run spreadsheet. + * * @param runSheet the run spreadsheet data (from CSV file) * @param columnNames the names of the columns * @return the list of run ranges */ public static final List<RunRange> findRunRanges(final RunSpreadsheet runSheet, final Set<String> columnNames) { - + final List<RunRange> ranges = new ArrayList<RunRange>(); final Iterator<CSVRecord> it = runSheet.getRecords().iterator(); @@ -69,17 +42,17 @@ // Is the record valid? if (isValidRecord(record, columnNames)) { if (range == null) { - // Create new range for the valid row. + // Create new range for the valid row. range = new RunRange(columnNames); - } else { + } else { // If this record is not in the range then add a new range. if (!range.inRange(record)) { // Add the current range and create a new one. ranges.add(range); range = new RunRange(columnNames); } - } - + } + // Update the range from the current record. range.update(record); } else { @@ -93,30 +66,11 @@ return ranges; } - - /** - * Get the list of unique values from a set of run ranges. - * - * @param ranges the run ranges - * @return the list of unique values from the field values - */ - public static List<Collection<String>> getUniqueValues(final List<RunRange> ranges) { - final Iterator<RunRange> it = ranges.iterator(); - final List<Collection<String>> uniqueValuesList = new ArrayList<Collection<String>>(); - while (it.hasNext()) { - final RunRange range = it.next(); - final Collection<String> values = range.getValues(); - if (!contains(values, uniqueValuesList)) { - uniqueValuesList.add(values); - } - } - return uniqueValuesList; - } - - /** - * Return <code>true</code> if the <code>CSVRecord</code> is valid, which means it has a run number and data in the - * columns used by this range. - * + + /** + * Return <code>true</code> if the <code>CSVRecord</code> is valid, which means it has a run number and + * data in the columns used by this range. + * * @param record the <code>CSVRecord</code> to check * @param columnNames the names of the columns * @return <code>true</code> if record is valid @@ -131,8 +85,7 @@ } for (final String columnName : columnNames) { // Check that required column data is not null, blank, or empty string. - if (record.get(columnName) == null || "".equals(record.get(columnName)) - || record.get(columnName).length() == 0) { + if (record.get(columnName) == null || "".equals(record.get(columnName)) || record.get(columnName).length() == 0) { return false; } } @@ -157,11 +110,11 @@ /** * The mapping of column names to values. */ - private final Map<String, String> values = new LinkedHashMap<String, String>(); + private Map<String, String> values = new LinkedHashMap<String, String>(); /** * Create a new range. - * + * * @param columnNames the names of the columns */ RunRange(final Set<String> columnNames) { @@ -172,8 +125,45 @@ } /** + * Get the last run number in the range. + * + * @return the last run number in the range + */ + public int getRunEnd() { + return runEnd; + } + + /** + * Get the first run number in the range. + * + * @return the first run number in the range + */ + public int getRunStart() { + return runStart; + } + + /** + * Get get value of a field by column name. + * + * @param columnName the column name + * @return the value of the field + */ + public String getValue(String columnName) { + return this.values.get(columnName); + } + + /** + * Get the raw values of the data. + * + * @return the raw data values + */ + public Collection<String> getValues() { + return this.values.values(); + } + + /** * Get the names of the columns used by this range. - * + * * @return the names of the columns */ public Set<String> getColumnNames() { @@ -181,50 +171,13 @@ } /** - * Get the last run number in the range. - * - * @return the last run number in the range - */ - public int getRunEnd() { - return this.runEnd; - } - - /** - * Get the first run number in the range. - * - * @return the first run number in the range - */ - public int getRunStart() { - return this.runStart; - } - - /** - * Get get value of a field by column name. - * - * @param columnName the column name - * @return the value of the field - */ - public String getValue(final String columnName) { - return this.values.get(columnName); - } - - /** - * Get the raw values of the data. - * - * @return the raw data values - */ - public Collection<String> getValues() { - return this.values.values(); - } - - /** - * Return <code>true</code> if the record is in the range, e.g. its data values are the same. - * + * Return <code>true</code> if the record is in the range, e.g. its data values are the same. + * * @param record the <code>CSVRecord</code> containing the run data * @return <code>true</code> if the the record is in range */ private boolean inRange(final CSVRecord record) { - for (final String columnName : this.columnNames) { + for (final String columnName : columnNames) { if (!record.get(columnName).equals(this.values.get(columnName))) { return false; } @@ -233,41 +186,85 @@ } /** + * Update the range from a record. + * @param record the <code>CSVRecord</code> with the run's data + */ + private void update(final CSVRecord record) { + final int run = Integer.parseInt(record.get("run")); + if (run < runStart) { + this.runStart = run; + } + if (run > runEnd) { + this.runEnd = run; + } + if (values.size() == 0) { + for (final String columnName : columnNames) { + this.values.put(columnName, record.get(columnName)); + } + } + } + + /** * Convert this object to a string. - * + * * @return this object converted to a string */ - @Override public String toString() { - final StringBuffer sb = new StringBuffer(); + StringBuffer sb = new StringBuffer(); sb.append("RunRange { "); sb.append("runStart: " + this.runStart + ", "); sb.append("runEnd: " + this.runEnd + ", "); - for (final String columnName : this.columnNames) { - sb.append(columnName + ": " + this.values.get(columnName) + ", "); + for (String columnName : columnNames) { + sb.append(columnName + ": " + values.get(columnName) + ", " ); } sb.setLength(sb.length() - 2); sb.append(" }"); return sb.toString(); } - - /** - * Update the range from a record. - * - * @param record the <code>CSVRecord</code> with the run's data - */ - private void update(final CSVRecord record) { - final int run = Integer.parseInt(record.get("run")); - if (run < this.runStart) { - this.runStart = run; - } - if (run > this.runEnd) { - this.runEnd = run; - } - if (this.values.size() == 0) { - for (final String columnName : this.columnNames) { - this.values.put(columnName, record.get(columnName)); - } - } + + /** + * Get the list of unique values from a set of run ranges. + * + * @param ranges the run ranges + * @return the list of unique values from the field values + */ + public static List<Collection<String>> getUniqueValues(List<RunRange> ranges) { + Iterator<RunRange> it = ranges.iterator(); + List<Collection<String>> uniqueValuesList = new ArrayList<Collection<String>>(); + while(it.hasNext()) { + RunRange range = it.next(); + Collection<String> values = range.getValues(); + if (!contains(values, uniqueValuesList)) { + uniqueValuesList.add(values); + } + } + return uniqueValuesList; + } + + /** + * Return <code>true</code> if the values are already in the unique values list. + * + * @param values the list of field values for a row + * @param uniqueValuesList the unique values list + * @return <code>true</code> if the values are already in the unique values list + */ + private static boolean contains(Collection<String> values, List<Collection<String>> uniqueValuesList) { + for (Collection<String> uniqueValues : uniqueValuesList) { + Iterator<String> valuesIterator = values.iterator(); + Iterator<String> uniqueValuesIterator = uniqueValues.iterator(); + boolean equals = true; + while (valuesIterator.hasNext() && uniqueValuesIterator.hasNext()) { + String value = valuesIterator.next(); + String uniqueValue = uniqueValuesIterator.next(); + if (!value.equals(uniqueValue)) { + equals = false; + break; + } + } + if (equals) { + return true; + } + } + return false; } } Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/run/RunSpreadsheet.java ============================================================================= --- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/run/RunSpreadsheet.java (original) +++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/run/RunSpreadsheet.java Mon Jul 6 15:27:19 2015 @@ -7,104 +7,37 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Calendar; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; +import java.util.Locale; +import java.util.TimeZone; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; /** - * A simple representation of the 2015 run spreadsheet (runs from 3/28 to 5/19) read from an exported CSV file as a list - * of records. + * A simple representation of the 2015 run spreadsheet (runs from 3/28 to 5/19) read from an exported CSV file as a list of records. * <p> - * Master copy of the spreadsheet is located at <a - * href="https://docs.google.com/spreadsheets/d/1l1NurPpsmpgZKgr1qoQpLQBBLz1sszLz4xZF-So4xs8/edit#gid=43855609" - * >HPS_Runs_2015</a>. + * Master copy of the spreadsheet is located at + * <a href="https://docs.google.com/spreadsheets/d/1l1NurPpsmpgZKgr1qoQpLQBBLz1sszLz4xZF-So4xs8/edit#gid=43855609">HPS_Runs_2015</a>. * <p> - * The rows are accessible as raw CSV data through the Apache Commons CSV library, and this data must be manually - * cleaned up and converted to the correct data type before being inserted into the conditions database. + * The rows are accessible as raw CSV data through the Apache Commons CSV library, and this data must be manually cleaned up and converted + * to the correct data type before being inserted into the conditions database. * - * @author Jeremy McCormick, SLAC + * @author Jeremy McCormick */ public final class RunSpreadsheet { - public static class RunData { - - private Date endDate; - private final CSVRecord record; - private final int run; - private Date startDate; - - RunData(final CSVRecord record) throws NumberFormatException { - this.record = record; - this.run = parseRunNumber(this.record); - try { - this.startDate = RunSpreadsheet.parseStartDate(this.record); - } catch (final ParseException e) { - } - try { - this.endDate = RunSpreadsheet.parseEndDate(this.record); - } catch (final ParseException e) { - } - } - - public Date getEndDate() { - return this.endDate; - } - - public CSVRecord getRecord() { - return this.record; - } - - public int getRun() { - return this.run; - } - - public Date getStartDate() { - return this.startDate; - } - - @Override - public String toString() { - return "RunData { run: " + this.run + ", startDate: " + this.startDate + ", endDate: " + this.endDate - + " }"; - } - } - - @SuppressWarnings("serial") - public static class RunMap extends LinkedHashMap<Integer, RunData> { - public RunMap() { - super(); - } - - public RunMap(final List<CSVRecord> records) { - super(); - for (final CSVRecord record : records) { - try { - this.addRunData(new RunData(record)); - } catch (final NumberFormatException e) { - e.printStackTrace(); - } - } - } - - private void addRunData(final RunData runData) { - this.put(runData.getRun(), runData); - } - } - - private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy H:mm"); - /** * The column headers. */ - private static String[] HEADERS = {"run", "date", "start_time", "end_time", "to_tape", "n_events", "trigger_rate", - "target", "beam_current", "beam_x", "beam_y", "trigger_config", "ecal_fadc_mode", "ecal_fadc_thresh", - "ecal_fadc_window", "ecal_cluster_thresh_seed", "ecal_cluster_thresh_cluster", "ecal_cluster_window_hits", - "ecal_cluster_window_pairs", "ecal_scalers_fadc", "ecal_scalers_dsc", "svt_y_position", "svt_offset_phase", - "svt_offset_time", "ecal_temp", "ecal_lv_current", "notes"}; + private static String[] HEADERS = {"run", "date", "start_time", "end_time", "to_tape", "n_events", "trigger_rate", "target", "beam_current", + "beam_x", "beam_y", "trigger_config", "ecal_fadc_mode", "ecal_fadc_thresh", "ecal_fadc_window", "ecal_cluster_thresh_seed", "ecal_cluster_thresh_cluster", + "ecal_cluster_window_hits", "ecal_cluster_window_pairs", "ecal_scalers_fadc", "ecal_scalers_dsc", "svt_y_position", "svt_offset_phase", "svt_offset_time", + "ecal_temp", "ecal_lv_current", "notes"}; /** * Read the CSV file from the command line and print the data to the terminal (just a basic test). @@ -119,22 +52,10 @@ System.out.print("end date: " + parseEndDate(record) + ", "); System.out.print(record); System.out.println(); - } catch (final Exception e) { + } catch (Exception e) { e.printStackTrace(); } } - } - - private static Date parseEndDate(final CSVRecord record) throws ParseException { - return DATE_FORMAT.parse(record.get("date") + " " + record.get("end_time")); - } - - private static int parseRunNumber(final CSVRecord record) throws NumberFormatException { - return Integer.parseInt(record.get("run")); - } - - private static Date parseStartDate(final CSVRecord record) throws ParseException { - return DATE_FORMAT.parse(record.get("date") + " " + record.get("start_time")); } /** @@ -168,13 +89,14 @@ * @return the <code>CSVRecord</code> or <code>null</code> if not found */ public CSVRecord findRun(final int run) { - for (final CSVRecord record : this.records) { + for (final CSVRecord record : records) { try { if (run == Integer.parseInt(record.get("run"))) { return record; } } catch (final NumberFormatException e) { - e.printStackTrace(); + //System.out.println("problem reading run" + run); + //e.printStackTrace(); } } return null; @@ -194,12 +116,12 @@ final CSVParser parser = new CSVParser(reader, format); - this.records = parser.getRecords(); + records = parser.getRecords(); // Remove first three rows of headers. - this.records.remove(0); - this.records.remove(0); - this.records.remove(0); + records.remove(0); + records.remove(0); + records.remove(0); parser.close(); } @@ -210,34 +132,135 @@ * @return the list of records read from the CSV file */ public List<CSVRecord> getRecords() { - return this.records; - } - + return records; + } + + public static final AnotherSimpleDateFormat DATE_FORMAT = new AnotherSimpleDateFormat("MM/dd/yyyy H:mm"); + private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("EST"); + + + @SuppressWarnings("serial") + public + static class AnotherSimpleDateFormat extends SimpleDateFormat { + public AnotherSimpleDateFormat(String formatstring) { + super(formatstring); + //Calendar c = Calendar.getInstance(TIME_ZONE,Locale.US); + //setTimeZone(TIME_ZONE); + } + + public Date parse(String source) throws ParseException { + setTimeZone(TIME_ZONE); + //System.out.println("parse " + source); + Date date = super.parse(source); + //System.out.println("update date " + date.toString() + " epoch " + date.getTime()); + Date dateUpdated = new Date(date.getTime()-3600*1000); + //System.out.println("updated date " + dateUpdated.toString() + " epoch " + dateUpdated.getTime()); + return dateUpdated; + } + + } + + + private static Date parseStartDate(CSVRecord record) throws ParseException { + //System.out.printf("Start date result of parsing %s %s is %s since epoch %d \n",record.get("date"), record.get("start_time"),DATE_FORMAT.parse(record.get("date") + " " + record.get("start_time")).toString(), DATE_FORMAT.parse(record.get("date") + " " + record.get("start_time")).getTime() ); + return DATE_FORMAT.parse(record.get("date") + " " + record.get("start_time")); + } + + private static Date parseEndDate(CSVRecord record) throws ParseException { + //DATE_FORMAT.setTimeZone(TIME_ZONE); + return DATE_FORMAT.parse(record.get("date") + " " + record.get("end_time")); + } + + private static int parseRunNumber(CSVRecord record) throws NumberFormatException { + return Integer.parseInt(record.get("run")); + } + + public static class RunData { + + private int run; + private Date startDate; + private Date endDate; + private CSVRecord record; + + RunData(CSVRecord record) throws NumberFormatException { + this.record = record; + run = parseRunNumber(this.record); + try { + startDate = RunSpreadsheet.parseStartDate(this.record); + } catch (ParseException e) { + } + try { + endDate = RunSpreadsheet.parseEndDate(this.record); + } catch (ParseException e) { + } + } + + public int getRun() { + return run; + } + + public Date getStartDate() { + return startDate; + } + + public Date getEndDate() { + return endDate; + } + + public String toString() { + return "RunData { run: " + run + ", startDate: " + startDate + ", endDate: " + endDate + " }"; + } + + public CSVRecord getRecord() { + return record; + } + } + + @SuppressWarnings("serial") + public static class RunMap extends LinkedHashMap<Integer, RunData> { + public RunMap() { + super(); + } + public RunMap(List<CSVRecord> records) { + super(); + for (final CSVRecord record : records) { + try { + addRunData(new RunData(record)); + } catch (NumberFormatException e) { + e.printStackTrace(); + } + } + } + private void addRunData(RunData runData) { + this.put(runData.getRun(), runData); + } + } + public RunMap getRunMap() { - return new RunMap(this.getRecords()); - } - - public RunMap getRunMap(final List<RunRange> ranges) { - final List<CSVRecord> records = new ArrayList<CSVRecord>(); - for (final RunRange range : ranges) { - System.out.println(range.toString()); - if (range.getColumnNames().contains("run")) { - if (!range.getValue("run").isEmpty()) { - final CSVRecord record = this.findRun(Integer.parseInt(range.getValue("run"))); - if (record != null) { - records.add(record); - } else { - throw new RuntimeException("this RunRange object was not found. This shouldn't happen. " - + range.toString()); - } - } else { - throw new RuntimeException("this RunRange object has an empty run value "); - } - } else { - throw new RuntimeException("this RunRange object has no run column? " + range.toString()); - } + return new RunMap(getRecords()); + } + + public RunMap getRunMap(List<RunRange> ranges) { + List<CSVRecord> records = new ArrayList<CSVRecord>(); + for(RunRange range : ranges) { + //System.out.println(range.toString()); + if(range.getColumnNames().contains("run")) { + if(!range.getValue("run").isEmpty()) { + CSVRecord record = findRun(Integer.parseInt(range.getValue("run"))); + if(record!=null) { + records.add(record); + } else { + throw new RuntimeException("this RunRange object was not found. This shouldn't happen. " + range.toString()); + } + } else { + throw new RuntimeException("this RunRange object has an empty run value "); + } + } else { + throw new RuntimeException("this RunRange object has no run column? " + range.toString()); + } } return new RunMap(records); - } - + } + + } Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/svt/SvtBiasConditionsLoader.java ============================================================================= --- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/svt/SvtBiasConditionsLoader.java (original) +++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/svt/SvtBiasConditionsLoader.java Mon Jul 6 15:27:19 2015 @@ -1,5 +1,5 @@ /** - * + * */ package org.hps.conditions.svt; @@ -10,6 +10,7 @@ import hep.aida.IPlotterStyle; import java.io.File; +import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; @@ -21,12 +22,13 @@ import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; +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.PosixParser; -import org.hps.conditions.api.ConditionsObjectException; import org.hps.conditions.api.ConditionsRecord; +import org.hps.conditions.api.DatabaseObjectException; import org.hps.conditions.database.DatabaseConditionsManager; import org.hps.conditions.run.RunRange; import org.hps.conditions.run.RunSpreadsheet; @@ -41,20 +43,15 @@ import org.lcsim.util.log.LogUtil; /** - * @author Per Hansson Adrian, SLAC + * @author Per Hansson Adrian <[log in to unmask]> + * */ public class SvtBiasConditionsLoader { - /** - * Setup control plots. - */ - private static AIDA aida = AIDA.defaultInstance(); - static IDataPointSet dpsBiasRuns = null; - - static IDataPointSet dpsRuns = null; private static final Set<String> FIELDS = new HashSet<String>(); - - private static Logger logger = LogUtil.create(SvtBiasConditionsLoader.class, new BasicLogFormatter(), Level.INFO); + private static Logger logger = LogUtil.create(SvtBiasConditionsLoader.class, new BasicLogFormatter(),Level.INFO); + + /** * Setup conditions. */ @@ -66,224 +63,292 @@ FIELDS.add("end_time"); } - private static IDataPoint addPoint(final IDataPointSet dps, final long mstime, final double val) { - final IDataPoint dp = dps.addPoint(); - dp.coordinate(0).setValue(mstime / 1000.); + /** + * Setup control plots. + */ + private static AIDA aida = AIDA.defaultInstance(); + static IDataPointSet dpsRuns = null; + static IDataPointSet dpsBiasRuns = null; + + private static void setupPlots(boolean show) { + IDataPointSetFactory dpsf = aida.analysisFactory().createDataPointSetFactory(aida.tree()); + dpsRuns = dpsf.create("dpsRuns","Run intervals", 2); + dpsBiasRuns = dpsf.create("dpsBiasRuns","Bias ON intervals associated with runs", 2); + IPlotter plotter = aida.analysisFactory().createPlotterFactory().create("Bias run ranges"); + IPlotterStyle plotterStyle = aida.analysisFactory().createPlotterFactory().createPlotterStyle(); + plotterStyle.xAxisStyle().setParameter("type", "date"); + plotter.createRegions(1, 3); + plotter.region(0).plot(dpsRuns,plotterStyle); + plotter.region(1).plot(dpsBiasRuns,plotterStyle); + plotter.region(2).plot(dpsRuns,plotterStyle); + plotter.region(2).plot(dpsBiasRuns,plotterStyle,"mode=overlay"); + if(show) plotter.show(); + + } + + private static IDataPoint addPoint(IDataPointSet dps, long mstime, double val) { + IDataPoint dp = dps.addPoint(); + dp.coordinate(0).setValue(mstime/1000.); dp.coordinate(1).setValue(val); return dp; } - - private final static SvtBiasConstantCollection findCollection(final List<SvtBiasConstantCollection> list, - final Date date) { - for (final SvtBiasConstantCollection collection : list) { - if (collection.find(date) != null) { - return collection; - } - } - return null; - } - + + + /** + * Default constructor + */ + public SvtBiasConditionsLoader() { + } + + + + /** * Check validity of @link RunData - * * @param data the @link RunData to check * @return <code>true</code> if valid, <code>false</code> otherwise. */ - private static boolean isValid(final RunData data) { - if (data.getStartDate() == null || data.getEndDate() == null || data.getStartDate().before(new Date(99, 1, 1))) { - logger.warning("This run data is not valid: " + data.toString()); + private static boolean isValid(RunData data) { + if(data.getStartDate() == null || data.getEndDate() == null || data.getStartDate().before(new Date(99,1,1))) { + logger.fine("This run data is not valid: " + data.toString()); return false; - } + } if (data.getStartDate().after(data.getEndDate())) { throw new RuntimeException("start date is after end date?!" + data.toString()); } return true; } - - private static final void loadToConditionsDB(final List<SvtBiasRunRange> ranges, final boolean doIt) { + + //private static Options options = null; + + + public static RunMap getRunMapFromSpreadSheet(String path) { + // Load in CSV records from the exported run spreadsheet. + logger.info(path); + final RunSpreadsheet runSheet = new RunSpreadsheet(new File(path)); + + // Find the run ranges that have the same fields values. + final List<RunRange> ranges = RunRange.findRunRanges(runSheet, FIELDS); + logger.info("Found " + ranges.size() + " ranges."); + for(RunRange range : ranges) logger.fine(range.toString()); + // find the run records (has converted dates and stuff) for these ranges + RunMap runmap = runSheet.getRunMap(ranges); + logger.info("Found " + runmap.size() + " runs in the run map."); + return runmap; + } + + public static List<SvtBiasRunRange> getBiasRunRanges(RunMap runmap, + SvtBiasMyaDumpReader biasMyaReader) { + List<SvtBiasRunRange> biasRunRanges = new ArrayList<SvtBiasRunRange>(); + // loop over runs from CSV + RunData prev = null; + for(Entry<Integer,RunData> entry : runmap.entrySet()) { + int run = entry.getKey(); + RunData data = entry.getValue(); + logger.fine("Processing " + run + " " + data.toString()); + + //check that data is ok + if (isValid(data)) { + if(prev!=null) { + if(isValid(prev)) { + if(prev.getEndDate().after(data.getStartDate())) { + throw new RuntimeException("prev end date after run started?: " + prev.toString() + " " + data.toString()); + } else if(prev.getStartDate().after(data.getEndDate())) { + throw new RuntimeException("prev start date before run ended?: " + prev.toString() + " " + data.toString()); + } + } + } + + // find the bias ranges applicable to this run + SvtBiasMyaRanges overlaps = biasMyaReader.findOverlappingRanges(data.getStartDate(), data.getEndDate()); + logger.fine("Found " + overlaps.size() + " overlapping bias ranges"); + logger.fine(overlaps.toString()); + + biasRunRanges.add(new SvtBiasRunRange(data,overlaps)); + prev = data; + + } + } + return biasRunRanges; + } + + + + + + /** + * 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.) + */ + public static void main(String[] args) { + + Options options = new Options(); + options.addOption(new Option("c", true, "CVS run file")); + options.addOption(new Option("m", true, "MYA dump file")); + options.addOption(new Option("g", false, "Actually load stuff into DB")); + options.addOption(new Option("s", false, "Show plots")); + + final CommandLineParser parser = new PosixParser(); + CommandLine cl = null; + try { + cl = parser.parse(options, args); + } catch (ParseException e) { + throw new RuntimeException("Cannot parse.", e); + } + + if(!cl.hasOption("c") || !cl.hasOption("m")) { + printUsage(options); + return; + } + + + + // Setup plots + setupPlots(cl.hasOption("s")?true:false); + + + // Load in CSV records from the exported run spreadsheet. + RunMap runmap = getRunMapFromSpreadSheet(cl.getOptionValue("c")); + + + // Load MYA dump + SvtBiasMyaDumpReader biasMyaReader = new SvtBiasMyaDumpReader(cl.getOptionValue("m")); + logger.info("Got " + biasMyaReader.getRanges().size() + " bias ranges"); + + + // Combine them to run ranges when bias was on + // each run may have multiple bias ranges + + List<SvtBiasRunRange> biasRunRanges = getBiasRunRanges(runmap,biasMyaReader); + + + + // fill graphs + if(cl.hasOption("s")) { + for(SvtBiasRunRange r : biasRunRanges) { + logger.info(r.toString()); + if(r.getRun().getRun()>5600) {//9999999999999.0) { + //if(dpsRuns.size()/4.0<500) {//9999999999999.0) { + addPoint(dpsRuns, r.getRun().getStartDate().getTime(),0.0); + addPoint(dpsRuns, r.getRun().getStartDate().getTime(),1.0); + addPoint(dpsRuns, r.getRun().getEndDate().getTime(),1.0); + addPoint(dpsRuns, r.getRun().getEndDate().getTime(),0.0); + + for(SvtBiasMyaRange br : r.getRanges()) { + addPoint(dpsBiasRuns, br.getStart().getDate().getTime(),0.0); + addPoint(dpsBiasRuns, br.getStart().getDate().getTime(),0.5); + addPoint(dpsBiasRuns, br.getEnd().getDate().getTime(),0.5); + addPoint(dpsBiasRuns, br.getEnd().getDate().getTime(),0.0); + } + + + } + + } + } + + + // load to DB + if(cl.hasOption("g")) { + loadToConditionsDB(biasRunRanges); + } + } + + + + private static void printUsage(Options options) { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp("Need to adhere to these options", options); + + } + + private final static SvtBiasConstantCollection findCollection(final List<SvtBiasConstantCollection> list, Date date) { + for( SvtBiasConstantCollection collection : list) { + if(collection.find(date) != null) { + return collection; + } + } + return null; + } + + private static final void loadToConditionsDB(List<SvtBiasRunRange> ranges) { logger.info("Load to DB..."); - + // Create a new collection for each run - final List<Integer> runsadded = new ArrayList<Integer>(); - - for (final SvtBiasRunRange range : ranges) { + List<Integer> runsadded = new ArrayList<Integer>(); + + + + + for(SvtBiasRunRange range : ranges) { logger.info("Loading " + range.toString()); - final RunData rundata = range.getRun(); - if (runsadded.contains(rundata.getRun())) { + RunData rundata = range.getRun(); + if(runsadded.contains(rundata.getRun())) { logger.warning("Run " + Integer.toString(rundata.getRun()) + " was already added?"); throw new RuntimeException("Run " + Integer.toString(rundata.getRun()) + " was already added?"); } runsadded.add(rundata.getRun()); - for (final SvtBiasMyaRange biasRange : range.getRanges()) { - // create a collection - final SvtBiasConstantCollection collection = new SvtBiasConstantCollection(); - // create a constant and add to the collection + + if(range.getRanges().isEmpty()) { + logger.info("No bias range for run " + range.getRun().getRun()); + continue; + } + + //create a collection + SvtBiasConstantCollection collection = new SvtBiasConstantCollection(); + int collectionId = -1; + try { + collectionId = MANAGER.getCollectionId(collection, "run ranges for SVT HV bias ON"); + } catch (SQLException e1) { + throw new RuntimeException(e1); + } + + collection.setCollectionId(collectionId); + + final ConditionsRecord condition = new ConditionsRecord(); + condition.setFieldValue("run_start", rundata.getRun()); + condition.setFieldValue("run_end", rundata.getRun()); + condition.setFieldValue("name", "svt_bias"); + condition.setFieldValue("table_name", "svt_bias_constants"); + condition.setFieldValue("notes", "constants from mya"); + condition.setFieldValue("created", new Date()); + condition.setFieldValue("created_by", System.getProperty("user.name")); + condition.setFieldValue("collection_id", collectionId); + + try { + for (SvtBiasMyaRange biasRange : range.getRanges()) { + //create a constant and add to the collection final SvtBiasConstant constant = new SvtBiasConstant(); constant.setFieldValue("start", biasRange.getStartDate()); constant.setFieldValue("end", biasRange.getEndDate()); constant.setFieldValue("value", biasRange.getStart().getValue()); + collection.add(constant); + + logger.info(condition.toString()); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + + try { try { - collection.add(constant); - } catch (final ConditionsObjectException e) { - throw new RuntimeException(e); + collection.insert(); + } catch (SQLException e) { + throw new RuntimeException("cant instert collection", e); } - - final ConditionsRecord condition = new ConditionsRecord(); - condition.setFieldValue("run_start", rundata.getRun()); - condition.setFieldValue("run_end", rundata.getRun()); - condition.setFieldValue("name", "svt_bias"); - condition.setFieldValue("table_name", "svt_bias"); - condition.setFieldValue("notes", "constants from mya"); - condition.setFieldValue("created", new Date()); - condition.setFieldValue("created_by", System.getProperty("user.name")); - - condition.setFieldValue("collection_id", collection.getCollectionId()); - - logger.info(condition.toString()); - - if (doIt) { - try { - condition.insert(); - } catch (final Exception e) { - throw new RuntimeException(e); - } - } - } - } - - } - - // private static Options options = null; - - /** - * 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.) - */ - public static void main(final String[] args) { - - final Options options = new Options(); - options.addOption(new Option("c", true, "CVS run file")); - options.addOption(new Option("m", true, "MYA dump file")); - options.addOption(new Option("g", false, "Actually load stuff into DB")); - options.addOption(new Option("s", false, "Show plots")); - - final CommandLineParser parser = new PosixParser(); - CommandLine cl = null; - try { - cl = parser.parse(options, args); - } catch (final ParseException e) { - throw new RuntimeException("Cannot parse.", e); - } - - // Setup plots - setupPlots(cl.hasOption("s") ? true : false); - - // Load in CSV records from the exported run spreadsheet. - final String path = cl.getOptionValue("c"); - logger.info(path); - final RunSpreadsheet runSheet = new RunSpreadsheet(new File(path)); - - // Find the run ranges that have the same fields values. - final List<RunRange> ranges = RunRange.findRunRanges(runSheet, FIELDS); - logger.info("Found " + ranges.size() + " ranges."); - for (final RunRange range : ranges) { - logger.info(range.toString()); - } - // find the run records (has converted dates and stuff) for these ranges - final RunMap runmap = runSheet.getRunMap(ranges); - logger.info("Found " + runmap.size() + " runs in the run map."); - - // Load MYA dump - final SvtBiasMyaDumpReader biasMyaReader = new SvtBiasMyaDumpReader(cl.getOptionValue("m")); - logger.info("Got " + biasMyaReader.getRanges().size() + " bias ranges"); - - // Combine them to run ranges when bias was on - // each run may have multiple bias ranges - - final List<SvtBiasRunRange> biasRunRanges = new ArrayList<SvtBiasRunRange>(); - // loop over runs from CSV - RunData prev = null; - for (final Entry<Integer, RunData> entry : runmap.entrySet()) { - final int run = entry.getKey(); - final RunData data = entry.getValue(); - logger.info("Processing " + run + " " + data.toString()); - - // check that data is ok - if (isValid(data)) { - if (prev != null) { - if (isValid(prev)) { - if (prev.getEndDate().after(data.getStartDate())) { - throw new RuntimeException("prev end date after run started?: " + prev.toString() + " " - + data.toString()); - } else if (prev.getStartDate().after(data.getEndDate())) { - throw new RuntimeException("prev start date before run ended?: " + prev.toString() + " " - + data.toString()); - } - } - } - - // find the bias ranges applicable to this run - final SvtBiasMyaRanges overlaps = biasMyaReader.findOverlappingRanges(data.getStartDate(), - data.getEndDate()); - logger.fine("Found " + overlaps.size() + " overlapping bias ranges"); - logger.fine(overlaps.toString()); - - biasRunRanges.add(new SvtBiasRunRange(data, overlaps)); - prev = data; - - } - } - - // fill graphs - if (cl.hasOption("s")) { - for (final SvtBiasRunRange r : biasRunRanges) { - logger.info(r.toString()); - if (r.getRun().getRun() > 5600) {// 9999999999999.0) { - // if(dpsRuns.size()/4.0<500) {//9999999999999.0) { - addPoint(dpsRuns, r.getRun().getStartDate().getTime(), 0.0); - addPoint(dpsRuns, r.getRun().getStartDate().getTime(), 1.0); - addPoint(dpsRuns, r.getRun().getEndDate().getTime(), 1.0); - addPoint(dpsRuns, r.getRun().getEndDate().getTime(), 0.0); - - for (final SvtBiasMyaRange br : r.getRanges()) { - addPoint(dpsBiasRuns, br.getStart().getDate().getTime(), 0.0); - addPoint(dpsBiasRuns, br.getStart().getDate().getTime(), 0.5); - addPoint(dpsBiasRuns, br.getEnd().getDate().getTime(), 0.5); - addPoint(dpsBiasRuns, br.getEnd().getDate().getTime(), 0.0); - } - - } - - } - } - - // load to DB - loadToConditionsDB(biasRunRanges, cl.hasOption("g") ? true : false); - - } - - private static void setupPlots(final boolean show) { - final IDataPointSetFactory dpsf = aida.analysisFactory().createDataPointSetFactory(aida.tree()); - dpsRuns = dpsf.create("dpsRuns", "Run intervals", 2); - dpsBiasRuns = dpsf.create("dpsBiasRuns", "Bias ON intervals associated with runs", 2); - final IPlotter plotter = aida.analysisFactory().createPlotterFactory().create("Bias run ranges"); - final IPlotterStyle plotterStyle = aida.analysisFactory().createPlotterFactory().createPlotterStyle(); - plotterStyle.xAxisStyle().setParameter("type", "date"); - plotter.createRegions(1, 3); - plotter.region(0).plot(dpsRuns, plotterStyle); - plotter.region(1).plot(dpsBiasRuns, plotterStyle); - plotter.region(2).plot(dpsRuns, plotterStyle); - plotter.region(2).plot(dpsBiasRuns, plotterStyle, "mode=overlay"); - if (show) { - plotter.show(); - } - - } - - /** - * Default constructor - */ - public SvtBiasConditionsLoader() { - } + condition.insert(); + + } catch (DatabaseObjectException | SQLException e) { + throw new RuntimeException(e); + } + + } + + + } + + + + } Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/svt/SvtBiasConstant.java ============================================================================= --- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/svt/SvtBiasConstant.java (original) +++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/svt/SvtBiasConstant.java Mon Jul 6 15:27:19 2015 @@ -10,12 +10,13 @@ import org.hps.conditions.database.Table; /** + * * Encapsulates an SVT bias constant, which is range in time where bias was ON. - * - * @author Per Hansson Adrian, SLAC + * + * @author Per Hansson Adrian <[log in to unmask]> */ -@Table(names = "svt_bias") -@Converter(multipleCollectionsAction = MultipleCollectionsAction.LAST_UPDATED) +@Table(names = "svt_bias_constants") +@Converter(multipleCollectionsAction = MultipleCollectionsAction.LAST_CREATED) public final class SvtBiasConstant extends BaseConditionsObject { /** @@ -23,51 +24,52 @@ */ @SuppressWarnings("serial") public static class SvtBiasConstantCollection extends BaseConditionsObjectCollection<SvtBiasConstant> { - + /** * Find bias constant by date. - * + * * @param date the offset * @return the constant containing the date or <code>null</code> otherwise. + * */ - public SvtBiasConstant find(final Date date) { - for (final SvtBiasConstant constant : this) { - if (date.after(constant.getStart()) && date.before(constant.getEnd())) { + public SvtBiasConstant find(Date date) { + for (SvtBiasConstant constant : this) { + if(date.after(constant.getStart()) && date.before(constant.getEnd())) { return constant; } } return null; } } + + + /** + * The start date. + * + * @return the start date + */ + @Field(names = {"start"}) + public Date getStart() { + return getFieldValue("start"); + } /** - * The end date. + * The end date. * * @return the end date */ @Field(names = {"end"}) public Date getEnd() { - return this.getFieldValue("end"); + return getFieldValue("end"); } /** - * The start date. - * - * @return the start date - */ - @Field(names = {"start"}) - public Date getStart() { - return this.getFieldValue("start"); - } - - /** - * The bias value . - * + * The bias value. + * * @return the bias value */ @Field(names = {"value"}) - public double getValue() { - return this.getFieldValue("value"); + public Double getValue() { + return getFieldValue("value"); } - } Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/svt/SvtBiasMyaDumpReader.java ============================================================================= --- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/svt/SvtBiasMyaDumpReader.java (original) +++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/svt/SvtBiasMyaDumpReader.java Mon Jul 6 15:27:19 2015 @@ -9,288 +9,284 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.TimeZone; import java.util.logging.Level; import java.util.logging.Logger; +import org.hps.conditions.run.RunSpreadsheet; import org.hps.conditions.run.RunSpreadsheet.RunData; import org.hps.util.BasicLogFormatter; import org.lcsim.util.log.LogUtil; -/** - * @author Per Hansson Adrian, SLAC - */ + + public class SvtBiasMyaDumpReader { - - public static final class SvtBiasMyaEntry { - private final Date date; - private final String name; - private final double value; - - public SvtBiasMyaEntry(final String name, final Date date, final double value) { - this.date = date; - this.name = name; - this.value = value; - } - - public Date getDate() { - return this.date; - } - - public double getValue() { - return this.value; - } - - @Override - public String toString() { - return this.name + " " + this.date.toString() + " value " + this.value; - } - } - - public static class SvtBiasMyaRange { - private SvtBiasMyaEntry end; - private SvtBiasMyaEntry start; - - public SvtBiasMyaRange() { - } - - public SvtBiasMyaRange(final SvtBiasMyaEntry start) { - this.start = start; - } - - public SvtBiasMyaEntry getEnd() { - return this.end; - } - - public Date getEndDate() { - return this.getEnd().getDate(); - } - - public SvtBiasMyaEntry getStart() { - return this.start; - } - - public Date getStartDate() { - return this.getStart().getDate(); - } - - public boolean overlap(final Date date_start, final Date date_end) { - if (date_end.before(this.getStartDate())) { - return false; - } else if (date_start.after(this.getEndDate())) { - return false; - } - return true; - } - - public void setEnd(final SvtBiasMyaEntry end) { - this.end = end; - } - - public void setStart(final SvtBiasMyaEntry start) { - this.start = start; - } - - @Override - public String toString() { - return "START: " + this.start.toString() + " END: " + this.end.toString(); - } - } - - public static final class SvtBiasMyaRanges extends ArrayList<SvtBiasMyaRange> { - public SvtBiasMyaRanges() { - } - - public SvtBiasMyaRanges findOverlappingRanges(final Date date_start, final Date date_end) { - logger.fine("look for overlaps from " + date_start.toString() + " to " + date_end.toString()); - final SvtBiasMyaRanges overlaps = new SvtBiasMyaRanges(); - for (final SvtBiasMyaRange range : this) { - logger.fine("loop bias range " + range.toString()); - if (range.overlap(date_start, date_end)) { - overlaps.add(range); - logger.fine("overlap found!! "); - } - } - return overlaps; - } - - @Override - public String toString() { - final StringBuffer sb = new StringBuffer(); - for (final SvtBiasMyaRange range : this) { - sb.append(range.toString() + "\n"); - } - return sb.toString(); - } - } - - public static final class SvtBiasRunRange { - private SvtBiasMyaRanges ranges; - private RunData run; - - public SvtBiasRunRange(final RunData run, final SvtBiasMyaRanges ranges) { - this.setRun(run); - this.setRanges(ranges); - } - - public SvtBiasMyaRanges getRanges() { - return this.ranges; - } - - public RunData getRun() { - return this.run; - } - - public void setRanges(final SvtBiasMyaRanges ranges) { - this.ranges = ranges; - } - - public void setRun(final RunData run) { - this.run = run; - } - - @Override - public String toString() { - final StringBuffer sb = new StringBuffer(); - sb.append("\nRun " + this.run.toString() + ":"); - for (final SvtBiasMyaRange r : this.ranges) { - sb.append("\n" + r.toString()); - } - return sb.toString(); - } - } - - private static final double BIASVALUEON = 178.0; - private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - + private static Logger logger = LogUtil.create(SvtBiasMyaDumpReader.class, new BasicLogFormatter(), Level.INFO); - public static void main(final String[] args) { - - final SvtBiasMyaDumpReader dumpReader = new SvtBiasMyaDumpReader(args); - + + public static void main(String[] args) { + + SvtBiasMyaDumpReader dumpReader = new SvtBiasMyaDumpReader(args); + dumpReader.printRanges(); - - } - - protected static List<SvtBiasMyaEntry> readMyaDump(final File file) { - - final List<SvtBiasMyaEntry> myaEntries = new ArrayList<SvtBiasMyaEntry>(); + + + } + + private static final SimpleDateFormat DATE_FORMAT = new RunSpreadsheet.AnotherSimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + //private static final TimeZone timeZone = TimeZone.getTimeZone("EST"); + public static final double BIASVALUEON = 178.0; + private List<SvtBiasMyaEntry> myaEntries = new ArrayList<SvtBiasMyaEntry>(); + private SvtBiasMyaRanges biasRanges = new SvtBiasMyaRanges(); + + public SvtBiasMyaDumpReader() { + } + + public SvtBiasMyaRanges findOverlappingRanges(Date date_start, Date date_end) { + return this.biasRanges.findOverlappingRanges(date_start, date_end); + } + + private void readFromFile(File file) { + addEntries(readMyaDump(file)); + logger.info("Got " + getEntries().size() + " entries from " + file.getName()); + + } + public void buildFromFiles(String[] args) { + for( int i=0; i<args.length; ++i) { + readFromFile(new File(args[i])); + } + buildRanges(); + } + + public SvtBiasMyaDumpReader(String[] args) { + buildFromFiles(args); + } + + public SvtBiasMyaDumpReader(String filepath) { + String[] files = {filepath}; + buildFromFiles(files); + } + + + public void addEntry(SvtBiasMyaEntry e) { + this.myaEntries.add(e); + } + + public void addEntries(List<SvtBiasMyaEntry> e) { + this.myaEntries.addAll(e); + } + + public List<SvtBiasMyaEntry> getEntries() { + return this.myaEntries; + } + + public SvtBiasMyaRanges getRanges() { + return this.biasRanges; + } + + + private void printRanges() { + for( SvtBiasMyaRange r : biasRanges) { + logger.info(r.toString()); + } + } + + + protected static List<SvtBiasMyaEntry> readMyaDump(File file) { + + List<SvtBiasMyaEntry> myaEntries = new ArrayList<SvtBiasMyaEntry>(); try { - final BufferedReader br = new BufferedReader(new FileReader(file)); + BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { - // System.out.println(line); - final String arr[] = line.split(" "); + //System.out.println(line); + String arr[] = line.split(" "); try { - - if (arr.length < 3) { - throw new ParseException("this line is not correct.", 0); + + if(arr.length<3) { + throw new ParseException("this line is not correct.",0); } - final Date date = DATE_FORMAT.parse(arr[0] + " " + arr[1]); - final double value = Double.parseDouble(arr[2]); - final SvtBiasMyaEntry entry = new SvtBiasMyaEntry(file.getName(), date, value); + + Date date = DATE_FORMAT.parse(arr[0] + " " + arr[1]); + double value = Double.parseDouble(arr[2]); + SvtBiasMyaEntry entry = new SvtBiasMyaEntry(file.getName(), date, value); myaEntries.add(entry); - } catch (final ParseException e) { + } catch (ParseException e) { e.printStackTrace(); } } br.close(); - } catch (final IOException e) { + } catch (IOException e) { e.printStackTrace(); } return myaEntries; } - - private final SvtBiasMyaRanges biasRanges = new SvtBiasMyaRanges(); - - private final List<SvtBiasMyaEntry> myaEntries = new ArrayList<SvtBiasMyaEntry>(); - - public SvtBiasMyaDumpReader() { - } - - public SvtBiasMyaDumpReader(final String filepath) { - final String[] files = {filepath}; - this.buildFromFiles(files); - } - - public SvtBiasMyaDumpReader(final String[] args) { - this.buildFromFiles(args); - } - - public void addEntries(final List<SvtBiasMyaEntry> e) { - this.myaEntries.addAll(e); - } - - public void addEntry(final SvtBiasMyaEntry e) { - this.myaEntries.add(e); - } - - public void buildFromFiles(final String[] args) { - for (final String arg : args) { - this.readFromFile(new File(arg)); - } - this.buildRanges(); - } - + public void buildRanges() { SvtBiasMyaRange range = null; SvtBiasMyaEntry eprev = null; - for (final SvtBiasMyaEntry e : this.myaEntries) { - - // System.out.println(e.toString()); - - if (eprev != null) { - if (e.getDate().before(eprev.getDate())) { + for(SvtBiasMyaEntry e : this.myaEntries) { + + //System.out.println(e.toString()); + + if(eprev!=null) { + if(e.getDate().before(eprev.getDate())) { throw new RuntimeException("date list is not ordered: " + eprev.toString() + " vs " + e.toString()); } } - - if (e.getValue() > BIASVALUEON) { - if (range == null) { + + if( e.getValue() > BIASVALUEON) { + if (range==null) { logger.fine("BIAS ON: " + e.toString()); range = new SvtBiasMyaRange(); range.setStart(e); - } + } } else { - // close it - if (range != null) { + //close it + if (range!=null) { logger.fine("BIAS TURNED OFF: " + e.toString()); range.setEnd(e); this.biasRanges.add(range); range = null; } - } + } eprev = e; } logger.info("Built " + this.biasRanges.size() + " ranges"); - - } - - public SvtBiasMyaRanges findOverlappingRanges(final Date date_start, final Date date_end) { - return this.biasRanges.findOverlappingRanges(date_start, date_end); - } - - public List<SvtBiasMyaEntry> getEntries() { - return this.myaEntries; - } - - public SvtBiasMyaRanges getRanges() { - return this.biasRanges; - } - - private void printRanges() { - for (final SvtBiasMyaRange r : this.biasRanges) { - logger.info(r.toString()); - } - } - - private void readFromFile(final File file) { - this.addEntries(readMyaDump(file)); - logger.info("Got " + this.getEntries().size() + " entries from " + file.getName()); - - } + + } + + + public static final class SvtBiasMyaEntry { + private Date date; + private String name; + private double value; + public SvtBiasMyaEntry(String name, Date date, double value) { + this.date = date; + this.name = name; + this.value = value; + } + public double getValue() { + return value; + } + public Date getDate() { + return this.date; + } + public String toString() { + return name + " " + date.toString() + " (epoch " + Long.toString(date.getTime()) + ")" + " value " + value; + } + } + + + + public static final class SvtBiasMyaRanges extends ArrayList<SvtBiasMyaRange> { + public SvtBiasMyaRanges() {} + public SvtBiasMyaRanges findOverlappingRanges(Date date_start, Date date_end) { + logger.fine("look for overlaps from " + date_start.toString() + " to " + date_end.toString()); + SvtBiasMyaRanges overlaps = new SvtBiasMyaRanges(); + for(SvtBiasMyaRange range : this) { + logger.fine("loop bias range " + range.toString()); + if( range.overlap(date_start,date_end) ) { + overlaps.add(range); + logger.fine("overlap found!! "); + } + } + return overlaps; + } + public String toString() { + StringBuffer sb = new StringBuffer(); + for(SvtBiasMyaRange range : this) { + sb.append(range.toString() + "\n"); + } + return sb.toString(); + } + + public boolean includes(Date date) { + for(SvtBiasMyaRange r : this) { + if(r.includes(date)) return true; + } + return false; + } + + } + + public static class SvtBiasMyaRange { + private SvtBiasMyaEntry start; + private SvtBiasMyaEntry end; + public SvtBiasMyaRange() {} + public Date getStartDate() { + return getStart().getDate(); + } + public Date getEndDate() { + return getEnd().getDate(); + } + public boolean overlap(Date date_start, Date date_end) { + if( date_end.before(getStartDate()) ) { + return false; + } else if ( date_start.after(getEndDate())) { + return false; + } + return true; + } + public SvtBiasMyaRange(SvtBiasMyaEntry start) { + this.start = start; + } + public SvtBiasMyaEntry getEnd() { + return end; + } + public void setEnd(SvtBiasMyaEntry end) { + this.end = end; + } + public SvtBiasMyaEntry getStart() { + return start; + } + public void setStart(SvtBiasMyaEntry start) { + this.start = start; + } + public String toString() { + return "START: " + start.toString() + " END: " + end.toString(); + } + public boolean includes(Date date) { + if( date.before(getStartDate()) || date.after(getEndDate()) ) { + return false; + } else { + return true; + } + } + } + + public static final class SvtBiasRunRange { + private RunData run; + private SvtBiasMyaRanges ranges; + public SvtBiasRunRange(RunData run, SvtBiasMyaRanges ranges) { + setRun(run); + setRanges(ranges); + } + public RunData getRun() { + return run; + } + public void setRun(RunData run) { + this.run = run; + } + public SvtBiasMyaRanges getRanges() { + return ranges; + } + public void setRanges(SvtBiasMyaRanges ranges) { + this.ranges = ranges; + } + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("\nRun " + run.toString() + ":"); + for (SvtBiasMyaRange r : ranges) { + sb.append("\n" + r.toString()); + } + return sb.toString(); + } + } + }