Skip to content

Commit d8ae869

Browse files
authored
Merge pull request #1 from JeffersonLab/hipo-diff
Incorporate HipoDiffBank functionality into HipoDiff
2 parents dcb8caf + 1ea04b4 commit d8ae869

1 file changed

Lines changed: 75 additions & 14 deletions

File tree

common-tools/clas-io/src/main/java/org/jlab/utils/HipoDiff.java

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
import org.jlab.jnp.hipo4.data.Event;
88
import org.jlab.jnp.hipo4.data.Schema;
99
import org.jlab.jnp.hipo4.data.SchemaFactory;
10+
import org.jlab.jnp.hipo4.io.HipoWriter;
1011
import org.jlab.utils.options.OptionParser;
1112

1213
public class HipoDiff {
1314

1415
/**
1516
* Bank sortable by any integer columns.
1617
*/
17-
static class SortedBank extends Bank {
18-
SortedBank(Schema s) { super(s); }
18+
static class DiffBank extends Bank {
19+
DiffBank(Schema s) { super(s); }
1920
/**
2021
* @param index the bank column indices to sort on
2122
* @return the sorted row indices
@@ -42,6 +43,43 @@ int[] getSorted(int... index) {
4243
}
4344
return rows;
4445
}
46+
/**
47+
* @param b the bank to compare with
48+
* @return the resulting diff bank
49+
*/
50+
public Bank getDiff(Bank b) {
51+
Bank diff = new Bank(getSchema());
52+
int rows = Math.min(getRows(), b.getRows());
53+
int ncols = getSchema().getElements();
54+
for (int row = 0; row < rows; row++) {
55+
for (int col = 0; col < ncols; col++) {
56+
switch (getSchema().getType(col)) {
57+
case 1: // byte
58+
diff.putByte(col, row, (byte)(b.getByte(col,row) - getByte(col,row)));
59+
break;
60+
case 2: // short
61+
diff.putShort(col, row, (short)(b.getShort(col,row) - getShort(col,row)));
62+
break;
63+
case 3: // int
64+
diff.putInt(col, row, b.getInt(col,row) - getInt(col,row));
65+
break;
66+
case 4: // float
67+
diff.putFloat(col, row, b.getFloat(col,row) - getFloat(col,row));
68+
break;
69+
case 5: // double
70+
diff.putDouble(col, row, b.getDouble(col,row) - getDouble(col,row));
71+
break;
72+
case 6: // long
73+
diff.putLong(col, row, b.getLong(col,row) - getLong(col,row));
74+
break;
75+
default:
76+
// unhandled type (arrays, etc.)
77+
break;
78+
}
79+
}
80+
}
81+
return diff;
82+
}
4583
}
4684

4785
static int nrow = 0;
@@ -57,9 +95,11 @@ int[] getSorted(int... index) {
5795
static boolean verboseMode;
5896
static boolean quietMode;
5997
static Bank runConfigBank;
98+
static HipoWriter writer;
99+
static Event event;
60100

61-
static ArrayList<SortedBank> banksA = new ArrayList<>();
62-
static ArrayList<SortedBank> banksB = new ArrayList<>();
101+
static ArrayList<DiffBank> banksA = new ArrayList<>();
102+
static ArrayList<DiffBank> banksB = new ArrayList<>();
63103
static HashMap<String, HashMap<String, Integer>> badEntries = new HashMap<>();
64104

65105
public static void main(String args[]) {
@@ -70,6 +110,7 @@ public static void main(String args[]) {
70110
op.addOption("-Q", null, "verbose mode");
71111
op.addOption("-b", null, "name of bank to diff");
72112
op.addOption("-s", null, "sort on column index");
113+
op.addOption("-o", null, "output HIPO diff file");
73114
op.setRequiresInputList(true);
74115
op.parse(args);
75116
if (op.getInputList().size() != 2) {
@@ -96,26 +137,35 @@ public static void main(String args[]) {
96137

97138
if (op.getOption("-b").stringValue() == null) {
98139
for (Schema s : sf.getSchemaList()) {
99-
banksA.add(new SortedBank(s));
100-
banksB.add(new SortedBank(s));
140+
banksA.add(new DiffBank(s));
141+
banksB.add(new DiffBank(s));
101142
}
102143
} else {
103-
banksA.add(new SortedBank(sf.getSchema(op.getOption("-b").stringValue())));
104-
banksB.add(new SortedBank(sf.getSchema(op.getOption("-b").stringValue())));
144+
banksA.add(new DiffBank(sf.getSchema(op.getOption("-b").stringValue())));
145+
banksB.add(new DiffBank(sf.getSchema(op.getOption("-b").stringValue())));
146+
}
147+
148+
if (op.getOption("-o").stringValue() != null) {
149+
writer = new HipoWriter();
150+
writer.getSchemaFactory().copy(readerA.getSchemaFactory());
151+
writer.open(op.getOption("-o").stringValue());
105152
}
106153

107154
compare(readerA, readerB);
155+
156+
if (writer != null) writer.close();
108157
}
109158

110-
public static void compare(HipoReader a, HipoReader b) {
159+
public static int compare(HipoReader a, HipoReader b) {
160+
int ret=0;
111161
Event eventA = new Event();
112162
Event eventB = new Event();
113163
while (a.hasNext() && b.hasNext() && (nmax < 1 || nevent < nmax)) {
114164
if (++nevent % 5000 == 0) System.out.println("Analyzed " + nevent + " events");
115165
a.nextEvent(eventA);
116166
b.nextEvent(eventB);
117167
eventA.read(runConfigBank);
118-
compare(eventA, eventB);
168+
ret += compare(eventA, eventB);
119169
}
120170
System.out.println("\n Analyzed " + nevent + " with " + nbadevent + " bad banks");
121171
System.out.println(nbadrow + "/" + nrow + " mismatched rows");
@@ -124,25 +174,34 @@ public static void compare(HipoReader a, HipoReader b) {
124174
System.out.println(name + " " + badEntries.get(name));
125175
}
126176
System.exit(nbadevent + nbadrow + nbadentry);
177+
return ret;
127178
}
128179

129-
public static void compare(Event a, Event b) {
180+
public static int compare(Event a, Event b) {
181+
int ret = 0;
130182
for (int i = 0; i < banksA.size(); i++) {
131183
a.read(banksA.get(i));
132184
b.read(banksB.get(i));
133-
compare(banksA.get(i), banksB.get(i));
185+
if (writer != null) {
186+
event.reset();
187+
event.write(banksA.get(i).getDiff(banksB.get(i)));
188+
writer.addEvent(event);
189+
}
190+
ret += compare(banksA.get(i), banksB.get(i));
134191
}
192+
return ret;
135193
}
136194

137-
public static void compare(SortedBank a, SortedBank b) {
195+
public static int compare(DiffBank a, DiffBank b) {
196+
int ret=0;
138197
if (a.getRows() != b.getRows()) {
139198
System.out.println("========================= Different number of rows:");
140199
runConfigBank.show();
141200
a.show();
142201
b.show();
143202
nbadevent++;
144203
System.out.println("=========================");
145-
return;
204+
return ++ret;
146205
}
147206
int[] rowsA = sortIndex == null ? null : a.getSorted(sortIndex);
148207
int[] rowsB = sortIndex == null ? null : b.getSorted(sortIndex);
@@ -207,11 +266,13 @@ public static void compare(SortedBank a, SortedBank b) {
207266
m.put(elementName, 0);
208267
}
209268
m.put(elementName, m.get(elementName) + 1);
269+
ret++;
210270
}
211271
}
212272
if (mismatch) {
213273
nbadrow++;
214274
}
215275
}
276+
return 0;
216277
}
217278
}

0 commit comments

Comments
 (0)