Skip to content

Commit dcb8caf

Browse files
committed
[Draft] Adds: hipo diff writer from banks in 2 hipo files
1 parent 735ef42 commit dcb8caf

2 files changed

Lines changed: 177 additions & 0 deletions

File tree

‎bin/hipo-diffBanks‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
. `dirname $0`/../libexec/env.sh
4+
5+
export MALLOC_ARENA_MAX=1
6+
7+
java ${JAVA_OPTS-} -Xmx1536m -Xms1024m -XX:+UseSerialGC \
8+
-cp ${COATJAVA_CLASSPATH:-''} \
9+
org.jlab.utils.HipoDiffBanks \
10+
$*
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package org.jlab.utils;
2+
3+
import org.jlab.jnp.hipo4.io.HipoReader;
4+
import org.jlab.jnp.hipo4.io.HipoWriter;
5+
import org.jlab.jnp.hipo4.data.Event;
6+
import org.jlab.jnp.hipo4.data.Bank;
7+
import org.jlab.jnp.hipo4.data.Schema;
8+
import org.jlab.jnp.hipo4.data.SchemaFactory;
9+
10+
public class HipoDiffBanks {
11+
12+
public static void main(String[] args) throws Exception {
13+
if (args.length < 3) {
14+
System.err.println("usage: HipoDiffBanks run1.hipo run2.hipo out.hipo");
15+
System.exit(1);
16+
}
17+
18+
String f1 = args[0];
19+
String f2 = args[1];
20+
String fout = args[2];
21+
22+
// open the input files
23+
HipoReader r1 = new HipoReader();
24+
HipoReader r2 = new HipoReader();
25+
r1.open(f1);
26+
r2.open(f2);
27+
28+
// grab schemas (dictionary) from run1
29+
SchemaFactory dict = r1.getSchemaFactory();
30+
31+
// prepare output writer with same dictionary
32+
HipoWriter w = new HipoWriter();
33+
w.getSchemaFactory().copy(dict);
34+
w.open(fout);
35+
36+
Event e1 = new Event();
37+
Event e2 = new Event();
38+
Event eOut = new Event();
39+
40+
long n1 = r1.getEventCount();
41+
long n2 = r2.getEventCount();
42+
long nEvents = Math.min(n1, n2);
43+
44+
// loop over events
45+
for (int ievt = 0; ievt < nEvents; ievt++) {
46+
47+
r1.getEvent(e1, ievt);
48+
r2.getEvent(e2, ievt);
49+
50+
eOut.reset();
51+
52+
// loop over every schema (bank definition) in file1
53+
for (Schema schema : dict.getSchemaList()) {
54+
55+
// only diff this bank if BOTH events actually have it
56+
if (!e1.hasBank(schema) || !e2.hasBank(schema)) {
57+
continue;
58+
}
59+
60+
// build banks for this schema
61+
Bank b1 = new Bank(schema);
62+
Bank b2 = new Bank(schema);
63+
64+
// read bank content from each event
65+
e1.read(b1);
66+
e2.read(b2);
67+
68+
int rows = Math.min(b1.getRows(), b2.getRows());
69+
70+
// create output (diff) bank with same schema
71+
Bank bDiff = new Bank(schema);
72+
bDiff.setRows(rows); // coatjava 13.0.2 Bank has setRows()
73+
74+
// loop rows/cols and subtract numeric columns
75+
int ncols = b1.getSchema().getElements();
76+
for (int row = 0; row < rows; row++) {
77+
78+
for (int col = 0; col < ncols; col++) {
79+
int ftype = b1.getSchema().getType(col);
80+
String fname = b1.getSchema().getElementName(col);
81+
82+
switch (ftype) {
83+
// NOTE: mapping here matches coatjava's internal codes
84+
// 1 = byte (int8)
85+
// 2 = short (int16)
86+
// 3 = int (int32)
87+
// 4 = float (float32)
88+
// 5 = double(float64)
89+
// 6 = long (int64)
90+
case 1: // byte
91+
bDiff.putByte(fname, row,
92+
(byte)(b2.getByte(fname,row) - b1.getByte(fname,row)));
93+
break;
94+
case 2: // short
95+
bDiff.putShort(fname, row,
96+
(short)(b2.getShort(fname,row) - b1.getShort(fname,row)));
97+
break;
98+
case 3: // int
99+
bDiff.putInt(fname, row,
100+
b2.getInt(fname,row) - b1.getInt(fname,row));
101+
break;
102+
case 4: // float
103+
bDiff.putFloat(fname, row,
104+
b2.getFloat(fname,row) - b1.getFloat(fname,row));
105+
break;
106+
case 5: // double
107+
bDiff.putDouble(fname, row,
108+
b2.getDouble(fname,row) - b1.getDouble(fname,row));
109+
break;
110+
case 6: // long
111+
bDiff.putLong(fname, row,
112+
b2.getLong(fname,row) - b1.getLong(fname,row));
113+
break;
114+
default:
115+
// unhandled type (arrays, etc.) -> just copy run2's value
116+
copyFieldNoSub(ftype, fname, row, b2, bDiff);
117+
break;
118+
}
119+
} // end col loop
120+
} // end row loop
121+
122+
// attach this diff bank into the output event
123+
eOut.write(bDiff);
124+
} // end schema loop
125+
126+
// write the diff event
127+
w.addEvent(eOut);
128+
} // end event loop
129+
130+
w.close();
131+
r1.close();
132+
r2.close();
133+
}
134+
135+
// fallback copy logic for non-subtracted types
136+
private static void copyFieldNoSub(int ftype, String fname, int row,
137+
Bank src, Bank dst) {
138+
139+
switch (ftype) {
140+
case 1: // byte
141+
dst.putByte(fname, row, src.getByte(fname,row));
142+
break;
143+
case 2: // short
144+
dst.putShort(fname, row, src.getShort(fname,row));
145+
break;
146+
case 3: // int
147+
dst.putInt(fname, row, src.getInt(fname,row));
148+
break;
149+
case 4: // float
150+
dst.putFloat(fname, row, src.getFloat(fname,row));
151+
break;
152+
case 5: // double
153+
dst.putDouble(fname, row, src.getDouble(fname,row));
154+
break;
155+
case 6: // long
156+
dst.putLong(fname, row, src.getLong(fname,row));
157+
break;
158+
default:
159+
// arrays / strings / etc.
160+
// We don't currently have a generic copier for those in this script.
161+
// It's ok to skip: they just won't appear in the diff bank.
162+
break;
163+
}
164+
}
165+
}
166+
167+

0 commit comments

Comments
 (0)