Skip to content

Commit e0992c2

Browse files
authored
Merge pull request #70 from JeffersonLab/iss69
Add TI time offset to conditions system
2 parents fe7b40a + a13c53a commit e0992c2

8 files changed

Lines changed: 119 additions & 57 deletions

File tree

conditions/src/main/java/org/hps/conditions/database/DatabaseConditionsManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ private void registerConverters() {
947947
* @return the <code>ResultSet</code> from the query
948948
* @throws RuntimeException if there is a query error
949949
*/
950-
ResultSet selectQuery(final String query) {
950+
public ResultSet selectQuery(final String query) {
951951
LOGGER.fine("executing SQL select query ..." + '\n' + query);
952952
ResultSet result = null;
953953
Statement statement = null;

conditions/src/main/java/org/hps/conditions/database/DatabaseUtilities.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public final class DatabaseUtilities {
1616
*
1717
* @param resultSet the database <code>ResultSet</code>
1818
*/
19-
static void cleanup(final ResultSet resultSet) {
19+
public static void cleanup(final ResultSet resultSet) {
2020
Statement statement = null;
2121
try {
2222
statement = resultSet.getStatement();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.hps.conditions.trigger;
2+
3+
import org.hps.conditions.api.BaseConditionsObject;
4+
import org.hps.conditions.api.BaseConditionsObjectCollection;
5+
import org.hps.conditions.database.Converter;
6+
import org.hps.conditions.database.Table;
7+
8+
/**
9+
* <p>
10+
* Represents the per-run trigger time offset in nanoseconds.
11+
* </p>
12+
* <p>
13+
* A single instance of this class is returned for an entire run.
14+
* </p>
15+
*/
16+
@Table(names = {"ti_time_offsets"})
17+
@Converter(converter = TiTimeOffsetConverter.class)
18+
public class TiTimeOffset extends BaseConditionsObject {
19+
20+
// FIXME: This is not actually used but it is here to make the conditions manager happy.
21+
public static class TiTimeOffsetCollection extends BaseConditionsObjectCollection<TiTimeOffset> {
22+
}
23+
24+
private Long value = null;
25+
26+
TiTimeOffset(Long value) {
27+
this.value = value;
28+
}
29+
30+
public Long getValue() {
31+
return value;
32+
}
33+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.hps.conditions.trigger;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
6+
import org.hps.conditions.database.AbstractConditionsObjectConverter;
7+
import org.hps.conditions.database.DatabaseConditionsManager;
8+
import org.hps.conditions.database.DatabaseUtilities;
9+
import org.lcsim.conditions.ConditionsManager;
10+
11+
public class TiTimeOffsetConverter extends AbstractConditionsObjectConverter<TiTimeOffset> {
12+
13+
public TiTimeOffset getData(final ConditionsManager manager, final String name) {
14+
15+
final DatabaseConditionsManager databaseConditionsManager = DatabaseConditionsManager.getInstance();
16+
17+
// Setup connection if necessary.
18+
boolean reopenedConnection = false;
19+
if (!databaseConditionsManager.isConnected()) {
20+
databaseConditionsManager.openConnection();
21+
reopenedConnection = true;
22+
}
23+
24+
final String query = "SELECT ti_time_offset from ti_time_offsets WHERE run = " + manager.getRun();
25+
final ResultSet resultSet = databaseConditionsManager.selectQuery(query);
26+
TiTimeOffset t = null;
27+
try {
28+
if (resultSet.next()) {
29+
t = new TiTimeOffset(resultSet.getLong(1));
30+
} else {
31+
throw new RuntimeException("No TiTimeOffset condition exists for run " + manager.getRun());
32+
}
33+
} catch (SQLException e) {
34+
throw new RuntimeException(e);
35+
}
36+
37+
DatabaseUtilities.cleanup(resultSet);
38+
39+
if (reopenedConnection) {
40+
databaseConditionsManager.closeConnection();
41+
}
42+
43+
return t;
44+
}
45+
46+
public Class<TiTimeOffset> getType() {
47+
return TiTimeOffset.class;
48+
}
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.hps.conditions.trigger;
2+
3+
import junit.framework.TestCase;
4+
5+
import org.hps.conditions.database.DatabaseConditionsManager;
6+
import org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException;
7+
8+
public final class TiTimeOffsetTest extends TestCase {
9+
10+
private static final int RUN_NUMBER = 5772;
11+
12+
private static DatabaseConditionsManager conditionsManager;
13+
14+
public void setUp() {
15+
conditionsManager = DatabaseConditionsManager.getInstance();
16+
try {
17+
conditionsManager.setDetector("HPS-PhysicsRun2016-Nominal-v4-4", RUN_NUMBER);
18+
} catch (final ConditionsNotFoundException e) {
19+
throw new RuntimeException(e);
20+
}
21+
}
22+
23+
public void testTiTimeOffset() {
24+
System.out.println("Loading TI time offset for run " + conditionsManager.getRun());
25+
final TiTimeOffset t =
26+
conditionsManager.getCachedConditions(TiTimeOffset.class, "ti_time_offsets").getCachedData();
27+
System.out.println("run " + RUN_NUMBER + "; ti_time_offset = " + t.getValue());
28+
}
29+
}

evio/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
<groupId>org.hps</groupId>
2020
<artifactId>hps-job</artifactId>
2121
</dependency>
22-
<dependency>
23-
<groupId>org.hps</groupId>
24-
<artifactId>hps-run-database</artifactId>
25-
</dependency>
2622
<dependency>
2723
<groupId>org.hps</groupId>
2824
<artifactId>hps-tracking</artifactId>

evio/src/main/java/org/hps/evio/LCSimEngRunEventBuilder.java

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.logging.Level;
66
import java.util.logging.Logger;
77

8+
import org.hps.conditions.trigger.TiTimeOffset;
89
import org.hps.record.epics.EpicsData;
910
import org.hps.record.epics.EpicsEvioProcessor;
1011
import org.hps.record.evio.EvioEventUtilities;
@@ -17,9 +18,9 @@
1718
import org.hps.record.triggerbank.SSPData;
1819
import org.hps.record.triggerbank.TDCData;
1920
import org.hps.record.triggerbank.TIData;
20-
import org.hps.rundb.RunManager;
2121
import org.jlab.coda.jevio.EvioEvent;
2222
import org.lcsim.conditions.ConditionsEvent;
23+
import org.lcsim.conditions.ConditionsManager;
2324
import org.lcsim.event.EventHeader;
2425

2526
/**
@@ -99,38 +100,11 @@ public void conditionsChanged(final ConditionsEvent conditionsEvent) {
99100
svtEventFlagger.initialize();
100101

101102
// Set TI time offset from run database.
102-
setTiTimeOffsetForRun(conditionsEvent.getConditionsManager().getRun());
103+
ConditionsManager mgr = conditionsEvent.getConditionsManager();
104+
TiTimeOffset t = mgr.getCachedConditions(TiTimeOffset.class, "ti_time_offsets").getCachedData();
105+
currentTiTimeOffset = t.getValue();
103106
}
104107

105-
/**
106-
* Get TI time offset from the run database, if available.
107-
* @param run the run number
108-
*/
109-
private void setTiTimeOffsetForRun(int run) {
110-
currentTiTimeOffset = null;
111-
RunManager runManager = RunManager.getRunManager();
112-
if (runManager.getRun() != null) {
113-
if (runManager.runExists()) {
114-
currentTiTimeOffset = runManager.getRunSummary().getTiTimeOffset();
115-
LOGGER.info("TI time offset set to " + currentTiTimeOffset + " for run "
116-
+ run + " from database");
117-
} else {
118-
LOGGER.warning("Run " + run
119-
+ " does not exist in the run database.");
120-
}
121-
} else {
122-
LOGGER.info("Run manager is not initialized; TI time offset not available.");
123-
}
124-
/* Make sure connection is closed immediately. --JM */
125-
try {
126-
LOGGER.info("Closing run manager db connection ...");
127-
RunManager.getRunManager().closeConnection();
128-
LOGGER.info("Run manager db connection was closed.");
129-
} catch (Exception e) {
130-
e.printStackTrace();
131-
}
132-
}
133-
134108
/**
135109
* Get the time from the TI data with time offset applied from run database.
136110
*

run-database/src/main/java/org/hps/rundb/RunManager.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.hps.rundb;
22

3-
import java.io.File;
43
import java.sql.Connection;
54
import java.sql.SQLException;
65
import java.util.List;
@@ -22,11 +21,6 @@
2221
* @author jeremym
2322
*/
2423
public final class RunManager implements ConditionsListener {
25-
26-
/**
27-
* Name of system property that can be used to specify custom database connection parameters.
28-
*/
29-
private static final String CONNECTION_PROPERTY_FILE = "org.hps.conditions.connection.file";
3024

3125
/**
3226
* The default connection parameters for read-only access to the run database.
@@ -95,20 +89,7 @@ public RunManager(final Connection connection) {
9589
/**
9690
* Class constructor using default connection parameters.
9791
*/
98-
public RunManager() {
99-
100-
/**
101-
* Read database URL from system prop setting.
102-
* The database, password and user from that file are overridden.
103-
*/
104-
if (System.getProperties().get(CONNECTION_PROPERTY_FILE) != null) {
105-
final String propFile = (String) System.getProperties().get(CONNECTION_PROPERTY_FILE);
106-
this.connectionParameters = ConnectionParameters.fromProperties(new File(propFile));
107-
this.connectionParameters.setDatabase("hps_run_db_v2");
108-
this.connectionParameters.setPassword("darkphoton");
109-
this.connectionParameters.setUser("hpsuser");
110-
111-
}
92+
public RunManager() {
11293
this.connection = this.connectionParameters.createConnection();
11394
factory = new DaoProvider(this.connection);
11495
LOGGER.log(Level.INFO, this.connectionParameters.toString());

0 commit comments

Comments
 (0)