Skip to content

Commit 6b56a44

Browse files
committed
fix
1 parent e312ef7 commit 6b56a44

5 files changed

Lines changed: 140 additions & 157 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import org.jlab.clara.std.services.EventWriterException;
77
import org.jlab.detector.calib.utils.ConstantsManager;
88
import org.jlab.detector.helicity.HelicitySequenceDelayed;
9-
import org.jlab.detector.serial.Occupancer;
109
import org.jlab.detector.serial.SerialHoncho;
1110
import org.jlab.detector.serial.PostProcessor;
11+
import org.jlab.detector.serial.Occupancer;
1212
import org.jlab.jnp.hipo4.data.Bank;
1313
import org.jlab.jnp.hipo4.data.Event;
1414
import org.jlab.jnp.hipo4.data.SchemaFactory;
@@ -66,7 +66,7 @@ protected HipoWriterSorted createWriter(Path file, JSONObject opts) throws Event
6666
protected void writeEvent(Object event) throws EventWriterException {
6767
Event t = serial.read((Event)event);
6868
if (!t.isEmpty()) writer.addEvent(t, 1);
69-
occupancer.process(t);
69+
occupancer.process(((Event)event));
7070
super.writeEvent(event);
7171
}
7272

‎common-tools/clas-detector/src/main/java/org/jlab/detector/banks/FilteredBank.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public final void setFilter(int... values){
6060
* @param evt
6161
*/
6262
public void read(Event evt){
63-
evt.read(bank);
63+
evt.read(bank);
6464
this.notifyRead();
6565
}
6666

‎common-tools/clas-detector/src/main/java/org/jlab/detector/calib/utils/OccupanceTable.java‎

Lines changed: 0 additions & 133 deletions
This file was deleted.

‎common-tools/clas-detector/src/main/java/org/jlab/detector/serial/Occupancer.java‎

Lines changed: 133 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,36 @@
44
import java.nio.file.Files;
55
import java.nio.file.Paths;
66
import java.util.ArrayList;
7+
import java.util.Map;
8+
import org.jlab.detector.banks.RawBank;
9+
import org.jlab.utils.groups.IndexedTable;
10+
import org.jlab.utils.groups.IndexedTable.IndexedEntry;
711
import org.jlab.detector.banks.RawBank.OrderGroups;
812
import org.jlab.detector.banks.RawDataBank;
9-
import org.jlab.detector.calib.utils.OccupanceTable;
13+
import org.jlab.jnp.hipo4.data.Bank;
1014
import org.jlab.jnp.hipo4.data.Event;
15+
import org.jlab.jnp.hipo4.data.Schema;
16+
import org.jlab.jnp.hipo4.data.SchemaFactory;
1117
import org.jlab.utils.system.ClasUtilsFile;
1218

13-
public class Occupancer extends ArrayList<OccupanceTable> {
19+
/**
20+
* Occupancy bookkeeper based on IndexedTable, with I/O helpers for indexed banks.
21+
*
22+
* @author baltzell
23+
*/
24+
public class Occupancer extends ArrayList<Occupancer.OccupanceTable> {
1425

1526
static final String BANKDIR = ClasUtilsFile.getResourceDir("CLAS12DIR","etc/bankdefs/hipo4/singles/occupancy");
16-
17-
int events;
27+
static final SchemaFactory schema = new SchemaFactory();
28+
static { schema.initFromDirectory(ClasUtilsFile.getResourceDir("CLAS12DIR","etc/bankdefs/hipo4")); }
29+
30+
int nevents;
1831
int prescale;
1932

2033
public Occupancer(int prescale) {
2134
super();
2235
this.prescale = prescale;
23-
this.events = 0;
36+
this.nevents = 0;
2437
try {
2538
addAll(Files.list(Paths.get(BANKDIR))
2639
.filter(Files::isRegularFile)
@@ -32,30 +45,130 @@ public Occupancer(int prescale) {
3245
System.getLogger(Occupancer.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex);
3346
}
3447
}
35-
48+
3649
public boolean process(Event event) {
37-
forEach(t -> {
38-
RawDataBank b = new RawDataBank(t.getHitBank(), 1000, OrderGroups.NODENOISE);
39-
b.read(event);
40-
t.fill(b, false);
41-
});
42-
if (++events % prescale == 0) {
50+
forEach(t -> { t.fill(event, false); });
51+
if (++nevents % prescale == 0) {
4352
forEach(t -> {
4453
if (t.getTable().getRowCount() > 0) {
45-
event.write(t.create(events, event));
46-
}
54+
event.write(t.create(nevents, event));
55+
}
4756
t.reset();
4857
});
49-
events = 0;
58+
nevents = 0;
5059
}
5160
return true;
5261
}
53-
62+
5463
public void reset() {
55-
forEach(t -> {
56-
t.reset();
57-
events = 0;
58-
});
64+
forEach(t -> { t.reset(); nevents = 0; });
5965
}
66+
67+
public static final class OccupanceTable {
6068

69+
Schema occSchema;
70+
Schema hitSchema;
71+
IndexedTable table;
72+
73+
/**
74+
* A 3-index table, e.g., sector/layer/component.
75+
* @param hitBank name of the hit bank
76+
*/
77+
public OccupanceTable(String hitBank) {
78+
hitSchema = schema.getSchema(hitBank);
79+
occSchema = schema.getSchema("OCC::" + hitBank);
80+
table = new IndexedTable(3, new String[]{"occ/F"});
81+
}
82+
83+
/**
84+
* An N-index table.
85+
* @param hitBank name of the hit bank
86+
* @param indexCount number of inidices in the hit bank
87+
*/
88+
public OccupanceTable(String hitBank, int indexCount) {
89+
hitSchema = schema.getSchema(hitBank);
90+
occSchema = schema.getSchema("OCC::" + hitBank);
91+
table = new IndexedTable(indexCount, new String[]{"occ/F"});
92+
}
93+
94+
public final IndexedTable getTable() { return table; }
95+
96+
/**
97+
* Zero the occupancy table.
98+
*/
99+
public final void reset() {
100+
table = new IndexedTable(table.getList().getIndexSize(), new String[]{"occ/F"});
101+
}
102+
103+
/**
104+
* Get the occupancy table, normalized by number of events.
105+
* @param events
106+
* @return
107+
*/
108+
public final IndexedTable getOccupancy(long events) {
109+
IndexedTable t = new IndexedTable(table.getList().getIndexSize(), new String[]{"occ/F"});
110+
for (long hash : ((Map<Long,IndexedEntry>)table.getList().getMap()).keySet()) {
111+
t.addEntry(IndexedTable.DEFAULT_GENERATOR.getIndices(hash, table.getList().getIndexSize()));
112+
t.setDoubleValueByHash((table.getDoubleValueByHash(0, hash))/events, 0, hash);
113+
}
114+
return t;
115+
}
116+
117+
/**
118+
* Fill the occupancy table.
119+
* @param weight
120+
* @param index
121+
*/
122+
public synchronized final void fill(float weight, int... index) {
123+
for (int i=0; i<index.length; i++) if (index[i] < 0) return;
124+
final long hash = IndexedTable.DEFAULT_GENERATOR.hashCode(index);
125+
if (!table.hasEntryByHash(hash)) {
126+
table.addEntry(index);
127+
table.setDoubleValueByHash(0.0d, 0, hash);
128+
}
129+
table.setDoubleValueByHash(table.getDoubleValueByHash(0, hash) + weight, 0, hash);
130+
}
131+
132+
/**
133+
* Fill occupancy table from a user-defined bank.
134+
* @param event
135+
* @param weighted
136+
*/
137+
public void fill(Event event, boolean weighted) {
138+
RawBank bank = new RawBank(hitSchema, 1000, OrderGroups.DEFAULT);
139+
bank.read(event);
140+
final int rows = bank.rows();
141+
int[] idx = new int[table.getList().getIndexSize()];
142+
for (int i=0; i<rows; i++) {
143+
for (int j=0; j<table.getList().getIndexSize(); j++) {
144+
if (j==2) idx[j] = bank.getShort(j,i);
145+
else idx[j] = bank.getByte(j,i);
146+
}
147+
if (weighted) fill(bank.getFloat(table.getList().getIndexSize(),i),idx);
148+
else fill(1.0f, idx);
149+
}
150+
}
151+
152+
/**
153+
* Get an occupancy bank, normalized by number of events.
154+
* @param events
155+
* @param event
156+
* @return
157+
*/
158+
public synchronized Bank create(long events, Event event) {
159+
Bank b = new Bank(occSchema, table.getRowCount());
160+
Map<Long,IndexedEntry> m = table.getList().getMap();
161+
int i = 0;
162+
for (long hash : m.keySet()) {
163+
int[] idx = IndexedTable.DEFAULT_GENERATOR.getIndices(hash, table.getList().getIndexSize());
164+
for (int j=0; j<table.getList().getIndexSize(); j++) {
165+
if (j == 2) b.putShort(j, i, (short)idx[j]);
166+
else b.putByte(j, i, (byte)idx[j]);
167+
}
168+
b.putFloat(table.getList().getIndexSize(), i, ((float)m.get(hash).getValue(0).intValue())/events);
169+
i++;
170+
}
171+
return b;
172+
}
173+
}
61174
}

‎common-tools/clas-reco/src/main/java/org/jlab/clas/reco/EngineProcessor.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Arrays;
2020
import org.jlab.coda.jevio.EvioException;
2121
import org.jlab.detector.decode.CLASDecoder4;
22+
import org.jlab.detector.serial.Occupancer;
2223
import org.jlab.io.evio.EvioDataEvent;
2324
import org.jlab.io.evio.EvioSource;
2425
import org.jlab.io.hipo.HipoDataEvent;
@@ -42,7 +43,8 @@ public class EngineProcessor {
4243
private SchemaFactory banksToKeep = null;
4344
private final List<String> schemaExempt = Arrays.asList("RUN::config","DC::tdc");
4445

45-
private CLASDecoder4 decoder = new CLASDecoder4();
46+
private final CLASDecoder4 decoder = new CLASDecoder4();
47+
private final Occupancer occupancer = new Occupancer(100);
4648

4749
public EngineProcessor(){}
4850

@@ -317,6 +319,7 @@ public void processFile(String file, String output){
317319
public void processEvent(DataEvent event, HipoDataSync writer) {
318320
processEvent(event);
319321
removeBanks(event);
322+
occupancer.process(((HipoDataEvent)event).getHipoEvent());
320323
writer.writeEvent(event);
321324
}
322325

0 commit comments

Comments
 (0)