44import java .nio .file .Files ;
55import java .nio .file .Paths ;
66import 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 ;
711import org .jlab .detector .banks .RawBank .OrderGroups ;
812import org .jlab .detector .banks .RawDataBank ;
9- import org .jlab .detector . calib . utils . OccupanceTable ;
13+ import org .jlab .jnp . hipo4 . data . Bank ;
1014import org .jlab .jnp .hipo4 .data .Event ;
15+ import org .jlab .jnp .hipo4 .data .Schema ;
16+ import org .jlab .jnp .hipo4 .data .SchemaFactory ;
1117import 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}
0 commit comments