Skip to content

Commit 048fb0c

Browse files
committed
add occupancer class
1 parent 988d890 commit 048fb0c

1 file changed

Lines changed: 192 additions & 0 deletions

File tree

  • common-tools/clas-detector/src/main/java/org/jlab/detector/serial
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package org.jlab.detector.serial;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
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;
11+
import org.jlab.detector.banks.RawBank.OrderGroups;
12+
import org.jlab.jnp.hipo4.data.Bank;
13+
import org.jlab.jnp.hipo4.data.Event;
14+
import org.jlab.jnp.hipo4.data.Schema;
15+
import org.jlab.jnp.hipo4.data.SchemaFactory;
16+
import org.jlab.utils.system.ClasUtilsFile;
17+
18+
/**
19+
* Occupancy bookkeeper based on IndexedTable, with I/O helpers for indexed banks.
20+
*
21+
* @author baltzell
22+
*/
23+
public class Occupancer extends ArrayList<Occupancer.OccupanceTable> {
24+
25+
static final String OCC_BANK_DIR = ClasUtilsFile.getResourceDir("CLAS12DIR","etc/bankdefs/hipo4/singles/occupancy");
26+
static final SchemaFactory FULL_SCHEMA = new SchemaFactory();
27+
static { FULL_SCHEMA.initFromDirectory(ClasUtilsFile.getResourceDir("CLAS12DIR","etc/bankdefs/hipo4")); }
28+
29+
long triggerMask;
30+
int prescale;
31+
int nevents = 0;
32+
Schema cfgSchema;
33+
34+
/**
35+
*
36+
* @param triggerMask
37+
* @param prescale
38+
*/
39+
public Occupancer(long triggerMask, int prescale) {
40+
super();
41+
this.triggerMask = triggerMask;
42+
this.prescale = prescale;
43+
init();
44+
}
45+
46+
private void init() {
47+
cfgSchema = FULL_SCHEMA.getSchema("RUN::config");
48+
try {
49+
// Initialize list, by reading occupancy bank filenames from a directory
50+
// and stripping ".json" suffix and "OCC::" prefix to get hit bank name:
51+
addAll(Files.list(Paths.get(OCC_BANK_DIR))
52+
.filter(Files::isRegularFile)
53+
.map(p -> p.getFileName().toString())
54+
.map(s -> s.substring(0, s.length()-5))
55+
.map(s -> s.substring(5, s.length()))
56+
.map(OccupanceTable::new).toList());
57+
} catch (IOException ex) {
58+
System.getLogger(Occupancer.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex);
59+
}
60+
}
61+
62+
public boolean process(Event event) {
63+
Bank cfgBank = event.read(new Bank(cfgSchema, 1));
64+
boolean passTrigger = cfgBank.getRows() > 0 && 0 != (triggerMask & cfgBank.getLong("trigger",0));
65+
if (triggerMask <= 0 || passTrigger) {
66+
int size = size();
67+
for (int i=0; i<size; i++) get(i).fill(event);
68+
if (++nevents % prescale == 0) {
69+
for (int i=0; i<size; i++) {
70+
if (get(i).getTable().getRowCount() > 0) {
71+
event.write(get(i).create(nevents));
72+
}
73+
get(i).reset();
74+
}
75+
nevents = 0;
76+
}
77+
}
78+
return true;
79+
}
80+
81+
public void reset() {
82+
int size = size();
83+
for (int i=0; i<size; i++) {
84+
get(i).reset();
85+
nevents = 0;
86+
}
87+
}
88+
89+
public static class OccupanceTable {
90+
91+
Schema occSchema;
92+
Schema hitSchema;
93+
IndexedTable table;
94+
95+
/**
96+
* A 3- or 4-index table, depending on "order".
97+
* @param hitBank name of the hit bank
98+
*/
99+
public OccupanceTable(String hitBank) {
100+
hitSchema = FULL_SCHEMA.getSchema(hitBank);
101+
occSchema = FULL_SCHEMA.getSchema("OCC::" + hitBank);
102+
table = new IndexedTable(occSchema.hasEntry("order")?4:3, new String[]{"occ/F"});
103+
}
104+
105+
/**
106+
* An N-index table.
107+
* @param hitBank name of the hit bank
108+
* @param indexCount number of inidices in the hit bank
109+
*/
110+
public OccupanceTable(String hitBank, int indexCount) {
111+
hitSchema = FULL_SCHEMA.getSchema(hitBank);
112+
occSchema = FULL_SCHEMA.getSchema("OCC::" + hitBank);
113+
table = new IndexedTable(indexCount, new String[]{"occ/F"});
114+
}
115+
116+
public final IndexedTable getTable() { return table; }
117+
118+
/**
119+
* Zero the occupancy table.
120+
*/
121+
public final void reset() {
122+
table = new IndexedTable(table.getList().getIndexSize(), new String[]{"occ/F"});
123+
}
124+
125+
/**
126+
* Get the occupancy table, normalized by number of events.
127+
* @param events
128+
* @return
129+
*/
130+
public final IndexedTable getOccupancy(long events) {
131+
IndexedTable t = new IndexedTable(table.getList().getIndexSize(), new String[]{"occ/F"});
132+
for (long hash : ((Map<Long,IndexedEntry>)table.getList().getMap()).keySet()) {
133+
t.addEntry(IndexedTable.DEFAULT_GENERATOR.getIndices(hash, table.getList().getIndexSize()));
134+
t.setDoubleValueByHash((table.getDoubleValueByHash(0, hash))/events, 0, hash);
135+
}
136+
return t;
137+
}
138+
139+
/**
140+
* Fill the occupancy table.
141+
* @param index
142+
*/
143+
public final void fill(int... index) {
144+
for (int i=0; i<index.length; i++) if (index[i] < 0) return;
145+
final long hash = IndexedTable.DEFAULT_GENERATOR.hashCode(index);
146+
if (!table.hasEntryByHash(hash)) {
147+
table.addEntry(index);
148+
table.setDoubleValueByHash(0.0d, 0, hash);
149+
}
150+
table.setDoubleValueByHash(table.getDoubleValueByHash(0, hash) + 1.0, 0, hash);
151+
}
152+
153+
/**
154+
* Fill occupancy table from a user-defined bank.
155+
* @param event
156+
*/
157+
public void fill(Event event) {
158+
RawBank bank = new RawBank(hitSchema, 1000, OrderGroups.DEFAULT);
159+
bank.read(event);
160+
final int rows = bank.rows();
161+
int[] idx = new int[table.getList().getIndexSize()];
162+
for (int i=0; i<rows; i++) {
163+
for (int j=0; j<table.getList().getIndexSize(); j++) {
164+
if (j==2) idx[j] = bank.getShort(j,i);
165+
else idx[j] = bank.getByte(j,i);
166+
}
167+
fill(idx);
168+
}
169+
}
170+
171+
/**
172+
* Get an occupancy bank, normalized by number of events.
173+
* @param events
174+
* @return
175+
*/
176+
public Bank create(long events) {
177+
Bank b = new Bank(occSchema, table.getRowCount());
178+
Map<Long,IndexedEntry> m = table.getList().getMap();
179+
int i = 0;
180+
for (long hash : m.keySet()) {
181+
int[] idx = IndexedTable.DEFAULT_GENERATOR.getIndices(hash, table.getList().getIndexSize());
182+
for (int j=0; j<table.getList().getIndexSize(); j++) {
183+
if (j == 2) b.putShort(j, i, (short)idx[j]);
184+
else b.putByte(j, i, (byte)idx[j]);
185+
}
186+
b.putFloat(table.getList().getIndexSize(), i, ((float)m.get(hash).getValue(0).intValue())/events);
187+
i++;
188+
}
189+
return b;
190+
}
191+
}
192+
}

0 commit comments

Comments
 (0)