Skip to content

Commit bf9ad64

Browse files
committed
make it work
1 parent f8bea77 commit bf9ad64

2 files changed

Lines changed: 80 additions & 54 deletions

File tree

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

Lines changed: 79 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,119 @@
22

33
import java.util.Map;
44
import org.jlab.io.base.DataBank;
5+
import org.jlab.io.base.DataEvent;
56
import org.jlab.utils.groups.IndexedTable;
7+
import org.jlab.utils.groups.IndexedTable.IndexedEntry;
68

7-
/**
8-
* Utility class for storing occupancy/multiplicity in an IndexedTable and
9-
* writing it to an occupancy bank with format (index... weight [*])
10-
*
11-
* @author baltzell
12-
*/
13-
public class OccupancyTable {
9+
public abstract class OccupancyTable {
1410

15-
private String[] indexNames;
16-
private IndexedTable table;
11+
public static void main(String[] args) {
12+
DC d = new DC();
13+
d.add(1,1,1);
14+
d.add(1,2,1);
15+
d.add(1,3,1);
16+
d.add(1,3,1);
17+
IndexedTable o = d.getOccupancy(10);
18+
o.show();
19+
o.getList().show();
20+
}
21+
22+
public static class DC extends OccupancyTable {
23+
public DC() { super("DC::occ","sector","layer"); }
24+
@Override
25+
public void add(DataEvent e) {
26+
DataBank b = e.getBank("DC::tot");
27+
if (b != null) {
28+
final int rows = b.rows();
29+
for (int i=0; i<rows; i++) {
30+
add(b.getByte(0, i), b.getByte(1,i));
31+
}
32+
}
33+
}
34+
}
35+
36+
protected String bankName;
37+
protected String[] valueNames = {"occ/I"};
38+
protected String[] indexNames;
39+
protected IndexedTable table;
1740

18-
public OccupancyTable(String... index) {
19-
if (index.length != 3 && index.length != 4)
20-
throw new IllegalArgumentException("Invalid index length: "+index.length);
41+
public OccupancyTable(String bank, String... index) {
42+
bankName = bank;
2143
indexNames = index;
22-
table = new IndexedTable(indexNames.length, new String[]{"occ/D"});
44+
table = new IndexedTable(indexNames.length, valueNames);
2345
}
2446

25-
/**
26-
* Reset the occupancy table.
27-
*/
28-
public void reset() {
29-
table = new IndexedTable(indexNames.length, new String[]{"occ/D"});
47+
public OccupancyTable(String bank) {
48+
bankName = bank;
49+
indexNames = new String[]{"sector","layer","component"};
50+
table = new IndexedTable(indexNames.length, valueNames);
3051
}
3152

3253
/**
33-
* Fill occupancy, user-defined weight.
34-
* @param weight
35-
* @param index
54+
* Get the occupancy table.
55+
* @param events number of events that have been added
56+
* @return
3657
*/
37-
public void add(double weight, int... index) {
38-
final long hash = IndexedTable.DEFAULT_GENERATOR.hashCode(index);
39-
if (!table.hasEntry(index)) {
40-
table.addEntry(index);
41-
table.setDoubleValueByHash(0.0, indexNames.length, hash);
58+
public final IndexedTable getOccupancy(int events) {
59+
IndexedTable t = new IndexedTable(indexNames.length, new String[]{"occ/F"});
60+
for (long hash : ((Map<Long,IndexedEntry>)table.getList().getMap()).keySet()) {
61+
t.addEntry(IndexedTable.DEFAULT_GENERATOR.getIndex(hash, 0),
62+
IndexedTable.DEFAULT_GENERATOR.getIndex(hash, 1),
63+
IndexedTable.DEFAULT_GENERATOR.getIndex(hash, 2));
64+
t.setDoubleValueByHash(((double)table.getIntValueByHash(0, hash))/events, 0, hash);
4265
}
43-
table.setDoubleValueByHash(weight + table.getDoubleValueByHash(indexNames.length, hash), indexNames.length, hash);
66+
return t;
4467
}
4568

4669
/**
47-
* Fill occupancy, unity weight.
48-
* @param index
70+
* Reset occupancy table.
4971
*/
50-
public void add(int... index) {
51-
add(1, index);
72+
public final void reset() {
73+
table = new IndexedTable(indexNames.length, valueNames);
5274
}
5375

5476
/**
55-
* Fill occupancy from hits bank, one per bank row.
56-
* Note, bank index data types are hard-coded here!
57-
* @param b hit bank
77+
* Fill occupancy table.
78+
* @param index
5879
*/
59-
public void add(DataBank b) {
60-
final int rows = b.rows();
61-
for (int i=0; i<rows; i++) {
62-
if (indexNames.length == 3)
63-
add(b.getByte(0, i), b.getByte(1,i), b.getShort(2,i));
64-
else
65-
add(b.getByte(0, i), b.getByte(1,i), b.getShort(2,i), b.getByte(3,i));
80+
public final void add(int... index) {
81+
final long hash = IndexedTable.DEFAULT_GENERATOR.hashCode(index);
82+
if (!table.hasEntryByHash(hash)) {
83+
table.addEntry(index);
84+
table.setIntValueByHash(0, 0, hash);
6685
}
86+
table.setIntValueByHash(table.getIntValueByHash(0, hash) + 1, 0, hash);
6787
}
6888

6989
/**
70-
* Get the number of rows for the occupancy bank.
90+
* Get number of rows for the occupancy bank.
7191
* @return rows
7292
*/
73-
public int getRows() {
93+
public final int getRows() {
7494
return table.getRowCount();
7595
}
76-
96+
7797
/**
78-
* Update an occupancy bank.
98+
* Update an occupancy bank, assuming 3/4-index s/l/c[/o].
7999
* @param b occupancy bank
80100
*/
81-
public void update(DataBank b) {
101+
public final void update(DataBank b) {
82102
int i = 0;
83-
Map<Long,Integer> m = table.getList().getMap();
84-
for (Map.Entry<Long,Integer> entry : m.entrySet()) {
85-
b.setInt(0, i, IndexedTable.DEFAULT_GENERATOR.getIndex(entry.getKey(), 0));
86-
b.setInt(1, i, IndexedTable.DEFAULT_GENERATOR.getIndex(entry.getKey(), 1));
87-
b.setInt(2, i, IndexedTable.DEFAULT_GENERATOR.getIndex(entry.getKey(), 2));
103+
Map<Long,IndexedEntry> m = table.getList().getMap();
104+
for (long hash : m.keySet()) {
105+
b.setByte(0, i, (byte)IndexedTable.DEFAULT_GENERATOR.getIndex(hash, 0));
106+
b.setByte(1, i, (byte)IndexedTable.DEFAULT_GENERATOR.getIndex(hash, 1));
107+
b.setShort(2, i, (short)IndexedTable.DEFAULT_GENERATOR.getIndex(hash, 2));
88108
if (indexNames.length == 4)
89-
b.setInt(3, i, IndexedTable.DEFAULT_GENERATOR.getIndex(entry.getKey(), 3));
90-
b.setInt(indexNames.length, i, entry.getValue());
109+
b.setByte(3, i, (byte)IndexedTable.DEFAULT_GENERATOR.getIndex(hash, 3));
110+
b.setInt(indexNames.length, i, m.get(hash).getValue(0).intValue());
91111
i++;
92112
}
93113
}
114+
115+
/**
116+
* Fill occupancy from hits bank.
117+
* @param e
118+
*/
119+
protected abstract void add(DataEvent e);
94120
}

etc/bankdefs/hipo4/occupancy.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{ "name":"sector" , "type":"B", "info":"sector"},
99
{ "name":"layer" , "type":"B", "info":"layer"},
1010
{ "name":"component" , "type":"S", "info":"component"},
11-
{ "name":"hits" , "type":"S", "info":"number of hits"}
11+
{ "name":"hits" , "type":"I", "info":"number of hits"}
1212
]
1313
}
1414
]

0 commit comments

Comments
 (0)