Skip to content

Commit 9615402

Browse files
authored
separate the serial post-processing for re-usability (#1413)
* add serial class * separate "serial" functionality for reusability
1 parent e6060b9 commit 9615402

2 files changed

Lines changed: 107 additions & 56 deletions

File tree

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

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
import java.util.TreeSet;
88
import org.jlab.clara.std.services.EventWriterException;
99
import org.jlab.detector.calib.utils.ConstantsManager;
10-
import org.jlab.detector.decode.CLASDecoder4;
11-
import org.jlab.detector.helicity.HelicitySequence;
1210
import org.jlab.detector.helicity.HelicitySequenceDelayed;
13-
import org.jlab.detector.helicity.HelicityState;
14-
import org.jlab.detector.scalers.DaqScalersSequence;
11+
import org.jlab.detector.serial.SerialHoncho;
1512
import org.jlab.detector.serial.PostProcessor;
1613
import org.jlab.jnp.hipo4.data.Bank;
1714
import org.jlab.jnp.hipo4.data.Event;
@@ -33,34 +30,22 @@
3330
*/
3431
public class Clas12Writer extends HipoToHipoWriter {
3532

36-
static final String[] TAG1BANKS = {"RUN::scaler","HEL::scaler","RAW::scaler","RAW::epics","HEL::flip","COAT::config"};
37-
38-
Bank[] tag1banks;
33+
SerialHoncho serial;
3934
Bank runConfig;
40-
Bank helicityAdc;
4135
ConstantsManager conman;
42-
TreeMap<Integer,Integer> eventUnix;
43-
TreeSet<HelicityState> helicities;
44-
DaqScalersSequence scalers;
4536
SchemaFactory fullSchema;
4637
boolean postprocess;
4738

4839
private void init(JSONObject opts) {
4940
fullSchema = new SchemaFactory();
5041
fullSchema.initFromDirectory(FileUtils.getEnvironmentPath("CLAS12DIR","etc/bankdefs/hipo4"));
42+
serial = new SerialHoncho(fullSchema);
5143
runConfig = new Bank(fullSchema.getSchema("RUN::config"));
52-
helicityAdc = new Bank(fullSchema.getSchema("HEL::adc"));
53-
helicities = new TreeSet<>();
54-
scalers = new DaqScalersSequence(fullSchema);
5544
conman = new ConstantsManager();
56-
eventUnix = new TreeMap<>();
5745
conman.init("/runcontrol/hwp","/runcontrol/helicity");
5846
postprocess = opts.optBoolean("postprocess", false);
5947
if (opts.has("variation")) conman.setVariation(opts.getString("variation"));
6048
if (opts.has("timestamp")) conman.setTimeStamp(opts.getString("timestamp"));
61-
tag1banks = new Bank[TAG1BANKS.length];
62-
for (int i=0; i<tag1banks.length; ++i)
63-
tag1banks[i] = new Bank(fullSchema.getSchema(TAG1BANKS[i]));
6449
}
6550

6651
@Override
@@ -78,29 +63,17 @@ protected HipoWriterSorted createWriter(Path file, JSONObject opts) throws Event
7863

7964
@Override
8065
protected void writeEvent(Object event) throws EventWriterException {
81-
scalers.add((Event)event);
82-
((Event)event).read(runConfig);
83-
((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-
}
89-
helicities.add(HelicityState.createFromFadcBank(helicityAdc, runConfig, conman));
90-
Event t = CLASDecoder4.createTaggedEvent((Event)event, runConfig, tag1banks);
66+
Event t = serial.read((Event)event);
9167
if (!t.isEmpty()) writer.addEvent(t, 1);
9268
super.writeEvent(event);
9369
}
9470

9571
@Override
9672
protected void closeWriter() {
97-
HelicitySequence.writeFlips(fullSchema, writer, helicities);
98-
writer.addEvent(getUnixEvent(runConfig),1);
73+
serial.finish(writer);
9974
super.closeWriter();
10075
if (postprocess) postprocess();
101-
// keep the latest helicity/scaler reading for the next file:
102-
while (helicities.size() > 60) helicities.pollFirst();
103-
scalers.clear(10);
76+
serial.clear();
10477
}
10578

10679
/**
@@ -120,35 +93,14 @@ private int getRunNumber() {
12093
return 0;
12194
}
12295

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-
14496
/**
14597
* Copy helicity/charge tag-1 information to all events.
14698
*/
14799
private void postprocess() {
148100
int d = conman.getConstants(getRunNumber(), "/runcontrol/helicity").getIntValue("delay",0,0,0);
149101
HelicitySequenceDelayed helicity = new HelicitySequenceDelayed(d);
150-
helicity.addStream(helicities);
151-
PostProcessor p = new PostProcessor(List.of(filename), fullSchema, helicity, scalers);
102+
helicity.addStream(serial.getHelicities());
103+
PostProcessor p = new PostProcessor(List.of(filename), fullSchema, helicity, serial.getScalers());
152104
HipoReader r = new HipoReader();
153105
r.open(filename);
154106
Event e = new Event();
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.jlab.detector.serial;
2+
3+
import java.util.TreeMap;
4+
import java.util.TreeSet;
5+
import org.jlab.detector.calib.utils.ConstantsManager;
6+
import org.jlab.detector.decode.CLASDecoder;
7+
import org.jlab.detector.helicity.HelicitySequence;
8+
import org.jlab.detector.helicity.HelicityState;
9+
import org.jlab.detector.scalers.DaqScalersSequence;
10+
import org.jlab.jnp.hipo4.data.Bank;
11+
import org.jlab.jnp.hipo4.data.Event;
12+
import org.jlab.jnp.hipo4.data.SchemaFactory;
13+
import org.jlab.jnp.hipo4.io.HipoWriterSorted;
14+
15+
/**
16+
*
17+
* @author baltzell
18+
*/
19+
public class SerialHoncho {
20+
21+
static final String[] TAG1BANKS = {"RUN::scaler","HEL::scaler","RAW::scaler","RAW::epics","HEL::flip","COAT::config"};
22+
23+
SchemaFactory schema;
24+
Bank[] tag1banks;
25+
Bank runConfig;
26+
Bank helicityAdc;
27+
ConstantsManager conman;
28+
TreeMap<Integer,Integer> eventUnix;
29+
TreeSet<HelicityState> helicities;
30+
DaqScalersSequence scalers;
31+
32+
public SerialHoncho(SchemaFactory schema) {
33+
this.schema = schema;
34+
conman = new ConstantsManager();
35+
conman.init("/runcontrol/hwp","/runcontrol/helicity");
36+
runConfig = new Bank(schema.getSchema("RUN::config"));
37+
helicityAdc = new Bank(schema.getSchema("HEL::adc"));
38+
helicities = new TreeSet<>();
39+
scalers = new DaqScalersSequence(schema);
40+
eventUnix = new TreeMap<>();
41+
tag1banks = new Bank[TAG1BANKS.length];
42+
for (int i=0; i<tag1banks.length; ++i)
43+
tag1banks[i] = new Bank(schema.getSchema(TAG1BANKS[i]));
44+
}
45+
46+
public synchronized Event read(Event event) {
47+
scalers.add(event);
48+
event.read(runConfig);
49+
event.read(helicityAdc);
50+
if (runConfig.getRows() > 0) {
51+
int unix = runConfig.getInt("unixtime",0);
52+
int evno = runConfig.getInt("event",0);
53+
if (unix > 0 && evno > 0) eventUnix.put(evno, unix);
54+
}
55+
helicities.add(HelicityState.createFromFadcBank(helicityAdc, runConfig, conman));
56+
return CLASDecoder.createTaggedEvent(event, runConfig, tag1banks);
57+
}
58+
59+
public void finish(HipoWriterSorted writer) {
60+
writer.addEvent(getUnixEvent(runConfig),1);
61+
HelicitySequence.writeFlips(schema, writer, helicities);
62+
}
63+
64+
public void clear() {
65+
while (helicities.size() > 100) helicities.pollFirst();
66+
scalers.clear(100);
67+
}
68+
69+
Event getUnixEvent(Bank config) {
70+
Bank unix = new Bank(schema.getSchema("RUN::unix"));
71+
unix.setRows(eventUnix.size());
72+
int row = 0;
73+
for (int evno : eventUnix.keySet()) {
74+
unix.putInt("event", row, evno);
75+
unix.putInt("unixtime",row, eventUnix.get(evno));
76+
row++;
77+
}
78+
Event e = new Event();
79+
e.write(config);
80+
e.write(unix);
81+
return e;
82+
}
83+
84+
public DaqScalersSequence getScalers() {
85+
return scalers;
86+
}
87+
88+
public TreeSet<HelicityState> getHelicities() {
89+
return helicities;
90+
}
91+
92+
public ConstantsManager getConstantsManager() {
93+
return conman;
94+
}
95+
96+
public SchemaFactory getSchemaFactory() {
97+
return schema;
98+
}
99+
}

0 commit comments

Comments
 (0)