Skip to content

Commit 79f325d

Browse files
authored
feat: cache unix times and write RUN::unix bank (#1278)
* cache unix times and write RUN::unix bank * add RUN::unix bankdef
1 parent da27c6a commit 79f325d

2 files changed

Lines changed: 45 additions & 11 deletions

File tree

common-tools/clara-io/src/main/java/org/jlab/io/clara/DecoderWriter.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.nio.file.Path;
5+
import java.util.TreeMap;
56
import java.util.TreeSet;
67
import org.jlab.analysis.postprocess.Processor;
78
import org.jlab.clara.std.services.EventWriterException;
@@ -38,6 +39,7 @@ public class DecoderWriter extends HipoToHipoWriter {
3839
Bank runConfig;
3940
Bank helicityAdc;
4041
ConstantsManager conman;
42+
TreeMap<Integer,Integer> eventUnix;
4143
TreeSet<HelicityState> helicities;
4244
DaqScalersSequence scalers;
4345
SchemaFactory fullSchema;
@@ -51,6 +53,7 @@ private void init(JSONObject opts) {
5153
helicities = new TreeSet<>();
5254
scalers = new DaqScalersSequence(fullSchema);
5355
conman = new ConstantsManager();
56+
eventUnix = new TreeMap<>();
5457
conman.init("/runcontrol/hwp","/runcontrol/helicity");
5558
postprocess = opts.optBoolean("postprocess", false);
5659
if (opts.has("variation")) conman.setVariation(opts.getString("variation"));
@@ -73,37 +76,37 @@ protected HipoWriterSorted createWriter(Path file, JSONObject opts) throws Event
7376
}
7477
}
7578

76-
/**
77-
* In addition to writing the incoming event, copies all tag-1 banks to new
78-
* tag-1 events and writes them, and stores helicity/scaler readings for later.
79-
* @param event
80-
* @throws EventWriterException
81-
*/
8279
@Override
8380
protected void writeEvent(Object event) throws EventWriterException {
8481
scalers.add((Event)event);
8582
((Event)event).read(runConfig);
8683
((Event)event).read(helicityAdc);
84+
if (runConfig.getRows() > 0) {
85+
int unix = runConfig.getInt("unixtime",0);
86+
int evno = runConfig.getInt("event",0);
87+
if (unix > 0 && evno > 0) eventUnix.put(evno, unix);
88+
}
8789
helicities.add(HelicityState.createFromFadcBank(helicityAdc, runConfig, conman));
8890
Event t = CLASDecoder4.createTaggedEvent((Event)event, runConfig, tag1banks);
8991
if (!t.isEmpty()) writer.addEvent(t, 1);
9092
super.writeEvent(event);
9193
}
9294

93-
/**
94-
* In addition to closing the writer, creates and writes tag-1 events with
95-
* HEL::flip bnks and clears old scaler/helicity readings.
96-
*/
9795
@Override
9896
protected void closeWriter() {
9997
HelicitySequence.writeFlips(fullSchema, writer, helicities);
98+
writer.addEvent(getUnixEvent(runConfig),1);
10099
super.closeWriter();
101100
if (postprocess) postprocess();
102101
// keep the latest helicity/scaler reading for the next file:
103102
while (helicities.size() > 60) helicities.pollFirst();
104103
scalers.clear(10);
105104
}
106-
105+
106+
/**
107+
* Get the first valid run number from a RUN::config bank.
108+
* @return run
109+
*/
107110
private int getRunNumber() {
108111
Event e = new Event();
109112
HipoReader r = new HipoReader();
@@ -117,6 +120,27 @@ private int getRunNumber() {
117120
return 0;
118121
}
119122

123+
/**
124+
* Get a new event with a RUN::unix bank containing event-timestamp mapping,
125+
* and the latest RUN::config bank.
126+
* @param config
127+
* @return
128+
*/
129+
private Event getUnixEvent(Bank config) {
130+
Bank unix = new Bank(fullSchema.getSchema("RUN::unix"));
131+
unix.setRows(eventUnix.size());
132+
int row = 0;
133+
for (int evno : eventUnix.keySet()) {
134+
unix.putInt("event", row, evno);
135+
unix.putInt("unixtime",row, eventUnix.get(evno));
136+
row++;
137+
}
138+
Event e = new Event();
139+
e.write(config);
140+
e.write(unix);
141+
return e;
142+
}
143+
120144
/**
121145
* Copy helicity/charge tag-1 information to all events.
122146
*/

etc/bankdefs/hipo4/header.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,15 @@
8181
"entries":[
8282
{ "name":"json" , "type":"B", "info":"json character" }
8383
]
84+
},
85+
{
86+
"name" : "RUN::unix",
87+
"group": 10000,
88+
"item" : 18,
89+
"info" : "Mapping between CODA event number and unix time",
90+
"entries":[
91+
{ "name":"event" , "type":"I", "info":"event number, from RUN::config.event" },
92+
{ "name":"unixtime" , "type":"I", "info":"unix time in seconds, from RUN::config.unixtime" }
93+
]
8494
}
8595
]

0 commit comments

Comments
 (0)