Skip to content

Commit e457e5b

Browse files
committed
add sorting
1 parent 4aa7a01 commit e457e5b

File tree

1 file changed

+79
-36
lines changed

1 file changed

+79
-36
lines changed

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

Lines changed: 79 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,50 @@
1212

1313
public class HipoDiff {
1414

15+
public static class SortedBank extends Bank {
16+
public SortedBank(Schema s){ super(s); }
17+
/**
18+
* bubble sort by arbitrary number of column indices
19+
* @param index
20+
* @return
21+
*/
22+
public int[] getSorted(int... index) {
23+
int[] rows = new int[getRows()];
24+
for (int row=0; row<rows.length; row++) rows[row] = row;
25+
for (int i=0; i<rows.length-1; i++) {
26+
for (int j=0; j<rows.length-i-1; j++) {
27+
for (int idx : index) {
28+
int i1 = getInt(idx, rows[j]);
29+
int i2 = getInt(idx, rows[j+1]);
30+
if (i1 > i2) {
31+
int tmp = rows[j];
32+
rows[j] = rows[j+1];
33+
rows[j+1] = tmp;
34+
break;
35+
}
36+
else if (i1 < i2) break;
37+
}
38+
}
39+
}
40+
return rows;
41+
}
42+
}
43+
1544
static int nrow = 0;
1645
static int nevent = -1;
1746
static int nentry = 0;
1847
static int nbadevent = 0;
1948
static int nbadrow = 0;
2049
static int nbadentry = 0;
50+
static int[] sortIndex = null;
51+
static int nmax;
2152
static double tolerance;
22-
static boolean verboseMode = false;
23-
static boolean quietMode = false;
24-
static Bank runConfigBank = null;
25-
static SchemaFactory schemaFactory = null;
26-
static ArrayList<Bank> banksA = new ArrayList<>();
27-
static ArrayList<Bank> banksB = new ArrayList<>();
53+
static boolean verboseMode;
54+
static boolean quietMode;
55+
static Bank runConfigBank;
56+
57+
static ArrayList<SortedBank> banksA = new ArrayList<>();
58+
static ArrayList<SortedBank> banksB = new ArrayList<>();
2859
static HashMap<String, HashMap<String,Integer>> badEntries = new HashMap<>();
2960

3061
public static void main(String args[]) {
@@ -35,6 +66,7 @@ public static void main(String args[]) {
3566
op.addOption("-q", null, "quiet mode");
3667
op.addOption("-Q", null, "verbose mode");
3768
op.addOption("-b", null,"name of bank to diff");
69+
op.addOption("-s", null,"sort on column index");
3870
op.setRequiresInputList(true);
3971
op.parse(args);
4072
if (op.getInputList().size() != 2) {
@@ -43,44 +75,52 @@ public static void main(String args[]) {
4375
System.exit(1);
4476
}
4577

78+
if (op.getOption("-s").stringValue() != null) {
79+
String[] stmp = op.getOption("-s").stringValue().split(",");
80+
sortIndex = new int[stmp.length];
81+
for (int i=0; i<stmp.length; i++) sortIndex[i] = Integer.parseInt(stmp[i]);
82+
}
4683
verboseMode = op.getOption("-Q").stringValue() != null;
4784
quietMode = op.getOption("-q").stringValue() != null;
48-
final int nmax = op.getOption("-n").intValue();
85+
nmax = op.getOption("-n").intValue();
4986
tolerance = op.getOption("-t").doubleValue();
5087

5188
HipoReader readerA = new HipoReader();
5289
HipoReader readerB = new HipoReader();
5390
readerA.open(op.getInputList().get(0));
5491
readerB.open(op.getInputList().get(1));
55-
Event eventA = new Event();
56-
Event eventB = new Event();
92+
SchemaFactory sf = readerA.getSchemaFactory();
93+
runConfigBank = new Bank(sf.getSchema("RUN::config"));
5794

58-
schemaFactory = readerA.getSchemaFactory();
59-
runConfigBank = new Bank(schemaFactory.getSchema("RUN::config"));
6095
if (op.getOption("-b").stringValue() == null) {
61-
for (Schema s : schemaFactory.getSchemaList()) {
62-
banksA.add(new Bank(s));
63-
banksB.add(new Bank(s));
96+
for (Schema s : sf.getSchemaList()) {
97+
banksA.add(new SortedBank(s));
98+
banksB.add(new SortedBank(s));
6499
}
65100
}
66101
else {
67-
banksA.add(new Bank(schemaFactory.getSchema(op.getOption("-b").stringValue())));
68-
banksB.add(new Bank(schemaFactory.getSchema(op.getOption("-b").stringValue())));
102+
banksA.add(new SortedBank(sf.getSchema(op.getOption("-b").stringValue())));
103+
banksB.add(new SortedBank(sf.getSchema(op.getOption("-b").stringValue())));
69104
}
70105

71-
while (readerA.hasNext() && readerB.hasNext() && (nmax < 1 || nevent < nmax)) {
106+
compare(readerA, readerB);
107+
}
108+
109+
public static void compare(HipoReader a, HipoReader b) {
110+
Event eventA = new Event();
111+
Event eventB = new Event();
112+
while (a.hasNext() && b.hasNext() && (nmax < 1 || nevent < nmax)) {
72113
if (++nevent % 10000 == 0) System.out.println("Analyzed " + nevent + " events");
73-
readerA.nextEvent(eventA);
74-
readerB.nextEvent(eventB);
114+
a.nextEvent(eventA);
115+
b.nextEvent(eventB);
75116
eventA.read(runConfigBank);
76117
compare(eventA, eventB);
77118
}
78119
System.out.println("\n Analyzed " + nevent + " with " + nbadevent + " bad banks");
79120
System.out.println(nbadrow + "/" + nrow + " mismatched rows");
80121
System.out.println(nbadentry + "/" + nentry + " mismatched entry");
81-
for (String name : badEntries.keySet()) {
122+
for (String name : badEntries.keySet())
82123
System.out.println(name + " " + badEntries.get(name));
83-
}
84124
System.exit(nbadevent + nbadrow + nbadentry);
85125
}
86126

@@ -91,8 +131,8 @@ public static void compare(Event a, Event b) {
91131
compare(banksA.get(i),banksB.get(i));
92132
}
93133
}
94-
95-
public static void compare(Bank a, Bank b) {
134+
135+
public static void compare(SortedBank a, SortedBank b) {
96136

97137
if (a.getRows() != b.getRows()) {
98138
System.out.println("========================= Different number of rows:");
@@ -103,9 +143,13 @@ public static void compare(Bank a, Bank b) {
103143
System.out.println("=========================");
104144
}
105145
else {
106-
for (int i = 0; i < a.getRows(); i++) {
146+
int[] rowsA = sortIndex==null ? null : a.getSorted(sortIndex);
147+
int[] rowsB = sortIndex==null ? null : b.getSorted(sortIndex);
148+
for (int row = 0; row < a.getRows(); row++) {
107149
boolean mismatch = false;
108150
nrow++;
151+
int rowA = sortIndex==null ? row : rowsA[row];
152+
int rowB = sortIndex==null ? row : rowsB[row];
109153
for (int j = 0; j < a.getSchema().getElements(); j++) {
110154
final int type = a.getSchema().getType(j);
111155
final String name = a.getSchema().getElementName(j);
@@ -114,29 +158,29 @@ public static void compare(Bank a, Bank b) {
114158
nentry++;
115159
switch (type) {
116160
case 1:
117-
if (a.getByte(name, i) != b.getByte(name, i)) {
161+
if (a.getByte(name, rowA) != b.getByte(name, rowB)) {
118162
element = j;
119-
values += a.getByte(name, i) + "/" + b.getByte(name, i);
163+
values += a.getByte(name, rowA) + "/" + b.getByte(name, rowB);
120164
}
121165
break;
122166
case 2:
123-
if (a.getShort(name, i) != b.getShort(name, i)) {
167+
if (a.getShort(name, rowA) != b.getShort(name, rowB)) {
124168
element = j;
125-
values += a.getShort(name, i) + "/" + b.getShort(name, i);
169+
values += a.getShort(name, rowA) + "/" + b.getShort(name, rowB);
126170
}
127171
break;
128172
case 3:
129-
if (a.getInt(name, i) != b.getInt(name, i)) {
173+
if (a.getInt(name, rowA) != b.getInt(name, rowB)) {
130174
element = j;
131-
values += a.getInt(name, i) + "/" + b.getInt(name, i);
175+
values += a.getInt(name, rowA) + "/" + b.getInt(name, rowB);
132176
}
133177
break;
134178
case 4:
135-
if ((!Double.isNaN(a.getFloat(name, i)) || !Double.isNaN(b.getFloat(name, i)))
136-
&& (!Double.isInfinite(a.getFloat(name, i)) || !Double.isInfinite(b.getFloat(name, i)))
137-
&& Math.abs(a.getFloat(name, i) - b.getFloat(name, i)) > tolerance) {
179+
if ((!Double.isNaN(a.getFloat(name, rowA)) || !Double.isNaN(b.getFloat(name, rowB)))
180+
&& (!Double.isInfinite(a.getFloat(name, rowA)) || !Double.isInfinite(b.getFloat(name, rowB)))
181+
&& Math.abs(a.getFloat(name, rowA) - b.getFloat(name, rowB)) > tolerance) {
138182
element = j;
139-
values += a.getFloat(name, i) + "/" + b.getFloat(name, i);
183+
values += a.getFloat(name, rowA) + "/" + b.getFloat(name, rowB);
140184
}
141185
break;
142186
}
@@ -148,7 +192,7 @@ public static void compare(Bank a, Bank b) {
148192
}
149193
if (!quietMode) {
150194
System.out.println(a.getSchema().getName()+" mismatch at event " + runConfigBank.getInt("event", 0)
151-
+ " in row " + i + " for variable " + name + " with values " + values);
195+
+ " in row " + row + " for variable " + name + " with values " + values);
152196
}
153197
mismatch = true;
154198
nbadentry++;
@@ -164,5 +208,4 @@ public static void compare(Bank a, Bank b) {
164208
}
165209
}
166210
}
167-
168211
}

0 commit comments

Comments
 (0)