22
33import java .util .Map ;
44import org .jlab .io .base .DataBank ;
5- import org .jlab .io .base .DataEvent ;
65import org .jlab .utils .groups .IndexedTable ;
76
87/**
9- *
8+ * Utility class for storing occupancy/multiplicity in an IndexedTable and
9+ * writing it to an occupancy bank with format (index... weight [*])
10+ *
1011 * @author baltzell
1112 */
1213public class OccupancyTable {
1314
1415 private String [] indexNames ;
1516 private IndexedTable table ;
16-
17+
1718 public OccupancyTable (String ... index ) {
1819 if (index .length != 3 && index .length != 4 )
1920 throw new IllegalArgumentException ("Invalid index length: " +index .length );
2021 indexNames = index ;
21- table = new IndexedTable (indexNames .length , new String []{"nhits /D" });
22+ table = new IndexedTable (indexNames .length , new String []{"occ /D" });
2223 }
2324
25+ /**
26+ * Reset the occupancy table.
27+ */
2428 public void reset () {
25- table = new IndexedTable (indexNames .length , new String []{"nhits /D" });
29+ table = new IndexedTable (indexNames .length , new String []{"occ /D" });
2630 }
2731
32+ /**
33+ * Fill occupancy, user-defined weight.
34+ * @param weight
35+ * @param index
36+ */
2837 public void add (double weight , int ... index ) {
38+ final long hash = IndexedTable .DEFAULT_GENERATOR .hashCode (index );
2939 if (!table .hasEntry (index )) {
3040 table .addEntry (index );
31- table .setDoubleValue (0.0 , "nhits" , index );
41+ table .setDoubleValueByHash (0.0 , indexNames . length , hash );
3242 }
33- table .setDoubleValue ( table .getDoubleValue ( "nhits" , index ) + weight , "nhits" , index );
43+ table .setDoubleValueByHash ( weight + table .getDoubleValueByHash ( indexNames . length , hash ), indexNames . length , hash );
3444 }
3545
46+ /**
47+ * Fill occupancy, unity weight.
48+ * @param index
49+ */
3650 public void add (int ... index ) {
3751 add (1 , index );
3852 }
3953
54+ /**
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
58+ */
4059 public void add (DataBank b ) {
4160 final int rows = b .rows ();
4261 for (int i =0 ; i <rows ; i ++) {
43- if (indexNames .length == 3 ) add (b .getByte (0 , i ), b .getByte (1 ,i ), b .getByte (2 ,i ));
44- else add (b .getByte (0 , i ), b .getByte (1 ,i ), b .getByte (2 ,i ), b .getByte (3 ,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 ));
4566 }
4667 }
4768
69+ /**
70+ * Get the number of rows for the occupancy bank.
71+ * @return rows
72+ */
73+ public int getRows () {
74+ return table .getRowCount ();
75+ }
76+
77+ /**
78+ * Update an occupancy bank.
79+ * @param b occupancy bank
80+ */
4881 public void update (DataBank b ) {
4982 int i = 0 ;
5083 Map <Long ,Integer > m = table .getList ().getMap ();
5184 for (Map .Entry <Long ,Integer > entry : m .entrySet ()) {
52- b .setInt ("sector" , i , IndexedTable .DEFAULT_GENERATOR .getIndex (entry .getKey (), 0 ));
53- b .setInt ("layer" , i , IndexedTable .DEFAULT_GENERATOR .getIndex (entry .getKey (), 1 ));
54- b .setInt ("component" , i , IndexedTable .DEFAULT_GENERATOR .getIndex (entry .getKey (), 2 ));
55- if (indexNames .length == 4 ) b .setInt ("order" , i , IndexedTable .DEFAULT_GENERATOR .getIndex (entry .getKey (), 3 ));
56- b .setInt ("nhits" , i , entry .getValue ());
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 ));
88+ if (indexNames .length == 4 )
89+ b .setInt (3 , i , IndexedTable .DEFAULT_GENERATOR .getIndex (entry .getKey (), 3 ));
90+ b .setInt (indexNames .length , i , entry .getValue ());
5791 i ++;
5892 }
5993 }
60-
61- public void update (DataEvent e , String bankName ) {
62- DataBank b = e .getBank (bankName );
63- if (b != null ) e .removeBank (bankName );
64- b = e .createBank (bankName , table .getRowCount ());
65- update (b );
66- e .appendBank (b );
67- }
68-
69- }
94+ }
0 commit comments