Skip to content

Commit ad23874

Browse files
port TI time offset to conditions system from run db (left in run db API and database for backward compatibility)
1 parent fd53558 commit ad23874

7 files changed

Lines changed: 110 additions & 37 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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
@Table(names = {"ti_time_offsets"})
9+
@Converter(converter = TiTimeOffsetConverter.class)
10+
public class TiTimeOffset extends BaseConditionsObject {
11+
12+
// FIXME: This is not actually used but it is here to make the conditions manager happy.
13+
public static class TiTimeOffsetCollection extends BaseConditionsObjectCollection<TiTimeOffset> {
14+
}
15+
16+
private Long value = null;
17+
18+
TiTimeOffset(Long value) {
19+
this.value = value;
20+
}
21+
22+
public Long getValue() {
23+
return value;
24+
}
25+
}
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
*

0 commit comments

Comments
 (0)